Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: caam - add in-kernel interface for blob generator

The NXP Cryptographic Acceleration and Assurance Module (CAAM)
can be used to protect user-defined data across system reboot:

- When the system is fused and boots into secure state, the master
key is a unique never-disclosed device-specific key
- random key is encrypted by key derived from master key
- data is encrypted using the random key
- encrypted data and its encrypted random key are stored alongside
- This blob can now be safely stored in non-volatile memory

On next power-on:
- blob is loaded into CAAM
- CAAM writes decrypted data either into memory or key register

Add functions to realize encrypting and decrypting into memory alongside
the CAAM driver.

They will be used in a later commit as a source for the trusted key
seal/unseal mechanism.

Reviewed-by: David Gstir <david@sigma-star.at>
Reviewed-by: Pankaj Gupta <pankaj.gupta@nxp.com>
Tested-by: Tim Harvey <tharvey@gateworks.com>
Tested-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>
Tested-by: Pankaj Gupta <pankaj.gupta@nxp.com>
Tested-by: Michael Walle <michael@walle.cc> # on ls1028a (non-E and E)
Tested-by: John Ernberg <john.ernberg@actia.se> # iMX8QXP
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

authored by

Ahmad Fatoum and committed by
Jarkko Sakkinen
007c3ff1 7a0e7d52

