Debian Codegraph @

lib/sha256-stream.c

raw
1 /* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-2.
3
4 Copyright (C) 2005-2006, 2008-2025 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>. */
18
19 /* Written by David Madore, considerably copypasting from
20 Scott G. Miller's sha1.c
21 */
22
23 #include <config.h>
24
25 /* Specification. */
26 #include "sha256.h"
27
28 #include <stdlib.h>
29
30 #if USE_UNLOCKED_IO
31 # include "unlocked-io.h"
32 #endif
33
34 #include "af_alg.h"
35
36 #define BLOCKSIZE 32768
37 #if BLOCKSIZE % 64 != 0
38 # error "invalid BLOCKSIZE"
39 #endif
40
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 sha256_ctx *),
48 void *(*finish_ctx) (struct sha256_ctx *, void *))
49 {
50 switch (afalg_stream (stream, alg, resblock, hashlen))
51 {
52 case 0: return 0;
53 case -EIO: return 1;
54 }
55
56 char *buffer = malloc (BLOCKSIZE + 72);
57 if (!buffer)
58 return 1;
59
60 struct sha256_ctx ctx;
61 init_ctx (&ctx);
62 size_t sum;
63
64 /* Iterate over full file contents. */
65 while (1)
66 {
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;
72
73 /* Read block. Take care for partial reads. */
74 while (1)
75 {
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;
83
84 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
85
86 sum += n;
87
88 if (sum == BLOCKSIZE)
89 break;
90
91 if (n == 0)
92 {
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))
97 {
98 free (buffer);
99 return 1;
100 }
101 goto process_partial_block;
102 }
103 }
104
105 /* Process buffer with BLOCKSIZE bytes. Note that
106 BLOCKSIZE % 64 == 0
107 */
108 sha256_process_block (buffer, BLOCKSIZE, &ctx);
109 }
110
111 process_partial_block:;
112
113 /* Process any remaining bytes. */
114 if (sum > 0)
115 sha256_process_bytes (buffer, sum, &ctx);
116
117 /* Construct result in desired memory. */
118 finish_ctx (&ctx, resblock);
119 free (buffer);
120 return 0;
121 }
122
123 int
124 sha256_stream (FILE *stream, void *resblock)
125 {
126 return shaxxx_stream (stream, "sha256", resblock, SHA256_DIGEST_SIZE,
127 sha256_init_ctx, sha256_finish_ctx);
128 }
129
130 int
131 sha224_stream (FILE *stream, void *resblock)
132 {
133 return shaxxx_stream (stream, "sha224", resblock, SHA224_DIGEST_SIZE,
134 sha224_init_ctx, sha224_finish_ctx);
135 }