kdf_exch.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 2020-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 <openssl/crypto.h>
  10. #include <openssl/kdf.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/err.h>
  14. #include <openssl/proverr.h>
  15. #include <openssl/params.h>
  16. #include "internal/numbers.h"
  17. #include "prov/implementations.h"
  18. #include "prov/provider_ctx.h"
  19. #include "prov/kdfexchange.h"
  20. #include "prov/providercommon.h"
  21. static OSSL_FUNC_keyexch_newctx_fn kdf_tls1_prf_newctx;
  22. static OSSL_FUNC_keyexch_newctx_fn kdf_hkdf_newctx;
  23. static OSSL_FUNC_keyexch_newctx_fn kdf_scrypt_newctx;
  24. static OSSL_FUNC_keyexch_init_fn kdf_init;
  25. static OSSL_FUNC_keyexch_derive_fn kdf_derive;
  26. static OSSL_FUNC_keyexch_freectx_fn kdf_freectx;
  27. static OSSL_FUNC_keyexch_dupctx_fn kdf_dupctx;
  28. static OSSL_FUNC_keyexch_set_ctx_params_fn kdf_set_ctx_params;
  29. static OSSL_FUNC_keyexch_get_ctx_params_fn kdf_get_ctx_params;
  30. static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
  31. static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
  32. static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_scrypt_settable_ctx_params;
  33. static OSSL_FUNC_keyexch_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
  34. static OSSL_FUNC_keyexch_gettable_ctx_params_fn kdf_hkdf_gettable_ctx_params;
  35. static OSSL_FUNC_keyexch_gettable_ctx_params_fn kdf_scrypt_gettable_ctx_params;
  36. typedef struct {
  37. void *provctx;
  38. EVP_KDF_CTX *kdfctx;
  39. KDF_DATA *kdfdata;
  40. } PROV_KDF_CTX;
  41. static void *kdf_newctx(const char *kdfname, void *provctx)
  42. {
  43. PROV_KDF_CTX *kdfctx;
  44. EVP_KDF *kdf = NULL;
  45. if (!ossl_prov_is_running())
  46. return NULL;
  47. kdfctx = OPENSSL_zalloc(sizeof(PROV_KDF_CTX));
  48. if (kdfctx == NULL)
  49. return NULL;
  50. kdfctx->provctx = provctx;
  51. kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname, NULL);
  52. if (kdf == NULL)
  53. goto err;
  54. kdfctx->kdfctx = EVP_KDF_CTX_new(kdf);
  55. EVP_KDF_free(kdf);
  56. if (kdfctx->kdfctx == NULL)
  57. goto err;
  58. return kdfctx;
  59. err:
  60. OPENSSL_free(kdfctx);
  61. return NULL;
  62. }
  63. #define KDF_NEWCTX(funcname, kdfname) \
  64. static void *kdf_##funcname##_newctx(void *provctx) \
  65. { \
  66. return kdf_newctx(kdfname, provctx); \
  67. }
  68. KDF_NEWCTX(tls1_prf, "TLS1-PRF")
  69. KDF_NEWCTX(hkdf, "HKDF")
  70. KDF_NEWCTX(scrypt, "SCRYPT")
  71. static int kdf_init(void *vpkdfctx, void *vkdf, const OSSL_PARAM params[])
  72. {
  73. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  74. if (!ossl_prov_is_running()
  75. || pkdfctx == NULL
  76. || vkdf == NULL
  77. || !ossl_kdf_data_up_ref(vkdf))
  78. return 0;
  79. pkdfctx->kdfdata = vkdf;
  80. return kdf_set_ctx_params(pkdfctx, params);
  81. }
  82. static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
  83. size_t outlen)
  84. {
  85. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  86. size_t kdfsize;
  87. int ret;
  88. if (!ossl_prov_is_running())
  89. return 0;
  90. kdfsize = EVP_KDF_CTX_get_kdf_size(pkdfctx->kdfctx);
  91. if (secret == NULL) {
  92. *secretlen = kdfsize;
  93. return 1;
  94. }
  95. if (kdfsize != SIZE_MAX) {
  96. if (outlen < kdfsize) {
  97. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  98. return 0;
  99. }
  100. outlen = kdfsize;
  101. }
  102. ret = EVP_KDF_derive(pkdfctx->kdfctx, secret, outlen, NULL);
  103. if (ret <= 0)
  104. return 0;
  105. *secretlen = outlen;
  106. return 1;
  107. }
  108. static void kdf_freectx(void *vpkdfctx)
  109. {
  110. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  111. EVP_KDF_CTX_free(pkdfctx->kdfctx);
  112. ossl_kdf_data_free(pkdfctx->kdfdata);
  113. OPENSSL_free(pkdfctx);
  114. }
  115. static void *kdf_dupctx(void *vpkdfctx)
  116. {
  117. PROV_KDF_CTX *srcctx = (PROV_KDF_CTX *)vpkdfctx;
  118. PROV_KDF_CTX *dstctx;
  119. if (!ossl_prov_is_running())
  120. return NULL;
  121. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  122. if (dstctx == NULL)
  123. return NULL;
  124. *dstctx = *srcctx;
  125. dstctx->kdfctx = EVP_KDF_CTX_dup(srcctx->kdfctx);
  126. if (dstctx->kdfctx == NULL) {
  127. OPENSSL_free(dstctx);
  128. return NULL;
  129. }
  130. if (!ossl_kdf_data_up_ref(dstctx->kdfdata)) {
  131. EVP_KDF_CTX_free(dstctx->kdfctx);
  132. OPENSSL_free(dstctx);
  133. return NULL;
  134. }
  135. return dstctx;
  136. }
  137. static int kdf_set_ctx_params(void *vpkdfctx, const OSSL_PARAM params[])
  138. {
  139. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  140. return EVP_KDF_CTX_set_params(pkdfctx->kdfctx, params);
  141. }
  142. static int kdf_get_ctx_params(void *vpkdfctx, OSSL_PARAM params[])
  143. {
  144. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  145. return EVP_KDF_CTX_get_params(pkdfctx->kdfctx, params);
  146. }
  147. static const OSSL_PARAM *kdf_settable_ctx_params(ossl_unused void *vpkdfctx,
  148. void *provctx,
  149. const char *kdfname)
  150. {
  151. EVP_KDF *kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname,
  152. NULL);
  153. const OSSL_PARAM *params;
  154. if (kdf == NULL)
  155. return NULL;
  156. params = EVP_KDF_settable_ctx_params(kdf);
  157. EVP_KDF_free(kdf);
  158. return params;
  159. }
  160. #define KDF_SETTABLE_CTX_PARAMS(funcname, kdfname) \
  161. static const OSSL_PARAM *kdf_##funcname##_settable_ctx_params(void *vpkdfctx, \
  162. void *provctx) \
  163. { \
  164. return kdf_settable_ctx_params(vpkdfctx, provctx, kdfname); \
  165. }
  166. KDF_SETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
  167. KDF_SETTABLE_CTX_PARAMS(hkdf, "HKDF")
  168. KDF_SETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
  169. static const OSSL_PARAM *kdf_gettable_ctx_params(ossl_unused void *vpkdfctx,
  170. void *provctx,
  171. const char *kdfname)
  172. {
  173. EVP_KDF *kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname,
  174. NULL);
  175. const OSSL_PARAM *params;
  176. if (kdf == NULL)
  177. return NULL;
  178. params = EVP_KDF_gettable_ctx_params(kdf);
  179. EVP_KDF_free(kdf);
  180. return params;
  181. }
  182. #define KDF_GETTABLE_CTX_PARAMS(funcname, kdfname) \
  183. static const OSSL_PARAM *kdf_##funcname##_gettable_ctx_params(void *vpkdfctx, \
  184. void *provctx) \
  185. { \
  186. return kdf_gettable_ctx_params(vpkdfctx, provctx, kdfname); \
  187. }
  188. KDF_GETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
  189. KDF_GETTABLE_CTX_PARAMS(hkdf, "HKDF")
  190. KDF_GETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
  191. #define KDF_KEYEXCH_FUNCTIONS(funcname) \
  192. const OSSL_DISPATCH ossl_kdf_##funcname##_keyexch_functions[] = { \
  193. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))kdf_##funcname##_newctx }, \
  194. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))kdf_init }, \
  195. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))kdf_derive }, \
  196. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))kdf_freectx }, \
  197. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))kdf_dupctx }, \
  198. { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))kdf_set_ctx_params }, \
  199. { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))kdf_get_ctx_params }, \
  200. { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS, \
  201. (void (*)(void))kdf_##funcname##_settable_ctx_params }, \
  202. { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS, \
  203. (void (*)(void))kdf_##funcname##_gettable_ctx_params }, \
  204. OSSL_DISPATCH_END \
  205. };
  206. KDF_KEYEXCH_FUNCTIONS(tls1_prf)
  207. KDF_KEYEXCH_FUNCTIONS(hkdf)
  208. KDF_KEYEXCH_FUNCTIONS(scrypt)