summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gl/Makefile.am16
-rw-r--r--gl/m4/gnulib-cache.m44
-rw-r--r--gl/m4/gnulib-common.m43
-rw-r--r--gl/m4/gnulib-comp.m414
-rw-r--r--gl/m4/sha256.m4 (renamed from gl/m4/sha1.m4)12
-rw-r--r--gl/math.in.h2
-rw-r--r--gl/sha1.c361
-rw-r--r--gl/sha256-stream.c (renamed from gl/sha1-stream.c)54
-rw-r--r--gl/sha256.c433
-rw-r--r--gl/sha256.h (renamed from gl/sha1.h)70
-rw-r--r--gl/stdbool.in.h12
-rw-r--r--gl/stdlib.in.h6
-rw-r--r--gl/sys/stat.h76
-rw-r--r--gl/sys_stat.in.h76
-rw-r--r--gl/unistd.in.h2
15 files changed, 623 insertions, 518 deletions
diff --git a/gl/Makefile.am b/gl/Makefile.am
index deb665cf..d0acf414 100644
--- a/gl/Makefile.am
+++ b/gl/Makefile.am
@@ -34,7 +34,7 @@
34# --macro-prefix=gl \ 34# --macro-prefix=gl \
35# --no-vc-files \ 35# --no-vc-files \
36# base64 \ 36# base64 \
37# crypto/sha1 \ 37# crypto/sha256 \
38# dirname \ 38# dirname \
39# environ \ 39# environ \
40# floorf \ 40# floorf \
@@ -241,19 +241,19 @@ EXTRA_DIST += af_alg.h sys-limits.h
241 241
242## end gnulib module crypto/af_alg 242## end gnulib module crypto/af_alg
243 243
244## begin gnulib module crypto/sha1 244## begin gnulib module crypto/sha256
245 245
246libgnu_a_SOURCES += sha1-stream.c 246libgnu_a_SOURCES += sha256-stream.c
247 247
248## end gnulib module crypto/sha1 248## end gnulib module crypto/sha256
249 249
250## begin gnulib module crypto/sha1-buffer 250## begin gnulib module crypto/sha256-buffer
251 251
252libgnu_a_SOURCES += sha1.c 252libgnu_a_SOURCES += sha256.c
253 253
254EXTRA_DIST += gl_openssl.h sha1.h 254EXTRA_DIST += gl_openssl.h sha256.h
255 255
256## end gnulib module crypto/sha1-buffer 256## end gnulib module crypto/sha256-buffer
257 257
258## begin gnulib module dirname 258## begin gnulib module dirname
259 259
diff --git a/gl/m4/gnulib-cache.m4 b/gl/m4/gnulib-cache.m4
index 9d0128a0..06fed91a 100644
--- a/gl/m4/gnulib-cache.m4
+++ b/gl/m4/gnulib-cache.m4
@@ -39,7 +39,7 @@
39# --macro-prefix=gl \ 39# --macro-prefix=gl \
40# --no-vc-files \ 40# --no-vc-files \
41# base64 \ 41# base64 \
42# crypto/sha1 \ 42# crypto/sha256 \
43# dirname \ 43# dirname \
44# environ \ 44# environ \
45# floorf \ 45# floorf \
@@ -65,7 +65,7 @@
65gl_LOCAL_DIR([]) 65gl_LOCAL_DIR([])
66gl_MODULES([ 66gl_MODULES([
67 base64 67 base64
68 crypto/sha1 68 crypto/sha256
69 dirname 69 dirname
70 environ 70 environ
71 floorf 71 floorf
diff --git a/gl/m4/gnulib-common.m4 b/gl/m4/gnulib-common.m4
index 30911d15..8a5daa23 100644
--- a/gl/m4/gnulib-common.m4
+++ b/gl/m4/gnulib-common.m4
@@ -313,7 +313,8 @@ AC_DEFUN([gl_COMMON_BODY], [
313#else 313#else
314# define _GL_ATTRIBUTE_MAYBE_UNUSED _GL_ATTRIBUTE_UNUSED 314# define _GL_ATTRIBUTE_MAYBE_UNUSED _GL_ATTRIBUTE_UNUSED
315#endif 315#endif
316/* Alternative spelling of this macro, for convenience. */ 316/* Alternative spelling of this macro, for convenience and for
317 compatibility with glibc/include/libc-symbols.h. */
317#define _GL_UNUSED _GL_ATTRIBUTE_MAYBE_UNUSED 318#define _GL_UNUSED _GL_ATTRIBUTE_MAYBE_UNUSED
318/* Earlier spellings of this macro. */ 319/* Earlier spellings of this macro. */
319#define _UNUSED_PARAMETER_ _GL_ATTRIBUTE_MAYBE_UNUSED 320#define _UNUSED_PARAMETER_ _GL_ATTRIBUTE_MAYBE_UNUSED
diff --git a/gl/m4/gnulib-comp.m4 b/gl/m4/gnulib-comp.m4
index a82c5de2..93b78841 100644
--- a/gl/m4/gnulib-comp.m4
+++ b/gl/m4/gnulib-comp.m4
@@ -57,8 +57,8 @@ AC_DEFUN([gl_EARLY],
57 # Code from module cloexec: 57 # Code from module cloexec:
58 # Code from module close: 58 # Code from module close:
59 # Code from module crypto/af_alg: 59 # Code from module crypto/af_alg:
60 # Code from module crypto/sha1: 60 # Code from module crypto/sha256:
61 # Code from module crypto/sha1-buffer: 61 # Code from module crypto/sha256-buffer:
62 # Code from module dirname: 62 # Code from module dirname:
63 # Code from module dirname-lgpl: 63 # Code from module dirname-lgpl:
64 # Code from module double-slash-root: 64 # Code from module double-slash-root:
@@ -265,7 +265,7 @@ AC_DEFUN([gl_INIT],
265 gl_UNISTD_MODULE_INDICATOR([close]) 265 gl_UNISTD_MODULE_INDICATOR([close])
266 gl_AF_ALG 266 gl_AF_ALG
267 AC_REQUIRE([AC_C_RESTRICT]) 267 AC_REQUIRE([AC_C_RESTRICT])
268 gl_SHA1 268 gl_SHA256
269 gl_MODULE_INDICATOR([dirname]) 269 gl_MODULE_INDICATOR([dirname])
270 gl_DOUBLE_SLASH_ROOT 270 gl_DOUBLE_SLASH_ROOT
271 gl_FUNC_DUP2 271 gl_FUNC_DUP2
@@ -1130,9 +1130,9 @@ AC_DEFUN([gl_FILE_LIST], [
1130 lib/setlocale-lock.c 1130 lib/setlocale-lock.c
1131 lib/setlocale_null.c 1131 lib/setlocale_null.c
1132 lib/setlocale_null.h 1132 lib/setlocale_null.h
1133 lib/sha1-stream.c 1133 lib/sha256-stream.c
1134 lib/sha1.c 1134 lib/sha256.c
1135 lib/sha1.h 1135 lib/sha256.h
1136 lib/size_max.h 1136 lib/size_max.h
1137 lib/snprintf.c 1137 lib/snprintf.c
1138 lib/sockets.c 1138 lib/sockets.c
@@ -1316,7 +1316,7 @@ AC_DEFUN([gl_FILE_LIST], [
1316 m4/servent.m4 1316 m4/servent.m4
1317 m4/setenv.m4 1317 m4/setenv.m4
1318 m4/setlocale_null.m4 1318 m4/setlocale_null.m4
1319 m4/sha1.m4 1319 m4/sha256.m4
1320 m4/size_max.m4 1320 m4/size_max.m4
1321 m4/snprintf.m4 1321 m4/snprintf.m4
1322 m4/socketlib.m4 1322 m4/socketlib.m4
diff --git a/gl/m4/sha1.m4 b/gl/m4/sha256.m4
index 0d7abc72..b3d8f92e 100644
--- a/gl/m4/sha1.m4
+++ b/gl/m4/sha256.m4
@@ -1,14 +1,14 @@
1# sha1.m4 serial 12 1# sha256.m4 serial 8
2dnl Copyright (C) 2002-2006, 2008-2022 Free Software Foundation, Inc. 2dnl Copyright (C) 2005, 2008-2022 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation 3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it, 4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved. 5dnl with or without modifications, as long as this notice is preserved.
6 6
7AC_DEFUN([gl_SHA1], 7AC_DEFUN([gl_SHA256],
8[ 8[
9 dnl Prerequisites of lib/sha1.c. 9 dnl Prerequisites of lib/sha256.c.
10 AC_REQUIRE([gl_BIGENDIAN]) 10 AC_REQUIRE([gl_BIGENDIAN])
11 11
12 dnl Determine HAVE_OPENSSL_SHA1 and LIB_CRYPTO 12 dnl Determine HAVE_OPENSSL_SHA256 and LIB_CRYPTO
13 gl_CRYPTO_CHECK([SHA1]) 13 gl_CRYPTO_CHECK([SHA256])
14]) 14])
diff --git a/gl/math.in.h b/gl/math.in.h
index a74a95da..270e71f4 100644
--- a/gl/math.in.h
+++ b/gl/math.in.h
@@ -2591,7 +2591,7 @@ _GL_EXTERN_C int rpl_isnanl (long double x) _GL_ATTRIBUTE_CONST;
2591# if defined isnan || defined GNULIB_NAMESPACE 2591# if defined isnan || defined GNULIB_NAMESPACE
2592_GL_MATH_CXX_REAL_FLOATING_DECL_1 (isnan) 2592_GL_MATH_CXX_REAL_FLOATING_DECL_1 (isnan)
2593# undef isnan 2593# undef isnan
2594# if __GNUC__ >= 6 || (defined __clang__ && !((defined __APPLE__ && defined __MACH__ && __clang_major__ < 12) || (defined __FreeBSD__ && __clang_major__ < 7) || defined __OpenBSD__ || (defined _WIN32 && !defined __CYGWIN__))) 2594# if __GNUC__ >= 6 || (defined __clang__ && !((defined __APPLE__ && defined __MACH__ && __clang_major__ < 12) || (defined __FreeBSD__ && (__clang_major__ < 7 || __clang_major__ >= 11)) || defined __OpenBSD__ || (defined _WIN32 && !defined __CYGWIN__)))
2595 /* This platform's <cmath> possibly defines isnan through a set of inline 2595 /* This platform's <cmath> possibly defines isnan through a set of inline
2596 functions. */ 2596 functions. */
2597_GL_MATH_CXX_REAL_FLOATING_DECL_2 (isnan, rpl_isnan, bool) 2597_GL_MATH_CXX_REAL_FLOATING_DECL_2 (isnan, rpl_isnan, bool)
diff --git a/gl/sha1.c b/gl/sha1.c
deleted file mode 100644
index 79e50ba0..00000000
--- a/gl/sha1.c
+++ /dev/null
@@ -1,361 +0,0 @@
1/* sha1.c - Functions to compute SHA1 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1.
3
4 Copyright (C) 2000-2001, 2003-2006, 2008-2022 Free Software Foundation, Inc.
5
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19/* Written by Scott G. Miller
20 Credits:
21 Robert Klep <robert@ilse.nl> -- Expansion function fix
22*/
23
24#include <config.h>
25
26/* Specification. */
27#if HAVE_OPENSSL_SHA1
28# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
29#endif
30#include "sha1.h"
31
32#include <stdalign.h>
33#include <stdint.h>
34#include <string.h>
35
36#include <byteswap.h>
37#ifdef WORDS_BIGENDIAN
38# define SWAP(n) (n)
39#else
40# define SWAP(n) bswap_32 (n)
41#endif
42
43#if ! HAVE_OPENSSL_SHA1
44
45/* This array contains the bytes used to pad the buffer to the next
46 64-byte boundary. (RFC 1321, 3.1: Step 1) */
47static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
48
49
50/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
51 initialize it to the start constants of the SHA1 algorithm. This
52 must be called before using hash in the call to sha1_hash. */
53void
54sha1_init_ctx (struct sha1_ctx *ctx)
55{
56 ctx->A = 0x67452301;
57 ctx->B = 0xefcdab89;
58 ctx->C = 0x98badcfe;
59 ctx->D = 0x10325476;
60 ctx->E = 0xc3d2e1f0;
61
62 ctx->total[0] = ctx->total[1] = 0;
63 ctx->buflen = 0;
64}
65
66/* Copy the 4 byte value from v into the memory location pointed to by *cp,
67 If your architecture allows unaligned access this is equivalent to
68 * (uint32_t *) cp = v */
69static void
70set_uint32 (char *cp, uint32_t v)
71{
72 memcpy (cp, &v, sizeof v);
73}
74
75/* Put result from CTX in first 20 bytes following RESBUF. The result
76 must be in little endian byte order. */
77void *
78sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
79{
80 char *r = resbuf;
81 set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
82 set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
83 set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
84 set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
85 set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
86
87 return resbuf;
88}
89
90/* Process the remaining bytes in the internal buffer and the usual
91 prolog according to the standard and write the result to RESBUF. */
92void *
93sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
94{
95 /* Take yet unprocessed bytes into account. */
96 uint32_t bytes = ctx->buflen;
97 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
98
99 /* Now count remaining bytes. */
100 ctx->total[0] += bytes;
101 if (ctx->total[0] < bytes)
102 ++ctx->total[1];
103
104 /* Put the 64-bit file length in *bits* at the end of the buffer. */
105 ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
106 ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
107
108 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
109
110 /* Process last bytes. */
111 sha1_process_block (ctx->buffer, size * 4, ctx);
112
113 return sha1_read_ctx (ctx, resbuf);
114}
115
116/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
117 result is always in little endian byte order, so that a byte-wise
118 output yields to the wanted ASCII representation of the message
119 digest. */
120void *
121sha1_buffer (const char *buffer, size_t len, void *resblock)
122{
123 struct sha1_ctx ctx;
124
125 /* Initialize the computation context. */
126 sha1_init_ctx (&ctx);
127
128 /* Process whole buffer but last len % 64 bytes. */
129 sha1_process_bytes (buffer, len, &ctx);
130
131 /* Put result in desired memory area. */
132 return sha1_finish_ctx (&ctx, resblock);
133}
134
135void
136sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
137{
138 /* When we already have some bits in our internal buffer concatenate
139 both inputs first. */
140 if (ctx->buflen != 0)
141 {
142 size_t left_over = ctx->buflen;
143 size_t add = 128 - left_over > len ? len : 128 - left_over;
144
145 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
146 ctx->buflen += add;
147
148 if (ctx->buflen > 64)
149 {
150 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
151
152 ctx->buflen &= 63;
153 /* The regions in the following copy operation cannot overlap,
154 because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
155 memcpy (ctx->buffer,
156 &((char *) ctx->buffer)[(left_over + add) & ~63],
157 ctx->buflen);
158 }
159
160 buffer = (const char *) buffer + add;
161 len -= add;
162 }
163
164 /* Process available complete blocks. */
165 if (len >= 64)
166 {
167#if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
168# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
169 if (UNALIGNED_P (buffer))
170 while (len > 64)
171 {
172 sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
173 buffer = (const char *) buffer + 64;
174 len -= 64;
175 }
176 else
177#endif
178 {
179 sha1_process_block (buffer, len & ~63, ctx);
180 buffer = (const char *) buffer + (len & ~63);
181 len &= 63;
182 }
183 }
184
185 /* Move remaining bytes in internal buffer. */
186 if (len > 0)
187 {
188 size_t left_over = ctx->buflen;
189
190 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
191 left_over += len;
192 if (left_over >= 64)
193 {
194 sha1_process_block (ctx->buffer, 64, ctx);
195 left_over -= 64;
196 /* The regions in the following copy operation cannot overlap,
197 because left_over ≤ 64. */
198 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
199 }
200 ctx->buflen = left_over;
201 }
202}
203
204/* --- Code below is the primary difference between md5.c and sha1.c --- */
205
206/* SHA1 round constants */
207#define K1 0x5a827999
208#define K2 0x6ed9eba1
209#define K3 0x8f1bbcdc
210#define K4 0xca62c1d6
211
212/* Round functions. Note that F2 is the same as F4. */
213#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
214#define F2(B,C,D) (B ^ C ^ D)
215#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
216#define F4(B,C,D) (B ^ C ^ D)
217
218/* Process LEN bytes of BUFFER, accumulating context into CTX.
219 It is assumed that LEN % 64 == 0.
220 Most of this code comes from GnuPG's cipher/sha1.c. */
221
222void
223sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
224{
225 const uint32_t *words = buffer;
226 size_t nwords = len / sizeof (uint32_t);
227 const uint32_t *endp = words + nwords;
228 uint32_t x[16];
229 uint32_t a = ctx->A;
230 uint32_t b = ctx->B;
231 uint32_t c = ctx->C;
232 uint32_t d = ctx->D;
233 uint32_t e = ctx->E;
234 uint32_t lolen = len;
235
236 /* First increment the byte count. RFC 1321 specifies the possible
237 length of the file up to 2^64 bits. Here we only compute the
238 number of bytes. Do a double word increment. */
239 ctx->total[0] += lolen;
240 ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
241
242#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
243
244#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
245 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
246 , (x[I&0x0f] = rol(tm, 1)) )
247
248#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
249 + F( B, C, D ) \
250 + K \
251 + M; \
252 B = rol( B, 30 ); \
253 } while(0)
254
255 while (words < endp)
256 {
257 uint32_t tm;
258 int t;
259 for (t = 0; t < 16; t++)
260 {
261 x[t] = SWAP (*words);
262 words++;
263 }
264
265 R( a, b, c, d, e, F1, K1, x[ 0] );
266 R( e, a, b, c, d, F1, K1, x[ 1] );
267 R( d, e, a, b, c, F1, K1, x[ 2] );
268 R( c, d, e, a, b, F1, K1, x[ 3] );
269 R( b, c, d, e, a, F1, K1, x[ 4] );
270 R( a, b, c, d, e, F1, K1, x[ 5] );
271 R( e, a, b, c, d, F1, K1, x[ 6] );
272 R( d, e, a, b, c, F1, K1, x[ 7] );
273 R( c, d, e, a, b, F1, K1, x[ 8] );
274 R( b, c, d, e, a, F1, K1, x[ 9] );
275 R( a, b, c, d, e, F1, K1, x[10] );
276 R( e, a, b, c, d, F1, K1, x[11] );
277 R( d, e, a, b, c, F1, K1, x[12] );
278 R( c, d, e, a, b, F1, K1, x[13] );
279 R( b, c, d, e, a, F1, K1, x[14] );
280 R( a, b, c, d, e, F1, K1, x[15] );
281 R( e, a, b, c, d, F1, K1, M(16) );
282 R( d, e, a, b, c, F1, K1, M(17) );
283 R( c, d, e, a, b, F1, K1, M(18) );
284 R( b, c, d, e, a, F1, K1, M(19) );
285 R( a, b, c, d, e, F2, K2, M(20) );
286 R( e, a, b, c, d, F2, K2, M(21) );
287 R( d, e, a, b, c, F2, K2, M(22) );
288 R( c, d, e, a, b, F2, K2, M(23) );
289 R( b, c, d, e, a, F2, K2, M(24) );
290 R( a, b, c, d, e, F2, K2, M(25) );
291 R( e, a, b, c, d, F2, K2, M(26) );
292 R( d, e, a, b, c, F2, K2, M(27) );
293 R( c, d, e, a, b, F2, K2, M(28) );
294 R( b, c, d, e, a, F2, K2, M(29) );
295 R( a, b, c, d, e, F2, K2, M(30) );
296 R( e, a, b, c, d, F2, K2, M(31) );
297 R( d, e, a, b, c, F2, K2, M(32) );
298 R( c, d, e, a, b, F2, K2, M(33) );
299 R( b, c, d, e, a, F2, K2, M(34) );
300 R( a, b, c, d, e, F2, K2, M(35) );
301 R( e, a, b, c, d, F2, K2, M(36) );
302 R( d, e, a, b, c, F2, K2, M(37) );
303 R( c, d, e, a, b, F2, K2, M(38) );
304 R( b, c, d, e, a, F2, K2, M(39) );
305 R( a, b, c, d, e, F3, K3, M(40) );
306 R( e, a, b, c, d, F3, K3, M(41) );
307 R( d, e, a, b, c, F3, K3, M(42) );
308 R( c, d, e, a, b, F3, K3, M(43) );
309 R( b, c, d, e, a, F3, K3, M(44) );
310 R( a, b, c, d, e, F3, K3, M(45) );
311 R( e, a, b, c, d, F3, K3, M(46) );
312 R( d, e, a, b, c, F3, K3, M(47) );
313 R( c, d, e, a, b, F3, K3, M(48) );
314 R( b, c, d, e, a, F3, K3, M(49) );
315 R( a, b, c, d, e, F3, K3, M(50) );
316 R( e, a, b, c, d, F3, K3, M(51) );
317 R( d, e, a, b, c, F3, K3, M(52) );
318 R( c, d, e, a, b, F3, K3, M(53) );
319 R( b, c, d, e, a, F3, K3, M(54) );
320 R( a, b, c, d, e, F3, K3, M(55) );
321 R( e, a, b, c, d, F3, K3, M(56) );
322 R( d, e, a, b, c, F3, K3, M(57) );
323 R( c, d, e, a, b, F3, K3, M(58) );
324 R( b, c, d, e, a, F3, K3, M(59) );
325 R( a, b, c, d, e, F4, K4, M(60) );
326 R( e, a, b, c, d, F4, K4, M(61) );
327 R( d, e, a, b, c, F4, K4, M(62) );
328 R( c, d, e, a, b, F4, K4, M(63) );
329 R( b, c, d, e, a, F4, K4, M(64) );
330 R( a, b, c, d, e, F4, K4, M(65) );
331 R( e, a, b, c, d, F4, K4, M(66) );
332 R( d, e, a, b, c, F4, K4, M(67) );
333 R( c, d, e, a, b, F4, K4, M(68) );
334 R( b, c, d, e, a, F4, K4, M(69) );
335 R( a, b, c, d, e, F4, K4, M(70) );
336 R( e, a, b, c, d, F4, K4, M(71) );
337 R( d, e, a, b, c, F4, K4, M(72) );
338 R( c, d, e, a, b, F4, K4, M(73) );
339 R( b, c, d, e, a, F4, K4, M(74) );
340 R( a, b, c, d, e, F4, K4, M(75) );
341 R( e, a, b, c, d, F4, K4, M(76) );
342 R( d, e, a, b, c, F4, K4, M(77) );
343 R( c, d, e, a, b, F4, K4, M(78) );
344 R( b, c, d, e, a, F4, K4, M(79) );
345
346 a = ctx->A += a;
347 b = ctx->B += b;
348 c = ctx->C += c;
349 d = ctx->D += d;
350 e = ctx->E += e;
351 }
352}
353
354#endif
355
356/*
357 * Hey Emacs!
358 * Local Variables:
359 * coding: utf-8
360 * End:
361 */
diff --git a/gl/sha1-stream.c b/gl/sha256-stream.c
index 0ba04352..c2e63989 100644
--- a/gl/sha1-stream.c
+++ b/gl/sha256-stream.c
@@ -1,7 +1,7 @@
1/* sha1.c - Functions to compute SHA1 message digest of files or 1/* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1. 2 memory blocks according to the NIST specification FIPS-180-2.
3 3
4 Copyright (C) 2000-2001, 2003-2006, 2008-2022 Free Software Foundation, Inc. 4 Copyright (C) 2005-2006, 2008-2022 Free Software Foundation, Inc.
5 5
6 This file is free software: you can redistribute it and/or modify 6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as 7 it under the terms of the GNU Lesser General Public License as
@@ -16,18 +16,17 @@
16 You should have received a copy of the GNU Lesser General Public License 16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 18
19/* Written by Scott G. Miller 19/* Written by David Madore, considerably copypasting from
20 Credits: 20 Scott G. Miller's sha1.c
21 Robert Klep <robert@ilse.nl> -- Expansion function fix
22*/ 21*/
23 22
24#include <config.h> 23#include <config.h>
25 24
26/* Specification. */ 25/* Specification. */
27#if HAVE_OPENSSL_SHA1 26#if HAVE_OPENSSL_SHA256
28# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE 27# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
29#endif 28#endif
30#include "sha1.h" 29#include "sha256.h"
31 30
32#include <stdlib.h> 31#include <stdlib.h>
33 32
@@ -42,13 +41,16 @@
42# error "invalid BLOCKSIZE" 41# error "invalid BLOCKSIZE"
43#endif 42#endif
44 43
45/* Compute SHA1 message digest for bytes read from STREAM. The 44/* Compute message digest for bytes read from STREAM using algorithm ALG.
46 resulting message digest number will be written into the 20 bytes 45 Write the message digest into RESBLOCK, which contains HASHLEN bytes.
47 beginning at RESBLOCK. */ 46 The initial and finishing operations are INIT_CTX and FINISH_CTX.
48int 47 Return zero if and only if successful. */
49sha1_stream (FILE *stream, void *resblock) 48static int
49shaxxx_stream (FILE *stream, char const *alg, void *resblock,
50 ssize_t hashlen, void (*init_ctx) (struct sha256_ctx *),
51 void *(*finish_ctx) (struct sha256_ctx *, void *))
50{ 52{
51 switch (afalg_stream (stream, "sha1", resblock, SHA1_DIGEST_SIZE)) 53 switch (afalg_stream (stream, alg, resblock, hashlen))
52 { 54 {
53 case 0: return 0; 55 case 0: return 0;
54 case -EIO: return 1; 56 case -EIO: return 1;
@@ -58,8 +60,8 @@ sha1_stream (FILE *stream, void *resblock)
58 if (!buffer) 60 if (!buffer)
59 return 1; 61 return 1;
60 62
61 struct sha1_ctx ctx; 63 struct sha256_ctx ctx;
62 sha1_init_ctx (&ctx); 64 init_ctx (&ctx);
63 size_t sum; 65 size_t sum;
64 66
65 /* Iterate over full file contents. */ 67 /* Iterate over full file contents. */
@@ -106,21 +108,35 @@ sha1_stream (FILE *stream, void *resblock)
106 /* Process buffer with BLOCKSIZE bytes. Note that 108 /* Process buffer with BLOCKSIZE bytes. Note that
107 BLOCKSIZE % 64 == 0 109 BLOCKSIZE % 64 == 0
108 */ 110 */
109 sha1_process_block (buffer, BLOCKSIZE, &ctx); 111 sha256_process_block (buffer, BLOCKSIZE, &ctx);
110 } 112 }
111 113
112 process_partial_block:; 114 process_partial_block:;
113 115
114 /* Process any remaining bytes. */ 116 /* Process any remaining bytes. */
115 if (sum > 0) 117 if (sum > 0)
116 sha1_process_bytes (buffer, sum, &ctx); 118 sha256_process_bytes (buffer, sum, &ctx);
117 119
118 /* Construct result in desired memory. */ 120 /* Construct result in desired memory. */
119 sha1_finish_ctx (&ctx, resblock); 121 finish_ctx (&ctx, resblock);
120 free (buffer); 122 free (buffer);
121 return 0; 123 return 0;
122} 124}
123 125
126int
127sha256_stream (FILE *stream, void *resblock)
128{
129 return shaxxx_stream (stream, "sha256", resblock, SHA256_DIGEST_SIZE,
130 sha256_init_ctx, sha256_finish_ctx);
131}
132
133int
134sha224_stream (FILE *stream, void *resblock)
135{
136 return shaxxx_stream (stream, "sha224", resblock, SHA224_DIGEST_SIZE,
137 sha224_init_ctx, sha224_finish_ctx);
138}
139
124/* 140/*
125 * Hey Emacs! 141 * Hey Emacs!
126 * Local Variables: 142 * Local Variables:
diff --git a/gl/sha256.c b/gl/sha256.c
new file mode 100644
index 00000000..c9ca618c
--- /dev/null
+++ b/gl/sha256.c
@@ -0,0 +1,433 @@
1/* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-2.
3
4 Copyright (C) 2005-2006, 2008-2022 Free Software Foundation, Inc.
5
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19/* Written by David Madore, considerably copypasting from
20 Scott G. Miller's sha1.c
21*/
22
23#include <config.h>
24
25/* Specification. */
26#if HAVE_OPENSSL_SHA256
27# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
28#endif
29#include "sha256.h"
30
31#include <stdalign.h>
32#include <stdint.h>
33#include <string.h>
34
35#include <byteswap.h>
36#ifdef WORDS_BIGENDIAN
37# define SWAP(n) (n)
38#else
39# define SWAP(n) bswap_32 (n)
40#endif
41
42#if ! HAVE_OPENSSL_SHA256
43
44/* This array contains the bytes used to pad the buffer to the next
45 64-byte boundary. */
46static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
47
48
49/*
50 Takes a pointer to a 256 bit block of data (eight 32 bit ints) and
51 initializes it to the start constants of the SHA256 algorithm. This
52 must be called before using hash in the call to sha256_hash
53*/
54void
55sha256_init_ctx (struct sha256_ctx *ctx)
56{
57 ctx->state[0] = 0x6a09e667UL;
58 ctx->state[1] = 0xbb67ae85UL;
59 ctx->state[2] = 0x3c6ef372UL;
60 ctx->state[3] = 0xa54ff53aUL;
61 ctx->state[4] = 0x510e527fUL;
62 ctx->state[5] = 0x9b05688cUL;
63 ctx->state[6] = 0x1f83d9abUL;
64 ctx->state[7] = 0x5be0cd19UL;
65
66 ctx->total[0] = ctx->total[1] = 0;
67 ctx->buflen = 0;
68}
69
70void
71sha224_init_ctx (struct sha256_ctx *ctx)
72{
73 ctx->state[0] = 0xc1059ed8UL;
74 ctx->state[1] = 0x367cd507UL;
75 ctx->state[2] = 0x3070dd17UL;
76 ctx->state[3] = 0xf70e5939UL;
77 ctx->state[4] = 0xffc00b31UL;
78 ctx->state[5] = 0x68581511UL;
79 ctx->state[6] = 0x64f98fa7UL;
80 ctx->state[7] = 0xbefa4fa4UL;
81
82 ctx->total[0] = ctx->total[1] = 0;
83 ctx->buflen = 0;
84}
85
86/* Copy the value from v into the memory location pointed to by *CP,
87 If your architecture allows unaligned access, this is equivalent to
88 * (__typeof__ (v) *) cp = v */
89static void
90set_uint32 (char *cp, uint32_t v)
91{
92 memcpy (cp, &v, sizeof v);
93}
94
95/* Put result from CTX in first 32 bytes following RESBUF.
96 The result must be in little endian byte order. */
97void *
98sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
99{
100 int i;
101 char *r = resbuf;
102
103 for (i = 0; i < 8; i++)
104 set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
105
106 return resbuf;
107}
108
109void *
110sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
111{
112 int i;
113 char *r = resbuf;
114
115 for (i = 0; i < 7; i++)
116 set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
117
118 return resbuf;
119}
120
121/* Process the remaining bytes in the internal buffer and the usual
122 prolog according to the standard and write the result to RESBUF. */
123static void
124sha256_conclude_ctx (struct sha256_ctx *ctx)
125{
126 /* Take yet unprocessed bytes into account. */
127 size_t bytes = ctx->buflen;
128 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
129
130 /* Now count remaining bytes. */
131 ctx->total[0] += bytes;
132 if (ctx->total[0] < bytes)
133 ++ctx->total[1];
134
135 /* Put the 64-bit file length in *bits* at the end of the buffer.
136 Use set_uint32 rather than a simple assignment, to avoid risk of
137 unaligned access. */
138 set_uint32 ((char *) &ctx->buffer[size - 2],
139 SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
140 set_uint32 ((char *) &ctx->buffer[size - 1],
141 SWAP (ctx->total[0] << 3));
142
143 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
144
145 /* Process last bytes. */
146 sha256_process_block (ctx->buffer, size * 4, ctx);
147}
148
149void *
150sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
151{
152 sha256_conclude_ctx (ctx);
153 return sha256_read_ctx (ctx, resbuf);
154}
155
156void *
157sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
158{
159 sha256_conclude_ctx (ctx);
160 return sha224_read_ctx (ctx, resbuf);
161}
162
163/* Compute SHA256 message digest for LEN bytes beginning at BUFFER. The
164 result is always in little endian byte order, so that a byte-wise
165 output yields to the wanted ASCII representation of the message
166 digest. */
167void *
168sha256_buffer (const char *buffer, size_t len, void *resblock)
169{
170 struct sha256_ctx ctx;
171
172 /* Initialize the computation context. */
173 sha256_init_ctx (&ctx);
174
175 /* Process whole buffer but last len % 64 bytes. */
176 sha256_process_bytes (buffer, len, &ctx);
177
178 /* Put result in desired memory area. */
179 return sha256_finish_ctx (&ctx, resblock);
180}
181
182void *
183sha224_buffer (const char *buffer, size_t len, void *resblock)
184{
185 struct sha256_ctx ctx;
186
187 /* Initialize the computation context. */
188 sha224_init_ctx (&ctx);
189
190 /* Process whole buffer but last len % 64 bytes. */
191 sha256_process_bytes (buffer, len, &ctx);
192
193 /* Put result in desired memory area. */
194 return sha224_finish_ctx (&ctx, resblock);
195}
196
197void
198sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
199{
200 /* When we already have some bits in our internal buffer concatenate
201 both inputs first. */
202 if (ctx->buflen != 0)
203 {
204 size_t left_over = ctx->buflen;
205 size_t add = 128 - left_over > len ? len : 128 - left_over;
206
207 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
208 ctx->buflen += add;
209
210 if (ctx->buflen > 64)
211 {
212 sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
213
214 ctx->buflen &= 63;
215 /* The regions in the following copy operation cannot overlap,
216 because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
217 memcpy (ctx->buffer,
218 &((char *) ctx->buffer)[(left_over + add) & ~63],
219 ctx->buflen);
220 }
221
222 buffer = (const char *) buffer + add;
223 len -= add;
224 }
225
226 /* Process available complete blocks. */
227 if (len >= 64)
228 {
229#if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
230# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
231 if (UNALIGNED_P (buffer))
232 while (len > 64)
233 {
234 sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
235 buffer = (const char *) buffer + 64;
236 len -= 64;
237 }
238 else
239#endif
240 {
241 sha256_process_block (buffer, len & ~63, ctx);
242 buffer = (const char *) buffer + (len & ~63);
243 len &= 63;
244 }
245 }
246
247 /* Move remaining bytes in internal buffer. */
248 if (len > 0)
249 {
250 size_t left_over = ctx->buflen;
251
252 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
253 left_over += len;
254 if (left_over >= 64)
255 {
256 sha256_process_block (ctx->buffer, 64, ctx);
257 left_over -= 64;
258 /* The regions in the following copy operation cannot overlap,
259 because left_over ≤ 64. */
260 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
261 }
262 ctx->buflen = left_over;
263 }
264}
265
266/* --- Code below is the primary difference between sha1.c and sha256.c --- */
267
268/* SHA256 round constants */
269#define K(I) sha256_round_constants[I]
270static const uint32_t sha256_round_constants[64] = {
271 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
272 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
273 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
274 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
275 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
276 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
277 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
278 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
279 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
280 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
281 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
282 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
283 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
284 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
285 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
286 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
287};
288
289/* Round functions. */
290#define F2(A,B,C) ( ( A & B ) | ( C & ( A | B ) ) )
291#define F1(E,F,G) ( G ^ ( E & ( F ^ G ) ) )
292
293/* Process LEN bytes of BUFFER, accumulating context into CTX.
294 It is assumed that LEN % 64 == 0.
295 Most of this code comes from GnuPG's cipher/sha1.c. */
296
297void
298sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
299{
300 const uint32_t *words = buffer;
301 size_t nwords = len / sizeof (uint32_t);
302 const uint32_t *endp = words + nwords;
303 uint32_t x[16];
304 uint32_t a = ctx->state[0];
305 uint32_t b = ctx->state[1];
306 uint32_t c = ctx->state[2];
307 uint32_t d = ctx->state[3];
308 uint32_t e = ctx->state[4];
309 uint32_t f = ctx->state[5];
310 uint32_t g = ctx->state[6];
311 uint32_t h = ctx->state[7];
312 uint32_t lolen = len;
313
314 /* First increment the byte count. FIPS PUB 180-2 specifies the possible
315 length of the file up to 2^64 bits. Here we only compute the
316 number of bytes. Do a double word increment. */
317 ctx->total[0] += lolen;
318 ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
319
320#define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
321#define S0(x) (rol(x,25)^rol(x,14)^(x>>3))
322#define S1(x) (rol(x,15)^rol(x,13)^(x>>10))
323#define SS0(x) (rol(x,30)^rol(x,19)^rol(x,10))
324#define SS1(x) (rol(x,26)^rol(x,21)^rol(x,7))
325
326#define M(I) ( tm = S1(x[(I-2)&0x0f]) + x[(I-7)&0x0f] \
327 + S0(x[(I-15)&0x0f]) + x[I&0x0f] \
328 , x[I&0x0f] = tm )
329
330#define R(A,B,C,D,E,F,G,H,K,M) do { t0 = SS0(A) + F2(A,B,C); \
331 t1 = H + SS1(E) \
332 + F1(E,F,G) \
333 + K \
334 + M; \
335 D += t1; H = t0 + t1; \
336 } while(0)
337
338 while (words < endp)
339 {
340 uint32_t tm;
341 uint32_t t0, t1;
342 int t;
343 /* FIXME: see sha1.c for a better implementation. */
344 for (t = 0; t < 16; t++)
345 {
346 x[t] = SWAP (*words);
347 words++;
348 }
349
350 R( a, b, c, d, e, f, g, h, K( 0), x[ 0] );
351 R( h, a, b, c, d, e, f, g, K( 1), x[ 1] );
352 R( g, h, a, b, c, d, e, f, K( 2), x[ 2] );
353 R( f, g, h, a, b, c, d, e, K( 3), x[ 3] );
354 R( e, f, g, h, a, b, c, d, K( 4), x[ 4] );
355 R( d, e, f, g, h, a, b, c, K( 5), x[ 5] );
356 R( c, d, e, f, g, h, a, b, K( 6), x[ 6] );
357 R( b, c, d, e, f, g, h, a, K( 7), x[ 7] );
358 R( a, b, c, d, e, f, g, h, K( 8), x[ 8] );
359 R( h, a, b, c, d, e, f, g, K( 9), x[ 9] );
360 R( g, h, a, b, c, d, e, f, K(10), x[10] );
361 R( f, g, h, a, b, c, d, e, K(11), x[11] );
362 R( e, f, g, h, a, b, c, d, K(12), x[12] );
363 R( d, e, f, g, h, a, b, c, K(13), x[13] );
364 R( c, d, e, f, g, h, a, b, K(14), x[14] );
365 R( b, c, d, e, f, g, h, a, K(15), x[15] );
366 R( a, b, c, d, e, f, g, h, K(16), M(16) );
367 R( h, a, b, c, d, e, f, g, K(17), M(17) );
368 R( g, h, a, b, c, d, e, f, K(18), M(18) );
369 R( f, g, h, a, b, c, d, e, K(19), M(19) );
370 R( e, f, g, h, a, b, c, d, K(20), M(20) );
371 R( d, e, f, g, h, a, b, c, K(21), M(21) );
372 R( c, d, e, f, g, h, a, b, K(22), M(22) );
373 R( b, c, d, e, f, g, h, a, K(23), M(23) );
374 R( a, b, c, d, e, f, g, h, K(24), M(24) );
375 R( h, a, b, c, d, e, f, g, K(25), M(25) );
376 R( g, h, a, b, c, d, e, f, K(26), M(26) );
377 R( f, g, h, a, b, c, d, e, K(27), M(27) );
378 R( e, f, g, h, a, b, c, d, K(28), M(28) );
379 R( d, e, f, g, h, a, b, c, K(29), M(29) );
380 R( c, d, e, f, g, h, a, b, K(30), M(30) );
381 R( b, c, d, e, f, g, h, a, K(31), M(31) );
382 R( a, b, c, d, e, f, g, h, K(32), M(32) );
383 R( h, a, b, c, d, e, f, g, K(33), M(33) );
384 R( g, h, a, b, c, d, e, f, K(34), M(34) );
385 R( f, g, h, a, b, c, d, e, K(35), M(35) );
386 R( e, f, g, h, a, b, c, d, K(36), M(36) );
387 R( d, e, f, g, h, a, b, c, K(37), M(37) );
388 R( c, d, e, f, g, h, a, b, K(38), M(38) );
389 R( b, c, d, e, f, g, h, a, K(39), M(39) );
390 R( a, b, c, d, e, f, g, h, K(40), M(40) );
391 R( h, a, b, c, d, e, f, g, K(41), M(41) );
392 R( g, h, a, b, c, d, e, f, K(42), M(42) );
393 R( f, g, h, a, b, c, d, e, K(43), M(43) );
394 R( e, f, g, h, a, b, c, d, K(44), M(44) );
395 R( d, e, f, g, h, a, b, c, K(45), M(45) );
396 R( c, d, e, f, g, h, a, b, K(46), M(46) );
397 R( b, c, d, e, f, g, h, a, K(47), M(47) );
398 R( a, b, c, d, e, f, g, h, K(48), M(48) );
399 R( h, a, b, c, d, e, f, g, K(49), M(49) );
400 R( g, h, a, b, c, d, e, f, K(50), M(50) );
401 R( f, g, h, a, b, c, d, e, K(51), M(51) );
402 R( e, f, g, h, a, b, c, d, K(52), M(52) );
403 R( d, e, f, g, h, a, b, c, K(53), M(53) );
404 R( c, d, e, f, g, h, a, b, K(54), M(54) );
405 R( b, c, d, e, f, g, h, a, K(55), M(55) );
406 R( a, b, c, d, e, f, g, h, K(56), M(56) );
407 R( h, a, b, c, d, e, f, g, K(57), M(57) );
408 R( g, h, a, b, c, d, e, f, K(58), M(58) );
409 R( f, g, h, a, b, c, d, e, K(59), M(59) );
410 R( e, f, g, h, a, b, c, d, K(60), M(60) );
411 R( d, e, f, g, h, a, b, c, K(61), M(61) );
412 R( c, d, e, f, g, h, a, b, K(62), M(62) );
413 R( b, c, d, e, f, g, h, a, K(63), M(63) );
414
415 a = ctx->state[0] += a;
416 b = ctx->state[1] += b;
417 c = ctx->state[2] += c;
418 d = ctx->state[3] += d;
419 e = ctx->state[4] += e;
420 f = ctx->state[5] += f;
421 g = ctx->state[6] += g;
422 h = ctx->state[7] += h;
423 }
424}
425
426#endif
427
428/*
429 * Hey Emacs!
430 * Local Variables:
431 * coding: utf-8
432 * End:
433 */
diff --git a/gl/sha1.h b/gl/sha256.h
index bc3470a5..533173a5 100644
--- a/gl/sha1.h
+++ b/gl/sha256.h
@@ -1,7 +1,6 @@
1/* Declarations of functions and data types used for SHA1 sum 1/* Declarations of functions and data types used for SHA256 and SHA224 sum
2 library functions. 2 library functions.
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2008-2022 Free Software 3 Copyright (C) 2005-2006, 2008-2022 Free Software Foundation, Inc.
4 Foundation, Inc.
5 4
6 This file is free software: you can redistribute it and/or modify 5 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as 6 it under the terms of the GNU Lesser General Public License as
@@ -16,13 +15,13 @@
16 You should have received a copy of the GNU Lesser General Public License 15 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 17
19#ifndef SHA1_H 18#ifndef SHA256_H
20# define SHA1_H 1 19# define SHA256_H 1
21 20
22# include <stdio.h> 21# include <stdio.h>
23# include <stdint.h> 22# include <stdint.h>
24 23
25# if HAVE_OPENSSL_SHA1 24# if HAVE_OPENSSL_SHA256
26# ifndef OPENSSL_API_COMPAT 25# ifndef OPENSSL_API_COMPAT
27# define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */ 26# define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
28# endif 27# endif
@@ -33,72 +32,79 @@
33extern "C" { 32extern "C" {
34# endif 33# endif
35 34
36# define SHA1_DIGEST_SIZE 20 35enum { SHA224_DIGEST_SIZE = 224 / 8 };
36enum { SHA256_DIGEST_SIZE = 256 / 8 };
37 37
38# if HAVE_OPENSSL_SHA1 38# if HAVE_OPENSSL_SHA256
39# define GL_OPENSSL_NAME 1 39# define GL_OPENSSL_NAME 224
40# include "gl_openssl.h"
41# define GL_OPENSSL_NAME 256
40# include "gl_openssl.h" 42# include "gl_openssl.h"
41# else 43# else
42/* Structure to save state of computation between the single steps. */ 44/* Structure to save state of computation between the single steps. */
43struct sha1_ctx 45struct sha256_ctx
44{ 46{
45 uint32_t A; 47 uint32_t state[8];
46 uint32_t B;
47 uint32_t C;
48 uint32_t D;
49 uint32_t E;
50 48
51 uint32_t total[2]; 49 uint32_t total[2];
52 uint32_t buflen; /* ≥ 0, ≤ 128 */ 50 size_t buflen; /* ≥ 0, ≤ 128 */
53 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */ 51 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
54}; 52};
55 53
56/* Initialize structure containing state of computation. */ 54/* Initialize structure containing state of computation. */
57extern void sha1_init_ctx (struct sha1_ctx *ctx); 55extern void sha256_init_ctx (struct sha256_ctx *ctx);
56extern void sha224_init_ctx (struct sha256_ctx *ctx);
58 57
59/* Starting with the result of former calls of this function (or the 58/* Starting with the result of former calls of this function (or the
60 initialization function update the context for the next LEN bytes 59 initialization function update the context for the next LEN bytes
61 starting at BUFFER. 60 starting at BUFFER.
62 It is necessary that LEN is a multiple of 64!!! */ 61 It is necessary that LEN is a multiple of 64!!! */
63extern void sha1_process_block (const void *buffer, size_t len, 62extern void sha256_process_block (const void *buffer, size_t len,
64 struct sha1_ctx *ctx); 63 struct sha256_ctx *ctx);
65 64
66/* Starting with the result of former calls of this function (or the 65/* Starting with the result of former calls of this function (or the
67 initialization function update the context for the next LEN bytes 66 initialization function update the context for the next LEN bytes
68 starting at BUFFER. 67 starting at BUFFER.
69 It is NOT required that LEN is a multiple of 64. */ 68 It is NOT required that LEN is a multiple of 64. */
70extern void sha1_process_bytes (const void *buffer, size_t len, 69extern void sha256_process_bytes (const void *buffer, size_t len,
71 struct sha1_ctx *ctx); 70 struct sha256_ctx *ctx);
72 71
73/* Process the remaining bytes in the buffer and put result from CTX 72/* Process the remaining bytes in the buffer and put result from CTX
74 in first 20 bytes following RESBUF. The result is always in little 73 in first 32 (28) bytes following RESBUF. The result is always in little
75 endian byte order, so that a byte-wise output yields to the wanted 74 endian byte order, so that a byte-wise output yields to the wanted
76 ASCII representation of the message digest. */ 75 ASCII representation of the message digest. */
77extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *restrict resbuf); 76extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *restrict resbuf);
77extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *restrict resbuf);
78 78
79 79
80/* Put result from CTX in first 20 bytes following RESBUF. The result is 80/* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
81 always in little endian byte order, so that a byte-wise output yields 81 always in little endian byte order, so that a byte-wise output yields
82 to the wanted ASCII representation of the message digest. */ 82 to the wanted ASCII representation of the message digest. */
83extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *restrict resbuf); 83extern void *sha256_read_ctx (const struct sha256_ctx *ctx,
84 void *restrict resbuf);
85extern void *sha224_read_ctx (const struct sha256_ctx *ctx,
86 void *restrict resbuf);
84 87
85 88
86/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The 89/* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER.
87 result is always in little endian byte order, so that a byte-wise 90 The result is always in little endian byte order, so that a byte-wise
88 output yields to the wanted ASCII representation of the message 91 output yields to the wanted ASCII representation of the message
89 digest. */ 92 digest. */
90extern void *sha1_buffer (const char *buffer, size_t len, 93extern void *sha256_buffer (const char *buffer, size_t len,
91 void *restrict resblock); 94 void *restrict resblock);
95extern void *sha224_buffer (const char *buffer, size_t len,
96 void *restrict resblock);
92 97
93# endif 98# endif
94 99
95/* Compute SHA1 message digest for bytes read from STREAM. 100/* Compute SHA256 (SHA224) message digest for bytes read from STREAM.
96 STREAM is an open file stream. Regular files are handled more efficiently. 101 STREAM is an open file stream. Regular files are handled more efficiently.
97 The contents of STREAM from its current position to its end will be read. 102 The contents of STREAM from its current position to its end will be read.
98 The case that the last operation on STREAM was an 'ungetc' is not supported. 103 The case that the last operation on STREAM was an 'ungetc' is not supported.
99 The resulting message digest number will be written into the 20 bytes 104 The resulting message digest number will be written into the 32 (28) bytes
100 beginning at RESBLOCK. */ 105 beginning at RESBLOCK. */
101extern int sha1_stream (FILE *stream, void *resblock); 106extern int sha256_stream (FILE *stream, void *resblock);
107extern int sha224_stream (FILE *stream, void *resblock);
102 108
103 109
104# ifdef __cplusplus 110# ifdef __cplusplus
diff --git a/gl/stdbool.in.h b/gl/stdbool.in.h
index 2fa46724..36f26560 100644
--- a/gl/stdbool.in.h
+++ b/gl/stdbool.in.h
@@ -59,8 +59,10 @@
59/* 7.16. Boolean type and values */ 59/* 7.16. Boolean type and values */
60 60
61#ifdef __cplusplus 61#ifdef __cplusplus
62# define _Bool bool 62# if !defined _MSC_VER
63# define bool bool 63# define _Bool bool
64# define bool bool
65# endif
64#else 66#else
65# if !defined __GNUC__ 67# if !defined __GNUC__
66 /* If @HAVE__BOOL@: 68 /* If @HAVE__BOOL@:
@@ -103,8 +105,10 @@ typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
103 105
104/* The other macros must be usable in preprocessor directives. */ 106/* The other macros must be usable in preprocessor directives. */
105#ifdef __cplusplus 107#ifdef __cplusplus
106# define false false 108# if !defined _MSC_VER
107# define true true 109# define false false
110# define true true
111# endif
108#else 112#else
109# define false 0 113# define false 0
110# define true 1 114# define true 1
diff --git a/gl/stdlib.in.h b/gl/stdlib.in.h
index a86643c3..8e0a609f 100644
--- a/gl/stdlib.in.h
+++ b/gl/stdlib.in.h
@@ -226,7 +226,7 @@ _GL_FUNCDECL_SYS (aligned_alloc, void *,
226_GL_CXXALIAS_SYS (aligned_alloc, void *, (size_t alignment, size_t size)); 226_GL_CXXALIAS_SYS (aligned_alloc, void *, (size_t alignment, size_t size));
227# endif 227# endif
228# endif 228# endif
229# if @HAVE_ALIGNED_ALLOC@ 229# if (__GLIBC__ >= 2) && @HAVE_ALIGNED_ALLOC@
230_GL_CXXALIASWARN (aligned_alloc); 230_GL_CXXALIASWARN (aligned_alloc);
231# endif 231# endif
232#else 232#else
@@ -1363,7 +1363,9 @@ _GL_CXXALIAS_SYS (strtol, long,
1363 (const char *restrict string, char **restrict endptr, 1363 (const char *restrict string, char **restrict endptr,
1364 int base)); 1364 int base));
1365# endif 1365# endif
1366# if __GLIBC__ >= 2
1366_GL_CXXALIASWARN (strtol); 1367_GL_CXXALIASWARN (strtol);
1368# endif
1367#elif defined GNULIB_POSIXCHECK 1369#elif defined GNULIB_POSIXCHECK
1368# undef strtol 1370# undef strtol
1369# if HAVE_RAW_DECL_STRTOL 1371# if HAVE_RAW_DECL_STRTOL
@@ -1444,7 +1446,9 @@ _GL_CXXALIAS_SYS (strtoul, unsigned long,
1444 (const char *restrict string, char **restrict endptr, 1446 (const char *restrict string, char **restrict endptr,
1445 int base)); 1447 int base));
1446# endif 1448# endif
1449# if __GLIBC__ >= 2
1447_GL_CXXALIASWARN (strtoul); 1450_GL_CXXALIASWARN (strtoul);
1451# endif
1448#elif defined GNULIB_POSIXCHECK 1452#elif defined GNULIB_POSIXCHECK
1449# undef strtoul 1453# undef strtoul
1450# if HAVE_RAW_DECL_STRTOUL 1454# if HAVE_RAW_DECL_STRTOUL
diff --git a/gl/sys/stat.h b/gl/sys/stat.h
index 81203838..68584420 100644
--- a/gl/sys/stat.h
+++ b/gl/sys/stat.h
@@ -1104,44 +1104,6 @@ _GL_WARN_ON_USE (lchmod, "lchmod is unportable - "
1104 1104
1105 1105
1106#if 0 1106#if 0
1107# if ! 1
1108/* mingw does not support symlinks, therefore it does not have lstat. But
1109 without links, stat does just fine. */
1110# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1111# define lstat stat
1112# endif
1113_GL_CXXALIAS_RPL_1 (lstat, stat, int,
1114 (const char *restrict name, struct stat *restrict buf));
1115# elif 0
1116# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1117# undef lstat
1118# define lstat rpl_lstat
1119# endif
1120_GL_FUNCDECL_RPL (lstat, int,
1121 (const char *restrict name, struct stat *restrict buf)
1122 _GL_ARG_NONNULL ((1, 2)));
1123_GL_CXXALIAS_RPL (lstat, int,
1124 (const char *restrict name, struct stat *restrict buf));
1125# else
1126_GL_CXXALIAS_SYS (lstat, int,
1127 (const char *restrict name, struct stat *restrict buf));
1128# endif
1129# if 1
1130_GL_CXXALIASWARN (lstat);
1131# endif
1132#elif 0
1133# undef lstat
1134# define lstat lstat_used_without_requesting_gnulib_module_lstat
1135#elif defined GNULIB_POSIXCHECK
1136# undef lstat
1137# if HAVE_RAW_DECL_LSTAT
1138_GL_WARN_ON_USE (lstat, "lstat is unportable - "
1139 "use gnulib module lstat for portability");
1140# endif
1141#endif
1142
1143
1144#if 0
1145# if 0 1107# if 0
1146# if !(defined __cplusplus && defined GNULIB_NAMESPACE) 1108# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1147# undef mkdir 1109# undef mkdir
@@ -1402,6 +1364,44 @@ _GL_WARN_ON_USE (stat, "stat is unportable - "
1402#endif 1364#endif
1403 1365
1404 1366
1367#if 0
1368# if ! 1
1369/* mingw does not support symlinks, therefore it does not have lstat. But
1370 without links, stat does just fine. */
1371# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1372# define lstat stat
1373# endif
1374_GL_CXXALIAS_RPL_1 (lstat, stat, int,
1375 (const char *restrict name, struct stat *restrict buf));
1376# elif 0
1377# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1378# undef lstat
1379# define lstat rpl_lstat
1380# endif
1381_GL_FUNCDECL_RPL (lstat, int,
1382 (const char *restrict name, struct stat *restrict buf)
1383 _GL_ARG_NONNULL ((1, 2)));
1384_GL_CXXALIAS_RPL (lstat, int,
1385 (const char *restrict name, struct stat *restrict buf));
1386# else
1387_GL_CXXALIAS_SYS (lstat, int,
1388 (const char *restrict name, struct stat *restrict buf));
1389# endif
1390# if 1
1391_GL_CXXALIASWARN (lstat);
1392# endif
1393#elif 0
1394# undef lstat
1395# define lstat lstat_used_without_requesting_gnulib_module_lstat
1396#elif defined GNULIB_POSIXCHECK
1397# undef lstat
1398# if HAVE_RAW_DECL_LSTAT
1399_GL_WARN_ON_USE (lstat, "lstat is unportable - "
1400 "use gnulib module lstat for portability");
1401# endif
1402#endif
1403
1404
1405#if 1 1405#if 1
1406/* On native Windows, map 'umask' to '_umask', so that -loldnames is not 1406/* On native Windows, map 'umask' to '_umask', so that -loldnames is not
1407 required. In C++ with GNULIB_NAMESPACE, avoid differences between 1407 required. In C++ with GNULIB_NAMESPACE, avoid differences between
diff --git a/gl/sys_stat.in.h b/gl/sys_stat.in.h
index 714c3cb1..0ec320f5 100644
--- a/gl/sys_stat.in.h
+++ b/gl/sys_stat.in.h
@@ -596,44 +596,6 @@ _GL_WARN_ON_USE (lchmod, "lchmod is unportable - "
596#endif 596#endif
597 597
598 598
599#if @GNULIB_LSTAT@
600# if ! @HAVE_LSTAT@
601/* mingw does not support symlinks, therefore it does not have lstat. But
602 without links, stat does just fine. */
603# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
604# define lstat stat
605# endif
606_GL_CXXALIAS_RPL_1 (lstat, stat, int,
607 (const char *restrict name, struct stat *restrict buf));
608# elif @REPLACE_LSTAT@
609# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
610# undef lstat
611# define lstat rpl_lstat
612# endif
613_GL_FUNCDECL_RPL (lstat, int,
614 (const char *restrict name, struct stat *restrict buf)
615 _GL_ARG_NONNULL ((1, 2)));
616_GL_CXXALIAS_RPL (lstat, int,
617 (const char *restrict name, struct stat *restrict buf));
618# else
619_GL_CXXALIAS_SYS (lstat, int,
620 (const char *restrict name, struct stat *restrict buf));
621# endif
622# if @HAVE_LSTAT@
623_GL_CXXALIASWARN (lstat);
624# endif
625#elif @GNULIB_OVERRIDES_STRUCT_STAT@
626# undef lstat
627# define lstat lstat_used_without_requesting_gnulib_module_lstat
628#elif defined GNULIB_POSIXCHECK
629# undef lstat
630# if HAVE_RAW_DECL_LSTAT
631_GL_WARN_ON_USE (lstat, "lstat is unportable - "
632 "use gnulib module lstat for portability");
633# endif
634#endif
635
636
637#if @GNULIB_MKDIR@ 599#if @GNULIB_MKDIR@
638# if @REPLACE_MKDIR@ 600# if @REPLACE_MKDIR@
639# if !(defined __cplusplus && defined GNULIB_NAMESPACE) 601# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
@@ -895,6 +857,44 @@ _GL_WARN_ON_USE (stat, "stat is unportable - "
895#endif 857#endif
896 858
897 859
860#if @GNULIB_LSTAT@
861# if ! @HAVE_LSTAT@
862/* mingw does not support symlinks, therefore it does not have lstat. But
863 without links, stat does just fine. */
864# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
865# define lstat stat
866# endif
867_GL_CXXALIAS_RPL_1 (lstat, stat, int,
868 (const char *restrict name, struct stat *restrict buf));
869# elif @REPLACE_LSTAT@
870# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
871# undef lstat
872# define lstat rpl_lstat
873# endif
874_GL_FUNCDECL_RPL (lstat, int,
875 (const char *restrict name, struct stat *restrict buf)
876 _GL_ARG_NONNULL ((1, 2)));
877_GL_CXXALIAS_RPL (lstat, int,
878 (const char *restrict name, struct stat *restrict buf));
879# else
880_GL_CXXALIAS_SYS (lstat, int,
881 (const char *restrict name, struct stat *restrict buf));
882# endif
883# if @HAVE_LSTAT@
884_GL_CXXALIASWARN (lstat);
885# endif
886#elif @GNULIB_OVERRIDES_STRUCT_STAT@
887# undef lstat
888# define lstat lstat_used_without_requesting_gnulib_module_lstat
889#elif defined GNULIB_POSIXCHECK
890# undef lstat
891# if HAVE_RAW_DECL_LSTAT
892_GL_WARN_ON_USE (lstat, "lstat is unportable - "
893 "use gnulib module lstat for portability");
894# endif
895#endif
896
897
898#if @GNULIB_MDA_UMASK@ 898#if @GNULIB_MDA_UMASK@
899/* On native Windows, map 'umask' to '_umask', so that -loldnames is not 899/* On native Windows, map 'umask' to '_umask', so that -loldnames is not
900 required. In C++ with GNULIB_NAMESPACE, avoid differences between 900 required. In C++ with GNULIB_NAMESPACE, avoid differences between
diff --git a/gl/unistd.in.h b/gl/unistd.in.h
index 57df09ec..ef3e68fc 100644
--- a/gl/unistd.in.h
+++ b/gl/unistd.in.h
@@ -1143,7 +1143,9 @@ _GL_FUNCDECL_SYS (getdomainname, int, (char *name, size_t len)
1143# endif 1143# endif
1144_GL_CXXALIAS_SYS (getdomainname, int, (char *name, size_t len)); 1144_GL_CXXALIAS_SYS (getdomainname, int, (char *name, size_t len));
1145# endif 1145# endif
1146# if __GLIBC__ >= 2
1146_GL_CXXALIASWARN (getdomainname); 1147_GL_CXXALIASWARN (getdomainname);
1148# endif
1147#elif defined GNULIB_POSIXCHECK 1149#elif defined GNULIB_POSIXCHECK
1148# undef getdomainname 1150# undef getdomainname
1149# if HAVE_RAW_DECL_GETDOMAINNAME 1151# if HAVE_RAW_DECL_GETDOMAINNAME