diff options
author | Bruno Haible <bruno@clisp.org> | 2019-08-24 16:16:33 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2019-08-24 16:16:33 +0200 |
commit | cac148f96e7c2296a14abff6d7130b53e76bfe63 (patch) | |
tree | 22129619b8da1bfad6a05c411284bd61d42e6653 | |
parent | 57959b9e826f42c3aa5bad7efff06e9ad3dc63f6 (diff) | |
download | gnulib-cac148f96e7c2296a14abff6d7130b53e76bfe63.tar.gz gnulib-cac148f96e7c2296a14abff6d7130b53e76bfe63.tar.bz2 |
crypto/gc-sm3: Fix compilation error with --with-libgcrypt.
* m4/gc-sm3.m4 (gl_GC_SM3): Test whether libgcrypt supports SM3. Define
LIBGCRYPT_HAS_MD_SM3.
* lib/gc-libgcrypt.c: Include sm3.h.
(_gc_hash_ctx, gc_hash_open, gc_hash_hmac_setkey, gc_hash_write,
gc_hash_read, gc_hash_close, gc_hash_buffer, gc_sm3): Use the gnulib
implementation if libgcrypt does not support SM3.
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | lib/gc-libgcrypt.c | 52 | ||||
-rw-r--r-- | m4/gc-sm3.m4 | 17 |
3 files changed, 74 insertions, 5 deletions
@@ -2,2 +2,12 @@ + crypto/gc-sm3: Fix compilation error with --with-libgcrypt. + * m4/gc-sm3.m4 (gl_GC_SM3): Test whether libgcrypt supports SM3. Define + LIBGCRYPT_HAS_MD_SM3. + * lib/gc-libgcrypt.c: Include sm3.h. + (_gc_hash_ctx, gc_hash_open, gc_hash_hmac_setkey, gc_hash_write, + gc_hash_read, gc_hash_close, gc_hash_buffer, gc_sm3): Use the gnulib + implementation if libgcrypt does not support SM3. + +2019-08-24 Bruno Haible <bruno@clisp.org> + crypto/gc-md2: Optimize and clarify code. diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c index fbfd0a138f..3ca17c2639 100644 --- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -35,2 +35,5 @@ #endif +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 +# include "sm3.h" +#endif @@ -245,2 +248,5 @@ gc_cipher_close (gc_cipher_handle handle) +/* Maximum of GC_MD2_DIGEST_SIZE and GC_SM3_DIGEST_SIZE. */ +#define MAX_DIGEST_SIZE 32 + typedef struct _gc_hash_ctx { @@ -249,6 +255,11 @@ typedef struct _gc_hash_ctx { gcry_md_hd_t gch; +#if GNULIB_GC_MD2 || (GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3) + char hash[MAX_DIGEST_SIZE]; +#endif #if GNULIB_GC_MD2 - char hash[GC_MD2_DIGEST_SIZE]; struct md2_ctx md2Context; #endif +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 + struct sm3_ctx sm3Context; +#endif } _gc_hash_ctx; @@ -314,3 +325,8 @@ gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) case GC_SM3: +# if LIBGCRYPT_HAS_MD_SM3 gcryalg = GCRY_MD_SM3; +# else + sm3_init_ctx (&ctx->sm3Context); + gcryalg = GCRY_MD_NONE; +# endif break; @@ -435,3 +451,6 @@ gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key) #endif - gcry_md_setkey (ctx->gch, key, len); +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 + if (ctx->alg != GC_SM3) +#endif + gcry_md_setkey (ctx->gch, key, len); } @@ -448,2 +467,7 @@ gc_hash_write (gc_hash_handle handle, size_t len, const char *data) #endif +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 + if (ctx->alg == GC_SM3) + sm3_process_bytes (data, len, &ctx->sm3Context); + else +#endif gcry_md_write (ctx->gch, data, len); @@ -465,2 +489,10 @@ gc_hash_read (gc_hash_handle handle) #endif +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 + if (ctx->alg == GC_SM3) + { + sm3_finish_ctx (&ctx->sm3Context, ctx->hash); + digest = ctx->hash; + } + else +#endif { @@ -481,3 +513,6 @@ gc_hash_close (gc_hash_handle handle) #endif - gcry_md_close (ctx->gch); +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 + if (ctx->alg != GC_SM3) +#endif + gcry_md_close (ctx->gch); @@ -497,3 +532,2 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) return GC_OK; - break; #endif @@ -550,4 +584,9 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) case GC_SM3: +# if !LIBGCRYPT_HAS_MD_SM3 + sm3_buffer (in, inlen, resbuf); + return GC_OK; +# else gcryalg = GCRY_MD_SM3; break; +# endif #endif @@ -674,2 +713,6 @@ gc_sm3 (const void *in, size_t inlen, void *resbuf) { +# if !LIBGCRYPT_HAS_MD_SM3 + sm3_buffer (in, inlen, resbuf); + return GC_OK; +# else size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SM3); @@ -699,2 +742,3 @@ gc_sm3 (const void *in, size_t inlen, void *resbuf) return GC_OK; +# endif } diff --git a/m4/gc-sm3.m4 b/m4/gc-sm3.m4 index 7eac44818d..992217d9fe 100644 --- a/m4/gc-sm3.m4 +++ b/m4/gc-sm3.m4 @@ -1,2 +1,2 @@ -# gc-sm3.m4 serial 1 +# gc-sm3.m4 serial 2 dnl Copyright (C) 2017-2019 Free Software Foundation, Inc. @@ -9,2 +9,17 @@ AC_DEFUN([gl_GC_SM3], AC_REQUIRE([gl_GC]) + AC_CACHE_CHECK([whether libgcrypt supports SM3], + [gl_cv_libcrypt_md_sm3], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[ + #include <gcrypt.h> + int a = GCRY_MD_SM3; + ]], [[]]) + ], + [gl_cv_libcrypt_md_sm3=yes], + [gl_cv_libcrypt_md_sm3=no]) + ]) + if test $gl_cv_libcrypt_md_sm3 = yes; then + AC_DEFINE([LIBGCRYPT_HAS_MD_SM3], [1], + [Define if libgcrypt supports the MD algorithm SM3.]) + fi ]) |