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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * auth_driver.c
michael@0 3 *
michael@0 4 * a driver for auth functions
michael@0 5 *
michael@0 6 * David A. McGrew
michael@0 7 * Cisco Systems, Inc.
michael@0 8 */
michael@0 9
michael@0 10 /*
michael@0 11 *
michael@0 12 * Copyright (c) 2001-2006, Cisco Systems, Inc.
michael@0 13 * All rights reserved.
michael@0 14 *
michael@0 15 * Redistribution and use in source and binary forms, with or without
michael@0 16 * modification, are permitted provided that the following conditions
michael@0 17 * are met:
michael@0 18 *
michael@0 19 * Redistributions of source code must retain the above copyright
michael@0 20 * notice, this list of conditions and the following disclaimer.
michael@0 21 *
michael@0 22 * Redistributions in binary form must reproduce the above
michael@0 23 * copyright notice, this list of conditions and the following
michael@0 24 * disclaimer in the documentation and/or other materials provided
michael@0 25 * with the distribution.
michael@0 26 *
michael@0 27 * Neither the name of the Cisco Systems, Inc. nor the names of its
michael@0 28 * contributors may be used to endorse or promote products derived
michael@0 29 * from this software without specific prior written permission.
michael@0 30 *
michael@0 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
michael@0 34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
michael@0 35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
michael@0 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
michael@0 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
michael@0 42 * OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 43 *
michael@0 44 */
michael@0 45
michael@0 46
michael@0 47 #include <stdio.h> /* for printf() */
michael@0 48 #include <stdlib.h> /* for xalloc() */
michael@0 49 #include <unistd.h> /* for getopt() */
michael@0 50
michael@0 51 #include "auth.h"
michael@0 52 #include "null_auth.h"
michael@0 53
michael@0 54 #define PRINT_DEBUG_DATA 0
michael@0 55
michael@0 56 extern auth_type_t tmmhv2;
michael@0 57
michael@0 58 const uint16_t msg0[9] = {
michael@0 59 0x6015, 0xf141, 0x5ba1, 0x29a0, 0xf604, 0xd1c, 0x2d9, 0xaa8a, 0x7931
michael@0 60 };
michael@0 61
michael@0 62 /* key1 is for TAG_WORDS = 2 */
michael@0 63
michael@0 64 const uint16_t key1[47] = {
michael@0 65 0xe627, 0x6a01, 0x5ea7, 0xf27a, 0xc536, 0x2192, 0x11be, 0xea35,
michael@0 66 0xdb9d, 0x63d6, 0xfa8a, 0xfc45, 0xe08b, 0xd216, 0xced2, 0x7853,
michael@0 67 0x1a82, 0x22f5, 0x90fb, 0x1c29, 0x708e, 0xd06f, 0x82c3, 0xbee6,
michael@0 68 0x4f21, 0x6f33, 0x65c0, 0xd211, 0xc25e, 0x9138, 0x4fa3, 0x7c1f,
michael@0 69 0x61ac, 0x3489, 0x2976, 0x8c19, 0x8252, 0xddbf, 0xcad3, 0xc28f,
michael@0 70 0x68d6, 0x58dd, 0x504f, 0x2bbf, 0x0278, 0x70b7, 0xcfca
michael@0 71 };
michael@0 72
michael@0 73 double
michael@0 74 auth_bits_per_second(auth_t *h, int msg_len);
michael@0 75
michael@0 76
michael@0 77 void
michael@0 78 usage(char *prog_name) {
michael@0 79 printf("usage: %s [ -t | -v ]\n", prog_name);
michael@0 80 exit(255);
michael@0 81 }
michael@0 82
michael@0 83 #define MAX_MSG_LEN 2048
michael@0 84
michael@0 85 int
michael@0 86 main (int argc, char *argv[]) {
michael@0 87 auth_t *a = NULL;
michael@0 88 err_status_t status;
michael@0 89 int i;
michael@0 90 int c;
michael@0 91 unsigned do_timing_test = 0;
michael@0 92 unsigned do_validation = 0;
michael@0 93
michael@0 94 /* process input arguments */
michael@0 95 while (1) {
michael@0 96 c = getopt(argc, argv, "tv");
michael@0 97 if (c == -1)
michael@0 98 break;
michael@0 99 switch (c) {
michael@0 100 case 't':
michael@0 101 do_timing_test = 1;
michael@0 102 break;
michael@0 103 case 'v':
michael@0 104 do_validation = 1;
michael@0 105 break;
michael@0 106 default:
michael@0 107 usage(argv[0]);
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 printf("auth driver\nDavid A. McGrew\nCisco Systems, Inc.\n");
michael@0 112
michael@0 113 if (!do_validation && !do_timing_test)
michael@0 114 usage(argv[0]);
michael@0 115
michael@0 116 if (do_validation) {
michael@0 117 printf("running self-test for %s...", tmmhv2.description);
michael@0 118 status = tmmhv2_add_big_test();
michael@0 119 if (status) {
michael@0 120 printf("tmmhv2_add_big_test failed with error code %d\n", status);
michael@0 121 exit(status);
michael@0 122 }
michael@0 123 status = auth_type_self_test(&tmmhv2);
michael@0 124 if (status) {
michael@0 125 printf("failed with error code %d\n", status);
michael@0 126 exit(status);
michael@0 127 }
michael@0 128 printf("passed\n");
michael@0 129 }
michael@0 130
michael@0 131 if (do_timing_test) {
michael@0 132
michael@0 133 /* tmmhv2 timing test */
michael@0 134 status = auth_type_alloc(&tmmhv2, &a, 94, 4);
michael@0 135 if (status) {
michael@0 136 fprintf(stderr, "can't allocate tmmhv2\n");
michael@0 137 exit(status);
michael@0 138 }
michael@0 139 status = auth_init(a, (uint8_t *)key1);
michael@0 140 if (status) {
michael@0 141 printf("error initializaing auth function\n");
michael@0 142 exit(status);
michael@0 143 }
michael@0 144
michael@0 145 printf("timing %s (tag length %d)\n",
michael@0 146 tmmhv2.description, auth_get_tag_length(a));
michael@0 147 for (i=8; i <= MAX_MSG_LEN; i *= 2)
michael@0 148 printf("msg len: %d\tgigabits per second: %f\n",
michael@0 149 i, auth_bits_per_second(a, i) / 1E9);
michael@0 150
michael@0 151 status = auth_dealloc(a);
michael@0 152 if (status) {
michael@0 153 printf("error deallocating auth function\n");
michael@0 154 exit(status);
michael@0 155 }
michael@0 156
michael@0 157 }
michael@0 158
michael@0 159 return 0;
michael@0 160 }
michael@0 161
michael@0 162 #define NUM_TRIALS 100000
michael@0 163
michael@0 164 #include <time.h>
michael@0 165
michael@0 166 double
michael@0 167 auth_bits_per_second(auth_t *a, int msg_len_octets) {
michael@0 168 int i;
michael@0 169 clock_t timer;
michael@0 170 uint8_t *result;
michael@0 171 int msg_len = (msg_len_octets + 1)/2;
michael@0 172 uint16_t *msg_string;
michael@0 173
michael@0 174 /* create random message */
michael@0 175 msg_string = (uint16_t *) crypto_alloc(msg_len_octets);
michael@0 176 if (msg_string == NULL)
michael@0 177 return 0.0; /* indicate failure */
michael@0 178 for (i=0; i < msg_len; i++)
michael@0 179 msg_string[i] = (uint16_t) random();
michael@0 180
michael@0 181 /* allocate temporary storage for authentication tag */
michael@0 182 result = crypto_alloc(auth_get_tag_length(a));
michael@0 183 if (result == NULL) {
michael@0 184 free(msg_string);
michael@0 185 return 0.0; /* indicate failure */
michael@0 186 }
michael@0 187
michael@0 188 timer = clock();
michael@0 189 for (i=0; i < NUM_TRIALS; i++) {
michael@0 190 auth_compute(a, (uint8_t *)msg_string, msg_len_octets, (uint8_t *)result);
michael@0 191 }
michael@0 192 timer = clock() - timer;
michael@0 193
michael@0 194 free(msg_string);
michael@0 195 free(result);
michael@0 196
michael@0 197 return (double) NUM_TRIALS * 8 * msg_len_octets * CLOCKS_PER_SEC / timer;
michael@0 198 }
michael@0 199
michael@0 200

mercurial