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