pvkkdf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2018-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/evp.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/proverr.h>
  12. #include <openssl/err.h>
  13. #include "internal/numbers.h" /* SIZE_MAX */
  14. #include "prov/provider_ctx.h"
  15. #include "prov/providercommon.h"
  16. #include "prov/implementations.h"
  17. #include "prov/provider_util.h"
  18. static OSSL_FUNC_kdf_newctx_fn kdf_pvk_new;
  19. static OSSL_FUNC_kdf_dupctx_fn kdf_pvk_dup;
  20. static OSSL_FUNC_kdf_freectx_fn kdf_pvk_free;
  21. static OSSL_FUNC_kdf_reset_fn kdf_pvk_reset;
  22. static OSSL_FUNC_kdf_derive_fn kdf_pvk_derive;
  23. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pvk_settable_ctx_params;
  24. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pvk_set_ctx_params;
  25. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pvk_gettable_ctx_params;
  26. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pvk_get_ctx_params;
  27. typedef struct {
  28. void *provctx;
  29. unsigned char *pass;
  30. size_t pass_len;
  31. unsigned char *salt;
  32. size_t salt_len;
  33. PROV_DIGEST digest;
  34. } KDF_PVK;
  35. static void kdf_pvk_init(KDF_PVK *ctx);
  36. static void *kdf_pvk_new(void *provctx)
  37. {
  38. KDF_PVK *ctx;
  39. if (!ossl_prov_is_running())
  40. return NULL;
  41. ctx = OPENSSL_zalloc(sizeof(*ctx));
  42. if (ctx == NULL)
  43. return NULL;
  44. ctx->provctx = provctx;
  45. kdf_pvk_init(ctx);
  46. return ctx;
  47. }
  48. static void kdf_pvk_cleanup(KDF_PVK *ctx)
  49. {
  50. ossl_prov_digest_reset(&ctx->digest);
  51. OPENSSL_free(ctx->salt);
  52. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  53. OPENSSL_cleanse(ctx, sizeof(*ctx));
  54. }
  55. static void kdf_pvk_free(void *vctx)
  56. {
  57. KDF_PVK *ctx = (KDF_PVK *)vctx;
  58. if (ctx != NULL) {
  59. kdf_pvk_cleanup(ctx);
  60. OPENSSL_free(ctx);
  61. }
  62. }
  63. static void *kdf_pvk_dup(void *vctx)
  64. {
  65. const KDF_PVK *src = (const KDF_PVK *)vctx;
  66. KDF_PVK *dest;
  67. dest = kdf_pvk_new(src->provctx);
  68. if (dest != NULL)
  69. if (!ossl_prov_memdup(src->salt, src->salt_len,
  70. &dest->salt, &dest->salt_len)
  71. || !ossl_prov_memdup(src->pass, src->pass_len,
  72. &dest->pass , &dest->pass_len)
  73. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  74. goto err;
  75. return dest;
  76. err:
  77. kdf_pvk_free(dest);
  78. return NULL;
  79. }
  80. static void kdf_pvk_reset(void *vctx)
  81. {
  82. KDF_PVK *ctx = (KDF_PVK *)vctx;
  83. void *provctx = ctx->provctx;
  84. kdf_pvk_cleanup(ctx);
  85. ctx->provctx = provctx;
  86. kdf_pvk_init(ctx);
  87. }
  88. static void kdf_pvk_init(KDF_PVK *ctx)
  89. {
  90. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  91. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  92. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  93. SN_sha1, 0);
  94. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  95. /* This is an error, but there is no way to indicate such directly */
  96. ossl_prov_digest_reset(&ctx->digest);
  97. }
  98. static int pvk_set_membuf(unsigned char **buffer, size_t *buflen,
  99. const OSSL_PARAM *p)
  100. {
  101. OPENSSL_clear_free(*buffer, *buflen);
  102. *buffer = NULL;
  103. *buflen = 0;
  104. if (p->data_size == 0) {
  105. if ((*buffer = OPENSSL_malloc(1)) == NULL)
  106. return 0;
  107. } else if (p->data != NULL) {
  108. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. static int kdf_pvk_derive(void *vctx, unsigned char *key, size_t keylen,
  114. const OSSL_PARAM params[])
  115. {
  116. KDF_PVK *ctx = (KDF_PVK *)vctx;
  117. const EVP_MD *md;
  118. EVP_MD_CTX *mctx;
  119. int res;
  120. if (!ossl_prov_is_running() || !kdf_pvk_set_ctx_params(ctx, params))
  121. return 0;
  122. if (ctx->pass == NULL) {
  123. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  124. return 0;
  125. }
  126. if (ctx->salt == NULL) {
  127. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  128. return 0;
  129. }
  130. md = ossl_prov_digest_md(&ctx->digest);
  131. if (md == NULL) {
  132. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
  133. return 0;
  134. }
  135. res = EVP_MD_get_size(md);
  136. if (res <= 0) {
  137. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  138. return 0;
  139. }
  140. if ((size_t)res > keylen) {
  141. ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
  142. return 0;
  143. }
  144. mctx = EVP_MD_CTX_new();
  145. res = mctx != NULL
  146. && EVP_DigestInit_ex(mctx, md, NULL)
  147. && EVP_DigestUpdate(mctx, ctx->salt, ctx->salt_len)
  148. && EVP_DigestUpdate(mctx, ctx->pass, ctx->pass_len)
  149. && EVP_DigestFinal_ex(mctx, key, NULL);
  150. EVP_MD_CTX_free(mctx);
  151. return res;
  152. }
  153. static int kdf_pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  154. {
  155. const OSSL_PARAM *p;
  156. KDF_PVK *ctx = vctx;
  157. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  158. if (params == NULL)
  159. return 1;
  160. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  161. return 0;
  162. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  163. if (!pvk_set_membuf(&ctx->pass, &ctx->pass_len, p))
  164. return 0;
  165. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
  166. if (!pvk_set_membuf(&ctx->salt, &ctx->salt_len, p))
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. static const OSSL_PARAM *kdf_pvk_settable_ctx_params(ossl_unused void *ctx,
  172. ossl_unused void *p_ctx)
  173. {
  174. static const OSSL_PARAM known_settable_ctx_params[] = {
  175. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  176. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  177. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  178. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  179. OSSL_PARAM_END
  180. };
  181. return known_settable_ctx_params;
  182. }
  183. static int kdf_pvk_get_ctx_params(void *vctx, OSSL_PARAM params[])
  184. {
  185. OSSL_PARAM *p;
  186. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  187. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  188. return -2;
  189. }
  190. static const OSSL_PARAM *kdf_pvk_gettable_ctx_params(ossl_unused void *ctx,
  191. ossl_unused void *p_ctx)
  192. {
  193. static const OSSL_PARAM known_gettable_ctx_params[] = {
  194. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  195. OSSL_PARAM_END
  196. };
  197. return known_gettable_ctx_params;
  198. }
  199. const OSSL_DISPATCH ossl_kdf_pvk_functions[] = {
  200. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pvk_new },
  201. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pvk_dup },
  202. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pvk_free },
  203. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pvk_reset },
  204. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pvk_derive },
  205. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  206. (void(*)(void))kdf_pvk_settable_ctx_params },
  207. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pvk_set_ctx_params },
  208. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  209. (void(*)(void))kdf_pvk_gettable_ctx_params },
  210. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pvk_get_ctx_params },
  211. OSSL_DISPATCH_END
  212. };