pbkdf1.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 1999-2024 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/trace.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include <string.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/proverr.h>
  17. #include "internal/cryptlib.h"
  18. #include "internal/numbers.h"
  19. #include "crypto/evp.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/providercommon.h"
  22. #include "prov/implementations.h"
  23. #include "prov/provider_util.h"
  24. static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf1_new;
  25. static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf1_dup;
  26. static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf1_free;
  27. static OSSL_FUNC_kdf_reset_fn kdf_pbkdf1_reset;
  28. static OSSL_FUNC_kdf_derive_fn kdf_pbkdf1_derive;
  29. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf1_settable_ctx_params;
  30. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf1_set_ctx_params;
  31. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf1_gettable_ctx_params;
  32. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf1_get_ctx_params;
  33. typedef struct {
  34. void *provctx;
  35. PROV_DIGEST digest;
  36. unsigned char *pass;
  37. size_t pass_len;
  38. unsigned char *salt;
  39. size_t salt_len;
  40. uint64_t iter;
  41. } KDF_PBKDF1;
  42. /*
  43. * PKCS5 PBKDF1 compatible key/IV generation as specified in:
  44. * https://tools.ietf.org/html/rfc8018#page-10
  45. */
  46. static int kdf_pbkdf1_do_derive(const unsigned char *pass, size_t passlen,
  47. const unsigned char *salt, size_t saltlen,
  48. uint64_t iter, const EVP_MD *md_type,
  49. unsigned char *out, size_t n)
  50. {
  51. uint64_t i;
  52. int mdsize, ret = 0;
  53. unsigned char md_tmp[EVP_MAX_MD_SIZE];
  54. EVP_MD_CTX *ctx = NULL;
  55. ctx = EVP_MD_CTX_new();
  56. if (ctx == NULL) {
  57. ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB);
  58. goto err;
  59. }
  60. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  61. || !EVP_DigestUpdate(ctx, pass, passlen)
  62. || !EVP_DigestUpdate(ctx, salt, saltlen)
  63. || !EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  64. goto err;
  65. mdsize = EVP_MD_size(md_type);
  66. if (mdsize < 0)
  67. goto err;
  68. if (n > (size_t)mdsize) {
  69. ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
  70. goto err;
  71. }
  72. for (i = 1; i < iter; i++) {
  73. if (!EVP_DigestInit_ex(ctx, md_type, NULL))
  74. goto err;
  75. if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
  76. goto err;
  77. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  78. goto err;
  79. }
  80. memcpy(out, md_tmp, n);
  81. ret = 1;
  82. err:
  83. OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
  84. EVP_MD_CTX_free(ctx);
  85. return ret;
  86. }
  87. static void *kdf_pbkdf1_new(void *provctx)
  88. {
  89. KDF_PBKDF1 *ctx;
  90. if (!ossl_prov_is_running())
  91. return NULL;
  92. ctx = OPENSSL_zalloc(sizeof(*ctx));
  93. if (ctx == NULL)
  94. return NULL;
  95. ctx->provctx = provctx;
  96. return ctx;
  97. }
  98. static void kdf_pbkdf1_cleanup(KDF_PBKDF1 *ctx)
  99. {
  100. ossl_prov_digest_reset(&ctx->digest);
  101. OPENSSL_free(ctx->salt);
  102. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  103. memset(ctx, 0, sizeof(*ctx));
  104. }
  105. static void kdf_pbkdf1_free(void *vctx)
  106. {
  107. KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
  108. if (ctx != NULL) {
  109. kdf_pbkdf1_cleanup(ctx);
  110. OPENSSL_free(ctx);
  111. }
  112. }
  113. static void kdf_pbkdf1_reset(void *vctx)
  114. {
  115. KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
  116. void *provctx = ctx->provctx;
  117. kdf_pbkdf1_cleanup(ctx);
  118. ctx->provctx = provctx;
  119. }
  120. static void *kdf_pbkdf1_dup(void *vctx)
  121. {
  122. const KDF_PBKDF1 *src = (const KDF_PBKDF1 *)vctx;
  123. KDF_PBKDF1 *dest;
  124. dest = kdf_pbkdf1_new(src->provctx);
  125. if (dest != NULL) {
  126. if (!ossl_prov_memdup(src->salt, src->salt_len,
  127. &dest->salt, &dest->salt_len)
  128. || !ossl_prov_memdup(src->pass, src->pass_len,
  129. &dest->pass , &dest->pass_len)
  130. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  131. goto err;
  132. dest->iter = src->iter;
  133. }
  134. return dest;
  135. err:
  136. kdf_pbkdf1_free(dest);
  137. return NULL;
  138. }
  139. static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen,
  140. const OSSL_PARAM *p)
  141. {
  142. OPENSSL_clear_free(*buffer, *buflen);
  143. *buffer = NULL;
  144. *buflen = 0;
  145. if (p->data_size == 0) {
  146. if ((*buffer = OPENSSL_malloc(1)) == NULL)
  147. return 0;
  148. } else if (p->data != NULL) {
  149. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. static int kdf_pbkdf1_derive(void *vctx, unsigned char *key, size_t keylen,
  155. const OSSL_PARAM params[])
  156. {
  157. KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
  158. const EVP_MD *md;
  159. if (!ossl_prov_is_running() || !kdf_pbkdf1_set_ctx_params(ctx, params))
  160. return 0;
  161. if (ctx->pass == NULL) {
  162. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  163. return 0;
  164. }
  165. if (ctx->salt == NULL) {
  166. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  167. return 0;
  168. }
  169. md = ossl_prov_digest_md(&ctx->digest);
  170. return kdf_pbkdf1_do_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
  171. ctx->iter, md, key, keylen);
  172. }
  173. static int kdf_pbkdf1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  174. {
  175. const OSSL_PARAM *p;
  176. KDF_PBKDF1 *ctx = vctx;
  177. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  178. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
  179. return 0;
  180. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  181. if (!kdf_pbkdf1_set_membuf(&ctx->pass, &ctx->pass_len, p))
  182. return 0;
  183. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  184. if (!kdf_pbkdf1_set_membuf(&ctx->salt, &ctx->salt_len, p))
  185. return 0;
  186. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
  187. if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
  188. return 0;
  189. return 1;
  190. }
  191. static const OSSL_PARAM *kdf_pbkdf1_settable_ctx_params(ossl_unused void *ctx,
  192. ossl_unused void *p_ctx)
  193. {
  194. static const OSSL_PARAM known_settable_ctx_params[] = {
  195. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  196. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  197. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  198. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  199. OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
  200. OSSL_PARAM_END
  201. };
  202. return known_settable_ctx_params;
  203. }
  204. static int kdf_pbkdf1_get_ctx_params(void *vctx, OSSL_PARAM params[])
  205. {
  206. OSSL_PARAM *p;
  207. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  208. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  209. return -2;
  210. }
  211. static const OSSL_PARAM *kdf_pbkdf1_gettable_ctx_params(ossl_unused void *ctx,
  212. ossl_unused void *p_ctx)
  213. {
  214. static const OSSL_PARAM known_gettable_ctx_params[] = {
  215. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  216. OSSL_PARAM_END
  217. };
  218. return known_gettable_ctx_params;
  219. }
  220. const OSSL_DISPATCH ossl_kdf_pbkdf1_functions[] = {
  221. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf1_new },
  222. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf1_dup },
  223. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf1_free },
  224. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf1_reset },
  225. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf1_derive },
  226. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  227. (void(*)(void))kdf_pbkdf1_settable_ctx_params },
  228. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_set_ctx_params },
  229. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  230. (void(*)(void))kdf_pbkdf1_gettable_ctx_params },
  231. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_get_ctx_params },
  232. OSSL_DISPATCH_END
  233. };