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.4 by root, Mon Oct 20 04:17:05 2003 UTC vs.
Revision 1.8 by root, Wed Jul 22 10:33:08 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 XULONG;
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 XULONG; /* 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 ; })
53#elif __GNUC__ > 2 && __GNUC_MINOR__ > 3 72#elif __GNUC__ > 2 && __GNUC_MINOR__ > 3
54# define zprefix(n) (__extension__ ({ uint32_t n__ = (n); n ? __builtin_clz (n) : 32; })) 73# define zprefix(n) (__extension__ ({ uint32_t n__ = (n); n ? __builtin_clz (n) : 32; }))
55#else 74#else
56static int zprefix (ULONG n) 75static int a_const zprefix (U32 n)
57{ 76{
58 static char zp[256] = 77 static char zp[256] =
59 { 78 {
60 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,
61 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,
85 104
86#define SHA_BLOCKSIZE 64 105#define SHA_BLOCKSIZE 64
87#define SHA_DIGESTSIZE 20 106#define SHA_DIGESTSIZE 20
88 107
89typedef struct { 108typedef struct {
90 ULONG digest[5]; /* message digest */ 109 U32 digest[5]; /* message digest */
91 ULONG count; /* 32-bit bit count */ 110 U32 count; /* 32-bit bit count */
92 int local; /* unprocessed amount in data */ 111 int local; /* unprocessed amount in data */
93 U8 data[SHA_BLOCKSIZE]; /* SHA data buffer */ 112 U8 data[SHA_BLOCKSIZE]; /* SHA data buffer */
94} SHA_INFO; 113} SHA_INFO;
95 114
96 115
129 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)
130 149
131#define FT(n) \ 150#define FT(n) \
132 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)
133 152
134static void sha_transform(SHA_INFO *restrict sha_info) 153static void a_regparm(1) sha_transform(SHA_INFO *restrict sha_info)
135{ 154{
136 int i; 155 int i;
137 U8 *dp; 156 U8 *restrict dp;
138 ULONG T, A, B, C, D, E, W[80], *restrict WP; 157 U32 A, B, C, D, E, W[80], *restrict WP;
158 XULONG T;
139 159
140 dp = sha_info->data; 160 dp = sha_info->data;
141 161
142#if BYTEORDER == 0x1234 162#if BYTEORDER == 0x1234
143 assert(sizeof(ULONG) == 4); 163 assert(sizeof(XULONG) == 4);
144# ifdef HAS_NTOHL 164# ifdef HAS_NTOHL
145 for (i = 0; i < 16; ++i) { 165 for (i = 0; i < 16; ++i) {
146 T = *((ULONG *) dp); 166 T = *((XULONG *) dp);
147 dp += 4; 167 dp += 4;
148 W[i] = ntohl (T); 168 W[i] = ntohl (T);
149 } 169 }
150# else 170# else
151 for (i = 0; i < 16; ++i) { 171 for (i = 0; i < 16; ++i) {
152 T = *((ULONG *) dp); 172 T = *((XULONG *) dp);
153 dp += 4; 173 dp += 4;
154 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) | 174 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
155 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff); 175 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
156 } 176 }
157# endif 177# endif
158#elif BYTEORDER == 0x4321 178#elif BYTEORDER == 0x4321
159 assert(sizeof(ULONG) == 4); 179 assert(sizeof(XULONG) == 4);
160 for (i = 0; i < 16; ++i) { 180 for (i = 0; i < 16; ++i) {
161 T = *((ULONG *) dp); 181 T = *((XULONG *) dp);
162 dp += 4; 182 dp += 4;
163 W[i] = T32(T); 183 W[i] = T32(T);
164 } 184 }
165#elif BYTEORDER == 0x12345678 185#elif BYTEORDER == 0x12345678
166 assert(sizeof(ULONG) == 8); 186 assert(sizeof(XULONG) == 8);
167 for (i = 0; i < 16; i += 2) { 187 for (i = 0; i < 16; i += 2) {
168 T = *((ULONG *) dp); 188 T = *((XULONG *) dp);
169 dp += 8; 189 dp += 8;
170 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) | 190 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
171 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff); 191 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
172 T >>= 32; 192 T >>= 32;
173 W[i+1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) | 193 W[i+1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
174 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff); 194 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
175 } 195 }
176#elif BYTEORDER == 0x87654321 196#elif BYTEORDER == 0x87654321
177 assert(sizeof(ULONG) == 8); 197 assert(sizeof(XULONG) == 8);
178 for (i = 0; i < 16; i += 2) { 198 for (i = 0; i < 16; i += 2) {
179 T = *((ULONG *) dp); 199 T = *((XULONG *) dp);
180 dp += 8; 200 dp += 8;
181 W[i] = T32(T >> 32); 201 W[i] = T32(T >> 32);
182 W[i+1] = T32(T); 202 W[i+1] = T32(T);
183 } 203 }
184#else 204#else
285 : zprefix (sha_info->digest[1]) + 32; 305 : zprefix (sha_info->digest[1]) + 32;
286} 306}
287 307
288#define TRIALCHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./;<=>?@[]{}^_|" 308#define TRIALCHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./;<=>?@[]{}^_|"
289 309
290static char nextenc[256]; 310static char
311nextenc[256];
291 312
292static char rand_char () 313static char
314rand_char ()
293{ 315{
294 return TRIALCHAR[rand () % sizeof (TRIALCHAR)]; 316 return TRIALCHAR[(int)(Drand01 () * sizeof (TRIALCHAR))];
295} 317}
296 318
297typedef double (*NVTime)(void); 319typedef double (*NVTime)(void);
298 320
299static double simple_nvtime (void) 321static double
322simple_nvtime (void)
300{ 323{
301 return time (0); 324 return time (0);
302} 325}
303 326
304static NVTime get_nvtime (void) 327static NVTime
328get_nvtime (void)
305{ 329{
306 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 330 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
307 331
308 if (svp && SvIOK(*svp)) 332 if (svp && SvIOK(*svp))
309 return INT2PTR(NVTime, SvIV(*svp)); 333 return INT2PTR(NVTime, SvIV(*svp));
366 int toklen, i; 390 int toklen, i;
367 time_t tstamp = timestamp ? timestamp : time (0); 391 time_t tstamp = timestamp ? timestamp : time (0);
368 struct tm *tm = gmtime (&tstamp); 392 struct tm *tm = gmtime (&tstamp);
369 393
370 New (0, token, 394 New (0, token,
371 1 + 1 // version 395 1 + 1 // version
372 + 12 + 1 // time field sans century 396 + 12 + 1 // time field sans century
373 + strlen (resource) + 1 // ressource 397 + strlen (resource) + 1 // ressource
374 + strlen (trial) + extrarand + 8 + 1 // trial 398 + strlen (trial) + extrarand + 8 + 1 // trial
375 + 1, 399 + 1,
376 char); 400 char);
377 401
378 if (!token) 402 if (!token)
379 croak ("out of memory"); 403 croak ("out of memory");
380 404
381 if (size > 64) 405 if (size > 64)
386 tm->tm_hour, tm->tm_min, tm->tm_sec, 410 tm->tm_hour, tm->tm_min, tm->tm_sec,
387 resource, trial); 411 resource, trial);
388 412
389 if (toklen > 8000) 413 if (toklen > 8000)
390 croak ("token length must be <= 8000 in this implementation\n"); 414 croak ("token length must be <= 8000 in this implementation\n");
415
416 perlinterp_release ();
391 417
392 i = toklen + extrarand; 418 i = toklen + extrarand;
393 while (toklen < i) 419 while (toklen < i)
394 token[toklen++] = rand_char (); 420 token[toklen++] = rand_char ();
395 421
414 do { 440 do {
415 *s = nextenc [*s]; 441 *s = nextenc [*s];
416 } while (*s++ == 'a'); 442 } while (*s++ == 'a');
417 } 443 }
418 444
445 perlinterp_acquire ();
446
419 RETVAL = newSVpvn (token, toklen); 447 RETVAL = newSVpvn (token, toklen);
420} 448}
421 OUTPUT: 449 OUTPUT:
422 RETVAL 450 RETVAL
423 451

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines