pkcs5.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**
  2. * \file pkcs5.c
  3. *
  4. * \brief PKCS#5 functions
  5. *
  6. * \author Mathias Olsson <mathias@kompetensum.com>
  7. *
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  10. */
  11. /*
  12. * PKCS#5 includes PBKDF2 and more
  13. *
  14. * http://tools.ietf.org/html/rfc2898 (Specification)
  15. * http://tools.ietf.org/html/rfc6070 (Test vectors)
  16. */
  17. #include "common.h"
  18. #if defined(MBEDTLS_PKCS5_C)
  19. #include "mbedtls/pkcs5.h"
  20. #include "mbedtls/error.h"
  21. #if defined(MBEDTLS_ASN1_PARSE_C)
  22. #include "mbedtls/asn1.h"
  23. #if defined(MBEDTLS_CIPHER_C)
  24. #include "mbedtls/cipher.h"
  25. #endif /* MBEDTLS_CIPHER_C */
  26. #include "mbedtls/oid.h"
  27. #endif /* MBEDTLS_ASN1_PARSE_C */
  28. #include <string.h>
  29. #include "mbedtls/platform.h"
  30. #include "psa_util_internal.h"
  31. #if defined(MBEDTLS_ASN1_PARSE_C) && defined(MBEDTLS_CIPHER_C)
  32. static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
  33. mbedtls_asn1_buf *salt, int *iterations,
  34. int *keylen, mbedtls_md_type_t *md_type)
  35. {
  36. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  37. mbedtls_asn1_buf prf_alg_oid;
  38. unsigned char *p = params->p;
  39. const unsigned char *end = params->p + params->len;
  40. if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  41. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
  42. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  43. }
  44. /*
  45. * PBKDF2-params ::= SEQUENCE {
  46. * salt OCTET STRING,
  47. * iterationCount INTEGER,
  48. * keyLength INTEGER OPTIONAL
  49. * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
  50. * }
  51. *
  52. */
  53. if ((ret = mbedtls_asn1_get_tag(&p, end, &salt->len,
  54. MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  55. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  56. }
  57. salt->p = p;
  58. p += salt->len;
  59. if ((ret = mbedtls_asn1_get_int(&p, end, iterations)) != 0) {
  60. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  61. }
  62. if (p == end) {
  63. return 0;
  64. }
  65. if ((ret = mbedtls_asn1_get_int(&p, end, keylen)) != 0) {
  66. if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  67. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  68. }
  69. }
  70. if (p == end) {
  71. return 0;
  72. }
  73. if ((ret = mbedtls_asn1_get_alg_null(&p, end, &prf_alg_oid)) != 0) {
  74. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  75. }
  76. if (mbedtls_oid_get_md_hmac(&prf_alg_oid, md_type) != 0) {
  77. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  78. }
  79. if (p != end) {
  80. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
  81. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  82. }
  83. return 0;
  84. }
  85. #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  86. int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
  87. const unsigned char *pwd, size_t pwdlen,
  88. const unsigned char *data, size_t datalen,
  89. unsigned char *output, size_t output_size,
  90. size_t *output_len);
  91. #endif
  92. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  93. int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
  94. const unsigned char *pwd, size_t pwdlen,
  95. const unsigned char *data, size_t datalen,
  96. unsigned char *output)
  97. {
  98. size_t output_len = 0;
  99. /* We assume caller of the function is providing a big enough output buffer
  100. * so we pass output_size as SIZE_MAX to pass checks, However, no guarantees
  101. * for the output size actually being correct.
  102. */
  103. return mbedtls_pkcs5_pbes2_ext(pbe_params, mode, pwd, pwdlen, data,
  104. datalen, output, SIZE_MAX, &output_len);
  105. }
  106. #endif
  107. int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
  108. const unsigned char *pwd, size_t pwdlen,
  109. const unsigned char *data, size_t datalen,
  110. unsigned char *output, size_t output_size,
  111. size_t *output_len)
  112. {
  113. int ret, iterations = 0, keylen = 0;
  114. unsigned char *p, *end;
  115. mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
  116. mbedtls_asn1_buf salt;
  117. mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
  118. unsigned char key[32], iv[32];
  119. const mbedtls_cipher_info_t *cipher_info;
  120. mbedtls_cipher_type_t cipher_alg;
  121. mbedtls_cipher_context_t cipher_ctx;
  122. unsigned int padlen = 0;
  123. p = pbe_params->p;
  124. end = p + pbe_params->len;
  125. /*
  126. * PBES2-params ::= SEQUENCE {
  127. * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
  128. * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
  129. * }
  130. */
  131. if (pbe_params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  132. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
  133. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  134. }
  135. if ((ret = mbedtls_asn1_get_alg(&p, end, &kdf_alg_oid,
  136. &kdf_alg_params)) != 0) {
  137. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  138. }
  139. // Only PBKDF2 supported at the moment
  140. //
  141. if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid) != 0) {
  142. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  143. }
  144. if ((ret = pkcs5_parse_pbkdf2_params(&kdf_alg_params,
  145. &salt, &iterations, &keylen,
  146. &md_type)) != 0) {
  147. return ret;
  148. }
  149. if ((ret = mbedtls_asn1_get_alg(&p, end, &enc_scheme_oid,
  150. &enc_scheme_params)) != 0) {
  151. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  152. }
  153. if (mbedtls_oid_get_cipher_alg(&enc_scheme_oid, &cipher_alg) != 0) {
  154. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  155. }
  156. cipher_info = mbedtls_cipher_info_from_type(cipher_alg);
  157. if (cipher_info == NULL) {
  158. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  159. }
  160. /*
  161. * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
  162. * since it is optional and we don't know if it was set or not
  163. */
  164. keylen = (int) mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
  165. if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
  166. enc_scheme_params.len != mbedtls_cipher_info_get_iv_size(cipher_info)) {
  167. return MBEDTLS_ERR_PKCS5_INVALID_FORMAT;
  168. }
  169. if (mode == MBEDTLS_PKCS5_DECRYPT) {
  170. if (output_size < datalen) {
  171. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  172. }
  173. }
  174. if (mode == MBEDTLS_PKCS5_ENCRYPT) {
  175. padlen = cipher_info->block_size - (datalen % cipher_info->block_size);
  176. if (output_size < (datalen + padlen)) {
  177. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  178. }
  179. }
  180. mbedtls_cipher_init(&cipher_ctx);
  181. memcpy(iv, enc_scheme_params.p, enc_scheme_params.len);
  182. if ((ret = mbedtls_pkcs5_pbkdf2_hmac_ext(md_type, pwd, pwdlen, salt.p,
  183. salt.len, iterations, keylen,
  184. key)) != 0) {
  185. goto exit;
  186. }
  187. if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
  188. goto exit;
  189. }
  190. if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
  191. (mbedtls_operation_t) mode)) != 0) {
  192. goto exit;
  193. }
  194. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  195. {
  196. /* PKCS5 uses CBC with PKCS7 padding (which is the same as
  197. * "PKCS5 padding" except that it's typically only called PKCS5
  198. * with 64-bit-block ciphers).
  199. */
  200. mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
  201. #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  202. /* For historical reasons, when decrypting, this function works when
  203. * decrypting even when support for PKCS7 padding is disabled. In this
  204. * case, it ignores the padding, and so will never report a
  205. * password mismatch.
  206. */
  207. if (mode == MBEDTLS_DECRYPT) {
  208. padding = MBEDTLS_PADDING_NONE;
  209. }
  210. #endif
  211. if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
  212. goto exit;
  213. }
  214. }
  215. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  216. if ((ret = mbedtls_cipher_crypt(&cipher_ctx, iv, enc_scheme_params.len,
  217. data, datalen, output, output_len)) != 0) {
  218. ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
  219. }
  220. exit:
  221. mbedtls_cipher_free(&cipher_ctx);
  222. return ret;
  223. }
  224. #endif /* MBEDTLS_ASN1_PARSE_C && MBEDTLS_CIPHER_C */
  225. static int pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
  226. const unsigned char *password,
  227. size_t plen, const unsigned char *salt, size_t slen,
  228. unsigned int iteration_count,
  229. uint32_t key_length, unsigned char *output)
  230. {
  231. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  232. unsigned int i;
  233. unsigned char md1[MBEDTLS_MD_MAX_SIZE];
  234. unsigned char work[MBEDTLS_MD_MAX_SIZE];
  235. unsigned char md_size = mbedtls_md_get_size(ctx->md_info);
  236. size_t use_len;
  237. unsigned char *out_p = output;
  238. unsigned char counter[4];
  239. memset(counter, 0, 4);
  240. counter[3] = 1;
  241. #if UINT_MAX > 0xFFFFFFFF
  242. if (iteration_count > 0xFFFFFFFF) {
  243. return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
  244. }
  245. #endif
  246. if ((ret = mbedtls_md_hmac_starts(ctx, password, plen)) != 0) {
  247. return ret;
  248. }
  249. while (key_length) {
  250. // U1 ends up in work
  251. //
  252. if ((ret = mbedtls_md_hmac_update(ctx, salt, slen)) != 0) {
  253. goto cleanup;
  254. }
  255. if ((ret = mbedtls_md_hmac_update(ctx, counter, 4)) != 0) {
  256. goto cleanup;
  257. }
  258. if ((ret = mbedtls_md_hmac_finish(ctx, work)) != 0) {
  259. goto cleanup;
  260. }
  261. if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
  262. goto cleanup;
  263. }
  264. memcpy(md1, work, md_size);
  265. for (i = 1; i < iteration_count; i++) {
  266. // U2 ends up in md1
  267. //
  268. if ((ret = mbedtls_md_hmac_update(ctx, md1, md_size)) != 0) {
  269. goto cleanup;
  270. }
  271. if ((ret = mbedtls_md_hmac_finish(ctx, md1)) != 0) {
  272. goto cleanup;
  273. }
  274. if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
  275. goto cleanup;
  276. }
  277. // U1 xor U2
  278. //
  279. mbedtls_xor(work, work, md1, md_size);
  280. }
  281. use_len = (key_length < md_size) ? key_length : md_size;
  282. memcpy(out_p, work, use_len);
  283. key_length -= (uint32_t) use_len;
  284. out_p += use_len;
  285. for (i = 4; i > 0; i--) {
  286. if (++counter[i - 1] != 0) {
  287. break;
  288. }
  289. }
  290. }
  291. cleanup:
  292. /* Zeroise buffers to clear sensitive data from memory. */
  293. mbedtls_platform_zeroize(work, MBEDTLS_MD_MAX_SIZE);
  294. mbedtls_platform_zeroize(md1, MBEDTLS_MD_MAX_SIZE);
  295. return ret;
  296. }
  297. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  298. int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
  299. const unsigned char *password,
  300. size_t plen, const unsigned char *salt, size_t slen,
  301. unsigned int iteration_count,
  302. uint32_t key_length, unsigned char *output)
  303. {
  304. return pkcs5_pbkdf2_hmac(ctx, password, plen, salt, slen, iteration_count,
  305. key_length, output);
  306. }
  307. #endif
  308. int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_alg,
  309. const unsigned char *password,
  310. size_t plen, const unsigned char *salt, size_t slen,
  311. unsigned int iteration_count,
  312. uint32_t key_length, unsigned char *output)
  313. {
  314. mbedtls_md_context_t md_ctx;
  315. const mbedtls_md_info_t *md_info = NULL;
  316. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  317. md_info = mbedtls_md_info_from_type(md_alg);
  318. if (md_info == NULL) {
  319. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  320. }
  321. mbedtls_md_init(&md_ctx);
  322. if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
  323. goto exit;
  324. }
  325. ret = pkcs5_pbkdf2_hmac(&md_ctx, password, plen, salt, slen,
  326. iteration_count, key_length, output);
  327. exit:
  328. mbedtls_md_free(&md_ctx);
  329. return ret;
  330. }
  331. #if defined(MBEDTLS_SELF_TEST)
  332. #if !defined(MBEDTLS_MD_CAN_SHA1)
  333. int mbedtls_pkcs5_self_test(int verbose)
  334. {
  335. if (verbose != 0) {
  336. mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n");
  337. }
  338. return 0;
  339. }
  340. #else
  341. #define MAX_TESTS 6
  342. static const size_t plen_test_data[MAX_TESTS] =
  343. { 8, 8, 8, 24, 9 };
  344. static const unsigned char password_test_data[MAX_TESTS][32] =
  345. {
  346. "password",
  347. "password",
  348. "password",
  349. "passwordPASSWORDpassword",
  350. "pass\0word",
  351. };
  352. static const size_t slen_test_data[MAX_TESTS] =
  353. { 4, 4, 4, 36, 5 };
  354. static const unsigned char salt_test_data[MAX_TESTS][40] =
  355. {
  356. "salt",
  357. "salt",
  358. "salt",
  359. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  360. "sa\0lt",
  361. };
  362. static const uint32_t it_cnt_test_data[MAX_TESTS] =
  363. { 1, 2, 4096, 4096, 4096 };
  364. static const uint32_t key_len_test_data[MAX_TESTS] =
  365. { 20, 20, 20, 25, 16 };
  366. static const unsigned char result_key_test_data[MAX_TESTS][32] =
  367. {
  368. { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
  369. 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
  370. 0x2f, 0xe0, 0x37, 0xa6 },
  371. { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
  372. 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
  373. 0xd8, 0xde, 0x89, 0x57 },
  374. { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
  375. 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
  376. 0x65, 0xa4, 0x29, 0xc1 },
  377. { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
  378. 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
  379. 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
  380. 0x38 },
  381. { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  382. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
  383. };
  384. int mbedtls_pkcs5_self_test(int verbose)
  385. {
  386. int ret, i;
  387. unsigned char key[64];
  388. for (i = 0; i < MAX_TESTS; i++) {
  389. if (verbose != 0) {
  390. mbedtls_printf(" PBKDF2 (SHA1) #%d: ", i);
  391. }
  392. ret = mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, password_test_data[i],
  393. plen_test_data[i], salt_test_data[i],
  394. slen_test_data[i], it_cnt_test_data[i],
  395. key_len_test_data[i], key);
  396. if (ret != 0 ||
  397. memcmp(result_key_test_data[i], key, key_len_test_data[i]) != 0) {
  398. if (verbose != 0) {
  399. mbedtls_printf("failed\n");
  400. }
  401. ret = 1;
  402. goto exit;
  403. }
  404. if (verbose != 0) {
  405. mbedtls_printf("passed\n");
  406. }
  407. }
  408. if (verbose != 0) {
  409. mbedtls_printf("\n");
  410. }
  411. exit:
  412. return ret;
  413. }
  414. #endif /* MBEDTLS_MD_CAN_SHA1 */
  415. #endif /* MBEDTLS_SELF_TEST */
  416. #endif /* MBEDTLS_PKCS5_C */