blob_gen.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Pengutronix, Steffen Trumtrar <[email protected]>
  4. * Copyright (C) 2021 Pengutronix, Ahmad Fatoum <[email protected]>
  5. */
  6. #define pr_fmt(fmt) "caam blob_gen: " fmt
  7. #include <linux/device.h>
  8. #include <soc/fsl/caam-blob.h>
  9. #include "compat.h"
  10. #include "desc_constr.h"
  11. #include "desc.h"
  12. #include "error.h"
  13. #include "intern.h"
  14. #include "jr.h"
  15. #include "regs.h"
  16. #define CAAM_BLOB_DESC_BYTES_MAX \
  17. /* Command to initialize & stating length of descriptor */ \
  18. (CAAM_CMD_SZ + \
  19. /* Command to append the key-modifier + key-modifier data */ \
  20. CAAM_CMD_SZ + CAAM_BLOB_KEYMOD_LENGTH + \
  21. /* Command to include input key + pointer to the input key */ \
  22. CAAM_CMD_SZ + CAAM_PTR_SZ_MAX + \
  23. /* Command to include output key + pointer to the output key */ \
  24. CAAM_CMD_SZ + CAAM_PTR_SZ_MAX + \
  25. /* Command describing the operation to perform */ \
  26. CAAM_CMD_SZ)
  27. struct caam_blob_priv {
  28. struct device jrdev;
  29. };
  30. struct caam_blob_job_result {
  31. int err;
  32. struct completion completion;
  33. };
  34. static void caam_blob_job_done(struct device *dev, u32 *desc, u32 err, void *context)
  35. {
  36. struct caam_blob_job_result *res = context;
  37. int ecode = 0;
  38. dev_dbg(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  39. if (err)
  40. ecode = caam_jr_strstatus(dev, err);
  41. res->err = ecode;
  42. /*
  43. * Upon completion, desc points to a buffer containing a CAAM job
  44. * descriptor which encapsulates data into an externally-storable
  45. * blob.
  46. */
  47. complete(&res->completion);
  48. }
  49. int caam_process_blob(struct caam_blob_priv *priv,
  50. struct caam_blob_info *info, bool encap)
  51. {
  52. struct caam_blob_job_result testres;
  53. struct device *jrdev = &priv->jrdev;
  54. dma_addr_t dma_in, dma_out;
  55. int op = OP_PCLID_BLOB;
  56. size_t output_len;
  57. u32 *desc;
  58. int ret;
  59. if (info->key_mod_len > CAAM_BLOB_KEYMOD_LENGTH)
  60. return -EINVAL;
  61. if (encap) {
  62. op |= OP_TYPE_ENCAP_PROTOCOL;
  63. output_len = info->input_len + CAAM_BLOB_OVERHEAD;
  64. } else {
  65. op |= OP_TYPE_DECAP_PROTOCOL;
  66. output_len = info->input_len - CAAM_BLOB_OVERHEAD;
  67. }
  68. desc = kzalloc(CAAM_BLOB_DESC_BYTES_MAX, GFP_KERNEL | GFP_DMA);
  69. if (!desc)
  70. return -ENOMEM;
  71. dma_in = dma_map_single(jrdev, info->input, info->input_len,
  72. DMA_TO_DEVICE);
  73. if (dma_mapping_error(jrdev, dma_in)) {
  74. dev_err(jrdev, "unable to map input DMA buffer\n");
  75. ret = -ENOMEM;
  76. goto out_free;
  77. }
  78. dma_out = dma_map_single(jrdev, info->output, output_len,
  79. DMA_FROM_DEVICE);
  80. if (dma_mapping_error(jrdev, dma_out)) {
  81. dev_err(jrdev, "unable to map output DMA buffer\n");
  82. ret = -ENOMEM;
  83. goto out_unmap_in;
  84. }
  85. /*
  86. * A data blob is encrypted using a blob key (BK); a random number.
  87. * The BK is used as an AES-CCM key. The initial block (B0) and the
  88. * initial counter (Ctr0) are generated automatically and stored in
  89. * Class 1 Context DWords 0+1+2+3. The random BK is stored in the
  90. * Class 1 Key Register. Operation Mode is set to AES-CCM.
  91. */
  92. init_job_desc(desc, 0);
  93. append_key_as_imm(desc, info->key_mod, info->key_mod_len,
  94. info->key_mod_len, CLASS_2 | KEY_DEST_CLASS_REG);
  95. append_seq_in_ptr_intlen(desc, dma_in, info->input_len, 0);
  96. append_seq_out_ptr_intlen(desc, dma_out, output_len, 0);
  97. append_operation(desc, op);
  98. print_hex_dump_debug("data@"__stringify(__LINE__)": ",
  99. DUMP_PREFIX_ADDRESS, 16, 1, info->input,
  100. info->input_len, false);
  101. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  102. DUMP_PREFIX_ADDRESS, 16, 1, desc,
  103. desc_bytes(desc), false);
  104. testres.err = 0;
  105. init_completion(&testres.completion);
  106. ret = caam_jr_enqueue(jrdev, desc, caam_blob_job_done, &testres);
  107. if (ret == -EINPROGRESS) {
  108. wait_for_completion(&testres.completion);
  109. ret = testres.err;
  110. print_hex_dump_debug("output@"__stringify(__LINE__)": ",
  111. DUMP_PREFIX_ADDRESS, 16, 1, info->output,
  112. output_len, false);
  113. }
  114. if (ret == 0)
  115. info->output_len = output_len;
  116. dma_unmap_single(jrdev, dma_out, output_len, DMA_FROM_DEVICE);
  117. out_unmap_in:
  118. dma_unmap_single(jrdev, dma_in, info->input_len, DMA_TO_DEVICE);
  119. out_free:
  120. kfree(desc);
  121. return ret;
  122. }
  123. EXPORT_SYMBOL(caam_process_blob);
  124. struct caam_blob_priv *caam_blob_gen_init(void)
  125. {
  126. struct caam_drv_private *ctrlpriv;
  127. struct device *jrdev;
  128. /*
  129. * caam_blob_gen_init() may expectedly fail with -ENODEV, e.g. when
  130. * CAAM driver didn't probe or when SoC lacks BLOB support. An
  131. * error would be harsh in this case, so we stick to info level.
  132. */
  133. jrdev = caam_jr_alloc();
  134. if (IS_ERR(jrdev)) {
  135. pr_info("job ring requested, but none currently available\n");
  136. return ERR_PTR(-ENODEV);
  137. }
  138. ctrlpriv = dev_get_drvdata(jrdev->parent);
  139. if (!ctrlpriv->blob_present) {
  140. dev_info(jrdev, "no hardware blob generation support\n");
  141. caam_jr_free(jrdev);
  142. return ERR_PTR(-ENODEV);
  143. }
  144. return container_of(jrdev, struct caam_blob_priv, jrdev);
  145. }
  146. EXPORT_SYMBOL(caam_blob_gen_init);
  147. void caam_blob_gen_exit(struct caam_blob_priv *priv)
  148. {
  149. caam_jr_free(&priv->jrdev);
  150. }
  151. EXPORT_SYMBOL(caam_blob_gen_exit);