key_gen.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * CAAM/SEC 4.x functions for handling key-generation jobs
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. */
  7. #include "compat.h"
  8. #include "jr.h"
  9. #include "error.h"
  10. #include "desc_constr.h"
  11. #include "key_gen.h"
  12. /**
  13. * split_key_len - Compute MDHA split key length for a given algorithm
  14. * @hash: Hashing algorithm selection, one of OP_ALG_ALGSEL_* - MD5, SHA1,
  15. * SHA224, SHA384, SHA512.
  16. *
  17. * Return: MDHA split key length
  18. */
  19. static inline u32 split_key_len(u32 hash)
  20. {
  21. /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
  22. static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
  23. u32 idx;
  24. idx = (hash & OP_ALG_ALGSEL_SUBMASK) >> OP_ALG_ALGSEL_SHIFT;
  25. return (u32)(mdpadlen[idx] * 2);
  26. }
  27. /**
  28. * split_key_pad_len - Compute MDHA split key pad length for a given algorithm
  29. * @hash: Hashing algorithm selection, one of OP_ALG_ALGSEL_* - MD5, SHA1,
  30. * SHA224, SHA384, SHA512.
  31. *
  32. * Return: MDHA split key pad length
  33. */
  34. static inline u32 split_key_pad_len(u32 hash)
  35. {
  36. return ALIGN(split_key_len(hash), 16);
  37. }
  38. void split_key_done(struct device *dev, u32 *desc, u32 err,
  39. void *context)
  40. {
  41. struct split_key_result *res = context;
  42. #ifdef DEBUG
  43. dev_err(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  44. #endif
  45. if (err)
  46. caam_jr_strstatus(dev, err);
  47. res->err = err;
  48. complete(&res->completion);
  49. }
  50. EXPORT_SYMBOL(split_key_done);
  51. /*
  52. get a split ipad/opad key
  53. Split key generation-----------------------------------------------
  54. [00] 0xb0810008 jobdesc: stidx=1 share=never len=8
  55. [01] 0x04000014 key: class2->keyreg len=20
  56. @0xffe01000
  57. [03] 0x84410014 operation: cls2-op sha1 hmac init dec
  58. [04] 0x24940000 fifold: class2 msgdata-last2 len=0 imm
  59. [05] 0xa4000001 jump: class2 local all ->1 [06]
  60. [06] 0x64260028 fifostr: class2 mdsplit-jdk len=40
  61. @0xffe04000
  62. */
  63. int gen_split_key(struct device *jrdev, u8 *key_out,
  64. struct alginfo * const adata, const u8 *key_in, u32 keylen,
  65. int max_keylen)
  66. {
  67. u32 *desc;
  68. struct split_key_result result;
  69. dma_addr_t dma_addr_in, dma_addr_out;
  70. int ret = -ENOMEM;
  71. adata->keylen = split_key_len(adata->algtype & OP_ALG_ALGSEL_MASK);
  72. adata->keylen_pad = split_key_pad_len(adata->algtype &
  73. OP_ALG_ALGSEL_MASK);
  74. #ifdef DEBUG
  75. dev_err(jrdev, "split keylen %d split keylen padded %d\n",
  76. adata->keylen, adata->keylen_pad);
  77. print_hex_dump(KERN_ERR, "ctx.key@" __stringify(__LINE__)": ",
  78. DUMP_PREFIX_ADDRESS, 16, 4, key_in, keylen, 1);
  79. #endif
  80. if (adata->keylen_pad > max_keylen)
  81. return -EINVAL;
  82. desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
  83. if (!desc) {
  84. dev_err(jrdev, "unable to allocate key input memory\n");
  85. return ret;
  86. }
  87. dma_addr_in = dma_map_single(jrdev, (void *)key_in, keylen,
  88. DMA_TO_DEVICE);
  89. if (dma_mapping_error(jrdev, dma_addr_in)) {
  90. dev_err(jrdev, "unable to map key input memory\n");
  91. goto out_free;
  92. }
  93. dma_addr_out = dma_map_single(jrdev, key_out, adata->keylen_pad,
  94. DMA_FROM_DEVICE);
  95. if (dma_mapping_error(jrdev, dma_addr_out)) {
  96. dev_err(jrdev, "unable to map key output memory\n");
  97. goto out_unmap_in;
  98. }
  99. init_job_desc(desc, 0);
  100. append_key(desc, dma_addr_in, keylen, CLASS_2 | KEY_DEST_CLASS_REG);
  101. /* Sets MDHA up into an HMAC-INIT */
  102. append_operation(desc, (adata->algtype & OP_ALG_ALGSEL_MASK) |
  103. OP_ALG_AAI_HMAC | OP_TYPE_CLASS2_ALG | OP_ALG_DECRYPT |
  104. OP_ALG_AS_INIT);
  105. /*
  106. * do a FIFO_LOAD of zero, this will trigger the internal key expansion
  107. * into both pads inside MDHA
  108. */
  109. append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB |
  110. FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2);
  111. /*
  112. * FIFO_STORE with the explicit split-key content store
  113. * (0x26 output type)
  114. */
  115. append_fifo_store(desc, dma_addr_out, adata->keylen,
  116. LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);
  117. #ifdef DEBUG
  118. print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
  119. DUMP_PREFIX_ADDRESS, 16, 4, key_in, keylen, 1);
  120. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  121. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  122. #endif
  123. result.err = 0;
  124. init_completion(&result.completion);
  125. ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
  126. if (!ret) {
  127. /* in progress */
  128. wait_for_completion_interruptible(&result.completion);
  129. ret = result.err;
  130. #ifdef DEBUG
  131. print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
  132. DUMP_PREFIX_ADDRESS, 16, 4, key_out,
  133. adata->keylen_pad, 1);
  134. #endif
  135. }
  136. dma_unmap_single(jrdev, dma_addr_out, adata->keylen_pad,
  137. DMA_FROM_DEVICE);
  138. out_unmap_in:
  139. dma_unmap_single(jrdev, dma_addr_in, keylen, DMA_TO_DEVICE);
  140. out_free:
  141. kfree(desc);
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(gen_split_key);