From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id CD9A91F461 for ; Sat, 24 Aug 2019 14:17:42 +0000 (UTC) Received: from localhost ([::1]:38024 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i1WrO-0001Na-0R for normalperson@yhbt.net; Sat, 24 Aug 2019 10:17:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50412) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i1WrK-0001NU-EE for bug-gnulib@gnu.org; Sat, 24 Aug 2019 10:17:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i1WrJ-0000BM-2o for bug-gnulib@gnu.org; Sat, 24 Aug 2019 10:17:38 -0400 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::11]:15101) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i1WrH-0000A8-J1 for bug-gnulib@gnu.org; Sat, 24 Aug 2019 10:17:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1566656253; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=mNxk6kRw95BF694uWYz3v5SmEg9zWw5XOM5nCIcdraQ=; b=RC9FNUsL7wXTv3PJIe8EZ7TCmqCNZFk+hYPiEbDibxAiM/NuiMct2UxqPT5nbU5q4X kpsg0bpweoJFfTBKPUnMFKdGHm+k9gi3RzVmma6+vB8u/oolso07caDj8ft2SoPZedjw 9qewqbjM9Y0oywzfCZHEImbwf+jfegg/IgYsSbEVwxyy3uSm1IOuoO0hr+dRqh8kVF3v /ggy6mMrFGMVtydtmyFXKdyw/ichukIKJAfvWsvB8eP5PvZYHcHAJNZNWAj/5M797YhZ AxveIbvQ9+ZwcELpxh4dcVvl+CF/ynIWUee3Ys2T0TVLDDQ852wUJgO7EK5S0XjOAhb9 lqiQ== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOGaf0zJZW" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 44.26.1 DYNA|AUTH) with ESMTPSA id 507e7dv7OEHSKW8 (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Sat, 24 Aug 2019 16:17:28 +0200 (CEST) From: Bruno Haible To: bug-gnulib@gnu.org Subject: crypto/gc-sm3: fix compilation error with --with-libgcrypt Date: Sat, 24 Aug 2019 16:17:28 +0200 Message-ID: <6018959.GhRQf1i402@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-157-generic; KDE/5.18.0; x86_64; ; ) MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a01:238:20a:202:5300::11 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jia Zhang Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" This fails: $ ./gnulib-tool --create-testdir --dir=../testdir1 --single-configure crypto/gc-sm3 $ cd ../testdir1 $ ./configure --with-libgcrypt $ make ... gc-libgcrypt.c: In function 'gc_hash_open': gc-libgcrypt.c:309:17: error: 'GCRY_MD_SM3' undeclared (first use in this function) gcryalg = GCRY_MD_SM3; ^ gc-libgcrypt.c:309:17: note: each undeclared identifier is reported only once for each function it appears in gc-libgcrypt.c: In function 'gc_hash_buffer': gc-libgcrypt.c:545:17: error: 'GCRY_MD_SM3' undeclared (first use in this function) gcryalg = GCRY_MD_SM3; ^ gc-libgcrypt.c: In function 'gc_sm3': gc-libgcrypt.c:669:42: error: 'GCRY_MD_SM3' undeclared (first use in this function) size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SM3); ^ Makefile:640: recipe for target 'gc-libgcrypt.o' failed make[4]: *** [gc-libgcrypt.o] Error 1 I'm using libgcrypt 1.6.5. This patch fixes it. It follows the coding idiom for md2 (which is also present in gnulib but not in libgcrypt), but leaves open the possibility that sm3 supports gets added in a future version of libgcrypt. AFAICS, it is scheduled for inclusion in libgcrypt 1.9. 2019-08-24 Bruno Haible 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. diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c index fbfd0a1..3ca17c2 100644 --- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -33,6 +33,9 @@ MD2 algorithm. Therefore take the implementation from gnulib. */ # include "md2.h" #endif +#if GNULIB_GC_SM3 && !LIBGCRYPT_HAS_MD_SM3 +# include "sm3.h" +#endif #include @@ -243,14 +246,22 @@ gc_cipher_close (gc_cipher_handle handle) /* Hashes. */ +/* Maximum of GC_MD2_DIGEST_SIZE and GC_SM3_DIGEST_SIZE. */ +#define MAX_DIGEST_SIZE 32 + typedef struct _gc_hash_ctx { Gc_hash alg; Gc_hash_mode mode; 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; Gc_rc @@ -312,7 +323,12 @@ gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) #if GNULIB_GC_SM3 case GC_SM3: +# if LIBGCRYPT_HAS_MD_SM3 gcryalg = GCRY_MD_SM3; +# else + sm3_init_ctx (&ctx->sm3Context); + gcryalg = GCRY_MD_NONE; +# endif break; #endif @@ -433,7 +449,10 @@ gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key) #if GNULIB_GC_MD2 if (ctx->alg != GC_MD2) #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); } void @@ -446,6 +465,11 @@ gc_hash_write (gc_hash_handle handle, size_t len, const char *data) md2_process_bytes (data, len, &ctx->md2Context); else #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); } @@ -463,6 +487,14 @@ gc_hash_read (gc_hash_handle handle) } else #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 { gcry_md_final (ctx->gch); digest = (const char *) gcry_md_read (ctx->gch, 0); @@ -479,7 +511,10 @@ gc_hash_close (gc_hash_handle handle) #if GNULIB_GC_MD2 if (ctx->alg != GC_MD2) #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); free (ctx); } @@ -495,7 +530,6 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) case GC_MD2: md2_buffer (in, inlen, resbuf); return GC_OK; - break; #endif #if GNULIB_GC_MD4 @@ -548,8 +582,13 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) #if GNULIB_GC_SM3 case GC_SM3: +# if !LIBGCRYPT_HAS_MD_SM3 + sm3_buffer (in, inlen, resbuf); + return GC_OK; +# else gcryalg = GCRY_MD_SM3; break; +# endif #endif default: @@ -672,6 +711,10 @@ gc_sha1 (const void *in, size_t inlen, void *resbuf) Gc_rc 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); gcry_md_hd_t hd; gpg_error_t err; @@ -697,6 +740,7 @@ gc_sm3 (const void *in, size_t inlen, void *resbuf) gcry_md_close (hd); return GC_OK; +# endif } #endif diff --git a/m4/gc-sm3.m4 b/m4/gc-sm3.m4 index 7eac448..992217d 100644 --- a/m4/gc-sm3.m4 +++ b/m4/gc-sm3.m4 @@ -1,4 +1,4 @@ -# gc-sm3.m4 serial 1 +# gc-sm3.m4 serial 2 dnl Copyright (C) 2017-2019 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -7,4 +7,19 @@ dnl with or without modifications, as long as this notice is preserved. 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 + 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 ])