Guest User

Untitled

a guest
Jan 28th, 2012
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. #include <time.h>
  7. #include <sys/time.h>
  8.  
  9. static uint64_t
  10. current_time(void)
  11. {
  12.         struct timeval tp;
  13.  
  14.         gettimeofday(&tp, NULL);
  15.         return (uint64_t)tp.tv_sec * 1000000 + tp.tv_usec;
  16. }
  17.  
  18. int
  19. main(int argc, char *argv[])
  20. {
  21.         unsigned char init[256] __attribute__ ((aligned(16)));
  22.         size_t i;
  23.         uint64_t key, ref;
  24.         uint64_t orig;
  25.  
  26.         if (argc < 2) {
  27.                 exit(EXIT_FAILURE);
  28.         }
  29.         ref = strtoull(argv[1], 0, 16);
  30.         for (i = 0; i < 256; i ++) {
  31.                 init[i] = i;
  32.         }
  33.         orig = current_time();
  34.         for (key = 0; key < ((uint64_t)1 << 40); key ++) {
  35.                 unsigned char buf[256] __attribute__ ((aligned(16)));
  36.                 unsigned c;
  37.                 unsigned k0, k1, k2, k3, k4;
  38.                 unsigned j;
  39.                 uint64_t out;
  40.  
  41. #define SWAP(a, b)   do { \
  42.                 unsigned char st = buf[(a)]; \
  43.                 buf[(a)] = buf[(b)]; \
  44.                 buf[(b)] = st; \
  45.         } while (0)
  46.  
  47.                 /*
  48.                  * Key schedule.
  49.                  */
  50.                 k0 = key & 0xFF;
  51.                 k1 = (key >> 8) & 0xFF;
  52.                 k2 = (key >> 16) & 0xFF;
  53.                 k3 = (key >> 24) & 0xFF;
  54.                 k4 = (key >> 32) & 0xFF;
  55.                 memcpy(buf, init, 256);
  56.                 c = 0;
  57.                 for (i = 0; i < 255; i += 5) {
  58.                         c = (c + k0 + buf[i]) & 0xFF;
  59.                         SWAP(i, c);
  60.                         c = (c + k0 + buf[i + 1]) & 0xFF;
  61.                         SWAP(i + 1, c);
  62.                         c = (c + k0 + buf[i + 2]) & 0xFF;
  63.                         SWAP(i + 2, c);
  64.                         c = (c + k0 + buf[i + 3]) & 0xFF;
  65.                         SWAP(i + 3, c);
  66.                         c = (c + k0 + buf[i + 4]) & 0xFF;
  67.                         SWAP(i + 4, c);
  68.                 }
  69.                 c = (c + k0 + buf[255]) & 0xFF;
  70.                 SWAP(255, c);
  71.  
  72.                 /*
  73.                  * Produce 64 bits of output.
  74.                  */
  75.                 j = 0;
  76.                 out = 0;
  77.  
  78. #define STEP(k)   do { \
  79.                 unsigned b0, b1; \
  80.                 b0 = buf[k]; \
  81.                 j = (j + b0) & 0xFF; \
  82.                 b1 = buf[j]; \
  83.                 buf[j] = b0; \
  84.                 buf[k] = b1; \
  85.                 out = (out << 8) | buf[(b0 + b1) & 0xFF]; \
  86.         } while (0)
  87.  
  88.                 STEP(0);
  89.                 STEP(1);
  90.                 STEP(2);
  91.                 STEP(3);
  92.                 STEP(4);
  93.                 STEP(5);
  94.                 STEP(6);
  95.                 STEP(7);
  96.  
  97.                 if (out == ref) {
  98.                         printf("\nkey = %016llx\n", (unsigned long long)key);
  99.                 }
  100.                 if (((key + 1) & 0xFFFFF) == 0) {
  101.                         uint64_t now;
  102.  
  103.                         now = current_time();
  104.                         printf("\r%.2f keys/s    ",
  105.                                 (double)key * 1000000.0 / (now - orig));
  106.                         fflush(stdout);
  107.                 }
  108.         }
  109.         return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment