hmacdrbg_kdf.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2022-2023 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. #include <stdlib.h>
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/err.h>
  13. #include <openssl/kdf.h>
  14. #include <openssl/proverr.h>
  15. #include <openssl/core_names.h>
  16. #include "prov/providercommon.h"
  17. #include "prov/implementations.h"
  18. #include "prov/hmac_drbg.h"
  19. #include "prov/provider_ctx.h"
  20. static OSSL_FUNC_kdf_newctx_fn hmac_drbg_kdf_new;
  21. static OSSL_FUNC_kdf_dupctx_fn hmac_drbg_kdf_dup;
  22. static OSSL_FUNC_kdf_freectx_fn hmac_drbg_kdf_free;
  23. static OSSL_FUNC_kdf_reset_fn hmac_drbg_kdf_reset;
  24. static OSSL_FUNC_kdf_derive_fn hmac_drbg_kdf_derive;
  25. static OSSL_FUNC_kdf_settable_ctx_params_fn hmac_drbg_kdf_settable_ctx_params;
  26. static OSSL_FUNC_kdf_set_ctx_params_fn hmac_drbg_kdf_set_ctx_params;
  27. static OSSL_FUNC_kdf_gettable_ctx_params_fn hmac_drbg_kdf_gettable_ctx_params;
  28. static OSSL_FUNC_kdf_get_ctx_params_fn hmac_drbg_kdf_get_ctx_params;
  29. typedef struct {
  30. PROV_DRBG_HMAC base;
  31. void *provctx;
  32. unsigned char *entropy, *nonce;
  33. size_t entropylen, noncelen;
  34. int init;
  35. } KDF_HMAC_DRBG;
  36. static void *hmac_drbg_kdf_new(void *provctx)
  37. {
  38. KDF_HMAC_DRBG *ctx;
  39. if (!ossl_prov_is_running())
  40. return NULL;
  41. ctx = OPENSSL_zalloc(sizeof(*ctx));
  42. if (ctx == NULL) {
  43. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  44. return NULL;
  45. }
  46. ctx->provctx = provctx;
  47. return ctx;
  48. }
  49. static void hmac_drbg_kdf_reset(void *vctx)
  50. {
  51. KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
  52. PROV_DRBG_HMAC *drbg = &ctx->base;
  53. void *provctx = ctx->provctx;
  54. EVP_MAC_CTX_free(drbg->ctx);
  55. ossl_prov_digest_reset(&drbg->digest);
  56. OPENSSL_clear_free(ctx->entropy, ctx->entropylen);
  57. OPENSSL_clear_free(ctx->nonce, ctx->noncelen);
  58. OPENSSL_cleanse(ctx, sizeof(*ctx));
  59. ctx->provctx = provctx;
  60. }
  61. static void hmac_drbg_kdf_free(void *vctx)
  62. {
  63. KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
  64. if (ctx != NULL) {
  65. hmac_drbg_kdf_reset(ctx);
  66. OPENSSL_free(ctx);
  67. }
  68. }
  69. static int ossl_drbg_hmac_dup(PROV_DRBG_HMAC *dst, const PROV_DRBG_HMAC *src) {
  70. if (src->ctx != NULL) {
  71. dst->ctx = EVP_MAC_CTX_dup(src->ctx);
  72. if (dst->ctx == NULL)
  73. return 0;
  74. }
  75. if (!ossl_prov_digest_copy(&dst->digest, &src->digest))
  76. return 0;
  77. memcpy(dst->K, src->K, sizeof(dst->K));
  78. memcpy(dst->V, src->V, sizeof(dst->V));
  79. dst->blocklen = src->blocklen;
  80. return 1;
  81. }
  82. static void *hmac_drbg_kdf_dup(void *vctx)
  83. {
  84. const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx;
  85. KDF_HMAC_DRBG *dst;
  86. dst = hmac_drbg_kdf_new(src->provctx);
  87. if (dst != NULL) {
  88. if (!ossl_drbg_hmac_dup(&dst->base, &src->base)
  89. || !ossl_prov_memdup(src->entropy, src->entropylen,
  90. &dst->entropy , &dst->entropylen)
  91. || !ossl_prov_memdup(src->nonce, src->noncelen,
  92. &dst->nonce, &dst->noncelen))
  93. goto err;
  94. dst->init = src->init;
  95. }
  96. return dst;
  97. err:
  98. hmac_drbg_kdf_free(dst);
  99. return NULL;
  100. }
  101. static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen,
  102. const OSSL_PARAM params[])
  103. {
  104. KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
  105. PROV_DRBG_HMAC *drbg = &ctx->base;
  106. if (!ossl_prov_is_running()
  107. || !hmac_drbg_kdf_set_ctx_params(vctx, params))
  108. return 0;
  109. if (!ctx->init) {
  110. if (ctx->entropy == NULL
  111. || ctx->entropylen == 0
  112. || ctx->nonce == NULL
  113. || ctx->noncelen == 0
  114. || !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,
  115. ctx->nonce, ctx->noncelen, NULL, 0))
  116. return 0;
  117. ctx->init = 1;
  118. }
  119. return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);
  120. }
  121. static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  122. {
  123. KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
  124. PROV_DRBG_HMAC *drbg = &hmac->base;
  125. const char *name;
  126. const EVP_MD *md;
  127. OSSL_PARAM *p;
  128. p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_MAC);
  129. if (p != NULL) {
  130. if (drbg->ctx == NULL)
  131. return 0;
  132. name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx));
  133. if (!OSSL_PARAM_set_utf8_string(p, name))
  134. return 0;
  135. }
  136. p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_DIGEST);
  137. if (p != NULL) {
  138. md = ossl_prov_digest_md(&drbg->digest);
  139. if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params(
  145. ossl_unused void *vctx, ossl_unused void *p_ctx)
  146. {
  147. static const OSSL_PARAM known_gettable_ctx_params[] = {
  148. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
  149. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  150. OSSL_PARAM_END
  151. };
  152. return known_gettable_ctx_params;
  153. }
  154. static int hmac_drbg_kdf_set_ctx_params(void *vctx,
  155. const OSSL_PARAM params[])
  156. {
  157. KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
  158. PROV_DRBG_HMAC *drbg = &hmac->base;
  159. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(hmac->provctx);
  160. const EVP_MD *md;
  161. const OSSL_PARAM *p;
  162. void *ptr = NULL;
  163. size_t size = 0;
  164. int md_size;
  165. if (params == NULL)
  166. return 1;
  167. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_ENTROPY);
  168. if (p != NULL) {
  169. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  170. return 0;
  171. OPENSSL_free(hmac->entropy);
  172. hmac->entropy = ptr;
  173. hmac->entropylen = size;
  174. hmac->init = 0;
  175. ptr = NULL;
  176. }
  177. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_NONCE);
  178. if (p != NULL) {
  179. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  180. return 0;
  181. OPENSSL_free(hmac->nonce);
  182. hmac->nonce = ptr;
  183. hmac->noncelen = size;
  184. hmac->init = 0;
  185. }
  186. p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
  187. if (p != NULL) {
  188. if (!ossl_prov_digest_load_from_params(&drbg->digest, params, libctx))
  189. return 0;
  190. /* Confirm digest is allowed. Allow all digests that are not XOF */
  191. md = ossl_prov_digest_md(&drbg->digest);
  192. if (md != NULL) {
  193. if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) {
  194. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  195. return 0;
  196. }
  197. md_size = EVP_MD_get_size(md);
  198. if (md_size <= 0)
  199. return 0;
  200. drbg->blocklen = (size_t)md_size;
  201. }
  202. return ossl_prov_macctx_load_from_params(&drbg->ctx, params,
  203. "HMAC", NULL, NULL, libctx);
  204. }
  205. return 1;
  206. }
  207. static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(
  208. ossl_unused void *vctx, ossl_unused void *p_ctx)
  209. {
  210. static const OSSL_PARAM known_settable_ctx_params[] = {
  211. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, NULL, 0),
  212. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, NULL, 0),
  213. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  214. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  215. OSSL_PARAM_END
  216. };
  217. return known_settable_ctx_params;
  218. }
  219. const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[] = {
  220. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))hmac_drbg_kdf_new },
  221. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))hmac_drbg_kdf_free },
  222. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))hmac_drbg_kdf_dup },
  223. { OSSL_FUNC_KDF_RESET, (void(*)(void))hmac_drbg_kdf_reset },
  224. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))hmac_drbg_kdf_derive },
  225. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  226. (void(*)(void))hmac_drbg_kdf_settable_ctx_params },
  227. { OSSL_FUNC_KDF_SET_CTX_PARAMS,
  228. (void(*)(void))hmac_drbg_kdf_set_ctx_params },
  229. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  230. (void(*)(void))hmac_drbg_kdf_gettable_ctx_params },
  231. { OSSL_FUNC_KDF_GET_CTX_PARAMS,
  232. (void(*)(void))hmac_drbg_kdf_get_ctx_params },
  233. OSSL_DISPATCH_END
  234. };