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