ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Digest-Hashcash/Hashcash.xs
Revision: 1.7
Committed: Tue Jul 21 05:01:01 2015 UTC (9 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-1_0
Changes since 1.6: +16 -6 lines
Log Message:
1.0

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5     #include <time.h>
6     #include <stdlib.h>
7     #include <stdint.h>
8    
9 root 1.7 #include "perlmulticore.h"
10    
11 root 1.1 /* NIST Secure Hash Algorithm */
12     /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
13     /* from Peter C. Gutmann's implementation as found in */
14     /* Applied Cryptography by Bruce Schneier */
15     /* Further modifications to include the "UNRAVEL" stuff, below */
16    
17     /* This code is in the public domain */
18    
19     /* pcg: I was tempted to just rip this code off, after all, if you don't
20     * demand anything I am inclined not to give anything. *Sigh* something
21     * kept me from doing it, so here's the truth: I took this code from the
22     * SHA1 perl module, since it looked reasonably well-crafted. I modified
23     * it here and there, though.
24     */
25    
26 root 1.5 /*
27     * we have lots of micro-optimizations here, this is just for toying
28     * around...
29     */
30    
31 root 1.1 /* don't expect _too_ much from compilers for now. */
32 root 1.2 #if __GNUC__ > 2
33 root 1.1 # define restrict __restrict__
34 root 1.2 # define inline __inline__
35     # ifdef __i386
36     # define GCCX86ASM 1
37     # endif
38 root 1.1 #elif __STDC_VERSION__ < 199900
39     # define restrict
40 root 1.2 # define inline
41 root 1.1 #endif
42    
43 root 1.5 #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    
55 root 1.1 /* Useful defines & typedefs */
56    
57     #if defined(U64TYPE) && (defined(USE_64_BIT_INT) || ((BYTEORDER != 0x1234) && (BYTEORDER != 0x4321)))
58     typedef U64TYPE ULONG;
59 root 1.4 # if BYTEORDER == 0x1234
60     # undef BYTEORDER
61     # define BYTEORDER 0x12345678
62     # elif BYTEORDER == 0x4321
63     # undef BYTEORDER
64 root 1.7 # define BYTEORDER 0x87654321
65 root 1.4 # endif
66 root 1.1 #else
67     typedef uint_fast32_t ULONG; /* 32-or-more-bit quantity */
68     #endif
69    
70 root 1.2 #if GCCX86ASM
71     # define zprefix(n) ({ int _r; __asm__ ("bsrl %1, %0" : "=r" (_r) : "r" (n)); 31 - _r ; })
72 root 1.4 #elif __GNUC__ > 2 && __GNUC_MINOR__ > 3
73     # define zprefix(n) (__extension__ ({ uint32_t n__ = (n); n ? __builtin_clz (n) : 32; }))
74 root 1.2 #else
75 root 1.5 static int a_const zprefix (ULONG n)
76 root 1.2 {
77     static char zp[256] =
78     {
79     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
80     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
81     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
86     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
87     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95     };
96    
97     return
98     n > 0xffffff ? zp[n >> 24]
99     : n > 0xffff ? 8 + zp[n >> 16]
100     : n > 0xff ? 16 + zp[n >> 8]
101     : 24 + zp[n];
102     }
103     #endif
104    
105 root 1.1 #define SHA_BLOCKSIZE 64
106     #define SHA_DIGESTSIZE 20
107    
108     typedef struct {
109     ULONG digest[5]; /* message digest */
110     ULONG count; /* 32-bit bit count */
111 root 1.2 int local; /* unprocessed amount in data */
112 root 1.1 U8 data[SHA_BLOCKSIZE]; /* SHA data buffer */
113     } SHA_INFO;
114    
115    
116     /* SHA f()-functions */
117     #define f1(x,y,z) ((x & y) | (~x & z))
118     #define f2(x,y,z) (x ^ y ^ z)
119     #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
120     #define f4(x,y,z) (x ^ y ^ z)
121    
122     /* SHA constants */
123     #define CONST1 0x5a827999L
124     #define CONST2 0x6ed9eba1L
125     #define CONST3 0x8f1bbcdcL
126     #define CONST4 0xca62c1d6L
127    
128     /* truncate to 32 bits -- should be a null op on 32-bit machines */
129     #define T32(x) ((x) & 0xffffffffL)
130    
131     /* 32-bit rotate */
132     #define R32(x,n) T32(((x << n) | (x >> (32 - n))))
133    
134     /* specific cases, for when the overall rotation is unraveled */
135     #define FA(n) \
136     T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); B = R32(B,30)
137    
138     #define FB(n) \
139     E = T32(R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n); A = R32(A,30)
140    
141     #define FC(n) \
142     D = T32(R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n); T = R32(T,30)
143    
144     #define FD(n) \
145     C = T32(R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n); E = R32(E,30)
146    
147     #define FE(n) \
148     B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30)
149    
150     #define FT(n) \
151     A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30)
152    
153 root 1.5 static void a_regparm(1) sha_transform(SHA_INFO *restrict sha_info)
154 root 1.1 {
155     int i;
156 root 1.5 U8 *restrict dp;
157 root 1.2 ULONG T, A, B, C, D, E, W[80], *restrict WP;
158 root 1.1
159     dp = sha_info->data;
160    
161     #if BYTEORDER == 0x1234
162     assert(sizeof(ULONG) == 4);
163 root 1.2 # ifdef HAS_NTOHL
164     for (i = 0; i < 16; ++i) {
165     T = *((ULONG *) dp);
166     dp += 4;
167     W[i] = ntohl (T);
168     }
169     # else
170 root 1.1 for (i = 0; i < 16; ++i) {
171     T = *((ULONG *) dp);
172     dp += 4;
173     W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
174     ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
175     }
176 root 1.2 # endif
177     #elif BYTEORDER == 0x4321
178 root 1.1 assert(sizeof(ULONG) == 4);
179     for (i = 0; i < 16; ++i) {
180     T = *((ULONG *) dp);
181     dp += 4;
182     W[i] = T32(T);
183     }
184 root 1.2 #elif BYTEORDER == 0x12345678
185 root 1.1 assert(sizeof(ULONG) == 8);
186     for (i = 0; i < 16; i += 2) {
187     T = *((ULONG *) dp);
188     dp += 8;
189     W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
190     ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
191     T >>= 32;
192     W[i+1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
193     ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
194     }
195 root 1.2 #elif BYTEORDER == 0x87654321
196 root 1.1 assert(sizeof(ULONG) == 8);
197     for (i = 0; i < 16; i += 2) {
198     T = *((ULONG *) dp);
199     dp += 8;
200     W[i] = T32(T >> 32);
201     W[i+1] = T32(T);
202     }
203 root 1.2 #else
204     #error Unknown byte order -- you need to add code here
205 root 1.1 #endif
206    
207 root 1.2 for (i = 16; i < 80; ++i)
208     {
209     T = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
210     W[i] = R32(T,1);
211     }
212 root 1.1
213     A = sha_info->digest[0];
214     B = sha_info->digest[1];
215     C = sha_info->digest[2];
216     D = sha_info->digest[3];
217     E = sha_info->digest[4];
218 root 1.2
219 root 1.1 WP = W;
220     FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
221     FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
222     FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
223     FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
224     FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
225     FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
226     FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
227     FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
228 root 1.2
229 root 1.1 sha_info->digest[0] = T32(sha_info->digest[0] + E);
230     sha_info->digest[1] = T32(sha_info->digest[1] + T);
231     sha_info->digest[2] = T32(sha_info->digest[2] + A);
232     sha_info->digest[3] = T32(sha_info->digest[3] + B);
233     sha_info->digest[4] = T32(sha_info->digest[4] + C);
234     }
235    
236     /* initialize the SHA digest */
237    
238 root 1.2 static void sha_init(SHA_INFO *restrict sha_info)
239 root 1.1 {
240     sha_info->digest[0] = 0x67452301L;
241     sha_info->digest[1] = 0xefcdab89L;
242     sha_info->digest[2] = 0x98badcfeL;
243     sha_info->digest[3] = 0x10325476L;
244     sha_info->digest[4] = 0xc3d2e1f0L;
245     sha_info->count = 0L;
246     sha_info->local = 0;
247     }
248    
249     /* update the SHA digest */
250    
251 root 1.2 static void sha_update(SHA_INFO *restrict sha_info, U8 *restrict buffer, int count)
252 root 1.1 {
253     int i;
254    
255 root 1.2 sha_info->count += count;
256 root 1.1 if (sha_info->local) {
257     i = SHA_BLOCKSIZE - sha_info->local;
258     if (i > count) {
259     i = count;
260     }
261     memcpy(((U8 *) sha_info->data) + sha_info->local, buffer, i);
262     count -= i;
263     buffer += i;
264     sha_info->local += i;
265     if (sha_info->local == SHA_BLOCKSIZE) {
266     sha_transform(sha_info);
267     } else {
268     return;
269     }
270     }
271     while (count >= SHA_BLOCKSIZE) {
272     memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
273     buffer += SHA_BLOCKSIZE;
274     count -= SHA_BLOCKSIZE;
275     sha_transform(sha_info);
276     }
277     memcpy(sha_info->data, buffer, count);
278     sha_info->local = count;
279     }
280    
281     /* finish computing the SHA digest */
282 root 1.2 static int sha_final(SHA_INFO *sha_info)
283 root 1.1 {
284 root 1.2 int count = sha_info->count;
285     int local = sha_info->local;
286 root 1.1
287 root 1.2 sha_info->data[local] = 0x80;
288 root 1.1
289 root 1.2 if (sha_info->local >= SHA_BLOCKSIZE - 8) {
290     memset(sha_info->data + local + 1, 0, SHA_BLOCKSIZE - 1 - local);
291     sha_transform(sha_info);
292     memset(sha_info->data, 0, SHA_BLOCKSIZE - 2);
293     } else {
294     memset(sha_info->data + local + 1, 0, SHA_BLOCKSIZE - 3 - local);
295     }
296    
297     sha_info->data[62] = count >> 5;
298     sha_info->data[63] = count << 3;
299    
300     sha_transform (sha_info);
301    
302     return sha_info->digest[0]
303     ? zprefix (sha_info->digest[0])
304     : zprefix (sha_info->digest[1]) + 32;
305 root 1.1 }
306    
307     #define TRIALCHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./;<=>?@[]{}^_|"
308    
309 root 1.7 static char
310     nextenc[256];
311 root 1.1
312 root 1.7 static char
313     rand_char ()
314 root 1.1 {
315 root 1.7 return TRIALCHAR[(int)(Drand01 () * sizeof (TRIALCHAR))];
316 root 1.1 }
317    
318 root 1.2 typedef double (*NVTime)(void);
319    
320 root 1.7 static double
321     simple_nvtime (void)
322 root 1.2 {
323     return time (0);
324     }
325    
326 root 1.7 static NVTime
327     get_nvtime (void)
328 root 1.1 {
329 root 1.2 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
330    
331     if (svp && SvIOK(*svp))
332     return INT2PTR(NVTime, SvIV(*svp));
333     else
334     return simple_nvtime;
335 root 1.1
336     }
337    
338     MODULE = Digest::Hashcash PACKAGE = Digest::Hashcash
339    
340     BOOT:
341     {
342     int i;
343    
344     for (i = 0; i < sizeof (TRIALCHAR); i++)
345     nextenc[TRIALCHAR[i]] = TRIALCHAR[(i + 1) % sizeof (TRIALCHAR)];
346     }
347    
348     PROTOTYPES: ENABLE
349    
350 root 1.2 # could be improved quite a bit in accuracy
351     NV
352     _estimate_rounds ()
353 root 1.1 CODE:
354 root 1.3 {
355 root 1.2 char data[40];
356     NVTime nvtime = get_nvtime ();
357     NV t1, t2, t;
358     int count = 0;
359     SHA_INFO ctx;
360    
361     t = nvtime ();
362     do {
363     t1 = nvtime ();
364     } while (t == t1);
365    
366     t = t2 = nvtime ();
367     do {
368     volatile int i;
369     sha_init (&ctx);
370     sha_update (&ctx, data, sizeof (data));
371     i = sha_final (&ctx);
372    
373     if (!(++count & 1023))
374     t2 = nvtime ();
375    
376     } while (t == t2);
377    
378     RETVAL = (NV)count / (t2 - t1);
379 root 1.3 }
380 root 1.1 OUTPUT:
381     RETVAL
382    
383     SV *
384 root 1.2 _gentoken (int size, IV timestamp, char *resource, char *trial = "", int extrarand = 0)
385 root 1.1 CODE:
386 root 1.3 {
387 root 1.1 SHA_INFO ctx1, ctx;
388     char *token, *seq, *s;
389     int toklen, i;
390     time_t tstamp = timestamp ? timestamp : time (0);
391     struct tm *tm = gmtime (&tstamp);
392    
393     New (0, token,
394 root 1.6 1 + 1 // version
395     + 12 + 1 // time field sans century
396     + strlen (resource) + 1 // ressource
397     + strlen (trial) + extrarand + 8 + 1 // trial
398     + 1,
399     char);
400 root 1.1
401     if (!token)
402     croak ("out of memory");
403    
404 root 1.2 if (size > 64)
405     croak ("size must be <= 64 in this implementation\n");
406 root 1.1
407     toklen = sprintf (token, "%d:%02d%02d%02d%02d%02d%02d:%s:%s",
408     0, tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
409     tm->tm_hour, tm->tm_min, tm->tm_sec,
410     resource, trial);
411    
412 root 1.2 if (toklen > 8000)
413     croak ("token length must be <= 8000 in this implementation\n");
414    
415 root 1.7 perlinterp_release ();
416    
417 root 1.1 i = toklen + extrarand;
418     while (toklen < i)
419     token[toklen++] = rand_char ();
420    
421     sha_init (&ctx1);
422     sha_update (&ctx1, token, toklen);
423    
424     seq = token + toklen;
425     i += 8;
426     while (toklen < i)
427     token[toklen++] = rand_char ();
428    
429     for (;;)
430     {
431     ctx = ctx1; // this "optimization" can help a lot for longer resource strings
432     sha_update (&ctx, seq, 8);
433 root 1.2 i = sha_final (&ctx);
434 root 1.1
435 root 1.2 if (i >= size)
436 root 1.1 break;
437    
438     s = seq;
439     do {
440     *s = nextenc [*s];
441     } while (*s++ == 'a');
442     }
443    
444 root 1.7 perlinterp_acquire ();
445    
446 root 1.1 RETVAL = newSVpvn (token, toklen);
447 root 1.3 }
448 root 1.1 OUTPUT:
449     RETVAL
450    
451     int
452     _prefixlen (SV *tok)
453     CODE:
454 root 1.3 {
455 root 1.1 STRLEN toklen;
456     char *token = SvPV (tok, toklen);
457     SHA_INFO ctx;
458    
459     sha_init (&ctx);
460     sha_update (&ctx, token, toklen);
461 root 1.2 RETVAL = sha_final (&ctx);
462 root 1.3 }
463 root 1.1 OUTPUT:
464     RETVAL
465    
466