Debian Codegraph @

src/fundamental/sha1.c

raw
1 /* SPDX-License-Identifier: LicenseRef-alg-sha1-public-domain */
2
3 /*
4 SHA-1 in C
5 By Steve Reid <[email protected]>
6 100% Public Domain
7
8 -----------------
9 Modified 7/98
10 By James H. Brown <[email protected]>
11 Still 100% Public Domain
12
13 Corrected a problem which generated improper hash values on 16 bit machines
14 Routine SHA1Update changed from
15 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len)
16 to
17 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned long len)
18
19 The 'len' parameter was declared an int which works fine on 32 bit machines.
20 However, on 16 bit machines an int is too small for the shifts being done
21 against
22 it. This caused the hash function to generate incorrect values if len was
23 greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
24
25 Since the file IO in main() reads 16K at a time, any file 8K or larger would
26 be guaranteed to generate the wrong hash (e.g. Test Vector #3,
27 a million "a"s).
28
29 I also changed the declaration of variables i & j in SHA1Update to
30 unsigned long from unsigned int for the same reason.
31
32 These changes should make no difference to any 32 bit implementations since
33 an int and a long are the same size in those environments.
34
35 --
36 I also corrected a few compiler warnings generated by Borland C.
37 1. Added #include <process.h> for exit() prototype
38 2. Removed unused variable 'j' in SHA1Final
39 3. Changed exit(0) to return(0) at end of main.
40
41 ALL changes I made can be located by searching for comments containing 'JHB'
42 -----------------
43 Modified 8/98
44 By Steve Reid <[email protected]>
45 Still 100% public domain
46
47 1- Removed #include <process.h> and used return() instead of exit()
48 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
49 3- Changed email address from [email protected] to [email protected]
50
51 -----------------
52 Modified 4/01
53 By Saul Kravitz <[email protected]>
54 Still 100% PD
55 Modified to run on Compaq Alpha hardware.
56
57 -----------------
58 Modified 07/2002
59 By Ralph Giles <[email protected]>
60 Still 100% public domain
61 modified for use with stdint types, autoconf
62 code cleanup, removed attribution comments
63 switched SHA1Final() argument order for consistency
64 use SHA1_ prefix for public api
65 move public api to sha1.h
66
67 -----------------
68 Modified 10/2017
69 By Björn Esser <[email protected]>
70 Still 100% public domain
71 modified for use with libxcrypt
72
73 Modified 09/2024
74 By anonymix007 <[email protected]>
75 Still 100% public domain
76 modified for use with systemd
77 */
78
79 #if SD_BOOT
80 # include "efi-string.h" /* IWYU pragma: keep */
81 #else
82 # include <string.h>
83 #endif
84
85 #include "memory-util.h"
86 #include "sha1.h"
87
88 #define SHA1_DIGEST_SIZE 20
89
90 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
91
92 /* blk0() and blk() perform the initial expand. */
93 /* I got the idea of expanding during the round function from SSLeay */
94 /* FIXME: can we do this in an endian-proof way? */
95 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
96 # define blk0(i) block.l[i]
97 #else
98 # define blk0(i) (block.l[i] = (rol(block.l[i], 24) & 0xFF00FF00) | (rol(block.l[i], 8) & 0x00FF00FF))
99 #endif
100 #define blk(i) \
101 (block.l[i & 15] = rol( \
102 block.l[(i + 13) & 15] ^ block.l[(i + 8) & 15] ^ block.l[(i + 2) & 15] ^ \
103 block.l[i & 15], \
104 1))
105
106 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
107 #define R0(v, w, x, y, z, i) \
108 z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
109 w = rol(w, 30);
110 #define R1(v, w, x, y, z, i) \
111 z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
112 w = rol(w, 30);
113 #define R2(v, w, x, y, z, i) \
114 z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
115 w = rol(w, 30);
116 #define R3(v, w, x, y, z, i) \
117 z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
118 w = rol(w, 30);
119 #define R4(v, w, x, y, z, i) \
120 z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
121 w = rol(w, 30);
122
123 /* Hash a single 512-bit block. This is the core of the algorithm. */
124 static void sha1_do_transform(uint32_t state[5], const uint8_t buffer[64]) {
125 uint32_t a, b, c, d, e;
126 typedef union {
127 uint8_t c[64];
128 uint32_t l[16];
129 } CHAR64LONG16;
130 CHAR64LONG16 block;
131
132 memcpy(&block, buffer, 64);
133
134 /* Copy context->state[] to working vars */
135 a = state[0];
136 b = state[1];
137 c = state[2];
138 d = state[3];
139 e = state[4];
140
141 /* 4 rounds of 20 operations each. Loop unrolled. */
142 R0(a, b, c, d, e, 0);
143 R0(e, a, b, c, d, 1);
144 R0(d, e, a, b, c, 2);
145 R0(c, d, e, a, b, 3);
146 R0(b, c, d, e, a, 4);
147 R0(a, b, c, d, e, 5);
148 R0(e, a, b, c, d, 6);
149 R0(d, e, a, b, c, 7);
150 R0(c, d, e, a, b, 8);
151 R0(b, c, d, e, a, 9);
152 R0(a, b, c, d, e, 10);
153 R0(e, a, b, c, d, 11);
154 R0(d, e, a, b, c, 12);
155 R0(c, d, e, a, b, 13);
156 R0(b, c, d, e, a, 14);
157 R0(a, b, c, d, e, 15);
158 R1(e, a, b, c, d, 16);
159 R1(d, e, a, b, c, 17);
160 R1(c, d, e, a, b, 18);
161 R1(b, c, d, e, a, 19);
162 R2(a, b, c, d, e, 20);
163 R2(e, a, b, c, d, 21);
164 R2(d, e, a, b, c, 22);
165 R2(c, d, e, a, b, 23);
166 R2(b, c, d, e, a, 24);
167 R2(a, b, c, d, e, 25);
168 R2(e, a, b, c, d, 26);
169 R2(d, e, a, b, c, 27);
170 R2(c, d, e, a, b, 28);
171 R2(b, c, d, e, a, 29);
172 R2(a, b, c, d, e, 30);
173 R2(e, a, b, c, d, 31);
174 R2(d, e, a, b, c, 32);
175 R2(c, d, e, a, b, 33);
176 R2(b, c, d, e, a, 34);
177 R2(a, b, c, d, e, 35);
178 R2(e, a, b, c, d, 36);
179 R2(d, e, a, b, c, 37);
180 R2(c, d, e, a, b, 38);
181 R2(b, c, d, e, a, 39);
182 R3(a, b, c, d, e, 40);
183 R3(e, a, b, c, d, 41);
184 R3(d, e, a, b, c, 42);
185 R3(c, d, e, a, b, 43);
186 R3(b, c, d, e, a, 44);
187 R3(a, b, c, d, e, 45);
188 R3(e, a, b, c, d, 46);
189 R3(d, e, a, b, c, 47);
190 R3(c, d, e, a, b, 48);
191 R3(b, c, d, e, a, 49);
192 R3(a, b, c, d, e, 50);
193 R3(e, a, b, c, d, 51);
194 R3(d, e, a, b, c, 52);
195 R3(c, d, e, a, b, 53);
196 R3(b, c, d, e, a, 54);
197 R3(a, b, c, d, e, 55);
198 R3(e, a, b, c, d, 56);
199 R3(d, e, a, b, c, 57);
200 R3(c, d, e, a, b, 58);
201 R3(b, c, d, e, a, 59);
202 R4(a, b, c, d, e, 60);
203 R4(e, a, b, c, d, 61);
204 R4(d, e, a, b, c, 62);
205 R4(c, d, e, a, b, 63);
206 R4(b, c, d, e, a, 64);
207 R4(a, b, c, d, e, 65);
208 R4(e, a, b, c, d, 66);
209 R4(d, e, a, b, c, 67);
210 R4(c, d, e, a, b, 68);
211 R4(b, c, d, e, a, 69);
212 R4(a, b, c, d, e, 70);
213 R4(e, a, b, c, d, 71);
214 R4(d, e, a, b, c, 72);
215 R4(c, d, e, a, b, 73);
216 R4(b, c, d, e, a, 74);
217 R4(a, b, c, d, e, 75);
218 R4(e, a, b, c, d, 76);
219 R4(d, e, a, b, c, 77);
220 R4(c, d, e, a, b, 78);
221 R4(b, c, d, e, a, 79);
222
223 /* Add the working vars back into context.state[] */
224 state[0] += a;
225 state[1] += b;
226 state[2] += c;
227 state[3] += d;
228 state[4] += e;
229
230 /* Wipe variables */
231 a = b = c = d = e = 0;
232 }
233
234 /* SHA1Init - Initialize new context */
235 void sha1_init_ctx(struct sha1_ctx *ctx) {
236 /* SHA1 initialization constants */
237 ctx->state[0] = 0x67452301;
238 ctx->state[1] = 0xEFCDAB89;
239 ctx->state[2] = 0x98BADCFE;
240 ctx->state[3] = 0x10325476;
241 ctx->state[4] = 0xC3D2E1F0;
242 ctx->count[0] = ctx->count[1] = 0;
243 }
244
245 /* Run your data through this. */
246 void sha1_process_bytes(const void *buffer, size_t size, struct sha1_ctx *ctx) {
247 size_t i, j;
248
249 j = (ctx->count[0] >> 3) & 63;
250 if ((ctx->count[0] += (uint32_t) size << 3) < ((uint32_t) size << 3))
251 ctx->count[1]++;
252 ctx->count[1] += (uint32_t) (size >> 29);
253 if ((j + size) > 63) {
254 memcpy(&ctx->buffer[j], buffer, (i = 64 - j));
255 sha1_do_transform(ctx->state, ctx->buffer);
256 DISABLE_WARNING_STRINGOP_OVERREAD;
257 for (; i + 63 < size; i += 64)
258 sha1_do_transform(ctx->state, (const uint8_t *) buffer + i);
259 REENABLE_WARNING;
260 j = 0;
261 } else
262 i = 0;
263 memcpy(&ctx->buffer[j], (const uint8_t *) buffer + i, size - i);
264 }
265
266 /* Add padding and return the message digest. */
267 void *sha1_finish_ctx(struct sha1_ctx *ctx, uint8_t result[static SHA1_DIGEST_SIZE]) {
268 uint32_t i;
269 uint8_t finalcount[8];
270
271 for (i = 0; i < 8; i++) {
272 finalcount[i] = (unsigned char) ((ctx->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) &
273 255); /* Endian independent */
274 }
275 sha1_process_bytes((const uint8_t *) "\200", 1, ctx);
276 while ((ctx->count[0] & 504) != 448)
277 sha1_process_bytes((const uint8_t *) "\0", 1, ctx);
278 sha1_process_bytes(finalcount, 8, ctx); /* Should cause a sha1_do_transform() */
279 for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
280 result[i] = (uint8_t) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
281 }
282
283 /* Wipe variables */
284 i = 0;
285 memzero(ctx, sizeof(struct sha1_ctx));
286 memzero(finalcount, 8); /* SWR */
287
288 return result;
289 }