summaryrefslogtreecommitdiffstats
path: root/gl/sha1.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/sha1.c')
-rw-r--r--gl/sha1.c126
1 files changed, 30 insertions, 96 deletions
diff --git a/gl/sha1.c b/gl/sha1.c
index 778389a..80f0b7a 100644
--- a/gl/sha1.c
+++ b/gl/sha1.c
@@ -1,20 +1,20 @@
1/* sha1.c - Functions to compute SHA1 message digest of files or 1/* sha1.c - Functions to compute SHA1 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-1.
3 3
4 Copyright (C) 2000-2001, 2003-2006, 2008-2013 Free Software Foundation, Inc. 4 Copyright (C) 2000-2001, 2003-2006, 2008-2023 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify it 6 This file is free software: you can redistribute it and/or modify
7 under the terms of the GNU General Public License as published by the 7 it under the terms of the GNU Lesser General Public License as
8 Free Software Foundation; either version 3, or (at your option) any 8 published by the Free Software Foundation; either version 2.1 of the
9 later version. 9 License, or (at your option) any later version.
10 10
11 This program is distributed in the hope that it will be useful, 11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details. 14 GNU Lesser General Public License for more details.
15 15
16 You should have received a copy of the GNU 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 <http://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 Scott G. Miller
20 Credits: 20 Credits:
@@ -23,28 +23,23 @@
23 23
24#include <config.h> 24#include <config.h>
25 25
26/* Specification. */
27#if HAVE_OPENSSL_SHA1
28# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
29#endif
26#include "sha1.h" 30#include "sha1.h"
27 31
28#include <stdalign.h>
29#include <stdint.h> 32#include <stdint.h>
30#include <stdlib.h>
31#include <string.h> 33#include <string.h>
32 34
33#if USE_UNLOCKED_IO 35#include <byteswap.h>
34# include "unlocked-io.h"
35#endif
36
37#ifdef WORDS_BIGENDIAN 36#ifdef WORDS_BIGENDIAN
38# define SWAP(n) (n) 37# define SWAP(n) (n)
39#else 38#else
40# define SWAP(n) \ 39# define SWAP(n) bswap_32 (n)
41 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
42#endif 40#endif
43 41
44#define BLOCKSIZE 32768 42#if ! HAVE_OPENSSL_SHA1
45#if BLOCKSIZE % 64 != 0
46# error "invalid BLOCKSIZE"
47#endif
48 43
49/* This array contains the bytes used to pad the buffer to the next 44/* This array contains the bytes used to pad the buffer to the next
50 64-byte boundary. (RFC 1321, 3.1: Step 1) */ 45 64-byte boundary. (RFC 1321, 3.1: Step 1) */
@@ -117,79 +112,6 @@ sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
117 return sha1_read_ctx (ctx, resbuf); 112 return sha1_read_ctx (ctx, resbuf);
118} 113}
119 114
120/* Compute SHA1 message digest for bytes read from STREAM. The
121 resulting message digest number will be written into the 16 bytes
122 beginning at RESBLOCK. */
123int
124sha1_stream (FILE *stream, void *resblock)
125{
126 struct sha1_ctx ctx;
127 size_t sum;
128
129 char *buffer = malloc (BLOCKSIZE + 72);
130 if (!buffer)
131 return 1;
132
133 /* Initialize the computation context. */
134 sha1_init_ctx (&ctx);
135
136 /* Iterate over full file contents. */
137 while (1)
138 {
139 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
140 computation function processes the whole buffer so that with the
141 next round of the loop another block can be read. */
142 size_t n;
143 sum = 0;
144
145 /* Read block. Take care for partial reads. */
146 while (1)
147 {
148 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
149
150 sum += n;
151
152 if (sum == BLOCKSIZE)
153 break;
154
155 if (n == 0)
156 {
157 /* Check for the error flag IFF N == 0, so that we don't
158 exit the loop after a partial read due to e.g., EAGAIN
159 or EWOULDBLOCK. */
160 if (ferror (stream))
161 {
162 free (buffer);
163 return 1;
164 }
165 goto process_partial_block;
166 }
167
168 /* We've read at least one byte, so ignore errors. But always
169 check for EOF, since feof may be true even though N > 0.
170 Otherwise, we could end up calling fread after EOF. */
171 if (feof (stream))
172 goto process_partial_block;
173 }
174
175 /* Process buffer with BLOCKSIZE bytes. Note that
176 BLOCKSIZE % 64 == 0
177 */
178 sha1_process_block (buffer, BLOCKSIZE, &ctx);
179 }
180
181 process_partial_block:;
182
183 /* Process any remaining bytes. */
184 if (sum > 0)
185 sha1_process_bytes (buffer, sum, &ctx);
186
187 /* Construct result in desired memory. */
188 sha1_finish_ctx (&ctx, resblock);
189 free (buffer);
190 return 0;
191}
192
193/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The 115/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
194 result is always in little endian byte order, so that a byte-wise 116 result is always in little endian byte order, so that a byte-wise
195 output yields to the wanted ASCII representation of the message 117 output yields to the wanted ASCII representation of the message
@@ -227,7 +149,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
227 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx); 149 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
228 150
229 ctx->buflen &= 63; 151 ctx->buflen &= 63;
230 /* The regions in the following copy operation cannot overlap. */ 152 /* The regions in the following copy operation cannot overlap,
153 because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
231 memcpy (ctx->buffer, 154 memcpy (ctx->buffer,
232 &((char *) ctx->buffer)[(left_over + add) & ~63], 155 &((char *) ctx->buffer)[(left_over + add) & ~63],
233 ctx->buflen); 156 ctx->buflen);
@@ -240,7 +163,7 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
240 /* Process available complete blocks. */ 163 /* Process available complete blocks. */
241 if (len >= 64) 164 if (len >= 64)
242 { 165 {
243#if !_STRING_ARCH_unaligned 166#if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
244# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0) 167# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
245 if (UNALIGNED_P (buffer)) 168 if (UNALIGNED_P (buffer))
246 while (len > 64) 169 while (len > 64)
@@ -269,6 +192,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
269 { 192 {
270 sha1_process_block (ctx->buffer, 64, ctx); 193 sha1_process_block (ctx->buffer, 64, ctx);
271 left_over -= 64; 194 left_over -= 64;
195 /* The regions in the following copy operation cannot overlap,
196 because left_over ≤ 64. */
272 memcpy (ctx->buffer, &ctx->buffer[16], left_over); 197 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
273 } 198 }
274 ctx->buflen = left_over; 199 ctx->buflen = left_over;
@@ -424,3 +349,12 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
424 e = ctx->E += e; 349 e = ctx->E += e;
425 } 350 }
426} 351}
352
353#endif
354
355/*
356 * Hey Emacs!
357 * Local Variables:
358 * coding: utf-8
359 * End:
360 */