2
0

auth.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * Copyright (c) 2001-2017, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47. #include "auth.h"
  48. #include "err.h" /* for srtp_debug */
  49. #include "datatypes.h" /* for octet_string */
  50. /* the debug module for authentiation */
  51. srtp_debug_module_t srtp_mod_auth = {
  52. 0, /* debugging is off by default */
  53. "auth func" /* printable name for module */
  54. };
  55. int srtp_auth_get_key_length(const srtp_auth_t *a)
  56. {
  57. return a->key_len;
  58. }
  59. int srtp_auth_get_tag_length(const srtp_auth_t *a)
  60. {
  61. return a->out_len;
  62. }
  63. int srtp_auth_get_prefix_length(const srtp_auth_t *a)
  64. {
  65. return a->prefix_len;
  66. }
  67. /*
  68. * srtp_auth_type_test() tests an auth function of type ct against
  69. * test cases provided in a list test_data of values of key, data, and tag
  70. * that is known to be good
  71. */
  72. /* should be big enough for most occasions */
  73. #define SELF_TEST_TAG_BUF_OCTETS 32
  74. srtp_err_status_t srtp_auth_type_test(const srtp_auth_type_t *at,
  75. const srtp_auth_test_case_t *test_data)
  76. {
  77. const srtp_auth_test_case_t *test_case = test_data;
  78. srtp_auth_t *a;
  79. srtp_err_status_t status;
  80. uint8_t tag[SELF_TEST_TAG_BUF_OCTETS];
  81. int i, case_num = 0;
  82. debug_print(srtp_mod_auth, "running self-test for auth function %s",
  83. at->description);
  84. /*
  85. * check to make sure that we have at least one test case, and
  86. * return an error if we don't - we need to be paranoid here
  87. */
  88. if (test_case == NULL) {
  89. return srtp_err_status_cant_check;
  90. }
  91. /* loop over all test cases */
  92. while (test_case != NULL) {
  93. /* check test case parameters */
  94. if (test_case->tag_length_octets > SELF_TEST_TAG_BUF_OCTETS) {
  95. return srtp_err_status_bad_param;
  96. }
  97. /* allocate auth */
  98. status = srtp_auth_type_alloc(at, &a, test_case->key_length_octets,
  99. test_case->tag_length_octets);
  100. if (status) {
  101. return status;
  102. }
  103. /* initialize auth */
  104. status = srtp_auth_init(a, test_case->key);
  105. if (status) {
  106. srtp_auth_dealloc(a);
  107. return status;
  108. }
  109. status = srtp_auth_start(a);
  110. if (status) {
  111. srtp_auth_dealloc(a);
  112. return status;
  113. }
  114. /* zeroize tag then compute */
  115. octet_string_set_to_zero(tag, test_case->tag_length_octets);
  116. status = srtp_auth_compute(a, test_case->data,
  117. test_case->data_length_octets, tag);
  118. if (status) {
  119. srtp_auth_dealloc(a);
  120. return status;
  121. }
  122. debug_print(srtp_mod_auth, "key: %s",
  123. srtp_octet_string_hex_string(test_case->key,
  124. test_case->key_length_octets));
  125. debug_print(srtp_mod_auth, "data: %s",
  126. srtp_octet_string_hex_string(
  127. test_case->data, test_case->data_length_octets));
  128. debug_print(
  129. srtp_mod_auth, "tag computed: %s",
  130. srtp_octet_string_hex_string(tag, test_case->tag_length_octets));
  131. debug_print(srtp_mod_auth, "tag expected: %s",
  132. srtp_octet_string_hex_string(test_case->tag,
  133. test_case->tag_length_octets));
  134. /* check the result */
  135. status = srtp_err_status_ok;
  136. for (i = 0; i < test_case->tag_length_octets; i++) {
  137. if (tag[i] != test_case->tag[i]) {
  138. status = srtp_err_status_algo_fail;
  139. debug_print(srtp_mod_auth, "test case %d failed", case_num);
  140. debug_print(srtp_mod_auth, " (mismatch at octet %d)", i);
  141. }
  142. }
  143. if (status) {
  144. srtp_auth_dealloc(a);
  145. return srtp_err_status_algo_fail;
  146. }
  147. /* deallocate the auth function */
  148. status = srtp_auth_dealloc(a);
  149. if (status) {
  150. return status;
  151. }
  152. /*
  153. * the auth function passed the test case, so move on to the next test
  154. * case in the list; if NULL, we'll quit and return an OK
  155. */
  156. test_case = test_case->next_test_case;
  157. ++case_num;
  158. }
  159. return srtp_err_status_ok;
  160. }
  161. /*
  162. * srtp_auth_type_self_test(at) performs srtp_auth_type_test on at's internal
  163. * list of test data.
  164. */
  165. srtp_err_status_t srtp_auth_type_self_test(const srtp_auth_type_t *at)
  166. {
  167. return srtp_auth_type_test(at, at->test_data);
  168. }