Home | History | Annotate | Download | only in test
      1 /*
      2  * auth_driver.c
      3  *
      4  * a driver for auth functions
      5  *
      6  * David A. McGrew
      7  * Cisco Systems, Inc.
      8  */
      9 
     10 /*
     11  *
     12  * Copyright (c) 2001-2006, Cisco Systems, Inc.
     13  * All rights reserved.
     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 
     46 
     47 #include <stdio.h>    /* for printf() */
     48 #include <stdlib.h>   /* for xalloc() */
     49 #include <unistd.h>   /* for getopt() */
     50 
     51 #include "auth.h"
     52 #include "null_auth.h"
     53 
     54 #define PRINT_DEBUG_DATA 0
     55 
     56 extern auth_type_t tmmhv2;
     57 
     58 const uint16_t msg0[9] = {
     59   0x6015, 0xf141, 0x5ba1, 0x29a0, 0xf604, 0xd1c, 0x2d9, 0xaa8a, 0x7931
     60 };
     61 
     62 /* key1 is for TAG_WORDS = 2 */
     63 
     64 const uint16_t key1[47] = {
     65   0xe627, 0x6a01, 0x5ea7, 0xf27a, 0xc536, 0x2192, 0x11be, 0xea35,
     66   0xdb9d, 0x63d6, 0xfa8a, 0xfc45, 0xe08b, 0xd216, 0xced2, 0x7853,
     67   0x1a82, 0x22f5, 0x90fb, 0x1c29, 0x708e, 0xd06f, 0x82c3, 0xbee6,
     68   0x4f21, 0x6f33, 0x65c0, 0xd211, 0xc25e, 0x9138, 0x4fa3, 0x7c1f,
     69   0x61ac, 0x3489, 0x2976, 0x8c19, 0x8252, 0xddbf, 0xcad3, 0xc28f,
     70   0x68d6, 0x58dd, 0x504f, 0x2bbf, 0x0278, 0x70b7, 0xcfca
     71 };
     72 
     73 double
     74 auth_bits_per_second(auth_t *h, int msg_len);
     75 
     76 
     77 void
     78 usage(char *prog_name) {
     79   printf("usage: %s [ -t | -v ]\n", prog_name);
     80   exit(255);
     81 }
     82 
     83 #define MAX_MSG_LEN 2048
     84 
     85 int
     86 main (int argc, char *argv[]) {
     87   auth_t *a = NULL;
     88   err_status_t status;
     89   int i;
     90   int c;
     91   unsigned do_timing_test = 0;
     92   unsigned do_validation = 0;
     93 
     94   /* process input arguments */
     95   while (1) {
     96     c = getopt(argc, argv, "tv");
     97     if (c == -1)
     98       break;
     99     switch (c) {
    100     case 't':
    101       do_timing_test = 1;
    102       break;
    103     case 'v':
    104       do_validation = 1;
    105       break;
    106     default:
    107       usage(argv[0]);
    108     }
    109   }
    110 
    111   printf("auth driver\nDavid A. McGrew\nCisco Systems, Inc.\n");
    112 
    113   if (!do_validation && !do_timing_test)
    114     usage(argv[0]);
    115 
    116   if (do_validation) {
    117     printf("running self-test for %s...", tmmhv2.description);
    118     status = tmmhv2_add_big_test();
    119     if (status) {
    120       printf("tmmhv2_add_big_test failed with error code %d\n", status);
    121       exit(status);
    122     }
    123     status = auth_type_self_test(&tmmhv2);
    124     if (status) {
    125       printf("failed with error code %d\n", status);
    126       exit(status);
    127     }
    128     printf("passed\n");
    129   }
    130 
    131   if (do_timing_test) {
    132 
    133     /* tmmhv2 timing test */
    134     status = auth_type_alloc(&tmmhv2, &a, 94, 4);
    135     if (status) {
    136       fprintf(stderr, "can't allocate tmmhv2\n");
    137       exit(status);
    138     }
    139     status = auth_init(a, (uint8_t *)key1);
    140     if (status) {
    141       printf("error initializaing auth function\n");
    142       exit(status);
    143     }
    144 
    145     printf("timing %s (tag length %d)\n",
    146 	   tmmhv2.description, auth_get_tag_length(a));
    147     for (i=8; i <= MAX_MSG_LEN; i *= 2)
    148       printf("msg len: %d\tgigabits per second: %f\n",
    149 	     i, auth_bits_per_second(a, i) / 1E9);
    150 
    151     status = auth_dealloc(a);
    152     if (status) {
    153       printf("error deallocating auth function\n");
    154       exit(status);
    155     }
    156 
    157   }
    158 
    159   return 0;
    160 }
    161 
    162 #define NUM_TRIALS 100000
    163 
    164 #include <time.h>
    165 
    166 double
    167 auth_bits_per_second(auth_t *a, int msg_len_octets) {
    168   int i;
    169   clock_t timer;
    170   uint8_t *result;
    171   int msg_len = (msg_len_octets + 1)/2;
    172   uint16_t *msg_string;
    173 
    174   /* create random message */
    175   msg_string = (uint16_t *) crypto_alloc(msg_len_octets);
    176   if (msg_string == NULL)
    177     return 0.0; /* indicate failure */
    178   for (i=0; i < msg_len; i++)
    179     msg_string[i] = (uint16_t) random();
    180 
    181   /* allocate temporary storage for authentication tag */
    182   result = crypto_alloc(auth_get_tag_length(a));
    183   if (result == NULL) {
    184     free(msg_string);
    185     return 0.0; /* indicate failure */
    186   }
    187 
    188   timer = clock();
    189   for (i=0; i < NUM_TRIALS; i++) {
    190     auth_compute(a, (uint8_t *)msg_string, msg_len_octets, (uint8_t *)result);
    191   }
    192   timer = clock() - timer;
    193 
    194   free(msg_string);
    195   free(result);
    196 
    197   return (double) NUM_TRIALS * 8 * msg_len_octets * CLOCKS_PER_SEC / timer;
    198 }
    199 
    200 
    201