[PATCH 2/3] hash_buffers: add output length param for XOF support

Jussi Kivilinna jussi.kivilinna at iki.fi
Fri Apr 9 18:53:13 CEST 2021


* src/cipher-proto.h (gcry_md_hash_buffers_t): Add nbytes parameter
for outbuf size for XOF algorithms.
* cipher/md.c (_gcry_md_hash_buffer)
(_gcry_md_hash_buffers_extract): Adjust for spec->hash_buffers
parameter change.
* cipher/keccak.c (_gcry_sha3_hash_buffers): Add nbytes parameter and
handling for XOF output.
* cipher/blake2.c (DEFINE_BLAKE2_VARIANT): Add nbytes parameter for
hash_buffers.
* cipher/rmd160.c (_gcry_rmd160_hash_buffers): Add nbytes parameter.
* cipher/sha1.c (_gcry_sha1_hash_buffers): Add nbytes parameter.
(_gcry_sha1_hash_buffer): Pass nbytes to _gcry_sha1_hash_buffers.
* cipher/sha256.c (_gcry_sha256_hash_buffers)
(_gcry_sha224_hash_buffers): Add nbytes parameter.
* cipher/sha512.c (_gcry_sha512_hash_buffers)
(_gcry_sha384_hash_buffers, _gcry_sha512_256_hash_buffers)
(_gcry_sha512_224_hash_buffers): Add nbytes parameter.
* cipher/sm3.c (_gcry_sm3_hash_buffers): Add nbytes parameter.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/blake2.c    |  3 ++-
 cipher/keccak.c    | 37 ++++++++++++++++++++++---------------
 cipher/md.c        |  4 ++--
 cipher/rmd160.c    |  5 ++++-
 cipher/sha1.c      |  7 +++++--
 cipher/sha256.c    | 10 ++++++++--
 cipher/sha512.c    | 21 +++++++++++++++------
 cipher/sm3.c       |  5 ++++-
 src/cipher-proto.h |  5 +++--
 9 files changed, 65 insertions(+), 32 deletions(-)

diff --git a/cipher/blake2.c b/cipher/blake2.c
index 0243ed33..750c00c3 100644
--- a/cipher/blake2.c
+++ b/cipher/blake2.c
@@ -946,10 +946,11 @@ gcry_err_code_t _gcry_blake2_init_with_key(void *ctx, unsigned int flags,
     gcry_assert (err == 0); \
   } \
   static void \
-  _gcry_blake2##bs##_##dbits##_hash_buffers(void *outbuf, \
+  _gcry_blake2##bs##_##dbits##_hash_buffers(void *outbuf, size_t nbytes, \
         const gcry_buffer_t *iov, int iovcnt) \
   { \
     BLAKE2##BS##_CONTEXT hd; \
+    (void)nbytes; \
     blake2##bs##_##dbits##_init (&hd, 0); \
     for (;iovcnt > 0; iov++, iovcnt--) \
       blake2##bs##_write (&hd, (const char*)iov[0].data + iov[0].off, \
diff --git a/cipher/keccak.c b/cipher/keccak.c
index bb643e3d..00593bdb 100644
--- a/cipher/keccak.c
+++ b/cipher/keccak.c
@@ -1223,8 +1223,8 @@ keccak_extract (void *context, void *out, size_t outlen)
 
 /* Variant of the above shortcut function using multiple buffers.  */
 static void
-_gcry_sha3_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt,
-                         const gcry_md_spec_t *spec)
+_gcry_sha3_hash_buffers (void *outbuf, size_t nbytes, const gcry_buffer_t *iov,
+			 int iovcnt, const gcry_md_spec_t *spec)
 {
   KECCAK_CONTEXT hd;
 
@@ -1232,36 +1232,43 @@ _gcry_sha3_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt,
   for (;iovcnt > 0; iov++, iovcnt--)
     keccak_write (&hd, (const char*)iov[0].data + iov[0].off, iov[0].len);
   keccak_final (&hd);
-  memcpy (outbuf, keccak_read (&hd), spec->mdlen);
+  if (spec->mdlen > 0)
+    memcpy (outbuf, keccak_read (&hd), spec->mdlen);
+  else
+    keccak_extract (&hd, outbuf, nbytes);
 }
 
 
 static void
