hmac_prov.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * HMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/params.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/hmac.h>
  20. #include "internal/ssl3_cbc.h"
  21. #include "prov/implementations.h"
  22. #include "prov/provider_ctx.h"
  23. #include "prov/provider_util.h"
  24. #include "prov/providercommon.h"
  25. /*
  26. * Forward declaration of everything implemented here. This is not strictly
  27. * necessary for the compiler, but provides an assurance that the signatures
  28. * of the functions in the dispatch table are correct.
  29. */
  30. static OSSL_FUNC_mac_newctx_fn hmac_new;
  31. static OSSL_FUNC_mac_dupctx_fn hmac_dup;
  32. static OSSL_FUNC_mac_freectx_fn hmac_free;
  33. static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
  34. static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
  35. static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
  36. static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
  37. static OSSL_FUNC_mac_init_fn hmac_init;
  38. static OSSL_FUNC_mac_update_fn hmac_update;
  39. static OSSL_FUNC_mac_final_fn hmac_final;
  40. /* local HMAC context structure */
  41. /* typedef EVP_MAC_IMPL */
  42. struct hmac_data_st {
  43. void *provctx;
  44. HMAC_CTX *ctx; /* HMAC context */
  45. PROV_DIGEST digest;
  46. unsigned char *key;
  47. size_t keylen;
  48. /* Length of full TLS record including the MAC and any padding */
  49. size_t tls_data_size;
  50. unsigned char tls_header[13];
  51. int tls_header_set;
  52. unsigned char tls_mac_out[EVP_MAX_MD_SIZE];
  53. size_t tls_mac_out_size;
  54. };
  55. static void *hmac_new(void *provctx)
  56. {
  57. struct hmac_data_st *macctx;
  58. if (!ossl_prov_is_running())
  59. return NULL;
  60. if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  61. || (macctx->ctx = HMAC_CTX_new()) == NULL) {
  62. OPENSSL_free(macctx);
  63. return NULL;
  64. }
  65. macctx->provctx = provctx;
  66. return macctx;
  67. }
  68. static void hmac_free(void *vmacctx)
  69. {
  70. struct hmac_data_st *macctx = vmacctx;
  71. if (macctx != NULL) {
  72. HMAC_CTX_free(macctx->ctx);
  73. ossl_prov_digest_reset(&macctx->digest);
  74. OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
  75. OPENSSL_free(macctx);
  76. }
  77. }
  78. static void *hmac_dup(void *vsrc)
  79. {
  80. struct hmac_data_st *src = vsrc;
  81. struct hmac_data_st *dst;
  82. HMAC_CTX *ctx;
  83. if (!ossl_prov_is_running())
  84. return NULL;
  85. dst = hmac_new(src->provctx);
  86. if (dst == NULL)
  87. return NULL;
  88. ctx = dst->ctx;
  89. *dst = *src;
  90. dst->ctx = ctx;
  91. dst->key = NULL;
  92. memset(&dst->digest, 0, sizeof(dst->digest));
  93. if (!HMAC_CTX_copy(dst->ctx, src->ctx)
  94. || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
  95. hmac_free(dst);
  96. return NULL;
  97. }
  98. if (src->key != NULL) {
  99. /* There is no "secure" OPENSSL_memdup */
  100. dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1);
  101. if (dst->key == NULL) {
  102. hmac_free(dst);
  103. return 0;
  104. }
  105. memcpy(dst->key, src->key, src->keylen);
  106. }
  107. return dst;
  108. }
  109. static size_t hmac_size(struct hmac_data_st *macctx)
  110. {
  111. return HMAC_size(macctx->ctx);
  112. }
  113. static int hmac_block_size(struct hmac_data_st *macctx)
  114. {
  115. const EVP_MD *md = ossl_prov_digest_md(&macctx->digest);
  116. if (md == NULL)
  117. return 0;
  118. return EVP_MD_block_size(md);
  119. }
  120. static int hmac_setkey(struct hmac_data_st *macctx,
  121. const unsigned char *key, size_t keylen)
  122. {
  123. const EVP_MD *digest;
  124. if (macctx->key != NULL)
  125. OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
  126. /* Keep a copy of the key in case we need it for TLS HMAC */
  127. macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1);
  128. if (macctx->key == NULL)
  129. return 0;
  130. memcpy(macctx->key, key, keylen);
  131. macctx->keylen = keylen;
  132. digest = ossl_prov_digest_md(&macctx->digest);
  133. /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
  134. if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
  135. return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
  136. ossl_prov_digest_engine(&macctx->digest));
  137. return 1;
  138. }
  139. static int hmac_init(void *vmacctx, const unsigned char *key,
  140. size_t keylen, const OSSL_PARAM params[])
  141. {
  142. struct hmac_data_st *macctx = vmacctx;
  143. if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
  144. return 0;
  145. if (key != NULL)
  146. return hmac_setkey(macctx, key, keylen);
  147. /* Just reinit the HMAC context */
  148. return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL);
  149. }
  150. static int hmac_update(void *vmacctx, const unsigned char *data,
  151. size_t datalen)
  152. {
  153. struct hmac_data_st *macctx = vmacctx;
  154. if (macctx->tls_data_size > 0) {
  155. /* We're doing a TLS HMAC */
  156. if (!macctx->tls_header_set) {
  157. /* We expect the first update call to contain the TLS header */
  158. if (datalen != sizeof(macctx->tls_header))
  159. return 0;
  160. memcpy(macctx->tls_header, data, datalen);
  161. macctx->tls_header_set = 1;
  162. return 1;
  163. }
  164. /* macctx->tls_data_size is datalen plus the padding length */
  165. if (macctx->tls_data_size < datalen)
  166. return 0;
  167. return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
  168. macctx->tls_mac_out,
  169. &macctx->tls_mac_out_size,
  170. macctx->tls_header,
  171. data,
  172. datalen,
  173. macctx->tls_data_size,
  174. macctx->key,
  175. macctx->keylen,
  176. 0);
  177. }
  178. return HMAC_Update(macctx->ctx, data, datalen);
  179. }
  180. static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  181. size_t outsize)
  182. {
  183. unsigned int hlen;
  184. struct hmac_data_st *macctx = vmacctx;
  185. if (!ossl_prov_is_running())
  186. return 0;
  187. if (macctx->tls_data_size > 0) {
  188. if (macctx->tls_mac_out_size == 0)
  189. return 0;
  190. if (outl != NULL)
  191. *outl = macctx->tls_mac_out_size;
  192. memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
  193. return 1;
  194. }
  195. if (!HMAC_Final(macctx->ctx, out, &hlen))
  196. return 0;
  197. *outl = hlen;
  198. return 1;
  199. }
  200. static const OSSL_PARAM known_gettable_ctx_params[] = {
  201. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  202. OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
  203. OSSL_PARAM_END
  204. };
  205. static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
  206. ossl_unused void *provctx)
  207. {
  208. return known_gettable_ctx_params;
  209. }
  210. static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  211. {
  212. struct hmac_data_st *macctx = vmacctx;
  213. OSSL_PARAM *p;
  214. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
  215. && !OSSL_PARAM_set_size_t(p, hmac_size(macctx)))
  216. return 0;
  217. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
  218. && !OSSL_PARAM_set_int(p, hmac_block_size(macctx)))
  219. return 0;
  220. return 1;
  221. }
  222. static const OSSL_PARAM known_settable_ctx_params[] = {
  223. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
  224. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  225. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  226. OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
  227. OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
  228. OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
  229. OSSL_PARAM_END
  230. };
  231. static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
  232. ossl_unused void *provctx)
  233. {
  234. return known_settable_ctx_params;
  235. }
  236. static int set_flag(const OSSL_PARAM params[], const char *key, int mask,
  237. int *flags)
  238. {
  239. const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key);
  240. int flag = 0;
  241. if (p != NULL) {
  242. if (!OSSL_PARAM_get_int(p, &flag))
  243. return 0;
  244. if (flag == 0)
  245. *flags &= ~mask;
  246. else
  247. *flags |= mask;
  248. }
  249. return 1;
  250. }
  251. /*
  252. * ALL parameters should be set before init().
  253. */
  254. static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  255. {
  256. struct hmac_data_st *macctx = vmacctx;
  257. OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
  258. const OSSL_PARAM *p;
  259. int flags = 0;
  260. if (params == NULL)
  261. return 1;
  262. if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
  263. return 0;
  264. if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT,
  265. &flags))
  266. return 0;
  267. if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT,
  268. &flags))
  269. return 0;
  270. if (flags)
  271. HMAC_CTX_set_flags(macctx->ctx, flags);
  272. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  273. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  274. return 0;
  275. if (!hmac_setkey(macctx, p->data, p->data_size))
  276. return 0;
  277. }
  278. if ((p = OSSL_PARAM_locate_const(params,
  279. OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
  280. if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
  281. return 0;
  282. }
  283. return 1;
  284. }
  285. const OSSL_DISPATCH ossl_hmac_functions[] = {
  286. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
  287. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
  288. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
  289. { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
  290. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
  291. { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
  292. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  293. (void (*)(void))hmac_gettable_ctx_params },
  294. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
  295. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  296. (void (*)(void))hmac_settable_ctx_params },
  297. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
  298. OSSL_DISPATCH_END
  299. };