pbkdf2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /*
  10. * HMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <openssl/hmac.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/kdf.h>
  20. #include <openssl/core_names.h>
  21. #include <openssl/proverr.h>
  22. #include "internal/cryptlib.h"
  23. #include "internal/numbers.h"
  24. #include "crypto/evp.h"
  25. #include "prov/provider_ctx.h"
  26. #include "prov/providercommon.h"
  27. #include "prov/implementations.h"
  28. #include "prov/provider_util.h"
  29. #include "pbkdf2.h"
  30. /* Constants specified in SP800-132 */
  31. #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112
  32. #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF
  33. #define KDF_PBKDF2_MIN_ITERATIONS 1000
  34. #define KDF_PBKDF2_MIN_SALT_LEN (128 / 8)
  35. static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf2_new;
  36. static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf2_dup;
  37. static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf2_free;
  38. static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset;
  39. static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive;
  40. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params;
  41. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params;
  42. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params;
  43. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params;
  44. static int pbkdf2_derive(const char *pass, size_t passlen,
  45. const unsigned char *salt, int saltlen, uint64_t iter,
  46. const EVP_MD *digest, unsigned char *key,
  47. size_t keylen, int extra_checks);
  48. typedef struct {
  49. void *provctx;
  50. unsigned char *pass;
  51. size_t pass_len;
  52. unsigned char *salt;
  53. size_t salt_len;
  54. uint64_t iter;
  55. PROV_DIGEST digest;
  56. int lower_bound_checks;
  57. } KDF_PBKDF2;
  58. static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);
  59. static void *kdf_pbkdf2_new_no_init(void *provctx)
  60. {
  61. KDF_PBKDF2 *ctx;
  62. if (!ossl_prov_is_running())
  63. return NULL;
  64. ctx = OPENSSL_zalloc(sizeof(*ctx));
  65. if (ctx == NULL) {
  66. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  67. return NULL;
  68. }
  69. ctx->provctx = provctx;
  70. return ctx;
  71. }
  72. static void *kdf_pbkdf2_new(void *provctx)
  73. {
  74. KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx);
  75. if (ctx != NULL)
  76. kdf_pbkdf2_init(ctx);
  77. return ctx;
  78. }
  79. static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
  80. {
  81. ossl_prov_digest_reset(&ctx->digest);
  82. OPENSSL_free(ctx->salt);
  83. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  84. memset(ctx, 0, sizeof(*ctx));
  85. }
  86. static void kdf_pbkdf2_free(void *vctx)
  87. {
  88. KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
  89. if (ctx != NULL) {
  90. kdf_pbkdf2_cleanup(ctx);
  91. OPENSSL_free(ctx);
  92. }
  93. }
  94. static void kdf_pbkdf2_reset(void *vctx)
  95. {
  96. KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
  97. void *provctx = ctx->provctx;
  98. kdf_pbkdf2_cleanup(ctx);
  99. ctx->provctx = provctx;
  100. kdf_pbkdf2_init(ctx);
  101. }
  102. static void *kdf_pbkdf2_dup(void *vctx)
  103. {
  104. const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx;
  105. KDF_PBKDF2 *dest;
  106. /* We need a new PBKDF2 object but uninitialised since we're filling it */
  107. dest = kdf_pbkdf2_new_no_init(src->provctx);
  108. if (dest != NULL) {
  109. if (!ossl_prov_memdup(src->salt, src->salt_len,
  110. &dest->salt, &dest->salt_len)
  111. || !ossl_prov_memdup(src->pass, src->pass_len,
  112. &dest->pass, &dest->pass_len)
  113. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  114. goto err;
  115. dest->iter = src->iter;
  116. dest->lower_bound_checks = src->lower_bound_checks;
  117. }
  118. return dest;
  119. err:
  120. kdf_pbkdf2_free(dest);
  121. return NULL;
  122. }
  123. static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
  124. {
  125. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  126. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  127. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  128. SN_sha1, 0);
  129. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  130. /* This is an error, but there is no way to indicate such directly */
  131. ossl_prov_digest_reset(&ctx->digest);
  132. ctx->iter = PKCS5_DEFAULT_ITER;
  133. ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks;
  134. }
  135. static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
  136. const OSSL_PARAM *p)
  137. {
  138. OPENSSL_clear_free(*buffer, *buflen);
  139. *buffer = NULL;
  140. *buflen = 0;
  141. if (p->data_size == 0) {
  142. if ((*buffer = OPENSSL_malloc(1)) == NULL) {
  143. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  144. return 0;
  145. }
  146. } else if (p->data != NULL) {
  147. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen,
  153. const OSSL_PARAM params[])
  154. {
  155. KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
  156. const EVP_MD *md;
  157. if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))
  158. return 0;
  159. if (ctx->pass == NULL) {
  160. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  161. return 0;
  162. }
  163. if (ctx->salt == NULL) {
  164. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  165. return 0;
  166. }
  167. md = ossl_prov_digest_md(&ctx->digest);
  168. return pbkdf2_derive((char *)ctx->pass, ctx->pass_len,
  169. ctx->salt, ctx->salt_len, ctx->iter,
  170. md, key, keylen, ctx->lower_bound_checks);
  171. }
  172. static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  173. {
  174. const OSSL_PARAM *p;
  175. KDF_PBKDF2 *ctx = vctx;
  176. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  177. int pkcs5;
  178. uint64_t iter, min_iter;
  179. if (params == NULL)
  180. return 1;
  181. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  182. return 0;
  183. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
  184. if (!OSSL_PARAM_get_int(p, &pkcs5))
  185. return 0;
  186. ctx->lower_bound_checks = pkcs5 == 0;
  187. }
  188. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  189. if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p))
  190. return 0;
  191. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
  192. if (ctx->lower_bound_checks != 0
  193. && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) {
  194. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
  195. return 0;
  196. }
  197. if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p))
  198. return 0;
  199. }
  200. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
  201. if (!OSSL_PARAM_get_uint64(p, &iter))
  202. return 0;
  203. min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1;
  204. if (iter < min_iter) {
  205. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
  206. return 0;
  207. }
  208. ctx->iter = iter;
  209. }
  210. return 1;
  211. }
  212. static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
  213. ossl_unused void *p_ctx)
  214. {
  215. static const OSSL_PARAM known_settable_ctx_params[] = {
  216. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  217. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  218. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  219. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  220. OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
  221. OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
  222. OSSL_PARAM_END
  223. };
  224. return known_settable_ctx_params;
  225. }
  226. static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
  227. {
  228. OSSL_PARAM *p;
  229. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  230. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  231. return -2;
  232. }
  233. static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,
  234. ossl_unused void *p_ctx)
  235. {
  236. static const OSSL_PARAM known_gettable_ctx_params[] = {
  237. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  238. OSSL_PARAM_END
  239. };
  240. return known_gettable_ctx_params;
  241. }
  242. const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = {
  243. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new },
  244. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup },
  245. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free },
  246. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset },
  247. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive },
  248. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  249. (void(*)(void))kdf_pbkdf2_settable_ctx_params },
  250. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params },
  251. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  252. (void(*)(void))kdf_pbkdf2_gettable_ctx_params },
  253. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params },
  254. { 0, NULL }
  255. };
  256. /*
  257. * This is an implementation of PKCS#5 v2.0 password based encryption key
  258. * derivation function PBKDF2. SHA1 version verified against test vectors
  259. * posted by Peter Gutmann to the PKCS-TNG mailing list.
  260. *
  261. * The constraints specified by SP800-132 have been added i.e.
  262. * - Check the range of the key length.
  263. * - Minimum iteration count of 1000.
  264. * - Randomly-generated portion of the salt shall be at least 128 bits.
  265. */
  266. static int pbkdf2_derive(const char *pass, size_t passlen,
  267. const unsigned char *salt, int saltlen, uint64_t iter,
  268. const EVP_MD *digest, unsigned char *key,
  269. size_t keylen, int lower_bound_checks)
  270. {
  271. int ret = 0;
  272. unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
  273. int cplen, k, tkeylen, mdlen;
  274. uint64_t j;
  275. unsigned long i = 1;
  276. HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
  277. mdlen = EVP_MD_get_size(digest);
  278. if (mdlen <= 0)
  279. return 0;
  280. /*
  281. * This check should always be done because keylen / mdlen >= (2^32 - 1)
  282. * results in an overflow of the loop counter 'i'.
  283. */
  284. if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
  285. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  286. return 0;
  287. }
  288. if (lower_bound_checks) {
  289. if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
  290. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
  291. return 0;
  292. }
  293. if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
  294. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
  295. return 0;
  296. }
  297. if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
  298. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
  299. return 0;
  300. }
  301. }
  302. hctx_tpl = HMAC_CTX_new();
  303. if (hctx_tpl == NULL)
  304. return 0;
  305. p = key;
  306. tkeylen = keylen;
  307. if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
  308. goto err;
  309. hctx = HMAC_CTX_new();
  310. if (hctx == NULL)
  311. goto err;
  312. while (tkeylen) {
  313. if (tkeylen > mdlen)
  314. cplen = mdlen;
  315. else
  316. cplen = tkeylen;
  317. /*
  318. * We are unlikely to ever use more than 256 blocks (5120 bits!) but
  319. * just in case...
  320. */
  321. itmp[0] = (unsigned char)((i >> 24) & 0xff);
  322. itmp[1] = (unsigned char)((i >> 16) & 0xff);
  323. itmp[2] = (unsigned char)((i >> 8) & 0xff);
  324. itmp[3] = (unsigned char)(i & 0xff);
  325. if (!HMAC_CTX_copy(hctx, hctx_tpl))
  326. goto err;
  327. if (!HMAC_Update(hctx, salt, saltlen)
  328. || !HMAC_Update(hctx, itmp, 4)
  329. || !HMAC_Final(hctx, digtmp, NULL))
  330. goto err;
  331. memcpy(p, digtmp, cplen);
  332. for (j = 1; j < iter; j++) {
  333. if (!HMAC_CTX_copy(hctx, hctx_tpl))
  334. goto err;
  335. if (!HMAC_Update(hctx, digtmp, mdlen)
  336. || !HMAC_Final(hctx, digtmp, NULL))
  337. goto err;
  338. for (k = 0; k < cplen; k++)
  339. p[k] ^= digtmp[k];
  340. }
  341. tkeylen -= cplen;
  342. i++;
  343. p += cplen;
  344. }
  345. ret = 1;
  346. err:
  347. HMAC_CTX_free(hctx);
  348. HMAC_CTX_free(hctx_tpl);
  349. return ret;
  350. }