key_gen.c 3.8 KB

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