pkcs5.c 12 KB

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