cleanse stack variable in blake2[b|s] finalization
authorNeil Horman <nhorman@openssl.org>
Mon, 1 Jan 2024 14:25:03 +0000 (09:25 -0500)
committerNeil Horman <nhorman@openssl.org>
Wed, 3 Jan 2024 17:42:01 +0000 (12:42 -0500)
If the output of a blake2[b|s] digest isn't a multipl of 8, then a stack
buffer is used to compute the final output, which is left un-zeroed
prior to return, allowing the potential leak of key data.  Ensure that,
if the stack variable is used, it gets cleared prior to return.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/23173)

providers/implementations/digests/blake2b_prov.c
providers/implementations/digests/blake2s_prov.c

index 970549ed0c2f4e38697a5fee1086c6af0777e9a3..a8b0848234bfe963e464eb386897687f011c0be4 100644 (file)
@@ -324,8 +324,10 @@ int ossl_blake2b_final(unsigned char *md, BLAKE2B_CTX *c)
     for (i = 0; i < iter; ++i)
         store64(target + sizeof(c->h[i]) * i, c->h[i]);
 
-    if (target != md)
+    if (target != md) {
         memcpy(md, target, c->outlen);
+        OPENSSL_cleanse(target, sizeof(outbuffer));
+    }
 
     OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
     return 1;
index a9a8f9d048a23340ef319893857624cfce1459c9..e43f78aaa73819fef93eb4ead7cb62bacd50e3ed 100644 (file)
@@ -314,8 +314,10 @@ int ossl_blake2s_final(unsigned char *md, BLAKE2S_CTX *c)
     for (i = 0; i < iter; ++i)
         store32(target + sizeof(c->h[i]) * i, c->h[i]);
 
-    if (target != md)
+    if (target != md) {
         memcpy(md, target, c->outlen);
+        OPENSSL_cleanse(target, sizeof(outbuffer));
+    }
 
     OPENSSL_cleanse(c, sizeof(BLAKE2S_CTX));
     return 1;