Commit 39a88f10d0 for openssl.org
commit 39a88f10d05fac070abf19220063729945345120
Author: Billy Brumley <bbb@iki.fi>
Date: Wed Oct 7 04:32:33 2026 -0400
AEAD: let AES-SIV start a new operation on re-init without a key
Re-initialising a SIV context without a key retained previous state, so
subsequent operations failed until the key was supplied again. Reset
that state on every init instead, matching the other AEADs. A context
that has never been keyed stays unusable.
test_cipher_reinit expected the old behavior; drop the special case.
Assisted-by: Claude:claude-fable-5-1
Reviewed-by: Bob Beck <beck@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Merge-date: Mon Nov 23 17:02:48 2026
Merged-from: https://github.com/openssl/openssl/pull/33139
diff --git a/crypto/modes/siv128.c b/crypto/modes/siv128.c
index ef3c546c4f..a0645192dd 100644
--- a/crypto/modes/siv128.c
+++ b/crypto/modes/siv128.c
@@ -177,6 +177,22 @@ int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
return 0;
memset(&ctx->d, 0, sizeof(ctx->d));
+
+ /* no new key: start a new operation with the existing one */
+ if (key == NULL) {
+ if (ctx->mac_ctx_init == NULL
+ || (mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
+ || !EVP_MAC_update(mac_ctx, zero, sizeof(zero))
+ || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len,
+ sizeof(ctx->d.byte))) {
+ EVP_MAC_CTX_free(mac_ctx);
+ return 0;
+ }
+ EVP_MAC_CTX_free(mac_ctx);
+ ctx->final_ret = -1;
+ ctx->crypto_ok = 1;
+ return 1;
+ }
EVP_CIPHER_CTX_free(ctx->cipher_ctx);
EVP_MAC_CTX_free(ctx->mac_ctx_init);
EVP_MAC_free(ctx->mac);
@@ -184,7 +200,7 @@ int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
ctx->cipher_ctx = NULL;
ctx->mac_ctx_init = NULL;
- if (key == NULL || cbc == NULL || ctr == NULL)
+ if (cbc == NULL || ctr == NULL)
return 0;
cbc_name = EVP_CIPHER_get0_name(cbc);
diff --git a/include/crypto/modes.h b/include/crypto/modes.h
index c7c31fd7ff..d56aab09ee 100644
--- a/include/crypto/modes.h
+++ b/include/crypto/modes.h
@@ -223,8 +223,8 @@ struct siv128_context {
EVP_CIPHER_CTX *cipher_ctx;
EVP_MAC *mac;
EVP_MAC_CTX *mac_ctx_init;
- int final_ret;
- int crypto_ok;
+ int final_ret; /* 0 once the tag has been computed (encrypt) or verified (decrypt), else -1 */
+ int crypto_ok; /* 1 before the single payload call, 0 after; -1 bypass (speed) */
};
#endif /* OPENSSL_NO_SIV */
diff --git a/include/crypto/siv.h b/include/crypto/siv.h
index 52d3fb8c9b..0c445d5f2e 100644
--- a/include/crypto/siv.h
+++ b/include/crypto/siv.h
@@ -20,6 +20,23 @@ typedef struct siv128_context SIV128_CONTEXT;
SIV128_CONTEXT *ossl_siv128_new(const unsigned char *key, int klen,
EVP_CIPHER *cbc, EVP_CIPHER *ctr,
OSSL_LIB_CTX *libctx, const char *propq);
+
+/**
+ * @brief Initialize a SIV context with a key, or restart with the existing one.
+ *
+ * With a NULL @p key the context keeps its key material, discards any AAD and
+ * payload state, and starts a new operation; @p klen, @p cbc, @p ctr,
+ * @p libctx and @p propq are then ignored. Fails if the context has no key.
+ *
+ * @param ctx the context to initialize
+ * @param key the key, or NULL to restart with the existing one
+ * @param klen the length of @p key in bytes
+ * @param cbc the CBC cipher for the CMAC
+ * @param ctr the CTR cipher for the encryption
+ * @param libctx the library context to fetch from
+ * @param propq the property query for fetches
+ * @returns 1 on success, 0 on failure
+ */
int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
const EVP_CIPHER *cbc, const EVP_CIPHER *ctr,
OSSL_LIB_CTX *libctx, const char *propq);
diff --git a/providers/implementations/ciphers/cipher_aes_siv.c b/providers/implementations/ciphers/cipher_aes_siv.c
index 3b501bccdc..b29ad5b1a2 100644
--- a/providers/implementations/ciphers/cipher_aes_siv.c
+++ b/providers/implementations/ciphers/cipher_aes_siv.c
@@ -86,14 +86,14 @@ static int siv_init(void *vctx, const unsigned char *key, size_t keylen,
ctx->enc = enc;
- if (key != NULL) {
- if (keylen != ctx->keylen) {
- ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
- return 0;
- }
- if (!ctx->hw->initkey(ctx, key, ctx->keylen))
- return 0;
+ if (key != NULL && keylen != ctx->keylen) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
+ return 0;
}
+ /* nothing to start an operation with until a key has been supplied */
+ if ((key != NULL || ctx->siv.mac_ctx_init != NULL)
+ && !ctx->hw->initkey(ctx, key, ctx->keylen))
+ return 0;
return aes_siv_set_ctx_params(ctx, params);
}
diff --git a/providers/implementations/ciphers/cipher_aes_siv_hw.c b/providers/implementations/ciphers/cipher_aes_siv_hw.c
index 50d5e861fa..f9dae88ddf 100644
--- a/providers/implementations/ciphers/cipher_aes_siv_hw.c
+++ b/providers/implementations/ciphers/cipher_aes_siv_hw.c
@@ -25,6 +25,10 @@ static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
OSSL_LIB_CTX *libctx = ctx->libctx;
const char *propq = NULL;
+ /* no new key: start a new operation with the existing one */
+ if (key == NULL)
+ return ossl_siv128_init(sctx, NULL, 0, NULL, NULL, libctx, propq);
+
EVP_CIPHER_free(ctx->cbc);
EVP_CIPHER_free(ctx->ctr);
ctx->cbc = NULL;
diff --git a/test/evp_libctx_test.c b/test/evp_libctx_test.c
index 522cb5e353..9a3192ad95 100644
--- a/test/evp_libctx_test.c
+++ b/test/evp_libctx_test.c
@@ -347,7 +347,7 @@ err:
static int test_cipher_reinit(int test_id)
{
- int ret = 0, diff, siv, no_null_key;
+ int ret = 0, diff, no_null_key;
int out1_len = 0, out2_len = 0, out3_len = 0;
EVP_CIPHER *cipher = NULL;
EVP_CIPHER_CTX *ctx = NULL;
@@ -390,9 +390,6 @@ static int test_cipher_reinit(int test_id)
if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
goto err;
- /* siv cannot be called with NULL key as the iv is irrelevant */
- siv = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_SIV_MODE);
-
/*
* Skip init call with a null key for RC4 as the stream cipher does not
* handle reinit (1.1.1 behaviour).
@@ -409,8 +406,7 @@ static int test_cipher_reinit(int test_id)
|| !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, sizeof(in)))
|| (!no_null_key
&& (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
- || !TEST_int_eq(EVP_EncryptUpdate(ctx, out3, &out3_len, in, sizeof(in)),
- siv ? 0 : 1))))
+ || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, sizeof(in))))))
goto err;
if (diff) {
@@ -420,7 +416,7 @@ static int test_cipher_reinit(int test_id)
goto err;
} else {
if (!TEST_mem_eq(out1, out1_len, out2, out2_len)
- || (!siv && !no_null_key && !TEST_mem_eq(out1, out1_len, out3, out3_len)))
+ || (!no_null_key && !TEST_mem_eq(out1, out1_len, out3, out3_len)))
goto err;
}
ret = 1;
@@ -497,22 +493,14 @@ static int test_cipher_reinit_partialupdate(int test_id)
if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
|| !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, in_len))
|| !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
- || !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, in_len)))
+ || !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, in_len))
+ || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
+ || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, in_len))
+ || (EVP_CIPHER_get_iv_length(cipher) != 0
+ && (!TEST_mem_eq(out1, out1_len, out2, out2_len)
+ || !TEST_mem_eq(out1, out1_len, out3, out3_len))))
goto err;
- if (EVP_CIPHER_get_iv_length(cipher) != 0)
- if (!TEST_mem_eq(out1, out1_len, out2, out2_len))
- goto err;
-
- if (EVP_CIPHER_get_mode(cipher) != EVP_CIPH_SIV_MODE) {
- if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
- || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, in_len)))
- goto err;
-
- if (EVP_CIPHER_get_iv_length(cipher) != 0)
- if (!TEST_mem_eq(out1, out1_len, out3, out3_len))
- goto err;
- }
ret = 1;
err:
EVP_CIPHER_free(cipher);