bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] crypto: add missing file af_alg.h
@ 2021-09-07  1:44 Paul Eggert
  2021-09-07 18:15 ` Bruno Haible
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Eggert @ 2021-09-07  1:44 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Paul Eggert

* modules/crypto/md5-buffer, modules/crypto/sha1-buffer:
* modules/crypto/sha256-buffer, modules/crypto/sha512-buffer:
(Files): Add lib/af_alg.h, since the .c files include it.
---
 ChangeLog                    | 7 +++++++
 modules/crypto/md5-buffer    | 1 +
 modules/crypto/sha1-buffer   | 1 +
 modules/crypto/sha256-buffer | 1 +
 modules/crypto/sha512-buffer | 1 +
 5 files changed, 11 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 39a892e28..76bf89555 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2021-09-06  Paul Eggert  <eggert@cs.ucla.edu>
+
+	crypto: add missing file af_alg.h
+	* modules/crypto/md5-buffer, modules/crypto/sha1-buffer:
+	* modules/crypto/sha256-buffer, modules/crypto/sha512-buffer:
+	(Files): Add lib/af_alg.h, since the .c files include it.
+
 2021-09-04  Paul Eggert  <eggert@cs.ucla.edu>
 
 	idx: break copying from glibc
diff --git a/modules/crypto/md5-buffer b/modules/crypto/md5-buffer
index cd51cab2d..a929046b9 100644
--- a/modules/crypto/md5-buffer
+++ b/modules/crypto/md5-buffer
@@ -2,6 +2,7 @@ Description:
 Compute MD5 checksum.
 
 Files:
+lib/af_alg.h
 lib/gl_openssl.h
 lib/md5.h
 lib/md5.c
diff --git a/modules/crypto/sha1-buffer b/modules/crypto/sha1-buffer
index a934d4e06..7cbdab8ca 100644
--- a/modules/crypto/sha1-buffer
+++ b/modules/crypto/sha1-buffer
@@ -2,6 +2,7 @@ Description:
 Compute SHA1 checksum.
 
 Files:
+lib/af_alg.h
 lib/gl_openssl.h
 lib/sha1.h
 lib/sha1.c
diff --git a/modules/crypto/sha256-buffer b/modules/crypto/sha256-buffer
index 85529a342..ddcac42d3 100644
--- a/modules/crypto/sha256-buffer
+++ b/modules/crypto/sha256-buffer
@@ -2,6 +2,7 @@ Description:
 Compute SHA224 and SHA256 checksums.
 
 Files:
+lib/af_alg.h
 lib/gl_openssl.h
 lib/sha256.h
 lib/sha256.c
diff --git a/modules/crypto/sha512-buffer b/modules/crypto/sha512-buffer
index 416a0b520..4bf1b80e2 100644
--- a/modules/crypto/sha512-buffer
+++ b/modules/crypto/sha512-buffer
@@ -2,6 +2,7 @@ Description:
 Compute SHA384 and SHA512 checksums.
 
 Files:
+lib/af_alg.h
 lib/gl_openssl.h
 lib/sha512.h
 lib/sha512.c
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] crypto: add missing file af_alg.h
  2021-09-07  1:44 [PATCH] crypto: add missing file af_alg.h Paul Eggert
@ 2021-09-07 18:15 ` Bruno Haible
  2021-09-07 21:51   ` Paul Eggert
  0 siblings, 1 reply; 7+ messages in thread
From: Bruno Haible @ 2021-09-07 18:15 UTC (permalink / raw)
  To: bug-gnulib, Paul Eggert

Paul Eggert wrote:
> * modules/crypto/md5-buffer, modules/crypto/sha1-buffer:
> * modules/crypto/sha256-buffer, modules/crypto/sha512-buffer:
> (Files): Add lib/af_alg.h, since the .c files include it.

This looks unnecessary to me. The '#include "af_alg.h"' occurs
only inside '#if defined GL_COMPILE_CRYPTO_STREAM' code, and
GL_COMPILE_CRYPTO_STREAM is supposed to only get defined by the
modules
  crypto/md5
  crypto/sha1
  crypto/sha256
  crypto/sha512
 - and these module includes the dependency to the module 'crypto/af_alg'.

Bruno





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] crypto: add missing file af_alg.h
  2021-09-07 18:15 ` Bruno Haible
@ 2021-09-07 21:51   ` Paul Eggert
  2021-09-07 23:23     ` Bruno Haible
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Eggert @ 2021-09-07 21:51 UTC (permalink / raw)
  To: Bruno Haible; +Cc: Gnulib bugs

On 9/7/21 11:15 AM, Bruno Haible wrote:

> This looks unnecessary to me.

Oh, you're right. I got confused by a recent attempt of mine to merge 
recent Gnulib into Emacs. Emacs avoids some modules, which led to 
af_alg.h going missing there, and I thought this was a Gnulib problem.

I will fix the problem on the Emacs side, and have reverted the Gnulib 
patch. Thanks for reporting the problem.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] crypto: add missing file af_alg.h
  2021-09-07 21:51   ` Paul Eggert
@ 2021-09-07 23:23     ` Bruno Haible
  2021-09-08  0:12       ` Paul Eggert
  2021-09-19 13:20       ` Improve 'configure --help' output for crypto modules Bruno Haible
  0 siblings, 2 replies; 7+ messages in thread
From: Bruno Haible @ 2021-09-07 23:23 UTC (permalink / raw)
  To: Paul Eggert; +Cc: bug-gnulib

Paul Eggert wrote:
> I got confused by a recent attempt of mine to merge 
> recent Gnulib into Emacs.

And I got confused by
1) a source code file which implements two modules at the same
   time, and the flag GL_COMPILE_CRYPTO_STREAM which is set by one
   of the modules and has an effect on the compilation unit of the
   other module.
2) the 'configure --help' output which suggest to use an option
   '--with-linux-crypto' — but that option does not exist.

For 1), I would like to split the shaN.c file: move out the *_stream
functions to a separate file shaN-stream.c. Likewise for md5.c.

This will cause some divergence of md5.c w.r.t. glibc. To me, that
sounds OK, since md5.c rarely changes in gnulib and has not changed
in glibc since 2012. Is that OK with you too?

Bruno





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] crypto: add missing file af_alg.h
  2021-09-07 23:23     ` Bruno Haible
@ 2021-09-08  0:12       ` Paul Eggert
  2021-09-19  0:02         ` crypto digests modules restructuring Bruno Haible
  2021-09-19 13:20       ` Improve 'configure --help' output for crypto modules Bruno Haible
  1 sibling, 1 reply; 7+ messages in thread
From: Paul Eggert @ 2021-09-08  0:12 UTC (permalink / raw)
  To: Bruno Haible; +Cc: bug-gnulib

On 9/7/21 4:23 PM, Bruno Haible wrote:
> This will cause some divergence of md5.c w.r.t. glibc. To me, that
> sounds OK, since md5.c rarely changes in gnulib and has not changed
> in glibc since 2012. Is that OK with you too?

It's fine, so long as we keep the files shareable between glibc and 
Gnulib. That is, I would prefer the divergence to be easily become a 
non-divergence, by copying the relevant files back to glibc eventually.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* crypto digests modules restructuring
  2021-09-08  0:12       ` Paul Eggert
@ 2021-09-19  0:02         ` Bruno Haible
  0 siblings, 0 replies; 7+ messages in thread
From: Bruno Haible @ 2021-09-19  0:02 UTC (permalink / raw)
  To: Paul Eggert; +Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 6215 bytes --]

Paul Eggert wrote:
> > This will cause some divergence of md5.c w.r.t. glibc. To me, that
> > sounds OK, since md5.c rarely changes in gnulib and has not changed
> > in glibc since 2012. Is that OK with you too?
> 
> It's fine, so long as we keep the files shareable between glibc and 
> Gnulib. That is, I would prefer the divergence to be easily become a 
> non-divergence, by copying the relevant files back to glibc eventually.

OK, I've pushed this restructuring of the crypto digest modules
(md2, md4, md5, sha1, sha256, sha512, sm3), along these lines:
  - separate source files for separate modules,
  - in the tests, make it clear whether a test tests the buffer API
    or the stream API.

For sha256, sha512, the source code became closer to the glibc one,
since glibc does not have the stream API for these.

For md5, porting back to glibc will imply copying the file lib/md5-stream.c
into glibc.


2021-09-18  Bruno Haible  <bruno@clisp.org>

	sm3-buffer tests: New module.
	* tests/test-sm3-buffer.c: Renamed from tests/test-sm3.c.
	* modules/crypto/sm3-buffer-tests: Renamed from
	modules/crypto/sm3-tests. Test tests/test-sm3-buffer.c instead of
	tests/test-sm3.c.

	sm3-buffer: New module.
	* lib/sm3-stream.c: New file, extracted from lib/sm3.c.
	* lib/sm3.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, sm3_stream): Moved to sm3-stream.c.
	* lib/sm3.h: Tweak.
	* modules/crypto/sm3-buffer: New file, based on modules/crypto/sm3.
	* modules/crypto/sm3: Rewritten.

2021-09-18  Bruno Haible  <bruno@clisp.org>

	sha512-buffer tests: New module.
	* tests/test-sha512-stream.c: Renamed from tests/test-sha512.c.
	* modules/crypto/sha512-buffer-tests: New file, based on
	modules/crypto/sha512-tests.
	* modules/crypto/sha512-tests: Remove tests that are now in
	modules/crypto/sha512-buffer-tests. Test tests/test-sha512-stream.c
	instead of tests/test-sha512.c.

	sha512: Clarify module to source relation.
	* lib/sha512-stream.c: New file, extracted from lib/sha512.c.
	* lib/sha512.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, shaxxx_stream, sha512_stream, sha384_stream): Moved to
	sha512-stream.c.
	* lib/sha512.h: Tweak.
	* modules/crypto/sha512 (Files): Add lib/sha512-stream.c.
	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
	(Makefile.am): Arrange to compile sha512-stream.c.
	(Link): Mention $(LIB_CRYPTO).

2021-09-18  Bruno Haible  <bruno@clisp.org>

	sha256-buffer tests: New module.
	* tests/test-sha256-stream.c: Renamed from tests/test-sha256.c.
	* modules/crypto/sha256-buffer-tests: New file, based on
	modules/crypto/sha256-tests.
	* modules/crypto/sha256-tests: Remove tests that are now in
	modules/crypto/sha256-buffer-tests. Test tests/test-sha256-stream.c
	instead of tests/test-sha256.c.

	sha256: Clarify module to source relation.
	* lib/sha256-stream.c: New file, extracted from lib/sha256.c.
	* lib/sha256.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, shaxxx_stream, sha256_stream, sha224_stream): Moved to
	sha256-stream.c.
	* lib/sha256.h: Tweak.
	* modules/crypto/sha256 (Files): Add lib/sha256-stream.c.
	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
	(Makefile.am): Arrange to compile sha256-stream.c.
	(Link): Mention $(LIB_CRYPTO).

2021-09-18  Bruno Haible  <bruno@clisp.org>

	sha1-buffer tests: New module.
	* tests/test-sha1-buffer.c: New file, based on tests/test-sha1.c.
	* tests/test-sha1-stream.c: Renamed from tests/test-sha1.c.
	(main): Remove sha1-buffer tests.
	* modules/crypto/sha1-buffer-tests: New file, based on
	modules/crypto/sha1-tests.
	* modules/crypto/sha1-tests: Remove tests that are now in
	modules/crypto/sha1-buffer-tests. Test tests/test-sha1-stream.c instead
	of tests/test-sha1.c.

	sha1: Clarify module to source relation.
	* lib/sha1-stream.c: New file, extracted from lib/sha1.c.
	* lib/sha1.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, sha1_stream): Moved to sha1-stream.c.
	* lib/sha1.h: Correct indentation.
	* modules/crypto/sha1 (Files): Add lib/sha1-stream.c.
	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
	(Makefile.am): Arrange to compile sha1-stream.c.
	(Link): Mention $(LIB_CRYPTO).

