Linux Security Modules development
 help / color / mirror / Atom feed
From: Rodolfo Giometti <giometti@enneenne.com>
To: David Howells <dhowells@redhat.com>,
	Jarkko Sakkinen <jarkko@kernel.org>,
	keyrings@vger.kernel.org
Cc: "Herbert Xu" <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	"Jens Wiklander" <jenswi@kernel.org>,
	"Sumit Garg" <sumit.garg@kernel.org>,
	op-tee@lists.trustedfirmware.org,
	"Horia Geantă" <horia.geanta@nxp.com>,
	"Pankaj Gupta" <pankaj.gupta@nxp.com>,
	"Sahil Malhotra" <sahil.malhotra@nxp.com>,
	"Ahmad Fatoum" <a.fatoum@pengutronix.de>,
	"Richard Weinberger" <richard@nod.at>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Demi Marie Obenour" <demiobenour@gmail.com>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	linux-crypto@vger.kernel.org, linux-integrity@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-api@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	"Rodolfo Giometti" <giometti@enneenne.com>
Subject: [RFC PATCH 7/9] crypto: caam - add an internal cbc(paes) taking the blob's key modifier
Date: Wed, 18 Nov 2026 15:10:47 +0100	[thread overview]
Message-ID: <20261118141047.616989-1-giometti@enneenne.com> (raw)
In-Reply-To: <20261118140421.584086-1-giometti@enneenne.com>

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
-- 
2.43.0


  parent reply	other threads:[~2026-11-18 14:11 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 14:04 [RFC PATCH 0/9] KEYS: keycrypt: keys that user space can use but never read Rodolfo Giometti
2026-11-18 14:04 ` [RFC PATCH 1/9] KEYS: keycrypt: Add a key type for keys user space never reads Rodolfo Giometti
2026-11-18 14:33   ` sashiko-bot
2026-11-18 14:04 ` [RFC PATCH 2/9] KEYS: keycrypt: Add the sw backend Rodolfo Giometti
2026-11-18 14:21   ` sashiko-bot
2026-11-18 14:04 ` [RFC PATCH 3/9] KEYS: keycrypt: Add the OP-TEE backend Rodolfo Giometti
2026-11-18 14:33   ` sashiko-bot
2026-11-18 14:07 ` [RFC PATCH 4/9] docs: keycrypt: document keycrypt keys Rodolfo Giometti
2026-11-18 14:19   ` sashiko-bot
2026-11-18 14:08 ` [RFC PATCH 5/9] MAINTAINERS: add KEYS-KEYCRYPT Rodolfo Giometti
2026-11-18 14:09 ` [RFC PATCH 6/9] KEYS: keycrypt: Add the engine tier Rodolfo Giometti
2026-11-18 14:17   ` sashiko-bot
2026-11-18 14:10 ` Rodolfo Giometti [this message]
2026-11-18 14:24   ` [RFC PATCH 7/9] crypto: caam - add an internal cbc(paes) taking the blob's key modifier sashiko-bot
2026-11-18 14:11 ` [RFC PATCH 8/9] KEYS: keycrypt: Add the CAAM backend Rodolfo Giometti
2026-11-18 14:36   ` sashiko-bot
2026-11-18 14:12 ` [RFC PATCH 9/9] KEYS: keycrypt: Add the aes256-gcm-stream algorithm Rodolfo Giometti
2026-11-18 14:30   ` sashiko-bot
2026-11-18 14:31 ` [RFC PATCH 0/9] KEYS: keycrypt: keys that user space can use but never read Jarkko Sakkinen
2026-11-18 14:39   ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118141047.616989-1-giometti@enneenne.com \
    --to=giometti@enneenne.com \
    --cc=a.fatoum@pengutronix.de \
    --cc=arnd@arndb.de \
    --cc=chleroy@kernel.org \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=demiobenour@gmail.com \
    --cc=dhowells@redhat.com \
    --cc=ebiggers@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horia.geanta@nxp.com \
    --cc=jarkko@kernel.org \
    --cc=jenswi@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=op-tee@lists.trustedfirmware.org \
    --cc=pankaj.gupta@nxp.com \
    --cc=paul@paul-moore.com \
    --cc=rdunlap@infradead.org \
    --cc=richard@nod.at \
    --cc=sahil.malhotra@nxp.com \
    --cc=serge@hallyn.com \
    --cc=skhan@linuxfoundation.org \
    --cc=sumit.garg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox