aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sha512.c
diff options
context:
space:
mode:
authorPaul Eggert2018-05-21 11:42:18 -0700
committerPaul Eggert2018-05-21 11:43:07 -0700
commit79f15092b9d16631840cd42db034b787fae762ac (patch)
tree86f3e4917944af36516547d4d19d5dda758684f1 /lib/sha512.c
parentf21db9e1206f830ee5e991a26f9e30056f68efb8 (diff)
downloademacs-79f15092b9d16631840cd42db034b787fae762ac.tar.gz
emacs-79f15092b9d16631840cd42db034b787fae762ac.zip
Update from Gnulib
This incorporates: 2018-05-21 crypto: omit stream ops Emacs doesn’t need 2018-05-13 truncate: Fix compilation error on Android 2018-05-13 imaxdiv: Fix compilation error on Android 2018-05-13 Support selective inclusion of recent mingw.org headers 2018-05-13 Add cross-compilation guesses for Linux systems sans glibc 2018-05-13 stdioext: Fix compilation errors with newer Android headers 2018-05-07 af_alg: Pacify --enable-gcc-warnings 2018-05-06 af_alg: Fix bug with streams that are not at position 0 2018-05-06 Followup to 'af_alg: New module' 2018-05-05 crypto/{md5,sha1,sha256,sha512}: simplify 2018-05-05 af_alg: New module 2018-05-05 af_alg: Improve function signature 2018-04-28 md5sum: Use AF_ALG when available 2018-04-28 sha512sum: Use AF_ALG when available 2018-04-28 sha256sum: Use AF_ALG when available 2018-04-28 sha1sum: Use AF_ALG when available 2018-05-05 all: Replace more http URLs by https URLs 2018-05-03 maint: port more modules to GCC 8 2018-05-03 Simplify code; drop support for Borland C++ on Windows * admin/merge-gnulib (GNULIB_MODULES): Use crypto/md5-buffer rather than crypto/md5, since Emacs doesn’t use the stream operations that in recent Gnulib pull in other stuff Emacs doesn’t need. Similarly for crypto/sha1-buffer, crypto/sha256-buffer, crypto/sha512-buffer. * build-aux/config.guess, build-aux/config.sub, lib/dosname.h: * lib/dup2.c, lib/errno.in.h, lib/euidaccess.c, lib/fcntl.c: * lib/fcntl.in.h, lib/fpending.c, lib/fsync.c, lib/getdtablesize.c: * lib/getopt.c, lib/gettimeofday.c, lib/inttypes.in.h, lib/md5.c: * lib/md5.h, lib/open.c, lib/pipe2.c, lib/putenv.c, lib/sha1.c: * lib/sha1.h, lib/sha256.c, lib/sha256.h, lib/sha512.c: * lib/sha512.h, lib/stat-time.h, lib/stdio-impl.h, lib/stdio.in.h: * lib/stdlib.in.h, lib/sys_stat.in.h, lib/sys_types.in.h: * lib/timespec.h, lib/unistd.in.h, lib/utimens.c, m4/c-strtod.m4: * m4/gnulib-common.m4, m4/inttypes.m4, m4/lstat.m4, m4/nocrash.m4: * m4/pselect.m4, m4/readlink.m4, m4/stdio_h.m4, m4/symlink.m4: * m4/unistd_h.m4, m4/utimens.m4: Copy from Gnulib. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
Diffstat (limited to 'lib/sha512.c')
-rw-r--r--lib/sha512.c107
1 files changed, 31 insertions, 76 deletions
diff --git a/lib/sha512.c b/lib/sha512.c
index 8a6dd4e83ac..e175e705f52 100644
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -177,21 +177,32 @@ sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf)
177} 177}
178#endif 178#endif
179 179
180/* Compute SHA512 message digest for bytes read from STREAM. The 180#ifdef GL_COMPILE_CRYPTO_STREAM
181 resulting message digest number will be written into the 64 bytes 181
182 beginning at RESBLOCK. */ 182#include "af_alg.h"
183int 183
184sha512_stream (FILE *stream, void *resblock) 184/* Compute message digest for bytes read from STREAM using algorithm ALG.
185 Write the message digest into RESBLOCK, which contains HASHLEN bytes.
186 The initial and finishing operations are INIT_CTX and FINISH_CTX.
187 Return zero if and only if successful. */
188static int
189shaxxx_stream (FILE *stream, char const *alg, void *resblock,
190 ssize_t hashlen, void (*init_ctx) (struct sha512_ctx *),
191 void *(*finish_ctx) (struct sha512_ctx *, void *))
185{ 192{
186 struct sha512_ctx ctx; 193 switch (afalg_stream (stream, alg, resblock, hashlen))
187 size_t sum; 194 {
195 case 0: return 0;
196 case -EIO: return 1;
197 }
188 198
189 char *buffer = malloc (BLOCKSIZE + 72); 199 char *buffer = malloc (BLOCKSIZE + 72);
190 if (!buffer) 200 if (!buffer)
191 return 1; 201 return 1;
192 202
193 /* Initialize the computation context. */ 203 struct sha512_ctx ctx;
194 sha512_init_ctx (&ctx); 204 init_ctx (&ctx);
205 size_t sum;
195 206
196 /* Iterate over full file contents. */ 207 /* Iterate over full file contents. */
197 while (1) 208 while (1)
@@ -245,81 +256,25 @@ sha512_stream (FILE *stream, void *resblock)
245 sha512_process_bytes (buffer, sum, &ctx); 256 sha512_process_bytes (buffer, sum, &ctx);
246 257
247 /* Construct result in desired memory. */ 258 /* Construct result in desired memory. */
248 sha512_finish_ctx (&ctx, resblock); 259 finish_ctx (&ctx, resblock);
249 free (buffer); 260 free (buffer);
250 return 0; 261 return 0;
251} 262}
252 263
253/* FIXME: Avoid code duplication */
254int 264int
255sha384_stream (FILE *stream, void *resblock) 265sha512_stream (FILE *stream, void *resblock)
256{ 266{
257 struct sha512_ctx ctx; 267 return shaxxx_stream (stream, "sha512", resblock, SHA512_DIGEST_SIZE,
258 size_t sum; 268 sha512_init_ctx, sha512_finish_ctx);
259 269}
260 char *buffer = malloc (BLOCKSIZE + 72);
261 if (!buffer)
262 return 1;
263
264 /* Initialize the computation context. */
265 sha384_init_ctx (&ctx);
266
267 /* Iterate over full file contents. */
268 while (1)
269 {
270 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
271 computation function processes the whole buffer so that with the
272 next round of the loop another block can be read. */
273 size_t n;
274 sum = 0;
275
276 /* Read block. Take care for partial reads. */
277 while (1)
278 {
279 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
280
281 sum += n;
282
283 if (sum == BLOCKSIZE)
284 break;
285
286 if (n == 0)
287 {
288 /* Check for the error flag IFF N == 0, so that we don't
289 exit the loop after a partial read due to e.g., EAGAIN
290 or EWOULDBLOCK. */
291 if (ferror (stream))
292 {
293 free (buffer);
294 return 1;
295 }
296 goto process_partial_block;
297 }
298
299 /* We've read at least one byte, so ignore errors. But always
300 check for EOF, since feof may be true even though N > 0.
301 Otherwise, we could end up calling fread after EOF. */
302 if (feof (stream))
303 goto process_partial_block;
304 }
305
306 /* Process buffer with BLOCKSIZE bytes. Note that
307 BLOCKSIZE % 128 == 0
308 */
309 sha512_process_block (buffer, BLOCKSIZE, &ctx);
310 }
311
312 process_partial_block:;
313
314 /* Process any remaining bytes. */
315 if (sum > 0)
316 sha512_process_bytes (buffer, sum, &ctx);
317 270
318 /* Construct result in desired memory. */ 271int
319 sha384_finish_ctx (&ctx, resblock); 272sha384_stream (FILE *stream, void *resblock)
320 free (buffer); 273{
321 return 0; 274 return shaxxx_stream (stream, "sha384", resblock, SHA384_DIGEST_SIZE,
275 sha384_init_ctx, sha384_finish_ctx);
322} 276}
277#endif
323 278
324#if ! HAVE_OPENSSL_SHA512 279#if ! HAVE_OPENSSL_SHA512
325/* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The 280/* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The