hmac_prov.c 11 KB

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