kdf_exch.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright 2020-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. #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_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
  30. static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
  31. static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_scrypt_settable_ctx_params;
  32. typedef struct {
  33. void *provctx;
  34. EVP_KDF_CTX *kdfctx;
  35. KDF_DATA *kdfdata;
  36. } PROV_KDF_CTX;
  37. static void *kdf_newctx(const char *kdfname, void *provctx)
  38. {
  39. PROV_KDF_CTX *kdfctx;
  40. EVP_KDF *kdf = NULL;
  41. if (!ossl_prov_is_running())
  42. return NULL;
  43. kdfctx = OPENSSL_zalloc(sizeof(PROV_KDF_CTX));
  44. if (kdfctx == NULL)
  45. return NULL;
  46. kdfctx->provctx = provctx;
  47. kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname, NULL);
  48. if (kdf == NULL)
  49. goto err;
  50. kdfctx->kdfctx = EVP_KDF_CTX_new(kdf);
  51. EVP_KDF_free(kdf);
  52. if (kdfctx->kdfctx == NULL)
  53. goto err;
  54. return kdfctx;
  55. err:
  56. OPENSSL_free(kdfctx);
  57. return NULL;
  58. }
  59. #define KDF_NEWCTX(funcname, kdfname) \
  60. static void *kdf_##funcname##_newctx(void *provctx) \
  61. { \
  62. return kdf_newctx(kdfname, provctx); \
  63. }
  64. KDF_NEWCTX(tls1_prf, "TLS1-PRF")
  65. KDF_NEWCTX(hkdf, "HKDF")
  66. KDF_NEWCTX(scrypt, "SCRYPT")
  67. static int kdf_init(void *vpkdfctx, void *vkdf, const OSSL_PARAM params[])
  68. {
  69. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  70. if (!ossl_prov_is_running()
  71. || pkdfctx == NULL
  72. || vkdf == NULL
  73. || !ossl_kdf_data_up_ref(vkdf))
  74. return 0;
  75. pkdfctx->kdfdata = vkdf;
  76. return kdf_set_ctx_params(pkdfctx, params);
  77. }
  78. static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
  79. size_t outlen)
  80. {
  81. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  82. size_t kdfsize;
  83. int ret;
  84. if (!ossl_prov_is_running())
  85. return 0;
  86. kdfsize = EVP_KDF_CTX_get_kdf_size(pkdfctx->kdfctx);
  87. if (secret == NULL) {
  88. *secretlen = kdfsize;
  89. return 1;
  90. }
  91. if (kdfsize != SIZE_MAX) {
  92. if (outlen < kdfsize) {
  93. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  94. return 0;
  95. }
  96. outlen = kdfsize;
  97. }
  98. ret = EVP_KDF_derive(pkdfctx->kdfctx, secret, outlen, NULL);
  99. if (ret <= 0)
  100. return 0;
  101. *secretlen = outlen;
  102. return 1;
  103. }
  104. static void kdf_freectx(void *vpkdfctx)
  105. {
  106. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  107. EVP_KDF_CTX_free(pkdfctx->kdfctx);
  108. ossl_kdf_data_free(pkdfctx->kdfdata);
  109. OPENSSL_free(pkdfctx);
  110. }
  111. static void *kdf_dupctx(void *vpkdfctx)
  112. {
  113. PROV_KDF_CTX *srcctx = (PROV_KDF_CTX *)vpkdfctx;
  114. PROV_KDF_CTX *dstctx;
  115. if (!ossl_prov_is_running())
  116. return NULL;
  117. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  118. if (dstctx == NULL)
  119. return NULL;
  120. *dstctx = *srcctx;
  121. dstctx->kdfctx = EVP_KDF_CTX_dup(srcctx->kdfctx);
  122. if (dstctx->kdfctx == NULL) {
  123. OPENSSL_free(dstctx);
  124. return NULL;
  125. }
  126. if (!ossl_kdf_data_up_ref(dstctx->kdfdata)) {
  127. EVP_KDF_CTX_free(dstctx->kdfctx);
  128. OPENSSL_free(dstctx);
  129. return NULL;
  130. }
  131. return dstctx;
  132. }
  133. static int kdf_set_ctx_params(void *vpkdfctx, const OSSL_PARAM params[])
  134. {
  135. PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
  136. return EVP_KDF_CTX_set_params(pkdfctx->kdfctx, params);
  137. }
  138. static const OSSL_PARAM *kdf_settable_ctx_params(ossl_unused void *vpkdfctx,
  139. void *provctx,
  140. const char *kdfname)
  141. {
  142. EVP_KDF *kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname,
  143. NULL);
  144. const OSSL_PARAM *params;
  145. if (kdf == NULL)
  146. return NULL;
  147. params = EVP_KDF_settable_ctx_params(kdf);
  148. EVP_KDF_free(kdf);
  149. return params;
  150. }
  151. #define KDF_SETTABLE_CTX_PARAMS(funcname, kdfname) \
  152. static const OSSL_PARAM *kdf_##funcname##_settable_ctx_params(void *vpkdfctx, \
  153. void *provctx) \
  154. { \
  155. return kdf_settable_ctx_params(vpkdfctx, provctx, kdfname); \
  156. }
  157. KDF_SETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
  158. KDF_SETTABLE_CTX_PARAMS(hkdf, "HKDF")
  159. KDF_SETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
  160. #define KDF_KEYEXCH_FUNCTIONS(funcname) \
  161. const OSSL_DISPATCH ossl_kdf_##funcname##_keyexch_functions[] = { \
  162. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))kdf_##funcname##_newctx }, \
  163. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))kdf_init }, \
  164. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))kdf_derive }, \
  165. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))kdf_freectx }, \
  166. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))kdf_dupctx }, \
  167. { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))kdf_set_ctx_params }, \
  168. { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS, \
  169. (void (*)(void))kdf_##funcname##_settable_ctx_params }, \
  170. OSSL_DISPATCH_END \
  171. };
  172. KDF_KEYEXCH_FUNCTIONS(tls1_prf)
  173. KDF_KEYEXCH_FUNCTIONS(hkdf)
  174. KDF_KEYEXCH_FUNCTIONS(scrypt)