hmac_ossl.c 8.0 KB

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