-_gcry_sha3_224_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
-                             int iovcnt)
+_gcry_sha3_224_hash_buffers (void *outbuf, size_t nbytes,
+			     const gcry_buffer_t *iov, int iovcnt)
 {
-  _gcry_sha3_hash_buffers (outbuf, iov, iovcnt, &_gcry_digest_spec_sha3_224);
+  _gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt,
+			   &_gcry_digest_spec_sha3_224);
 }
 
 static void
-_gcry_sha3_256_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
-                             int iovcnt)
+_gcry_sha3_256_hash_buffers (void *outbuf, size_t nbytes,
+			     const gcry_buffer_t *iov, int iovcnt)
 {
-  _gcry_sha3_hash_buffers (outbuf, iov, iovcnt, &_gcry_digest_spec_sha3_256);
+  _gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt,
+			   &_gcry_digest_spec_sha3_256);
 }
 
 static void
-_gcry_sha3_384_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
-                             int iovcnt)
+_gcry_sha3_384_hash_buffers (void *outbuf, size_t nbytes,
+			     const gcry_buffer_t *iov, int iovcnt)
 {
-  _gcry_sha3_hash_buffers (outbuf, iov, iovcnt, &_gcry_digest_spec_sha3_384);
+  _gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt,
+			   &_gcry_digest_spec_sha3_384);
 }
 
 static void
-_gcry_sha3_512_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
-                             int iovcnt)
+_gcry_sha3_512_hash_buffers (void *outbuf, size_t nbytes,
+			     const gcry_buffer_t *iov, int iovcnt)
 {
-  _gcry_sha3_hash_buffers (outbuf, iov, iovcnt, &_gcry_digest_spec_sha3_512);
+  _gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt,
+			   &_gcry_digest_spec_sha3_512);
 }
 
 
