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
31 # include "unlocked-io.h"
36 #define BLOCKSIZE 32768
37 #if BLOCKSIZE % 128 != 0
38 # error "invalid BLOCKSIZE"
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. */
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
))
56 char *buffer
= malloc (BLOCKSIZE
+ 72);
60 struct sha512_ctx ctx
;
64 /* Iterate over full file contents. */
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. */
73 /* Read block. Take care for partial reads. */
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 */
82 goto process_partial_block
;
84 n
= fread (buffer
+ sum
, 1, BLOCKSIZE
- sum
, stream
);
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
101 goto process_partial_block
;
105 /* Process buffer with BLOCKSIZE bytes. Note that
108 sha512_process_block (buffer
, BLOCKSIZE
, &ctx
);
111 process_partial_block
:;
113 /* Process any remaining bytes. */
115 sha512_process_bytes (buffer
, sum
, &ctx
);
117 /* Construct result in desired memory. */
118 finish_ctx (&ctx
, resblock
);
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
);