Line | Count | Source |
1 | | |
2 | | /* |
3 | | * Copyright (C) Maxim Dounin |
4 | | * Copyright (C) NGINX, Inc. |
5 | | * |
6 | | * An internal SHA1 implementation. |
7 | | */ |
8 | | |
9 | | |
10 | | #include <nxt_main.h> |
11 | | #include <nxt_sha1.h> |
12 | | |
13 | | |
14 | | static const u_char *nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, |
15 | | size_t size); |
16 | | |
17 | | |
18 | | void |
19 | | nxt_sha1_init(nxt_sha1_t *ctx) |
20 | 2.50k | { |
21 | 2.50k | ctx->a = 0x67452301; |
22 | 2.50k | ctx->b = 0xefcdab89; |
23 | 2.50k | ctx->c = 0x98badcfe; |
24 | 2.50k | ctx->d = 0x10325476; |
25 | 2.50k | ctx->e = 0xc3d2e1f0; |
26 | | |
27 | 2.50k | ctx->bytes = 0; |
28 | 2.50k | } |
29 | | |
30 | | |
31 | | void |
32 | | nxt_sha1_update(nxt_sha1_t *ctx, const void *data, size_t size) |
33 | 3.75k | { |
34 | 3.75k | size_t used, free; |
35 | | |
36 | 3.75k | used = (size_t) (ctx->bytes & 0x3f); |
37 | 3.75k | ctx->bytes += size; |
38 | | |
39 | 3.75k | if (used) { |
40 | 1.24k | free = 64 - used; |
41 | | |
42 | 1.24k | if (size < free) { |
43 | 1.20k | memcpy(&ctx->buffer[used], data, size); |
44 | 1.20k | return; |
45 | 1.20k | } |
46 | | |
47 | 40 | memcpy(&ctx->buffer[used], data, free); |
48 | 40 | data = (u_char *) data + free; |
49 | 40 | size -= free; |
50 | 40 | (void) nxt_sha1_body(ctx, ctx->buffer, 64); |
51 | 40 | } |
52 | | |
53 | 2.55k | if (size >= 64) { |
54 | 74 | data = nxt_sha1_body(ctx, data, size & ~(size_t) 0x3f); |
55 | 74 | size &= 0x3f; |
56 | 74 | } |
57 | | |
58 | 2.55k | memcpy(ctx->buffer, data, size); |
59 | 2.55k | } |
60 | | |
61 | | |
62 | | void |
63 | | nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx) |
64 | 2.50k | { |
65 | 2.50k | size_t used, free; |
66 | | |
67 | 2.50k | used = (size_t) (ctx->bytes & 0x3f); |
68 | | |
69 | 2.50k | ctx->buffer[used++] = 0x80; |
70 | | |
71 | 2.50k | free = 64 - used; |
72 | | |
73 | 2.50k | if (free < 8) { |
74 | 25 | nxt_memzero(&ctx->buffer[used], free); |
75 | 25 | (void) nxt_sha1_body(ctx, ctx->buffer, 64); |
76 | 25 | used = 0; |
77 | 25 | free = 64; |
78 | 25 | } |
79 | | |
80 | 2.50k | nxt_memzero(&ctx->buffer[used], free - 8); |
81 | | |
82 | 2.50k | ctx->bytes <<= 3; |
83 | 2.50k | ctx->buffer[56] = (u_char) (ctx->bytes >> 56); |
84 | 2.50k | ctx->buffer[57] = (u_char) (ctx->bytes >> 48); |
85 | 2.50k | ctx->buffer[58] = (u_char) (ctx->bytes >> 40); |
86 | 2.50k | ctx->buffer[59] = (u_char) (ctx->bytes >> 32); |
87 | 2.50k | ctx->buffer[60] = (u_char) (ctx->bytes >> 24); |
88 | 2.50k | ctx->buffer[61] = (u_char) (ctx->bytes >> 16); |
89 | 2.50k | ctx->buffer[62] = (u_char) (ctx->bytes >> 8); |
90 | 2.50k | ctx->buffer[63] = (u_char) ctx->bytes; |
91 | | |
92 | 2.50k | (void) nxt_sha1_body(ctx, ctx->buffer, 64); |
93 | | |
94 | 2.50k | result[0] = (u_char) (ctx->a >> 24); |
95 | 2.50k | result[1] = (u_char) (ctx->a >> 16); |
96 | 2.50k | result[2] = (u_char) (ctx->a >> 8); |
97 | 2.50k | result[3] = (u_char) ctx->a; |
98 | 2.50k | result[4] = (u_char) (ctx->b >> 24); |
99 | 2.50k | result[5] = (u_char) (ctx->b >> 16); |
100 | 2.50k | result[6] = (u_char) (ctx->b >> 8); |
101 | 2.50k | result[7] = (u_char) ctx->b; |
102 | 2.50k | result[8] = (u_char) (ctx->c >> 24); |
103 | 2.50k | result[9] = (u_char) (ctx->c >> 16); |
104 | 2.50k | result[10] = (u_char) (ctx->c >> 8); |
105 | 2.50k | result[11] = (u_char) ctx->c; |
106 | 2.50k | result[12] = (u_char) (ctx->d >> 24); |
107 | 2.50k | result[13] = (u_char) (ctx->d >> 16); |
108 | 2.50k | result[14] = (u_char) (ctx->d >> 8); |
109 | 2.50k | result[15] = (u_char) ctx->d; |
110 | 2.50k | result[16] = (u_char) (ctx->e >> 24); |
111 | 2.50k | result[17] = (u_char) (ctx->e >> 16); |
112 | 2.50k | result[18] = (u_char) (ctx->e >> 8); |
113 | 2.50k | result[19] = (u_char) ctx->e; |
114 | | |
115 | 2.50k | nxt_memzero(ctx, sizeof(*ctx)); |
116 | 2.50k | } |
117 | | |
118 | | |
119 | | /* |
120 | | * Helper functions. |
121 | | */ |
122 | | |
123 | 597k | #define ROTATE(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits)))) |
124 | | |
125 | 53.3k | #define F1(b, c, d) (((b) & (c)) | ((~(b)) & (d))) |
126 | 106k | #define F2(b, c, d) ((b) ^ (c) ^ (d)) |
127 | 53.3k | #define F3(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) |
128 | | |
129 | | #define STEP(f, a, b, c, d, e, w, t) \ |
130 | 213k | temp = ROTATE(5, (a)) + f((b), (c), (d)) + (e) + (w) + (t); \ |
131 | 213k | (e) = (d); \ |
132 | 213k | (d) = (c); \ |
133 | 213k | (c) = ROTATE(30, (b)); \ |
134 | 213k | (b) = (a); \ |
135 | 213k | (a) = temp; |
136 | | |
137 | | |
138 | | /* |
139 | | * GET() reads 4 input bytes in big-endian byte order and returns |
140 | | * them as uint32_t. |
141 | | */ |
142 | | |
143 | | #define GET(n) \ |
144 | 42.7k | ( ((uint32_t) p[n * 4 + 3]) \ |
145 | 42.7k | | ((uint32_t) p[n * 4 + 2] << 8) \ |
146 | 42.7k | | ((uint32_t) p[n * 4 + 1] << 16) \ |
147 | 42.7k | | ((uint32_t) p[n * 4] << 24)) |
148 | | |
149 | | |
150 | | /* |
151 | | * This processes one or more 64-byte data blocks, but does not update |
152 | | * the bit counters. There are no alignment requirements. |
153 | | */ |
154 | | |
155 | | static const u_char * |
156 | | nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, size_t size) |
157 | 2.64k | { |
158 | 2.64k | uint32_t a, b, c, d, e, temp; |
159 | 2.64k | uint32_t saved_a, saved_b, saved_c, saved_d, saved_e; |
160 | 2.64k | uint32_t words[80]; |
161 | 2.64k | nxt_uint_t i; |
162 | 2.64k | const u_char *p; |
163 | | |
164 | 2.64k | p = data; |
165 | | |
166 | 2.64k | a = ctx->a; |
167 | 2.64k | b = ctx->b; |
168 | 2.64k | c = ctx->c; |
169 | 2.64k | d = ctx->d; |
170 | 2.64k | e = ctx->e; |
171 | | |
172 | 2.66k | do { |
173 | 2.66k | saved_a = a; |
174 | 2.66k | saved_b = b; |
175 | 2.66k | saved_c = c; |
176 | 2.66k | saved_d = d; |
177 | 2.66k | saved_e = e; |
178 | | |
179 | | /* Load data block into the words array */ |
180 | | |
181 | 45.3k | for (i = 0; i < 16; i++) { |
182 | 42.7k | words[i] = GET(i); |
183 | 42.7k | } |
184 | | |
185 | 173k | for (i = 16; i < 80; i++) { |
186 | 170k | words[i] = ROTATE(1, words[i - 3] |
187 | 170k | ^ words[i - 8] |
188 | 170k | ^ words[i - 14] |
189 | 170k | ^ words[i - 16]); |
190 | 170k | } |
191 | | |
192 | | /* Transformations */ |
193 | | |
194 | 2.66k | STEP(F1, a, b, c, d, e, words[0], 0x5a827999); |
195 | 2.66k | STEP(F1, a, b, c, d, e, words[1], 0x5a827999); |
196 | 2.66k | STEP(F1, a, b, c, d, e, words[2], 0x5a827999); |
197 | 2.66k | STEP(F1, a, b, c, d, e, words[3], 0x5a827999); |
198 | 2.66k | STEP(F1, a, b, c, d, e, words[4], 0x5a827999); |
199 | 2.66k | STEP(F1, a, b, c, d, e, words[5], 0x5a827999); |
200 | 2.66k | STEP(F1, a, b, c, d, e, words[6], 0x5a827999); |
201 | 2.66k | STEP(F1, a, b, c, d, e, words[7], 0x5a827999); |
202 | 2.66k | STEP(F1, a, b, c, d, e, words[8], 0x5a827999); |
203 | 2.66k | STEP(F1, a, b, c, d, e, words[9], 0x5a827999); |
204 | 2.66k | STEP(F1, a, b, c, d, e, words[10], 0x5a827999); |
205 | 2.66k | STEP(F1, a, b, c, d, e, words[11], 0x5a827999); |
206 | 2.66k | STEP(F1, a, b, c, d, e, words[12], 0x5a827999); |
207 | 2.66k | STEP(F1, a, b, c, d, e, words[13], 0x5a827999); |
208 | 2.66k | STEP(F1, a, b, c, d, e, words[14], 0x5a827999); |
209 | 2.66k | STEP(F1, a, b, c, d, e, words[15], 0x5a827999); |
210 | 2.66k | STEP(F1, a, b, c, d, e, words[16], 0x5a827999); |
211 | 2.66k | STEP(F1, a, b, c, d, e, words[17], 0x5a827999); |
212 | 2.66k | STEP(F1, a, b, c, d, e, words[18], 0x5a827999); |
213 | 2.66k | STEP(F1, a, b, c, d, e, words[19], 0x5a827999); |
214 | | |
215 | 2.66k | STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1); |
216 | 2.66k | STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1); |
217 | 2.66k | STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1); |
218 | 2.66k | STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1); |
219 | 2.66k | STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1); |
220 | 2.66k | STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1); |
221 | 2.66k | STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1); |
222 | 2.66k | STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1); |
223 | 2.66k | STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1); |
224 | 2.66k | STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1); |
225 | 2.66k | STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1); |
226 | 2.66k | STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1); |
227 | 2.66k | STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1); |
228 | 2.66k | STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1); |
229 | 2.66k | STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1); |
230 | 2.66k | STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1); |
231 | 2.66k | STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1); |
232 | 2.66k | STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1); |
233 | 2.66k | STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1); |
234 | 2.66k | STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1); |
235 | | |
236 | 2.66k | STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc); |
237 | 2.66k | STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc); |
238 | 2.66k | STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc); |
239 | 2.66k | STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc); |
240 | 2.66k | STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc); |
241 | 2.66k | STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc); |
242 | 2.66k | STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc); |
243 | 2.66k | STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc); |
244 | 2.66k | STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc); |
245 | 2.66k | STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc); |
246 | 2.66k | STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc); |
247 | 2.66k | STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc); |
248 | 2.66k | STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc); |
249 | 2.66k | STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc); |
250 | 2.66k | STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc); |
251 | 2.66k | STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc); |
252 | 2.66k | STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc); |
253 | 2.66k | STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc); |
254 | 2.66k | STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc); |
255 | 2.66k | STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc); |
256 | | |
257 | 2.66k | STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6); |
258 | 2.66k | STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6); |
259 | 2.66k | STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6); |
260 | 2.66k | STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6); |
261 | 2.66k | STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6); |
262 | 2.66k | STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6); |
263 | 2.66k | STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6); |
264 | 2.66k | STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6); |
265 | 2.66k | STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6); |
266 | 2.66k | STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6); |
267 | 2.66k | STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6); |
268 | 2.66k | STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6); |
269 | 2.66k | STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6); |
270 | 2.66k | STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6); |
271 | 2.66k | STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6); |
272 | 2.66k | STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6); |
273 | 2.66k | STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6); |
274 | 2.66k | STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6); |
275 | 2.66k | STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6); |
276 | 2.66k | STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6); |
277 | | |
278 | 2.66k | a += saved_a; |
279 | 2.66k | b += saved_b; |
280 | 2.66k | c += saved_c; |
281 | 2.66k | d += saved_d; |
282 | 2.66k | e += saved_e; |
283 | | |
284 | 2.66k | p += 64; |
285 | | |
286 | 2.66k | } while (size -= 64); |
287 | | |
288 | 2.64k | ctx->a = a; |
289 | 2.64k | ctx->b = b; |
290 | 2.64k | ctx->c = c; |
291 | 2.64k | ctx->d = d; |
292 | 2.64k | ctx->e = e; |
293 | | |
294 | 2.64k | return p; |
295 | 2.64k | } |