michael@0: /* michael@0: * auth_driver.c michael@0: * michael@0: * a driver for auth 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: michael@0: #include /* for printf() */ michael@0: #include /* for xalloc() */ michael@0: #include /* for getopt() */ michael@0: michael@0: #include "auth.h" michael@0: #include "null_auth.h" michael@0: michael@0: #define PRINT_DEBUG_DATA 0 michael@0: michael@0: extern auth_type_t tmmhv2; michael@0: michael@0: const uint16_t msg0[9] = { michael@0: 0x6015, 0xf141, 0x5ba1, 0x29a0, 0xf604, 0xd1c, 0x2d9, 0xaa8a, 0x7931 michael@0: }; michael@0: michael@0: /* key1 is for TAG_WORDS = 2 */ michael@0: michael@0: const uint16_t key1[47] = { michael@0: 0xe627, 0x6a01, 0x5ea7, 0xf27a, 0xc536, 0x2192, 0x11be, 0xea35, michael@0: 0xdb9d, 0x63d6, 0xfa8a, 0xfc45, 0xe08b, 0xd216, 0xced2, 0x7853, michael@0: 0x1a82, 0x22f5, 0x90fb, 0x1c29, 0x708e, 0xd06f, 0x82c3, 0xbee6, michael@0: 0x4f21, 0x6f33, 0x65c0, 0xd211, 0xc25e, 0x9138, 0x4fa3, 0x7c1f, michael@0: 0x61ac, 0x3489, 0x2976, 0x8c19, 0x8252, 0xddbf, 0xcad3, 0xc28f, michael@0: 0x68d6, 0x58dd, 0x504f, 0x2bbf, 0x0278, 0x70b7, 0xcfca michael@0: }; michael@0: michael@0: double michael@0: auth_bits_per_second(auth_t *h, int msg_len); michael@0: michael@0: michael@0: void michael@0: usage(char *prog_name) { michael@0: printf("usage: %s [ -t | -v ]\n", prog_name); michael@0: exit(255); michael@0: } michael@0: michael@0: #define MAX_MSG_LEN 2048 michael@0: michael@0: int michael@0: main (int argc, char *argv[]) { michael@0: auth_t *a = NULL; michael@0: err_status_t status; michael@0: int i; michael@0: int c; michael@0: unsigned do_timing_test = 0; michael@0: unsigned do_validation = 0; michael@0: michael@0: /* process input arguments */ michael@0: while (1) { michael@0: c = getopt(argc, argv, "tv"); michael@0: if (c == -1) michael@0: break; michael@0: switch (c) { michael@0: case 't': michael@0: do_timing_test = 1; michael@0: break; michael@0: case 'v': michael@0: do_validation = 1; michael@0: break; michael@0: default: michael@0: usage(argv[0]); michael@0: } michael@0: } michael@0: michael@0: printf("auth driver\nDavid A. McGrew\nCisco Systems, Inc.\n"); michael@0: michael@0: if (!do_validation && !do_timing_test) michael@0: usage(argv[0]); michael@0: michael@0: if (do_validation) { michael@0: printf("running self-test for %s...", tmmhv2.description); michael@0: status = tmmhv2_add_big_test(); michael@0: if (status) { michael@0: printf("tmmhv2_add_big_test failed with error code %d\n", status); michael@0: exit(status); michael@0: } michael@0: status = auth_type_self_test(&tmmhv2); michael@0: if (status) { michael@0: printf("failed with error code %d\n", status); michael@0: exit(status); michael@0: } michael@0: printf("passed\n"); michael@0: } michael@0: michael@0: if (do_timing_test) { michael@0: michael@0: /* tmmhv2 timing test */ michael@0: status = auth_type_alloc(&tmmhv2, &a, 94, 4); michael@0: if (status) { michael@0: fprintf(stderr, "can't allocate tmmhv2\n"); michael@0: exit(status); michael@0: } michael@0: status = auth_init(a, (uint8_t *)key1); michael@0: if (status) { michael@0: printf("error initializaing auth function\n"); michael@0: exit(status); michael@0: } michael@0: michael@0: printf("timing %s (tag length %d)\n", michael@0: tmmhv2.description, auth_get_tag_length(a)); michael@0: for (i=8; i <= MAX_MSG_LEN; i *= 2) michael@0: printf("msg len: %d\tgigabits per second: %f\n", michael@0: i, auth_bits_per_second(a, i) / 1E9); michael@0: michael@0: status = auth_dealloc(a); michael@0: if (status) { michael@0: printf("error deallocating auth function\n"); michael@0: exit(status); michael@0: } michael@0: michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: #define NUM_TRIALS 100000 michael@0: michael@0: #include michael@0: michael@0: double michael@0: auth_bits_per_second(auth_t *a, int msg_len_octets) { michael@0: int i; michael@0: clock_t timer; michael@0: uint8_t *result; michael@0: int msg_len = (msg_len_octets + 1)/2; michael@0: uint16_t *msg_string; michael@0: michael@0: /* create random message */ michael@0: msg_string = (uint16_t *) crypto_alloc(msg_len_octets); michael@0: if (msg_string == NULL) michael@0: return 0.0; /* indicate failure */ michael@0: for (i=0; i < msg_len; i++) michael@0: msg_string[i] = (uint16_t) random(); michael@0: michael@0: /* allocate temporary storage for authentication tag */ michael@0: result = crypto_alloc(auth_get_tag_length(a)); michael@0: if (result == NULL) { michael@0: free(msg_string); michael@0: return 0.0; /* indicate failure */ michael@0: } michael@0: michael@0: timer = clock(); michael@0: for (i=0; i < NUM_TRIALS; i++) { michael@0: auth_compute(a, (uint8_t *)msg_string, msg_len_octets, (uint8_t *)result); michael@0: } michael@0: timer = clock() - timer; michael@0: michael@0: free(msg_string); michael@0: free(result); michael@0: michael@0: return (double) NUM_TRIALS * 8 * msg_len_octets * CLOCKS_PER_SEC / timer; michael@0: } michael@0: michael@0: