netwerk/srtp/src/crypto/hash/auth.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.c
michael@0 3 *
michael@0 4 * some bookkeeping functions for authentication 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 #include "auth.h"
michael@0 47
michael@0 48 /* the debug module for authentiation */
michael@0 49
michael@0 50 debug_module_t mod_auth = {
michael@0 51 0, /* debugging is off by default */
michael@0 52 "auth func" /* printable name for module */
michael@0 53 };
michael@0 54
michael@0 55
michael@0 56 int
michael@0 57 auth_get_key_length(const auth_t *a) {
michael@0 58 return a->key_len;
michael@0 59 }
michael@0 60
michael@0 61 int
michael@0 62 auth_get_tag_length(const auth_t *a) {
michael@0 63 return a->out_len;
michael@0 64 }
michael@0 65
michael@0 66 int
michael@0 67 auth_get_prefix_length(const auth_t *a) {
michael@0 68 return a->prefix_len;
michael@0 69 }
michael@0 70
michael@0 71 int
michael@0 72 auth_type_get_ref_count(const auth_type_t *at) {
michael@0 73 return at->ref_count;
michael@0 74 }
michael@0 75
michael@0 76 /*
michael@0 77 * auth_type_test() tests an auth function of type ct against
michael@0 78 * test cases provided in a list test_data of values of key, data, and tag
michael@0 79 * that is known to be good
michael@0 80 */
michael@0 81
michael@0 82 /* should be big enough for most occasions */
michael@0 83 #define SELF_TEST_TAG_BUF_OCTETS 32
michael@0 84
michael@0 85 err_status_t
michael@0 86 auth_type_test(const auth_type_t *at, const auth_test_case_t *test_data) {
michael@0 87 const auth_test_case_t *test_case = test_data;
michael@0 88 auth_t *a;
michael@0 89 err_status_t status;
michael@0 90 uint8_t tag[SELF_TEST_TAG_BUF_OCTETS];
michael@0 91 int i, case_num = 0;
michael@0 92
michael@0 93 debug_print(mod_auth, "running self-test for auth function %s",
michael@0 94 at->description);
michael@0 95
michael@0 96 /*
michael@0 97 * check to make sure that we have at least one test case, and
michael@0 98 * return an error if we don't - we need to be paranoid here
michael@0 99 */
michael@0 100 if (test_case == NULL)
michael@0 101 return err_status_cant_check;
michael@0 102
michael@0 103 /* loop over all test cases */
michael@0 104 while (test_case != NULL) {
michael@0 105
michael@0 106 /* check test case parameters */
michael@0 107 if (test_case->tag_length_octets > SELF_TEST_TAG_BUF_OCTETS)
michael@0 108 return err_status_bad_param;
michael@0 109
michael@0 110 /* allocate auth */
michael@0 111 status = auth_type_alloc(at, &a, test_case->key_length_octets,
michael@0 112 test_case->tag_length_octets);
michael@0 113 if (status)
michael@0 114 return status;
michael@0 115
michael@0 116 /* initialize auth */
michael@0 117 status = auth_init(a, test_case->key);
michael@0 118 if (status) {
michael@0 119 auth_dealloc(a);
michael@0 120 return status;
michael@0 121 }
michael@0 122
michael@0 123 /* zeroize tag then compute */
michael@0 124 octet_string_set_to_zero(tag, test_case->tag_length_octets);
michael@0 125 status = auth_compute(a, test_case->data,
michael@0 126 test_case->data_length_octets, tag);
michael@0 127 if (status) {
michael@0 128 auth_dealloc(a);
michael@0 129 return status;
michael@0 130 }
michael@0 131
michael@0 132 debug_print(mod_auth, "key: %s",
michael@0 133 octet_string_hex_string(test_case->key,
michael@0 134 test_case->key_length_octets));
michael@0 135 debug_print(mod_auth, "data: %s",
michael@0 136 octet_string_hex_string(test_case->data,
michael@0 137 test_case->data_length_octets));
michael@0 138 debug_print(mod_auth, "tag computed: %s",
michael@0 139 octet_string_hex_string(tag, test_case->tag_length_octets));
michael@0 140 debug_print(mod_auth, "tag expected: %s",
michael@0 141 octet_string_hex_string(test_case->tag,
michael@0 142 test_case->tag_length_octets));
michael@0 143
michael@0 144 /* check the result */
michael@0 145 status = err_status_ok;
michael@0 146 for (i=0; i < test_case->tag_length_octets; i++)
michael@0 147 if (tag[i] != test_case->tag[i]) {
michael@0 148 status = err_status_algo_fail;
michael@0 149 debug_print(mod_auth, "test case %d failed", case_num);
michael@0 150 debug_print(mod_auth, " (mismatch at octet %d)", i);
michael@0 151 }
michael@0 152 if (status) {
michael@0 153 auth_dealloc(a);
michael@0 154 return err_status_algo_fail;
michael@0 155 }
michael@0 156
michael@0 157 /* deallocate the auth function */
michael@0 158 status = auth_dealloc(a);
michael@0 159 if (status)
michael@0 160 return status;
michael@0 161
michael@0 162 /*
michael@0 163 * the auth function passed the test case, so move on to the next test
michael@0 164 * case in the list; if NULL, we'll quit and return an OK
michael@0 165 */
michael@0 166 test_case = test_case->next_test_case;
michael@0 167 ++case_num;
michael@0 168 }
michael@0 169
michael@0 170 return err_status_ok;
michael@0 171 }
michael@0 172
michael@0 173
michael@0 174 /*
michael@0 175 * auth_type_self_test(at) performs auth_type_test on at's internal
michael@0 176 * list of test data.
michael@0 177 */
michael@0 178
michael@0 179 err_status_t
michael@0 180 auth_type_self_test(const auth_type_t *at) {
michael@0 181 return auth_type_test(at, at->test_data);
michael@0 182 }
michael@0 183

mercurial