michael@0: /* michael@0: * stat-driver.c michael@0: * michael@0: * test driver for the stat_test functions michael@0: * michael@0: * David A. McGrew michael@0: * Cisco Systems, Inc. michael@0: */ michael@0: michael@0: /* michael@0: * michael@0: * Copyright (c) 2001-2006, Cisco Systems, Inc. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * michael@0: * Neither the name of the Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, michael@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, michael@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED michael@0: * OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #include /* for printf() */ michael@0: michael@0: #include "err.h" michael@0: #include "stat.h" michael@0: michael@0: #include "cipher.h" michael@0: michael@0: typedef struct { michael@0: void *state; michael@0: } random_source_t; michael@0: michael@0: err_status_t michael@0: random_source_alloc(void); michael@0: michael@0: void michael@0: err_check(err_status_t s) { michael@0: if (s) { michael@0: printf("error (code %d)\n", s); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: int michael@0: main (int argc, char *argv[]) { michael@0: uint8_t buffer[2500]; michael@0: unsigned int buf_len = 2500; michael@0: int i, j; michael@0: extern cipher_type_t aes_icm; michael@0: cipher_t *c; michael@0: uint8_t key[46] = { michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 michael@0: }; michael@0: v128_t nonce; michael@0: int num_trials = 500; michael@0: int num_fail; michael@0: michael@0: printf("statistical tests driver\n"); michael@0: michael@0: for (i=0; i < 2500; i++) michael@0: buffer[i] = 0; michael@0: michael@0: /* run tests */ michael@0: printf("running stat_tests on all-null buffer, expecting failure\n"); michael@0: printf("monobit %d\n", stat_test_monobit(buffer)); michael@0: printf("poker %d\n", stat_test_poker(buffer)); michael@0: printf("runs %d\n", stat_test_runs(buffer)); michael@0: michael@0: for (i=0; i < 2500; i++) michael@0: buffer[i] = rand(); michael@0: printf("running stat_tests on rand(), expecting success\n"); michael@0: printf("monobit %d\n", stat_test_monobit(buffer)); michael@0: printf("poker %d\n", stat_test_poker(buffer)); michael@0: printf("runs %d\n", stat_test_runs(buffer)); michael@0: michael@0: printf("running stat_tests on AES-128-ICM, expecting success\n"); michael@0: /* set buffer to cipher output */ michael@0: for (i=0; i < 2500; i++) michael@0: buffer[i] = 0; michael@0: err_check(cipher_type_alloc(&aes_icm, &c, 30)); michael@0: err_check(cipher_init(c, key, direction_encrypt)); michael@0: err_check(cipher_set_iv(c, &nonce)); michael@0: err_check(cipher_encrypt(c, buffer, &buf_len)); michael@0: /* run tests on cipher outout */ michael@0: printf("monobit %d\n", stat_test_monobit(buffer)); michael@0: printf("poker %d\n", stat_test_poker(buffer)); michael@0: printf("runs %d\n", stat_test_runs(buffer)); michael@0: michael@0: printf("runs test (please be patient): "); michael@0: fflush(stdout); michael@0: num_fail = 0; michael@0: v128_set_to_zero(&nonce); michael@0: for(j=0; j < num_trials; j++) { michael@0: for (i=0; i < 2500; i++) michael@0: buffer[i] = 0; michael@0: nonce.v32[3] = i; michael@0: err_check(cipher_set_iv(c, &nonce)); michael@0: err_check(cipher_encrypt(c, buffer, &buf_len)); michael@0: if (stat_test_runs(buffer)) { michael@0: num_fail++; michael@0: } michael@0: } michael@0: michael@0: printf("%d failures in %d tests\n", num_fail, num_trials); michael@0: printf("(nota bene: a small fraction of stat_test failures does not \n" michael@0: "indicate that the random source is invalid)\n"); michael@0: michael@0: err_check(cipher_dealloc(c)); michael@0: michael@0: printf("running stat_tests on AES-256-ICM, expecting success\n"); michael@0: /* set buffer to cipher output */ michael@0: for (i=0; i < 2500; i++) michael@0: buffer[i] = 0; michael@0: err_check(cipher_type_alloc(&aes_icm, &c, 46)); michael@0: err_check(cipher_init(c, key, direction_encrypt)); michael@0: err_check(cipher_set_iv(c, &nonce)); michael@0: err_check(cipher_encrypt(c, buffer, &buf_len)); michael@0: /* run tests on cipher outout */ michael@0: printf("monobit %d\n", stat_test_monobit(buffer)); michael@0: printf("poker %d\n", stat_test_poker(buffer)); michael@0: printf("runs %d\n", stat_test_runs(buffer)); michael@0: michael@0: printf("runs test (please be patient): "); michael@0: fflush(stdout); michael@0: num_fail = 0; michael@0: v128_set_to_zero(&nonce); michael@0: for(j=0; j < num_trials; j++) { michael@0: for (i=0; i < 2500; i++) michael@0: buffer[i] = 0; michael@0: nonce.v32[3] = i; michael@0: err_check(cipher_set_iv(c, &nonce)); michael@0: err_check(cipher_encrypt(c, buffer, &buf_len)); michael@0: if (stat_test_runs(buffer)) { michael@0: num_fail++; michael@0: } michael@0: } michael@0: michael@0: printf("%d failures in %d tests\n", num_fail, num_trials); michael@0: printf("(nota bene: a small fraction of stat_test failures does not \n" michael@0: "indicate that the random source is invalid)\n"); michael@0: michael@0: err_check(cipher_dealloc(c)); michael@0: michael@0: return 0; michael@0: }