This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new fa6459263 crypto/mbedtls: write back the OFB/CFB IV in aes_alt
fa6459263 is described below
commit fa645926383dd6bed2d846e1634efa4e1965ffcd
Author: makejian <[email protected]>
AuthorDate: Mon Nov 23 10:43:58 2026 +0800
crypto/mbedtls: write back the OFB/CFB IV in aes_alt
mbedtls_aes_crypt_ofb/cfb128/cfb8 open a fresh session for every call
and the driver keeps the advanced IV to itself in these modes, so iv[]
stayed at its initial value and a message split over several calls
restarted the stream each time.
Derive the next iv[] from the data after the call, the way
mbedtls_aes_crypt_cbc() already does: CFB128 chains on the last
ciphertext block, CFB8 on the last 16 bytes of iv || ciphertext, and
OFB on the last keystream block, i.e. output ^ input. For CFB128/OFB
this is only done when the call ends on a block boundary.
A CFB128/OFB call that continues after a partial block, that is, one
with *iv_off != 0, now returns MBEDTLS_ERR_AES_BAD_INPUT_DATA instead
of silently reusing the keystream from the start of iv[]: with OFB
that reuse makes the XOR of the two ciphertexts equal the XOR of the
two plaintexts. The iv_off bookkeeping also follows the mbedtls
semantics, *iv_off = (*iv_off + length) % 16, and a zero-length CFB8
call returns early so that input - 16 is never formed on a NULL input.
Build-tested with sim:sim/crypto and CONFIG_CRYPTO_MBEDTLS; the
existing crypto tests still pass.
Signed-off-by: makejian <[email protected]>
---
crypto/mbedtls/source/aes_alt.c | 70 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 66 insertions(+), 4 deletions(-)
diff --git a/crypto/mbedtls/source/aes_alt.c b/crypto/mbedtls/source/aes_alt.c
index 649e6f792..4be6e44da 100644
--- a/crypto/mbedtls/source/aes_alt.c
+++ b/crypto/mbedtls/source/aes_alt.c
@@ -329,6 +329,7 @@ int mbedtls_aes_crypt_cfb128(FAR mbedtls_aes_context *ctx,
const unsigned char *input,
unsigned char *output)
{
+ unsigned char temp[16];
int ret;
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT)
@@ -336,11 +337,17 @@ int mbedtls_aes_crypt_cfb128(FAR mbedtls_aes_context *ctx,
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
}
- if (*iv_off > 15)
+ if (*iv_off != 0)
{
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
}
+ memset(temp, 0, sizeof(temp));
+ if (mode == MBEDTLS_AES_DECRYPT && length >= 16)
+ {
+ memcpy(temp, input + length - 16, 16);
+ }
+
ctx->dev.session.cipher = CRYPTO_AES_CFB_128;
ret = cryptodev_get_session(&ctx->dev);
if (ret != 0)
@@ -358,7 +365,17 @@ int mbedtls_aes_crypt_cfb128(FAR mbedtls_aes_context *ctx,
ret = cryptodev_crypt(&ctx->dev);
if (ret == 0)
{
- *iv_off = length % ECB_BLOCK_SIZE;
+ *iv_off = (*iv_off + length) % ECB_BLOCK_SIZE;
+
+ /* The driver does not return the IV; CFB128 chains on the last
+ * ciphertext block, so only whole-block calls can be continued.
+ */
+
+ if (length >= 16 && *iv_off == 0)
+ {
+ memcpy(iv, mode == MBEDTLS_AES_ENCRYPT ?
+ output + length - 16 : temp, 16);
+ }
}
cryptodev_free_session(&ctx->dev);
@@ -374,13 +391,29 @@ int mbedtls_aes_crypt_cfb8(FAR mbedtls_aes_context *ctx,
const unsigned char *input,
unsigned char *output)
{
+ unsigned char temp[32];
+ size_t n = length < 16 ? length : 16;
int ret;
+ if (length == 0)
+ {
+ return 0;
+ }
+
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT)
{
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
}
+ /* The next IV is the last 16 bytes of IV || ciphertext. */
+
+ memset(temp, 0, sizeof(temp));
+ memcpy(temp, iv, 16);
+ if (mode == MBEDTLS_AES_DECRYPT)
+ {
+ memcpy(temp + 16, input + length - n, n);
+ }
+
ctx->dev.session.cipher = CRYPTO_AES_CFB_8;
ret = cryptodev_get_session(&ctx->dev);
if (ret != 0)
@@ -397,6 +430,17 @@ int mbedtls_aes_crypt_cfb8(FAR mbedtls_aes_context *ctx,
ctx->dev.crypt.iv = (caddr_t)iv;
ret = cryptodev_crypt(&ctx->dev);
cryptodev_free_session(&ctx->dev);
+
+ if (ret == 0)
+ {
+ if (mode == MBEDTLS_AES_ENCRYPT)
+ {
+ memcpy(temp + 16, output + length - n, n);
+ }
+
+ memcpy(iv, temp + n, 16);
+ }
+
return ret;
}
#endif /* MBEDTLS_CIPHER_MODE_CFB */
@@ -412,13 +456,21 @@ int mbedtls_aes_crypt_ofb(FAR mbedtls_aes_context *ctx,
const unsigned char *input,
unsigned char *output)
{
+ unsigned char temp[16];
int ret;
+ int i;
- if (*iv_off > 15)
+ if (*iv_off != 0)
{
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
}
+ memset(temp, 0, sizeof(temp));
+ if (length >= 16)
+ {
+ memcpy(temp, input + length - 16, 16);
+ }
+
ctx->dev.session.cipher = CRYPTO_AES_OFB;
ret = cryptodev_get_session(&ctx->dev);
if (ret != 0)
@@ -435,7 +487,17 @@ int mbedtls_aes_crypt_ofb(FAR mbedtls_aes_context *ctx,
ret = cryptodev_crypt(&ctx->dev);
if (ret == 0)
{
- *iv_off = length % ECB_BLOCK_SIZE;
+ *iv_off = (*iv_off + length) % ECB_BLOCK_SIZE;
+
+ /* The OFB IV is the last keystream block, i.e. output ^ input. */
+
+ if (length >= 16 && *iv_off == 0)
+ {
+ for (i = 0; i < 16; i++)
+ {
+ iv[i] = output[length - 16 + i] ^ temp[i];
+ }
+ }
}
cryptodev_free_session(&ctx->dev);