auth.c (6221B)
1 /* 2 * auth.c 3 * 4 * some bookkeeping functions for authentication functions 5 * 6 * David A. McGrew 7 * Cisco Systems, Inc. 8 */ 9 10 /* 11 * 12 * Copyright (c) 2001-2017, 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 #ifdef HAVE_CONFIG_H 47 #include <config.h> 48 #endif 49 50 #include "auth.h" 51 #include "err.h" /* for srtp_debug */ 52 #include "datatypes.h" /* for octet_string */ 53 54 /* the debug module for authentiation */ 55 56 srtp_debug_module_t srtp_mod_auth = { 57 0, /* debugging is off by default */ 58 "auth func" /* printable name for module */ 59 }; 60 61 int srtp_auth_get_key_length(const srtp_auth_t *a) 62 { 63 return a->key_len; 64 } 65 66 int srtp_auth_get_tag_length(const srtp_auth_t *a) 67 { 68 return a->out_len; 69 } 70 71 int srtp_auth_get_prefix_length(const srtp_auth_t *a) 72 { 73 return a->prefix_len; 74 } 75 76 /* 77 * srtp_auth_type_test() tests an auth function of type ct against 78 * test cases provided in a list test_data of values of key, data, and tag 79 * that is known to be good 80 */ 81 82 /* should be big enough for most occasions */ 83 #define SELF_TEST_TAG_BUF_OCTETS 32 84 85 srtp_err_status_t srtp_auth_type_test(const srtp_auth_type_t *at, 86 const srtp_auth_test_case_t *test_data) 87 { 88 const srtp_auth_test_case_t *test_case = test_data; 89 srtp_auth_t *a; 90 srtp_err_status_t status; 91 uint8_t tag[SELF_TEST_TAG_BUF_OCTETS]; 92 int i, case_num = 0; 93 94 debug_print(srtp_mod_auth, "running self-test for auth function %s", 95 at->description); 96 97 /* 98 * check to make sure that we have at least one test case, and 99 * return an error if we don't - we need to be paranoid here 100 */ 101 if (test_case == NULL) { 102 return srtp_err_status_cant_check; 103 } 104 105 /* loop over all test cases */ 106 while (test_case != NULL) { 107 /* check test case parameters */ 108 if (test_case->tag_length_octets > SELF_TEST_TAG_BUF_OCTETS) { 109 return srtp_err_status_bad_param; 110 } 111 112 /* allocate auth */ 113 status = srtp_auth_type_alloc(at, &a, test_case->key_length_octets, 114 test_case->tag_length_octets); 115 if (status) { 116 return status; 117 } 118 119 /* initialize auth */ 120 status = srtp_auth_init(a, test_case->key); 121 if (status) { 122 srtp_auth_dealloc(a); 123 return status; 124 } 125 126 status = srtp_auth_start(a); 127 if (status) { 128 srtp_auth_dealloc(a); 129 return status; 130 } 131 132 /* zeroize tag then compute */ 133 octet_string_set_to_zero(tag, test_case->tag_length_octets); 134 status = srtp_auth_compute(a, test_case->data, 135 test_case->data_length_octets, tag); 136 if (status) { 137 srtp_auth_dealloc(a); 138 return status; 139 } 140 141 debug_print(srtp_mod_auth, "key: %s", 142 srtp_octet_string_hex_string(test_case->key, 143 test_case->key_length_octets)); 144 debug_print(srtp_mod_auth, "data: %s", 145 srtp_octet_string_hex_string( 146 test_case->data, test_case->data_length_octets)); 147 debug_print( 148 srtp_mod_auth, "tag computed: %s", 149 srtp_octet_string_hex_string(tag, test_case->tag_length_octets)); 150 debug_print(srtp_mod_auth, "tag expected: %s", 151 srtp_octet_string_hex_string(test_case->tag, 152 test_case->tag_length_octets)); 153 154 /* check the result */ 155 status = srtp_err_status_ok; 156 for (i = 0; i < test_case->tag_length_octets; i++) { 157 if (tag[i] != test_case->tag[i]) { 158 status = srtp_err_status_algo_fail; 159 debug_print(srtp_mod_auth, "test case %d failed", case_num); 160 debug_print(srtp_mod_auth, " (mismatch at octet %d)", i); 161 } 162 } 163 if (status) { 164 srtp_auth_dealloc(a); 165 return srtp_err_status_algo_fail; 166 } 167 168 /* deallocate the auth function */ 169 status = srtp_auth_dealloc(a); 170 if (status) { 171 return status; 172 } 173 174 /* 175 * the auth function passed the test case, so move on to the next test 176 * case in the list; if NULL, we'll quit and return an OK 177 */ 178 test_case = test_case->next_test_case; 179 ++case_num; 180 } 181 182 return srtp_err_status_ok; 183 } 184 185 /* 186 * srtp_auth_type_self_test(at) performs srtp_auth_type_test on at's internal 187 * list of test data. 188 */ 189 190 srtp_err_status_t srtp_auth_type_self_test(const srtp_auth_type_t *at) 191 { 192 return srtp_auth_type_test(at, at->test_data); 193 }