2021-09-18  Bruno Haible  <bruno@clisp.org>

	md5-buffer tests: New module.
	* tests/test-md5-buffer.c: New file, based on tests/test-md5.c.
	* tests/test-md5-stream.c: Renamed from tests/test-md5.c.
	(main): Remove md5-buffer tests.
	* modules/crypto/md5-buffer-tests: New file, based on
	modules/crypto/md5-tests.
	* modules/crypto/md5-tests: Remove tests that are now in
	modules/crypto/md5-buffer-tests. Test tests/test-md5-stream.c instead
	of tests/test-md5.c.

	md5: Clarify module to source relation.
	* lib/md5-stream.c: New file, extracted from lib/md5.c.
	* lib/md5.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, md5_stream): Moved to md5-stream.c.
	* lib/md5.h: Tweak.
	* modules/crypto/md5 (Files): Add lib/md5-stream.c.
	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
	(Makefile.am): Arrange to compile md5-stream.c.
	(Link): Mention $(LIB_CRYPTO).

2021-09-18  Bruno Haible  <bruno@clisp.org>

	md4-buffer tests: New module.
	* tests/test-md4-buffer.c: Renamed from tests/test-md4.c.
	* modules/crypto/md4-buffer-tests: Renamed from
	modules/crypto/md4-tests. Test tests/test-md4-buffer.c instead of
	tests/test-md4.c.

	md4-buffer: New module.
	* lib/md4-stream.c: New file, extracted from lib/md4.c.
	* lib/md4.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, md4_stream): Moved to md4-stream.c.
	* lib/md4.h: Reorder declarations.
	* modules/crypto/md4-buffer: New file, based on modules/crypto/md4.
	* modules/crypto/md4: Rewritten.

2021-09-18  Bruno Haible  <bruno@clisp.org>

	md2-buffer tests: New module.
	* tests/test-md2-buffer.c: Renamed from tests/test-md2.c.
	* modules/crypto/md2-buffer-tests: Renamed from
	modules/crypto/md2-tests. Test tests/test-md2-buffer.c instead of
	tests/test-md2.c.

	md2-buffer: New module.
	* lib/md2-stream.c: New file, extracted from lib/md2.c.
	* lib/md2.c: Don't include stdlib.h, unlocked-io.h.
	(BLOCKSIZE, md2_stream): Moved to md2-stream.c.
	* lib/md2.h: Reorder declarations.
	* modules/crypto/md2-buffer: New file, based on modules/crypto/md2.
	* modules/crypto/md2: Rewritten.


[-- Attachment #2: 0001-md2-buffer-New-module.patch --]
[-- Type: text/x-patch, Size: 9926 bytes --]

From 1cae2a35d45acdfc2c094f64fd3344c01f815673 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:29:33 +0200
Subject: [PATCH 01/14] md2-buffer: New module.

* lib/md2-stream.c: New file, extracted from lib/md2.c.
* lib/md2.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, md2_stream): Moved to md2-stream.c.
* lib/md2.h: Reorder declarations.
* modules/crypto/md2-buffer: New file, based on modules/crypto/md2.
* modules/crypto/md2: Rewritten.
---
 ChangeLog                 |  10 ++++
 lib/md2-stream.c          | 109 ++++++++++++++++++++++++++++++++++++++
 lib/md2.c                 |  84 +----------------------------
 lib/md2.h                 |  12 +++--
 modules/crypto/md2        |   8 ++-
 modules/crypto/md2-buffer |  24 +++++++++
 6 files changed, 154 insertions(+), 93 deletions(-)
 create mode 100644 lib/md2-stream.c
 create mode 100644 modules/crypto/md2-buffer

diff --git a/ChangeLog b/ChangeLog
index 61a7e58e0..ed35ce693 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	md2-buffer: New module.
+	* lib/md2-stream.c: New file, extracted from lib/md2.c.
+	* lib/md2.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, md2_stream): Moved to md2-stream.c.
+	* lib/md2.h: Reorder declarations.
+	* modules/crypto/md2-buffer: New file, based on modules/crypto/md2.
+	* modules/crypto/md2: Rewritten.
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	string, wchar: Don't cause link errors for rpl_free (regr. 2021-09-07).
diff --git a/lib/md2-stream.c b/lib/md2-stream.c
new file mode 100644
index 000000000..83eb1d597
--- /dev/null
+++ b/lib/md2-stream.c
@@ -0,0 +1,109 @@
+/* Functions to compute MD2 message digest of files or memory blocks.
+   according to the definition of MD2 in RFC 1319 from April 1992.
+   Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2021 Free Software
+   Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Adapted by Simon Josefsson from public domain Libtomcrypt 1.06 by
+   Tom St Denis. */
+
+#include <config.h>
+
+/* Specification.  */
+#include "md2.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute MD2 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+int
+md2_stream (FILE *stream, void *resblock)
+{
+  struct md2_ctx ctx;
+  size_t sum;
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  /* Initialize the computation context.  */
+  md2_init_ctx (&ctx);
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+
+          /* We've read at least one byte, so ignore errors.  But always
+             check for EOF, since feof may be true even though N > 0.
+             Otherwise, we could end up calling fread after EOF.  */
+          if (feof (stream))
+            goto process_partial_block;
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+         BLOCKSIZE % 64 == 0
+       */
+      md2_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    md2_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  md2_finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
diff --git a/lib/md2.c b/lib/md2.c
index 85b4d2029..5dc811977 100644
--- a/lib/md2.c
+++ b/lib/md2.c
@@ -21,23 +21,14 @@
 
 #include <config.h>
 
+/* Specification.  */
 #include "md2.h"
 
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 
 #include <minmax.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
 static void md2_update_chksum (struct md2_ctx *md);
 static void md2_compress (struct md2_ctx *md);
 
@@ -87,79 +78,6 @@ md2_finish_ctx (struct md2_ctx *ctx, void *resbuf)
   return md2_read_ctx (ctx, resbuf);
 }
 
-/* Compute MD2 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-int
-md2_stream (FILE *stream, void *resblock)
-{
-  struct md2_ctx ctx;
-  size_t sum;
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  /* Initialize the computation context.  */
-  md2_init_ctx (&ctx);
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-         BLOCKSIZE % 64 == 0
-       */
-      md2_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
-process_partial_block:;
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    md2_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  md2_finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
-
 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
diff --git a/lib/md2.h b/lib/md2.h
index 078756a00..f85799590 100644
--- a/lib/md2.h
+++ b/lib/md2.h
@@ -66,11 +66,6 @@ extern void *md2_finish_ctx (struct md2_ctx *ctx, void *restrict resbuf);
 extern void *md2_read_ctx (const struct md2_ctx *ctx, void *restrict resbuf);
 
 
-/* Compute MD2 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-extern int md2_stream (FILE *stream, void *resblock);
-
 /* Compute MD2 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -78,6 +73,13 @@ extern int md2_stream (FILE *stream, void *resblock);
 extern void *md2_buffer (const char *buffer, size_t len,
                          void *restrict resblock);
 
+
+/* Compute MD2 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+extern int md2_stream (FILE *stream, void *resblock);
+
+
 # ifdef __cplusplus
 }
 # endif
diff --git a/modules/crypto/md2 b/modules/crypto/md2
index d89eb9564..da418bc3b 100644
--- a/modules/crypto/md2
+++ b/modules/crypto/md2
@@ -2,17 +2,15 @@ Description:
 Compute MD2 checksum.
 
 Files:
-lib/md2.h
-lib/md2.c
+lib/md2-stream.c
 
 Depends-on:
-minmax
+crypto/md2-buffer
 
 configure.ac:
-AC_REQUIRE([AC_C_RESTRICT])
 
 Makefile.am:
-lib_SOURCES += md2.c
+lib_SOURCES += md2-stream.c
 
 Include:
 "md2.h"
diff --git a/modules/crypto/md2-buffer b/modules/crypto/md2-buffer
new file mode 100644
index 000000000..d89eb9564
--- /dev/null
+++ b/modules/crypto/md2-buffer
@@ -0,0 +1,24 @@
+Description:
+Compute MD2 checksum.
+
+Files:
+lib/md2.h
+lib/md2.c
+
+Depends-on:
+minmax
+
+configure.ac:
+AC_REQUIRE([AC_C_RESTRICT])
+
+Makefile.am:
+lib_SOURCES += md2.c
+
+Include:
+"md2.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
-- 
2.25.1


[-- Attachment #3: 0002-md2-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 2138 bytes --]

From 341e0c26819ce4c1a26b95b3c780bed88036d211 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:30:12 +0200
Subject: [PATCH 02/14] md2-buffer tests: New module.

* tests/test-md2-buffer.c: Renamed from tests/test-md2.c.
* modules/crypto/md2-buffer-tests: Renamed from
modules/crypto/md2-tests. Test tests/test-md2-buffer.c instead of
tests/test-md2.c.
---
 ChangeLog                               |  6 ++++++
 modules/crypto/md2-buffer-tests         | 10 ++++++++++
 modules/crypto/md2-tests                | 10 ----------
 tests/{test-md2.c => test-md2-buffer.c} |  0
 4 files changed, 16 insertions(+), 10 deletions(-)
 create mode 100644 modules/crypto/md2-buffer-tests
 delete mode 100644 modules/crypto/md2-tests
 rename tests/{test-md2.c => test-md2-buffer.c} (100%)

diff --git a/ChangeLog b/ChangeLog
index ed35ce693..43f605b3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	md2-buffer tests: New module.
+	* tests/test-md2-buffer.c: Renamed from tests/test-md2.c.
+	* modules/crypto/md2-buffer-tests: Renamed from
+	modules/crypto/md2-tests. Test tests/test-md2-buffer.c instead of
+	tests/test-md2.c.
+
 	md2-buffer: New module.
 	* lib/md2-stream.c: New file, extracted from lib/md2.c.
 	* lib/md2.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/md2-buffer-tests b/modules/crypto/md2-buffer-tests
new file mode 100644
index 000000000..20b2e9f41
--- /dev/null
+++ b/modules/crypto/md2-buffer-tests
@@ -0,0 +1,10 @@
+Files:
+tests/test-md2-buffer.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-md2-buffer
+check_PROGRAMS += test-md2-buffer
diff --git a/modules/crypto/md2-tests b/modules/crypto/md2-tests
deleted file mode 100644
index 2beed8747..000000000
--- a/modules/crypto/md2-tests
+++ /dev/null
@@ -1,10 +0,0 @@
-Files:
-tests/test-md2.c
-
-Depends-on:
-
-configure.ac:
-
-Makefile.am:
-TESTS += test-md2
-check_PROGRAMS += test-md2
diff --git a/tests/test-md2.c b/tests/test-md2-buffer.c
similarity index 100%
rename from tests/test-md2.c
rename to tests/test-md2-buffer.c
-- 
2.25.1


[-- Attachment #4: 0003-md4-buffer-New-module.patch --]
[-- Type: text/x-patch, Size: 10208 bytes --]

From fc0ce037c0a3787319b606f414494c1d62c0accd Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:32:01 +0200
Subject: [PATCH 03/14] md4-buffer: New module.

* lib/md4-stream.c: New file, extracted from lib/md4.c.
* lib/md4.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, md4_stream): Moved to md4-stream.c.
* lib/md4.h: Reorder declarations.
* modules/crypto/md4-buffer: New file, based on modules/crypto/md4.
* modules/crypto/md4: Rewritten.
---
 ChangeLog                 |  10 ++++
 lib/md4-stream.c          | 109 ++++++++++++++++++++++++++++++++++++++
 lib/md4.c                 |  84 +----------------------------
 lib/md4.h                 |  12 +++--
 modules/crypto/md4        |  12 ++---
 modules/crypto/md4-buffer |  28 ++++++++++
 6 files changed, 158 insertions(+), 97 deletions(-)
 create mode 100644 lib/md4-stream.c
 create mode 100644 modules/crypto/md4-buffer

diff --git a/ChangeLog b/ChangeLog
index 43f605b3a..5ee0f7f1c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	md4-buffer: New module.
+	* lib/md4-stream.c: New file, extracted from lib/md4.c.
+	* lib/md4.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, md4_stream): Moved to md4-stream.c.
+	* lib/md4.h: Reorder declarations.
+	* modules/crypto/md4-buffer: New file, based on modules/crypto/md4.
+	* modules/crypto/md4: Rewritten.
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	md2-buffer tests: New module.
diff --git a/lib/md4-stream.c b/lib/md4-stream.c
new file mode 100644
index 000000000..aba665aad
--- /dev/null
+++ b/lib/md4-stream.c
@@ -0,0 +1,109 @@
+/* Functions to compute MD4 message digest of files or memory blocks.
+   according to the definition of MD4 in RFC 1320 from April 1992.
+   Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2021 Free Software
+   Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt
+   cipher/md4.c . */
+
+#include <config.h>
+
+/* Specification.  */
+#include "md4.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute MD4 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+int
+md4_stream (FILE * stream, void *resblock)
+{
+  struct md4_ctx ctx;
+  size_t sum;
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  /* Initialize the computation context.  */
+  md4_init_ctx (&ctx);
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+
+          /* We've read at least one byte, so ignore errors.  But always
+             check for EOF, since feof may be true even though N > 0.
+             Otherwise, we could end up calling fread after EOF.  */
+          if (feof (stream))
+            goto process_partial_block;
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+         BLOCKSIZE % 64 == 0
+       */
+      md4_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    md4_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  md4_finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
diff --git a/lib/md4.c b/lib/md4.c
index ae2c29b4b..464063194 100644
--- a/lib/md4.c
+++ b/lib/md4.c
@@ -21,18 +21,14 @@
 
 #include <config.h>
 
+/* Specification.  */
 #include "md4.h"
 
 #include <stdalign.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
 #include <byteswap.h>
 #ifdef WORDS_BIGENDIAN
 # define SWAP(n) bswap_32 (n)
@@ -40,11 +36,6 @@
 # define SWAP(n) (n)
 #endif
 
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
 /* This array contains the bytes used to pad the buffer to the next
    64-byte boundary.  (RFC 1320, 3.1: Step 1)  */
 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */  };
