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"cxx . . $ `<file>/lib/crypt-port.h`/
79 #include "alg-sha1.h"cxx . . $ `<file>/lib/alg-sha1.h`/
80
81 #if INCLUDE_sha1cryptcxx . . $ `crypt-hashes.h:17:9`!
82
83 #define SHA1_DIGEST_SIZEcxx . . $ `lib/alg-sha1.c:83:9`! 20
84
85 #define rolcxx . . $ `lib/alg-sha1.c:85:9`!(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_BIGENDIANcxx . . $ `lib/crypt-port.h:106:10`!
91 #define blk0(i) block.l[i]
92 #else
93 #define blk0cxx . . $ `lib/alg-sha1.c:93:9`!(i) (block.l[i] = (rolcxx . . $ `lib/alg-sha1.c:85:9`!(block.l[i],24)&0xFF00FF00) \
94 |(rolcxx . . $ `lib/alg-sha1.c:85:9`!(block.l[i],8)&0x00FF00FF))
95 #endif
96 #define blkcxx . . $ `lib/alg-sha1.c:96:9`!(i) (block.l[i&15] = rolcxx . . $ `lib/alg-sha1.c:85:9`!(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 R0cxx . . $ `lib/alg-sha1.c:100:9`!(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0cxx . . $ `lib/alg-sha1.c:93:9`!(i)+0x5A827999+rolcxx . . $ `lib/alg-sha1.c:85:9`!(v,5);w=rolcxx . . $ `lib/alg-sha1.c:85:9`!(w,30);
101 #define R1cxx . . $ `lib/alg-sha1.c:101:9`!(v,w,x,y,z,i) z+=((w&(x^y))^y)+blkcxx . . $ `lib/alg-sha1.c:96:9`!(i)+0x5A827999+rolcxx . . $ `lib/alg-sha1.c:85:9`!(v,5);w=rolcxx . . $ `lib/alg-sha1.c:85:9`!(w,30);
102 #define R2cxx . . $ `lib/alg-sha1.c:102:9`!(v,w,x,y,z,i) z+=(w^x^y)+blkcxx . . $ `lib/alg-sha1.c:96:9`!(i)+0x6ED9EBA1+rolcxx . . $ `lib/alg-sha1.c:85:9`!(v,5);w=rolcxx . . $ `lib/alg-sha1.c:85:9`!(w,30);
103 #define R3cxx . . $ `lib/alg-sha1.c:103:9`!(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blkcxx . . $ `lib/alg-sha1.c:96:9`!(i)+0x8F1BBCDC+rolcxx . . $ `lib/alg-sha1.c:85:9`!(v,5);w=rolcxx . . $ `lib/alg-sha1.c:85:9`!(w,30);
104 #define R4cxx . . $ `lib/alg-sha1.c:104:9`!(v,w,x,y,z,i) z+=(w^x^y)+blkcxx . . $ `lib/alg-sha1.c:96:9`!(i)+0xCA62C1D6+rolcxx . . $ `lib/alg-sha1.c:85:9`!(v,5);w=rolcxx . . $ `lib/alg-sha1.c:85:9`!(w,30);
105
106
107 /* Hash a single 512-bit block. This is the core of the algorithm. */
108 static void
109 sha1_do_transformcxx . . $ sha1_do_transform(7cc3180c6a470cbb). (uint32_tcxx . . $ uint32_t# state[5], const uint8_tcxx . . $ uint8_t# buffer[64])
110 {
111 uint32_tcxx . . $ uint32_t# a, b, c, d, e;
112 typedef unioncxx . . $ sha1_do_transform(7cc3180c6a470cbb).$anonymous_type_0#
113 {
114 uint8_tcxx . . $ uint8_t# ccxx . . $ sha1_do_transform(7cc3180c6a470cbb).$anonymous_type_0#c.[64];
115 uint32_tcxx . . $ uint32_t# lcxx . . $ sha1_do_transform(7cc3180c6a470cbb).$anonymous_type_0#l.[16];
116 } CHAR64LONG16cxx . . $ sha1_do_transform(7cc3180c6a470cbb).CHAR64LONG16#;
117 CHAR64LONG16cxx . . $ sha1_do_transform(7cc3180c6a470cbb).CHAR64LONG16# block;
118
119 memcpycxx . . $ memcpy(cc817cb175f282d). (&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 R0cxx . . $ `lib/alg-sha1.c:100:9`!(a,b,c,d,e, 0);
130 R0cxx . . $ `lib/alg-sha1.c:100:9`!(e,a,b,c,d, 1);
131 R0cxx . . $ `lib/alg-sha1.c:100:9`!(d,e,a,b,c, 2);
132 R0cxx . . $ `lib/alg-sha1.c:100:9`!(c,d,e,a,b, 3);
133 R0cxx . . $ `lib/alg-sha1.c:100:9`!(b,c,d,e,a, 4);
134 R0cxx . . $ `lib/alg-sha1.c:100:9`!(a,b,c,d,e, 5);
135 R0cxx . . $ `lib/alg-sha1.c:100:9`!(e,a,b,c,d, 6);
136 R0cxx . . $ `lib/alg-sha1.c:100:9`!(d,e,a,b,c, 7);
137 R0cxx . . $ `lib/alg-sha1.c:100:9`!(c,d,e,a,b, 8);
138 R0cxx . . $ `lib/alg-sha1.c:100:9`!(b,c,d,e,a, 9);
139 R0cxx . . $ `lib/alg-sha1.c:100:9`!(a,b,c,d,e,10);
140 R0cxx . . $ `lib/alg-sha1.c:100:9`!(e,a,b,c,d,11);
141 R0cxx . . $ `lib/alg-sha1.c:100:9`!(d,e,a,b,c,12);
142 R0cxx . . $ `lib/alg-sha1.c:100:9`!(c,d,e,a,b,13);
143 R0cxx . . $ `lib/alg-sha1.c:100:9`!(b,c,d,e,a,14);
144 R0cxx . . $ `lib/alg-sha1.c:100:9`!(a,b,c,d,e,15);
145 R1cxx . . $ `lib/alg-sha1.c:101:9`!(e,a,b,c,d,16);
146 R1cxx . . $ `lib/alg-sha1.c:101:9`!(d,e,a,b,c,17);
147 R1cxx . . $ `lib/alg-sha1.c:101:9`!(c,d,e,a,b,18);
148 R1cxx . . $ `lib/alg-sha1.c:101:9`!(b,c,d,e,a,19);
149 R2cxx . . $ `lib/alg-sha1.c:102:9`!(a,b,c,d,e,20);
150 R2cxx . . $ `lib/alg-sha1.c:102:9`!(e,a,b,c,d,21);
151 R2cxx . . $ `lib/alg-sha1.c:102:9`!(d,e,a,b,c,22);
152 R2cxx . . $ `lib/alg-sha1.c:102:9`!(c,d,e,a,b,23);
153 R2cxx . . $ `lib/alg-sha1.c:102:9`!(b,c,d,e,a,24);
154 R2cxx . . $ `lib/alg-sha1.c:102:9`!(a,b,c,d,e,25);
155 R2cxx . . $ `lib/alg-sha1.c:102:9`!(e,a,b,c,d,26);
156 R2cxx . . $ `lib/alg-sha1.c:102:9`!(d,e,a,b,c,27);
157 R2cxx . . $ `lib/alg-sha1.c:102:9`!(c,d,e,a,b,28);
158 R2cxx . . $ `lib/alg-sha1.c:102:9`!(b,c,d,e,a,29);
159 R2cxx . . $ `lib/alg-sha1.c:102:9`!(a,b,c,d,e,30);
160 R2cxx . . $ `lib/alg-sha1.c:102:9`!(e,a,b,c,d,31);
161 R2cxx . . $ `lib/alg-sha1.c:102:9`!(d,e,a,b,c,32);
162 R2cxx . . $ `lib/alg-sha1.c:102:9`!(c,d,e,a,b,33);
163 R2cxx . . $ `lib/alg-sha1.c:102:9`!(b,c,d,e,a,34);
164 R2cxx . . $ `lib/alg-sha1.c:102:9`!(a,b,c,d,e,35);
165 R2cxx . . $ `lib/alg-sha1.c:102:9`!(e,a,b,c,d,36);
166 R2cxx . . $ `lib/alg-sha1.c:102:9`!(d,e,a,b,c,37);
167 R2cxx . . $ `lib/alg-sha1.c:102:9`!(c,d,e,a,b,38);
168 R2cxx . . $ `lib/alg-sha1.c:102:9`!(b,c,d,e,a,39);
169 R3cxx . . $ `lib/alg-sha1.c:103:9`!(a,b,c,d,e,40);
170 R3cxx . . $ `lib/alg-sha1.c:103:9`!(e,a,b,c,d,41);
171 R3cxx . . $ `lib/alg-sha1.c:103:9`!(d,e,a,b,c,42);
172 R3cxx . . $ `lib/alg-sha1.c:103:9`!(c,d,e,a,b,43);
173 R3cxx . . $ `lib/alg-sha1.c:103:9`!(b,c,d,e,a,44);
174 R3cxx . . $ `lib/alg-sha1.c:103:9`!(a,b,c,d,e,45);
175 R3cxx . . $ `lib/alg-sha1.c:103:9`!(e,a,b,c,d,46);
176 R3cxx . . $ `lib/alg-sha1.c:103:9`!(d,e,a,b,c,47);
177 R3cxx . . $ `lib/alg-sha1.c:103:9`!(c,d,e,a,b,48);
178 R3cxx . . $ `lib/alg-sha1.c:103:9`!(b,c,d,e,a,49);
179 R3cxx . . $ `lib/alg-sha1.c:103:9`!(a,b,c,d,e,50);
180 R3cxx . . $ `lib/alg-sha1.c:103:9`!(e,a,b,c,d,51);
181 R3cxx . . $ `lib/alg-sha1.c:103:9`!(d,e,a,b,c,52);
182 R3cxx . . $ `lib/alg-sha1.c:103:9`!(c,d,e,a,b,53);
183 R3cxx . . $ `lib/alg-sha1.c:103:9`!(b,c,d,e,a,54);
184 R3cxx . . $ `lib/alg-sha1.c:103:9`!(a,b,c,d,e,55);
185 R3cxx . . $ `lib/alg-sha1.c:103:9`!(e,a,b,c,d,56);
186 R3cxx . . $ `lib/alg-sha1.c:103:9`!(d,e,a,b,c,57);
187 R3cxx . . $ `lib/alg-sha1.c:103:9`!(c,d,e,a,b,58);
188 R3cxx . . $ `lib/alg-sha1.c:103:9`!(b,c,d,e,a,59);
189 R4cxx . . $ `lib/alg-sha1.c:104:9`!(a,b,c,d,e,60);
190 R4cxx . . $ `lib/alg-sha1.c:104:9`!(e,a,b,c,d,61);
191 R4cxx . . $ `lib/alg-sha1.c:104:9`!(d,e,a,b,c,62);
192 R4cxx . . $ `lib/alg-sha1.c:104:9`!(c,d,e,a,b,63);
193 R4cxx . . $ `lib/alg-sha1.c:104:9`!(b,c,d,e,a,64);
194 R4cxx . . $ `lib/alg-sha1.c:104:9`!(a,b,c,d,e,65);
195 R4cxx . . $ `lib/alg-sha1.c:104:9`!(e,a,b,c,d,66);
196 R4cxx . . $ `lib/alg-sha1.c:104:9`!(d,e,a,b,c,67);
197 R4cxx . . $ `lib/alg-sha1.c:104:9`!(c,d,e,a,b,68);
198 R4cxx . . $ `lib/alg-sha1.c:104:9`!(b,c,d,e,a,69);
199 R4cxx . . $ `lib/alg-sha1.c:104:9`!(a,b,c,d,e,70);
200 R4cxx . . $ `lib/alg-sha1.c:104:9`!(e,a,b,c,d,71);
201 R4cxx . . $ `lib/alg-sha1.c:104:9`!(d,e,a,b,c,72);
202 R4cxx . . $ `lib/alg-sha1.c:104:9`!(c,d,e,a,b,73);
203 R4cxx . . $ `lib/alg-sha1.c:104:9`!(b,c,d,e,a,74);
204 R4cxx . . $ `lib/alg-sha1.c:104:9`!(a,b,c,d,e,75);
205 R4cxx . . $ `lib/alg-sha1.c:104:9`!(e,a,b,c,d,76);
206 R4cxx . . $ `lib/alg-sha1.c:104:9`!(d,e,a,b,c,77);
207 R4cxx . . $ `lib/alg-sha1.c:104:9`!(c,d,e,a,b,78);
208 R4cxx . . $ `lib/alg-sha1.c:104:9`!(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_ctxcxx . . $ `lib/crypt-port.h:358:9`! (struct sha1_ctxcxx . . $ sha1_ctx#* ctx)
225 {
226 /* SHA1 initialization constants */
227 ctx->statecxx . . $ sha1_ctx#state.[0] = 0x67452301;
228 ctx->statecxx . . $ sha1_ctx#state.[1] = 0xEFCDAB89;
229 ctx->statecxx . . $ sha1_ctx#state.[2] = 0x98BADCFE;
230 ctx->statecxx . . $ sha1_ctx#state.[3] = 0x10325476;
231 ctx->statecxx . . $ sha1_ctx#state.[4] = 0xC3D2E1F0;
232 ctx->countcxx . . $ sha1_ctx#count.[0] = ctx->countcxx . . $ sha1_ctx#count.[1] = 0;
233 }
234
235
236 /* Run your data through this. */
237 void
238 sha1_process_bytescxx . . $ `lib/crypt-port.h:359:9`! (const void *buffer, struct sha1_ctxcxx . . $ sha1_ctx# *ctx, size_t size)
239 {
240 const uint8_tcxx . . $ uint8_t# *data = buffer;
241 size_t j = (ctx->countcxx . . $ sha1_ctx#count.[0] >> 3) & 63;
242
243 if ((ctx->countcxx . . $ sha1_ctx#count.[0] += (uint32_tcxx . . $ uint32_t#)size << 3) < ((uint32_tcxx . . $ uint32_t#)size << 3)) ctx->countcxx . . $ sha1_ctx#count.[1]++;
244 ctx->countcxx . . $ sha1_ctx#count.[1] += (uint32_tcxx . . $ uint32_t#)(size >> 29);
245 if ((j + size) > 63)
246 {
247 const size_t i = 64 - j;
248 memcpycxx . . $ memcpy(cc817cb175f282d). (&ctx->buffercxx . . $ sha1_ctx#buffer.[j], data, i);
249 sha1_do_transformcxx . . $ sha1_do_transform(7cc3180c6a470cbb). (ctx->statecxx . . $ sha1_ctx#state., ctx->buffercxx . . $ sha1_ctx#buffer.);
250 j = 0;
251 size = size > i ? size - i : 0;
252 data += i;
253 while (size > 63)
254 {
255 sha1_do_transformcxx . . $ sha1_do_transform(7cc3180c6a470cbb). (ctx->statecxx . . $ sha1_ctx#state., data);
256 size -= 64;
257 data += 64;
258 }
259 }
260 memcpycxx . . $ memcpy(cc817cb175f282d). (&ctx->buffercxx . . $ sha1_ctx#buffer.[j], data, size);
261 }
262
263
264 /* Add padding and return the message digest. */
265 void *
266 sha1_finish_ctxcxx . . $ `lib/crypt-port.h:357:9`! (struct sha1_ctxcxx . . $ sha1_ctx# *ctx, void *resbuf)
267 {
268 uint32_tcxx . . $ uint32_t# i;
269 uint8_tcxx . . $ uint8_t# finalcount[8];
270
271 for (i = 0; i < 8; i++)
272 {
273 finalcount[i] = (unsigned char)((ctx->countcxx . . $ sha1_ctx#count.[(i >= 4 ? 0 : 1)]
274 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
275 }
276 sha1_process_bytescxx . . $ `lib/crypt-port.h:359:9`! ((const uint8_tcxx . . $ uint8_t# *)"\200", ctx, 1);
277 while ((ctx->countcxx . . $ sha1_ctx#count.[0] & 504) != 448)
278 sha1_process_bytescxx . . $ `lib/crypt-port.h:359:9`! ((const uint8_tcxx . . $ uint8_t# *)"\0", ctx, 1);
279 sha1_process_bytescxx . . $ `lib/crypt-port.h:359:9`! (finalcount, ctx, 8); /* Should cause a sha1_do_transform() */
280 for (i = 0; i < SHA1_DIGEST_SIZEcxx . . $ `lib/alg-sha1.c:83:9`!; i++)
281 {
282 *((uint8_tcxx . . $ uint8_t# *)resbuf + i) = (uint8_tcxx . . $ uint8_t#)((ctx->statecxx . . $ sha1_ctx#state.[i>>2]
283 >> ((3-(i & 3)) * 8) ) & 255);
284 }
285
286 /* Wipe variables */
287 i = 0;
288 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (ctx, sizeof (struct sha1_ctx));
289 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (finalcount, 8); /* SWR */
290
291 return resbuf;
292 }
293
294 #endif