hmac_ossl.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "auth_test_cases.h"
  51. #include <openssl/evp.h>
  52. #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
  53. #define SRTP_OSSL_USE_EVP_MAC
  54. /* before this version reinit of EVP_MAC_CTX was not supported so need to
  55. * duplicate the CTX each time */
  56. #define SRTP_OSSL_MIN_REINIT_VERSION 0x30000030L
  57. #endif
  58. #ifndef SRTP_OSSL_USE_EVP_MAC
  59. #include <openssl/hmac.h>
  60. #endif
  61. #define SHA1_DIGEST_SIZE 20
  62. /* the debug module for authentication */
  63. srtp_debug_module_t srtp_mod_hmac = {
  64. 0, /* debugging is off by default */
  65. "hmac sha-1 openssl" /* printable name for module */
  66. };
  67. /*
  68. * There are three different behaviors of OpenSSL HMAC for different versions.
  69. *
  70. * 1. Pre-3.0 - Use HMAC API
  71. * 2. 3.0.0 - 3.0.2 - EVP API is required, but doesn't support reinitialization,
  72. * so we have to use EVP_MAC_CTX_dup
  73. * 3. 3.0.3 and later - EVP API is required and supports reinitialization
  74. *
  75. * The distingtion between cases 2 & 3 needs to be made at runtime, because in a
  76. * shared library context you might end up building against 3.0.3 and running
  77. * against 3.0.2.
  78. */
  79. typedef struct {
  80. #ifdef SRTP_OSSL_USE_EVP_MAC
  81. EVP_MAC *mac;
  82. EVP_MAC_CTX *ctx;
  83. int use_dup;
  84. EVP_MAC_CTX *ctx_dup;
  85. #else
  86. HMAC_CTX *ctx;
  87. #endif
  88. } srtp_hmac_ossl_ctx_t;
  89. static srtp_err_status_t srtp_hmac_alloc(srtp_auth_t **a,
  90. int key_len,
  91. int out_len)
  92. {
  93. extern const srtp_auth_type_t srtp_hmac;
  94. srtp_hmac_ossl_ctx_t *hmac;
  95. debug_print(srtp_mod_hmac, "allocating auth func with key length %d",
  96. key_len);
  97. debug_print(srtp_mod_hmac, " tag length %d",
  98. out_len);
  99. /* check output length - should be less than 20 bytes */
  100. if (out_len > SHA1_DIGEST_SIZE) {
  101. return srtp_err_status_bad_param;
  102. }
  103. *a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
  104. if (*a == NULL) {
  105. return srtp_err_status_alloc_fail;
  106. }
  107. hmac =
  108. (srtp_hmac_ossl_ctx_t *)srtp_crypto_alloc(sizeof(srtp_hmac_ossl_ctx_t));
  109. if (hmac == NULL) {
  110. srtp_crypto_free(*a);
  111. *a = NULL;
  112. return srtp_err_status_alloc_fail;
  113. }
  114. #ifdef SRTP_OSSL_USE_EVP_MAC
  115. hmac->mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
  116. if (hmac->mac == NULL) {
  117. srtp_crypto_free(hmac);
  118. srtp_crypto_free(*a);
  119. *a = NULL;
  120. return srtp_err_status_alloc_fail;
  121. }
  122. hmac->ctx = EVP_MAC_CTX_new(hmac->mac);
  123. if (hmac->ctx == NULL) {
  124. EVP_MAC_free(hmac->mac);
  125. srtp_crypto_free(hmac);
  126. srtp_crypto_free(*a);
  127. *a = NULL;
  128. return srtp_err_status_alloc_fail;
  129. }
  130. hmac->use_dup =
  131. OpenSSL_version_num() < SRTP_OSSL_MIN_REINIT_VERSION ? 1 : 0;
  132. if (hmac->use_dup) {
  133. debug_print0(srtp_mod_hmac, "using EVP_MAC_CTX_dup");
  134. hmac->ctx_dup = hmac->ctx;
  135. hmac->ctx = NULL;
  136. }
  137. #else
  138. hmac->ctx = HMAC_CTX_new();
  139. if (hmac->ctx == NULL) {
  140. srtp_crypto_free(hmac);
  141. srtp_crypto_free(*a);
  142. *a = NULL;
  143. return srtp_err_status_alloc_fail;
  144. }
  145. #endif
  146. /* set pointers */
  147. (*a)->state = hmac;
  148. (*a)->type = &srtp_hmac;
  149. (*a)->out_len = out_len;
  150. (*a)->key_len = key_len;
  151. (*a)->prefix_len = 0;
  152. return srtp_err_status_ok;
  153. }
  154. static srtp_err_status_t srtp_hmac_dealloc(srtp_auth_t *a)
  155. {
  156. srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)a->state;
  157. if (hmac) {
  158. #ifdef SRTP_OSSL_USE_EVP_MAC
  159. EVP_MAC_CTX_free(hmac->ctx);
  160. EVP_MAC_CTX_free(hmac->ctx_dup);
  161. EVP_MAC_free(hmac->mac);
  162. #else
  163. HMAC_CTX_free(hmac->ctx);
  164. #endif
  165. /* zeroize entire state*/
  166. octet_string_set_to_zero(hmac, sizeof(srtp_hmac_ossl_ctx_t));
  167. srtp_crypto_free(hmac);
  168. }
  169. /* zeroize entire state*/
  170. octet_string_set_to_zero(a, sizeof(srtp_auth_t));
  171. /* free memory */
  172. srtp_crypto_free(a);
  173. return srtp_err_status_ok;
  174. }
  175. static srtp_err_status_t srtp_hmac_start(void *statev)
  176. {
  177. srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
  178. #ifdef SRTP_OSSL_USE_EVP_MAC
  179. if (hmac->use_dup) {
  180. EVP_MAC_CTX_free(hmac->ctx);
  181. hmac->ctx = EVP_MAC_CTX_dup(hmac->ctx_dup);
  182. if (hmac->ctx == NULL) {
  183. return srtp_err_status_alloc_fail;
  184. }
  185. } else {
  186. if (EVP_MAC_init(hmac->ctx, NULL, 0, NULL) == 0) {
  187. return srtp_err_status_auth_fail;
  188. }
  189. }
  190. #else
  191. if (HMAC_Init_ex(hmac->ctx, NULL, 0, NULL, NULL) == 0)
  192. return srtp_err_status_auth_fail;
  193. #endif
  194. return srtp_err_status_ok;
  195. }
  196. static srtp_err_status_t srtp_hmac_init(void *statev,
  197. const uint8_t *key,
  198. int key_len)
  199. {
  200. srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
  201. #ifdef SRTP_OSSL_USE_EVP_MAC
  202. OSSL_PARAM params[2];
  203. params[0] = OSSL_PARAM_construct_utf8_string("digest", "SHA1", 0);
  204. params[1] = OSSL_PARAM_construct_end();
  205. if (EVP_MAC_init(hmac->use_dup ? hmac->ctx_dup : hmac->ctx, key, key_len,
  206. params) == 0) {
  207. return srtp_err_status_auth_fail;
  208. }
  209. #else
  210. if (HMAC_Init_ex(hmac->ctx, key, key_len, EVP_sha1(), NULL) == 0)
  211. return srtp_err_status_auth_fail;
  212. #endif
  213. return srtp_err_status_ok;
  214. }
  215. static srtp_err_status_t srtp_hmac_update(void *statev,
  216. const uint8_t *message,
  217. int msg_octets)
  218. {
  219. srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
  220. debug_print(srtp_mod_hmac, "input: %s",
  221. srtp_octet_string_hex_string(message, msg_octets));
  222. #ifdef SRTP_OSSL_USE_EVP_MAC
  223. if (EVP_MAC_update(hmac->ctx, message, msg_octets) == 0) {
  224. return srtp_err_status_auth_fail;
  225. }
  226. #else
  227. if (HMAC_Update(hmac->ctx, message, msg_octets) == 0)
  228. return srtp_err_status_auth_fail;
  229. #endif
  230. return srtp_err_status_ok;
  231. }
  232. static srtp_err_status_t srtp_hmac_compute(void *statev,
  233. const uint8_t *message,
  234. int msg_octets,
  235. int tag_len,
  236. uint8_t *result)
  237. {
  238. srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
  239. uint8_t hash_value[SHA1_DIGEST_SIZE];
  240. int i;
  241. #ifdef SRTP_OSSL_USE_EVP_MAC
  242. size_t len;
  243. #else
  244. unsigned int len;
  245. #endif
  246. debug_print(srtp_mod_hmac, "input: %s",
  247. srtp_octet_string_hex_string(message, msg_octets));
  248. /* check tag length, return error if we can't provide the value expected */
  249. if (tag_len > SHA1_DIGEST_SIZE) {
  250. return srtp_err_status_bad_param;
  251. }
  252. /* hash message, copy output into H */
  253. #ifdef SRTP_OSSL_USE_EVP_MAC
  254. if (EVP_MAC_update(hmac->ctx, message, msg_octets) == 0) {
  255. return srtp_err_status_auth_fail;
  256. }
  257. if (EVP_MAC_final(hmac->ctx, hash_value, &len, sizeof hash_value) == 0) {
  258. return srtp_err_status_auth_fail;
  259. }
  260. #else
  261. if (HMAC_Update(hmac->ctx, message, msg_octets) == 0)
  262. return srtp_err_status_auth_fail;
  263. if (HMAC_Final(hmac->ctx, hash_value, &len) == 0)
  264. return srtp_err_status_auth_fail;
  265. #endif
  266. if (tag_len < 0 || len < (unsigned int)tag_len)
  267. return srtp_err_status_auth_fail;
  268. /* copy hash_value to *result */
  269. for (i = 0; i < tag_len; i++) {
  270. result[i] = hash_value[i];
  271. }
  272. debug_print(srtp_mod_hmac, "output: %s",
  273. srtp_octet_string_hex_string(hash_value, tag_len));
  274. return srtp_err_status_ok;
  275. }
  276. static const char srtp_hmac_description[] =
  277. "hmac sha-1 authentication function";
  278. /*
  279. * srtp_auth_type_t hmac is the hmac metaobject
  280. */
  281. const srtp_auth_type_t srtp_hmac = {
  282. srtp_hmac_alloc, /* */
  283. srtp_hmac_dealloc, /* */
  284. srtp_hmac_init, /* */
  285. srtp_hmac_compute, /* */
  286. srtp_hmac_update, /* */
  287. srtp_hmac_start, /* */
  288. srtp_hmac_description, /* */
  289. &srtp_hmac_test_case_0, /* */
  290. SRTP_HMAC_SHA1 /* */
  291. };