ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Digest-Hashcash/Hashcash.xs
(Generate patch)

Comparing Digest-Hashcash/Hashcash.xs (file contents):
Revision 1.3 by root, Thu Sep 11 06:20:26 2003 UTC vs.
Revision 1.7 by root, Tue Jul 21 05:01:01 2015 UTC

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)))
39typedef U64TYPE ULONG; 58typedef 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
48typedef uint_fast32_t ULONG; /* 32-or-more-bit quantity */ 67typedef 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
54static int zprefix (ULONG n) 75static 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
132static void sha_transform(SHA_INFO *restrict sha_info) 153static 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
288static char nextenc[256]; 309static char
310nextenc[256];
289 311
290static char rand_char () 312static char
313rand_char ()
291{ 314{
292 return TRIALCHAR[rand () % sizeof (TRIALCHAR)]; 315 return TRIALCHAR[(int)(Drand01 () * sizeof (TRIALCHAR))];
293} 316}
294 317
295typedef double (*NVTime)(void); 318typedef double (*NVTime)(void);
296 319
297static double simple_nvtime (void) 320static double
321simple_nvtime (void)
298{ 322{
299 return time (0); 323 return time (0);
300} 324}
301 325
302static NVTime get_nvtime (void) 326static NVTime
327get_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));
364 int toklen, i; 389 int toklen, i;
365 time_t tstamp = timestamp ? timestamp : time (0); 390 time_t tstamp = timestamp ? timestamp : time (0);
366 struct tm *tm = gmtime (&tstamp); 391 struct tm *tm = gmtime (&tstamp);
367 392
368 New (0, token, 393 New (0, token,
369 1 + 1 // version 394 1 + 1 // version
370 + 12 + 1 // time field sans century 395 + 12 + 1 // time field sans century
371 + strlen (resource) + 1 // ressource 396 + strlen (resource) + 1 // ressource
372 + strlen (trial) + extrarand + 8 + 1 // trial 397 + strlen (trial) + extrarand + 8 + 1 // trial
373 + 1, 398 + 1,
374 char); 399 char);
375 400
376 if (!token) 401 if (!token)
377 croak ("out of memory"); 402 croak ("out of memory");
378 403
379 if (size > 64) 404 if (size > 64)
384 tm->tm_hour, tm->tm_min, tm->tm_sec, 409 tm->tm_hour, tm->tm_min, tm->tm_sec,
385 resource, trial); 410 resource, trial);
386 411
387 if (toklen > 8000) 412 if (toklen > 8000)
388 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 ();
389 416
390 i = toklen + extrarand; 417 i = toklen + extrarand;
391 while (toklen < i) 418 while (toklen < i)
392 token[toklen++] = rand_char (); 419 token[toklen++] = rand_char ();
393 420
412 do { 439 do {
413 *s = nextenc [*s]; 440 *s = nextenc [*s];
414 } while (*s++ == 'a'); 441 } while (*s++ == 'a');
415 } 442 }
416 443
444 perlinterp_acquire ();
445
417 RETVAL = newSVpvn (token, toklen); 446 RETVAL = newSVpvn (token, toklen);
418} 447}
419 OUTPUT: 448 OUTPUT:
420 RETVAL 449 RETVAL
421 450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines