key_gen.c 3.3 KB

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