2
0

hmac.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * hmac.c
  3. *
  4. * implementation of hmac auth_type_t
  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 "hmac.h"
  45. #include "alloc.h"
  46. /* the debug module for authentiation */
  47. debug_module_t mod_hmac = {
  48. 0, /* debugging is off by default */
  49. "hmac sha-1" /* printable name for module */
  50. };
  51. err_status_t
  52. hmac_alloc(auth_t **a, int key_len, int out_len) {
  53. extern auth_type_t hmac;
  54. uint8_t *pointer;
  55. debug_print(mod_hmac, "allocating auth func with key length %d", key_len);
  56. debug_print(mod_hmac, " tag length %d", out_len);
  57. /*
  58. * check key length - note that we don't support keys larger
  59. * than 20 bytes yet
  60. */
  61. if (key_len > 20)
  62. return err_status_bad_param;
  63. /* check output length - should be less than 20 bytes */
  64. if (out_len > 20)
  65. return err_status_bad_param;
  66. /* allocate memory for auth and hmac_ctx_t structures */
  67. pointer = (uint8_t*)crypto_alloc(sizeof(hmac_ctx_t) + sizeof(auth_t));
  68. if (pointer == NULL)
  69. return err_status_alloc_fail;
  70. /* set pointers */
  71. *a = (auth_t *)pointer;
  72. (*a)->type = &hmac;
  73. (*a)->state = pointer + sizeof(auth_t);
  74. (*a)->out_len = out_len;
  75. (*a)->key_len = key_len;
  76. (*a)->prefix_len = 0;
  77. /* increment global count of all hmac uses */
  78. hmac.ref_count++;
  79. return err_status_ok;
  80. }
  81. err_status_t
  82. hmac_dealloc(auth_t *a) {
  83. extern auth_type_t hmac;
  84. /* zeroize entire state*/
  85. octet_string_set_to_zero((uint8_t *)a,
  86. sizeof(hmac_ctx_t) + sizeof(auth_t));
  87. /* free memory */
  88. crypto_free(a);
  89. /* decrement global count of all hmac uses */
  90. hmac.ref_count--;
  91. return err_status_ok;
  92. }
  93. err_status_t
  94. hmac_init(hmac_ctx_t *state, const uint8_t *key, int key_len) {
  95. int i;
  96. uint8_t ipad[64];
  97. /*
  98. * check key length - note that we don't support keys larger
  99. * than 20 bytes yet
  100. */
  101. if (key_len > 20)
  102. return err_status_bad_param;
  103. /*
  104. * set values of ipad and opad by exoring the key into the
  105. * appropriate constant values
  106. */
  107. for (i=0; i < key_len; i++) {
  108. ipad[i] = key[i] ^ 0x36;
  109. state->opad[i] = key[i] ^ 0x5c;
  110. }
  111. /* set the rest of ipad, opad to constant values */
  112. for ( ; i < 64; i++) {
  113. ipad[i] = 0x36;
  114. ((uint8_t *)state->opad)[i] = 0x5c;
  115. }
  116. debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(ipad, 64));
  117. /* initialize sha1 context */
  118. sha1_init(&state->init_ctx);
  119. /* hash ipad ^ key */
  120. sha1_update(&state->init_ctx, ipad, 64);
  121. memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t));
  122. return err_status_ok;
  123. }
  124. err_status_t
  125. hmac_start(hmac_ctx_t *state) {
  126. memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t));
  127. return err_status_ok;
  128. }
  129. err_status_t
  130. hmac_update(hmac_ctx_t *state, const uint8_t *message, int msg_octets) {
  131. debug_print(mod_hmac, "input: %s",
  132. octet_string_hex_string(message, msg_octets));
  133. /* hash message into sha1 context */
  134. sha1_update(&state->ctx, message, msg_octets);
  135. return err_status_ok;
  136. }
  137. err_status_t
  138. hmac_compute(hmac_ctx_t *state, const void *message,
  139. int msg_octets, int tag_len, uint8_t *result) {
  140. uint32_t hash_value[5];
  141. uint32_t H[5];
  142. int i;
  143. /* check tag length, return error if we can't provide the value expected */
  144. if (tag_len > 20)
  145. return err_status_bad_param;
  146. /* hash message, copy output into H */
  147. hmac_update(state, (const uint8_t*)message, msg_octets);
  148. sha1_final(&state->ctx, H);
  149. /*
  150. * note that we don't need to debug_print() the input, since the
  151. * function hmac_update() already did that for us
  152. */
  153. debug_print(mod_hmac, "intermediate state: %s",
  154. octet_string_hex_string((uint8_t *)H, 20));
  155. /* re-initialize hash context */
  156. sha1_init(&state->ctx);
  157. /* hash opad ^ key */
  158. sha1_update(&state->ctx, (uint8_t *)state->opad, 64);
  159. /* hash the result of the inner hash */
  160. sha1_update(&state->ctx, (uint8_t *)H, 20);
  161. /* the result is returned in the array hash_value[] */
  162. sha1_final(&state->ctx, hash_value);
  163. /* copy hash_value to *result */
  164. for (i=0; i < tag_len; i++)
  165. result[i] = ((uint8_t *)hash_value)[i];
  166. debug_print(mod_hmac, "output: %s",
  167. octet_string_hex_string((uint8_t *)hash_value, tag_len));
  168. return err_status_ok;
  169. }
  170. /* begin test case 0 */
  171. uint8_t
  172. hmac_test_case_0_key[20] = {
  173. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  174. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  175. 0x0b, 0x0b, 0x0b, 0x0b
  176. };
  177. uint8_t
  178. hmac_test_case_0_data[8] = {
  179. 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 /* "Hi There" */
  180. };
  181. uint8_t
  182. hmac_test_case_0_tag[20] = {
  183. 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
  184. 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
  185. 0xf1, 0x46, 0xbe, 0x00
  186. };
  187. auth_test_case_t
  188. hmac_test_case_0 = {
  189. 20, /* octets in key */
  190. hmac_test_case_0_key, /* key */
  191. 8, /* octets in data */
  192. hmac_test_case_0_data, /* data */
  193. 20, /* octets in tag */
  194. hmac_test_case_0_tag, /* tag */
  195. NULL /* pointer to next testcase */
  196. };
  197. /* end test case 0 */
  198. char hmac_description[] = "hmac sha-1 authentication function";
  199. /*
  200. * auth_type_t hmac is the hmac metaobject
  201. */
  202. auth_type_t
  203. hmac = {
  204. (auth_alloc_func) hmac_alloc,
  205. (auth_dealloc_func) hmac_dealloc,
  206. (auth_init_func) hmac_init,
  207. (auth_compute_func) hmac_compute,
  208. (auth_update_func) hmac_update,
  209. (auth_start_func) hmac_start,
  210. (char *) hmac_description,
  211. (int) 0, /* instance count */
  212. (auth_test_case_t *) &hmac_test_case_0,
  213. (debug_module_t *) &mod_hmac,
  214. (auth_type_id_t) HMAC_SHA1
  215. };