OLD | NEW |
(Empty) | |
| 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[46] = { |
| 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, 0x06, 0x07, |
| 45 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 46 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 |
| 47 }; |
| 48 v128_t nonce; |
| 49 int num_trials = 500; |
| 50 int num_fail; |
| 51 |
| 52 printf("statistical tests driver\n"); |
| 53 |
| 54 for (i=0; i < 2500; i++) |
| 55 buffer[i] = 0; |
| 56 |
| 57 /* run tests */ |
| 58 printf("running stat_tests on all-null buffer, expecting failure\n"); |
| 59 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 60 printf("poker %d\n", stat_test_poker(buffer)); |
| 61 printf("runs %d\n", stat_test_runs(buffer)); |
| 62 |
| 63 for (i=0; i < 2500; i++) |
| 64 buffer[i] = rand(); |
| 65 printf("running stat_tests on rand(), expecting success\n"); |
| 66 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 67 printf("poker %d\n", stat_test_poker(buffer)); |
| 68 printf("runs %d\n", stat_test_runs(buffer)); |
| 69 |
| 70 printf("running stat_tests on AES-128-ICM, expecting success\n"); |
| 71 /* set buffer to cipher output */ |
| 72 for (i=0; i < 2500; i++) |
| 73 buffer[i] = 0; |
| 74 err_check(cipher_type_alloc(&aes_icm, &c, 30)); |
| 75 err_check(cipher_init(c, key, direction_encrypt)); |
| 76 err_check(cipher_set_iv(c, &nonce)); |
| 77 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 78 /* run tests on cipher outout */ |
| 79 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 80 printf("poker %d\n", stat_test_poker(buffer)); |
| 81 printf("runs %d\n", stat_test_runs(buffer)); |
| 82 |
| 83 printf("runs test (please be patient): "); |
| 84 fflush(stdout); |
| 85 num_fail = 0; |
| 86 v128_set_to_zero(&nonce); |
| 87 for(j=0; j < num_trials; j++) { |
| 88 for (i=0; i < 2500; i++) |
| 89 buffer[i] = 0; |
| 90 nonce.v32[3] = i; |
| 91 err_check(cipher_set_iv(c, &nonce)); |
| 92 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 93 if (stat_test_runs(buffer)) { |
| 94 num_fail++; |
| 95 } |
| 96 } |
| 97 |
| 98 printf("%d failures in %d tests\n", num_fail, num_trials); |
| 99 printf("(nota bene: a small fraction of stat_test failures does not \n" |
| 100 "indicate that the random source is invalid)\n"); |
| 101 |
| 102 err_check(cipher_dealloc(c)); |
| 103 |
| 104 printf("running stat_tests on AES-256-ICM, expecting success\n"); |
| 105 /* set buffer to cipher output */ |
| 106 for (i=0; i < 2500; i++) |
| 107 buffer[i] = 0; |
| 108 err_check(cipher_type_alloc(&aes_icm, &c, 46)); |
| 109 err_check(cipher_init(c, key, direction_encrypt)); |
| 110 err_check(cipher_set_iv(c, &nonce)); |
| 111 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 112 /* run tests on cipher outout */ |
| 113 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 114 printf("poker %d\n", stat_test_poker(buffer)); |
| 115 printf("runs %d\n", stat_test_runs(buffer)); |
| 116 |
| 117 printf("runs test (please be patient): "); |
| 118 fflush(stdout); |
| 119 num_fail = 0; |
| 120 v128_set_to_zero(&nonce); |
| 121 for(j=0; j < num_trials; j++) { |
| 122 for (i=0; i < 2500; i++) |
| 123 buffer[i] = 0; |
| 124 nonce.v32[3] = i; |
| 125 err_check(cipher_set_iv(c, &nonce)); |
| 126 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 127 if (stat_test_runs(buffer)) { |
| 128 num_fail++; |
| 129 } |
| 130 } |
| 131 |
| 132 printf("%d failures in %d tests\n", num_fail, num_trials); |
| 133 printf("(nota bene: a small fraction of stat_test failures does not \n" |
| 134 "indicate that the random source is invalid)\n"); |
| 135 |
| 136 err_check(cipher_dealloc(c)); |
| 137 |
| 138 return 0; |
| 139 } |
OLD | NEW |