blob_gen.c 5.3 KB

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