pkcs5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * \file pkcs5.c
  3. *
  4. * \brief PKCS#5 functions
  5. *
  6. * \author Mathias Olsson <[email protected]>
  7. *
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. /*
  24. * PKCS#5 includes PBKDF2 and more
  25. *
  26. * http://tools.ietf.org/html/rfc2898 (Specification)
  27. * http://tools.ietf.org/html/rfc6070 (Test vectors)
  28. */
  29. #include "common.h"
  30. #if defined(MBEDTLS_PKCS5_C)
  31. #include "mbedtls/pkcs5.h"
  32. #include "mbedtls/error.h"
  33. #if defined(MBEDTLS_ASN1_PARSE_C)
  34. #include "mbedtls/asn1.h"
  35. #include "mbedtls/cipher.h"
  36. #include "mbedtls/oid.h"
  37. #endif /* MBEDTLS_ASN1_PARSE_C */
  38. #include <string.h>
  39. #include "mbedtls/platform.h"
  40. #if defined(MBEDTLS_ASN1_PARSE_C)
  41. static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
  42. mbedtls_asn1_buf *salt, int *iterations,
  43. int *keylen, mbedtls_md_type_t *md_type)
  44. {
  45. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  46. mbedtls_asn1_buf prf_alg_oid;
  47. unsigned char *p = params->p;
  48. const unsigned char *end = params->p + params->len;
  49. if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  50. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
  51. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  52. }
  53. /*
  54. * PBKDF2-params ::= SEQUENCE {
  55. * salt OCTET STRING,
  56. * iterationCount INTEGER,
  57. * keyLength INTEGER OPTIONAL
  58. * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
  59. * }
  60. *
  61. */
  62. if ((ret = mbedtls_asn1_get_tag(&p, end, &salt->len,
  63. MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  64. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  65. }
  66. salt->p = p;
  67. p += salt->len;
  68. if ((ret = mbedtls_asn1_get_int(&p, end, iterations)) != 0) {
  69. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  70. }
  71. if (p == end) {
  72. return 0;
  73. }
  74. if ((ret = mbedtls_asn1_get_int(&p, end, keylen)) != 0) {
  75. if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  76. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  77. }
  78. }
  79. if (p == end) {
  80. return 0;
  81. }
  82. if ((ret = mbedtls_asn1_get_alg_null(&p, end, &prf_alg_oid)) != 0) {
  83. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  84. }
  85. if (mbedtls_oid_get_md_hmac(&prf_alg_oid, md_type) != 0) {
  86. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  87. }
  88. if (p != end) {
  89. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
  90. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  91. }
  92. return 0;
  93. }
  94. int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
  95. const unsigned char *pwd, size_t pwdlen,
  96. const unsigned char *data, size_t datalen,
  97. unsigned char *output)
  98. {
  99. int ret, iterations = 0, keylen = 0;
  100. unsigned char *p, *end;
  101. mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
  102. mbedtls_asn1_buf salt;
  103. mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
  104. unsigned char key[32], iv[32];
  105. size_t olen = 0;
  106. const mbedtls_md_info_t *md_info;
  107. const mbedtls_cipher_info_t *cipher_info;
  108. mbedtls_md_context_t md_ctx;
  109. mbedtls_cipher_type_t cipher_alg;
  110. mbedtls_cipher_context_t cipher_ctx;
  111. p = pbe_params->p;
  112. end = p + pbe_params->len;
  113. /*
  114. * PBES2-params ::= SEQUENCE {
  115. * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
  116. * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
  117. * }
  118. */
  119. if (pbe_params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  120. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
  121. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  122. }
  123. if ((ret = mbedtls_asn1_get_alg(&p, end, &kdf_alg_oid,
  124. &kdf_alg_params)) != 0) {
  125. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  126. }
  127. // Only PBKDF2 supported at the moment
  128. //
  129. if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid) != 0) {
  130. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  131. }
  132. if ((ret = pkcs5_parse_pbkdf2_params(&kdf_alg_params,
  133. &salt, &iterations, &keylen,
  134. &md_type)) != 0) {
  135. return ret;
  136. }
  137. md_info = mbedtls_md_info_from_type(md_type);
  138. if (md_info == NULL) {
  139. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  140. }
  141. if ((ret = mbedtls_asn1_get_alg(&p, end, &enc_scheme_oid,
  142. &enc_scheme_params)) != 0) {
  143. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
  144. }
  145. if (mbedtls_oid_get_cipher_alg(&enc_scheme_oid, &cipher_alg) != 0) {
  146. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  147. }
  148. cipher_info = mbedtls_cipher_info_from_type(cipher_alg);
  149. if (cipher_info == NULL) {
  150. return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
  151. }
  152. /*
  153. * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
  154. * since it is optional and we don't know if it was set or not
  155. */
  156. keylen = cipher_info->key_bitlen / 8;
  157. if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
  158. enc_scheme_params.len != cipher_info->iv_size) {
  159. return MBEDTLS_ERR_PKCS5_INVALID_FORMAT;
  160. }
  161. mbedtls_md_init(&md_ctx);
  162. mbedtls_cipher_init(&cipher_ctx);
  163. memcpy(iv, enc_scheme_params.p, enc_scheme_params.len);
  164. if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
  165. goto exit;
  166. }
  167. if ((ret = mbedtls_pkcs5_pbkdf2_hmac(&md_ctx, pwd, pwdlen, salt.p, salt.len,
  168. iterations, keylen, key)) != 0) {
  169. goto exit;
  170. }
  171. if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
  172. goto exit;
  173. }
  174. if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
  175. (mbedtls_operation_t) mode)) != 0) {
  176. goto exit;
  177. }
  178. if ((ret = mbedtls_cipher_crypt(&cipher_ctx, iv, enc_scheme_params.len,
  179. data, datalen, output, &olen)) != 0) {
  180. ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
  181. }
  182. exit:
  183. mbedtls_md_free(&md_ctx);
  184. mbedtls_cipher_free(&cipher_ctx);
  185. return ret;
  186. }
  187. #endif /* MBEDTLS_ASN1_PARSE_C */
  188. int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
  189. const unsigned char *password,
  190. size_t plen, const unsigned char *salt, size_t slen,
  191. unsigned int iteration_count,
  192. uint32_t key_length, unsigned char *output)
  193. {
  194. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  195. int j;
  196. unsigned int i;
  197. unsigned char md1[MBEDTLS_MD_MAX_SIZE];
  198. unsigned char work[MBEDTLS_MD_MAX_SIZE];
  199. unsigned char md_size = mbedtls_md_get_size(ctx->md_info);
  200. size_t use_len;
  201. unsigned char *out_p = output;
  202. unsigned char counter[4];
  203. memset(counter, 0, 4);
  204. counter[3] = 1;
  205. #if UINT_MAX > 0xFFFFFFFF
  206. if (iteration_count > 0xFFFFFFFF) {
  207. return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
  208. }
  209. #endif
  210. if ((ret = mbedtls_md_hmac_starts(ctx, password, plen)) != 0) {
  211. return ret;
  212. }
  213. while (key_length) {
  214. // U1 ends up in work
  215. //
  216. if ((ret = mbedtls_md_hmac_update(ctx, salt, slen)) != 0) {
  217. goto cleanup;
  218. }
  219. if ((ret = mbedtls_md_hmac_update(ctx, counter, 4)) != 0) {
  220. goto cleanup;
  221. }
  222. if ((ret = mbedtls_md_hmac_finish(ctx, work)) != 0) {
  223. goto cleanup;
  224. }
  225. if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
  226. goto cleanup;
  227. }
  228. memcpy(md1, work, md_size);
  229. for (i = 1; i < iteration_count; i++) {
  230. // U2 ends up in md1
  231. //
  232. if ((ret = mbedtls_md_hmac_update(ctx, md1, md_size)) != 0) {
  233. goto cleanup;
  234. }
  235. if ((ret = mbedtls_md_hmac_finish(ctx, md1)) != 0) {
  236. goto cleanup;
  237. }
  238. if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
  239. goto cleanup;
  240. }
  241. // U1 xor U2
  242. //
  243. for (j = 0; j < md_size; j++) {
  244. work[j] ^= md1[j];
  245. }
  246. }
  247. use_len = (key_length < md_size) ? key_length : md_size;
  248. memcpy(out_p, work, use_len);
  249. key_length -= (uint32_t) use_len;
  250. out_p += use_len;
  251. for (i = 4; i > 0; i--) {
  252. if (++counter[i - 1] != 0) {
  253. break;
  254. }
  255. }
  256. }
  257. cleanup:
  258. /* Zeroise buffers to clear sensitive data from memory. */
  259. mbedtls_platform_zeroize(work, MBEDTLS_MD_MAX_SIZE);
  260. mbedtls_platform_zeroize(md1, MBEDTLS_MD_MAX_SIZE);
  261. return ret;
  262. }
  263. #if defined(MBEDTLS_SELF_TEST)
  264. #if !defined(MBEDTLS_SHA1_C)
  265. int mbedtls_pkcs5_self_test(int verbose)
  266. {
  267. if (verbose != 0) {
  268. mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n");
  269. }
  270. return 0;
  271. }
  272. #else
  273. #define MAX_TESTS 6
  274. static const size_t plen_test_data[MAX_TESTS] =
  275. { 8, 8, 8, 24, 9 };
  276. static const unsigned char password_test_data[MAX_TESTS][32] =
  277. {
  278. "password",
  279. "password",
  280. "password",
  281. "passwordPASSWORDpassword",
  282. "pass\0word",
  283. };
  284. static const size_t slen_test_data[MAX_TESTS] =
  285. { 4, 4, 4, 36, 5 };
  286. static const unsigned char salt_test_data[MAX_TESTS][40] =
  287. {
  288. "salt",
  289. "salt",
  290. "salt",
  291. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  292. "sa\0lt",
  293. };
  294. static const uint32_t it_cnt_test_data[MAX_TESTS] =
  295. { 1, 2, 4096, 4096, 4096 };
  296. static const uint32_t key_len_test_data[MAX_TESTS] =
  297. { 20, 20, 20, 25, 16 };
  298. static const unsigned char result_key_test_data[MAX_TESTS][32] =
  299. {
  300. { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
  301. 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
  302. 0x2f, 0xe0, 0x37, 0xa6 },
  303. { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
  304. 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
  305. 0xd8, 0xde, 0x89, 0x57 },
  306. { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
  307. 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
  308. 0x65, 0xa4, 0x29, 0xc1 },
  309. { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
  310. 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
  311. 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
  312. 0x38 },
  313. { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  314. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
  315. };
  316. int mbedtls_pkcs5_self_test(int verbose)
  317. {
  318. mbedtls_md_context_t sha1_ctx;
  319. const mbedtls_md_info_t *info_sha1;
  320. int ret, i;
  321. unsigned char key[64];
  322. mbedtls_md_init(&sha1_ctx);
  323. info_sha1 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  324. if (info_sha1 == NULL) {
  325. ret = 1;
  326. goto exit;
  327. }
  328. if ((ret = mbedtls_md_setup(&sha1_ctx, info_sha1, 1)) != 0) {
  329. ret = 1;
  330. goto exit;
  331. }
  332. for (i = 0; i < MAX_TESTS; i++) {
  333. if (verbose != 0) {
  334. mbedtls_printf(" PBKDF2 (SHA1) #%d: ", i);
  335. }
  336. ret = mbedtls_pkcs5_pbkdf2_hmac(&sha1_ctx, password_test_data[i],
  337. plen_test_data[i], salt_test_data[i],
  338. slen_test_data[i], it_cnt_test_data[i],
  339. key_len_test_data[i], key);
  340. if (ret != 0 ||
  341. memcmp(result_key_test_data[i], key, key_len_test_data[i]) != 0) {
  342. if (verbose != 0) {
  343. mbedtls_printf("failed\n");
  344. }
  345. ret = 1;
  346. goto exit;
  347. }
  348. if (verbose != 0) {
  349. mbedtls_printf("passed\n");
  350. }
  351. }
  352. if (verbose != 0) {
  353. mbedtls_printf("\n");
  354. }
  355. exit:
  356. mbedtls_md_free(&sha1_ctx);
  357. return ret;
  358. }
  359. #endif /* MBEDTLS_SHA1_C */
  360. #endif /* MBEDTLS_SELF_TEST */
  361. #endif /* MBEDTLS_PKCS5_C */