1 /* This file is copied from the libsrs2 sources */
2 /* Modified by Timo Röhling <timo.roehling@gmx.de> */
3
4 /* NIST Secure Hash Algorithm */
5 /* Borrowed from SHA1.xs by Gisle Ass */
6 /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
7 /* from Peter C. Gutmann's implementation as found in */
8 /* Applied Cryptography by Bruce Schneier */
9 /* Further modifications to include the "UNRAVEL" stuff, below */
10 /* HMAC functions by Shevek <srs@anarres.org> for inclusion in
11 * libsrs2, under GPL-2 or BSD license. Combine this lot in any way
12 * you think will stand up in court. I hope my intent is clear. */
13
14 /* This code is in the public domain */
15
16 /* Useful defines & typedefs */
17
18 #include <stdarg.h>
19 #include <time.h> /* time */
20 #include <sys/types.h> /* tyepdefs */
21 #include <sys/time.h> /* timeval / timezone struct */
22 #include <string.h> /* memcpy, strcpy, memset */
23
24 #include "srs2.h"
25
26 #ifdef SIZEOF_UNSIGNED_LONG
27 #if SIZEOF_UNSIGNED_LONG < 4
28 #error "SHA1 requires an unsigned long of at least 32 bits"
29 #endif
30 #endif
31
32 #if SIZEOF_UNSIGNED_LONG == 4
33 # ifdef WORDS_BIGENDIAN
34 # define BYTEORDER 0x4321
35 # else
36 # define BYTEORDER 0x1234
37 # endif
38 #elif SIZEOF_UNSIGNED_LONG == 8
39 # ifdef WORDS_BIGENDIAN
40 # define BYTEORDER 0x87654321
41 # else
42 # define BYTEORDER 0x12345678
43 # endif
44 #else
45 # error "SHA1 requires an unsigned long of either 4 or 8 bytes"
46 #endif
47
48 /* UNRAVEL should be fastest & biggest */
49 /* UNROLL_LOOPS should be just as big, but slightly slower */
50 /* both undefined should be smallest and slowest */
51
52 #define SHA_VERSION 1
53 #define UNRAVEL
54 /* #define UNROLL_LOOPS */
55
56 /* SHA f()-functions */
57 #define f1(x,y,z) ((x & y) | (~x & z))
58 #define f2(x,y,z) (x ^ y ^ z)
59 #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
60 #define f4(x,y,z) (x ^ y ^ z)
61
62 /* SHA constants */
63 #define CONST1 0x5a827999L
64 #define CONST2 0x6ed9eba1L
65 #define CONST3 0x8f1bbcdcL
66 #define CONST4 0xca62c1d6L
67
68 /* truncate to 32 bits -- should be a null op on 32-bit machines */
69 #define T32(x) ((x) & 0xffffffffL)
70
71 /* 32-bit rotate */
72 #define R32(x,n) T32(((x << n) | (x >> (32 - n))))
73
74 /* the generic case, for when the overall rotation is not unraveled */
75 #define FG(n) \
76 T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); \
77 E = D; D = C; C = R32(B,30); B = A; A = T
78
79 /* specific cases, for when the overall rotation is unraveled */
80 #define FA(n) \
81 T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); B = R32(B,30)
82
83 #define FB(n) \
84 E = T32(R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n); A = R32(A,30)
85
86 #define FC(n) \
87 D = T32(R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n); T = R32(T,30)
88
89 #define FD(n) \
90 C = T32(R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n); E = R32(E,30)
91
92 #define FE(n) \
93 B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30)
94
95 #define FT(n) \
96 A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30)
97
98
sha_transform(SHA_INFO * sha_info)99 static void sha_transform(SHA_INFO *sha_info)
100 {
101 int i;
102 sha_byte *dp;
103 ULONG T, A, B, C, D, E, W[80], *WP;
104
105 dp = sha_info->data;
106
107 /*
108 the following makes sure that at least one code block below is
109 traversed or an error is reported, without the necessity for nested
110 preprocessor if/else/endif blocks, which are a great pain in the
111 nether regions of the anatomy...
112 */
113 #undef SWAP_DONE
114
115 #if BYTEORDER == 0x1234
116 #define SWAP_DONE
117 /* assert(sizeof(ULONG) == 4); */
118 for (i = 0; i < 16; ++i) {
119 T = *((ULONG *) dp);
120 dp += 4;
121 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
122 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
123 }
124 #endif
125
126 #if BYTEORDER == 0x4321
127 #define SWAP_DONE
128 /* assert(sizeof(ULONG) == 4); */
129 for (i = 0; i < 16; ++i) {
130 T = *((ULONG *) dp);
131 dp += 4;
132 W[i] = T32(T);
133 }
134 #endif
135
136 #if BYTEORDER == 0x12345678
137 #define SWAP_DONE
138 /* assert(sizeof(ULONG) == 8); */
139 for (i = 0; i < 16; i += 2) {
140 T = *((ULONG *) dp);
141 dp += 8;
142 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
143 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
144 T >>= 32;
145 W[i+1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
146 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
147 }
148 #endif
149
150 #if BYTEORDER == 0x87654321
151 #define SWAP_DONE
152 /* assert(sizeof(ULONG) == 8); */
153 for (i = 0; i < 16; i += 2) {
154 T = *((ULONG *) dp);
155 dp += 8;
156 W[i] = T32(T >> 32);
157 W[i+1] = T32(T);
158 }
159 #endif
160
161 #ifndef SWAP_DONE
162 #error Unknown byte order -- you need to add code here
163 #endif /* SWAP_DONE */
164
165 for (i = 16; i < 80; ++i) {
166 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
167 #if (SHA_VERSION == 1)
168 W[i] = R32(W[i], 1);
169 #endif /* SHA_VERSION */
170 }
171 A = sha_info->digest[0];
172 B = sha_info->digest[1];
173 C = sha_info->digest[2];
174 D = sha_info->digest[3];
175 E = sha_info->digest[4];
176 WP = W;
177 #ifdef UNRAVEL
178 FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
179 FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
180 FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
181 FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
182 FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
183 FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
184 FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
185 FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
186 sha_info->digest[0] = T32(sha_info->digest[0] + E);
187 sha_info->digest[1] = T32(sha_info->digest[1] + T);
188 sha_info->digest[2] = T32(sha_info->digest[2] + A);
189 sha_info->digest[3] = T32(sha_info->digest[3] + B);
190 sha_info->digest[4] = T32(sha_info->digest[4] + C);
191 #else /* !UNRAVEL */
192 #ifdef UNROLL_LOOPS
193 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
194 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
195 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
196 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
197 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
198 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
199 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
200 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
201 #else /* !UNROLL_LOOPS */
202 for (i = 0; i < 20; ++i) { FG(1); }
203 for (i = 20; i < 40; ++i) { FG(2); }
204 for (i = 40; i < 60; ++i) { FG(3); }
205 for (i = 60; i < 80; ++i) { FG(4); }
206 #endif /* !UNROLL_LOOPS */
207 sha_info->digest[0] = T32(sha_info->digest[0] + A);
208 sha_info->digest[1] = T32(sha_info->digest[1] + B);
209 sha_info->digest[2] = T32(sha_info->digest[2] + C);
210 sha_info->digest[3] = T32(sha_info->digest[3] + D);
211 sha_info->digest[4] = T32(sha_info->digest[4] + E);
212 #endif /* !UNRAVEL */
213 }
214
215 /* initialize the SHA digest */
216
217 static void
sha_init(SHA_INFO * sha_info)218 sha_init(SHA_INFO *sha_info)
219 {
220 sha_info->digest[0] = 0x67452301L;
221 sha_info->digest[1] = 0xefcdab89L;
222 sha_info->digest[2] = 0x98badcfeL;
223 sha_info->digest[3] = 0x10325476L;
224 sha_info->digest[4] = 0xc3d2e1f0L;
225 sha_info->count_lo = 0L;
226 sha_info->count_hi = 0L;
227 sha_info->local = 0;
228 }
229
230 /* update the SHA digest */
231
232 static void
sha_update(SHA_INFO * sha_info,sha_byte * buffer,int count)233 sha_update(SHA_INFO *sha_info, sha_byte *buffer, int count)
234 {
235 int i;
236 ULONG clo;
237
238 clo = T32(sha_info->count_lo + ((ULONG) count << 3));
239 if (clo < sha_info->count_lo) {
240 ++sha_info->count_hi;
241 }
242 sha_info->count_lo = clo;
243 sha_info->count_hi += (ULONG) count >> 29;
244 if (sha_info->local) {
245 i = SHA_BLOCKSIZE - sha_info->local;
246 if (i > count) {
247 i = count;
248 }
249 memcpy(((sha_byte *) sha_info->data) + sha_info->local, buffer, i);
250 count -= i;
251 buffer += i;
252 sha_info->local += i;
253 if (sha_info->local == SHA_BLOCKSIZE) {
254 sha_transform(sha_info);
255 } else {
256 return;
257 }
258 }
259 while (count >= SHA_BLOCKSIZE) {
260 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
261 buffer += SHA_BLOCKSIZE;
262 count -= SHA_BLOCKSIZE;
263 sha_transform(sha_info);
264 }
265 memcpy(sha_info->data, buffer, count);
266 sha_info->local = count;
267 }
268
269
270 static void
sha_transform_and_copy(unsigned char digest[20],SHA_INFO * sha_info)271 sha_transform_and_copy(unsigned char digest[20], SHA_INFO *sha_info)
272 {
273 sha_transform(sha_info);
274 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
275 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
276 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
277 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
278 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
279 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
280 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
281 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
282 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
283 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
284 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
285 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
286 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
287 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
288 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
289 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
290 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
291 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
292 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
293 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
294 }
295
296 /* finish computing the SHA digest */
297 static void
sha_final(unsigned char digest[20],SHA_INFO * sha_info)298 sha_final(unsigned char digest[20], SHA_INFO *sha_info)
299 {
300 int count;
301 ULONG lo_bit_count, hi_bit_count;
302
303 lo_bit_count = sha_info->count_lo;
304 hi_bit_count = sha_info->count_hi;
305 count = (int) ((lo_bit_count >> 3) & 0x3f);
306 ((sha_byte *) sha_info->data)[count++] = 0x80;
307 if (count > SHA_BLOCKSIZE - 8) {
308 memset(((sha_byte *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count);
309 sha_transform(sha_info);
310 memset((sha_byte *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
311 } else {
312 memset(((sha_byte *) sha_info->data) + count, 0,
313 SHA_BLOCKSIZE - 8 - count);
314 }
315 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
316 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
317 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
318 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
319 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
320 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
321 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
322 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
323 sha_transform_and_copy(digest, sha_info);
324 }
325
326
327
328 /********************************************************************/
329
330 /*
331 SHA_INFO ctx;
332 unsigned char *data;
333 STRLEN len;
334 unsigned char digeststr[20];
335
336 sha_init(&ctx);
337
338 for (i = 0; i < items; i++) {
339 data = (unsigned char *)(SvPVbyte(ST(i), len));
340 sha_update(&ctx, data, len);
341 }
342 sha_final(digeststr, &ctx);
343 */
344
345 static void
sha_digest(char * out,char * data,unsigned len)346 sha_digest(char *out, char *data, unsigned len)
347 {
348 SHA_INFO ctx;
349 sha_init(&ctx);
350 sha_update(&ctx, (sha_byte*)data, len);
351 sha_final((sha_byte*)out, &ctx);
352 }
353
354 void
srs_hmac_init(srs_hmac_ctx_t * ctx,char * secret,unsigned len)355 srs_hmac_init(srs_hmac_ctx_t *ctx, char *secret, unsigned len)
356 {
357 char sbuf[SHA_BLOCKSIZE];
358 unsigned i;
359
360 if (len > SHA_BLOCKSIZE) {
361 sha_digest(sbuf, secret, len);
362 secret = sbuf;
363 len = SHA_DIGESTSIZE;
364 }
365
366 memset(ctx->ipad, 0x36, SHA_BLOCKSIZE);
367 memset(ctx->opad, 0x5c, SHA_BLOCKSIZE);
368 for (i = 0; i < len; i++) {
369 ctx->ipad[i] ^= secret[i];
370 ctx->opad[i] ^= secret[i];
371 }
372
373 memset(sbuf, 0, SHA_BLOCKSIZE);
374
375 sha_init(&ctx->sctx);
376 sha_update(&ctx->sctx, (sha_byte*)ctx->ipad, SHA_BLOCKSIZE);
377 }
378
379 void
srs_hmac_update(srs_hmac_ctx_t * ctx,char * data,unsigned len)380 srs_hmac_update(srs_hmac_ctx_t *ctx, char *data, unsigned len)
381 {
382 sha_update(&ctx->sctx, (sha_byte*)data, len);
383 }
384
385 void
srs_hmac_fini(srs_hmac_ctx_t * ctx,char * out)386 srs_hmac_fini(srs_hmac_ctx_t *ctx, char *out)
387 {
388 sha_byte buf[SHA_DIGESTSIZE + 1];
389
390 sha_final(buf, &ctx->sctx);
391 sha_init(&ctx->sctx);
392 sha_update(&ctx->sctx, (sha_byte*)ctx->opad, SHA_BLOCKSIZE);
393 sha_update(&ctx->sctx, buf, SHA_DIGESTSIZE);
394 sha_final((sha_byte*)out, &ctx->sctx);
395 }
396