From patchwork Sat Nov 15 18:51:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 699 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763232782; bh=UKqzz8QKmwf/WU6zZ14IW1oiG6u7uhdLM+gFnOjFN48=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=LxyTKBJcb52Nof5hN0NJDi8C8/w2pivAQ0g/8qiLSKtR/LGEMIgzO21iZ1I5zl11L PCToWlpNeLgNLgWcr9Dx/z+BVmyY9NVdGV5HzV5CFDAdg9KejssxAFqOuE7KLN+I1p m3IWBV7W8tEuuo5k71mse+m/hpuIh7C1FTDyjw6SJmoZSNB87JPT+x0QM/A5/aUbKF Qup4Oj4gvs0SWYFn1C4wSm4w3JQXiaptYwwHa/GwPK1u8cewby1ibuluhdqSJrs31w ywTtk3QPw6FmYnNfjcMRS7WFIabzY1pzEy5thhloMA5kOlcfE7E3LzyX7bWvs/LWH8 HoZTD6/lhfV/w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 8D1566861E for ; Sat, 15 Nov 2025 11:53:02 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id DHdzfDvBOpUj for ; Sat, 15 Nov 2025 11:53:02 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763232782; bh=UKqzz8QKmwf/WU6zZ14IW1oiG6u7uhdLM+gFnOjFN48=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=LxyTKBJcb52Nof5hN0NJDi8C8/w2pivAQ0g/8qiLSKtR/LGEMIgzO21iZ1I5zl11L PCToWlpNeLgNLgWcr9Dx/z+BVmyY9NVdGV5HzV5CFDAdg9KejssxAFqOuE7KLN+I1p m3IWBV7W8tEuuo5k71mse+m/hpuIh7C1FTDyjw6SJmoZSNB87JPT+x0QM/A5/aUbKF Qup4Oj4gvs0SWYFn1C4wSm4w3JQXiaptYwwHa/GwPK1u8cewby1ibuluhdqSJrs31w ywTtk3QPw6FmYnNfjcMRS7WFIabzY1pzEy5thhloMA5kOlcfE7E3LzyX7bWvs/LWH8 HoZTD6/lhfV/w== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7428568614 for ; Sat, 15 Nov 2025 11:53:02 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763232780; bh=AgDX4HzVJu6FXYxQBEtHv3R/CaUsD+2QngxY+vAzkOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=US+vD6v2ek7BlGaejxAkBD0GA5PYe0WfPpjqUJrbhcV+Mwx5aOvfgCAa4Hkgyubyp 6v9xQik8OI0/bc0tqBwSI71QZZIz06p8Gr7AczHm5tF2GLflFcAPn9aHbRKyJZaBK1 oBsidgC5jCX9CygTpy4XcPGlHPWmu0GwGWnZ4aQlpjK8e3XkjqRrv09OhUuXTHtuTL stobbZMpciKHkDrhEdnd6E4GzMCzGL/otzctO9edh4K8BTTcC7Bupt5donl0hRUkCU 3xR1iA9oTXRJmVct+MCawR7ms79RFv3ayXtvdfTnDaEKSwuytaFVYt06dZ/BKStUE7 V55vrRIxXBDAQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1333C685D9; Sat, 15 Nov 2025 11:53:00 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id ConyFTCy2Knx; Sat, 15 Nov 2025 11:53:00 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763232779; bh=ZEPXymEtbhaPl9ElOIW4Xnw3Yj8u6cHGVZUfAQMgjHc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DJaDjYLG3cdRpKpPuuh+YGHtbrmRnnFwpbu9km0ryhZGMXDFDxDOo7FotJFePPm3U gPeJ4oCRZTh4CJ+r2ju+amJod+5Cz8q46gjhjU8VHxSsfk83ihTyr1EzA1iKCpbKF0 0PQvl7+COhmL2e6uK3pTvgBYKbTel1EEOkVeobuGPf1ggeomSxnJvzYVShJr4zgPKw lI/Ph4TBedGCizyIXzXwwsBUf8gKiwaXz/jFUGUJhE7J7f52YT9T8OkfyAkRBdAeo9 Pu2odv55XOG5YB4agJJWpk5lmC0l1qiBFf/TvF+7jpXXAnkb1bYw2BXYcXhY1h7r8o OvJhiKFmjRM8g== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 4787F66654; Sat, 15 Nov 2025 11:52:59 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sat, 15 Nov 2025 11:51:50 -0700 Message-ID: <20251115185212.539268-2-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251115185212.539268-1-sjg@u-boot.org> References: <20251115185212.539268-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: UCJ4GN34FYMHUZWPN2IOLMNPEHGZR5P2 X-Message-ID-Hash: UCJ4GN34FYMHUZWPN2IOLMNPEHGZR5P2 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 01/16] aes: Use const pointers for read-only parameters List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Update AES function signatures to use const pointers for parameters that are not modified. This improves type safety and makes it easier to see which parameters are read-only. Fix the Nuvoton npcm_ae driver as well since it implements the API. Really that should be handled by a driver, but leave that for now. Functions updated: - aes_expand_key(): key parameter - aes_encrypt(): in and expkey parameters - aes_decrypt(): in and expkey parameters - aes_apply_cbc_chain_data(): cbc_chain_data and src parameters - aes_cbc_encrypt_blocks(): key_exp, iv, and src parameters - aes_cbc_decrypt_blocks(): key_exp, iv, and src parameters - add_round_key(): key parameter (internal) - debug_print_vector(): data parameter (internal) Co-developed-by: Claude Signed-off-by: Simon Glass --- drivers/crypto/nuvoton/npcm_aes.c | 22 ++++++++-------- include/uboot_aes.h | 42 +++++++++++++++---------------- lib/aes.c | 22 ++++++++-------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/drivers/crypto/nuvoton/npcm_aes.c b/drivers/crypto/nuvoton/npcm_aes.c index 8d3a30ea918..68b582b0d12 100644 --- a/drivers/crypto/nuvoton/npcm_aes.c +++ b/drivers/crypto/nuvoton/npcm_aes.c @@ -102,10 +102,10 @@ static int npcm_aes_init(u8 dec_enc) return 0; } -static inline void npcm_aes_load_iv(u8 *iv) +static inline void npcm_aes_load_iv(const u8 *iv) { struct npcm_aes_regs *regs = aes_priv->regs; - u32 *p = (u32 *)iv; + const u32 *p = (const u32 *)iv; u32 i; /* Initialization Vector is loaded in 32-bit chunks */ @@ -113,10 +113,10 @@ static inline void npcm_aes_load_iv(u8 *iv) writel(p[i], ®s->aes_iv_0 + i); } -static inline void npcm_aes_load_key(u8 *key) +static inline void npcm_aes_load_key(const u8 *key) { struct npcm_aes_regs *regs = aes_priv->regs; - u32 *p = (u32 *)key; + const u32 *p = (const u32 *)key; u32 i; /* The key can be loaded either via the configuration or by using sideband @@ -140,7 +140,7 @@ static inline void npcm_aes_load_key(u8 *key) } } -static inline void npcm_aes_write(u32 *in) +static inline void npcm_aes_write(const u32 *in) { struct npcm_aes_regs *regs = aes_priv->regs; u32 i; @@ -160,7 +160,7 @@ static inline void npcm_aes_read(u32 *out) out[i] = readl(®s->aes_fifo_data); } -static void npcm_aes_feed(u32 num_aes_blocks, u32 *datain, u32 *dataout) +static void npcm_aes_feed(u32 num_aes_blocks, const u32 *datain, u32 *dataout) { struct npcm_aes_regs *regs = aes_priv->regs; u32 aes_datablk; @@ -235,14 +235,14 @@ static void npcm_aes_feed(u32 num_aes_blocks, u32 *datain, u32 *dataout) } } -void aes_expand_key(u8 *key, u32 key_size, u8 *expkey) +void aes_expand_key(const u8 *key, u32 key_size, u8 *expkey) { /* npcm hw expands the key automatically, just copy it */ memcpy(expkey, key, SIZE_AES_BLOCK * 2); } -void aes_cbc_encrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, - u32 num_aes_blocks) +void aes_cbc_encrypt_blocks(u32 key_size, const u8 *key_exp, const u8 *iv, + const u8 *src, u8 *dst, u32 num_aes_blocks) { if (npcm_aes_init(AES_OP_ENCRYPT)) return; @@ -254,8 +254,8 @@ void aes_cbc_encrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, npcm_aes_feed(num_aes_blocks, (u32 *)src, (u32 *)dst); } -void aes_cbc_decrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, - u32 num_aes_blocks) +void aes_cbc_decrypt_blocks(u32 key_size, const u8 *key_exp, const u8 *iv, + const u8 *src, u8 *dst, u32 num_aes_blocks) { if (npcm_aes_init(AES_OP_DECRYPT)) return; diff --git a/include/uboot_aes.h b/include/uboot_aes.h index d2583bed992..440049e3069 100644 --- a/include/uboot_aes.h +++ b/include/uboot_aes.h @@ -44,67 +44,67 @@ enum { * Expand a key into a key schedule, which is then used for the other * operations. * - * @key Key + * @key Key (not modified) * @key_size Size of the key (in bits) * @expkey Buffer to place expanded key, AES_EXPAND_KEY_LENGTH */ -void aes_expand_key(u8 *key, u32 key_size, u8 *expkey); +void aes_expand_key(const u8 *key, u32 key_size, u8 *expkey); /** * aes_encrypt() - Encrypt single block of data with AES 128 * * @key_size Size of the aes key (in bits) - * @in Input data - * @expkey Expanded key to use for encryption (from aes_expand_key()) + * @in Input data (not modified) + * @expkey Expanded key to use for encryption (from aes_expand_key(), not modified) * @out Output data */ -void aes_encrypt(u32 key_size, u8 *in, u8 *expkey, u8 *out); +void aes_encrypt(u32 key_size, const u8 *in, const u8 *expkey, u8 *out); /** * aes_decrypt() - Decrypt single block of data with AES 128 * * @key_size Size of the aes key (in bits) - * @in Input data - * @expkey Expanded key to use for decryption (from aes_expand_key()) + * @in Input data (not modified) + * @expkey Expanded key to use for decryption (from aes_expand_key(), not modified) * @out Output data */ -void aes_decrypt(u32 key_size, u8 *in, u8 *expkey, u8 *out); +void aes_decrypt(u32 key_size, const u8 *in, const u8 *expkey, u8 *out); /** * Apply chain data to the destination using EOR * * Each array is of length AES_BLOCK_LENGTH. * - * @cbc_chain_data Chain data - * @src Source data + * @cbc_chain_data Chain data (not modified) + * @src Source data (not modified) * @dst Destination data, which is modified here */ -void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst); +void aes_apply_cbc_chain_data(const u8 *cbc_chain_data, const u8 *src, u8 *dst); /** * aes_cbc_encrypt_blocks() - Encrypt multiple blocks of data with AES CBC. * * @key_size Size of the aes key (in bits) - * @key_exp Expanded key to use - * @iv Initialization vector - * @src Source data to encrypt + * @key_exp Expanded key to use (not modified) + * @iv Initialization vector (not modified) + * @src Source data to encrypt (not modified) * @dst Destination buffer * @num_aes_blocks Number of AES blocks to encrypt */ -void aes_cbc_encrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, - u32 num_aes_blocks); +void aes_cbc_encrypt_blocks(u32 key_size, const u8 *key_exp, const u8 *iv, + const u8 *src, u8 *dst, u32 num_aes_blocks); /** * Decrypt multiple blocks of data with AES CBC. * * @key_size Size of the aes key (in bits) - * @key_exp Expanded key to use - * @iv Initialization vector - * @src Source data to decrypt + * @key_exp Expanded key to use (not modified) + * @iv Initialization vector (not modified) + * @src Source data to decrypt (not modified) * @dst Destination buffer * @num_aes_blocks Number of AES blocks to decrypt */ -void aes_cbc_decrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, - u32 num_aes_blocks); +void aes_cbc_decrypt_blocks(u32 key_size, const u8 *key_exp, const u8 *iv, + const u8 *src, u8 *dst, u32 num_aes_blocks); #endif /* _AES_REF_H_ */ diff --git a/lib/aes.c b/lib/aes.c index 3bcbeeab9af..6894dd89e50 100644 --- a/lib/aes.c +++ b/lib/aes.c @@ -498,7 +498,7 @@ static void inv_mix_sub_columns(u8 *state) * encrypt/decrypt columns of the key * n.b. you can replace this with byte-wise xor if you wish. */ -static void add_round_key(u32 *state, u32 *key) +static void add_round_key(u32 *state, const u32 *key) { int idx; @@ -537,7 +537,7 @@ static u32 aes_get_keycols(u32 key_len) } /* produce AES_STATECOLS bytes for each round */ -void aes_expand_key(u8 *key, u32 key_len, u8 *expkey) +void aes_expand_key(const u8 *key, u32 key_len, u8 *expkey) { u8 tmp0, tmp1, tmp2, tmp3, tmp4; uint idx, aes_rounds, aes_keycols; @@ -574,7 +574,7 @@ void aes_expand_key(u8 *key, u32 key_len, u8 *expkey) } /* encrypt one 128 bit block */ -void aes_encrypt(u32 key_len, u8 *in, u8 *expkey, u8 *out) +void aes_encrypt(u32 key_len, const u8 *in, const u8 *expkey, u8 *out) { u8 state[AES_STATECOLS * 4]; u32 round, aes_rounds; @@ -597,7 +597,7 @@ void aes_encrypt(u32 key_len, u8 *in, u8 *expkey, u8 *out) memcpy(out, state, sizeof(state)); } -void aes_decrypt(u32 key_len, u8 *in, u8 *expkey, u8 *out) +void aes_decrypt(u32 key_len, const u8 *in, const u8 *expkey, u8 *out) { u8 state[AES_STATECOLS * 4]; int round, aes_rounds; @@ -620,7 +620,7 @@ void aes_decrypt(u32 key_len, u8 *in, u8 *expkey, u8 *out) memcpy(out, state, sizeof(state)); } -static void debug_print_vector(char *name, u32 num_bytes, u8 *data) +static void debug_print_vector(char *name, u32 num_bytes, const u8 *data) { #ifdef DEBUG printf("%s [%d] @0x%p", name, num_bytes, data); @@ -628,7 +628,7 @@ static void debug_print_vector(char *name, u32 num_bytes, u8 *data) #endif } -void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst) +void aes_apply_cbc_chain_data(const u8 *cbc_chain_data, const u8 *src, u8 *dst) { int i; @@ -636,11 +636,11 @@ void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst) *dst++ = *src++ ^ *cbc_chain_data++; } -void aes_cbc_encrypt_blocks(u32 key_len, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, - u32 num_aes_blocks) +void aes_cbc_encrypt_blocks(u32 key_len, const u8 *key_exp, const u8 *iv, + const u8 *src, u8 *dst, u32 num_aes_blocks) { u8 tmp_data[AES_BLOCK_LENGTH]; - u8 *cbc_chain_data = iv; + const u8 *cbc_chain_data = iv; u32 i; for (i = 0; i < num_aes_blocks; i++) { @@ -662,8 +662,8 @@ void aes_cbc_encrypt_blocks(u32 key_len, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, } } -void aes_cbc_decrypt_blocks(u32 key_len, u8 *key_exp, u8 *iv, u8 *src, u8 *dst, - u32 num_aes_blocks) +void aes_cbc_decrypt_blocks(u32 key_len, const u8 *key_exp, const u8 *iv, + const u8 *src, u8 *dst, u32 num_aes_blocks) { u8 tmp_data[AES_BLOCK_LENGTH], tmp_block[AES_BLOCK_LENGTH]; /* Convenient array of 0's for IV */