pkcs5.c 14 KB

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