pbkdf1.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 1999-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/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_MALLOC_FAILURE);
  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. for (i = 1; i < iter; i++) {
  69. if (!EVP_DigestInit_ex(ctx, md_type, NULL))
  70. goto err;
  71. if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
  72. goto err;
  73. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  74. goto err;
  75. }
  76. memcpy(out, md_tmp, n);
  77. ret = 1;
  78. err:
  79. EVP_MD_CTX_free(ctx);
  80. return ret;
  81. }
  82. static void *kdf_pbkdf1_new(void *provctx)
  83. {
  84. KDF_PBKDF1 *ctx;
  85. if (!ossl_prov_is_running())
  86. return NULL;
  87. ctx = OPENSSL_zalloc(sizeof(*ctx));
  88. if (ctx == NULL) {
  89. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  90. return NULL;
  91. }
  92. ctx->provctx = provctx;
  93. return ctx;
  94. }
  95. static void kdf_pbkdf1_cleanup(KDF_PBKDF1 *ctx)
  96. {
  97. ossl_prov_digest_reset(&ctx->digest);
  98. OPENSSL_free(ctx->salt);
  99. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  100. memset(ctx, 0, sizeof(*ctx));
  101. }
  102. static void kdf_pbkdf1_free(void *vctx)
  103. {
  104. KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
  105. if (ctx != NULL) {
  106. kdf_pbkdf1_cleanup(ctx);
  107. OPENSSL_free(ctx);
  108. }
  109. }
  110. static void kdf_pbkdf1_reset(void *vctx)
  111. {
  112. KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
  113. void *provctx = ctx->provctx;
  114. kdf_pbkdf1_cleanup(ctx);
  115. ctx->provctx = provctx;
  116. }
  117. static void *kdf_pbkdf1_dup(void *vctx)
  118. {
  119. const KDF_PBKDF1 *src = (const KDF_PBKDF1 *)vctx;
  120. KDF_PBKDF1 *dest;
  121. dest = kdf_pbkdf1_new(src->provctx);
  122. if (dest != NULL) {
  123. if (!ossl_prov_memdup(src->salt, src->salt_len,
  124. &dest->salt, &dest->salt_len)
  125. || !ossl_prov_memdup(src->pass, src->pass_len,
  126. &dest->pass , &dest->pass_len)
  127. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  128. goto err;
  129. dest->iter = src->iter;
  130. }
  131. return dest;
  132. err:
  133. kdf_pbkdf1_free(dest);
  134. return NULL;
  135. }
  136. static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen,
  137. const OSSL_PARAM *p)
  138. {
  139. OPENSSL_clear_free(*buffer, *buflen);
  140. *buffer = NULL;
  141. *buflen = 0;
  142. if (p->data_size == 0) {
  143. if ((*buffer = OPENSSL_malloc(1)) == NULL) {
  144. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  145. return 0;
  146. }
  147. } else if (p->data != NULL) {
  148. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static int kdf_pbkdf1_derive(void *vctx, unsigned char *key, size_t keylen,
  154. const OSSL_PARAM params[])
  155. {
  156. KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
  157. const EVP_MD *md;
  158. if (!ossl_prov_is_running() || !kdf_pbkdf1_set_ctx_params(ctx, params))
  159. return 0;
  160. if (ctx->pass == NULL) {
  161. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  162. return 0;
  163. }
  164. if (ctx->salt == NULL) {
  165. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  166. return 0;
  167. }
  168. md = ossl_prov_digest_md(&ctx->digest);
  169. return kdf_pbkdf1_do_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
  170. ctx->iter, md, key, keylen);
  171. }
  172. static int kdf_pbkdf1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  173. {
  174. const OSSL_PARAM *p;
  175. KDF_PBKDF1 *ctx = vctx;
  176. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  177. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
  178. return 0;
  179. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  180. if (!kdf_pbkdf1_set_membuf(&ctx->pass, &ctx->pass_len, p))
  181. return 0;
  182. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  183. if (!kdf_pbkdf1_set_membuf(&ctx->salt, &ctx->salt_len, p))
  184. return 0;
  185. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
  186. if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
  187. return 0;
  188. return 1;
  189. }
  190. static const OSSL_PARAM *kdf_pbkdf1_settable_ctx_params(ossl_unused void *ctx,
  191. ossl_unused void *p_ctx)
  192. {
  193. static const OSSL_PARAM known_settable_ctx_params[] = {
  194. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  195. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  196. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  197. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  198. OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
  199. OSSL_PARAM_END
  200. };
  201. return known_settable_ctx_params;
  202. }
  203. static int kdf_pbkdf1_get_ctx_params(void *vctx, OSSL_PARAM params[])
  204. {
  205. OSSL_PARAM *p;
  206. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  207. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  208. return -2;
  209. }
  210. static const OSSL_PARAM *kdf_pbkdf1_gettable_ctx_params(ossl_unused void *ctx,
  211. ossl_unused void *p_ctx)
  212. {
  213. static const OSSL_PARAM known_gettable_ctx_params[] = {
  214. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  215. OSSL_PARAM_END
  216. };
  217. return known_gettable_ctx_params;
  218. }
  219. const OSSL_DISPATCH ossl_kdf_pbkdf1_functions[] = {
  220. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf1_new },
  221. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf1_dup },
  222. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf1_free },
  223. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf1_reset },
  224. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf1_derive },
  225. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  226. (void(*)(void))kdf_pbkdf1_settable_ctx_params },
  227. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_set_ctx_params },
  228. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  229. (void(*)(void))kdf_pbkdf1_gettable_ctx_params },
  230. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_get_ctx_params },
  231. { 0, NULL }
  232. };