auth.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /* zeroize tag then compute */
  110. octet_string_set_to_zero(tag, test_case->tag_length_octets);
  111. status = srtp_auth_compute(a, test_case->data,
  112. test_case->data_length_octets, tag);
  113. if (status) {
  114. srtp_auth_dealloc(a);
  115. return status;
  116. }
  117. debug_print(srtp_mod_auth, "key: %s",
  118. srtp_octet_string_hex_string(test_case->key,
  119. test_case->key_length_octets));
  120. debug_print(srtp_mod_auth, "data: %s",
  121. srtp_octet_string_hex_string(
  122. test_case->data, test_case->data_length_octets));
  123. debug_print(
  124. srtp_mod_auth, "tag computed: %s",
  125. srtp_octet_string_hex_string(tag, test_case->tag_length_octets));
  126. debug_print(srtp_mod_auth, "tag expected: %s",
  127. srtp_octet_string_hex_string(test_case->tag,
  128. test_case->tag_length_octets));
  129. /* check the result */
  130. status = srtp_err_status_ok;
  131. for (i = 0; i < test_case->tag_length_octets; i++) {
  132. if (tag[i] != test_case->tag[i]) {
  133. status = srtp_err_status_algo_fail;
  134. debug_print(srtp_mod_auth, "test case %d failed", case_num);
  135. debug_print(srtp_mod_auth, " (mismatch at octet %d)", i);
  136. }
  137. }
  138. if (status) {
  139. srtp_auth_dealloc(a);
  140. return srtp_err_status_algo_fail;
  141. }
  142. /* deallocate the auth function */
  143. status = srtp_auth_dealloc(a);
  144. if (status) {
  145. return status;
  146. }
  147. /*
  148. * the auth function passed the test case, so move on to the next test
  149. * case in the list; if NULL, we'll quit and return an OK
  150. */
  151. test_case = test_case->next_test_case;
  152. ++case_num;
  153. }
  154. return srtp_err_status_ok;
  155. }
  156. /*
  157. * srtp_auth_type_self_test(at) performs srtp_auth_type_test on at's internal
  158. * list of test data.
  159. */
  160. srtp_err_status_t srtp_auth_type_self_test(const srtp_auth_type_t *at)
  161. {
  162. return srtp_auth_type_test(at, at->test_data);
  163. }