diff --git a/cipher/md.c b/cipher/md.c
index f142e9c3..6497ab41 100644
--- a/cipher/md.c
+++ b/cipher/md.c
@@ -1214,7 +1214,7 @@ _gcry_md_hash_buffer (int algo, void *digest,
       iov.off = 0;
       iov.len = length;
 
-      spec->hash_buffers (digest, &iov, 1);
+      spec->hash_buffers (digest, spec->mdlen, &iov, 1);
     }
   else
     {
@@ -1294,7 +1294,7 @@ _gcry_md_hash_buffers_extract (int algo, unsigned int flags, void *digest,
 
   if (!hmac && spec->hash_buffers)
     {
-      spec->hash_buffers (digest, iov, iovcnt);
+      spec->hash_buffers (digest, digestlen, iov, iovcnt);
     }
   else
     {
diff --git a/cipher/rmd160.c b/cipher/rmd160.c
index 37462130..1861205e 100644
--- a/cipher/rmd160.c
+++ b/cipher/rmd160.c
@@ -481,10 +481,13 @@ rmd160_read( void *context )
  * into outbuf which must have a size of 20 bytes.
  */
 static void
-_gcry_rmd160_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_rmd160_hash_buffers (void *outbuf, size_t nbytes,
+			   const gcry_buffer_t *iov, int iovcnt)
 {
   RMD160_CONTEXT hd;
 
+  (void)nbytes;
+
   rmd160_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
diff --git a/cipher/sha1.c b/cipher/sha1.c
index 48fdbb33..3bb24c7e 100644
--- a/cipher/sha1.c
+++ b/cipher/sha1.c
@@ -637,10 +637,13 @@ sha1_read( void *context )
  * into outbuf which must have a size of 20 bytes.
  */
 static void
-_gcry_sha1_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_sha1_hash_buffers (void *outbuf, size_t nbytes,
+			 const gcry_buffer_t *iov, int iovcnt)
 {
   SHA1_CONTEXT hd;
 
+  (void)nbytes;
+
   sha1_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
@@ -658,7 +661,7 @@ _gcry_sha1_hash_buffer (void *outbuf, const void *buffer, size_t length)
   iov.data = (void *)buffer;
   iov.len = length;
 
-  _gcry_sha1_hash_buffers (outbuf, &iov, 1);
+  _gcry_sha1_hash_buffers (outbuf, 20, &iov, 1);
 }
 
 
diff --git a/cipher/sha256.c b/cipher/sha256.c
index 871615b6..7b2c78f7 100644
--- a/cipher/sha256.c
+++ b/cipher/sha256.c
@@ -631,10 +631,13 @@ sha256_read (void *context)
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 32 bytes.  */
 static void
-_gcry_sha256_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_sha256_hash_buffers (void *outbuf, size_t nbytes,
+			   const gcry_buffer_t *iov, int iovcnt)
 {
   SHA256_CONTEXT hd;
 
+  (void)nbytes;
+
   sha256_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
@@ -647,10 +650,13 @@ _gcry_sha256_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 28 bytes.  */
 static void
-_gcry_sha224_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_sha224_hash_buffers (void *outbuf, size_t nbytes,
+			   const gcry_buffer_t *iov, int iovcnt)
 {
   SHA256_CONTEXT hd;
 
+  (void)nbytes;
+
   sha224_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
diff --git a/cipher/sha512.c b/cipher/sha512.c
index a23d5bda..6859cf52 100644
--- a/cipher/sha512.c
+++ b/cipher/sha512.c
@@ -865,10 +865,13 @@ sha512_read (void *context)
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 64 bytes.  */
 static void
-_gcry_sha512_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_sha512_hash_buffers (void *outbuf, size_t nbytes,
+			   const gcry_buffer_t *iov, int iovcnt)
 {
   SHA512_CONTEXT hd;
 
+  (void)nbytes;
+
   sha512_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
@@ -882,10 +885,13 @@ _gcry_sha512_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 48 bytes.  */
 static void
-_gcry_sha384_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_sha384_hash_buffers (void *outbuf, size_t nbytes,
+			   const gcry_buffer_t *iov, int iovcnt)
 {
   SHA512_CONTEXT hd;
 
+  (void)nbytes;
+
   sha384_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
@@ -899,11 +905,12 @@ _gcry_sha384_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 32 bytes.  */
 static void
-_gcry_sha512_256_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
-			       int iovcnt)
+_gcry_sha512_256_hash_buffers (void *outbuf, size_t nbytes,
+			       const gcry_buffer_t *iov, int iovcnt)
 {
   SHA512_CONTEXT hd;
 
+  (void)nbytes;
 
   sha512_256_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
@@ -918,11 +925,13 @@ _gcry_sha512_256_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 28 bytes.  */
 static void
-_gcry_sha512_224_hash_buffers (void *outbuf, const gcry_buffer_t *iov,
-			       int iovcnt)
+_gcry_sha512_224_hash_buffers (void *outbuf, size_t nbytes,
+			       const gcry_buffer_t *iov, int iovcnt)
 {
   SHA512_CONTEXT hd;
 
+  (void)nbytes;
+
   sha512_224_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
diff --git a/cipher/sm3.c b/cipher/sm3.c
index c687cdf0..d52a7494 100644
--- a/cipher/sm3.c
+++ b/cipher/sm3.c
@@ -341,10 +341,13 @@ sm3_read (void *context)
 /* Shortcut functions which puts the hash value of the supplied buffer iov
  * into outbuf which must have a size of 32 bytes.  */
 static void
-_gcry_sm3_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
+_gcry_sm3_hash_buffers (void *outbuf, size_t nbytes,
+			const gcry_buffer_t *iov, int iovcnt)
 {
   SM3_CONTEXT hd;
 
+  (void)nbytes;
+
   sm3_init (&hd, 0);
   for (;iovcnt > 0; iov++, iovcnt--)
     _gcry_md_block_write (&hd,
diff --git a/src/cipher-proto.h b/src/cipher-proto.h
index dde56d30..b730fd06 100644
--- a/src/cipher-proto.h
+++ b/src/cipher-proto.h
@@ -222,8 +222,9 @@ typedef unsigned char *(*gcry_md_read_t) (void *c);
 typedef void (*gcry_md_extract_t) (void *c, void *outbuf, size_t nbytes);
 
 /* Type for the md_hash_buffers function. */
-typedef void (*gcry_md_hash_buffers_t) (void *outbuf, const gcry_buffer_t *iov,
-				        int iovcnt);
+typedef void (*gcry_md_hash_buffers_t) (void *outbuf, size_t nbytes,
+					const gcry_buffer_t *iov,
+					int iovcnt);
 
 typedef struct gcry_md_oid_spec
 {
-- 
2.27.0




More information about the Gcrypt-devel mailing list