@@ -115,79 +106,6 @@ md4_finish_ctx (struct md4_ctx *ctx, void *resbuf)
   return md4_read_ctx (ctx, resbuf);
 }
 
-/* Compute MD4 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-int
-md4_stream (FILE * stream, void *resblock)
-{
-  struct md4_ctx ctx;
-  size_t sum;
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  /* Initialize the computation context.  */
-  md4_init_ctx (&ctx);
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-         BLOCKSIZE % 64 == 0
-       */
-      md4_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
-process_partial_block:;
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    md4_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  md4_finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
-
 /* Compute MD4 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
diff --git a/lib/md4.h b/lib/md4.h
index 4ece10164..1f5e426c1 100644
--- a/lib/md4.h
+++ b/lib/md4.h
@@ -72,11 +72,6 @@ extern void *md4_finish_ctx (struct md4_ctx *ctx, void *restrict resbuf);
 extern void *md4_read_ctx (const struct md4_ctx *ctx, void *restrict resbuf);
 
 
-/* Compute MD4 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-extern int md4_stream (FILE * stream, void *resblock);
-
 /* Compute MD4 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -84,6 +79,13 @@ extern int md4_stream (FILE * stream, void *resblock);
 extern void *md4_buffer (const char *buffer, size_t len,
                          void *restrict resblock);
 
+
+/* Compute MD4 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+extern int md4_stream (FILE * stream, void *resblock);
+
+
 # ifdef __cplusplus
 }
 # endif
diff --git a/modules/crypto/md4 b/modules/crypto/md4
index f5a5f160e..66c67dcf6 100644
--- a/modules/crypto/md4
+++ b/modules/crypto/md4
@@ -2,21 +2,15 @@ Description:
 Compute MD4 checksum.
 
 Files:
-lib/md4.h
-lib/md4.c
-m4/md4.m4
+lib/md4-stream.c
 
 Depends-on:
-byteswap
-stdalign
-stdint
+crypto/md4-buffer
 
 configure.ac:
-AC_REQUIRE([AC_C_RESTRICT])
-gl_MD4
 
 Makefile.am:
-lib_SOURCES += md4.c
+lib_SOURCES += md4-stream.c
 
 Include:
 "md4.h"
diff --git a/modules/crypto/md4-buffer b/modules/crypto/md4-buffer
new file mode 100644
index 000000000..f5a5f160e
--- /dev/null
+++ b/modules/crypto/md4-buffer
@@ -0,0 +1,28 @@
+Description:
+Compute MD4 checksum.
+
+Files:
+lib/md4.h
+lib/md4.c
+m4/md4.m4
+
+Depends-on:
+byteswap
+stdalign
+stdint
+
+configure.ac:
+AC_REQUIRE([AC_C_RESTRICT])
+gl_MD4
+
+Makefile.am:
+lib_SOURCES += md4.c
+
+Include:
+"md4.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
-- 
2.25.1


[-- Attachment #5: 0004-md4-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 2138 bytes --]

From 88c2657f2a985387094ecf26d1b0373151ea65d2 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:33:31 +0200
Subject: [PATCH 04/14] md4-buffer tests: New module.

* tests/test-md4-buffer.c: Renamed from tests/test-md4.c.
* modules/crypto/md4-buffer-tests: Renamed from
modules/crypto/md4-tests. Test tests/test-md4-buffer.c instead of
tests/test-md4.c.
---
 ChangeLog                               |  6 ++++++
 modules/crypto/md4-buffer-tests         | 10 ++++++++++
 modules/crypto/md4-tests                | 10 ----------
 tests/{test-md4.c => test-md4-buffer.c} |  0
 4 files changed, 16 insertions(+), 10 deletions(-)
 create mode 100644 modules/crypto/md4-buffer-tests
 delete mode 100644 modules/crypto/md4-tests
 rename tests/{test-md4.c => test-md4-buffer.c} (100%)

diff --git a/ChangeLog b/ChangeLog
index 5ee0f7f1c..312cbb840 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	md4-buffer tests: New module.
+	* tests/test-md4-buffer.c: Renamed from tests/test-md4.c.
+	* modules/crypto/md4-buffer-tests: Renamed from
+	modules/crypto/md4-tests. Test tests/test-md4-buffer.c instead of
+	tests/test-md4.c.
+
 	md4-buffer: New module.
 	* lib/md4-stream.c: New file, extracted from lib/md4.c.
 	* lib/md4.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/md4-buffer-tests b/modules/crypto/md4-buffer-tests
new file mode 100644
index 000000000..8238db40d
--- /dev/null
+++ b/modules/crypto/md4-buffer-tests
@@ -0,0 +1,10 @@
+Files:
+tests/test-md4-buffer.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-md4-buffer
+check_PROGRAMS += test-md4-buffer
diff --git a/modules/crypto/md4-tests b/modules/crypto/md4-tests
deleted file mode 100644
index b4b851e85..000000000
--- a/modules/crypto/md4-tests
+++ /dev/null
@@ -1,10 +0,0 @@
-Files:
-tests/test-md4.c
-
-Depends-on:
-
-configure.ac:
-
-Makefile.am:
-TESTS += test-md4
-check_PROGRAMS += test-md4
diff --git a/tests/test-md4.c b/tests/test-md4-buffer.c
similarity index 100%
rename from tests/test-md4.c
rename to tests/test-md4-buffer.c
-- 
2.25.1


[-- Attachment #6: 0005-md5-Clarify-module-to-source-relation.patch --]
[-- Type: text/x-patch, Size: 10734 bytes --]

From 439979c0004197d6e0dd67c7807f57edacdf8a0d Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:35:19 +0200
Subject: [PATCH 05/14] md5: Clarify module to source relation.

* lib/md5-stream.c: New file, extracted from lib/md5.c.
* lib/md5.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, md5_stream): Moved to md5-stream.c.
* lib/md5.h: Tweak.
* modules/crypto/md5 (Files): Add lib/md5-stream.c.
(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
(Makefile.am): Arrange to compile md5-stream.c.
(Link): Mention $(LIB_CRYPTO).
---
 ChangeLog          |  12 ++++
 lib/md5-stream.c   | 141 +++++++++++++++++++++++++++++++++++++++++++++
 lib/md5.c          | 100 +-------------------------------
 lib/md5.h          |   1 +
 modules/crypto/md5 |   6 +-
 5 files changed, 162 insertions(+), 98 deletions(-)
 create mode 100644 lib/md5-stream.c

diff --git a/ChangeLog b/ChangeLog
index 312cbb840..13d6d4dbe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	md5: Clarify module to source relation.
+	* lib/md5-stream.c: New file, extracted from lib/md5.c.
+	* lib/md5.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, md5_stream): Moved to md5-stream.c.
+	* lib/md5.h: Tweak.
+	* modules/crypto/md5 (Files): Add lib/md5-stream.c.
+	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
+	(Makefile.am): Arrange to compile md5-stream.c.
+	(Link): Mention $(LIB_CRYPTO).
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	md4-buffer tests: New module.
diff --git a/lib/md5-stream.c b/lib/md5-stream.c
new file mode 100644
index 000000000..fb483b733
--- /dev/null
+++ b/lib/md5-stream.c
@@ -0,0 +1,141 @@
+/* Functions to compute MD5 message digest of files or memory blocks.
+   according to the definition of MD5 in RFC 1321 from April 1992.
+   Copyright (C) 1995-1997, 1999-2001, 2005-2006, 2008-2021 Free Software
+   Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
+
+#include <config.h>
+
+/* Specification.  */
+#if HAVE_OPENSSL_MD5
+# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
+#endif
+#include "md5.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#include "af_alg.h"
+
+#ifdef _LIBC
+# include <endian.h>
+# if __BYTE_ORDER == __BIG_ENDIAN
+#  define WORDS_BIGENDIAN 1
+# endif
+/* We need to keep the namespace clean so define the MD5 function
+   protected using leading __ .  */
+# define md5_init_ctx __md5_init_ctx
+# define md5_process_block __md5_process_block
+# define md5_process_bytes __md5_process_bytes
+# define md5_finish_ctx __md5_finish_ctx
+# define md5_stream __md5_stream
+#endif
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute MD5 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 16 bytes
+   beginning at RESBLOCK.  */
+int
+md5_stream (FILE *stream, void *resblock)
+{
+  switch (afalg_stream (stream, "md5", resblock, MD5_DIGEST_SIZE))
+    {
+    case 0: return 0;
+    case -EIO: return 1;
+    }
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  struct md5_ctx ctx;
+  md5_init_ctx (&ctx);
+  size_t sum;
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() as EOF may
+             not be sticky.  For details of such systems, see:
+             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
+          if (feof (stream))
+            goto process_partial_block;
+
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+         BLOCKSIZE % 64 == 0
+       */
+      md5_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+process_partial_block:
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    md5_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  md5_finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
diff --git a/lib/md5.c b/lib/md5.c
index b1dd452ed..7955665cc 100644
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -21,6 +21,7 @@
 
 #include <config.h>
 
+/* Specification.  */
 #if HAVE_OPENSSL_MD5
 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
 #endif
@@ -28,14 +29,9 @@
 
 #include <stdalign.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
 #ifdef _LIBC
 # include <endian.h>
 # if __BYTE_ORDER == __BIG_ENDIAN
@@ -48,7 +44,6 @@
 # define md5_process_bytes __md5_process_bytes
 # define md5_finish_ctx __md5_finish_ctx
 # define md5_read_ctx __md5_read_ctx
-# define md5_stream __md5_stream
 # define md5_buffer __md5_buffer
 #endif
 
@@ -59,12 +54,8 @@
 # define SWAP(n) (n)
 #endif
 
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
 #if ! HAVE_OPENSSL_MD5
+
 /* This array contains the bytes used to pad the buffer to the next
    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
@@ -132,93 +123,7 @@ md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
 
   return md5_read_ctx (ctx, resbuf);
 }
-#endif
-
-#if defined _LIBC || defined GL_COMPILE_CRYPTO_STREAM
-
-#include "af_alg.h"
-
-/* Compute MD5 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-int
-md5_stream (FILE *stream, void *resblock)
-{
-  switch (afalg_stream (stream, "md5", resblock, MD5_DIGEST_SIZE))
-    {
-    case 0: return 0;
-    case -EIO: return 1;
-    }
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  struct md5_ctx ctx;
-  md5_init_ctx (&ctx);
-  size_t sum;
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          /* Either process a partial fread() from this loop,
-             or the fread() in afalg_stream may have gotten EOF.
-             We need to avoid a subsequent fread() as EOF may
-             not be sticky.  For details of such systems, see:
-             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
-          if (feof (stream))
-            goto process_partial_block;
-
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-         BLOCKSIZE % 64 == 0
-       */
-      md5_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
-process_partial_block:
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    md5_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  md5_finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
-#endif
 
-#if ! HAVE_OPENSSL_MD5
 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -479,6 +384,7 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
   ctx->C = C;
   ctx->D = D;
 }
