diff options
Diffstat (limited to 'gl/sha1.c')
-rw-r--r-- | gl/sha1.c | 125 |
1 files changed, 30 insertions, 95 deletions
@@ -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-2021 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,24 @@ | |||
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> | 32 | #include <stdalign.h> |
29 | #include <stdint.h> | 33 | #include <stdint.h> |
30 | #include <stdlib.h> | ||
31 | #include <string.h> | 34 | #include <string.h> |
32 | 35 | ||
33 | #if USE_UNLOCKED_IO | 36 | #include <byteswap.h> |
34 | # include "unlocked-io.h" | ||
35 | #endif | ||
36 | |||
37 | #ifdef WORDS_BIGENDIAN | 37 | #ifdef WORDS_BIGENDIAN |
38 | # define SWAP(n) (n) | 38 | # define SWAP(n) (n) |
39 | #else | 39 | #else |
40 | # define SWAP(n) \ | 40 | # define SWAP(n) bswap_32 (n) |
41 | (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) | ||
42 | #endif | 41 | #endif |
43 | 42 | ||
44 | #define BLOCKSIZE 32768 | 43 | #if ! HAVE_OPENSSL_SHA1 |
45 | #if BLOCKSIZE % 64 != 0 | ||
46 | # error "invalid BLOCKSIZE" | ||
47 | #endif | ||
48 | 44 | ||
49 | /* This array contains the bytes used to pad the buffer to the next | 45 | /* This array contains the bytes used to pad the buffer to the next |
50 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ | 46 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ |
@@ -117,79 +113,6 @@ sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf) | |||
117 | return sha1_read_ctx (ctx, resbuf); | 113 | return sha1_read_ctx (ctx, resbuf); |
118 | } | 114 | } |
119 | 115 | ||
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. */ | ||
123 | int | ||
124 | sha1_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 | 116 | /* 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 | 117 | result is always in little endian byte order, so that a byte-wise |
195 | output yields to the wanted ASCII representation of the message | 118 | output yields to the wanted ASCII representation of the message |
@@ -227,7 +150,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) | |||
227 | sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx); | 150 | sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx); |
228 | 151 | ||
229 | ctx->buflen &= 63; | 152 | ctx->buflen &= 63; |
230 | /* The regions in the following copy operation cannot overlap. */ | 153 | /* The regions in the following copy operation cannot overlap, |
154 | because ctx->buflen < 64 ≤ (left_over + add) & ~63. */ | ||
231 | memcpy (ctx->buffer, | 155 | memcpy (ctx->buffer, |
232 | &((char *) ctx->buffer)[(left_over + add) & ~63], | 156 | &((char *) ctx->buffer)[(left_over + add) & ~63], |
233 | ctx->buflen); | 157 | ctx->buflen); |
@@ -240,7 +164,7 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) | |||
240 | /* Process available complete blocks. */ | 164 | /* Process available complete blocks. */ |
241 | if (len >= 64) | 165 | if (len >= 64) |
242 | { | 166 | { |
243 | #if !_STRING_ARCH_unaligned | 167 | #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned) |
244 | # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0) | 168 | # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0) |
245 | if (UNALIGNED_P (buffer)) | 169 | if (UNALIGNED_P (buffer)) |
246 | while (len > 64) | 170 | while (len > 64) |
@@ -269,6 +193,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) | |||
269 | { | 193 | { |
270 | sha1_process_block (ctx->buffer, 64, ctx); | 194 | sha1_process_block (ctx->buffer, 64, ctx); |
271 | left_over -= 64; | 195 | left_over -= 64; |
196 | /* The regions in the following copy operation cannot overlap, | ||
197 | because left_over ≤ 64. */ | ||
272 | memcpy (ctx->buffer, &ctx->buffer[16], left_over); | 198 | memcpy (ctx->buffer, &ctx->buffer[16], left_over); |
273 | } | 199 | } |
274 | ctx->buflen = left_over; | 200 | ctx->buflen = left_over; |
@@ -424,3 +350,12 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) | |||
424 | e = ctx->E += e; | 350 | e = ctx->E += e; |
425 | } | 351 | } |
426 | } | 352 | } |
353 | |||
354 | #endif | ||
355 | |||
356 | /* | ||
357 | * Hey Emacs! | ||
358 | * Local Variables: | ||
359 | * coding: utf-8 | ||
360 | * End: | ||
361 | */ | ||