gettext: Sync with gettext 0.23.
[gnulib.git] / lib / sha512-stream.c
blob6362e55a6a6685f67108be89f657c18302bacd23
1 /* sha512.c - Functions to compute SHA512 and SHA384 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-2.
4 Copyright (C) 2005-2006, 2008-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
19 /* Written by David Madore, considerably copypasting from
20 Scott G. Miller's sha1.c
23 #include <config.h>
25 /* Specification. */
26 #include "sha512.h"
28 #include <stdlib.h>
30 #if USE_UNLOCKED_IO
31 # include "unlocked-io.h"
32 #endif
34 #include "af_alg.h"
36 #define BLOCKSIZE 32768
37 #if BLOCKSIZE % 128 != 0
38 # error "invalid BLOCKSIZE"
39 #endif
41 /* Compute message digest for bytes read from STREAM using algorithm ALG.
42 Write the message digest into RESBLOCK, which contains HASHLEN bytes.
43 The initial and finishing operations are INIT_CTX and FINISH_CTX.
44 Return zero if and only if successful. */
45 static int
46 shaxxx_stream (FILE *stream, char const *alg, void *resblock,
47 ssize_t hashlen, void (*init_ctx) (struct sha512_ctx *),
48 void *(*finish_ctx) (struct sha512_ctx *, void *))
50 switch (afalg_stream (stream, alg, resblock, hashlen))
52 case 0: return 0;
53 case -EIO: return 1;
56 char *buffer = malloc (BLOCKSIZE + 72);
57 if (!buffer)
58 return 1;
60 struct sha512_ctx ctx;
61 init_ctx (&ctx);
62 size_t sum;
64 /* Iterate over full file contents. */
65 while (1)
67 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
68 computation function processes the whole buffer so that with the
69 next round of the loop another block can be read. */
70 size_t n;
71 sum = 0;
73 /* Read block. Take care for partial reads. */
74 while (1)
76 /* Either process a partial fread() from this loop,
77 or the fread() in afalg_stream may have gotten EOF.
78 We need to avoid a subsequent fread() as EOF may
79 not be sticky. For details of such systems, see:
80 https://sourceware.org/bugzilla/show_bug.cgi?id=1190 */
81 if (feof (stream))
82 goto process_partial_block;
84 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
86 sum += n;
88 if (sum == BLOCKSIZE)
89 break;
91 if (n == 0)
93 /* Check for the error flag IFF N == 0, so that we don't
94 exit the loop after a partial read due to e.g., EAGAIN
95 or EWOULDBLOCK. */
96 if (ferror (stream))
98 free (buffer);
99 return 1;
101 goto process_partial_block;
105 /* Process buffer with BLOCKSIZE bytes. Note that
106 BLOCKSIZE % 128 == 0
108 sha512_process_block (buffer, BLOCKSIZE, &ctx);
111 process_partial_block:;
113 /* Process any remaining bytes. */
114 if (sum > 0)
115 sha512_process_bytes (buffer, sum, &ctx);
117 /* Construct result in desired memory. */
118 finish_ctx (&ctx, resblock);
119 free (buffer);
120 return 0;
124 sha512_stream (FILE *stream, void *resblock)
126 return shaxxx_stream (stream, "sha512", resblock, SHA512_DIGEST_SIZE,
127 sha512_init_ctx, sha512_finish_ctx);
131 sha384_stream (FILE *stream, void *resblock)
133 return shaxxx_stream (stream, "sha384", resblock, SHA384_DIGEST_SIZE,
134 sha384_init_ctx, sha384_finish_ctx);
138 * Hey Emacs!
139 * Local Variables:
140 * coding: utf-8
141 * End: