summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2019-08-24 17:34:29 +0200
committerBruno Haible <bruno@clisp.org>2019-08-24 17:34:29 +0200
commit387edff2fbcea3bfbf52ff60e31e4890192a3d37 (patch)
tree9054c574d21960cb067d8bb203d576b7297ee3d7
parent06ba5700871cfae584c604dafa93a3e3a4f23048 (diff)
downloadgnulib-387edff2fbcea3bfbf52ff60e31e4890192a3d37.tar.gz
gnulib-387edff2fbcea3bfbf52ff60e31e4890192a3d37.tar.bz2
crypto/gc-sha256, crypto/gc-sha512: New modules.
* lib/gc.h (gc_sha256, gc_sha512): New declarations. * lib/gc-gnulib.c: Include sha256.h, sha512.h. (MAX_DIGEST_SIZE): Set to 64. (_gc_hash_ctx, gc_hash_open, gc_hash_digest_length, gc_hash_write, gc_hash_read, gc_hash_buffer): Add support for sha256 and sha512. (gc_sha256, gc_sha512): New functions. * lib/gc-libgcrypt.c (gc_sha256, gc_sha512): New functions. * modules/crypto/gc-sha256: New file, based on modules/crypto/gc-sha1. * modules/crypto/gc-sha512: New file, based on modules/crypto/gc-sha1.
-rw-r--r--ChangeLog13
-rw-r--r--lib/gc-gnulib.c90
-rw-r--r--lib/gc-libgcrypt.c64
-rw-r--r--lib/gc.h2
-rw-r--r--modules/crypto/gc-sha25624
-rw-r--r--modules/crypto/gc-sha51224
6 files changed, 216 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 53e5b25a1d..3d701d7b3e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
2019-08-24 Bruno Haible <bruno@clisp.org>
+ crypto/gc-sha256, crypto/gc-sha512: New modules.
+ * lib/gc.h (gc_sha256, gc_sha512): New declarations.
+ * lib/gc-gnulib.c: Include sha256.h, sha512.h.
+ (MAX_DIGEST_SIZE): Set to 64.
+ (_gc_hash_ctx, gc_hash_open, gc_hash_digest_length, gc_hash_write,
+ gc_hash_read, gc_hash_buffer): Add support for sha256 and sha512.
+ (gc_sha256, gc_sha512): New functions.
+ * lib/gc-libgcrypt.c (gc_sha256, gc_sha512): New functions.
+ * modules/crypto/gc-sha256: New file, based on modules/crypto/gc-sha1.
+ * modules/crypto/gc-sha512: New file, based on modules/crypto/gc-sha1.
+
+2019-08-24 Bruno Haible <bruno@clisp.org>
+
crypto/gc-sha1 tests: Improve output when the test fails.
* tests/test-gc-sha1.c (main): In case of mismatch, print the entire
output.
diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c
index 6fcb4a1c87..21405919ee 100644
--- a/lib/gc-gnulib.c
+++ b/lib/gc-gnulib.c
@@ -48,6 +48,12 @@
#if GNULIB_GC_SHA1
# include "sha1.h"
#endif
+#if GNULIB_GC_SHA256
+# include "sha256.h"
+#endif
+#if GNULIB_GC_SHA512
+# include "sha512.h"
+#endif
#if GNULIB_GC_SM3
# include "sm3.h"
#endif
@@ -602,7 +608,7 @@ gc_cipher_close (gc_cipher_handle handle)
/* Hashes. */
-#define MAX_DIGEST_SIZE 32
+#define MAX_DIGEST_SIZE 64
typedef struct _gc_hash_ctx
{
@@ -621,6 +627,12 @@ typedef struct _gc_hash_ctx
#if GNULIB_GC_SHA1
struct sha1_ctx sha1Context;
#endif
+#if GNULIB_GC_SHA256
+ struct sha256_ctx sha256Context;
+#endif
+#if GNULIB_GC_SHA512
+ struct sha512_ctx sha512Context;
+#endif
#if GNULIB_GC_SM3
struct sm3_ctx sm3Context;
#endif
@@ -669,6 +681,18 @@ gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
break;
#endif
+#if GNULIB_GC_SHA256
+ case GC_SHA256:
+ sha256_init_ctx (&ctx->sha256Context);
+ break;
+#endif
+
+#if GNULIB_GC_SHA512
+ case GC_SHA512:
+ sha512_init_ctx (&ctx->sha512Context);
+ break;
+#endif
+
#if GNULIB_GC_SM3
case GC_SM3:
sm3_init_ctx (&ctx->sm3Context);
@@ -730,6 +754,14 @@ gc_hash_digest_length (Gc_hash hash)
len = GC_SHA1_DIGEST_SIZE;
break;
+ case GC_SHA256:
+ len = GC_SHA256_DIGEST_SIZE;
+ break;
+
+ case GC_SHA512:
+ len = GC_SHA512_DIGEST_SIZE;
+ break;
+
case GC_SM3:
len = GC_SM3_DIGEST_SIZE;
break;
@@ -772,6 +804,18 @@ gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
break;
#endif
+#if GNULIB_GC_SHA256
+ case GC_SHA256:
+ sha256_process_bytes (data, len, &ctx->sha256Context);
+ break;
+#endif
+
+#if GNULIB_GC_SHA512
+ case GC_SHA512:
+ sha512_process_bytes (data, len, &ctx->sha512Context);
+ break;
+#endif
+
#if GNULIB_GC_SM3
case GC_SM3:
sm3_process_bytes (data, len, &ctx->sm3Context);
@@ -819,6 +863,20 @@ gc_hash_read (gc_hash_handle handle)
break;
#endif
+#if GNULIB_GC_SHA256
+ case GC_SHA256:
+ sha256_finish_ctx (&ctx->sha256Context, ctx->hash);
+ ret = ctx->hash;
+ break;
+#endif
+
+#if GNULIB_GC_SHA512
+ case GC_SHA512:
+ sha512_finish_ctx (&ctx->sha512Context, ctx->hash);
+ ret = ctx->hash;
+ break;
+#endif
+
#if GNULIB_GC_SM3
case GC_SM3:
sm3_finish_ctx (&ctx->sm3Context, ctx->hash);
@@ -870,6 +928,18 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
break;
#endif
+#if GNULIB_GC_SHA256
+ case GC_SHA256:
+ sha256_buffer (in, inlen, resbuf);
+ break;
+#endif
+
+#if GNULIB_GC_SHA512
+ case GC_SHA512:
+ sha512_buffer (in, inlen, resbuf);
+ break;
+#endif
+
#if GNULIB_GC_SM3
case GC_SM3:
sm3_buffer (in, inlen, resbuf);
@@ -919,6 +989,24 @@ gc_sha1 (const void *in, size_t inlen, void *resbuf)
}
#endif
+#if GNULIB_GC_SHA256
+Gc_rc
+gc_sha256 (const void *in, size_t inlen, void *resbuf)
+{
+ sha256_buffer (in, inlen, resbuf);
+ return GC_OK;
+}
+#endif
+
+#if GNULIB_GC_SHA512
+Gc_rc
+gc_sha512 (const void *in, size_t inlen, void *resbuf)
+{
+ sha512_buffer (in, inlen, resbuf);
+ return GC_OK;
+}
+#endif
+
#if GNULIB_GC_SM3
Gc_rc
gc_sm3 (const void *in, size_t inlen, void *resbuf)
diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c
index 3ca17c2639..8ea121a247 100644
--- a/lib/gc-libgcrypt.c
+++ b/lib/gc-libgcrypt.c
@@ -707,6 +707,70 @@ gc_sha1 (const void *in, size_t inlen, void *resbuf)
}
#endif
+#if GNULIB_GC_SHA256
+Gc_rc
+gc_sha256 (const void *in, size_t inlen, void *resbuf)
+{
+ size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA256);
+ gcry_md_hd_t hd;
+ gpg_error_t err;
+ unsigned char *p;
+
+ assert (outlen == GC_SHA256_DIGEST_SIZE);
+
+ err = gcry_md_open (&hd, GCRY_MD_SHA256, 0);
+ if (err != GPG_ERR_NO_ERROR)
+ return GC_INVALID_HASH;
+
+ gcry_md_write (hd, in, inlen);
+
+ p = gcry_md_read (hd, GCRY_MD_SHA256);
+ if (p == NULL)
+ {
+ gcry_md_close (hd);
+ return GC_INVALID_HASH;
+ }
+
+ memcpy (resbuf, p, outlen);
+
+ gcry_md_close (hd);
+
+ return GC_OK;
+}
+#endif
+
+#if GNULIB_GC_SHA512
+Gc_rc
+gc_sha512 (const void *in, size_t inlen, void *resbuf)
+{
+ size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA512);
+ gcry_md_hd_t hd;
+ gpg_error_t err;
+ unsigned char *p;
+
+ assert (outlen == GC_SHA512_DIGEST_SIZE);
+
+ err = gcry_md_open (&hd, GCRY_MD_SHA512, 0);
+ if (err != GPG_ERR_NO_ERROR)
+ return GC_INVALID_HASH;
+
+ gcry_md_write (hd, in, inlen);
+
+ p = gcry_md_read (hd, GCRY_MD_SHA512);
+ if (p == NULL)
+ {
+ gcry_md_close (hd);
+ return GC_INVALID_HASH;
+ }
+
+ memcpy (resbuf, p, outlen);
+
+ gcry_md_close (hd);
+
+ return GC_OK;
+}
+#endif
+
#if GNULIB_GC_SM3
Gc_rc
gc_sm3 (const void *in, size_t inlen, void *resbuf)
diff --git a/lib/gc.h b/lib/gc.h
index b5f8327dcd..d591408f3e 100644
--- a/lib/gc.h
+++ b/lib/gc.h
@@ -159,6 +159,8 @@ extern Gc_rc gc_md2 (const void *in, size_t inlen, void *resbuf);
extern Gc_rc gc_md4 (const void *in, size_t inlen, void *resbuf);
extern Gc_rc gc_md5 (const void *in, size_t inlen, void *resbuf);
extern Gc_rc gc_sha1 (const void *in, size_t inlen, void *resbuf);
+extern Gc_rc gc_sha256 (const void *in, size_t inlen, void *resbuf);
+extern Gc_rc gc_sha512 (const void *in, size_t inlen, void *resbuf);
extern Gc_rc gc_sm3 (const void *in, size_t inlen, void *resbuf);
extern Gc_rc gc_hmac_md5 (const void *key, size_t keylen,
const void *in, size_t inlen, char *resbuf);
diff --git a/modules/crypto/gc-sha256 b/modules/crypto/gc-sha256
new file mode 100644
index 0000000000..ee3156fe3b
--- /dev/null
+++ b/modules/crypto/gc-sha256
@@ -0,0 +1,24 @@
+Description:
+Generic crypto wrappers for SHA-256 functions.
+
+Files:
+m4/gc-sha256.m4
+
+Depends-on:
+crypto/gc
+crypto/sha256 [test "$ac_cv_libgcrypt" != yes]
+
+configure.ac:
+gl_GC_SHA256
+gl_MODULE_INDICATOR([gc-sha256])
+
+Makefile.am:
+
+Include:
+"gc.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
diff --git a/modules/crypto/gc-sha512 b/modules/crypto/gc-sha512
new file mode 100644
index 0000000000..7372fec243
--- /dev/null
+++ b/modules/crypto/gc-sha512
@@ -0,0 +1,24 @@
+Description:
+Generic crypto wrappers for SHA-512 functions.
+
+Files:
+m4/gc-sha512.m4
+
+Depends-on:
+crypto/gc
+crypto/sha512 [test "$ac_cv_libgcrypt" != yes]
+
+configure.ac:
+gl_GC_SHA512
+gl_MODULE_INDICATOR([gc-sha512])
+
+Makefile.am:
+
+Include:
+"gc.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson

Return to:

Send suggestions and report system problems to the System administrator.