+289
+3
drivers/crypto/caam/Kconfig
··· 151 151 Selecting this will register the SEC4 hardware rng to 152 152 the hw_random API for supplying the kernel entropy pool. 153 153 154 + config CRYPTO_DEV_FSL_CAAM_BLOB_GEN 155 + bool 156 + 154 157 endif # CRYPTO_DEV_FSL_CAAM_JR 155 158 156 159 endif # CRYPTO_DEV_FSL_CAAM
+1
drivers/crypto/caam/Makefile
··· 21 21 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o 22 22 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o 23 23 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caampkc.o pkc_desc.o 24 + caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_BLOB_GEN) += blob_gen.o 24 25 25 26 caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o 26 27 ifneq ($(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI),)
+182
drivers/crypto/caam/blob_gen.c
··· 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 + */ 6 + 7 + #define pr_fmt(fmt) "caam blob_gen: " fmt 8 + 9 + #include <linux/device.h> 10 + #include <soc/fsl/caam-blob.h> 11 + 12 + #include "compat.h" 13 + #include "desc_constr.h" 14 + #include "desc.h" 15 + #include "error.h" 16 + #include "intern.h" 17 + #include "jr.h" 18 + #include "regs.h" 19 + 20 + #define CAAM_BLOB_DESC_BYTES_MAX \ 21 + /* Command to initialize & stating length of descriptor */ \ 22 + (CAAM_CMD_SZ + \ 23 + /* Command to append the key-modifier + key-modifier data */ \ 24 + CAAM_CMD_SZ + CAAM_BLOB_KEYMOD_LENGTH + \ 25 + /* Command to include input key + pointer to the input key */ \ 26 + CAAM_CMD_SZ + CAAM_PTR_SZ_MAX + \ 27 + /* Command to include output key + pointer to the output key */ \ 28 + CAAM_CMD_SZ + CAAM_PTR_SZ_MAX + \ 29 + /* Command describing the operation to perform */ \ 30 + CAAM_CMD_SZ) 31 + 32 + struct caam_blob_priv { 33 + struct device jrdev; 34 + }; 35 + 36 + struct caam_blob_job_result { 37 + int err; 38 + struct completion completion; 39 + }; 40 + 41 + static void caam_blob_job_done(struct device *dev, u32 *desc, u32 err, void *context) 42 + { 43 + struct caam_blob_job_result *res = context; 44 + int ecode = 0; 45 + 46 + dev_dbg(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 47 + 48 + if (err) 49 + ecode = caam_jr_strstatus(dev, err); 50 + 51 + res->err = ecode; 52 + 53 + /* 54 + * Upon completion, desc points to a buffer containing a CAAM job 55 + * descriptor which encapsulates data into an externally-storable 56 + * blob. 57 + */ 58 + complete(&res->completion); 59 + } 60 + 61 + int caam_process_blob(struct caam_blob_priv *priv, 62 + struct caam_blob_info *info, bool encap) 63 + { 64 + struct caam_blob_job_result testres; 65 + struct device *jrdev = &priv->jrdev; 66 + dma_addr_t dma_in, dma_out; 67 + int op = OP_PCLID_BLOB; 68 + size_t output_len; 69 + u32 *desc; 70 + int ret; 71 + 72 + if (info->key_mod_len > CAAM_BLOB_KEYMOD_LENGTH) 73 + return -EINVAL; 74 + 75 + if (encap) { 76 + op |= OP_TYPE_ENCAP_PROTOCOL; 77 + output_len = info->input_len + CAAM_BLOB_OVERHEAD; 78 + } else { 79 + op |= OP_TYPE_DECAP_PROTOCOL; 80 + output_len = info->input_len - CAAM_BLOB_OVERHEAD; 81 + } 82 + 83 + desc = kzalloc(CAAM_BLOB_DESC_BYTES_MAX, GFP_KERNEL | GFP_DMA); 84 + if (!desc) 85 + return -ENOMEM; 86 + 87 + dma_in = dma_map_single(jrdev, info->input, info->input_len, 88 + DMA_TO_DEVICE); 89 + if (dma_mapping_error(jrdev, dma_in)) { 90 + dev_err(jrdev, "unable to map input DMA buffer\n"); 91 + ret = -ENOMEM; 92 + goto out_free; 93 + } 94 + 95 + dma_out = dma_map_single(jrdev, info->output, output_len, 96 + DMA_FROM_DEVICE); 97 + if (dma_mapping_error(jrdev, dma_out)) { 98 + dev_err(jrdev, "unable to map output DMA buffer\n"); 99 + ret = -ENOMEM; 100 + goto out_unmap_in; 101 + } 102 + 103 + /* 104 + * A data blob is encrypted using a blob key (BK); a random number. 105 + * The BK is used as an AES-CCM key. The initial block (B0) and the 106 + * initial counter (Ctr0) are generated automatically and stored in 107 + * Class 1 Context DWords 0+1+2+3. The random BK is stored in the 108 + * Class 1 Key Register. Operation Mode is set to AES-CCM. 109 + */ 110 + 111 + init_job_desc(desc, 0); 112 + append_key_as_imm(desc, info->key_mod, info->key_mod_len, 113 + info->key_mod_len, CLASS_2 | KEY_DEST_CLASS_REG); 114 + append_seq_in_ptr_intlen(desc, dma_in, info->input_len, 0); 115 + append_seq_out_ptr_intlen(desc, dma_out, output_len, 0); 116 + append_operation(desc, op); 117 + 118 + print_hex_dump_debug("data@"__stringify(__LINE__)": ", 119 + DUMP_PREFIX_ADDRESS, 16, 1, info->input, 120 + info->input_len, false); 121 + print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 122 + DUMP_PREFIX_ADDRESS, 16, 1, desc, 123 + desc_bytes(desc), false); 124 + 125 + testres.err = 0; 126 + init_completion(&testres.completion); 127 + 128 + ret = caam_jr_enqueue(jrdev, desc, caam_blob_job_done, &testres); 129 + if (ret == -EINPROGRESS) { 130 + wait_for_completion(&testres.completion); 131 + ret = testres.err; 132 + print_hex_dump_debug("output@"__stringify(__LINE__)": ", 133 + DUMP_PREFIX_ADDRESS, 16, 1, info->output, 134 + output_len, false); 135 + } 136 + 137 + if (ret == 0) 138 + info->output_len = output_len; 139 + 140 + dma_unmap_single(jrdev, dma_out, output_len, DMA_FROM_DEVICE); 141 + out_unmap_in: 142 + dma_unmap_single(jrdev, dma_in, info->input_len, DMA_TO_DEVICE); 143 + out_free: 144 + kfree(desc); 145 + 146 + return ret; 147 + } 148 + EXPORT_SYMBOL(caam_process_blob); 149 + 150 + struct caam_blob_priv *caam_blob_gen_init(void) 151 + { 152 + struct caam_drv_private *ctrlpriv; 153 + struct device *jrdev; 154 + 155 + /* 156 + * caam_blob_gen_init() may expectedly fail with -ENODEV, e.g. when 157 + * CAAM driver didn't probe or when SoC lacks BLOB support. An 158 + * error would be harsh in this case, so we stick to info level. 159 + */ 160 + 161 + jrdev = caam_jr_alloc(); 162 + if (IS_ERR(jrdev)) { 163 + pr_info("job ring requested, but none currently available\n"); 164 + return ERR_PTR(-ENODEV); 165 + } 166 + 167 + ctrlpriv = dev_get_drvdata(jrdev->parent); 168 + if (!ctrlpriv->blob_present) { 169 + dev_info(jrdev, "no hardware blob generation support\n"); 170 + caam_jr_free(jrdev); 171 + return ERR_PTR(-ENODEV); 172 + } 173 + 174 + return container_of(jrdev, struct caam_blob_priv, jrdev); 175 + } 176 + EXPORT_SYMBOL(caam_blob_gen_init); 177 + 178 + void caam_blob_gen_exit(struct caam_blob_priv *priv) 179 + { 180 + caam_jr_free(&priv->jrdev); 181 + } 182 + EXPORT_SYMBOL(caam_blob_gen_exit);
+103
include/soc/fsl/caam-blob.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Copyright (C) 2020 Pengutronix, Ahmad Fatoum <kernel@pengutronix.de> 4 + */ 5 + 6 + #ifndef __CAAM_BLOB_GEN 7 + #define __CAAM_BLOB_GEN 8 + 9 + #include <linux/types.h> 10 + #include <linux/errno.h> 11 + 12 + #define CAAM_BLOB_KEYMOD_LENGTH 16 13 + #define CAAM_BLOB_OVERHEAD (32 + 16) 14 + #define CAAM_BLOB_MAX_LEN 4096 15 + 16 + struct caam_blob_priv; 17 + 18 + /** 19 + * struct caam_blob_info - information for CAAM blobbing 20 + * @input: pointer to input buffer (must be DMAable) 21 + * @input_len: length of @input buffer in bytes. 22 + * @output: pointer to output buffer (must be DMAable) 23 + * @output_len: length of @output buffer in bytes. 24 + * @key_mod: key modifier 25 + * @key_mod_len: length of @key_mod in bytes. 26 + * May not exceed %CAAM_BLOB_KEYMOD_LENGTH 27 + */ 28 + struct caam_blob_info { 29 + void *input; 30 + size_t input_len; 31 + 32 + void *output; 33 + size_t output_len; 34 + 35 + const void *key_mod; 36 + size_t key_mod_len; 37 + }; 38 + 39 + /** 40 + * caam_blob_gen_init - initialize blob generation 41 + * Return: pointer to new &struct caam_blob_priv instance on success 42 + * and ``ERR_PTR(-ENODEV)`` if CAAM has no hardware blobbing support 43 + * or no job ring could be allocated. 44 + */ 45 + struct caam_blob_priv *caam_blob_gen_init(void); 46 + 47 + /** 48 + * caam_blob_gen_exit - free blob generation resources 49 + * @priv: instance returned by caam_blob_gen_init() 50 + */ 51 + void caam_blob_gen_exit(struct caam_blob_priv *priv); 52 + 53 + /** 54 + * caam_process_blob - encapsulate or decapsulate blob 55 + * @priv: instance returned by caam_blob_gen_init() 56 + * @info: pointer to blobbing info describing key, blob and 57 + * key modifier buffers. 58 + * @encap: true for encapsulation, false for decapsulation 59 + * 60 + * Return: %0 and sets ``info->output_len`` on success and a negative 61 + * error code otherwise. 62 + */ 63 + int caam_process_blob(struct caam_blob_priv *priv, 64 + struct caam_blob_info *info, bool encap); 65 + 66 + /** 67 + * caam_encap_blob - encapsulate blob 68 + * @priv: instance returned by caam_blob_gen_init() 69 + * @info: pointer to blobbing info describing input key, 70 + * output blob and key modifier buffers. 71 + * 72 + * Return: %0 and sets ``info->output_len`` on success and 73 + * a negative error code otherwise. 74 + */ 75 + static inline int caam_encap_blob(struct caam_blob_priv *priv, 76 + struct caam_blob_info *info) 77 + { 78 + if (info->output_len < info->input_len + CAAM_BLOB_OVERHEAD) 79 + return -EINVAL; 80 + 81 + return caam_process_blob(priv, info, true); 82 + } 83 + 84 + /** 85 + * caam_decap_blob - decapsulate blob 86 + * @priv: instance returned by caam_blob_gen_init() 87 + * @info: pointer to blobbing info describing output key, 88 + * input blob and key modifier buffers. 89 + * 90 + * Return: %0 and sets ``info->output_len`` on success and 91 + * a negative error code otherwise. 92 + */ 93 + static inline int caam_decap_blob(struct caam_blob_priv *priv, 94 + struct caam_blob_info *info) 95 + { 96 + if (info->input_len < CAAM_BLOB_OVERHEAD || 97 + info->output_len < info->input_len - CAAM_BLOB_OVERHEAD) 98 + return -EINVAL; 99 + 100 + return caam_process_blob(priv, info, false); 101 + } 102 + 103 + #endif