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