cbc(paes) decapsulates every blob with the fixed key modifier "SECURE_KEY", the one trusted keys use for their "pk" keys, and is reachable from AF_ALG: a blob made for either can be used through the other by whoever holds CAP_NET_ADMIN or CAP_SYS_ADMIN (with AF_ALG's default restriction), for whatever operation they choose. A kernel user that wants its blobs bound to something of its own, such as the metadata of the key they protect, has no way to say so.
Add a second cbc(paes), cbc-paes-km-caam, flagged CRYPTO_ALG_INTERNAL so that only kernel code asking for that flag can allocate it, as the DCP's paes is, which trusted keys allocate by driver name with that flag; its key names the key modifier to decapsulate with. caam_encap_blob() already takes a caller's key modifier, so a kernel user can now bind its blobs to a value of its own.
Its only user is the keycrypt CAAM backend, which is a proposal, and so is this.
Signed-off-by: Rodolfo Giometti giometti@enneenne.com --- drivers/crypto/caam/caamalg.c | 89 ++++++++++++++++++++++++++---- drivers/crypto/caam/caamalg_desc.c | 3 +- drivers/crypto/caam/caamalg_desc.h | 1 + include/soc/fsl/caam-blob.h | 8 +++ 4 files changed, 89 insertions(+), 12 deletions(-)
diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index d00caa9cdf8e..f34717f312b8 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -125,6 +125,8 @@ struct caam_ctx { dma_addr_t key_dma; u8 protected_key[CAAM_MAX_KEY_SIZE]; dma_addr_t protected_key_dma; + u8 key_mod[CAAM_BLOB_KEYMOD_LENGTH]; + unsigned int key_mod_len; enum dma_data_direction dir; struct device *jrdev; struct alginfo adata; @@ -784,18 +786,19 @@ static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, return 0; }
-static int paes_skcipher_setkey(struct crypto_skcipher *skcipher, - const u8 *key, - unsigned int keylen) +static int paes_set_blob(struct crypto_skcipher *skcipher, + const struct caam_pkey_info *pkey_info, + const u8 *blob, unsigned int keylen, + const u8 *key_mod, unsigned int key_mod_len) { - struct caam_pkey_info *pkey_info = (struct caam_pkey_info *)key; struct caam_ctx *ctx = crypto_skcipher_ctx_dma(skcipher); struct device *jrdev = ctx->jrdev; int err;
- ctx->cdata.key_inline = false; + if (WARN_ON(key_mod_len > CAAM_BLOB_KEYMOD_LENGTH)) + return -EINVAL;
- keylen = keylen - CAAM_PKEY_HEADER; + ctx->cdata.key_inline = false;
if (pkey_info->is_pkey != 1) { dev_dbg(jrdev, "not a protected key\n"); @@ -842,19 +845,52 @@ static int paes_skcipher_setkey(struct crypto_skcipher *skcipher, /* Retrieve the length of blob*/ ctx->cdata.keylen = keylen;
- /* Retrieve the address of the blob */ - ctx->cdata.key_virt = pkey_info->key_buf; - /* set command option */ ctx->cdata.key_cmd_opt = KEY_ENC;
- memcpy(ctx->key, ctx->cdata.key_virt, keylen); + /* The key modifier the blob is decapsulated with, at every job */ + memcpy(ctx->key_mod, key_mod, key_mod_len); + ctx->key_mod_len = key_mod_len; + + /* Retrieve the address of the blob */ + memcpy(ctx->key, blob, keylen); + ctx->cdata.key_virt = ctx->key; dma_sync_single_for_device(jrdev, ctx->key_dma, keylen, DMA_TO_DEVICE); ctx->cdata.key_dma = ctx->key_dma;
return 0; }
+static int paes_skcipher_setkey(struct crypto_skcipher *skcipher, + const u8 *key, + unsigned int keylen) +{ + const struct caam_pkey_info *pkey_info = + (const struct caam_pkey_info *)key; + + return paes_set_blob(skcipher, pkey_info, pkey_info->key_buf, + keylen - CAAM_PKEY_HEADER, KEYMOD, + sizeof(KEYMOD) - 1); +} + +/* + * The internal cbc(paes): the blob comes with the key modifier it was + * encapsulated with, which only kernel code can choose; a caller that + * picks a modifier of its own keeps its blobs apart from those of + * cbc(paes) and of trusted "pk" keys + */ +static int paes_km_skcipher_setkey(struct crypto_skcipher *skcipher, + const u8 *key, + unsigned int keylen) +{ + const struct caam_pkey_info *pkey_info = + (const struct caam_pkey_info *)key; + + return paes_set_blob(skcipher, pkey_info, key + CAAM_PKEY_KM_HEADER, + keylen - CAAM_PKEY_KM_HEADER, + key + CAAM_PKEY_HEADER, CAAM_BLOB_KEYMOD_LENGTH); +} + static int aes_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, unsigned int keylen) { @@ -1398,7 +1434,9 @@ static int init_skcipher_job(struct skcipher_request *req, edesc->desc_dma = desc_dma; edesc->desc_dma_bytes = desc_bytes(desc);
- cnstr_desc_protected_blob_decap(edesc->hw_desc, &ctx->cdata, desc_dma); + cnstr_desc_protected_blob_decap(edesc->hw_desc, &ctx->cdata, + ctx->key_mod, ctx->key_mod_len, + desc_dma); } else { sh_desc = encrypt ? ctx->sh_desc_enc : ctx->sh_desc_dec; ptr = encrypt ? ctx->sh_desc_enc_dma : ctx->sh_desc_dec_dma; @@ -2031,6 +2069,30 @@ static struct caam_skcipher_alg driver_algs[] = { .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, .caam.is_paes = true, }, + { + .skcipher.base = { + .base = { + .cra_name = "cbc(paes)", + .cra_driver_name = "cbc-paes-km-caam", + .cra_blocksize = AES_BLOCK_SIZE, + /* kernel users only: never from AF_ALG */ + .cra_flags = CRYPTO_ALG_INTERNAL, + }, + .setkey = paes_km_skcipher_setkey, + .encrypt = skcipher_encrypt, + .decrypt = skcipher_decrypt, + .min_keysize = AES_MIN_KEY_SIZE + CAAM_BLOB_OVERHEAD + + CAAM_PKEY_KM_HEADER, + .max_keysize = AES_MAX_KEY_SIZE + CAAM_BLOB_OVERHEAD + + CAAM_PKEY_KM_HEADER, + .ivsize = AES_BLOCK_SIZE, + }, + .skcipher.op = { + .do_one_request = skcipher_do_one_req, + }, + .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, + .caam.is_paes = true, + }, { .skcipher.base = { .base = { @@ -3812,6 +3874,11 @@ static int caam_cra_init(struct crypto_skcipher *tfm) * AF_ALG refuses ALG_SET_KEY once an op socket exists, so * concurrent jobs all decapsulate the same blob into the same * black key. + * In-kernel users of the internal cbc(paes) set the key once, + * before any request, as well. It also relies on the black + * key being decapsulated identically by every job, which ECB + * black keys are; CCM ones, with their random nonce, would + * need a buffer per request. */ ctx->protected_key_dma = dma_map_single(ctx->jrdev, ctx->protected_key, diff --git a/drivers/crypto/caam/caamalg_desc.c b/drivers/crypto/caam/caamalg_desc.c index 04c1105eb1f5..db3bbba67c8b 100644 --- a/drivers/crypto/caam/caamalg_desc.c +++ b/drivers/crypto/caam/caamalg_desc.c @@ -1410,6 +1410,7 @@ void cnstr_desc_skcipher_enc_dec(u32 * const desc, struct alginfo *cdata, EXPORT_SYMBOL(cnstr_desc_skcipher_enc_dec);
void cnstr_desc_protected_blob_decap(u32 * const desc, struct alginfo *cdata, + const u8 *key_mod, unsigned int key_mod_len, dma_addr_t next_desc_addr) { u32 protected_store; @@ -1417,7 +1418,7 @@ void cnstr_desc_protected_blob_decap(u32 * const desc, struct alginfo *cdata, init_job_desc(desc, 0);
/* Load key modifier */ - append_load_as_imm(desc, KEYMOD, sizeof(KEYMOD) - 1, + append_load_as_imm(desc, key_mod, key_mod_len, LDST_CLASS_2_CCB | LDST_SRCDST_BYTE_KEY);
append_seq_in_ptr_intlen(desc, cdata->key_dma, diff --git a/drivers/crypto/caam/caamalg_desc.h b/drivers/crypto/caam/caamalg_desc.h index 323490a4a756..ae673b596a6d 100644 --- a/drivers/crypto/caam/caamalg_desc.h +++ b/drivers/crypto/caam/caamalg_desc.h @@ -117,6 +117,7 @@ void cnstr_shdsc_xts_skcipher_encap(u32 * const desc, struct alginfo *cdata); void cnstr_shdsc_xts_skcipher_decap(u32 * const desc, struct alginfo *cdata);
void cnstr_desc_protected_blob_decap(u32 * const desc, struct alginfo *cdata, + const u8 *key_mod, unsigned int key_mod_len, dma_addr_t next_desc);
void cnstr_desc_skcipher_enc_dec(u32 * const desc, struct alginfo *cdata, diff --git a/include/soc/fsl/caam-blob.h b/include/soc/fsl/caam-blob.h index 922f7ec3e231..13c21c7e4c2e 100644 --- a/include/soc/fsl/caam-blob.h +++ b/include/soc/fsl/caam-blob.h @@ -38,6 +38,14 @@ struct caam_pkey_info { /* sizeof struct caam_pkey_info */ #define CAAM_PKEY_HEADER 4
+/* + * A key of the CAAM's internal cbc(paes): struct caam_pkey_info, the + * CAAM_BLOB_KEYMOD_LENGTH-byte key modifier the blob was encapsulated + * with, then the blob + */ +#define CAAM_PKEY_KM_HEADER (CAAM_PKEY_HEADER + \ + CAAM_BLOB_KEYMOD_LENGTH) + /** * struct caam_blob_info - information for CAAM blobbing * @pkey_info: pointer to keep protected key information