Debian Codegraph @

lib/alg-sha1.c

raw
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 #include "crypt-port.h"
73 #include "alg-sha1.h"
74
75 #if INCLUDE_sha1crypt
76
77 #define SHA1_DIGEST_SIZE 20
78
79 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
80
81 /* blk0() and blk() perform the initial expand. */
82 /* I got the idea of expanding during the round function from SSLeay */
83 /* FIXME: can we do this in an endian-proof way? */
84 #if XCRYPT_USE_BIGENDIAN
85 #define blk0(i) block.l[i]
86 #else
87 #define blk0(i) (block.l[i] = (rol(block.l[i],24)&0xFF00FF00) \
88 |(rol(block.l[i],8)&0x00FF00FF))
89 #endif
90 #define blk(i) (block.l[i&15] = rol(block.l[(i+13)&15]^block.l[(i+8)&15] \
91 ^block.l[(i+2)&15]^block.l[i&15],1))
92
93 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
94 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
95 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
96 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
97 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
98 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
99
100
101 /* Hash a single 512-bit block. This is the core of the algorithm. */
102 static void
103 sha1_do_transform (uint32_t state[5], const uint8_t buffer[64])
104 {
105 uint32_t a, b, c, d, e;
106 typedef union
107 {
108 uint8_t c[64];
109 uint32_t l[16];
110 } CHAR64LONG16;
111 CHAR64LONG16 block;
112
113 memcpy (&block, buffer, 64);
114
115 /* Copy context->state[] to working vars */
116 a = state[0];
117 b = state[1];
118 c = state[2];
119 d = state[3];
120 e = state[4];
121
122 /* 4 rounds of 20 operations each. Loop unrolled. */
123 R0(a,b,c,d,e, 0);
124 R0(e,a,b,c,d, 1);
125 R0(d,e,a,b,c, 2);
126 R0(c,d,e,a,b, 3);
127 R0(b,c,d,e,a, 4);
128 R0(a,b,c,d,e, 5);
129 R0(e,a,b,c,d, 6);
130 R0(d,e,a,b,c, 7);
131 R0(c,d,e,a,b, 8);
132 R0(b,c,d,e,a, 9);
133 R0(a,b,c,d,e,10);
134 R0(e,a,b,c,d,11);
135 R0(d,e,a,b,c,12);
136 R0(c,d,e,a,b,13);
137 R0(b,c,d,e,a,14);
138 R0(a,b,c,d,e,15);
139 R1(e,a,b,c,d,16);
140 R1(d,e,a,b,c,17);
141 R1(c,d,e,a,b,18);
142 R1(b,c,d,e,a,19);
143 R2(a,b,c,d,e,20);
144 R2(e,a,b,c,d,21);
145 R2(d,e,a,b,c,22);
146 R2(c,d,e,a,b,23);
147 R2(b,c,d,e,a,24);
148 R2(a,b,c,d,e,25);
149 R2(e,a,b,c,d,26);
150 R2(d,e,a,b,c,27);
151 R2(c,d,e,a,b,28);
152 R2(b,c,d,e,a,29);
153 R2(a,b,c,d,e,30);
154 R2(e,a,b,c,d,31);
155 R2(d,e,a,b,c,32);
156 R2(c,d,e,a,b,33);
157 R2(b,c,d,e,a,34);
158 R2(a,b,c,d,e,35);
159 R2(e,a,b,c,d,36);
160 R2(d,e,a,b,c,37);
161 R2(c,d,e,a,b,38);
162 R2(b,c,d,e,a,39);
163 R3(a,b,c,d,e,40);
164 R3(e,a,b,c,d,41);
165 R3(d,e,a,b,c,42);
166 R3(c,d,e,a,b,43);
167 R3(b,c,d,e,a,44);
168 R3(a,b,c,d,e,45);
169 R3(e,a,b,c,d,46);
170 R3(d,e,a,b,c,47);
171 R3(c,d,e,a,b,48);
172 R3(b,c,d,e,a,49);
173 R3(a,b,c,d,e,50);
174 R3(e,a,b,c,d,51);
175 R3(d,e,a,b,c,52);
176 R3(c,d,e,a,b,53);
177 R3(b,c,d,e,a,54);
178 R3(a,b,c,d,e,55);
179 R3(e,a,b,c,d,56);
180 R3(d,e,a,b,c,57);
181 R3(c,d,e,a,b,58);
182 R3(b,c,d,e,a,59);
183 R4(a,b,c,d,e,60);
184 R4(e,a,b,c,d,61);
185 R4(d,e,a,b,c,62);
186 R4(c,d,e,a,b,63);
187 R4(b,c,d,e,a,64);
188 R4(a,b,c,d,e,65);
189 R4(e,a,b,c,d,66);
190 R4(d,e,a,b,c,67);
191 R4(c,d,e,a,b,68);
192 R4(b,c,d,e,a,69);
193 R4(a,b,c,d,e,70);
194 R4(e,a,b,c,d,71);
195 R4(d,e,a,b,c,72);
196 R4(c,d,e,a,b,73);
197 R4(b,c,d,e,a,74);
198 R4(a,b,c,d,e,75);
199 R4(e,a,b,c,d,76);
200 R4(d,e,a,b,c,77);
201 R4(c,d,e,a,b,78);
202 R4(b,c,d,e,a,79);
203
204 /* Add the working vars back into context.state[] */
205 state[0] += a;
206 state[1] += b;
207 state[2] += c;
208 state[3] += d;
209 state[4] += e;
210
211 /* Wipe variables */
212 a = b = c = d = e = 0;
213 }
214
215
216 /* SHA1Init - Initialize new context */
217 void
218 sha1_init_ctx (struct sha1_ctx* ctx)
219 {
220 /* SHA1 initialization constants */
221 ctx->state[0] = 0x67452301;
222 ctx->state[1] = 0xEFCDAB89;
223 ctx->state[2] = 0x98BADCFE;
224 ctx->state[3] = 0x10325476;
225 ctx->state[4] = 0xC3D2E1F0;
226 ctx->count[0] = ctx->count[1] = 0;
227 }
228
229
230 /* Run your data through this. */
231 void
232 sha1_process_bytes (const void *buffer, struct sha1_ctx *ctx, size_t size)
233 {
234 size_t i, j;
235
236 j = (ctx->count[0] >> 3) & 63;
237 if ((ctx->count[0] += (uint32_t)size << 3) < ((uint32_t)size << 3)) ctx->count[1]++;
238 ctx->count[1] += (uint32_t)(size >> 29);
239 if ((j + size) > 63)
240 {
241 memcpy (&ctx->buffer[j], buffer, (i = 64-j));
242 sha1_do_transform (ctx->state, ctx->buffer);
243 for ( ; i + 63 < size; i += 64)
244 sha1_do_transform (ctx->state, (const uint8_t*)buffer + i);
245 j = 0;
246 }
247 else i = 0;
248 memcpy (&ctx->buffer[j], (const uint8_t *)buffer + i, size - i);
249
250 }
251
252
253 /* Add padding and return the message digest. */
254 void *
255 sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
256 {
257 uint32_t i;
258 uint8_t finalcount[8];
259
260 for (i = 0; i < 8; i++)
261 {
262 finalcount[i] = (unsigned char)((ctx->count[(i >= 4 ? 0 : 1)]
263 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
264 }
265 sha1_process_bytes ((const uint8_t *)"\200", ctx, 1);
266 while ((ctx->count[0] & 504) != 448)
267 sha1_process_bytes ((const uint8_t *)"\0", ctx, 1);
268 sha1_process_bytes (finalcount, ctx, 8); /* Should cause a sha1_do_transform() */
269 for (i = 0; i < SHA1_DIGEST_SIZE; i++)
270 {
271 *((uint8_t *)resbuf + i) = (uint8_t)((ctx->state[i>>2]
272 >> ((3-(i & 3)) * 8) ) & 255);
273 }
274
275 /* Wipe variables */
276 i = 0;
277 explicit_bzero (ctx, sizeof (struct sha1_ctx));
278 explicit_bzero (finalcount, 8); /* SWR */
279
280 return resbuf;
281 }
282
283 #endif