Debian Codegraph @

lib/alg-sha1.c

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