| 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 |
| 11 *» | 11 #ifdef HAVE_CONFIG_H |
| 12 * Copyright (c) 2001-2006, Cisco Systems, Inc. | 12 #include <config.h> |
| 13 * All rights reserved. | 13 #endif |
| 14 * | |
| 15 * Redistribution and use in source and binary forms, with or without | |
| 16 * modification, are permitted provided that the following conditions | |
| 17 * are met: | |
| 18 * | |
| 19 * Redistributions of source code must retain the above copyright | |
| 20 * notice, this list of conditions and the following disclaimer. | |
| 21 * | |
| 22 * Redistributions in binary form must reproduce the above | |
| 23 * copyright notice, this list of conditions and the following | |
| 24 * disclaimer in the documentation and/or other materials provided | |
| 25 * with the distribution. | |
| 26 * | |
| 27 * Neither the name of the Cisco Systems, Inc. nor the names of its | |
| 28 * contributors may be used to endorse or promote products derived | |
| 29 * from this software without specific prior written permission. | |
| 30 * | |
| 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 42 * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 43 * | |
| 44 */ | |
| 45 | 14 |
| 46 #include <stdio.h> /* for printf() */ | 15 #include <stdio.h> /* for printf() */ |
| 47 | 16 |
| 48 #include "err.h" | 17 #include "err.h" |
| 49 #include "stat.h" | 18 #include "stat.h" |
| 19 #include "srtp.h" |
| 50 | 20 |
| 51 #include "cipher.h" | 21 #include "cipher.h" |
| 52 | 22 |
| 53 typedef struct { | 23 typedef struct { |
| 54 void *state; | 24 void *state; |
| 55 } random_source_t; | 25 } random_source_t; |
| 56 | 26 |
| 57 err_status_t | 27 err_status_t |
| 58 random_source_alloc(void); | 28 random_source_alloc(void); |
| 59 | 29 |
| 60 void | 30 void |
| 61 err_check(err_status_t s) { | 31 err_check(err_status_t s) { |
| 62 if (s) { | 32 if (s) { |
| 63 printf("error (code %d)\n", s); | 33 printf("error (code %d)\n", s); |
| 64 exit(1); | 34 exit(1); |
| 65 } | 35 } |
| 66 } | 36 } |
| 67 | 37 |
| 68 int | 38 int |
| 69 main (int argc, char *argv[]) { | 39 main (int argc, char *argv[]) { |
| 70 uint8_t buffer[2500]; | 40 uint8_t buffer[2532]; |
| 71 unsigned int buf_len = 2500; | 41 unsigned int buf_len = 2500; |
| 72 int i, j; | 42 int i, j; |
| 73 extern cipher_type_t aes_icm; | 43 extern cipher_type_t aes_icm; |
| 44 #ifdef OPENSSL |
| 45 extern cipher_type_t aes_gcm_128_openssl; |
| 46 extern cipher_type_t aes_gcm_256_openssl; |
| 47 #endif |
| 74 cipher_t *c; | 48 cipher_t *c; |
| 75 uint8_t key[46] = { | 49 uint8_t key[46] = { |
| 76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 50 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 77 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 51 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 52 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 79 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 80 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 81 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 | 55 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 |
| 82 }; | 56 }; |
| 83 v128_t nonce; | 57 v128_t nonce; |
| 84 int num_trials = 500; | 58 int num_trials = 500; |
| 85 int num_fail; | 59 int num_fail; |
| 86 | 60 |
| 87 printf("statistical tests driver\n"); | 61 printf("statistical tests driver\n"); |
| 88 | 62 |
| 63 v128_set_to_zero(&nonce); |
| 89 for (i=0; i < 2500; i++) | 64 for (i=0; i < 2500; i++) |
| 90 buffer[i] = 0; | 65 buffer[i] = 0; |
| 91 | 66 |
| 92 /* run tests */ | 67 /* run tests */ |
| 93 printf("running stat_tests on all-null buffer, expecting failure\n"); | 68 printf("running stat_tests on all-null buffer, expecting failure\n"); |
| 94 printf("monobit %d\n", stat_test_monobit(buffer)); | 69 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 95 printf("poker %d\n", stat_test_poker(buffer)); | 70 printf("poker %d\n", stat_test_poker(buffer)); |
| 96 printf("runs %d\n", stat_test_runs(buffer)); | 71 printf("runs %d\n", stat_test_runs(buffer)); |
| 97 | 72 |
| 98 for (i=0; i < 2500; i++) | 73 for (i=0; i < 2500; i++) |
| 99 buffer[i] = rand(); | 74 buffer[i] = rand(); |
| 100 printf("running stat_tests on rand(), expecting success\n"); | 75 printf("running stat_tests on rand(), expecting success\n"); |
| 101 printf("monobit %d\n", stat_test_monobit(buffer)); | 76 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 102 printf("poker %d\n", stat_test_poker(buffer)); | 77 printf("poker %d\n", stat_test_poker(buffer)); |
| 103 printf("runs %d\n", stat_test_runs(buffer)); | 78 printf("runs %d\n", stat_test_runs(buffer)); |
| 104 | 79 |
| 105 printf("running stat_tests on AES-128-ICM, expecting success\n"); | 80 printf("running stat_tests on AES-128-ICM, expecting success\n"); |
| 106 /* set buffer to cipher output */ | 81 /* set buffer to cipher output */ |
| 107 for (i=0; i < 2500; i++) | 82 for (i=0; i < 2500; i++) |
| 108 buffer[i] = 0; | 83 buffer[i] = 0; |
| 109 err_check(cipher_type_alloc(&aes_icm, &c, 30)); | 84 err_check(cipher_type_alloc(&aes_icm, &c, 30, 0)); |
| 110 err_check(cipher_init(c, key, direction_encrypt)); | 85 err_check(cipher_init(c, key)); |
| 111 err_check(cipher_set_iv(c, &nonce)); | 86 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 112 err_check(cipher_encrypt(c, buffer, &buf_len)); | 87 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 113 /* run tests on cipher outout */ | 88 /* run tests on cipher outout */ |
| 114 printf("monobit %d\n", stat_test_monobit(buffer)); | 89 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 115 printf("poker %d\n", stat_test_poker(buffer)); | 90 printf("poker %d\n", stat_test_poker(buffer)); |
| 116 printf("runs %d\n", stat_test_runs(buffer)); | 91 printf("runs %d\n", stat_test_runs(buffer)); |
| 117 | 92 |
| 118 printf("runs test (please be patient): "); | 93 printf("runs test (please be patient): "); |
| 119 fflush(stdout); | 94 fflush(stdout); |
| 120 num_fail = 0; | 95 num_fail = 0; |
| 121 v128_set_to_zero(&nonce); | 96 v128_set_to_zero(&nonce); |
| 122 for(j=0; j < num_trials; j++) { | 97 for(j=0; j < num_trials; j++) { |
| 123 for (i=0; i < 2500; i++) | 98 for (i=0; i < 2500; i++) |
| 124 buffer[i] = 0; | 99 buffer[i] = 0; |
| 125 nonce.v32[3] = i; | 100 nonce.v32[3] = i; |
| 126 err_check(cipher_set_iv(c, &nonce)); | 101 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 127 err_check(cipher_encrypt(c, buffer, &buf_len)); | 102 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 128 if (stat_test_runs(buffer)) { | 103 if (stat_test_runs(buffer)) { |
| 129 num_fail++; | 104 num_fail++; |
| 130 } | 105 } |
| 131 } | 106 } |
| 132 | 107 |
| 133 printf("%d failures in %d tests\n", num_fail, num_trials); | 108 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" | 109 printf("(nota bene: a small fraction of stat_test failures does not \n" |
| 135 "indicate that the random source is invalid)\n"); | 110 "indicate that the random source is invalid)\n"); |
| 136 | 111 |
| 137 err_check(cipher_dealloc(c)); | 112 err_check(cipher_dealloc(c)); |
| 138 | 113 |
| 139 printf("running stat_tests on AES-256-ICM, expecting success\n"); | 114 printf("running stat_tests on AES-256-ICM, expecting success\n"); |
| 140 /* set buffer to cipher output */ | 115 /* set buffer to cipher output */ |
| 141 for (i=0; i < 2500; i++) | 116 for (i=0; i < 2500; i++) |
| 142 buffer[i] = 0; | 117 buffer[i] = 0; |
| 143 err_check(cipher_type_alloc(&aes_icm, &c, 46)); | 118 err_check(cipher_type_alloc(&aes_icm, &c, 46, 0)); |
| 144 err_check(cipher_init(c, key, direction_encrypt)); | 119 err_check(cipher_init(c, key)); |
| 145 err_check(cipher_set_iv(c, &nonce)); | 120 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 146 err_check(cipher_encrypt(c, buffer, &buf_len)); | 121 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 147 /* run tests on cipher outout */ | 122 /* run tests on cipher outout */ |
| 148 printf("monobit %d\n", stat_test_monobit(buffer)); | 123 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 149 printf("poker %d\n", stat_test_poker(buffer)); | 124 printf("poker %d\n", stat_test_poker(buffer)); |
| 150 printf("runs %d\n", stat_test_runs(buffer)); | 125 printf("runs %d\n", stat_test_runs(buffer)); |
| 151 | 126 |
| 152 printf("runs test (please be patient): "); | 127 printf("runs test (please be patient): "); |
| 153 fflush(stdout); | 128 fflush(stdout); |
| 154 num_fail = 0; | 129 num_fail = 0; |
| 155 v128_set_to_zero(&nonce); | 130 v128_set_to_zero(&nonce); |
| 156 for(j=0; j < num_trials; j++) { | 131 for(j=0; j < num_trials; j++) { |
| 157 for (i=0; i < 2500; i++) | 132 for (i=0; i < 2500; i++) |
| 158 buffer[i] = 0; | 133 buffer[i] = 0; |
| 159 nonce.v32[3] = i; | 134 nonce.v32[3] = i; |
| 160 err_check(cipher_set_iv(c, &nonce)); | 135 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 161 err_check(cipher_encrypt(c, buffer, &buf_len)); | 136 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 162 if (stat_test_runs(buffer)) { | 137 if (stat_test_runs(buffer)) { |
| 163 num_fail++; | 138 num_fail++; |
| 164 } | 139 } |
| 165 } | 140 } |
| 166 | 141 |
| 142 #ifdef OPENSSL |
| 143 { |
| 144 printf("running stat_tests on AES-128-GCM, expecting success\n"); |
| 145 /* set buffer to cipher output */ |
| 146 for (i=0; i < 2500; i++) { |
| 147 buffer[i] = 0; |
| 148 } |
| 149 err_check(cipher_type_alloc(&aes_gcm_128_openssl, &c, AES_128_GCM_KEYSIZE_WS
ALT, 8)); |
| 150 err_check(cipher_init(c, key)); |
| 151 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 152 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 153 /* run tests on cipher outout */ |
| 154 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 155 printf("poker %d\n", stat_test_poker(buffer)); |
| 156 printf("runs %d\n", stat_test_runs(buffer)); |
| 157 fflush(stdout); |
| 158 num_fail = 0; |
| 159 v128_set_to_zero(&nonce); |
| 160 for(j=0; j < num_trials; j++) { |
| 161 for (i=0; i < 2500; i++) { |
| 162 buffer[i] = 0; |
| 163 } |
| 164 nonce.v32[3] = i; |
| 165 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 166 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 167 buf_len = 2500; |
| 168 if (stat_test_runs(buffer)) { |
| 169 num_fail++; |
| 170 } |
| 171 } |
| 172 |
| 173 printf("running stat_tests on AES-256-GCM, expecting success\n"); |
| 174 /* set buffer to cipher output */ |
| 175 for (i=0; i < 2500; i++) { |
| 176 buffer[i] = 0; |
| 177 } |
| 178 err_check(cipher_type_alloc(&aes_gcm_256_openssl, &c, AES_256_GCM_KEYSIZE_WS
ALT, 16)); |
| 179 err_check(cipher_init(c, key)); |
| 180 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 181 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 182 /* run tests on cipher outout */ |
| 183 printf("monobit %d\n", stat_test_monobit(buffer)); |
| 184 printf("poker %d\n", stat_test_poker(buffer)); |
| 185 printf("runs %d\n", stat_test_runs(buffer)); |
| 186 fflush(stdout); |
| 187 num_fail = 0; |
| 188 v128_set_to_zero(&nonce); |
| 189 for(j=0; j < num_trials; j++) { |
| 190 for (i=0; i < 2500; i++) { |
| 191 buffer[i] = 0; |
| 192 } |
| 193 nonce.v32[3] = i; |
| 194 err_check(cipher_set_iv(c, &nonce, direction_encrypt)); |
| 195 err_check(cipher_encrypt(c, buffer, &buf_len)); |
| 196 buf_len = 2500; |
| 197 if (stat_test_runs(buffer)) { |
| 198 num_fail++; |
| 199 } |
| 200 } |
| 201 } |
| 202 #endif |
| 203 |
| 167 printf("%d failures in %d tests\n", num_fail, num_trials); | 204 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" | 205 printf("(nota bene: a small fraction of stat_test failures does not \n" |
| 169 "indicate that the random source is invalid)\n"); | 206 "indicate that the random source is invalid)\n"); |
| 170 | 207 |
| 171 err_check(cipher_dealloc(c)); | 208 err_check(cipher_dealloc(c)); |
| 172 | 209 |
| 173 return 0; | 210 return 0; |
| 174 } | 211 } |
| OLD | NEW |