pvkkdf.c 7.3 KB

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