+
 #endif
 
 /*
diff --git a/lib/md5.h b/lib/md5.h
index ca7c78ed5..bae5960a8 100644
--- a/lib/md5.h
+++ b/lib/md5.h
@@ -124,6 +124,7 @@ extern void *__md5_buffer (const char *buffer, size_t len,
                            void *restrict resblock) __THROW;
 
 # endif
+
 /* Compute MD5 message digest for bytes read from STREAM.
    STREAM is an open file stream.  Regular files are handled more efficiently.
    The contents of STREAM from its current position to its end will be read.
diff --git a/modules/crypto/md5 b/modules/crypto/md5
index afcdbc38f..ea9722ed3 100644
--- a/modules/crypto/md5
+++ b/modules/crypto/md5
@@ -2,19 +2,23 @@ Description:
 Compute MD5 checksum.
 
 Files:
+lib/md5-stream.c
 
 Depends-on:
 crypto/af_alg
 crypto/md5-buffer
 
 configure.ac:
-AC_DEFINE([GL_COMPILE_CRYPTO_STREAM], 1, [Compile Gnulib crypto stream ops.])
 
 Makefile.am:
+lib_SOURCES += md5-stream.c
 
 Include:
 "md5.h"
 
+Link:
+$(LIB_CRYPTO)
+
 License:
 LGPLv2+
 
-- 
2.25.1


[-- Attachment #7: 0006-md5-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 5508 bytes --]

From d8a1c91c657909fc0b2148f9e47b6eb8ef79d8ad Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:35:24 +0200
Subject: [PATCH 06/14] md5-buffer tests: New module.

* tests/test-md5-buffer.c: New file, based on tests/test-md5.c.
* tests/test-md5-stream.c: Renamed from tests/test-md5.c.
(main): Remove md5-buffer tests.
* modules/crypto/md5-buffer-tests: New file, based on
modules/crypto/md5-tests.
* modules/crypto/md5-tests: Remove tests that are now in
modules/crypto/md5-buffer-tests. Test tests/test-md5-stream.c instead
of tests/test-md5.c.
---
 ChangeLog                               | 10 ++++++
 modules/crypto/md5-buffer-tests         | 18 ++++++++++
 modules/crypto/md5-tests                | 14 +++-----
 tests/{test-md5.c => test-md5-buffer.c} | 13 --------
 tests/test-md5-stream.c                 | 44 +++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 23 deletions(-)
 create mode 100644 modules/crypto/md5-buffer-tests
 rename tests/{test-md5.c => test-md5-buffer.c} (80%)
 create mode 100644 tests/test-md5-stream.c

diff --git a/ChangeLog b/ChangeLog
index 13d6d4dbe..e709058cc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	md5-buffer tests: New module.
+	* tests/test-md5-buffer.c: New file, based on tests/test-md5.c.
+	* tests/test-md5-stream.c: Renamed from tests/test-md5.c.
+	(main): Remove md5-buffer tests.
+	* modules/crypto/md5-buffer-tests: New file, based on
+	modules/crypto/md5-tests.
+	* modules/crypto/md5-tests: Remove tests that are now in
+	modules/crypto/md5-buffer-tests. Test tests/test-md5-stream.c instead
+	of tests/test-md5.c.
+
 	md5: Clarify module to source relation.
 	* lib/md5-stream.c: New file, extracted from lib/md5.c.
 	* lib/md5.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/md5-buffer-tests b/modules/crypto/md5-buffer-tests
new file mode 100644
index 000000000..cc07337f0
--- /dev/null
+++ b/modules/crypto/md5-buffer-tests
@@ -0,0 +1,18 @@
+Files:
+tests/test-md5-buffer.c
+tests/bench-md5.c
+tests/bench-digest.h
+
+Depends-on:
+c99
+getrusage
+gettimeofday
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-md5-buffer
+check_PROGRAMS += test-md5-buffer
+noinst_PROGRAMS += bench-md5
+test_md5_buffer_LDADD = $(LDADD) @LIB_CRYPTO@
+bench_md5_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/modules/crypto/md5-tests b/modules/crypto/md5-tests
index c1160f13d..b2de00a54 100644
--- a/modules/crypto/md5-tests
+++ b/modules/crypto/md5-tests
@@ -1,20 +1,14 @@
 Files:
-tests/test-md5.c
+tests/test-md5-stream.c
 tests/test-digest.h
-tests/bench-md5.c
-tests/bench-digest.h
 tests/macros.h
 
 Depends-on:
 c99
-getrusage
-gettimeofday
 
 configure.ac:
 
 Makefile.am:
-TESTS += test-md5
-check_PROGRAMS += test-md5
-noinst_PROGRAMS += bench-md5
-test_md5_LDADD = $(LDADD) @LIB_CRYPTO@
-bench_md5_LDADD = $(LDADD) @LIB_CRYPTO@
+TESTS += test-md5-stream
+check_PROGRAMS += test-md5-stream
+test_md5_stream_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/tests/test-md5.c b/tests/test-md5-buffer.c
similarity index 80%
rename from tests/test-md5.c
rename to tests/test-md5-buffer.c
index c6c55ec8e..7afc944a2 100644
--- a/tests/test-md5.c
+++ b/tests/test-md5-buffer.c
@@ -22,14 +22,7 @@
 #include "md5.h"
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-
-#include "macros.h"
-
-#define TESTFILE "test-md5.data"
-#include "test-digest.h"
 
 int
 main (void)
@@ -70,11 +63,5 @@ main (void)
       return 1;
     }
 
-  /* Test md5_stream.  */
-  test_digest_on_files (md5_stream, "md5_stream", 16,
-                        "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e",
-                        "\x0d\x70\x06\xcd\x05\x5e\x94\xcf\x61\x45\x87\xe1\xd2\xae\x0c\x8e",
-                        "\xec\x99\x67\x9b\xff\xc0\xf9\xb0\x6d\x18\x30\x6b\x06\xd6\x56\x23");
-
   return 0;
 }
diff --git a/tests/test-md5-stream.c b/tests/test-md5-stream.c
new file mode 100644
index 000000000..e506c4b78
--- /dev/null
+++ b/tests/test-md5-stream.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2005, 2009-2021 Free Software Foundation, Inc.
+ * Written by Simon Josefsson
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson. */
+
+#include <config.h>
+
+#include "md5.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+#define TESTFILE "test-md5.data"
+#include "test-digest.h"
+
+int
+main (void)
+{
+  /* Test md5_stream.  */
+  test_digest_on_files (md5_stream, "md5_stream", 16,
+                        "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e",
+                        "\x0d\x70\x06\xcd\x05\x5e\x94\xcf\x61\x45\x87\xe1\xd2\xae\x0c\x8e",
+                        "\xec\x99\x67\x9b\xff\xc0\xf9\xb0\x6d\x18\x30\x6b\x06\xd6\x56\x23");
+
+  return 0;
+}
-- 
2.25.1


