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