| OLD | NEW |
| 1 /* | 1 /* |
| 2 * stat-driver.c | 2 * stat-driver.c |
| 3 * | 3 * |
| 4 * test driver for the stat_test functions | 4 * test driver for the stat_test functions |
| 5 * | 5 * |
| 6 * David A. McGrew | 6 * David A. McGrew |
| 7 * Cisco Systems, Inc. | 7 * Cisco Systems, Inc. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /* | 10 /* |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 42 * OF THE POSSIBILITY OF SUCH DAMAGE. | 42 * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 * | 43 * |
| 44 */ | 44 */ |
| 45 | 45 |
| 46 #ifdef HAVE_CONFIG_H |
| 47 #include <config.h> |
| 48 #endif |
| 49 |
| 46 #include <stdio.h> /* for printf() */ | 50 #include <stdio.h> /* for printf() */ |
| 47 | 51 |
| 48 #include "err.h" | 52 #include "err.h" |
| 49 #include "stat.h" | 53 #include "stat.h" |
| 54 #include "srtp.h" |
| 50 | 55 |
| 51 #include "cipher.h" | 56 #include "cipher.h" |
| 52 | 57 |
| 53 typedef struct { | 58 typedef struct { |
| 54 void *state; | 59 void *state; |
| 55 } random_source_t; | 60 } random_source_t; |
| 56 | 61 |
| 57 err_status_t | 62 err_status_t |
| 58 random_source_alloc(void); | 63 random_source_alloc(void); |
| 59 | 64 |
| 60 void | 65 void |
| 61 err_check(err_status_t s) { | 66 err_check(err_status_t s) { |
| 62 if (s) { | 67 if (s) { |
| 63 printf("error (code %d)\n", s); | 68 printf("error (code %d)\n", s); |
| 64 exit(1); | 69 exit(1); |
| 65 } | 70 } |
| 66 } | 71 } |
| 67 | 72 |
| 68 int | 73 int |
| 69 main (int argc, char *argv[]) { | 74 main (int argc, char *argv[]) { |
| 70 uint8_t buffer[2500]; | 75 uint8_t buffer[2532]; |
| 71 unsigned int buf_len = 2500; | 76 unsigned int buf_len = 2500; |
| 72 int i, j; | 77 int i, j; |
| 73 extern cipher_type_t aes_icm; | 78 extern cipher_type_t aes_icm; |
| 79 #ifdef OPENSSL |
| 80 extern cipher_type_t aes_gcm_128_openssl; |
| 81 extern cipher_type_t aes_gcm_256_openssl; |
| 82 #endif |
| 74 cipher_t *c; | 83 cipher_t *c; |
| 75 uint8_t key[46] = { | 84 uint8_t key[46] = { |
| 76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 85 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 77 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 86 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 87 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 79 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 88 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 80 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 89 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 81 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 | 90 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 |
| 82 }; | 91 }; |
| 83 v128_t nonce; | 92 v128_t nonce; |
| 84 int num_trials = 500; | 93 int num_trials = 500; |
| 85 int num_fail; | 94 int num_fail; |
| 86 | 95 |
| 87 printf("statistical tests driver\n"); | 96 printf("statistical tests driver\n"); |
| 88 | 97 |
| 98 v128_set_to_zero(&nonce); |
| 89 for (i=0; i < 2500; i++) | 99 for (i=0; i < 2500; i++) |
| 90 buffer[i] = 0; | 100 buffer[i] = 0; |
| 91 | 101 |
| 92 /* run tests */ | 102 /* run tests */ |
| 93 printf("running stat_tests on all-null buffer, expecting failure\n"); | 103 printf("running stat_tests on all-null buffer, expecting failure\n"); |
| 94 printf("monobit %d\n", stat_test_monobit(buffer)); | 104 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 95 printf("poker %d\n", stat_test_poker(buffer)); | 105 printf("poker %d\n", stat_test_poker(buffer)); |
| 96 printf("runs %d\n", stat_test_runs(buffer)); | 106 printf("runs %d\n", stat_test_runs(buffer)); |
| 97 | 107 |
| 98 for (i=0; i < 2500; i++) | 108 for (i=0; i < 2500; i++) |
| 99 buffer[i] = rand(); | 109 buffer[i] = rand(); |
| 100 printf("running stat_tests on rand(), expecting success\n"); | 110 printf("running stat_tests on rand(), expecting success\n"); |
| 101 printf("monobit %d\n", stat_test_monobit(buffer)); | 111 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 102 printf("poker %d\n", stat_test_poker(buffer)); | 112 printf("poker %d\n", stat_test_poker(buffer)); |
| 103 printf("runs %d\n", stat_test_runs(buffer)); | 113 printf("runs %d\n", stat_test_runs(buffer)); |
| 104 | 114 |
| 105 printf("running stat_tests on AES-128-ICM, expecting success\n"); | 115 printf("running stat_tests on AES-128-ICM, expecting success\n"); |
| 106 /* set buffer to cipher output */ | 116 /* set buffer to cipher output */ |
| 107 for (i=0; i < 2500; i++) | 117 for (i=0; i < 2500; i++) |
| 108 buffer[i] = 0; | 118 buffer[i] = 0; |
| 109 err_check(cipher_type_alloc(&aes_icm, &c, 30)); | 119 err_check(cipher_type_alloc(&aes_icm, &c, 30, 0)); |
| 110 err_check(cipher_init(c, key, direction_encrypt)); | 120 err_check(cipher_init(c, key)); |
| 111 err_check(cipher_set_iv(c, &nonce)); | 121 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 112 err_check(cipher_encrypt(c, buffer, &buf_len)); | 122 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 113 /* run tests on cipher outout */ | 123 /* run tests on cipher outout */ |
| 114 printf("monobit %d\n", stat_test_monobit(buffer)); | 124 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 115 printf("poker %d\n", stat_test_poker(buffer)); | 125 printf("poker %d\n", stat_test_poker(buffer)); |
| 116 printf("runs %d\n", stat_test_runs(buffer)); | 126 printf("runs %d\n", stat_test_runs(buffer)); |
| 117 | 127 |
| 118 printf("runs test (please be patient): "); | 128 printf("runs test (please be patient): "); |
| 119 fflush(stdout); | 129 fflush(stdout); |
| 120 num_fail = 0; | 130 num_fail = 0; |
| 121 v128_set_to_zero(&nonce); | 131 v128_set_to_zero(&nonce); |
| 122 for(j=0; j < num_trials; j++) { | 132 for(j=0; j < num_trials; j++) { |
| 123 for (i=0; i < 2500; i++) | 133 for (i=0; i < 2500; i++) |
| 124 buffer[i] = 0; | 134 buffer[i] = 0; |
| 125 nonce.v32[3] = i; | 135 nonce.v32[3] = i; |
| 126 err_check(cipher_set_iv(c, &nonce)); | 136 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 127 err_check(cipher_encrypt(c, buffer, &buf_len)); | 137 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 128 if (stat_test_runs(buffer)) { | 138 if (stat_test_runs(buffer)) { |
| 129 num_fail++; | 139 num_fail++; |
| 130 } | 140 } |
| 131 } | 141 } |
| 132 | 142 |
| 133 printf("%d failures in %d tests\n", num_fail, num_trials); | 143 printf("%d failures in %d tests\n", num_fail, num_trials); |
| 134 printf("(nota bene: a small fraction of stat_test failures does not \n" | 144 printf("(nota bene: a small fraction of stat_test failures does not \n" |
| 135 "indicate that the random source is invalid)\n"); | 145 "indicate that the random source is invalid)\n"); |
| 136 | 146 |
| 137 err_check(cipher_dealloc(c)); | 147 err_check(cipher_dealloc(c)); |
| 138 | 148 |
| 139 printf("running stat_tests on AES-256-ICM, expecting success\n"); | 149 printf("running stat_tests on AES-256-ICM, expecting success\n"); |
| 140 /* set buffer to cipher output */ | 150 /* set buffer to cipher output */ |
| 141 for (i=0; i < 2500; i++) | 151 for (i=0; i < 2500; i++) |
| 142 buffer[i] = 0; | 152 buffer[i] = 0; |
| 143 err_check(cipher_type_alloc(&aes_icm, &c, 46)); | 153 err_check(cipher_type_alloc(&aes_icm, &c, 46, 0)); |
| 144 err_check(cipher_init(c, key, direction_encrypt)); | 154 err_check(cipher_init(c, key)); |
| 145 err_check(cipher_set_iv(c, &nonce)); | 155 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 146 err_check(cipher_encrypt(c, buffer, &buf_len)); | 156 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 147 /* run tests on cipher outout */ | 157 /* run tests on cipher outout */ |
| 148 printf("monobit %d\n", stat_test_monobit(buffer)); | 158 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 149 printf("poker %d\n", stat_test_poker(buffer)); | 159 printf("poker %d\n", stat_test_poker(buffer)); |
| 150 printf("runs %d\n", stat_test_runs(buffer)); | 160 printf("runs %d\n", stat_test_runs(buffer)); |
| 151 | 161 |
| 152 printf("runs test (please be patient): "); | 162 printf("runs test (please be patient): "); |
| 153 fflush(stdout); | 163 fflush(stdout); |
| 154 num_fail = 0; | 164 num_fail = 0; |
| 155 v128_set_to_zero(&nonce); | 165 v128_set_to_zero(&nonce); |
| 156 for(j=0; j < num_trials; j++) { | 166 for(j=0; j < num_trials; j++) { |
| 157 for (i=0; i < 2500; i++) | 167 for (i=0; i < 2500; i++) |
| 158 buffer[i] = 0; | 168 buffer[i] = 0; |
| 159 nonce.v32[3] = i; | 169 nonce.v32[3] = i; |
| 160 err_check(cipher_set_iv(c, &nonce)); | 170 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 161 err_check(cipher_encrypt(c, buffer, &buf_len)); | 171 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 162 if (stat_test_runs(buffer)) { | 172 if (stat_test_runs(buffer)) { |
| 163 num_fail++; | 173 num_fail++; |
| 164 } | 174 } |
| 165 } | 175 } |
| 166 | 176 |
| 177 #ifdef OPENSSL |
| 178 { |
| 179 printf("running stat_tests on AES-128-GCM, expecting success\n"); |
| 180 /* set buffer to cipher output */ |
| 181 for (i=0; i < 2500; i++) { |
| 182 buffer[i] = 0; |
| 183 } |
| 184 err_check(cipher_type_alloc(&aes_gcm_128_openssl, &c, AES_128_GCM_KEYSIZE_WS
ALT, 8)); |
| 185 err_check(cipher_init(c, key)); |
| 186 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 187 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 188 /* run tests on cipher outout */ |
| 189 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 190 printf("poker %d\n", stat_test_poker(buffer)); |
| 191 printf("runs %d\n", stat_test_runs(buffer)); |
| 192 fflush(stdout); |
| 193 num_fail = 0; |
| 194 v128_set_to_zero(&nonce); |
| 195 for(j=0; j < num_trials; j++) { |
| 196 for (i=0; i < 2500; i++) { |
| 197 buffer[i] = 0; |
| 198 } |
| 199 nonce.v32[3] = i; |
| 200 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 201 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 202 buf_len = 2500; |
| 203 if (stat_test_runs(buffer)) { |
| 204 num_fail++; |
| 205 } |
| 206 } |
| 207 |
| 208 printf("running stat_tests on AES-256-GCM, expecting success\n"); |
| 209 /* set buffer to cipher output */ |
| 210 for (i=0; i < 2500; i++) { |
| 211 buffer[i] = 0; |
| 212 } |
| 213 err_check(cipher_type_alloc(&aes_gcm_256_openssl, &c, AES_256_GCM_KEYSIZE_WS
ALT, 16)); |
| 214 err_check(cipher_init(c, key)); |
| 215 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 216 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 217 /* run tests on cipher outout */ |
| 218 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 219 printf("poker %d\n", stat_test_poker(buffer)); |
| 220 printf("runs %d\n", stat_test_runs(buffer)); |
| 221 fflush(stdout); |
| 222 num_fail = 0; |
| 223 v128_set_to_zero(&nonce); |
| 224 for(j=0; j < num_trials; j++) { |
| 225 for (i=0; i < 2500; i++) { |
| 226 buffer[i] = 0; |
| 227 } |
| 228 nonce.v32[3] = i; |
| 229 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 230 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 231 buf_len = 2500; |
| 232 if (stat_test_runs(buffer)) { |
| 233 num_fail++; |
| 234 } |
| 235 } |
| 236 } |
| 237 #endif |
| 238 |
| 167 printf("%d failures in %d tests\n", num_fail, num_trials); | 239 printf("%d failures in %d tests\n", num_fail, num_trials); |
| 168 printf("(nota bene: a small fraction of stat_test failures does not \n" | 240 printf("(nota bene: a small fraction of stat_test failures does not \n" |
| 169 "indicate that the random source is invalid)\n"); | 241 "indicate that the random source is invalid)\n"); |
| 170 | 242 |
| 171 err_check(cipher_dealloc(c)); | 243 err_check(cipher_dealloc(c)); |
| 172 | 244 |
| 173 return 0; | 245 return 0; |
| 174 } | 246 } |
| OLD | NEW |