[-- Attachment #8: 0007-sha1-Clarify-module-to-source-relation.patch --]
[-- Type: text/x-patch, Size: 10307 bytes --]

From ecf145d277937c22f28294ef573f5a2cfb58fa30 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:36:29 +0200
Subject: [PATCH 07/14] sha1: Clarify module to source relation.

* lib/sha1-stream.c: New file, extracted from lib/sha1.c.
* lib/sha1.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, sha1_stream): Moved to sha1-stream.c.
* lib/sha1.h: Correct indentation.
* modules/crypto/sha1 (Files): Add lib/sha1-stream.c.
(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
(Makefile.am): Arrange to compile sha1-stream.c.
(Link): Mention $(LIB_CRYPTO).
---
 ChangeLog           |  12 +++++
 lib/sha1-stream.c   | 129 ++++++++++++++++++++++++++++++++++++++++++++
 lib/sha1.c          |  99 ++--------------------------------
 lib/sha1.h          |   3 +-
 modules/crypto/sha1 |   6 ++-
 5 files changed, 151 insertions(+), 98 deletions(-)
 create mode 100644 lib/sha1-stream.c

diff --git a/ChangeLog b/ChangeLog
index e709058cc..f9d246b03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	sha1: Clarify module to source relation.
+	* lib/sha1-stream.c: New file, extracted from lib/sha1.c.
+	* lib/sha1.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, sha1_stream): Moved to sha1-stream.c.
+	* lib/sha1.h: Correct indentation.
+	* modules/crypto/sha1 (Files): Add lib/sha1-stream.c.
+	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
+	(Makefile.am): Arrange to compile sha1-stream.c.
+	(Link): Mention $(LIB_CRYPTO).
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	md5-buffer tests: New module.
diff --git a/lib/sha1-stream.c b/lib/sha1-stream.c
new file mode 100644
index 000000000..fbf8c7b2e
--- /dev/null
+++ b/lib/sha1-stream.c
@@ -0,0 +1,129 @@
+/* sha1.c - Functions to compute SHA1 message digest of files or
+   memory blocks according to the NIST specification FIPS-180-1.
+
+   Copyright (C) 2000-2001, 2003-2006, 2008-2021 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Scott G. Miller
+   Credits:
+      Robert Klep <robert@ilse.nl>  -- Expansion function fix
+*/
+
+#include <config.h>
+
+/* Specification.  */
+#if HAVE_OPENSSL_SHA1
+# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
+#endif
+#include "sha1.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#include "af_alg.h"
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute SHA1 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 20 bytes
+   beginning at RESBLOCK.  */
+int
+sha1_stream (FILE *stream, void *resblock)
+{
+  switch (afalg_stream (stream, "sha1", resblock, SHA1_DIGEST_SIZE))
+    {
+    case 0: return 0;
+    case -EIO: return 1;
+    }
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  struct sha1_ctx ctx;
+  sha1_init_ctx (&ctx);
+  size_t sum;
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() as EOF may
+             not be sticky.  For details of such systems, see:
+             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
+          if (feof (stream))
+            goto process_partial_block;
+
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+                        BLOCKSIZE % 64 == 0
+       */
+      sha1_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+ process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    sha1_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  sha1_finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
diff --git a/lib/sha1.c b/lib/sha1.c
index df2dbb495..52b102031 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -23,6 +23,7 @@
 
 #include <config.h>
 
+/* Specification.  */
 #if HAVE_OPENSSL_SHA1
 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
 #endif
@@ -30,13 +31,8 @@
 
 #include <stdalign.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <string.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
 #include <byteswap.h>
 #ifdef WORDS_BIGENDIAN
 # define SWAP(n) (n)
@@ -44,12 +40,8 @@
 # define SWAP(n) bswap_32 (n)
 #endif
 
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
 #if ! HAVE_OPENSSL_SHA1
+
 /* This array contains the bytes used to pad the buffer to the next
    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
@@ -120,93 +112,7 @@ sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
 
   return sha1_read_ctx (ctx, resbuf);
 }
-#endif
-
-#ifdef GL_COMPILE_CRYPTO_STREAM
-
-#include "af_alg.h"
 
-/* Compute SHA1 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 20 bytes
-   beginning at RESBLOCK.  */
-int
-sha1_stream (FILE *stream, void *resblock)
-{
-  switch (afalg_stream (stream, "sha1", resblock, SHA1_DIGEST_SIZE))
-    {
-    case 0: return 0;
-    case -EIO: return 1;
-    }
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  struct sha1_ctx ctx;
-  sha1_init_ctx (&ctx);
-  size_t sum;
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          /* Either process a partial fread() from this loop,
-             or the fread() in afalg_stream may have gotten EOF.
-             We need to avoid a subsequent fread() as EOF may
-             not be sticky.  For details of such systems, see:
-             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
-          if (feof (stream))
-            goto process_partial_block;
-
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-                        BLOCKSIZE % 64 == 0
-       */
-      sha1_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
- process_partial_block:;
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    sha1_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  sha1_finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
-#endif
-
-#if ! HAVE_OPENSSL_SHA1
 /* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -444,6 +350,7 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
       e = ctx->E += e;
     }
 }
+
 #endif
 
 /*
diff --git a/lib/sha1.h b/lib/sha1.h
index a619623f7..e12a23cd4 100644
--- a/lib/sha1.h
+++ b/lib/sha1.h
@@ -30,7 +30,7 @@
 extern "C" {
 # endif
 
-#define SHA1_DIGEST_SIZE 20
+# define SHA1_DIGEST_SIZE 20
 
 # if HAVE_OPENSSL_SHA1
 #  define GL_OPENSSL_NAME 1
@@ -88,6 +88,7 @@ extern void *sha1_buffer (const char *buffer, size_t len,
                           void *restrict resblock);
 
 # endif
+
 /* Compute SHA1 message digest for bytes read from STREAM.
    STREAM is an open file stream.  Regular files are handled more efficiently.
    The contents of STREAM from its current position to its end will be read.
diff --git a/modules/crypto/sha1 b/modules/crypto/sha1
index 86ae4d87e..96e5c8fe1 100644
--- a/modules/crypto/sha1
+++ b/modules/crypto/sha1
@@ -2,19 +2,23 @@ Description:
 Compute SHA1 checksum.
 
 Files:
+lib/sha1-stream.c
 
 Depends-on:
 crypto/af_alg
 crypto/sha1-buffer
 
 configure.ac:
-AC_DEFINE([GL_COMPILE_CRYPTO_STREAM], 1, [Compile Gnulib crypto stream ops.])
 
 Makefile.am:
+lib_SOURCES += sha1-stream.c
 
 Include:
 "sha1.h"
 
+Link:
+$(LIB_CRYPTO)
+
 License:
 LGPLv2+
 
-- 
2.25.1


[-- Attachment #9: 0008-sha1-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 5650 bytes --]

From 517b9f77086d38cb1409194576cab40fb3f714d5 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:37:51 +0200
Subject: [PATCH 08/14] sha1-buffer tests: New module.

* tests/test-sha1-buffer.c: New file, based on tests/test-sha1.c.
* tests/test-sha1-stream.c: Renamed from tests/test-sha1.c.
(main): Remove sha1-buffer tests.
* modules/crypto/sha1-buffer-tests: New file, based on
modules/crypto/sha1-tests.
* modules/crypto/sha1-tests: Remove tests that are now in
modules/crypto/sha1-buffer-tests. Test tests/test-sha1-stream.c instead
of tests/test-sha1.c.
---
 ChangeLog                                 | 10 ++++++
 modules/crypto/sha1-buffer-tests          | 18 ++++++++++
 modules/crypto/sha1-tests                 | 14 +++-----
 tests/{test-sha1.c => test-sha1-buffer.c} | 13 -------
 tests/test-sha1-stream.c                  | 42 +++++++++++++++++++++++
 5 files changed, 74 insertions(+), 23 deletions(-)
 create mode 100644 modules/crypto/sha1-buffer-tests
 rename tests/{test-sha1.c => test-sha1-buffer.c} (73%)
 create mode 100644 tests/test-sha1-stream.c

diff --git a/ChangeLog b/ChangeLog
index f9d246b03..7f8468554 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	sha1-buffer tests: New module.
+	* tests/test-sha1-buffer.c: New file, based on tests/test-sha1.c.
+	* tests/test-sha1-stream.c: Renamed from tests/test-sha1.c.
+	(main): Remove sha1-buffer tests.
+	* modules/crypto/sha1-buffer-tests: New file, based on
+	modules/crypto/sha1-tests.
+	* modules/crypto/sha1-tests: Remove tests that are now in
+	modules/crypto/sha1-buffer-tests. Test tests/test-sha1-stream.c instead
+	of tests/test-sha1.c.
+
 	sha1: Clarify module to source relation.
 	* lib/sha1-stream.c: New file, extracted from lib/sha1.c.
 	* lib/sha1.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/sha1-buffer-tests b/modules/crypto/sha1-buffer-tests
new file mode 100644
index 000000000..7420378c8
--- /dev/null
+++ b/modules/crypto/sha1-buffer-tests
@@ -0,0 +1,18 @@
+Files:
+tests/test-sha1-buffer.c
+tests/bench-sha1.c
+tests/bench-digest.h
+
+Depends-on:
+c99
+getrusage
+gettimeofday
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sha1-buffer
+check_PROGRAMS += test-sha1-buffer
+noinst_PROGRAMS += bench-sha1
+test_sha1_buffer_LDADD = $(LDADD) @LIB_CRYPTO@
+bench_sha1_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/modules/crypto/sha1-tests b/modules/crypto/sha1-tests
index 366e4aade..b774ffcee 100644
--- a/modules/crypto/sha1-tests
+++ b/modules/crypto/sha1-tests
@@ -1,20 +1,14 @@
 Files:
-tests/test-sha1.c
+tests/test-sha1-stream.c
 tests/test-digest.h
-tests/bench-sha1.c
-tests/bench-digest.h
 tests/macros.h
 
 Depends-on:
 c99
-getrusage
-gettimeofday
 
 configure.ac:
 
 Makefile.am:
-TESTS += test-sha1
-check_PROGRAMS += test-sha1
-noinst_PROGRAMS += bench-sha1
-test_sha1_LDADD = $(LDADD) @LIB_CRYPTO@
-bench_sha1_LDADD = $(LDADD) @LIB_CRYPTO@
+TESTS += test-sha1-stream
+check_PROGRAMS += test-sha1-stream
+test_sha1_stream_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/tests/test-sha1.c b/tests/test-sha1-buffer.c
similarity index 73%
rename from tests/test-sha1.c
rename to tests/test-sha1-buffer.c
index ed1949a23..e5f1f08e0 100644
--- a/tests/test-sha1.c
+++ b/tests/test-sha1-buffer.c
@@ -20,14 +20,7 @@
 #include "sha1.h"
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-
-#include "macros.h"
-
-#define TESTFILE "test-sha1.data"
-#include "test-digest.h"
 
 int
 main (void)
@@ -51,11 +44,5 @@ main (void)
       return 1;
     }
 
-  /* Test sha1_stream.  */
-  test_digest_on_files (sha1_stream, "sha1_stream", 20,
-                        "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09",
-                        "\x9c\x04\xcd\x63\x72\x07\x7e\x9b\x11\xf7\x0c\xa1\x11\xc9\x80\x7d\xc7\x13\x7e\x4b",
-                        "\x91\xab\x6b\x1b\x8d\x29\x25\x3c\xcb\x8d\xce\xb7\x7a\x25\x26\x2c\x92\xc9\x22\x09");
-
   return 0;
 }
diff --git a/tests/test-sha1-stream.c b/tests/test-sha1-stream.c
new file mode 100644
index 000000000..00ec9f080
--- /dev/null
+++ b/tests/test-sha1-stream.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2005, 2008-2021 Free Software Foundation, Inc.
+ * Written by Simon Josefsson
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include "sha1.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+#define TESTFILE "test-sha1.data"
+#include "test-digest.h"
+
+int
+main (void)
+{
+  /* Test sha1_stream.  */
+  test_digest_on_files (sha1_stream, "sha1_stream", 20,
+                        "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09",
+                        "\x9c\x04\xcd\x63\x72\x07\x7e\x9b\x11\xf7\x0c\xa1\x11\xc9\x80\x7d\xc7\x13\x7e\x4b",
+                        "\x91\xab\x6b\x1b\x8d\x29\x25\x3c\xcb\x8d\xce\xb7\x7a\x25\x26\x2c\x92\xc9\x22\x09");
+
+  return 0;
+}
-- 
2.25.1


[-- Attachment #10: 0009-sha256-Clarify-module-to-source-relation.patch --]
[-- Type: text/x-patch, Size: 11593 bytes --]

From 36cd6e0a4e9761db5f0b98508b21d552d9e50b28 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:38:36 +0200
Subject: [PATCH 09/14] sha256: Clarify module to source relation.

* lib/sha256-stream.c: New file, extracted from lib/sha256.c.
* lib/sha256.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, shaxxx_stream, sha256_stream, sha224_stream): Moved to
sha256-stream.c.
* lib/sha256.h: Tweak.
* modules/crypto/sha256 (Files): Add lib/sha256-stream.c.
(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
(Makefile.am): Arrange to compile sha256-stream.c.
(Link): Mention $(LIB_CRYPTO).
---
 ChangeLog             |  13 ++++
 lib/sha256-stream.c   | 145 ++++++++++++++++++++++++++++++++++++++++++
 lib/sha256.c          | 116 +--------------------------------
 lib/sha256.h          |   1 +
 modules/crypto/sha256 |   6 +-
 5 files changed, 167 insertions(+), 114 deletions(-)
 create mode 100644 lib/sha256-stream.c

diff --git a/ChangeLog b/ChangeLog
index 7f8468554..18d43f3b5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	sha256: Clarify module to source relation.
+	* lib/sha256-stream.c: New file, extracted from lib/sha256.c.
+	* lib/sha256.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, shaxxx_stream, sha256_stream, sha224_stream): Moved to
+	sha256-stream.c.
+	* lib/sha256.h: Tweak.
+	* modules/crypto/sha256 (Files): Add lib/sha256-stream.c.
+	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
+	(Makefile.am): Arrange to compile sha256-stream.c.
+	(Link): Mention $(LIB_CRYPTO).
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	sha1-buffer tests: New module.
diff --git a/lib/sha256-stream.c b/lib/sha256-stream.c
new file mode 100644
index 000000000..093b57959
--- /dev/null
+++ b/lib/sha256-stream.c
@@ -0,0 +1,145 @@
+/* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
+   memory blocks according to the NIST specification FIPS-180-2.
+
+   Copyright (C) 2005-2006, 2008-2021 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by David Madore, considerably copypasting from
+   Scott G. Miller's sha1.c
+*/
+
+#include <config.h>
+
+/* Specification.  */
+#if HAVE_OPENSSL_SHA256
+# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
+#endif
+#include "sha256.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#include "af_alg.h"
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute message digest for bytes read from STREAM using algorithm ALG.
+   Write the message digest into RESBLOCK, which contains HASHLEN bytes.
+   The initial and finishing operations are INIT_CTX and FINISH_CTX.
+   Return zero if and only if successful.  */
+static int
+shaxxx_stream (FILE *stream, char const *alg, void *resblock,
+               ssize_t hashlen, void (*init_ctx) (struct sha256_ctx *),
+               void *(*finish_ctx) (struct sha256_ctx *, void *))
+{
+  switch (afalg_stream (stream, alg, resblock, hashlen))
+    {
+    case 0: return 0;
+    case -EIO: return 1;
+    }
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  struct sha256_ctx ctx;
+  init_ctx (&ctx);
+  size_t sum;
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() as EOF may
+             not be sticky.  For details of such systems, see:
+             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
+          if (feof (stream))
+            goto process_partial_block;
+
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+                        BLOCKSIZE % 64 == 0
+       */
+      sha256_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+ process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    sha256_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
+
+int
+sha256_stream (FILE *stream, void *resblock)
+{
+  return shaxxx_stream (stream, "sha256", resblock, SHA256_DIGEST_SIZE,
+                        sha256_init_ctx, sha256_finish_ctx);
+}
+
+int
+sha224_stream (FILE *stream, void *resblock)
+{
+  return shaxxx_stream (stream, "sha224", resblock, SHA224_DIGEST_SIZE,
+                        sha224_init_ctx, sha224_finish_ctx);
+}
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
diff --git a/lib/sha256.c b/lib/sha256.c
index 2a2ca5316..2b8687f1d 100644
--- a/lib/sha256.c
+++ b/lib/sha256.c
@@ -22,6 +22,7 @@
 
 #include <config.h>
 
+/* Specification.  */
 #if HAVE_OPENSSL_SHA256
 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
 #endif
@@ -29,13 +30,8 @@
 
 #include <stdalign.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <string.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
 #include <byteswap.h>
 #ifdef WORDS_BIGENDIAN
 # define SWAP(n) (n)
@@ -43,12 +39,8 @@
 # define SWAP(n) bswap_32 (n)
 #endif
 
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
 #if ! HAVE_OPENSSL_SHA256
+
 /* This array contains the bytes used to pad the buffer to the next
    64-byte boundary.  */
 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
@@ -167,110 +159,7 @@ sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
   sha256_conclude_ctx (ctx);
   return sha224_read_ctx (ctx, resbuf);
 }
-#endif
-
-#ifdef GL_COMPILE_CRYPTO_STREAM
-
-#include "af_alg.h"
-
-/* Compute message digest for bytes read from STREAM using algorithm ALG.
-   Write the message digest into RESBLOCK, which contains HASHLEN bytes.
-   The initial and finishing operations are INIT_CTX and FINISH_CTX.
-   Return zero if and only if successful.  */
-static int
-shaxxx_stream (FILE *stream, char const *alg, void *resblock,
-               ssize_t hashlen, void (*init_ctx) (struct sha256_ctx *),
-               void *(*finish_ctx) (struct sha256_ctx *, void *))
-{
-  switch (afalg_stream (stream, alg, resblock, hashlen))
-    {
-    case 0: return 0;
-    case -EIO: return 1;
-    }
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  struct sha256_ctx ctx;
-  init_ctx (&ctx);
-  size_t sum;
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          /* Either process a partial fread() from this loop,
-             or the fread() in afalg_stream may have gotten EOF.
-             We need to avoid a subsequent fread() as EOF may
-             not be sticky.  For details of such systems, see:
-             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
-          if (feof (stream))
-            goto process_partial_block;
-
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-                        BLOCKSIZE % 64 == 0
-       */
-      sha256_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
- process_partial_block:;
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    sha256_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
 
-int
-sha256_stream (FILE *stream, void *resblock)
-{
-  return shaxxx_stream (stream, "sha256", resblock, SHA256_DIGEST_SIZE,
-                        sha256_init_ctx, sha256_finish_ctx);
-}
-
-int
-sha224_stream (FILE *stream, void *resblock)
-{
-  return shaxxx_stream (stream, "sha224", resblock, SHA224_DIGEST_SIZE,
-                        sha224_init_ctx, sha224_finish_ctx);
-}
-#endif
-
-#if ! HAVE_OPENSSL_SHA256
 /* Compute SHA256 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -533,6 +422,7 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
       h = ctx->state[7] += h;
     }
 }
+
 #endif
 
 /*
diff --git a/lib/sha256.h b/lib/sha256.h
index 31d788b88..e09b3de80 100644
--- a/lib/sha256.h
+++ b/lib/sha256.h
@@ -93,6 +93,7 @@ extern void *sha224_buffer (const char *buffer, size_t len,
                             void *restrict resblock);
 
 # endif
+
 /* Compute SHA256 (SHA224) message digest for bytes read from STREAM.
    STREAM is an open file stream.  Regular files are handled more efficiently.
    The contents of STREAM from its current position to its end will be read.
diff --git a/modules/crypto/sha256 b/modules/crypto/sha256
index 0e2b4f038..68bc1900e 100644
--- a/modules/crypto/sha256
+++ b/modules/crypto/sha256
@@ -2,19 +2,23 @@ Description:
 Compute SHA224 and SHA256 checksums.
 
 Files:
+lib/sha256-stream.c
 
 Depends-on:
 crypto/af_alg
 crypto/sha256-buffer
 
 configure.ac:
-AC_DEFINE([GL_COMPILE_CRYPTO_STREAM], 1, [Compile Gnulib crypto stream ops.])
 
 Makefile.am:
+lib_SOURCES += sha256-stream.c
 
 Include:
 "sha256.h"
 
+Link:
+$(LIB_CRYPTO)
+
 License:
 LGPLv2+
 
-- 
2.25.1


[-- Attachment #11: 0010-sha256-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 3059 bytes --]

From ecad57d5a208a154ed0af6d93bd27a071c5d909b Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:39:47 +0200
Subject: [PATCH 10/14] sha256-buffer tests: New module.

* tests/test-sha256-stream.c: Renamed from tests/test-sha256.c.
* modules/crypto/sha256-buffer-tests: New file, based on
modules/crypto/sha256-tests.
* modules/crypto/sha256-tests: Remove tests that are now in
modules/crypto/sha256-buffer-tests. Test tests/test-sha256-stream.c
instead of tests/test-sha256.c.
---
 ChangeLog                                     |  8 ++++++++
 modules/crypto/sha256-buffer-tests            | 16 ++++++++++++++++
 modules/crypto/sha256-tests                   | 16 ++++------------
 tests/{test-sha256.c => test-sha256-stream.c} |  0
 4 files changed, 28 insertions(+), 12 deletions(-)
 create mode 100644 modules/crypto/sha256-buffer-tests
 rename tests/{test-sha256.c => test-sha256-stream.c} (100%)

diff --git a/ChangeLog b/ChangeLog
index 18d43f3b5..7ad6b0299 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	sha256-buffer tests: New module.
+	* tests/test-sha256-stream.c: Renamed from tests/test-sha256.c.
+	* modules/crypto/sha256-buffer-tests: New file, based on
+	modules/crypto/sha256-tests.
+	* modules/crypto/sha256-tests: Remove tests that are now in
+	modules/crypto/sha256-buffer-tests. Test tests/test-sha256-stream.c
+	instead of tests/test-sha256.c.
+
 	sha256: Clarify module to source relation.
 	* lib/sha256-stream.c: New file, extracted from lib/sha256.c.
 	* lib/sha256.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/sha256-buffer-tests b/modules/crypto/sha256-buffer-tests
new file mode 100644
index 000000000..53924b4a0
--- /dev/null
+++ b/modules/crypto/sha256-buffer-tests
@@ -0,0 +1,16 @@
+Files:
+tests/bench-sha224.c
+tests/bench-sha256.c
+tests/bench-digest.h
+
+Depends-on:
+c99
+getrusage
+gettimeofday
+
+configure.ac:
+
+Makefile.am:
+noinst_PROGRAMS += bench-sha224 bench-sha256
+bench_sha224_LDADD = $(LDADD) @LIB_CRYPTO@
+bench_sha256_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/modules/crypto/sha256-tests b/modules/crypto/sha256-tests
index fb9e06d70..9357cc24d 100644
--- a/modules/crypto/sha256-tests
+++ b/modules/crypto/sha256-tests
@@ -1,22 +1,14 @@
 Files:
-tests/test-sha256.c
+tests/test-sha256-stream.c
 tests/test-digest.h
-tests/bench-sha224.c
-tests/bench-sha256.c
-tests/bench-digest.h
 tests/macros.h
 
 Depends-on:
 c99
-getrusage
-gettimeofday
 
 configure.ac:
 
 Makefile.am:
-TESTS += test-sha256
-check_PROGRAMS += test-sha256
-noinst_PROGRAMS += bench-sha224 bench-sha256
-test_sha256_LDADD = $(LDADD) @LIB_CRYPTO@
-bench_sha224_LDADD = $(LDADD) @LIB_CRYPTO@
-bench_sha256_LDADD = $(LDADD) @LIB_CRYPTO@
+TESTS += test-sha256-stream
+check_PROGRAMS += test-sha256-stream
+test_sha256_stream_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/tests/test-sha256.c b/tests/test-sha256-stream.c
similarity index 100%
rename from tests/test-sha256.c
rename to tests/test-sha256-stream.c
-- 
2.25.1


[-- Attachment #12: 0011-sha512-Clarify-module-to-source-relation.patch --]
[-- Type: text/x-patch, Size: 11625 bytes --]

From 2b6d343ff9f3a536054024c233ff46c2d19e598e Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:40:46 +0200
Subject: [PATCH 11/14] sha512: Clarify module to source relation.

* lib/sha512-stream.c: New file, extracted from lib/sha512.c.
* lib/sha512.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, shaxxx_stream, sha512_stream, sha384_stream): Moved to
sha512-stream.c.
* lib/sha512.h: Tweak.
* modules/crypto/sha512 (Files): Add lib/sha512-stream.c.
(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
(Makefile.am): Arrange to compile sha512-stream.c.
(Link): Mention $(LIB_CRYPTO).
---
 ChangeLog             |  13 ++++
 lib/sha512-stream.c   | 145 ++++++++++++++++++++++++++++++++++++++++++
 lib/sha512.c          | 116 +--------------------------------
 lib/sha512.h          |   1 +
 modules/crypto/sha512 |   6 +-
 5 files changed, 167 insertions(+), 114 deletions(-)
 create mode 100644 lib/sha512-stream.c

diff --git a/ChangeLog b/ChangeLog
index 7ad6b0299..f3e77f5e4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	sha512: Clarify module to source relation.
+	* lib/sha512-stream.c: New file, extracted from lib/sha512.c.
+	* lib/sha512.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, shaxxx_stream, sha512_stream, sha384_stream): Moved to
+	sha512-stream.c.
+	* lib/sha512.h: Tweak.
+	* modules/crypto/sha512 (Files): Add lib/sha512-stream.c.
+	(configure.ac): Don't define GL_COMPILE_CRYPTO_STREAM.
+	(Makefile.am): Arrange to compile sha512-stream.c.
+	(Link): Mention $(LIB_CRYPTO).
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	sha256-buffer tests: New module.
diff --git a/lib/sha512-stream.c b/lib/sha512-stream.c
new file mode 100644
index 000000000..624cf1905
--- /dev/null
+++ b/lib/sha512-stream.c
@@ -0,0 +1,145 @@
+/* sha512.c - Functions to compute SHA512 and SHA384 message digest of files or
+   memory blocks according to the NIST specification FIPS-180-2.
+
+   Copyright (C) 2005-2006, 2008-2021 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by David Madore, considerably copypasting from
+   Scott G. Miller's sha1.c
+*/
+
+#include <config.h>
+
+/* Specification.  */
+#if HAVE_OPENSSL_SHA512
+# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
+#endif
+#include "sha512.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#include "af_alg.h"
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 128 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute message digest for bytes read from STREAM using algorithm ALG.
+   Write the message digest into RESBLOCK, which contains HASHLEN bytes.
+   The initial and finishing operations are INIT_CTX and FINISH_CTX.
+   Return zero if and only if successful.  */
+static int
+shaxxx_stream (FILE *stream, char const *alg, void *resblock,
+               ssize_t hashlen, void (*init_ctx) (struct sha512_ctx *),
+               void *(*finish_ctx) (struct sha512_ctx *, void *))
+{
+  switch (afalg_stream (stream, alg, resblock, hashlen))
+    {
+    case 0: return 0;
+    case -EIO: return 1;
+    }
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  struct sha512_ctx ctx;
+  init_ctx (&ctx);
+  size_t sum;
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() as EOF may
+             not be sticky.  For details of such systems, see:
+             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
+          if (feof (stream))
+            goto process_partial_block;
+
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+                        BLOCKSIZE % 128 == 0
+       */
+      sha512_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+ process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    sha512_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
+
+int
+sha512_stream (FILE *stream, void *resblock)
+{
+  return shaxxx_stream (stream, "sha512", resblock, SHA512_DIGEST_SIZE,
+                        sha512_init_ctx, sha512_finish_ctx);
+}
+
+int
+sha384_stream (FILE *stream, void *resblock)
+{
+  return shaxxx_stream (stream, "sha384", resblock, SHA384_DIGEST_SIZE,
+                        sha384_init_ctx, sha384_finish_ctx);
+}
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
diff --git a/lib/sha512.c b/lib/sha512.c
index a3533209c..2865d6e58 100644
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -22,6 +22,7 @@
 
 #include <config.h>
 
+/* Specification.  */
 #if HAVE_OPENSSL_SHA512
 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
 #endif
@@ -29,13 +30,8 @@
 
 #include <stdalign.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <string.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
 #include <byteswap.h>
 #ifdef WORDS_BIGENDIAN
 # define SWAP(n) (n)
@@ -43,12 +39,8 @@
 # define SWAP(n) bswap_64 (n)
 #endif
 
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 128 != 0
-# error "invalid BLOCKSIZE"
-#endif
-
 #if ! HAVE_OPENSSL_SHA512
+
 /* This array contains the bytes used to pad the buffer to the next
    128-byte boundary.  */
 static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ...  */ };
@@ -168,110 +160,7 @@ sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf)
   sha512_conclude_ctx (ctx);
   return sha384_read_ctx (ctx, resbuf);
 }
