hmac_prov.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2018-2021 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/engine.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/hmac.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. /* Defined in ssl/s3_cbc.c */
  56. int ssl3_cbc_digest_record(const EVP_MD *md,
  57. unsigned char *md_out,
  58. size_t *md_out_size,
  59. const unsigned char header[13],
  60. const unsigned char *data,
  61. size_t data_size,
  62. size_t data_plus_mac_plus_padding_size,
  63. const unsigned char *mac_secret,
  64. size_t mac_secret_length, char is_sslv3);
  65. static void *hmac_new(void *provctx)
  66. {
  67. struct hmac_data_st *macctx;
  68. if (!ossl_prov_is_running())
  69. return NULL;
  70. if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  71. || (macctx->ctx = HMAC_CTX_new()) == NULL) {
  72. OPENSSL_free(macctx);
  73. return NULL;
  74. }
  75. macctx->provctx = provctx;
  76. return macctx;
  77. }
  78. static void hmac_free(void *vmacctx)
  79. {
  80. struct hmac_data_st *macctx = vmacctx;
  81. if (macctx != NULL) {
  82. HMAC_CTX_free(macctx->ctx);
  83. ossl_prov_digest_reset(&macctx->digest);
  84. OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
  85. OPENSSL_free(macctx);
  86. }
  87. }
  88. static void *hmac_dup(void *vsrc)
  89. {
  90. struct hmac_data_st *src = vsrc;
  91. struct hmac_data_st *dst;
  92. HMAC_CTX *ctx;
  93. if (!ossl_prov_is_running())
  94. return NULL;
  95. dst = hmac_new(src->provctx);
  96. if (dst == NULL)
  97. return NULL;
  98. ctx = dst->ctx;
  99. *dst = *src;
  100. dst->ctx = ctx;
  101. dst->key = NULL;
  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(void *vmacctx)
  119. {
  120. struct hmac_data_st *macctx = vmacctx;
  121. return HMAC_size(macctx->ctx);
  122. }
  123. static int hmac_setkey(struct hmac_data_st *macctx,
  124. const unsigned char *key, size_t keylen)
  125. {
  126. const EVP_MD *digest;
  127. if (macctx->keylen > 0)
  128. OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
  129. /* Keep a copy of the key in case we need it for TLS HMAC */
  130. macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1);
  131. if (macctx->key == NULL)
  132. return 0;
  133. memcpy(macctx->key, key, keylen);
  134. macctx->keylen = keylen;
  135. digest = ossl_prov_digest_md(&macctx->digest);
  136. /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
  137. if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
  138. return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
  139. ossl_prov_digest_engine(&macctx->digest));
  140. return 1;
  141. }
  142. static int hmac_init(void *vmacctx, const unsigned char *key,
  143. size_t keylen, const OSSL_PARAM params[])
  144. {
  145. struct hmac_data_st *macctx = vmacctx;
  146. if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
  147. return 0;
  148. if (key != NULL && !hmac_setkey(macctx, key, keylen))
  149. return 0;
  150. return 1;
  151. }
  152. static int hmac_update(void *vmacctx, const unsigned char *data,
  153. size_t datalen)
  154. {
  155. struct hmac_data_st *macctx = vmacctx;
  156. if (macctx->tls_data_size > 0) {
  157. /* We're doing a TLS HMAC */
  158. if (!macctx->tls_header_set) {
  159. /* We expect the first update call to contain the TLS header */
  160. if (datalen != sizeof(macctx->tls_header))
  161. return 0;
  162. memcpy(macctx->tls_header, data, datalen);
  163. macctx->tls_header_set = 1;
  164. return 1;
  165. }
  166. /* macctx->tls_data_size is datalen plus the padding length */
  167. if (macctx->tls_data_size < datalen)
  168. return 0;
  169. return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
  170. macctx->tls_mac_out,
  171. &macctx->tls_mac_out_size,
  172. macctx->tls_header,
  173. data,
  174. datalen,
  175. macctx->tls_data_size,
  176. macctx->key,
  177. macctx->keylen,
  178. 0);
  179. }
  180. return HMAC_Update(macctx->ctx, data, datalen);
  181. }
  182. static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  183. size_t outsize)
  184. {
  185. unsigned int hlen;
  186. struct hmac_data_st *macctx = vmacctx;
  187. if (!ossl_prov_is_running())
  188. return 0;
  189. if (macctx->tls_data_size > 0) {
  190. if (macctx->tls_mac_out_size == 0)
  191. return 0;
  192. if (outl != NULL)
  193. *outl = macctx->tls_mac_out_size;
  194. memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
  195. return 1;
  196. }
  197. if (!HMAC_Final(macctx->ctx, out, &hlen))
  198. return 0;
  199. *outl = hlen;
  200. return 1;
  201. }
  202. static const OSSL_PARAM known_gettable_ctx_params[] = {
  203. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  204. OSSL_PARAM_END
  205. };
  206. static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
  207. ossl_unused void *provctx)
  208. {
  209. return known_gettable_ctx_params;
  210. }
  211. static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  212. {
  213. OSSL_PARAM *p;
  214. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  215. return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx));
  216. return 1;
  217. }
  218. static const OSSL_PARAM known_settable_ctx_params[] = {
  219. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
  220. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  221. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  222. OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
  223. OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
  224. OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
  225. OSSL_PARAM_END
  226. };
  227. static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
  228. ossl_unused void *provctx)
  229. {
  230. return known_settable_ctx_params;
  231. }
  232. static int set_flag(const OSSL_PARAM params[], const char *key, int mask,
  233. int *flags)
  234. {
  235. const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key);
  236. int flag = 0;
  237. if (p != NULL) {
  238. if (!OSSL_PARAM_get_int(p, &flag))
  239. return 0;
  240. if (flag == 0)
  241. *flags &= ~mask;
  242. else
  243. *flags |= mask;
  244. }
  245. return 1;
  246. }
  247. /*
  248. * ALL parameters should be set before init().
  249. */
  250. static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  251. {
  252. struct hmac_data_st *macctx = vmacctx;
  253. OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
  254. const OSSL_PARAM *p;
  255. int flags = 0;
  256. if (params == NULL)
  257. return 1;
  258. if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
  259. return 0;
  260. if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT,
  261. &flags))
  262. return 0;
  263. if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT,
  264. &flags))
  265. return 0;
  266. if (flags)
  267. HMAC_CTX_set_flags(macctx->ctx, flags);
  268. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  269. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  270. return 0;
  271. if (macctx->keylen > 0)
  272. OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
  273. /* Keep a copy of the key if we need it for TLS HMAC */
  274. macctx->key = OPENSSL_secure_malloc(p->data_size > 0 ? p->data_size : 1);
  275. if (macctx->key == NULL)
  276. return 0;
  277. memcpy(macctx->key, p->data, p->data_size);
  278. macctx->keylen = p->data_size;
  279. if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size,
  280. ossl_prov_digest_md(&macctx->digest),
  281. NULL /* ENGINE */))
  282. return 0;
  283. }
  284. if ((p = OSSL_PARAM_locate_const(params,
  285. OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
  286. if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
  287. return 0;
  288. }
  289. return 1;
  290. }
  291. const OSSL_DISPATCH ossl_hmac_functions[] = {
  292. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
  293. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
  294. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
  295. { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
  296. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
  297. { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
  298. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  299. (void (*)(void))hmac_gettable_ctx_params },
  300. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
  301. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  302. (void (*)(void))hmac_settable_ctx_params },
  303. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
  304. { 0, NULL }
  305. };