Directly return from final sha3/keccak_final if no bytes are requested
authorPatrick Steuer <patrick.steuer@de.ibm.com>
Mon, 5 Aug 2019 14:53:16 +0000 (16:53 +0200)
committerPatrick Steuer <patrick.steuer@de.ibm.com>
Sun, 18 Aug 2019 19:06:03 +0000 (21:06 +0200)
Requesting zero bytes from shake previously led to out-of-bounds write
on some platforms.

Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9433)

crypto/sha/sha3.c
providers/common/digests/sha3_prov.c

index 19ef426..fafa355 100644 (file)
@@ -89,6 +89,9 @@ int sha3_final(unsigned char *md, KECCAK1600_CTX *ctx)
     size_t bsz = ctx->block_size;
     size_t num = ctx->bufsz;
 
+    if (ctx->md_size == 0)
+        return 1;
+
     /*
      * Pad the data with 10*1. Note that |num| can be |bsz - 1|
      * in which case both byte operations below are performed on
index 469a160..17b15b7 100644 (file)
@@ -90,10 +90,12 @@ static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
 static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
                         size_t outsz)
 {
-    int ret;
+    int ret = 1;
     KECCAK1600_CTX *ctx = vctx;
 
-    ret = ctx->meth.final(out, ctx);
+    if (outsz > 0)
+        ret = ctx->meth.final(out, ctx);
+
     *outl = ctx->md_size;
     return ret;
 }