-#endif
-
-#ifdef GL_COMPILE_CRYPTO_STREAM
-
-#include "af_alg.h"
-
-/* Compute message digest for bytes read from STREAM using algorithm ALG.
-   Write the message digest into RESBLOCK, which contains HASHLEN bytes.
-   The initial and finishing operations are INIT_CTX and FINISH_CTX.
-   Return zero if and only if successful.  */
-static int
-shaxxx_stream (FILE *stream, char const *alg, void *resblock,
-               ssize_t hashlen, void (*init_ctx) (struct sha512_ctx *),
-               void *(*finish_ctx) (struct sha512_ctx *, void *))
-{
-  switch (afalg_stream (stream, alg, resblock, hashlen))
-    {
-    case 0: return 0;
-    case -EIO: return 1;
-    }
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  struct sha512_ctx ctx;
-  init_ctx (&ctx);
-  size_t sum;
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          /* Either process a partial fread() from this loop,
-             or the fread() in afalg_stream may have gotten EOF.
-             We need to avoid a subsequent fread() as EOF may
-             not be sticky.  For details of such systems, see:
-             https://sourceware.org/bugzilla/show_bug.cgi?id=1190  */
-          if (feof (stream))
-            goto process_partial_block;
-
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-                        BLOCKSIZE % 128 == 0
-       */
-      sha512_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
- process_partial_block:;
-
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    sha512_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
 
-int
-sha512_stream (FILE *stream, void *resblock)
-{
-  return shaxxx_stream (stream, "sha512", resblock, SHA512_DIGEST_SIZE,
-                        sha512_init_ctx, sha512_finish_ctx);
-}
-
-int
-sha384_stream (FILE *stream, void *resblock)
-{
-  return shaxxx_stream (stream, "sha384", resblock, SHA384_DIGEST_SIZE,
-                        sha384_init_ctx, sha384_finish_ctx);
-}
-#endif
-
-#if ! HAVE_OPENSSL_SHA512
 /* Compute SHA512 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -578,6 +467,7 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
       h = ctx->state[7] = u64plus (ctx->state[7], h);
     }
 }
+
 #endif
 
 /*
diff --git a/lib/sha512.h b/lib/sha512.h
index 8a540bff8..e15afe996 100644
--- a/lib/sha512.h
+++ b/lib/sha512.h
@@ -96,6 +96,7 @@ extern void *sha384_buffer (const char *buffer, size_t len,
                             void *restrict resblock);
 
 # endif
+
 /* Compute SHA512 (SHA384) message digest for bytes read from STREAM.
    STREAM is an open file stream.  Regular files are handled more efficiently.
    The contents of STREAM from its current position to its end will be read.
diff --git a/modules/crypto/sha512 b/modules/crypto/sha512
index c4827c618..37a09e181 100644
--- a/modules/crypto/sha512
+++ b/modules/crypto/sha512
@@ -2,19 +2,23 @@ Description:
 Compute SHA384 and SHA512 checksums.
 
 Files:
+lib/sha512-stream.c
 
 Depends-on:
 crypto/af_alg
 crypto/sha512-buffer
 
 configure.ac:
-AC_DEFINE([GL_COMPILE_CRYPTO_STREAM], 1, [Compile Gnulib crypto stream ops.])
 
 Makefile.am:
+lib_SOURCES += sha512-stream.c
 
 Include:
 "sha512.h"
 
+Link:
+$(LIB_CRYPTO)
+
 License:
 LGPLv2+
 
-- 
2.25.1


[-- Attachment #13: 0012-sha512-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 3059 bytes --]

From 9dbf2816fd77fbd502b58d03f9f67e4894f8d78f Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:42:20 +0200
Subject: [PATCH 12/14] sha512-buffer tests: New module.

* tests/test-sha512-stream.c: Renamed from tests/test-sha512.c.
* modules/crypto/sha512-buffer-tests: New file, based on
modules/crypto/sha512-tests.
* modules/crypto/sha512-tests: Remove tests that are now in
modules/crypto/sha512-buffer-tests. Test tests/test-sha512-stream.c
instead of tests/test-sha512.c.
---
 ChangeLog                                     |  8 ++++++++
 modules/crypto/sha512-buffer-tests            | 16 ++++++++++++++++
 modules/crypto/sha512-tests                   | 16 ++++------------
 tests/{test-sha512.c => test-sha512-stream.c} |  0
 4 files changed, 28 insertions(+), 12 deletions(-)
 create mode 100644 modules/crypto/sha512-buffer-tests
 rename tests/{test-sha512.c => test-sha512-stream.c} (100%)

diff --git a/ChangeLog b/ChangeLog
index f3e77f5e4..398c8cfe7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	sha512-buffer tests: New module.
+	* tests/test-sha512-stream.c: Renamed from tests/test-sha512.c.
+	* modules/crypto/sha512-buffer-tests: New file, based on
+	modules/crypto/sha512-tests.
+	* modules/crypto/sha512-tests: Remove tests that are now in
+	modules/crypto/sha512-buffer-tests. Test tests/test-sha512-stream.c
+	instead of tests/test-sha512.c.
+
 	sha512: Clarify module to source relation.
 	* lib/sha512-stream.c: New file, extracted from lib/sha512.c.
 	* lib/sha512.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/sha512-buffer-tests b/modules/crypto/sha512-buffer-tests
new file mode 100644
index 000000000..0b61ec444
--- /dev/null
+++ b/modules/crypto/sha512-buffer-tests
@@ -0,0 +1,16 @@
+Files:
+tests/bench-sha384.c
+tests/bench-sha512.c
+tests/bench-digest.h
+
+Depends-on:
+c99
+getrusage
+gettimeofday
+
+configure.ac:
+
+Makefile.am:
+noinst_PROGRAMS += bench-sha384 bench-sha512
+bench_sha384_LDADD = $(LDADD) @LIB_CRYPTO@
+bench_sha512_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/modules/crypto/sha512-tests b/modules/crypto/sha512-tests
index fba9dc538..9efe7d234 100644
--- a/modules/crypto/sha512-tests
+++ b/modules/crypto/sha512-tests
@@ -1,22 +1,14 @@
 Files:
-tests/test-sha512.c
+tests/test-sha512-stream.c
 tests/test-digest.h
-tests/bench-sha384.c
-tests/bench-sha512.c
-tests/bench-digest.h
 tests/macros.h
 
 Depends-on:
 c99
-getrusage
-gettimeofday
 
 configure.ac:
 
 Makefile.am:
-TESTS += test-sha512
-check_PROGRAMS += test-sha512
-noinst_PROGRAMS += bench-sha384 bench-sha512
-test_sha512_LDADD = $(LDADD) @LIB_CRYPTO@
-bench_sha384_LDADD = $(LDADD) @LIB_CRYPTO@
-bench_sha512_LDADD = $(LDADD) @LIB_CRYPTO@
+TESTS += test-sha512-stream
+check_PROGRAMS += test-sha512-stream
+test_sha512_stream_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/tests/test-sha512.c b/tests/test-sha512-stream.c
similarity index 100%
rename from tests/test-sha512.c
rename to tests/test-sha512-stream.c
-- 
2.25.1


[-- Attachment #14: 0013-sm3-buffer-New-module.patch --]
[-- Type: text/x-patch, Size: 10502 bytes --]

From 81ca30b0dedb08c6ef54c308cd264bc234a060c4 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:42:57 +0200
Subject: [PATCH 13/14] sm3-buffer: New module.

* lib/sm3-stream.c: New file, extracted from lib/sm3.c.
* lib/sm3.c: Don't include stdlib.h, unlocked-io.h.
(BLOCKSIZE, sm3_stream): Moved to sm3-stream.c.
* lib/sm3.h: Tweak.
* modules/crypto/sm3-buffer: New file, based on modules/crypto/sm3.
* modules/crypto/sm3: Rewritten.
---
 ChangeLog                 |  10 ++++
 lib/sm3-stream.c          | 123 ++++++++++++++++++++++++++++++++++++++
 lib/sm3.c                 | 104 ++++----------------------------
 lib/sm3.h                 |   1 +
 modules/crypto/sm3        |  13 +---
 modules/crypto/sm3-buffer |  32 ++++++++++
 6 files changed, 180 insertions(+), 103 deletions(-)
 create mode 100644 lib/sm3-stream.c
 create mode 100644 modules/crypto/sm3-buffer

diff --git a/ChangeLog b/ChangeLog
index 398c8cfe7..074bd3caf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2021-09-18  Bruno Haible  <bruno@clisp.org>
+
+	sm3-buffer: New module.
+	* lib/sm3-stream.c: New file, extracted from lib/sm3.c.
+	* lib/sm3.c: Don't include stdlib.h, unlocked-io.h.
+	(BLOCKSIZE, sm3_stream): Moved to sm3-stream.c.
+	* lib/sm3.h: Tweak.
+	* modules/crypto/sm3-buffer: New file, based on modules/crypto/sm3.
+	* modules/crypto/sm3: Rewritten.
+
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
 	sha512-buffer tests: New module.
diff --git a/lib/sm3-stream.c b/lib/sm3-stream.c
new file mode 100644
index 000000000..de91b8002
--- /dev/null
+++ b/lib/sm3-stream.c
@@ -0,0 +1,123 @@
+/* sm3.c - Functions to compute SM3 message digest of files or memory blocks
+   according to the specification GM/T 004-2012 Cryptographic Hash Algorithm
+   SM3, published by State Encryption Management Bureau, China.
+
+   SM3 cryptographic hash algorithm.
+   <http://www.sca.gov.cn/sca/xwdt/2010-12/17/content_1002389.shtml>
+
+   Copyright (C) 2017-2021 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Jia Zhang <qianyue.zj@alibaba-inc.com>, 2017,
+   considerably copypasting from David Madore's sha256.c */
+
+#include <config.h>
+
+/* Specification.  */
+#if HAVE_OPENSSL_SM3
+# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
+#endif
+#include "sm3.h"
+
+#include <stdlib.h>
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
+#define BLOCKSIZE 32768
+#if BLOCKSIZE % 64 != 0
+# error "invalid BLOCKSIZE"
+#endif
+
+/* Compute SM3 message digest for bytes read from STREAM.  The
+   resulting message digest number will be written into the 32 bytes
+   beginning at RESBLOCK.  */
+int
+sm3_stream (FILE *stream, void *resblock)
+{
+  struct sm3_ctx ctx;
+  size_t sum;
+
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
+  /* Initialize the computation context.  */
+  sm3_init_ctx (&ctx);
+
+  /* Iterate over full file contents.  */
+  while (1)
+    {
+      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
+         computation function processes the whole buffer so that with the
+         next round of the loop another block can be read.  */
+      size_t n;
+      sum = 0;
+
+      /* Read block.  Take care for partial reads.  */
+      while (1)
+        {
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          if (n == 0)
+            {
+              /* Check for the error flag IFF N == 0, so that we don't
+                 exit the loop after a partial read due to e.g., EAGAIN
+                 or EWOULDBLOCK.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  return 1;
+                }
+              goto process_partial_block;
+            }
+
+          /* We've read at least one byte, so ignore errors.  But always
+             check for EOF, since feof may be true even though N > 0.
+             Otherwise, we could end up calling fread after EOF.  */
+          if (feof (stream))
+            goto process_partial_block;
+        }
+
+      /* Process buffer with BLOCKSIZE bytes.  Note that
+                        BLOCKSIZE % 64 == 0
+       */
+      sm3_process_block (buffer, BLOCKSIZE, &ctx);
+    }
+
+ process_partial_block:;
+
+  /* Process any remaining bytes.  */
+  if (sum > 0)
+    sm3_process_bytes (buffer, sum, &ctx);
+
+  /* Construct result in desired memory.  */
+  sm3_finish_ctx (&ctx, resblock);
+  free (buffer);
+  return 0;
+}
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
diff --git a/lib/sm3.c b/lib/sm3.c
index bc4769b3d..0f3b83e7c 100644
--- a/lib/sm3.c
+++ b/lib/sm3.c
@@ -23,12 +23,9 @@
 /* Written by Jia Zhang <qianyue.zj@alibaba-inc.com>, 2017,
    considerably copypasting from David Madore's sha256.c */
 
-#ifndef DEBUG_SM3
-# define DEBUG_SM3 0
-#endif
-
 #include <config.h>
 
+/* Specification.  */
 #if HAVE_OPENSSL_SM3
 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
 #endif
@@ -36,19 +33,8 @@
 
 #include <stdalign.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <string.h>
 
-#if USE_UNLOCKED_IO
-# include "unlocked-io.h"
-#endif
-
-#if ! DEBUG_SM3
-# define dbg_printf(fmt, ...) do { } while (0)
-#else
-# define dbg_printf printf
-#endif
-
 #include <byteswap.h>
 #ifdef WORDS_BIGENDIAN
 # define SWAP(n) (n)
@@ -56,12 +42,18 @@
 # define SWAP(n) bswap_32 (n)
 #endif
 
-#define BLOCKSIZE 32768
-#if BLOCKSIZE % 64 != 0
-# error "invalid BLOCKSIZE"
+#ifndef DEBUG_SM3
+# define DEBUG_SM3 0
+#endif
+
+#if ! DEBUG_SM3
+# define dbg_printf(fmt, ...) do { } while (0)
+#else
+# define dbg_printf printf
 #endif
 
 #if ! HAVE_OPENSSL_SM3
+
 /* This array contains the bytes used to pad the buffer to the next
    64-byte boundary.  */
 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
@@ -145,82 +137,7 @@ sm3_finish_ctx (struct sm3_ctx *ctx, void *resbuf)
   sm3_conclude_ctx (ctx);
   return sm3_read_ctx (ctx, resbuf);
 }
-#endif
-
-/* Compute SM3 message digest for bytes read from STREAM.  The
-   resulting message digest number will be written into the 32 bytes
-   beginning at RESBLOCK.  */
-int
-sm3_stream (FILE *stream, void *resblock)
-{
-  struct sm3_ctx ctx;
-  size_t sum;
-
-  char *buffer = malloc (BLOCKSIZE + 72);
-  if (!buffer)
-    return 1;
-
-  /* Initialize the computation context.  */
-  sm3_init_ctx (&ctx);
-
-  /* Iterate over full file contents.  */
-  while (1)
-    {
-      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
-      size_t n;
-      sum = 0;
-
-      /* Read block.  Take care for partial reads.  */
-      while (1)
-        {
-          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-          sum += n;
-
-          if (sum == BLOCKSIZE)
-            break;
-
-          if (n == 0)
-            {
-              /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
-              if (ferror (stream))
-                {
-                  free (buffer);
-                  return 1;
-                }
-              goto process_partial_block;
-            }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
-        }
-
-      /* Process buffer with BLOCKSIZE bytes.  Note that
-                        BLOCKSIZE % 64 == 0
-       */
-      sm3_process_block (buffer, BLOCKSIZE, &ctx);
-    }
-
- process_partial_block:;
 
-  /* Process any remaining bytes.  */
-  if (sum > 0)
-    sm3_process_bytes (buffer, sum, &ctx);
-
-  /* Construct result in desired memory.  */
-  sm3_finish_ctx (&ctx, resblock);
-  free (buffer);
-  return 0;
-}
-
-#if ! HAVE_OPENSSL_SM3
 /* Compute SM3 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
    output yields to the wanted ASCII representation of the message
@@ -488,6 +405,7 @@ sm3_process_block (const void *buffer, size_t len, struct sm3_ctx *ctx)
       h = ctx->state[7] ^= h;
     }
 }
+
 #endif
 
 /*
diff --git a/lib/sm3.h b/lib/sm3.h
index e1b6d9085..dda4d9814 100644
--- a/lib/sm3.h
+++ b/lib/sm3.h
@@ -90,6 +90,7 @@ extern void *sm3_buffer (const char *buffer, size_t len,
                          void *restrict resblock);
 
 # endif
+
 /* Compute SM3 message digest for bytes read from STREAM.  The
    resulting message digest number will be written into the 32 bytes
    beginning at RESBLOCK.  */
diff --git a/modules/crypto/sm3 b/modules/crypto/sm3
index 3df7d8825..773f0fa91 100644
--- a/modules/crypto/sm3
+++ b/modules/crypto/sm3
@@ -2,22 +2,15 @@ Description:
 Compute SM3 checksums.
 
 Files:
-lib/sm3.h
-lib/sm3.c
-m4/sm3.m4
+lib/sm3-stream.c
 
 Depends-on:
-byteswap
-c99
-extern-inline
-stdalign
-stdint
+crypto/sm3-buffer
 
 configure.ac:
-gl_SM3
 
 Makefile.am:
-lib_SOURCES += sm3.c
+lib_SOURCES += sm3-stream.c
 
 Include:
 "sm3.h"
diff --git a/modules/crypto/sm3-buffer b/modules/crypto/sm3-buffer
new file mode 100644
index 000000000..3df7d8825
--- /dev/null
+++ b/modules/crypto/sm3-buffer
@@ -0,0 +1,32 @@
+Description:
+Compute SM3 checksums.
+
+Files:
+lib/sm3.h
+lib/sm3.c
+m4/sm3.m4
+
+Depends-on:
+byteswap
+c99
+extern-inline
+stdalign
+stdint
+
+configure.ac:
+gl_SM3
+
+Makefile.am:
+lib_SOURCES += sm3.c
+
+Include:
+"sm3.h"
+
+Link:
+$(LIB_CRYPTO)
+
+License:
+LGPLv2+
+
+Maintainer:
+Jia Zhang
-- 
2.25.1


[-- Attachment #15: 0014-sm3-buffer-tests-New-module.patch --]
[-- Type: text/x-patch, Size: 2227 bytes --]

From c63728b59f5916bb62e0053d8211f4d07dbaa771 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 19 Sep 2021 01:43:02 +0200
Subject: [PATCH 14/14] sm3-buffer tests: New module.

* tests/test-sm3-buffer.c: Renamed from tests/test-sm3.c.
* modules/crypto/sm3-buffer-tests: Renamed from
modules/crypto/sm3-tests. Test tests/test-sm3-buffer.c instead of
tests/test-sm3.c.
---
 ChangeLog                               |  6 ++++++
 modules/crypto/sm3-buffer-tests         | 11 +++++++++++
 modules/crypto/sm3-tests                | 11 -----------
 tests/{test-sm3.c => test-sm3-buffer.c} |  0
 4 files changed, 17 insertions(+), 11 deletions(-)
 create mode 100644 modules/crypto/sm3-buffer-tests
 delete mode 100644 modules/crypto/sm3-tests
 rename tests/{test-sm3.c => test-sm3-buffer.c} (100%)

diff --git a/ChangeLog b/ChangeLog
index 074bd3caf..154678293 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2021-09-18  Bruno Haible  <bruno@clisp.org>
 
+	sm3-buffer tests: New module.
+	* tests/test-sm3-buffer.c: Renamed from tests/test-sm3.c.
+	* modules/crypto/sm3-buffer-tests: Renamed from
+	modules/crypto/sm3-tests. Test tests/test-sm3-buffer.c instead of
+	tests/test-sm3.c.
+
 	sm3-buffer: New module.
 	* lib/sm3-stream.c: New file, extracted from lib/sm3.c.
 	* lib/sm3.c: Don't include stdlib.h, unlocked-io.h.
diff --git a/modules/crypto/sm3-buffer-tests b/modules/crypto/sm3-buffer-tests
new file mode 100644
index 000000000..983a22d46
--- /dev/null
+++ b/modules/crypto/sm3-buffer-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-sm3-buffer.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sm3-buffer
+check_PROGRAMS += test-sm3-buffer
+test_sm3_buffer_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/modules/crypto/sm3-tests b/modules/crypto/sm3-tests
deleted file mode 100644
index fac6ca6c2..000000000
--- a/modules/crypto/sm3-tests
+++ /dev/null
@@ -1,11 +0,0 @@
-Files:
-tests/test-sm3.c
-
-Depends-on:
-
-configure.ac:
-
-Makefile.am:
-TESTS += test-sm3
-check_PROGRAMS += test-sm3
-test_sm3_LDADD = $(LDADD) @LIB_CRYPTO@
diff --git a/tests/test-sm3.c b/tests/test-sm3-buffer.c
similarity index 100%
rename from tests/test-sm3.c
rename to tests/test-sm3-buffer.c
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Improve 'configure --help' output for crypto modules
  2021-09-07 23:23     ` Bruno Haible
  2021-09-08  0:12       ` Paul Eggert
@ 2021-09-19 13:20       ` Bruno Haible
  1 sibling, 0 replies; 7+ messages in thread
From: Bruno Haible @ 2021-09-19 13:20 UTC (permalink / raw)
  To: bug-gnulib

I wrote:
> And I got confused by
> ...
> 2) the 'configure --help' output which suggest to use an option
>    '--with-linux-crypto' — but that option does not exist.

This patch improves several things regarding the 'configure --help' output.


2021-09-19  Bruno Haible  <bruno@clisp.org>

	Improve 'configure --help' output for crypto modules.
	* m4/af_alg.m4 (gl_AF_ALG): List the hash functions for which we use the
	Linux crypto. Also mention that we use it on files only.
	* m4/gl-openssl.m4 (gl_CRYPTO_CHECK): Use manual line breaking, not the
	line-filling style of AS_HELP_STRING. Show that --with-openssl takes an
	optional argument. List the hash functions for which we use the OpenSSL
	library. Don't mention --with-linux-crypto if the af_alg module is not
	in use. Mention that --with-linux-crypto applies only to the Linux
	kernel. Mention that it gets used only when available. Mention that it
	takes precedence only for files. In the error or warning, mention the
	pre-built package name that people can install.

diff --git a/m4/af_alg.m4 b/m4/af_alg.m4
index cba09e89d..601cf6747 100644
--- a/m4/af_alg.m4
+++ b/m4/af_alg.m4
@@ -1,4 +1,4 @@
-# af_alg.m4 serial 5
+# af_alg.m4 serial 6
 dnl Copyright 2018-2021 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -35,7 +35,8 @@ AC_DEFUN_ONCE([gl_AF_ALG],
   use_af_alg=no
   AC_ARG_WITH([linux-crypto],
     [AS_HELP_STRING([[--with-linux-crypto]],
-       [use Linux kernel cryptographic API (if available)])],
+       [use Linux kernel cryptographic API (if available) for the hash functions
+        MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512 on files])],
     [use_af_alg=$withval],
     [use_af_alg=no])
   dnl We cannot use it if it is not available.
diff --git a/m4/gl-openssl.m4 b/m4/gl-openssl.m4
index 96a7268a2..c968eb681 100644
--- a/m4/gl-openssl.m4
+++ b/m4/gl-openssl.m4
@@ -1,4 +1,4 @@
-# gl-openssl.m4 serial 5
+# gl-openssl.m4 serial 6
 dnl Copyright (C) 2013-2021 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -19,15 +19,20 @@ AC_DEFUN([gl_CRYPTO_CHECK],
   m4_divert_once([DEFAULTS], [LIB_CRYPTO=])
 
   AC_ARG_WITH([openssl],
-    [AS_HELP_STRING([--with-openssl],
-      [use libcrypto hash routines. Valid ARGs are:
-       'yes', 'no',
-       'auto' => use if any version available,
-       'auto-gpl-compat' => use if gpl compatible version available,
-       'optional' => use if available and warn if not available;
-       default is ']gl_CRYPTO_CHECK_DEFAULT['.
-       Note also --with-linux-crypto, which will enable
-       use of kernel crypto routines, which have precedence])],
+    [[  --with-openssl[=ARG]    use libcrypto hash routines for the hash functions
+                          MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512.
+                          Valid ARGs are:
+                            'yes',
+                            'no',
+                            'auto' => use if any version available,
+                            'auto-gpl-compat' => use if GPL compatible version
+                                                 available,
+                            'optional' => use if available
+                                          and warn if not available;
+                          Default is ']gl_CRYPTO_CHECK_DEFAULT['.]m4_ifdef([gl_AF_ALG], [
+                          Note also --with-linux-crypto, which will enable the
+                          use of Linux kernel crypto routines (if available),
+                          which has precedence for files.])],
     [],
     [with_openssl=$with_openssl_default])
 
@@ -56,10 +61,15 @@ AC_DEFUN([gl_CRYPTO_CHECK],
               [Define to 1 if libcrypto is used for $1.])])])
     fi
     if test "x$LIB_CRYPTO" = x; then
+      message='openssl development library not found for $1.
+  If you want to install it, first find the pre-built package name:
+    - On Debian and Debian-based systems: libssl-dev,
+    - On Red Hat distributions: openssl-devel.
+    - Other: https://repology.org/project/openssl/versions'
       if test "x$with_openssl" = xyes; then
-        AC_MSG_ERROR([openssl development library not found for $1])
+        AC_MSG_ERROR([$message])
       elif test "x$with_openssl" = xoptional; then
-        AC_MSG_WARN([openssl development library not found for $1])
+        AC_MSG_WARN([$message])
       fi
     fi
   fi





^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-09-19 13:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-07  1:44 [PATCH] crypto: add missing file af_alg.h Paul Eggert
2021-09-07 18:15 ` Bruno Haible
2021-09-07 21:51   ` Paul Eggert
2021-09-07 23:23     ` Bruno Haible
2021-09-08  0:12       ` Paul Eggert
2021-09-19  0:02         ` crypto digests modules restructuring Bruno Haible
2021-09-19 13:20       ` Improve 'configure --help' output for crypto modules Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).