| 1 | #if defined(__POWERPC__) && defined(__ALTIVEC__) && (!defined(__GNUC__) || !defined(__MACH__)) |
| 2 | #include <altivec.h> |
| 3 | #endif |
| 4 | |
| 5 | #ifdef __CARBON__ |
| 6 | #include <CoreServices/CoreServices.h> |
| 7 | #endif |
| 8 | |
| 9 | #if defined(__POWERPC__) && !defined(__MACH__) && !defined(__UNIX__) && !defined(__CARBON__) |
| 10 | #include <Gestalt.h> |
| 11 | #endif |
| 12 | |
| 13 | #include <time.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <signal.h> |
| 18 | #include <setjmp.h> |
| 19 | #include "random.h" |
| 20 | #include "sha1.h" |
| 21 | |
| 22 | #define BUILD_DLL |
| 23 | #include "libfastmint.h" |
| 24 | |
| 25 | |
| 26 | /* Index into array of available minters */ |
| 27 | static int fastest_minter = -1; |
| 28 | static unsigned int num_minters = 0; |
| 29 | #define MAX_MINTERS 20 |
| 30 | static HC_Minter minters[MAX_MINTERS]; |
| 31 | |
| 32 | const char *encodeAlphabets[] = { |
| 33 | "0123456789ABCDEF", |
| 34 | "0123456789abcdef", |
| 35 | "ABCDEFGHIJKLMNOP", |
| 36 | "abcdefghijklmnop", |
| 37 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/" |
| 38 | }; |
| 39 | |
| 40 | const int EncodeBitRate[] = { 4, 4, 4, 4, 6 }; |
| 41 | |
| 42 | /* Keep track of what the CPU supports */ |
| 43 | static volatile int gIllegalInstructionTrapped = 0; |
| 44 | static jmp_buf gEnv; |
| 45 | int gProcessorSupportFlags = 0; |
| 46 | |
| 47 | /* SHA-1 magic gunge */ |
| 48 | #define H0 0x67452301 |
| 49 | #define H1 0xEFCDAB89 |
| 50 | #define H2 0x98BADCFE |
| 51 | #define H3 0x10325476 |
| 52 | #define H4 0xC3D2E1F0 |
| 53 | static const uInt32 SHA1_IV[ 5 ] = { H0, H1, H2, H3, H4 }; |
| 54 | |
| 55 | /* SIGILL handler */ |
| 56 | static void sig_ill_handler(int sig) |
| 57 | { |
| 58 | sig = sig; |
| 59 | gIllegalInstructionTrapped = 1; |
| 60 | longjmp(gEnv,1); |
| 61 | } |
| 62 | |
| 63 | #if defined(__ALTIVEC__) && !defined(__GNUC__) |
| 64 | #pragma dont_inline on |
| 65 | /* Dummy Altivec operation for testing */ |
| 66 | static void dummy_altivec_fn(void) |
| 67 | { |
| 68 | volatile vector unsigned int v; |
| 69 | v = vec_or(v,v); |
| 70 | } |
| 71 | #pragma dont_inline reset |
| 72 | #endif |
| 73 | |
| 74 | /* Detect whether extended CPU features like Altivec, MMX, etc are supported */ |
| 75 | static void hashcash_detect_features(void) |
| 76 | { |
| 77 | #if defined(__POWERPC__) && defined(__ALTIVEC__) |
| 78 | int hasAltivec = 0; |
| 79 | |
| 80 | #if defined(__UNIX__) || defined(__MACH__) |
| 81 | /* Use generic SIGILL trap handler */ |
| 82 | void *oldhandler; |
| 83 | |
| 84 | gIllegalInstructionTrapped = 0; |
| 85 | oldhandler = signal(SIGILL, sig_ill_handler); |
| 86 | |
| 87 | if(!setjmp(gEnv)) { |
| 88 | #ifdef __GNUC__ |
| 89 | asm volatile ( "vor v0,v0,v0" ); |
| 90 | #else |
| 91 | dummy_altivec_fn(); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | signal(SIGILL, oldhandler); |
| 96 | |
| 97 | hasAltivec = !gIllegalInstructionTrapped; |
| 98 | #else |
| 99 | /* Carbon and MacOS Classic */ |
| 100 | long cpuAttributes; |
| 101 | OSErr err = Gestalt(gestaltPowerPCProcessorFeatures, &cpuAttributes); |
| 102 | if(err == 0) |
| 103 | hasAltivec = ((1 << gestaltPowerPCHasVectorInstructions) & cpuAttributes) != 0; |
| 104 | #endif |
| 105 | |
| 106 | if(hasAltivec) |
| 107 | gProcessorSupportFlags |= HC_CPU_SUPPORTS_ALTIVEC; |
| 108 | else |
| 109 | gProcessorSupportFlags &= ~(HC_CPU_SUPPORTS_ALTIVEC); |
| 110 | #elif defined(__i386__) && defined(__GNUC__) |
| 111 | void *oldhandler; |
| 112 | int hasMMX = 0; |
| 113 | |
| 114 | gIllegalInstructionTrapped = 0; |
| 115 | oldhandler = signal(SIGILL, sig_ill_handler); |
| 116 | |
| 117 | if(!setjmp(gEnv)) { |
| 118 | asm volatile ( |
| 119 | "movl $1, %%eax\n\t" |
| 120 | "push %%ebx\n\t" |
| 121 | "cpuid\n\t" |
| 122 | "andl $0x800000, %%edx\n\t" |
| 123 | "pop %%ebx\n\t" |
| 124 | : "=d" (hasMMX) |
| 125 | : /* no input */ |
| 126 | : "eax", "ecx" |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | signal(SIGILL, oldhandler); |
| 131 | |
| 132 | if(hasMMX && !gIllegalInstructionTrapped) |
| 133 | gProcessorSupportFlags |= HC_CPU_SUPPORTS_MMX; |
| 134 | else |
| 135 | gProcessorSupportFlags &= ~(HC_CPU_SUPPORTS_MMX); |
| 136 | #elif defined(__AMD64__) |
| 137 | gProcessorSupportFlags = HC_CPU_SUPPORTS_MMX; |
| 138 | #else |
| 139 | gProcessorSupportFlags = 0; |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | /* Statically guesstimate the fastest hashcash minting routine. Takes |
| 144 | * into account only the gross hardware architecture and features |
| 145 | * available. Updates fastest_minter. Also initialises and populates |
| 146 | * the available minter array if necessary. |
| 147 | */ |
| 148 | |
| 149 | static const EncodeAlphabet encodings[] = { |
| 150 | EncodeBase64, |
| 151 | EncodeBase64, |
| 152 | EncodeBase64, |
| 153 | EncodeBase64, |
| 154 | EncodeBase64, |
| 155 | EncodeBase64, |
| 156 | EncodeBase64, |
| 157 | EncodeBase64, |
| 158 | EncodeBase64, |
| 159 | EncodeBase64, |
| 160 | EncodeBase64 |
| 161 | }; |
| 162 | |
| 163 | void hashcash_select_minter() |
| 164 | { |
| 165 | static const HC_Mint_Routine funcs[] = { |
| 166 | minter_library, |
| 167 | minter_ansi_compact_1, |
| 168 | minter_ansi_standard_1, |
| 169 | minter_ansi_ultracompact_1, |
| 170 | minter_ansi_compact_2, |
| 171 | minter_ansi_standard_2, |
| 172 | minter_altivec_standard_1, |
| 173 | minter_altivec_compact_2, |
| 174 | minter_altivec_standard_2, |
| 175 | minter_mmx_compact_1, |
| 176 | minter_mmx_standard_1, |
| 177 | NULL }; |
| 178 | static const HC_Mint_Capable_Routine tests[] = { |
| 179 | minter_library_test, |
| 180 | minter_ansi_compact_1_test, |
| 181 | minter_ansi_standard_1_test, |
| 182 | minter_ansi_ultracompact_1_test, |
| 183 | minter_ansi_compact_2_test, |
| 184 | minter_ansi_standard_2_test, |
| 185 | minter_altivec_standard_1_test, |
| 186 | minter_altivec_compact_2_test, |
| 187 | minter_altivec_standard_2_test, |
| 188 | minter_mmx_compact_1_test, |
| 189 | minter_mmx_standard_1_test, |
| 190 | NULL }; |
| 191 | static const char *names[] = { |
| 192 | #if defined( OPENSSL ) |
| 193 | "SHA1 library (openSSL)", |
| 194 | #else |
| 195 | "SHA1 library (hashcash)", |
| 196 | #endif |
| 197 | "ANSI Compact 1-pipe", |
| 198 | "ANSI Standard 1-pipe", |
| 199 | "ANSI Ultra-Compact 1-pipe", |
| 200 | "ANSI Compact 2-pipe", |
| 201 | "ANSI Standard 2-pipe", |
| 202 | "PowerPC Altivec Standard 1x4-pipe", |
| 203 | "PowerPC Altivec Compact 2x4-pipe", |
| 204 | "PowerPC Altivec Standard 2x4-pipe", |
| 205 | "AMD64/x86 MMX Compact 1x2-pipe", |
| 206 | "AMD64/x86 MMX Standard 1x2-pipe", |
| 207 | NULL }; |
| 208 | int i = 0 ; |
| 209 | |
| 210 | /* Populate array */ |
| 211 | if(!num_minters) { |
| 212 | num_minters = (sizeof(funcs) / sizeof(*funcs)) - 1; |
| 213 | if ( num_minters > MAX_MINTERS ) { |
| 214 | fprintf(stderr, |
| 215 | "INTERNAL ERROR: " |
| 216 | "increase size of MAX_MINTERS\n"); |
| 217 | exit(3); /* EXIT_ERROR */ |
| 218 | } |
| 219 | for(i=0; i < num_minters; i++) { |
| 220 | minters[i].name = names[i]; |
| 221 | minters[i].func = funcs[i]; |
| 222 | minters[i].test = tests[i]; |
| 223 | minters[i].encoding = encodings[i]; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* If nothing else works, just use the compact_1 minter on x86 |
| 228 | and standard_1 elsewhere */ |
| 229 | |
| 230 | #ifdef __i386__ |
| 231 | fastest_minter = 1; |
| 232 | #elif defined(__M68000__) |
| 233 | fastest_minter = 3; |
| 234 | #else |
| 235 | fastest_minter = 2; |
| 236 | #endif |
| 237 | |
| 238 | /* Detect actual CPU capabilities */ |
| 239 | hashcash_detect_features(); |
| 240 | |
| 241 | /* See if any of the vectorised minters work, choose the |
| 242 | highest-numbered one that does */ |
| 243 | |
| 244 | for(i=6; i < num_minters; i++) { |
| 245 | if(minters[i].test()) { fastest_minter = i; } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* Do a quick, silent benchmark of the selected backend. Assumes it |
| 250 | * works. */ |
| 251 | unsigned long hashcash_per_sec_calc( void ) |
| 252 | { |
| 253 | static const unsigned int test_bits = 64; |
| 254 | static const char *test_string = |
| 255 | "1:32:040404:[email protected]::0123456789abcdef:00000000"; |
| 256 | static const int test_tail = 52; |
| 257 | unsigned long rate = 0, iter_count = 256; |
| 258 | volatile clock_t begin = 0 , end = 0 , tmp = 0 , res = 0 , taken = 0 ; |
| 259 | double elapsed = 0 , multiple = 0 ; |
| 260 | unsigned char block[SHA1_INPUT_BYTES] = {0}; |
| 261 | int gotbits = 0; |
| 262 | HC_Mint_Routine best_minter_fp; |
| 263 | |
| 264 | /* Ensure a valid minter backend is selected */ |
| 265 | if(!num_minters) { hashcash_select_minter(); } |
| 266 | best_minter_fp = minters[fastest_minter].func; |
| 267 | |
| 268 | /* Determine clock resolution */ |
| 269 | end = clock(); |
| 270 | while((begin = clock()) == end) |
| 271 | ; |
| 272 | while((end = clock()) == begin) |
| 273 | ; |
| 274 | if ( end < begin ) { tmp = begin; begin = end; end = tmp; } |
| 275 | res = end - begin; |
| 276 | |
| 277 | /* where there is poor resolution use this */ |
| 278 | /* less accurate but faster -- otherwise takes 0.5secs */ |
| 279 | |
| 280 | if ( res > 1000 ) { |
| 281 | /* Run minter, with clock running */ |
| 282 | begin = end; |
| 283 | do { |
| 284 | /* set up SHA-1 block */ |
| 285 | strncpy((char*)block, test_string, SHA1_INPUT_BYTES); |
| 286 | block[test_tail] = 0x80; |
| 287 | memset(block+test_tail+1, 0, 59-test_tail); |
| 288 | PUT_WORD(block+60, test_tail << 3); |
| 289 | |
| 290 | best_minter_fp(test_bits, &gotbits, block, |
| 291 | SHA1_IV, test_tail, iter_count, |
| 292 | NULL, NULL, 0, 0); |
| 293 | if ( gotbits >= test_bits) { |
| 294 | /* The benchmark will be inaccurate |
| 295 | if we actually find a preimage! */ |
| 296 | fprintf(stderr, |
| 297 | "Error in hashcash_quickbench(): found preimage while trying to benchmark!\n"); |
| 298 | return 1; |
| 299 | } |
| 300 | rate += iter_count; |
| 301 | end = clock(); |
| 302 | if ( end < begin ) { taken = begin-end; } |
| 303 | else { taken = end-begin; } |
| 304 | } while ( taken < 8*res ); |
| 305 | multiple = CLOCKS_PER_SEC / (double)(8*res); |
| 306 | rate *= multiple; |
| 307 | |
| 308 | return rate; |
| 309 | } |
| 310 | |
| 311 | /* Run increasing lengths of minting until we have sufficient |
| 312 | * elapsed time for precision */ |
| 313 | while(iter_count) { |
| 314 | /* set up SHA-1 block */ |
| 315 | strncpy((char*)block, test_string, SHA1_INPUT_BYTES); |
| 316 | block[test_tail] = 0x80; |
| 317 | memset(block+test_tail+1, 0, 59-test_tail); |
| 318 | PUT_WORD(block+60, test_tail << 3); |
| 319 | |
| 320 | /* Run minter, with clock running */ |
| 321 | end = clock(); |
| 322 | while((begin = clock()) == end) {} |
| 323 | best_minter_fp(test_bits,&gotbits,block,SHA1_IV,test_tail, |
| 324 | iter_count, NULL, NULL, 0, 0); |
| 325 | if ( gotbits >= test_bits) { |
| 326 | /* The benchmark will be inaccurate if we |
| 327 | * actually find a preimage! */ |
| 328 | fprintf(stderr, "Error in hashcash_quickbench(): found preimage while trying to benchmark!\n"); |
| 329 | return 1; |
| 330 | } |
| 331 | end = clock(); |
| 332 | if ( end < begin ) { tmp = begin; begin = end; end = tmp; } |
| 333 | elapsed = (end-begin) / (double) CLOCKS_PER_SEC; |
| 334 | |
| 335 | if(end-begin > (res * 16)) |
| 336 | break; |
| 337 | |
| 338 | iter_count <<= 1; |
| 339 | } |
| 340 | |
| 341 | rate = iter_count / elapsed; |
| 342 | return rate; |
| 343 | } |
| 344 | |
| 345 | /* version of hashcash_per_sec_calc which caches result, so only doing |
| 346 | * the work once. Note: hashcash_use_core will dirty the cache to |
| 347 | * trigger a recalc |
| 348 | */ |
| 349 | |
| 350 | static int cached_per_sec = 0; |
| 351 | |
| 352 | unsigned long hashcash_per_sec(void) |
| 353 | { |
| 354 | static unsigned long cache = 0; |
| 355 | if ( !cached_per_sec ) { |
| 356 | cache = hashcash_per_sec_calc(); |
| 357 | cached_per_sec = 1; |
| 358 | } |
| 359 | return cache; |
| 360 | } |
| 361 | |
| 362 | /* Test and benchmark available hashcash minting backends. Returns |
| 363 | * the speed of the fastest valid routine, and updates fastest_minter |
| 364 | * as appropriate. |
| 365 | */ |
| 366 | unsigned long hashcash_benchtest(int verbose, int core) |
| 367 | { |
| 368 | unsigned long i, a, b; |
| 369 | int best_minter = -1, got_bits = 0; |
| 370 | static const unsigned int test_bits = 22; |
| 371 | static const char *test_string = |
| 372 | "1:22:040404:[email protected]::0123456789abcdef:0000000000"; |
| 373 | static const int test_tail = 52; /* must be less than 56 */ |
| 374 | static const int bit_stats[] = { 8, 10, 16, 20, 22, |
| 375 | 24, 26, 28, 30, 0 }; |
| 376 | unsigned char block[SHA1_INPUT_BYTES+1] = {0}; |
| 377 | volatile clock_t begin = 0, end = 0, tmp = 0; |
| 378 | double elapsed = 0, rate = 0, peak_rate = 0; |
| 379 | SHA1_ctx crypter; |
| 380 | unsigned char hash[SHA1_DIGEST_BYTES] = {0}; |
| 381 | const char *p = NULL , *q = NULL ; |
| 382 | int start = 0, stop = 0; |
| 383 | |
| 384 | /* If minter list isn't valid, make it so */ |
| 385 | hashcash_select_minter(); |
| 386 | |
| 387 | /* print header */ |
| 388 | if(verbose > 0 ) { |
| 389 | printf(" Rate Name (* machine default)\n"); |
| 390 | } |
| 391 | if(verbose >= 3) { printf("\n"); } |
| 392 | |
| 393 | if ( core >= 0 ) { |
| 394 | start = core; stop = start+1; |
| 395 | } else { |
| 396 | start = 0; stop = num_minters; |
| 397 | } |
| 398 | for(i = start; i < stop; i++) { |
| 399 | /* If the minter can't run... */ |
| 400 | if(!minters[i].test()) { |
| 401 | if(verbose >= 2) { |
| 402 | printf(" --- %s (Not available on this machine)\n", minters[i].name); |
| 403 | } |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | if(verbose) { |
| 408 | printf(" %s\r", minters[i].name); |
| 409 | fflush(stdout); |
| 410 | } |
| 411 | |
| 412 | /* set up SHA-1 block */ |
| 413 | strncpy((char*)block, test_string, SHA1_INPUT_BYTES); |
| 414 | block[test_tail] = 0x80; |
| 415 | memset(block+test_tail+1, 0, 59-test_tail); |
| 416 | PUT_WORD(block+60, test_tail << 3); |
| 417 | |
| 418 | /* Run minter, with clock running */ |
| 419 | end = clock(); |
| 420 | while((begin = clock()) == end) {} |
| 421 | minters[i].func(test_bits, &got_bits, block, SHA1_IV, |
| 422 | test_tail, 1 << 30, NULL, NULL, 0, 0); |
| 423 | end = clock(); |
| 424 | if ( end < begin ) { tmp = begin; begin = end; end = tmp; } |
| 425 | elapsed = (end-begin) / (double) CLOCKS_PER_SEC; |
| 426 | |
| 427 | /* Different minter iteration patterns will find |
| 428 | * different solutions */ |
| 429 | /* Verify solution correctness first, using reference |
| 430 | * SHA-1 library */ |
| 431 | |
| 432 | SHA1_Init(&crypter); |
| 433 | SHA1_Update(&crypter, block, test_tail); |
| 434 | SHA1_Final(&crypter, hash); |
| 435 | for(a=0; a < SHA1_DIGEST_BYTES-1 && hash[a] == 0; a++) {} |
| 436 | for(b=0; b < 8 && (hash[a] & 0x80) == 0; b++) { |
| 437 | hash[a] <<= 1; |
| 438 | } |
| 439 | if(got_bits != (a*8)+b || got_bits < test_bits || |
| 440 | block[test_tail] != (unsigned char) 0x80) { |
| 441 | if(verbose) { |
| 442 | printf("ERROR!\n"); |
| 443 | printf(" Wanted %u bits, reported %d bits, got %lu bits.\n", test_bits, got_bits, (a*8)+b); |
| 444 | if(block[test_tail] == (unsigned char) 0x80) { |
| 445 | printf(" End-of-block marker remains intact.\n"); |
| 446 | } else { |
| 447 | printf(" End-of-block marker damaged!\n"); |
| 448 | } |
| 449 | block[test_tail] = 0; |
| 450 | printf(" \"%s\"\n", block); |
| 451 | printf(" Time taken: %.3f\n\n", elapsed); |
| 452 | } |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | /* Use knowledge of encoding alphabet to calculate |
| 457 | iterations taken */ |
| 458 | a = test_tail-8; |
| 459 | b = 0; |
| 460 | p = encodeAlphabets[minters[i].encoding]; |
| 461 | while(a < test_tail && block[a] == '0') { |
| 462 | a++; |
| 463 | } |
| 464 | for( ; a < test_tail; a++) { |
| 465 | q = strchr(p, block[a]); |
| 466 | if(!q) |
| 467 | break; |
| 468 | b = (b * strlen(p)) + (q - p); |
| 469 | } |
| 470 | if(a != test_tail) { |
| 471 | if(verbose) { |
| 472 | printf("ERROR!\n"); |
| 473 | printf(" Unable to parse iteration count.\n"); |
| 474 | printf(" \"%s\"\n", block); |
| 475 | printf(" \"%s\"\n", p); |
| 476 | } |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | /* We know the elapsed time and the iteration count, |
| 481 | so calculate the rate */ |
| 482 | rate = b / elapsed; |
| 483 | if(verbose) { |
| 484 | printf("%9lu %s %c\n", (unsigned long) rate, |
| 485 | minters[i].name, |
| 486 | (i == fastest_minter) ? '*' : ' '); |
| 487 | } |
| 488 | |
| 489 | if(rate > peak_rate) { |
| 490 | peak_rate = rate; |
| 491 | best_minter = i; |
| 492 | } |
| 493 | |
| 494 | /* Optionally print out the stats */ |
| 495 | if(verbose >= 3) { |
| 496 | block[test_tail] = 0; |
| 497 | printf(" Solution: %s\n", block); |
| 498 | printf(" Iterations: %lu\n", b); |
| 499 | printf(" Time taken: %.3f\n\n", elapsed); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | fastest_minter = best_minter; |
| 504 | |
| 505 | if(verbose && best_minter >= 0) { |
| 506 | printf("Best minter: %s (%lu hashes/sec)\n", |
| 507 | minters[best_minter].name, (unsigned long) peak_rate); |
| 508 | } |
| 509 | if(verbose >= 2 && best_minter >= 0) { |
| 510 | printf("Projected average times to mint:\n"); |
| 511 | |
| 512 | for(i = 0; bit_stats[i]; i++) { |
| 513 | elapsed = (1 << bit_stats[i]) / peak_rate; |
| 514 | printf("%3d bits: %9.3f seconds", bit_stats[i], |
| 515 | elapsed); |
| 516 | if(elapsed > 200000) { |
| 517 | printf(" (%.1f days)", elapsed/(3600*24)); |
| 518 | } else if(elapsed > 5000) { |
| 519 | printf(" (%.1f hours)", elapsed/3600); |
| 520 | } else if(elapsed > 100) { |
| 521 | printf(" (%.1f minutes)", elapsed/60); |
| 522 | } else if(elapsed < 0.005) { |
| 523 | printf(" (%.1f microseconds)", elapsed * |
| 524 | 1000000); |
| 525 | } |
| 526 | printf("\n"); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return (unsigned long) peak_rate; |
| 531 | } |
| 532 | |
| 533 | /* Attempt to mint a hashcash token with a given bit-value. |
| 534 | * Will append a random string to token that produces the required |
| 535 | * preimage, then return a pointer to the resultant string in result. |
| 536 | * Caller must free() result buffer after use. |
| 537 | * Returns the number of bits actually minted (may be more or less |
| 538 | * than requested). |
| 539 | */ |
| 540 | |
| 541 | double hashcash_fastmint(const int bits, const char *token, int compress, |
| 542 | char **result, hashcash_callback cb, void* user_args) |
| 543 | { |
| 544 | SHA1_ctx crypter; |
| 545 | unsigned char hash[SHA1_DIGEST_BYTES] = {0}; |
| 546 | unsigned int IV[SHA1_DIGEST_WORDS] = {0}; |
| 547 | unsigned char *buffer = NULL, *block = NULL, c = 0; |
| 548 | unsigned char *last = NULL; |
| 549 | unsigned int buflen = 0, tail = 0, a = 0, b = 0, save_tail = 0; |
| 550 | unsigned long t = 0, loop = 0, iters = 0, i = 0, first = 1; |
| 551 | HC_Mint_Routine best_minter; |
| 552 | double counter = 0, expected = 0; |
| 553 | int gotBits = 0, bit_rate = 6, chars = 0, blocks = 1, oldblocks = 0; |
| 554 | int prevBits = 0; |
| 555 | int* best = &gotBits; /* NB this is needed for CALLBACK macros */ |
| 556 | /* this is to allow this fn to call the same callback macro */ |
| 557 | MINTER_CALLBACK_VARS; |
| 558 | |
| 559 | /* Make sure list of minters is valid */ |
| 560 | if(fastest_minter < 0) { hashcash_select_minter(); } |
| 561 | |
| 562 | /* only the library minter can cope with split blocks */ |
| 563 | if ( compress > 1 ) { fastest_minter = 0; } |
| 564 | |
| 565 | best_minter = minters[fastest_minter].func; |
| 566 | |
| 567 | expected = hashcash_expected_tries( bits ); |
| 568 | |
| 569 | again: |
| 570 | /* Set up string for hashing */ |
| 571 | tail = strlen(token); |
| 572 | buflen = (tail - (tail % SHA1_INPUT_BYTES)) + 2*SHA1_INPUT_BYTES; |
| 573 | buffer = malloc(buflen); |
| 574 | memset(buffer, 0, buflen); |
| 575 | strncpy((char*)buffer, token, buflen); |
| 576 | |
| 577 | /* Add 96 bits of random data */ |
| 578 | t = tail + 16; |
| 579 | for( ; tail < t; tail++) { |
| 580 | random_getbytes(&c, 1); |
| 581 | buffer[tail] = encodeAlphabets[EncodeBase64][c & 0x3f]; |
| 582 | } |
| 583 | #if defined( DEBUG ) |
| 584 | fprintf( stderr, "tail = \"%s\"\n", buffer+tail-16 ); |
| 585 | #endif |
| 586 | |
| 587 | /* Add separator and zeroed count field (for v1 hashcash format) */ |
| 588 | buffer[tail++] = ':'; |
| 589 | save_tail = tail; |
| 590 | |
| 591 | bit_rate = EncodeBitRate[encodings[fastest_minter]]; |
| 592 | #if defined( DEBUG ) |
| 593 | chars = 18/bit_rate; |
| 594 | #else |
| 595 | chars = 31/bit_rate; |
| 596 | #endif |
| 597 | for ( i = compress ? 1 : chars; |
| 598 | i <= chars && (first || gotBits < bits); i++ ) { |
| 599 | first = 0; |
| 600 | tail = save_tail; |
| 601 | t = tail + i; |
| 602 | for( ; tail < t; tail++) { buffer[tail] = '0'; } |
| 603 | switch (compress) { |
| 604 | case 0: /* fast stamps */ |
| 605 | /* Align to optimal counting positions */ |
| 606 | for( ; (tail % SHA1_INPUT_BYTES) != 32 && |
| 607 | (tail % SHA1_INPUT_BYTES) != 52; tail++) { |
| 608 | buffer[tail] = '0'; |
| 609 | } |
| 610 | break; |
| 611 | case 1: /* produce moderately compact stamps */ |
| 612 | /* ensure counting is all within one SHA-1 block */ |
| 613 | for( ; (tail % SHA1_INPUT_BYTES) < i || |
| 614 | (tail % SHA1_INPUT_BYTES) >= 56; tail++) { |
| 615 | buffer[tail] = '0'; |
| 616 | } |
| 617 | break; |
| 618 | default: /* produce very compact stamps */ |
| 619 | oldblocks = blocks; |
| 620 | if ( (tail % SHA1_INPUT_BYTES) < i || |
| 621 | (tail % SHA1_INPUT_BYTES) >= 56 ) { |
| 622 | blocks = 2; /* split across block */ |
| 623 | } |
| 624 | /* no padding at all! */ |
| 625 | } |
| 626 | |
| 627 | /* Hash all but the final block, due to invariance */ |
| 628 | t = tail - (tail % SHA1_INPUT_BYTES); |
| 629 | if ( blocks > 1 && t >= 64 && (tail % SHA1_INPUT_BYTES) < i ) { |
| 630 | t -= 64; |
| 631 | } |
| 632 | SHA1_Init(&crypter); |
| 633 | SHA1_Update(&crypter, buffer, t); |
| 634 | #if defined(OPENSSL) |
| 635 | IV[0]=crypter.h0; |
| 636 | IV[1]=crypter.h1; |
| 637 | IV[2]=crypter.h2; |
| 638 | IV[3]=crypter.h3; |
| 639 | IV[4]=crypter.h4; |
| 640 | #else |
| 641 | for(a=0; a < 5; a++) { IV[a] = crypter.H[a]; } |
| 642 | #endif |
| 643 | block = buffer + t; |
| 644 | |
| 645 | /* Fill in the padding and trailer */ |
| 646 | buffer[tail] = 0x80; |
| 647 | /* if number of blocks change get rid of old padding */ |
| 648 | if ( blocks > oldblocks) { |
| 649 | /* note only need 7 chars as 1 char wider */ |
| 650 | memset(buffer+tail+1,0,7); |
| 651 | } |
| 652 | PUT_WORD(block+(blocks>1?64:0)+60, tail << 3); |
| 653 | tail -= t; |
| 654 | |
| 655 | /* Run the minter over the last block */ |
| 656 | loop=best_minter(bits, &gotBits, block, IV, tail, |
| 657 | 0x1U << (i*bit_rate), cb, |
| 658 | user_args,counter,expected); |
| 659 | |
| 660 | if (loop==0) { |
| 661 | free(buffer); |
| 662 | if ( last ) { free( last ); } |
| 663 | if (*best==-1) {return -1;} |
| 664 | else { return 0; } |
| 665 | } |
| 666 | |
| 667 | if ( gotBits == 0 || gotBits > prevBits ) { |
| 668 | block[tail] = 0; |
| 669 | prevBits = gotBits; |
| 670 | t = strlen( (char*)buffer ); |
| 671 | last = realloc( last, t+1 ); |
| 672 | strncpy( (char*)last, (char*)buffer, t+1 ); |
| 673 | } |
| 674 | |
| 675 | counter += (double)loop; |
| 676 | } |
| 677 | |
| 678 | /* if we succeeded call the callback also */ |
| 679 | if ( gotBits >= bits ) { MINTER_CALLBACK(); } |
| 680 | |
| 681 | /* Verify solution using reference library */ |
| 682 | SHA1_Init(&crypter); |
| 683 | SHA1_Update(&crypter, last, strlen( (char*)last )); |
| 684 | SHA1_Final(&crypter, hash); |
| 685 | |
| 686 | for(a=0; a < SHA1_DIGEST_BYTES-1 && hash[a] == 0; a++) |
| 687 | ; |
| 688 | for(b=0; b < 8 && (hash[a] & 0x80) == 0; b++) |
| 689 | hash[a] <<= 1; |
| 690 | b += a*8; |
| 691 | |
| 692 | /* The minter appears to be broken! */ |
| 693 | if(b < gotBits) { |
| 694 | fprintf(stderr, "ERROR: requested %d bits, reported %d bits, got %d bits using %s minter: \"%s\"\n", |
| 695 | bits, gotBits, b, minters[fastest_minter].name, |
| 696 | last ); |
| 697 | exit(3); |
| 698 | } |
| 699 | |
| 700 | /* The minter might not be able to detect unusually large |
| 701 | * (32+) bit counts, so we're allowed to give it another try. |
| 702 | */ |
| 703 | if(b < bits) { |
| 704 | /* fprintf( stderr, "buffer = %s\n", buffer ); |
| 705 | fprintf( stderr, "wrapped\n" ); */ |
| 706 | free(buffer); |
| 707 | goto again; |
| 708 | } |
| 709 | |
| 710 | *result = (char*)buffer; |
| 711 | if ( last ) { free( last ); } |
| 712 | return counter; |
| 713 | } |
| 714 | |
| 715 | int hashcash_core(void) |
| 716 | { |
| 717 | if (!num_minters) { hashcash_select_minter(); } |
| 718 | return fastest_minter; |
| 719 | } |
| 720 | |
| 721 | int hashcash_use_core(int core) |
| 722 | { |
| 723 | if (!num_minters) { hashcash_select_minter(); } |
| 724 | if ( core < 0 || core >= num_minters ) { return -1; } |
| 725 | if ( !minters[core].test() ) { return 0; } |
| 726 | fastest_minter = core; |
| 727 | /* force recalc */ |
| 728 | cached_per_sec = 0; |
| 729 | return 1; |
| 730 | } |
| 731 | |
| 732 | const char* hashcash_core_name(int core) |
| 733 | { |
| 734 | if (!num_minters) { hashcash_select_minter(); } |
| 735 | if ( core < 0 || core >= num_minters ) { |
| 736 | return "undefined core"; |
| 737 | } |
| 738 | return minters[core].name; |
| 739 | } |