… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <time.h> |
5 | #include <time.h> |
6 | #include <stdlib.h> |
6 | #include <stdlib.h> |
7 | #include <stdint.h> |
7 | #include <stdint.h> |
|
|
8 | |
|
|
9 | #include "perlmulticore.h" |
8 | |
10 | |
9 | /* NIST Secure Hash Algorithm */ |
11 | /* NIST Secure Hash Algorithm */ |
10 | /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */ |
12 | /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */ |
11 | /* from Peter C. Gutmann's implementation as found in */ |
13 | /* from Peter C. Gutmann's implementation as found in */ |
12 | /* Applied Cryptography by Bruce Schneier */ |
14 | /* Applied Cryptography by Bruce Schneier */ |
… | |
… | |
17 | /* pcg: I was tempted to just rip this code off, after all, if you don't |
19 | /* pcg: I was tempted to just rip this code off, after all, if you don't |
18 | * demand anything I am inclined not to give anything. *Sigh* something |
20 | * demand anything I am inclined not to give anything. *Sigh* something |
19 | * kept me from doing it, so here's the truth: I took this code from the |
21 | * kept me from doing it, so here's the truth: I took this code from the |
20 | * SHA1 perl module, since it looked reasonably well-crafted. I modified |
22 | * SHA1 perl module, since it looked reasonably well-crafted. I modified |
21 | * it here and there, though. |
23 | * it here and there, though. |
|
|
24 | */ |
|
|
25 | |
|
|
26 | /* |
|
|
27 | * we have lots of micro-optimizations here, this is just for toying |
|
|
28 | * around... |
22 | */ |
29 | */ |
23 | |
30 | |
24 | /* don't expect _too_ much from compilers for now. */ |
31 | /* don't expect _too_ much from compilers for now. */ |
25 | #if __GNUC__ > 2 |
32 | #if __GNUC__ > 2 |
26 | # define restrict __restrict__ |
33 | # define restrict __restrict__ |
… | |
… | |
31 | #elif __STDC_VERSION__ < 199900 |
38 | #elif __STDC_VERSION__ < 199900 |
32 | # define restrict |
39 | # define restrict |
33 | # define inline |
40 | # define inline |
34 | #endif |
41 | #endif |
35 | |
42 | |
|
|
43 | #if __GNUC__ < 2 |
|
|
44 | # define __attribute__(x) |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #ifdef __i386 |
|
|
48 | # define a_regparm(n) __attribute__((__regparm__(n))) |
|
|
49 | #else |
|
|
50 | # define a_regparm(n) |
|
|
51 | #endif |
|
|
52 | |
|
|
53 | #define a_const __attribute__((__const__)) |
|
|
54 | |
36 | /* Useful defines & typedefs */ |
55 | /* Useful defines & typedefs */ |
37 | |
56 | |
38 | #if defined(U64TYPE) && (defined(USE_64_BIT_INT) || ((BYTEORDER != 0x1234) && (BYTEORDER != 0x4321))) |
57 | #if defined(U64TYPE) && (defined(USE_64_BIT_INT) || ((BYTEORDER != 0x1234) && (BYTEORDER != 0x4321))) |
39 | typedef U64TYPE ULONG; |
58 | typedef U64TYPE ULONG; |
40 | # if BYTEORDER == 0x1234 |
59 | # if BYTEORDER == 0x1234 |
41 | # undef BYTEORDER |
60 | # undef BYTEORDER |
42 | # define BYTEORDER 0x12345678 |
61 | # define BYTEORDER 0x12345678 |
43 | # elif BYTEORDER == 0x4321 |
62 | # elif BYTEORDER == 0x4321 |
44 | # undef BYTEORDER |
63 | # undef BYTEORDER |
45 | # define BYTEORDER 0x87654321 |
64 | # define BYTEORDER 0x87654321 |
46 | # endif |
65 | # endif |
47 | #else |
66 | #else |
48 | typedef uint_fast32_t ULONG; /* 32-or-more-bit quantity */ |
67 | typedef uint_fast32_t ULONG; /* 32-or-more-bit quantity */ |
49 | #endif |
68 | #endif |
50 | |
69 | |
51 | #if GCCX86ASM |
70 | #if GCCX86ASM |
52 | # define zprefix(n) ({ int _r; __asm__ ("bsrl %1, %0" : "=r" (_r) : "r" (n)); 31 - _r ; }) |
71 | # define zprefix(n) ({ int _r; __asm__ ("bsrl %1, %0" : "=r" (_r) : "r" (n)); 31 - _r ; }) |
|
|
72 | #elif __GNUC__ > 2 && __GNUC_MINOR__ > 3 |
|
|
73 | # define zprefix(n) (__extension__ ({ uint32_t n__ = (n); n ? __builtin_clz (n) : 32; })) |
53 | #else |
74 | #else |
54 | static int zprefix (ULONG n) |
75 | static int a_const zprefix (ULONG n) |
55 | { |
76 | { |
56 | static char zp[256] = |
77 | static char zp[256] = |
57 | { |
78 | { |
58 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
79 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
59 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
80 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
… | |
… | |
127 | B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30) |
148 | B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30) |
128 | |
149 | |
129 | #define FT(n) \ |
150 | #define FT(n) \ |
130 | A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30) |
151 | A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30) |
131 | |
152 | |
132 | static void sha_transform(SHA_INFO *restrict sha_info) |
153 | static void a_regparm(1) sha_transform(SHA_INFO *restrict sha_info) |
133 | { |
154 | { |
134 | int i; |
155 | int i; |
135 | U8 *dp; |
156 | U8 *restrict dp; |
136 | ULONG T, A, B, C, D, E, W[80], *restrict WP; |
157 | ULONG T, A, B, C, D, E, W[80], *restrict WP; |
137 | |
158 | |
138 | dp = sha_info->data; |
159 | dp = sha_info->data; |
139 | |
160 | |
140 | #if BYTEORDER == 0x1234 |
161 | #if BYTEORDER == 0x1234 |
… | |
… | |
283 | : zprefix (sha_info->digest[1]) + 32; |
304 | : zprefix (sha_info->digest[1]) + 32; |
284 | } |
305 | } |
285 | |
306 | |
286 | #define TRIALCHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./;<=>?@[]{}^_|" |
307 | #define TRIALCHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./;<=>?@[]{}^_|" |
287 | |
308 | |
288 | static char nextenc[256]; |
309 | static char |
|
|
310 | nextenc[256]; |
289 | |
311 | |
290 | static char rand_char () |
312 | static char |
|
|
313 | rand_char () |
291 | { |
314 | { |
292 | return TRIALCHAR[rand () % sizeof (TRIALCHAR)]; |
315 | return TRIALCHAR[(int)(Drand01 () * sizeof (TRIALCHAR))]; |
293 | } |
316 | } |
294 | |
317 | |
295 | typedef double (*NVTime)(void); |
318 | typedef double (*NVTime)(void); |
296 | |
319 | |
297 | static double simple_nvtime (void) |
320 | static double |
|
|
321 | simple_nvtime (void) |
298 | { |
322 | { |
299 | return time (0); |
323 | return time (0); |
300 | } |
324 | } |
301 | |
325 | |
302 | static NVTime get_nvtime (void) |
326 | static NVTime |
|
|
327 | get_nvtime (void) |
303 | { |
328 | { |
304 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
329 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
305 | |
330 | |
306 | if (svp && SvIOK(*svp)) |
331 | if (svp && SvIOK(*svp)) |
307 | return INT2PTR(NVTime, SvIV(*svp)); |
332 | return INT2PTR(NVTime, SvIV(*svp)); |
… | |
… | |
324 | |
349 | |
325 | # could be improved quite a bit in accuracy |
350 | # could be improved quite a bit in accuracy |
326 | NV |
351 | NV |
327 | _estimate_rounds () |
352 | _estimate_rounds () |
328 | CODE: |
353 | CODE: |
|
|
354 | { |
329 | char data[40]; |
355 | char data[40]; |
330 | NVTime nvtime = get_nvtime (); |
356 | NVTime nvtime = get_nvtime (); |
331 | NV t1, t2, t; |
357 | NV t1, t2, t; |
332 | int count = 0; |
358 | int count = 0; |
333 | SHA_INFO ctx; |
359 | SHA_INFO ctx; |
… | |
… | |
348 | t2 = nvtime (); |
374 | t2 = nvtime (); |
349 | |
375 | |
350 | } while (t == t2); |
376 | } while (t == t2); |
351 | |
377 | |
352 | RETVAL = (NV)count / (t2 - t1); |
378 | RETVAL = (NV)count / (t2 - t1); |
|
|
379 | } |
353 | OUTPUT: |
380 | OUTPUT: |
354 | RETVAL |
381 | RETVAL |
355 | |
382 | |
356 | SV * |
383 | SV * |
357 | _gentoken (int size, IV timestamp, char *resource, char *trial = "", int extrarand = 0) |
384 | _gentoken (int size, IV timestamp, char *resource, char *trial = "", int extrarand = 0) |
358 | CODE: |
385 | CODE: |
|
|
386 | { |
359 | SHA_INFO ctx1, ctx; |
387 | SHA_INFO ctx1, ctx; |
360 | char *token, *seq, *s; |
388 | char *token, *seq, *s; |
361 | int toklen, i; |
389 | int toklen, i; |
362 | time_t tstamp = timestamp ? timestamp : time (0); |
390 | time_t tstamp = timestamp ? timestamp : time (0); |
363 | struct tm *tm = gmtime (&tstamp); |
391 | struct tm *tm = gmtime (&tstamp); |
364 | |
392 | |
365 | New (0, token, |
393 | New (0, token, |
366 | 1 + 1 // version |
394 | 1 + 1 // version |
367 | + 12 + 1 // time field sans century |
395 | + 12 + 1 // time field sans century |
368 | + strlen (resource) + 1 // ressource |
396 | + strlen (resource) + 1 // ressource |
369 | + strlen (trial) + extrarand + 8 + 1 // trial |
397 | + strlen (trial) + extrarand + 8 + 1 // trial |
370 | + 1, |
398 | + 1, |
371 | char); |
399 | char); |
372 | |
400 | |
373 | if (!token) |
401 | if (!token) |
374 | croak ("out of memory"); |
402 | croak ("out of memory"); |
375 | |
403 | |
376 | if (size > 64) |
404 | if (size > 64) |
… | |
… | |
381 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
409 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
382 | resource, trial); |
410 | resource, trial); |
383 | |
411 | |
384 | if (toklen > 8000) |
412 | if (toklen > 8000) |
385 | croak ("token length must be <= 8000 in this implementation\n"); |
413 | croak ("token length must be <= 8000 in this implementation\n"); |
|
|
414 | |
|
|
415 | perlinterp_release (); |
386 | |
416 | |
387 | i = toklen + extrarand; |
417 | i = toklen + extrarand; |
388 | while (toklen < i) |
418 | while (toklen < i) |
389 | token[toklen++] = rand_char (); |
419 | token[toklen++] = rand_char (); |
390 | |
420 | |
… | |
… | |
409 | do { |
439 | do { |
410 | *s = nextenc [*s]; |
440 | *s = nextenc [*s]; |
411 | } while (*s++ == 'a'); |
441 | } while (*s++ == 'a'); |
412 | } |
442 | } |
413 | |
443 | |
|
|
444 | perlinterp_acquire (); |
|
|
445 | |
414 | RETVAL = newSVpvn (token, toklen); |
446 | RETVAL = newSVpvn (token, toklen); |
|
|
447 | } |
415 | OUTPUT: |
448 | OUTPUT: |
416 | RETVAL |
449 | RETVAL |
417 | |
450 | |
418 | int |
451 | int |
419 | _prefixlen (SV *tok) |
452 | _prefixlen (SV *tok) |
420 | CODE: |
453 | CODE: |
|
|
454 | { |
421 | STRLEN toklen; |
455 | STRLEN toklen; |
422 | char *token = SvPV (tok, toklen); |
456 | char *token = SvPV (tok, toklen); |
423 | SHA_INFO ctx; |
457 | SHA_INFO ctx; |
424 | |
458 | |
425 | sha_init (&ctx); |
459 | sha_init (&ctx); |
426 | sha_update (&ctx, token, toklen); |
460 | sha_update (&ctx, token, toklen); |
427 | RETVAL = sha_final (&ctx); |
461 | RETVAL = sha_final (&ctx); |
|
|
462 | } |
428 | OUTPUT: |
463 | OUTPUT: |
429 | RETVAL |
464 | RETVAL |
430 | |
465 | |
431 | |
466 | |