[PATCH v2 30/30] crypto: sa2ul - add SA3UL_CM AES support

From: Manorit Chawdhry

Date: Wed Nov 04 2026 - 00:49:18 EST


SA3UL_CM revision 3.1 needs an extra key-schedule round for AES-256
decryption and a random AES-ECB operation after power-up to activate
its side-channel countermeasures.

Derive round 15 in a separate temporary and wipe it after copying,
rather than overflowing the expanded-key array.

Prime the hardware through sa_run() before registering algorithms,
so self-tests cannot precede the random operation. Use a temporary
context and a callback that releases DMA state and wakes the waiter.
Use a DMA result callback so an aborted or short transfer fails probe
instead of being treated as successful priming. Bound the wait to ten
seconds and terminate DMA synchronously on timeout before releasing
the temporary context, buffers and stack-backed completion state.

Signed-off-by: Manorit Chawdhry <m-chawdhry@xxxxxx>
---
drivers/crypto/sa2ul.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++-
drivers/crypto/sa2ul.h | 2 +
2 files changed, 156 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/sa2ul.c b/drivers/crypto/sa2ul.c
index d7ea61452577..59e1b049c648 100644
--- a/drivers/crypto/sa2ul.c
+++ b/drivers/crypto/sa2ul.c
@@ -13,6 +13,7 @@
#include <linux/dmaengine.h>
#include <linux/dmapool.h>
#include <linux/interrupt.h>
+#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/mempool.h>
#include <linux/module.h>
@@ -21,6 +22,8 @@
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
+#include <linux/random.h>
+#include <linux/slab.h>

#include <crypto/aes.h>
#include <crypto/algapi.h>
@@ -401,11 +404,22 @@ static void sa_prepare_iopads(struct algo_data *data, const u8 *key,
memzero_explicit(&k, sizeof(k));
}

