kdf_meth.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2019-2020 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/evp.h>
  10. #include <openssl/err.h>
  11. #include <openssl/core.h>
  12. #include <openssl/core_dispatch.h>
  13. #include <openssl/kdf.h>
  14. #include "crypto/evp.h"
  15. #include "internal/provider.h"
  16. #include "evp_local.h"
  17. static int evp_kdf_up_ref(void *vkdf)
  18. {
  19. EVP_KDF *kdf = (EVP_KDF *)vkdf;
  20. int ref = 0;
  21. CRYPTO_UP_REF(&kdf->refcnt, &ref, kdf->lock);
  22. return 1;
  23. }
  24. static void evp_kdf_free(void *vkdf){
  25. EVP_KDF *kdf = (EVP_KDF *)vkdf;
  26. int ref = 0;
  27. if (kdf != NULL) {
  28. CRYPTO_DOWN_REF(&kdf->refcnt, &ref, kdf->lock);
  29. if (ref <= 0) {
  30. ossl_provider_free(kdf->prov);
  31. CRYPTO_THREAD_lock_free(kdf->lock);
  32. OPENSSL_free(kdf);
  33. }
  34. }
  35. }
  36. static void *evp_kdf_new(void)
  37. {
  38. EVP_KDF *kdf = NULL;
  39. if ((kdf = OPENSSL_zalloc(sizeof(*kdf))) == NULL
  40. || (kdf->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  41. OPENSSL_free(kdf);
  42. return NULL;
  43. }
  44. kdf->refcnt = 1;
  45. return kdf;
  46. }
  47. static void *evp_kdf_from_dispatch(int name_id,
  48. const OSSL_DISPATCH *fns,
  49. OSSL_PROVIDER *prov)
  50. {
  51. EVP_KDF *kdf = NULL;
  52. int fnkdfcnt = 0, fnctxcnt = 0;
  53. if ((kdf = evp_kdf_new()) == NULL) {
  54. EVPerr(0, ERR_R_MALLOC_FAILURE);
  55. return NULL;
  56. }
  57. kdf->name_id = name_id;
  58. for (; fns->function_id != 0; fns++) {
  59. switch (fns->function_id) {
  60. case OSSL_FUNC_KDF_NEWCTX:
  61. if (kdf->newctx != NULL)
  62. break;
  63. kdf->newctx = OSSL_FUNC_kdf_newctx(fns);
  64. fnctxcnt++;
  65. break;
  66. case OSSL_FUNC_KDF_DUPCTX:
  67. if (kdf->dupctx != NULL)
  68. break;
  69. kdf->dupctx = OSSL_FUNC_kdf_dupctx(fns);
  70. break;
  71. case OSSL_FUNC_KDF_FREECTX:
  72. if (kdf->freectx != NULL)
  73. break;
  74. kdf->freectx = OSSL_FUNC_kdf_freectx(fns);
  75. fnctxcnt++;
  76. break;
  77. case OSSL_FUNC_KDF_RESET:
  78. if (kdf->reset != NULL)
  79. break;
  80. kdf->reset = OSSL_FUNC_kdf_reset(fns);
  81. break;
  82. case OSSL_FUNC_KDF_DERIVE:
  83. if (kdf->derive != NULL)
  84. break;
  85. kdf->derive = OSSL_FUNC_kdf_derive(fns);
  86. fnkdfcnt++;
  87. break;
  88. case OSSL_FUNC_KDF_GETTABLE_PARAMS:
  89. if (kdf->gettable_params != NULL)
  90. break;
  91. kdf->gettable_params =
  92. OSSL_FUNC_kdf_gettable_params(fns);
  93. break;
  94. case OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS:
  95. if (kdf->gettable_ctx_params != NULL)
  96. break;
  97. kdf->gettable_ctx_params =
  98. OSSL_FUNC_kdf_gettable_ctx_params(fns);
  99. break;
  100. case OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS:
  101. if (kdf->settable_ctx_params != NULL)
  102. break;
  103. kdf->settable_ctx_params =
  104. OSSL_FUNC_kdf_settable_ctx_params(fns);
  105. break;
  106. case OSSL_FUNC_KDF_GET_PARAMS:
  107. if (kdf->get_params != NULL)
  108. break;
  109. kdf->get_params = OSSL_FUNC_kdf_get_params(fns);
  110. break;
  111. case OSSL_FUNC_KDF_GET_CTX_PARAMS:
  112. if (kdf->get_ctx_params != NULL)
  113. break;
  114. kdf->get_ctx_params = OSSL_FUNC_kdf_get_ctx_params(fns);
  115. break;
  116. case OSSL_FUNC_KDF_SET_CTX_PARAMS:
  117. if (kdf->set_ctx_params != NULL)
  118. break;
  119. kdf->set_ctx_params = OSSL_FUNC_kdf_set_ctx_params(fns);
  120. break;
  121. }
  122. }
  123. if (fnkdfcnt != 1 || fnctxcnt != 2) {
  124. /*
  125. * In order to be a consistent set of functions we must have at least
  126. * a derive function, and a complete set of context management
  127. * functions.
  128. */
  129. evp_kdf_free(kdf);
  130. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  131. return NULL;
  132. }
  133. kdf->prov = prov;
  134. if (prov != NULL)
  135. ossl_provider_up_ref(prov);
  136. return kdf;
  137. }
  138. EVP_KDF *EVP_KDF_fetch(OPENSSL_CTX *libctx, const char *algorithm,
  139. const char *properties)
  140. {
  141. return evp_generic_fetch(libctx, OSSL_OP_KDF, algorithm, properties,
  142. evp_kdf_from_dispatch, evp_kdf_up_ref,
  143. evp_kdf_free);
  144. }
  145. int EVP_KDF_up_ref(EVP_KDF *kdf)
  146. {
  147. return evp_kdf_up_ref(kdf);
  148. }
  149. void EVP_KDF_free(EVP_KDF *kdf)
  150. {
  151. evp_kdf_free(kdf);
  152. }
  153. const OSSL_PARAM *EVP_KDF_gettable_params(const EVP_KDF *kdf)
  154. {
  155. if (kdf->gettable_params == NULL)
  156. return NULL;
  157. return kdf->gettable_params(ossl_provider_ctx(EVP_KDF_provider(kdf)));
  158. }
  159. const OSSL_PARAM *EVP_KDF_gettable_ctx_params(const EVP_KDF *kdf)
  160. {
  161. if (kdf->gettable_ctx_params == NULL)
  162. return NULL;
  163. return kdf->gettable_ctx_params(ossl_provider_ctx(EVP_KDF_provider(kdf)));
  164. }
  165. const OSSL_PARAM *EVP_KDF_settable_ctx_params(const EVP_KDF *kdf)
  166. {
  167. if (kdf->settable_ctx_params == NULL)
  168. return NULL;
  169. return kdf->settable_ctx_params(ossl_provider_ctx(EVP_KDF_provider(kdf)));
  170. }
  171. void EVP_KDF_do_all_provided(OPENSSL_CTX *libctx,
  172. void (*fn)(EVP_KDF *kdf, void *arg),
  173. void *arg)
  174. {
  175. evp_generic_do_all(libctx, OSSL_OP_KDF,
  176. (void (*)(void *, void *))fn, arg,
  177. evp_kdf_from_dispatch, evp_kdf_free);
  178. }