2
0

auth.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-2006, 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. #include "auth.h"
  45. /* the debug module for authentiation */
  46. debug_module_t mod_auth = {
  47. 0, /* debugging is off by default */
  48. "auth func" /* printable name for module */
  49. };
  50. int
  51. auth_get_key_length(const auth_t *a) {
  52. return a->key_len;
  53. }
  54. int
  55. auth_get_tag_length(const auth_t *a) {
  56. return a->out_len;
  57. }
  58. int
  59. auth_get_prefix_length(const auth_t *a) {
  60. return a->prefix_len;
  61. }
  62. int
  63. auth_type_get_ref_count(const auth_type_t *at) {
  64. return at->ref_count;
  65. }
  66. /*
  67. * auth_type_test() tests an auth function of type ct against
  68. * test cases provided in a list test_data of values of key, data, and tag
  69. * that is known to be good
  70. */
  71. /* should be big enough for most occasions */
  72. #define SELF_TEST_TAG_BUF_OCTETS 32
  73. err_status_t
  74. auth_type_test(const auth_type_t *at, const auth_test_case_t *test_data) {
  75. const auth_test_case_t *test_case = test_data;
  76. auth_t *a;
  77. err_status_t status;
  78. uint8_t tag[SELF_TEST_TAG_BUF_OCTETS];
  79. int i, case_num = 0;
  80. debug_print(mod_auth, "running self-test for auth function %s",
  81. at->description);
  82. /*
  83. * check to make sure that we have at least one test case, and
  84. * return an error if we don't - we need to be paranoid here
  85. */
  86. if (test_case == NULL)
  87. return err_status_cant_check;
  88. /* loop over all test cases */
  89. while (test_case != NULL) {
  90. /* check test case parameters */
  91. if (test_case->tag_length_octets > SELF_TEST_TAG_BUF_OCTETS)
  92. return err_status_bad_param;
  93. /* allocate auth */
  94. status = auth_type_alloc(at, &a, test_case->key_length_octets,
  95. test_case->tag_length_octets);
  96. if (status)
  97. return status;
  98. /* initialize auth */
  99. status = auth_init(a, test_case->key);
  100. if (status) {
  101. auth_dealloc(a);
  102. return status;
  103. }
  104. /* zeroize tag then compute */
  105. octet_string_set_to_zero(tag, test_case->tag_length_octets);
  106. status = auth_compute(a, test_case->data,
  107. test_case->data_length_octets, tag);
  108. if (status) {
  109. auth_dealloc(a);
  110. return status;
  111. }
  112. debug_print(mod_auth, "key: %s",
  113. octet_string_hex_string(test_case->key,
  114. test_case->key_length_octets));
  115. debug_print(mod_auth, "data: %s",
  116. octet_string_hex_string(test_case->data,
  117. test_case->data_length_octets));
  118. debug_print(mod_auth, "tag computed: %s",
  119. octet_string_hex_string(tag, test_case->tag_length_octets));
  120. debug_print(mod_auth, "tag expected: %s",
  121. octet_string_hex_string(test_case->tag,
  122. test_case->tag_length_octets));
  123. /* check the result */
  124. status = err_status_ok;
  125. for (i=0; i < test_case->tag_length_octets; i++)
  126. if (tag[i] != test_case->tag[i]) {
  127. status = err_status_algo_fail;
  128. debug_print(mod_auth, "test case %d failed", case_num);
  129. debug_print(mod_auth, " (mismatch at octet %d)", i);
  130. }
  131. if (status) {
  132. auth_dealloc(a);
  133. return err_status_algo_fail;
  134. }
  135. /* deallocate the auth function */
  136. status = auth_dealloc(a);
  137. if (status)
  138. return status;
  139. /*
  140. * the auth function passed the test case, so move on to the next test
  141. * case in the list; if NULL, we'll quit and return an OK
  142. */
  143. test_case = test_case->next_test_case;
  144. ++case_num;
  145. }
  146. return err_status_ok;
  147. }
  148. /*
  149. * auth_type_self_test(at) performs auth_type_test on at's internal
  150. * list of test data.
  151. */
  152. err_status_t
  153. auth_type_self_test(const auth_type_t *at) {
  154. return auth_type_test(at, at->test_data);
  155. }