1386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/*
2386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * auth_driver.c
3386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
4386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * a driver for auth functions
5386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
6386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * David A. McGrew
7386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Cisco Systems, Inc.
8386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari */
9386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
10386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/*
11386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
12386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Copyright (c) 2001-2006, Cisco Systems, Inc.
13386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * All rights reserved.
14386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
15386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Redistribution and use in source and binary forms, with or without
16386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * modification, are permitted provided that the following conditions
17386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * are met:
18386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
19386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   Redistributions of source code must retain the above copyright
20386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   notice, this list of conditions and the following disclaimer.
21386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
22386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   Redistributions in binary form must reproduce the above
23386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   copyright notice, this list of conditions and the following
24386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   disclaimer in the documentation and/or other materials provided
25386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   with the distribution.
26386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
27386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   Neither the name of the Cisco Systems, Inc. nor the names of its
28386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   contributors may be used to endorse or promote products derived
29386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   from this software without specific prior written permission.
30386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
31386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * OF THE POSSIBILITY OF SUCH DAMAGE.
43386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
44386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari */
45386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
46386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
47386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include <stdio.h>    /* for printf() */
48386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include <stdlib.h>   /* for xalloc() */
49386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include <unistd.h>   /* for getopt() */
50386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
51386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include "auth.h"
52386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include "null_auth.h"
53386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
54386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#define PRINT_DEBUG_DATA 0
55386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
56386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariextern auth_type_t tmmhv2;
57386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
58386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariconst uint16_t msg0[9] = {
59386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0x6015, 0xf141, 0x5ba1, 0x29a0, 0xf604, 0xd1c, 0x2d9, 0xaa8a, 0x7931
60386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari};
61386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
62386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/* key1 is for TAG_WORDS = 2 */
63386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
64386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariconst uint16_t key1[47] = {
65386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0xe627, 0x6a01, 0x5ea7, 0xf27a, 0xc536, 0x2192, 0x11be, 0xea35,
66386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0xdb9d, 0x63d6, 0xfa8a, 0xfc45, 0xe08b, 0xd216, 0xced2, 0x7853,
67386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0x1a82, 0x22f5, 0x90fb, 0x1c29, 0x708e, 0xd06f, 0x82c3, 0xbee6,
68386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0x4f21, 0x6f33, 0x65c0, 0xd211, 0xc25e, 0x9138, 0x4fa3, 0x7c1f,
69386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0x61ac, 0x3489, 0x2976, 0x8c19, 0x8252, 0xddbf, 0xcad3, 0xc28f,
70386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0x68d6, 0x58dd, 0x504f, 0x2bbf, 0x0278, 0x70b7, 0xcfca
71386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari};
72386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
73386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaridouble
74386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariauth_bits_per_second(auth_t *h, int msg_len);
75386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
76386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
77386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagarivoid
78386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariusage(char *prog_name) {
79386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  printf("usage: %s [ -t | -v ]\n", prog_name);
80386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  exit(255);
81386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
82386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
83386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#define MAX_MSG_LEN 2048
84386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
85386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariint
86386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagarimain (int argc, char *argv[]) {
87386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  auth_t *a = NULL;
88386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  err_status_t status;
89386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  int i;
90386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  int c;
91386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  unsigned do_timing_test = 0;
92386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  unsigned do_validation = 0;
93386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
94386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  /* process input arguments */
95386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  while (1) {
96386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    c = getopt(argc, argv, "tv");
97386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    if (c == -1)
98386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      break;
99386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    switch (c) {
100386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    case 't':
101386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      do_timing_test = 1;
102386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      break;
103386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    case 'v':
104386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      do_validation = 1;
105386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      break;
106386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    default:
107386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      usage(argv[0]);
108386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    }
109386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  }
110386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
111386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  printf("auth driver\nDavid A. McGrew\nCisco Systems, Inc.\n");
112386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
113386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (!do_validation && !do_timing_test)
114386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    usage(argv[0]);
115386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
116386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (do_validation) {
117386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    printf("running self-test for %s...", tmmhv2.description);
118386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    status = tmmhv2_add_big_test();
119386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    if (status) {
120386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      printf("tmmhv2_add_big_test failed with error code %d\n", status);
121386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      exit(status);
122386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    }
123386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    status = auth_type_self_test(&tmmhv2);
124386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    if (status) {
125386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      printf("failed with error code %d\n", status);
126386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      exit(status);
127386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    }
128386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    printf("passed\n");
129386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  }
130386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
131386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (do_timing_test) {
132386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
133386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    /* tmmhv2 timing test */
134386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    status = auth_type_alloc(&tmmhv2, &a, 94, 4);
135386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    if (status) {
136386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      fprintf(stderr, "can't allocate tmmhv2\n");
137386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      exit(status);
138386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    }
139386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    status = auth_init(a, (uint8_t *)key1);
140386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    if (status) {
141386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      printf("error initializaing auth function\n");
142386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      exit(status);
143386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    }
144386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
145386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    printf("timing %s (tag length %d)\n",
146386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari	   tmmhv2.description, auth_get_tag_length(a));
147386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    for (i=8; i <= MAX_MSG_LEN; i *= 2)
148386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      printf("msg len: %d\tgigabits per second: %f\n",
149386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari	     i, auth_bits_per_second(a, i) / 1E9);
150386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
151386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    status = auth_dealloc(a);
152386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    if (status) {
153386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      printf("error deallocating auth function\n");
154386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari      exit(status);
155386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    }
156386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
157386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  }
158386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
159386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  return 0;
160386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
161386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
162386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#define NUM_TRIALS 100000
163386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
164386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include <time.h>
165386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
166386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaridouble
167386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagariauth_bits_per_second(auth_t *a, int msg_len_octets) {
168386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  int i;
169386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  clock_t timer;
170386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  uint8_t *result;
171386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  int msg_len = (msg_len_octets + 1)/2;
172386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  uint16_t *msg_string;
173386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
174386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  /* create random message */
175386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  msg_string = (uint16_t *) crypto_alloc(msg_len_octets);
176386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (msg_string == NULL)
177386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    return 0.0; /* indicate failure */
178386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  for (i=0; i < msg_len; i++)
179386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    msg_string[i] = (uint16_t) random();
180386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
181386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  /* allocate temporary storage for authentication tag */
182386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  result = crypto_alloc(auth_get_tag_length(a));
183386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (result == NULL) {
184386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    free(msg_string);
185386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    return 0.0; /* indicate failure */
186386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  }
187386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
188386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  timer = clock();
189386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  for (i=0; i < NUM_TRIALS; i++) {
190386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    auth_compute(a, (uint8_t *)msg_string, msg_len_octets, (uint8_t *)result);
191386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  }
192386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  timer = clock() - timer;
193386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
194386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  free(msg_string);
195386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  free(result);
196386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
197386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  return (double) NUM_TRIALS * 8 * msg_len_octets * CLOCKS_PER_SEC / timer;
198386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
199386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
200386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
201