pbkdf2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright 2018-2019 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 <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <openssl/hmac.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/core_names.h>
  16. #include "internal/cryptlib.h"
  17. #include "internal/numbers.h"
  18. #include "crypto/evp.h"
  19. #include "internal/provider_ctx.h"
  20. #include "internal/providercommonerr.h"
  21. #include "internal/provider_algs.h"
  22. #include "internal/provider_util.h"
  23. /* Constants specified in SP800-132 */
  24. #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112
  25. #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF
  26. #define KDF_PBKDF2_MIN_ITERATIONS 1000
  27. #define KDF_PBKDF2_MIN_SALT_LEN (128 / 8)
  28. /*
  29. * For backwards compatibility reasons,
  30. * Extra checks are done by default in fips mode only.
  31. */
  32. #ifdef FIPS_MODE
  33. # define KDF_PBKDF2_DEFAULT_CHECKS 1
  34. #else
  35. # define KDF_PBKDF2_DEFAULT_CHECKS 0
  36. #endif /* FIPS_MODE */
  37. static OSSL_OP_kdf_newctx_fn kdf_pbkdf2_new;
  38. static OSSL_OP_kdf_freectx_fn kdf_pbkdf2_free;
  39. static OSSL_OP_kdf_reset_fn kdf_pbkdf2_reset;
  40. static OSSL_OP_kdf_derive_fn kdf_pbkdf2_derive;
  41. static OSSL_OP_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params;
  42. static OSSL_OP_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params;
  43. static int pbkdf2_derive(const char *pass, size_t passlen,
  44. const unsigned char *salt, int saltlen, uint64_t iter,
  45. const EVP_MD *digest, unsigned char *key,
  46. size_t keylen, int extra_checks);
  47. typedef struct {
  48. void *provctx;
  49. unsigned char *pass;
  50. size_t pass_len;
  51. unsigned char *salt;
  52. size_t salt_len;
  53. uint64_t iter;
  54. PROV_DIGEST digest;
  55. int lower_bound_checks;
  56. } KDF_PBKDF2;
  57. static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);
  58. static void *kdf_pbkdf2_new(void *provctx)
  59. {
  60. KDF_PBKDF2 *ctx;
  61. ctx = OPENSSL_zalloc(sizeof(*ctx));
  62. if (ctx == NULL) {
  63. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  64. return NULL;
  65. }
  66. ctx->provctx = provctx;
  67. kdf_pbkdf2_init(ctx);
  68. return ctx;
  69. }
  70. static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
  71. {
  72. ossl_prov_digest_reset(&ctx->digest);
  73. OPENSSL_free(ctx->salt);
  74. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  75. memset(ctx, 0, sizeof(*ctx));
  76. }
  77. static void kdf_pbkdf2_free(void *vctx)
  78. {
  79. KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
  80. kdf_pbkdf2_cleanup(ctx);
  81. OPENSSL_free(ctx);
  82. }
  83. static void kdf_pbkdf2_reset(void *vctx)
  84. {
  85. KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
  86. kdf_pbkdf2_cleanup(ctx);
  87. kdf_pbkdf2_init(ctx);
  88. }
  89. static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
  90. {
  91. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  92. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_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. ctx->iter = PKCS5_DEFAULT_ITER;
  99. ctx->lower_bound_checks = KDF_PBKDF2_DEFAULT_CHECKS;
  100. }
  101. static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
  102. const OSSL_PARAM *p)
  103. {
  104. OPENSSL_clear_free(*buffer, *buflen);
  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. *buffer = NULL;
  112. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  113. return 0;
  114. }
  115. return 1;
  116. }
  117. static int kdf_pbkdf2_derive(void *vctx, unsigned char *key,
  118. size_t keylen)
  119. {
  120. KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
  121. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  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. return pbkdf2_derive((char *)ctx->pass, ctx->pass_len,
  131. ctx->salt, ctx->salt_len, ctx->iter,
  132. md, key, keylen, ctx->lower_bound_checks);
  133. }
  134. static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  135. {
  136. const OSSL_PARAM *p;
  137. KDF_PBKDF2 *ctx = vctx;
  138. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
  139. int pkcs5;
  140. uint64_t iter, min_iter;
  141. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  142. return 0;
  143. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
  144. if (!OSSL_PARAM_get_int(p, &pkcs5))
  145. return 0;
  146. ctx->lower_bound_checks = pkcs5 == 0;
  147. }
  148. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  149. if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p))
  150. return 0;
  151. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
  152. if (ctx->lower_bound_checks != 0
  153. && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) {
  154. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
  155. return 0;
  156. }
  157. if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len,p))
  158. return 0;
  159. }
  160. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
  161. if (!OSSL_PARAM_get_uint64(p, &iter))
  162. return 0;
  163. min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1;
  164. if (iter < min_iter) {
  165. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
  166. return 0;
  167. }
  168. ctx->iter = iter;
  169. }
  170. return 1;
  171. }
  172. static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(void)
  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_uint64(OSSL_KDF_PARAM_ITER, NULL),
  180. OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
  181. OSSL_PARAM_END
  182. };
  183. return known_settable_ctx_params;
  184. }
  185. static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
  186. {
  187. OSSL_PARAM *p;
  188. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  189. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  190. return -2;
  191. }
  192. static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(void)
  193. {
  194. static const OSSL_PARAM known_gettable_ctx_params[] = {
  195. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  196. OSSL_PARAM_END
  197. };
  198. return known_gettable_ctx_params;
  199. }
  200. const OSSL_DISPATCH kdf_pbkdf2_functions[] = {
  201. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new },
  202. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free },
  203. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset },
  204. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive },
  205. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  206. (void(*)(void))kdf_pbkdf2_settable_ctx_params },
  207. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params },
  208. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  209. (void(*)(void))kdf_pbkdf2_gettable_ctx_params },
  210. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params },
  211. { 0, NULL }
  212. };
  213. /*
  214. * This is an implementation of PKCS#5 v2.0 password based encryption key
  215. * derivation function PBKDF2. SHA1 version verified against test vectors
  216. * posted by Peter Gutmann to the PKCS-TNG mailing list.
  217. *
  218. * The constraints specified by SP800-132 have been added i.e.
  219. * - Check the range of the key length.
  220. * - Minimum iteration count of 1000.
  221. * - Randomly-generated portion of the salt shall be at least 128 bits.
  222. */
  223. static int pbkdf2_derive(const char *pass, size_t passlen,
  224. const unsigned char *salt, int saltlen, uint64_t iter,
  225. const EVP_MD *digest, unsigned char *key,
  226. size_t keylen, int lower_bound_checks)
  227. {
  228. int ret = 0;
  229. unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
  230. int cplen, k, tkeylen, mdlen;
  231. uint64_t j;
  232. unsigned long i = 1;
  233. HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
  234. mdlen = EVP_MD_size(digest);
  235. if (mdlen <= 0)
  236. return 0;
  237. /*
  238. * This check should always be done because keylen / mdlen >= (2^32 - 1)
  239. * results in an overflow of the loop counter 'i'.
  240. */
  241. if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
  242. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN);
  243. return 0;
  244. }
  245. if (lower_bound_checks) {
  246. if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
  247. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN);
  248. return 0;
  249. }
  250. if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
  251. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
  252. return 0;
  253. }
  254. if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
  255. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
  256. return 0;
  257. }
  258. }
  259. hctx_tpl = HMAC_CTX_new();
  260. if (hctx_tpl == NULL)
  261. return 0;
  262. p = key;
  263. tkeylen = keylen;
  264. if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
  265. goto err;
  266. hctx = HMAC_CTX_new();
  267. if (hctx == NULL)
  268. goto err;
  269. while (tkeylen) {
  270. if (tkeylen > mdlen)
  271. cplen = mdlen;
  272. else
  273. cplen = tkeylen;
  274. /*
  275. * We are unlikely to ever use more than 256 blocks (5120 bits!) but
  276. * just in case...
  277. */
  278. itmp[0] = (unsigned char)((i >> 24) & 0xff);
  279. itmp[1] = (unsigned char)((i >> 16) & 0xff);
  280. itmp[2] = (unsigned char)((i >> 8) & 0xff);
  281. itmp[3] = (unsigned char)(i & 0xff);
  282. if (!HMAC_CTX_copy(hctx, hctx_tpl))
  283. goto err;
  284. if (!HMAC_Update(hctx, salt, saltlen)
  285. || !HMAC_Update(hctx, itmp, 4)
  286. || !HMAC_Final(hctx, digtmp, NULL))
  287. goto err;
  288. memcpy(p, digtmp, cplen);
  289. for (j = 1; j < iter; j++) {
  290. if (!HMAC_CTX_copy(hctx, hctx_tpl))
  291. goto err;
  292. if (!HMAC_Update(hctx, digtmp, mdlen)
  293. || !HMAC_Final(hctx, digtmp, NULL))
  294. goto err;
  295. for (k = 0; k < cplen; k++)
  296. p[k] ^= digtmp[k];
  297. }
  298. tkeylen -= cplen;
  299. i++;
  300. p += cplen;
  301. }
  302. ret = 1;
  303. err:
  304. HMAC_CTX_free(hctx);
  305. HMAC_CTX_free(hctx_tpl);
  306. return ret;
  307. }