netwerk/srtp/src/crypto/test/auth_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/auth_driver.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,200 @@
     1.4 +/*
     1.5 + * auth_driver.c
     1.6 + *
     1.7 + * a driver for auth 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 +
    1.50 +#include <stdio.h>    /* for printf() */
    1.51 +#include <stdlib.h>   /* for xalloc() */
    1.52 +#include <unistd.h>   /* for getopt() */
    1.53 +
    1.54 +#include "auth.h"
    1.55 +#include "null_auth.h"
    1.56 +
    1.57 +#define PRINT_DEBUG_DATA 0
    1.58 +
    1.59 +extern auth_type_t tmmhv2;
    1.60 +
    1.61 +const uint16_t msg0[9] = {
    1.62 +  0x6015, 0xf141, 0x5ba1, 0x29a0, 0xf604, 0xd1c, 0x2d9, 0xaa8a, 0x7931
    1.63 +};
    1.64 +
    1.65 +/* key1 is for TAG_WORDS = 2 */
    1.66 +
    1.67 +const uint16_t key1[47] = {
    1.68 +  0xe627, 0x6a01, 0x5ea7, 0xf27a, 0xc536, 0x2192, 0x11be, 0xea35,
    1.69 +  0xdb9d, 0x63d6, 0xfa8a, 0xfc45, 0xe08b, 0xd216, 0xced2, 0x7853,
    1.70 +  0x1a82, 0x22f5, 0x90fb, 0x1c29, 0x708e, 0xd06f, 0x82c3, 0xbee6,
    1.71 +  0x4f21, 0x6f33, 0x65c0, 0xd211, 0xc25e, 0x9138, 0x4fa3, 0x7c1f,
    1.72 +  0x61ac, 0x3489, 0x2976, 0x8c19, 0x8252, 0xddbf, 0xcad3, 0xc28f,
    1.73 +  0x68d6, 0x58dd, 0x504f, 0x2bbf, 0x0278, 0x70b7, 0xcfca
    1.74 +};
    1.75 +
    1.76 +double
    1.77 +auth_bits_per_second(auth_t *h, int msg_len);
    1.78 +
    1.79 +
    1.80 +void
    1.81 +usage(char *prog_name) {
    1.82 +  printf("usage: %s [ -t | -v ]\n", prog_name);
    1.83 +  exit(255);
    1.84 +}
    1.85 +
    1.86 +#define MAX_MSG_LEN 2048
    1.87 +
    1.88 +int
    1.89 +main (int argc, char *argv[]) {
    1.90 +  auth_t *a = NULL;
    1.91 +  err_status_t status;
    1.92 +  int i;
    1.93 +  int c;
    1.94 +  unsigned do_timing_test = 0;
    1.95 +  unsigned do_validation = 0;
    1.96 +
    1.97 +  /* process input arguments */
    1.98 +  while (1) {
    1.99 +    c = getopt(argc, argv, "tv");
   1.100 +    if (c == -1) 
   1.101 +      break;
   1.102 +    switch (c) {
   1.103 +    case 't':
   1.104 +      do_timing_test = 1;
   1.105 +      break;
   1.106 +    case 'v':
   1.107 +      do_validation = 1;
   1.108 +      break;
   1.109 +    default:
   1.110 +      usage(argv[0]);
   1.111 +    }    
   1.112 +  }
   1.113 +  
   1.114 +  printf("auth driver\nDavid A. McGrew\nCisco Systems, Inc.\n");
   1.115 +
   1.116 +  if (!do_validation && !do_timing_test)
   1.117 +    usage(argv[0]);
   1.118 +
   1.119 +  if (do_validation) {
   1.120 +    printf("running self-test for %s...", tmmhv2.description);
   1.121 +    status = tmmhv2_add_big_test();
   1.122 +    if (status) {
   1.123 +      printf("tmmhv2_add_big_test failed with error code %d\n", status);
   1.124 +      exit(status);
   1.125 +    }  
   1.126 +    status = auth_type_self_test(&tmmhv2);
   1.127 +    if (status) {
   1.128 +      printf("failed with error code %d\n", status);
   1.129 +      exit(status);
   1.130 +    }
   1.131 +    printf("passed\n");
   1.132 +  }
   1.133 +
   1.134 +  if (do_timing_test) {
   1.135 +
   1.136 +    /* tmmhv2 timing test */
   1.137 +    status = auth_type_alloc(&tmmhv2, &a, 94, 4);
   1.138 +    if (status) {
   1.139 +      fprintf(stderr, "can't allocate tmmhv2\n");
   1.140 +      exit(status);
   1.141 +    }
   1.142 +    status = auth_init(a, (uint8_t *)key1);
   1.143 +    if (status) {
   1.144 +      printf("error initializaing auth function\n");
   1.145 +      exit(status);
   1.146 +    }
   1.147 +    
   1.148 +    printf("timing %s (tag length %d)\n", 
   1.149 +	   tmmhv2.description, auth_get_tag_length(a));
   1.150 +    for (i=8; i <= MAX_MSG_LEN; i *= 2)
   1.151 +      printf("msg len: %d\tgigabits per second: %f\n",
   1.152 +	     i, auth_bits_per_second(a, i) / 1E9);
   1.153 +
   1.154 +    status = auth_dealloc(a);
   1.155 +    if (status) {
   1.156 +      printf("error deallocating auth function\n");
   1.157 +      exit(status);
   1.158 +    }
   1.159 +    
   1.160 +  }
   1.161 +
   1.162 +  return 0;
   1.163 +}
   1.164 +
   1.165 +#define NUM_TRIALS 100000
   1.166 +
   1.167 +#include <time.h>
   1.168 +
   1.169 +double
   1.170 +auth_bits_per_second(auth_t *a, int msg_len_octets) {
   1.171 +  int i;
   1.172 +  clock_t timer;
   1.173 +  uint8_t *result;
   1.174 +  int msg_len = (msg_len_octets + 1)/2;
   1.175 +  uint16_t *msg_string; 
   1.176 +
   1.177 +  /* create random message */
   1.178 +  msg_string = (uint16_t *) crypto_alloc(msg_len_octets);
   1.179 +  if (msg_string == NULL)
   1.180 +    return 0.0; /* indicate failure */  
   1.181 +  for (i=0; i < msg_len; i++) 
   1.182 +    msg_string[i] = (uint16_t) random();
   1.183 +
   1.184 +  /* allocate temporary storage for authentication tag */
   1.185 +  result = crypto_alloc(auth_get_tag_length(a));
   1.186 +  if (result == NULL) {
   1.187 +    free(msg_string);
   1.188 +    return 0.0; /* indicate failure */  
   1.189 +  }
   1.190 +  
   1.191 +  timer = clock();
   1.192 +  for (i=0; i < NUM_TRIALS; i++) {
   1.193 +    auth_compute(a, (uint8_t *)msg_string, msg_len_octets, (uint8_t *)result);
   1.194 +  }
   1.195 +  timer = clock() - timer;
   1.196 +
   1.197 +  free(msg_string);
   1.198 +  free(result);
   1.199 +  
   1.200 +  return (double) NUM_TRIALS * 8 * msg_len_octets * CLOCKS_PER_SEC / timer;
   1.201 +}
   1.202 +
   1.203 +

mercurial