+/* SubWord using kernel AES S-box */
+static inline u32 sa_aes_subword(u32 w)
+{
+ return (u32)crypto_aes_sbox[w & 0xff] |
+ ((u32)crypto_aes_sbox[(w >> 8) & 0xff] << 8) |
+ ((u32)crypto_aes_sbox[(w >> 16) & 0xff] << 16) |
+ ((u32)crypto_aes_sbox[(w >> 24) & 0xff] << 24);
+}
+
/* Derive the inverse key used in AES-CBC decryption operation */
-static inline int sa_aes_inv_key(u8 *inv_key, const u8 *key, u16 key_sz)
+static inline int sa_aes_inv_key(u8 *inv_key, const u8 *key, u16 key_sz,
+ bool aes_cm)
{
struct crypto_aes_ctx ctx __cleanup(aes_zeroize_ctx);
int key_pos;
+ u32 round15[4];

if (aes_expandkey(&ctx, key, key_sz)) {
dev_err(sa_k3_dev, "%s: bad key len(%d)\n", __func__, key_sz);
@@ -418,6 +432,24 @@ static inline int sa_aes_inv_key(u8 *inv_key, const u8 *key, u16 key_sz)
ctx.key_enc[53] = ctx.key_enc[52] ^ ctx.key_enc[47];
}

+ /*
+ * SA3UL CM hardware variant needs an extra round 15 for AES-256.
+ * ctx.key_enc[] only holds 60 words (rounds 0-14), so round 15 is
+ * computed into a local array.
+ */
+ if (key_sz == AES_KEYSIZE_256 && aes_cm) {
+ round15[0] = sa_aes_subword(ctx.key_enc[59]) ^ ctx.key_enc[52];
+ round15[1] = round15[0] ^ ctx.key_enc[53];
+ round15[2] = round15[1] ^ ctx.key_enc[54];
+ round15[3] = round15[2] ^ ctx.key_enc[55];
+
+ /* Decrypt key = round14 || round15 */
+ memcpy(inv_key, &ctx.key_enc[56], 16);
+ memcpy(inv_key + 16, round15, 16);
+ memzero_explicit(round15, sizeof(round15));
+ return 0;
+ }
+
/* Based crypto_aes_expand_key logic */
switch (key_sz) {
case AES_KEYSIZE_128:
@@ -457,7 +489,10 @@ static int sa_set_sc_enc(struct algo_data *ad, const u8 *key, u16 key_sz,

/* For AES-CBC decryption get the inverse key */
if (ad->inv_key && !enc) {
- if (sa_aes_inv_key(&sc_buf[SC_ENC_KEY_OFFSET], key, key_sz))
+ struct sa_crypto_data *dev_data = dev_get_drvdata(sa_k3_dev);
+ bool aes_cm = dev_data ? dev_data->aes_cm : false;
+
+ if (sa_aes_inv_key(&sc_buf[SC_ENC_KEY_OFFSET], key, key_sz, aes_cm))
return -EINVAL;
/* For all other cases: key is used */
} else {
@@ -2474,6 +2509,114 @@ static const struct of_device_id of_match[] = {
};
MODULE_DEVICE_TABLE(of, of_match);

+/*
+ * SA3UL_CM (aes_cm) requires a one-time TRNG-seeded AES-ECB priming
+ * operation after every power-up/reset before its side-channel
+ * countermeasures are considered active.
+ */
+static void sa_aes_cm_trng_prime_callback(void *data,
+ const struct dmaengine_result *result)
+{
+ struct sa_rx_data *rxd = data;
+ struct crypto_async_request *base = rxd->req;
+ int err = result->result == DMA_TRANS_NOERROR && !result->residue ? 0 : -EIO;
+
+ /* sa_run() bypasses crypto_engine, so complete the wait directly. */
+ sa_sync_from_device(rxd);
+ sa_free_sa_rx_data(rxd);
+ crypto_req_done(base->data, err);
+}
+
+static int sa_aes_cm_trng_prime(struct sa_crypto_data *dev_data)
+{
+ struct sa_tfm_ctx *ctx;
+ struct algo_data ad = { 0 };
+ struct sa_cmdl_cfg cfg = { 0 };
+ struct crypto_async_request base = { 0 };
+ struct crypto_wait wait;
+ struct sa_req req = { 0 };
+ struct scatterlist sg;
+ u8 key[AES_KEYSIZE_128] = { 0 };
+ u8 *pt = NULL;
+ bool ctx_ready = false;
+ int cmdl_len;
+ int ret;
+
+ ctx = kzalloc_obj(*ctx, GFP_KERNEL);
+ if (!ctx)
+ return -ENOMEM;
+
+ ctx->dev_data = dev_data;
+ ret = sa_init_ctx_info(&ctx->enc, dev_data);
+ if (ret)
+ goto out;
+ ctx_ready = true;
+
+ ad.enc_eng.eng_id = SA_ENG_ID_EM1;
+ ad.enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
+ ad.mci_enc = mci_ecb_enc_array[0];
+
+ get_random_bytes(key, sizeof(key));
+ pt = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
+ if (!pt) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ get_random_bytes(pt, AES_BLOCK_SIZE);
+
+ ret = sa_init_sc(&ctx->enc, dev_data->match_data, key, sizeof(key),
+ NULL, 0, &ad, 1, &ctx->enc.epib[1]);
+ if (ret)
+ goto out;
+
+ cfg.enc = true;
+ cfg.enc_eng_id = ad.enc_eng.eng_id;
+ cfg.iv_size = 0;
+ cmdl_len = sa_format_cmdl_gen(&cfg, (u8 *)ctx->enc.cmdl,
+ &ctx->enc.cmdl_upd_info);
+ if (cmdl_len <= 0 || cmdl_len > SA_MAX_CMDL_WORDS * sizeof(u32)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ ctx->enc.cmdl_size = cmdl_len;
+
+ sg_init_one(&sg, pt, AES_BLOCK_SIZE);
+ crypto_init_wait(&wait);
+ base.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
+ base.complete = crypto_req_done;
+ base.data = &wait;
+
+ req.size = AES_BLOCK_SIZE;
+ req.enc_size = AES_BLOCK_SIZE;
+ req.type = CRYPTO_ALG_TYPE_SKCIPHER;
+ req.base = &base;
+ req.ctx = ctx;
+ req.enc = true;
+ req.src = &sg;
+ req.dst = &sg;
+ req.callback = sa_aes_cm_trng_prime_callback;
+
+ ret = sa_run(&req);
+ if (ret == -EINPROGRESS) {
+ if (!wait_for_completion_timeout(&wait.completion,
+ msecs_to_jiffies(10000))) {
+ /* Stop DMA and late callbacks before releasing the stack wait. */
+ sa_cancel_dma(dev_data);
+ ret = -ETIMEDOUT;
+ } else {
+ ret = wait.err;
+ }
+ }
+
+out:
+ if (ctx_ready)
+ sa_free_ctx_info(&ctx->enc, dev_data);
+ memzero_explicit(key, sizeof(key));
+ kfree_sensitive(pt);
+ kfree_sensitive(ctx);
+ return ret;
+}
+
static int sa_ul_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
@@ -2514,6 +2657,7 @@ static int sa_ul_probe(struct platform_device *pdev)
major = FIELD_GET(SA_REVISION_MAJOR_MASK, rev);
minor = FIELD_GET(SA_REVISION_MINOR_MASK, rev);
dev_info(dev, "SAxUL_VERSION: %u.%u\n", major, minor);
+ dev_data->aes_cm = (major == 3 && minor == 1);

ret = sa_init_mem(dev_data);
if (ret)
@@ -2545,6 +2689,14 @@ static int sa_ul_probe(struct platform_device *pdev)
if (val & ~status)
writel_relaxed(val, saul_base + SA_ENGINE_ENABLE_CONTROL);

+ if (dev_data->aes_cm) {
+ ret = sa_aes_cm_trng_prime(dev_data);
+ if (ret) {
+ dev_err_probe(dev, ret, "SA3UL CM TRNG priming failed\n");
+ goto free_engine;
+ }
+ }
+
sa_register_algos(dev_data);

ret = of_platform_populate(node, NULL, NULL, dev);
diff --git a/drivers/crypto/sa2ul.h b/drivers/crypto/sa2ul.h
index ba3fb91daaa4..199eff4467d7 100644
--- a/drivers/crypto/sa2ul.h
+++ b/drivers/crypto/sa2ul.h
@@ -194,6 +194,8 @@ struct sa_crypto_data {
spinlock_t request_lock; /* request admission */
struct mutex dma_lock; /* DMA setup and cancellation */
bool stopping;
+ /* true if HW is SA3UL_CM (SA_REVISION major=3 minor=1) */
+ bool aes_cm;
};

/**

--
2.43.0