Advertisement
bitpwner

RC4 2nd Byte Bias Demo, Multithreaded

Aug 9th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include <sys/time.h>
  7.  
  8. #define KEY_LENGTH            16
  9. #define ITERATIONS            16777216
  10. #define THREAD_COUNT          4
  11. #define ITERATIONS_PER_THREAD ITERATIONS/THREAD_COUNT
  12.  
  13. void ksa(int state[], int key[]) {
  14.     int i,j=0,t;
  15.     for (i=0; i<256; ++i)
  16.         state[i] = i;
  17.     for (i=0; i<256; ++i) {
  18.         j = (j + state[i] + key[i % KEY_LENGTH]) % 256;
  19.         t = state[i];
  20.         state[i] = state[j];
  21.         state[j] = t;
  22.     }
  23. }
  24.  
  25. void prga(int state[], int out[]) {  
  26.     int i=0,j=0,x,t;
  27.     for (x=0; x<256; ++x) {
  28.         i = (i+1) % 256;
  29.         j = (j+state[i]) % 256;
  30.         t = state[i];
  31.         state[i] = state[j];
  32.         state[j] = t;
  33.         out[x] = state[(state[i] + state[j]) % 256];
  34.     }
  35. }
  36.  
  37. void makeRandomKey(int key[]) {
  38.     int i;
  39.     for (i=0; i<KEY_LENGTH; ++i)
  40.         key[i] = rand() % 256;
  41. }
  42.  
  43. void populateOccurances(int* occurances) {
  44.     int state[256];
  45.     int out[256];
  46.     int key[KEY_LENGTH];
  47.     int i,j;
  48.     srand(time(NULL));
  49.     for (i=0; i<ITERATIONS_PER_THREAD; ++i) {
  50.         makeRandomKey(key);
  51.         ksa(state, key);
  52.         prga(state, out);
  53.         for (j=0; j<256; ++j)
  54.             ++occurances[j*256+out[j]];
  55.     }
  56. }
  57.  
  58. static void *populateOccurancesThread(void *p) {
  59.     int* occurances = (int*) p;
  60.     populateOccurances(occurances);
  61.     pthread_exit(NULL);
  62. }
  63.  
  64. int main(int argc, char *argv[]) {
  65.  
  66.     struct timeval time_started, time_now, time_diff;
  67.     gettimeofday(&time_started, NULL);
  68.  
  69.     int occurances[THREAD_COUNT][256*256];
  70.     int bytePosition,charOccurance;
  71.     int i,k;
  72.     pthread_t threads[THREAD_COUNT];
  73.  
  74.     for (k=0; k<THREAD_COUNT; ++k)
  75.         for (i=0; i<256*256; ++i)
  76.             occurances[k][i] = 0;
  77.    
  78.     for (k=0; k<THREAD_COUNT; ++k) {
  79.         int rc = pthread_create(&threads[k], NULL, populateOccurancesThread, (void*)&occurances[k]);
  80.         if (rc) {
  81.             printf("ERROR; return code from pthread_create() is %d\n", rc);
  82.             exit(-1);
  83.         }
  84.     }
  85.  
  86.     for (k=0; k<THREAD_COUNT; ++k) {
  87.         void * status;
  88.         int rc = pthread_join(threads[k], &status);
  89.         if (rc) {
  90.             printf("ERROR: return code from pthread_join() is %d\n", rc);
  91.             exit(-1);
  92.         }
  93.     }
  94.    
  95.     for (bytePosition=0; bytePosition<256; ++bytePosition) {
  96.         printf("%d,", bytePosition);
  97.     }
  98.  
  99.     printf("\n");
  100.     for (charOccurance=0; charOccurance<256; ++charOccurance) {
  101.         for (bytePosition=0; bytePosition<256; ++bytePosition) {
  102.             int occuranceSum = 0;
  103.             for (k=0; k<THREAD_COUNT; ++k) {
  104.                 occuranceSum += occurances[k][charOccurance*256+bytePosition];
  105.             }
  106.             printf("%d,", occuranceSum);
  107.         }
  108.         printf("\n");
  109.     }
  110.  
  111.     gettimeofday(&time_now, NULL);
  112.     timersub(&time_now, &time_started, &time_diff);
  113.     printf("Time taken,%ld.%.6ld s\n", time_diff.tv_sec, time_diff.tv_usec);
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement