hmac_ossl.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * hmac_ossl.c
  3. *
  4. * Implementation of hmac srtp_auth_type_t that leverages OpenSSL
  5. *
  6. * John A. Foley
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright(c) 2013-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 "alloc.h"
  49. #include "err.h" /* for srtp_debug */
  50. #include <openssl/evp.h>
  51. #include <openssl/hmac.h>
  52. #define SHA1_DIGEST_SIZE 20
  53. /* the debug module for authentiation */
  54. srtp_debug_module_t srtp_mod_hmac = {
  55. 0, /* debugging is off by default */
  56. "hmac sha-1 openssl" /* printable name for module */
  57. };
  58. static srtp_err_status_t srtp_hmac_alloc(srtp_auth_t **a,
  59. int key_len,
  60. int out_len)
  61. {
  62. extern const srtp_auth_type_t srtp_hmac;
  63. debug_print(srtp_mod_hmac, "allocating auth func with key length %d",
  64. key_len);
  65. debug_print(srtp_mod_hmac, " tag length %d",
  66. out_len);
  67. /* check output length - should be less than 20 bytes */
  68. if (out_len > SHA1_DIGEST_SIZE) {
  69. return srtp_err_status_bad_param;
  70. }
  71. /* OpenSSL 1.1.0 made HMAC_CTX an opaque structure, which must be allocated
  72. using HMAC_CTX_new. But this function doesn't exist in OpenSSL 1.0.x. */
  73. #if OPENSSL_VERSION_NUMBER < 0x10100000L || LIBRESSL_VERSION_NUMBER
  74. {
  75. /* allocate memory for auth and HMAC_CTX structures */
  76. uint8_t *pointer;
  77. HMAC_CTX *new_hmac_ctx;
  78. pointer = (uint8_t *)srtp_crypto_alloc(sizeof(HMAC_CTX) +
  79. sizeof(srtp_auth_t));
  80. if (pointer == NULL) {
  81. return srtp_err_status_alloc_fail;
  82. }
  83. *a = (srtp_auth_t *)pointer;
  84. (*a)->state = pointer + sizeof(srtp_auth_t);
  85. new_hmac_ctx = (HMAC_CTX *)((*a)->state);
  86. HMAC_CTX_init(new_hmac_ctx);
  87. }
  88. #else
  89. *a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
  90. if (*a == NULL) {
  91. return srtp_err_status_alloc_fail;
  92. }
  93. (*a)->state = HMAC_CTX_new();
  94. if ((*a)->state == NULL) {
  95. srtp_crypto_free(*a);
  96. *a = NULL;
  97. return srtp_err_status_alloc_fail;
  98. }
  99. #endif
  100. /* set pointers */
  101. (*a)->type = &srtp_hmac;
  102. (*a)->out_len = out_len;
  103. (*a)->key_len = key_len;
  104. (*a)->prefix_len = 0;
  105. return srtp_err_status_ok;
  106. }
  107. static srtp_err_status_t srtp_hmac_dealloc(srtp_auth_t *a)
  108. {
  109. HMAC_CTX *hmac_ctx;
  110. hmac_ctx = (HMAC_CTX *)a->state;
  111. #if OPENSSL_VERSION_NUMBER < 0x10100000L || LIBRESSL_VERSION_NUMBER
  112. HMAC_CTX_cleanup(hmac_ctx);
  113. /* zeroize entire state*/
  114. octet_string_set_to_zero(a, sizeof(HMAC_CTX) + sizeof(srtp_auth_t));
  115. #else
  116. HMAC_CTX_free(hmac_ctx);
  117. /* zeroize entire state*/
  118. octet_string_set_to_zero(a, sizeof(srtp_auth_t));
  119. #endif
  120. /* free memory */
  121. srtp_crypto_free(a);
  122. return srtp_err_status_ok;
  123. }
  124. static srtp_err_status_t srtp_hmac_start(void *statev)
  125. {
  126. HMAC_CTX *state = (HMAC_CTX *)statev;
  127. if (HMAC_Init_ex(state, NULL, 0, NULL, NULL) == 0)
  128. return srtp_err_status_auth_fail;
  129. return srtp_err_status_ok;
  130. }
  131. static srtp_err_status_t srtp_hmac_init(void *statev,
  132. const uint8_t *key,
  133. int key_len)
  134. {
  135. HMAC_CTX *state = (HMAC_CTX *)statev;
  136. if (HMAC_Init_ex(state, key, key_len, EVP_sha1(), NULL) == 0)
  137. return srtp_err_status_auth_fail;
  138. return srtp_err_status_ok;
  139. }
  140. static srtp_err_status_t srtp_hmac_update(void *statev,
  141. const uint8_t *message,
  142. int msg_octets)
  143. {
  144. HMAC_CTX *state = (HMAC_CTX *)statev;
  145. debug_print(srtp_mod_hmac, "input: %s",
  146. srtp_octet_string_hex_string(message, msg_octets));
  147. if (HMAC_Update(state, message, msg_octets) == 0)
  148. return srtp_err_status_auth_fail;
  149. return srtp_err_status_ok;
  150. }
  151. static srtp_err_status_t srtp_hmac_compute(void *statev,
  152. const uint8_t *message,
  153. int msg_octets,
  154. int tag_len,
  155. uint8_t *result)
  156. {
  157. HMAC_CTX *state = (HMAC_CTX *)statev;
  158. uint8_t hash_value[SHA1_DIGEST_SIZE];
  159. int i;
  160. unsigned int len;
  161. /* check tag length, return error if we can't provide the value expected */
  162. if (tag_len > SHA1_DIGEST_SIZE) {
  163. return srtp_err_status_bad_param;
  164. }
  165. /* hash message, copy output into H */
  166. if (HMAC_Update(state, message, msg_octets) == 0)
  167. return srtp_err_status_auth_fail;
  168. if (HMAC_Final(state, hash_value, &len) == 0)
  169. return srtp_err_status_auth_fail;
  170. if (len < tag_len)
  171. return srtp_err_status_auth_fail;
  172. /* copy hash_value to *result */
  173. for (i = 0; i < tag_len; i++) {
  174. result[i] = hash_value[i];
  175. }
  176. debug_print(srtp_mod_hmac, "output: %s",
  177. srtp_octet_string_hex_string(hash_value, tag_len));
  178. return srtp_err_status_ok;
  179. }
  180. /* begin test case 0 */
  181. /* clang-format off */
  182. static const uint8_t srtp_hmac_test_case_0_key[SHA1_DIGEST_SIZE] = {
  183. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  184. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  185. 0x0b, 0x0b, 0x0b, 0x0b
  186. };
  187. /* clang-format on */
  188. /* clang-format off */
  189. static const uint8_t srtp_hmac_test_case_0_data[8] = {
  190. 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 /* "Hi There" */
  191. };
  192. /* clang-format on */
  193. /* clang-format off */
  194. static const uint8_t srtp_hmac_test_case_0_tag[SHA1_DIGEST_SIZE] = {
  195. 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
  196. 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
  197. 0xf1, 0x46, 0xbe, 0x00
  198. };
  199. /* clang-format on */
  200. static const srtp_auth_test_case_t srtp_hmac_test_case_0 = {
  201. sizeof(srtp_hmac_test_case_0_key), /* octets in key */
  202. srtp_hmac_test_case_0_key, /* key */
  203. sizeof(srtp_hmac_test_case_0_data), /* octets in data */
  204. srtp_hmac_test_case_0_data, /* data */
  205. sizeof(srtp_hmac_test_case_0_tag), /* octets in tag */
  206. srtp_hmac_test_case_0_tag, /* tag */
  207. NULL /* pointer to next testcase */
  208. };
  209. /* end test case 0 */
  210. static const char srtp_hmac_description[] =
  211. "hmac sha-1 authentication function";
  212. /*
  213. * srtp_auth_type_t hmac is the hmac metaobject
  214. */
  215. const srtp_auth_type_t srtp_hmac = {
  216. srtp_hmac_alloc, /* */
  217. srtp_hmac_dealloc, /* */
  218. srtp_hmac_init, /* */
  219. srtp_hmac_compute, /* */
  220. srtp_hmac_update, /* */
  221. srtp_hmac_start, /* */
  222. srtp_hmac_description, /* */
  223. &srtp_hmac_test_case_0, /* */
  224. SRTP_HMAC_SHA1 /* */
  225. };