1 /* 2 * stat-driver.c 3 * 4 * test driver for the stat_test functions 5 * 6 * David A. McGrew 7 * Cisco Systems, Inc. 8 */ 9 10 11 #include <stdio.h> /* for printf() */ 12 13 #include "err.h" 14 #include "stat.h" 15 16 #include "cipher.h" 17 18 typedef struct { 19 void *state; 20 } random_source_t; 21 22 err_status_t 23 random_source_alloc(void); 24 25 void 26 err_check(err_status_t s) { 27 if (s) { 28 printf("error (code %d)\n", s); 29 exit(1); 30 } 31 } 32 33 int 34 main (int argc, char *argv[]) { 35 uint8_t buffer[2500]; 36 unsigned int buf_len = 2500; 37 int i, j; 38 extern cipher_type_t aes_icm; 39 cipher_t *c; 40 uint8_t key[30] = { 41 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 42 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 43 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 44 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 45 }; 46 v128_t nonce; 47 int num_trials = 500; 48 int num_fail; 49 50 printf("statistical tests driver\n"); 51 52 for (i=0; i < 2500; i++) 53 buffer[i] = 0; 54 55 /* run tests */ 56 printf("running stat_tests on all-null buffer, expecting failure\n"); 57 printf("monobit %d\n", stat_test_monobit(buffer)); 58 printf("poker %d\n", stat_test_poker(buffer)); 59 printf("runs %d\n", stat_test_runs(buffer)); 60 61 for (i=0; i < 2500; i++) 62 buffer[i] = rand(); 63 printf("running stat_tests on rand(), expecting success\n"); 64 printf("monobit %d\n", stat_test_monobit(buffer)); 65 printf("poker %d\n", stat_test_poker(buffer)); 66 printf("runs %d\n", stat_test_runs(buffer)); 67 68 printf("running stat_tests on AES-128-ICM, expecting success\n"); 69 /* set buffer to cipher output */ 70 for (i=0; i < 2500; i++) 71 buffer[i] = 0; 72 err_check(cipher_type_alloc(&aes_icm, &c, 30)); 73 err_check(cipher_init(c, key, direction_encrypt)); 74 err_check(cipher_set_iv(c, &nonce)); 75 err_check(cipher_encrypt(c, buffer, &buf_len)); 76 /* run tests on cipher outout */ 77 printf("monobit %d\n", stat_test_monobit(buffer)); 78 printf("poker %d\n", stat_test_poker(buffer)); 79 printf("runs %d\n", stat_test_runs(buffer)); 80 81 printf("runs test (please be patient): "); 82 fflush(stdout); 83 num_fail = 0; 84 v128_set_to_zero(&nonce); 85 for(j=0; j < num_trials; j++) { 86 for (i=0; i < 2500; i++) 87 buffer[i] = 0; 88 nonce.v32[3] = i; 89 err_check(cipher_set_iv(c, &nonce)); 90 err_check(cipher_encrypt(c, buffer, &buf_len)); 91 if (stat_test_runs(buffer)) { 92 num_fail++; 93 } 94 } 95 96 printf("%d failures in %d tests\n", num_fail, num_trials); 97 printf("(nota bene: a small fraction of stat_test failures does not \n" 98 "indicate that the random source is invalid)\n"); 99 100 return 0; 101 } 102