netwerk/srtp/src/crypto/test/stat_driver.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/srtp/src/crypto/test/stat_driver.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +/*
     1.5 + * stat-driver.c
     1.6 + *
     1.7 + * test driver for the stat_test functions
     1.8 + *
     1.9 + * David A. McGrew
    1.10 + * Cisco Systems, Inc.
    1.11 + */
    1.12 +
    1.13 +/*
    1.14 + *	
    1.15 + * Copyright (c) 2001-2006, Cisco Systems, Inc.
    1.16 + * All rights reserved.
    1.17 + * 
    1.18 + * Redistribution and use in source and binary forms, with or without
    1.19 + * modification, are permitted provided that the following conditions
    1.20 + * are met:
    1.21 + * 
    1.22 + *   Redistributions of source code must retain the above copyright
    1.23 + *   notice, this list of conditions and the following disclaimer.
    1.24 + * 
    1.25 + *   Redistributions in binary form must reproduce the above
    1.26 + *   copyright notice, this list of conditions and the following
    1.27 + *   disclaimer in the documentation and/or other materials provided
    1.28 + *   with the distribution.
    1.29 + * 
    1.30 + *   Neither the name of the Cisco Systems, Inc. nor the names of its
    1.31 + *   contributors may be used to endorse or promote products derived
    1.32 + *   from this software without specific prior written permission.
    1.33 + * 
    1.34 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.35 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.36 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.37 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    1.38 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    1.39 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.40 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.41 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.42 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.43 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.44 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.45 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.46 + *
    1.47 + */
    1.48 +
    1.49 +#include <stdio.h>         /* for printf() */
    1.50 +
    1.51 +#include "err.h"
    1.52 +#include "stat.h"
    1.53 +
    1.54 +#include "cipher.h"
    1.55 +
    1.56 +typedef struct {
    1.57 +  void *state;
    1.58 +} random_source_t;
    1.59 +
    1.60 +err_status_t
    1.61 +random_source_alloc(void);
    1.62 +
    1.63 +void
    1.64 +err_check(err_status_t s) {
    1.65 +  if (s) {
    1.66 +    printf("error (code %d)\n", s);
    1.67 +    exit(1);
    1.68 +  }
    1.69 +}
    1.70 +
    1.71 +int
    1.72 +main (int argc, char *argv[]) {
    1.73 +  uint8_t buffer[2500];
    1.74 +  unsigned int buf_len = 2500;
    1.75 +  int i, j;
    1.76 +  extern cipher_type_t aes_icm;
    1.77 +  cipher_t *c;
    1.78 +  uint8_t key[46] = {
    1.79 +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
    1.80 +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
    1.81 +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
    1.82 +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
    1.83 +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
    1.84 +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05
    1.85 +    };
    1.86 +  v128_t nonce;
    1.87 +  int num_trials = 500;
    1.88 +  int num_fail;
    1.89 +
    1.90 +  printf("statistical tests driver\n");
    1.91 +
    1.92 +  for (i=0; i < 2500; i++)
    1.93 +    buffer[i] = 0;
    1.94 +
    1.95 +  /* run tests */
    1.96 +  printf("running stat_tests on all-null buffer, expecting failure\n");
    1.97 +  printf("monobit %d\n", stat_test_monobit(buffer));
    1.98 +  printf("poker   %d\n", stat_test_poker(buffer));
    1.99 +  printf("runs    %d\n", stat_test_runs(buffer));
   1.100 +
   1.101 +  for (i=0; i < 2500; i++)
   1.102 +    buffer[i] = rand();
   1.103 +  printf("running stat_tests on rand(), expecting success\n");
   1.104 +  printf("monobit %d\n", stat_test_monobit(buffer));
   1.105 +  printf("poker   %d\n", stat_test_poker(buffer));
   1.106 +  printf("runs    %d\n", stat_test_runs(buffer));
   1.107 +
   1.108 +  printf("running stat_tests on AES-128-ICM, expecting success\n");
   1.109 +  /* set buffer to cipher output */
   1.110 +  for (i=0; i < 2500; i++)
   1.111 +    buffer[i] = 0;
   1.112 +  err_check(cipher_type_alloc(&aes_icm, &c, 30));
   1.113 +  err_check(cipher_init(c, key, direction_encrypt));
   1.114 +  err_check(cipher_set_iv(c, &nonce));
   1.115 +  err_check(cipher_encrypt(c, buffer, &buf_len));
   1.116 +  /* run tests on cipher outout */
   1.117 +  printf("monobit %d\n", stat_test_monobit(buffer));
   1.118 +  printf("poker   %d\n", stat_test_poker(buffer));
   1.119 +  printf("runs    %d\n", stat_test_runs(buffer));
   1.120 +
   1.121 +  printf("runs test (please be patient): ");
   1.122 +  fflush(stdout);
   1.123 +  num_fail = 0;
   1.124 +  v128_set_to_zero(&nonce);
   1.125 +  for(j=0; j < num_trials; j++) {
   1.126 +    for (i=0; i < 2500; i++)
   1.127 +      buffer[i] = 0;
   1.128 +    nonce.v32[3] = i;
   1.129 +    err_check(cipher_set_iv(c, &nonce));
   1.130 +    err_check(cipher_encrypt(c, buffer, &buf_len));
   1.131 +    if (stat_test_runs(buffer)) {
   1.132 +      num_fail++;
   1.133 +    }
   1.134 +  }
   1.135 +
   1.136 +  printf("%d failures in %d tests\n", num_fail, num_trials);
   1.137 +  printf("(nota bene: a small fraction of stat_test failures does not \n"
   1.138 +	 "indicate that the random source is invalid)\n");
   1.139 +
   1.140 +  err_check(cipher_dealloc(c));
   1.141 +
   1.142 +  printf("running stat_tests on AES-256-ICM, expecting success\n");
   1.143 +  /* set buffer to cipher output */
   1.144 +  for (i=0; i < 2500; i++)
   1.145 +    buffer[i] = 0;
   1.146 +  err_check(cipher_type_alloc(&aes_icm, &c, 46));
   1.147 +  err_check(cipher_init(c, key, direction_encrypt));
   1.148 +  err_check(cipher_set_iv(c, &nonce));
   1.149 +  err_check(cipher_encrypt(c, buffer, &buf_len));
   1.150 +  /* run tests on cipher outout */
   1.151 +  printf("monobit %d\n", stat_test_monobit(buffer));
   1.152 +  printf("poker   %d\n", stat_test_poker(buffer));
   1.153 +  printf("runs    %d\n", stat_test_runs(buffer));
   1.154 +
   1.155 +  printf("runs test (please be patient): ");
   1.156 +  fflush(stdout);
   1.157 +  num_fail = 0;
   1.158 +  v128_set_to_zero(&nonce);
   1.159 +  for(j=0; j < num_trials; j++) {
   1.160 +    for (i=0; i < 2500; i++)
   1.161 +      buffer[i] = 0;
   1.162 +    nonce.v32[3] = i;
   1.163 +    err_check(cipher_set_iv(c, &nonce));
   1.164 +    err_check(cipher_encrypt(c, buffer, &buf_len));
   1.165 +    if (stat_test_runs(buffer)) {
   1.166 +      num_fail++;
   1.167 +    }
   1.168 +  }
   1.169 +
   1.170 +  printf("%d failures in %d tests\n", num_fail, num_trials);
   1.171 +  printf("(nota bene: a small fraction of stat_test failures does not \n"
   1.172 +	 "indicate that the random source is invalid)\n");
   1.173 +
   1.174 +  err_check(cipher_dealloc(c));
   1.175 +
   1.176 +  return 0;
   1.177 +}

mercurial