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 
srtp_auth_get_key_length(const srtp_auth_t * a)61 int srtp_auth_get_key_length(const srtp_auth_t *a)
62 {
63     return a->key_len;
64 }
65 
srtp_auth_get_tag_length(const srtp_auth_t * a)66 int srtp_auth_get_tag_length(const srtp_auth_t *a)
67 {
68     return a->out_len;
69 }
70 
srtp_auth_get_prefix_length(const srtp_auth_t * a)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 
srtp_auth_type_test(const srtp_auth_type_t * at,const srtp_auth_test_case_t * test_data)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         /* zeroize tag then compute */
127         octet_string_set_to_zero(tag, test_case->tag_length_octets);
128         status = srtp_auth_compute(a, test_case->data,
129                                    test_case->data_length_octets, tag);
130         if (status) {
131             srtp_auth_dealloc(a);
132             return status;
133         }
134 
135         debug_print(srtp_mod_auth, "key: %s",
136                     srtp_octet_string_hex_string(test_case->key,
137                                                  test_case->key_length_octets));
138         debug_print(srtp_mod_auth, "data: %s",
139                     srtp_octet_string_hex_string(
140                         test_case->data, test_case->data_length_octets));
141         debug_print(
142             srtp_mod_auth, "tag computed: %s",
143             srtp_octet_string_hex_string(tag, test_case->tag_length_octets));
144         debug_print(srtp_mod_auth, "tag expected: %s",
145                     srtp_octet_string_hex_string(test_case->tag,
146                                                  test_case->tag_length_octets));
147 
148         /* check the result */
149         status = srtp_err_status_ok;
150         for (i = 0; i < test_case->tag_length_octets; i++) {
151             if (tag[i] != test_case->tag[i]) {
152                 status = srtp_err_status_algo_fail;
153                 debug_print(srtp_mod_auth, "test case %d failed", case_num);
154                 debug_print(srtp_mod_auth, "  (mismatch at octet %d)", i);
155             }
156         }
157         if (status) {
158             srtp_auth_dealloc(a);
159             return srtp_err_status_algo_fail;
160         }
161 
162         /* deallocate the auth function */
163         status = srtp_auth_dealloc(a);
164         if (status) {
165             return status;
166         }
167 
168         /*
169          * the auth function passed the test case, so move on to the next test
170          * case in the list; if NULL, we'll quit and return an OK
171          */
172         test_case = test_case->next_test_case;
173         ++case_num;
174     }
175 
176     return srtp_err_status_ok;
177 }
178 
179 /*
180  * srtp_auth_type_self_test(at) performs srtp_auth_type_test on at's internal
181  * list of test data.
182  */
183 
srtp_auth_type_self_test(const srtp_auth_type_t * at)184 srtp_err_status_t srtp_auth_type_self_test(const srtp_auth_type_t *at)
185 {
186     return srtp_auth_type_test(at, at->test_data);
187 }
188