blake2: add implementation support for variable digest length
authorAntoine Salon <asalon@vmware.com>
Thu, 20 Dec 2018 23:08:23 +0000 (15:08 -0800)
committerMatt Caswell <matt@openssl.org>
Wed, 6 Feb 2019 09:18:43 +0000 (09:18 +0000)
Signed-off-by: Antoine Salon <asalon@vmware.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/7726)

crypto/blake2/blake2_locl.h
crypto/blake2/blake2b.c
crypto/blake2/blake2s.c

index 892a1af1b645349e9c6f25ee09a2e36521a80239..33bd64ba58fbc737513b5894a779ef60560bc1c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -49,6 +49,7 @@ struct blake2s_ctx_st {
     uint32_t f[2];
     uint8_t  buf[BLAKE2S_BLOCKBYTES];
     size_t   buflen;
     uint32_t f[2];
     uint8_t  buf[BLAKE2S_BLOCKBYTES];
     size_t   buflen;
+    size_t   outlen;
 };
 
 struct blake2b_param_st {
 };
 
 struct blake2b_param_st {
@@ -73,6 +74,7 @@ struct blake2b_ctx_st {
     uint64_t f[2];
     uint8_t  buf[BLAKE2B_BLOCKBYTES];
     size_t   buflen;
     uint64_t f[2];
     uint8_t  buf[BLAKE2B_BLOCKBYTES];
     size_t   buflen;
+    size_t   outlen;
 };
 
 #define BLAKE2B_DIGEST_LENGTH 64
 };
 
 #define BLAKE2B_DIGEST_LENGTH 64
index d4e53269c77d7f22660650ba6c9fe136af92aef2..fb128e4bb4fd028ecdfd7f2c39036377a07a71dd 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -62,12 +62,14 @@ static ossl_inline void blake2b_init0(BLAKE2B_CTX *S)
     }
 }
 
     }
 }
 
-/* init xors IV with input parameter block */
+/* init xors IV with input parameter block and sets the output length */
 static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
 {
     size_t i;
     const uint8_t *p = (const uint8_t *)(P);
 static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
 {
     size_t i;
     const uint8_t *p = (const uint8_t *)(P);
+
     blake2b_init0(S);
     blake2b_init0(S);
+    S->outlen = P->digest_length;
 
     /* The param struct is carefully hand packed, and should be 64 bytes on
      * every platform. */
 
     /* The param struct is carefully hand packed, and should be 64 bytes on
      * every platform. */
@@ -252,6 +254,7 @@ int BLAKE2b_Update(BLAKE2B_CTX *c, const void *data, size_t datalen)
  */
 int BLAKE2b_Final(unsigned char *md, BLAKE2B_CTX *c)
 {
  */
 int BLAKE2b_Final(unsigned char *md, BLAKE2B_CTX *c)
 {
+    uint8_t outbuffer[BLAKE2B_OUTBYTES] = {0};
     int i;
 
     blake2b_set_lastblock(c);
     int i;
 
     blake2b_set_lastblock(c);
@@ -259,11 +262,12 @@ int BLAKE2b_Final(unsigned char *md, BLAKE2B_CTX *c)
     memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
     blake2b_compress(c, c->buf, c->buflen);
 
     memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
     blake2b_compress(c, c->buf, c->buflen);
 
-    /* Output full hash to message digest */
+    /* Output full hash to temp buffer */
     for (i = 0; i < 8; ++i) {
     for (i = 0; i < 8; ++i) {
-        store64(md + sizeof(c->h[i]) * i, c->h[i]);
+        store64(outbuffer + sizeof(c->h[i]) * i, c->h[i]);
     }
 
     }
 
+    memcpy(md, outbuffer, c->outlen);
     OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
     return 1;
 }
     OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
     return 1;
 }
index c0f0f266f727e111a218e8f391efbb28f3b61445..4a02778d738d760a2ef632ce705996ee285cd797 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -58,16 +58,18 @@ static ossl_inline void blake2s_init0(BLAKE2S_CTX *S)
     }
 }
 
     }
 }
 
-/* init2 xors IV with input parameter block */
+/* init xors IV with input parameter block and sets the output length */
 static void blake2s_init_param(BLAKE2S_CTX *S, const BLAKE2S_PARAM *P)
 {
     const uint8_t *p = (const uint8_t *)(P);
     size_t i;
 static void blake2s_init_param(BLAKE2S_CTX *S, const BLAKE2S_PARAM *P)
 {
     const uint8_t *p = (const uint8_t *)(P);
     size_t i;
+    
+    blake2s_init0(S);
+    S->outlen = P->digest_length;
 
     /* The param struct is carefully hand packed, and should be 32 bytes on
      * every platform. */
     assert(sizeof(BLAKE2S_PARAM) == 32);
 
     /* The param struct is carefully hand packed, and should be 32 bytes on
      * every platform. */
     assert(sizeof(BLAKE2S_PARAM) == 32);
-    blake2s_init0(S);
     /* IV XOR ParamBlock */
     for (i = 0; i < 8; ++i) {
         S->h[i] ^= load32(&p[i*4]);
     /* IV XOR ParamBlock */
     for (i = 0; i < 8; ++i) {
         S->h[i] ^= load32(&p[i*4]);
@@ -246,6 +248,7 @@ int BLAKE2s_Update(BLAKE2S_CTX *c, const void *data, size_t datalen)
  */
 int BLAKE2s_Final(unsigned char *md, BLAKE2S_CTX *c)
 {
  */
 int BLAKE2s_Final(unsigned char *md, BLAKE2S_CTX *c)
 {
+    uint8_t outbuffer[BLAKE2S_OUTBYTES] = {0};
     int i;
 
     blake2s_set_lastblock(c);
     int i;
 
     blake2s_set_lastblock(c);
@@ -255,9 +258,10 @@ int BLAKE2s_Final(unsigned char *md, BLAKE2S_CTX *c)
 
     /* Output full hash to temp buffer */
     for (i = 0; i < 8; ++i) {
 
     /* Output full hash to temp buffer */
     for (i = 0; i < 8; ++i) {
-        store32(md + sizeof(c->h[i]) * i, c->h[i]);
+        store32(outbuffer + sizeof(c->h[i]) * i, c->h[i]);
     }
 
     }
 
+    memcpy(md, outbuffer, c->outlen);
     OPENSSL_cleanse(c, sizeof(BLAKE2S_CTX));
     return 1;
 }
     OPENSSL_cleanse(c, sizeof(BLAKE2S_CTX));
     return 1;
 }