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