38 #if defined(POLARSSL_PKCS5_C)
45 static int pkcs5_parse_pbkdf2_params(
asn1_buf *params,
51 unsigned char **p = ¶ms->
p;
52 const unsigned char *end = params->
p + params->
len;
103 const unsigned char *pwd,
size_t pwdlen,
104 const unsigned char *data,
size_t datalen,
105 unsigned char *output )
107 int ret, iterations = 0, keylen = 0;
108 unsigned char *p, *end;
109 asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
112 unsigned char key[32], iv[32];
121 end = p + pbe_params->
len;
136 if( ( ret =
asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
144 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
145 &salt, &iterations, &keylen,
152 if( md_info == NULL )
155 if( ( ret =
asn1_get_alg( &p, end, &enc_scheme_oid, &enc_scheme_params ) ) != 0 )
162 if( cipher_info == NULL )
168 enc_scheme_params.
len != cipher_info->
iv_size )
173 memcpy( iv, enc_scheme_params.
p, enc_scheme_params.
len );
175 if( ( ret =
md_init_ctx( &md_ctx, md_info ) ) != 0 )
179 iterations, keylen, key ) ) != 0 )
187 if( ( ret =
cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
190 if( ( ret =
cipher_set_iv( &cipher_ctx, iv, enc_scheme_params.
len ) ) != 0 )
197 output, &olen ) ) != 0 )
202 if( ( ret =
cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
213 size_t plen,
const unsigned char *salt,
size_t slen,
214 unsigned int iteration_count,
215 uint32_t key_length,
unsigned char *output )
223 unsigned char *out_p = output;
224 unsigned char counter[4];
226 memset( counter, 0, 4 );
229 if( iteration_count > 0xFFFFFFFF )
248 memcpy( md1, work, md_size );
250 for ( i = 1; i < iteration_count; i++ )
265 for( j = 0; j < md_size; j++ )
269 use_len = ( key_length < md_size ) ? key_length : md_size;
270 memcpy( out_p, work, use_len );
272 key_length -= (uint32_t) use_len;
275 for( i = 4; i > 0; i-- )
276 if( ++counter[i - 1] != 0 )
283 #if defined(POLARSSL_SELF_TEST)
289 size_t plen[MAX_TESTS] =
290 { 8, 8, 8, 8, 24, 9 };
292 unsigned char password[MAX_TESTS][32] =
298 "passwordPASSWORDpassword",
302 size_t slen[MAX_TESTS] =
303 { 4, 4, 4, 4, 36, 5 };
305 unsigned char salt[MAX_TESTS][40] =
311 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
315 uint32_t it_cnt[MAX_TESTS] =
316 { 1, 2, 4096, 16777216, 4096, 4096 };
318 uint32_t key_len[MAX_TESTS] =
319 { 20, 20, 20, 20, 25, 16 };
322 unsigned char result_key[MAX_TESTS][32] =
324 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
325 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
326 0x2f, 0xe0, 0x37, 0xa6 },
327 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
328 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
329 0xd8, 0xde, 0x89, 0x57 },
330 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
331 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
332 0x65, 0xa4, 0x29, 0xc1 },
333 { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
334 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
335 0x26, 0x34, 0xe9, 0x84 },
336 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
337 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
338 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
340 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
341 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
349 unsigned char key[64];
352 if( info_sha1 == NULL )
355 if( ( ret =
md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
358 for( i = 0; i < MAX_TESTS; i++ )
360 printf(
" PBKDF2 (SHA1) #%d: ", i );
363 slen[i], it_cnt[i], key_len[i], key );
365 memcmp( result_key[i], key, key_len[i] ) != 0 )
368 printf(
"failed\n" );
374 printf(
"passed\n" );
#define POLARSSL_ERR_PKCS5_INVALID_FORMAT
Unexpected ASN.1 data.
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
int pkcs5_self_test(int verbose)
Checkup routine.
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
int asn1_get_int(unsigned char **p, const unsigned char *end, int *val)
Retrieve an integer ASN.1 tag and its value.
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
#define POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE
Requested encryption or digest alg not available.
Configuration options (set of defines)
#define OID_CMP(oid_str, oid_buf)
Compares two asn1_buf structures for the same OID.
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
int pkcs5_pbkdf2_hmac(md_context_t *ctx, const unsigned char *password, size_t plen, const unsigned char *salt, size_t slen, unsigned int iteration_count, uint32_t key_length, unsigned char *output)
PKCS#5 PBKDF2 using HMAC.
Object Identifier (OID) database.
const md_info_t * md_info
Information about the associated message digest.
#define POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH
Given private key password does not allow for correct decryption.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int pkcs5_pbes2(asn1_buf *pbe_params, int mode, const unsigned char *pwd, size_t pwdlen, const unsigned char *data, size_t datalen, unsigned char *output)
PKCS#5 PBES2 function.
int oid_get_cipher_alg(const asn1_buf *oid, cipher_type_t *cipher_alg)
Translate encryption algorithm OID into cipher_type.
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int asn1_get_alg_null(unsigned char **p, const unsigned char *end, asn1_buf *alg)
Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no params.
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
int asn1_get_alg(unsigned char **p, const unsigned char *end, asn1_buf *alg, asn1_buf *params)
Retrieve an AlgorithmIdentifier ASN.1 sequence.
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
unsigned char * p
ASN1 data, e.g.
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
int md_hmac_starts(md_context_t *ctx, const unsigned char *key, size_t keylen)
Generic HMAC context setup.
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
Type-length-value structure that allows for ASN1 using DER.
#define OID_PKCS5_PBKDF2
id-PBKDF2 OBJECT IDENTIFIER ::= {pkcs-5 12}
size_t len
ASN1 length, e.g.
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
#define POLARSSL_MD_MAX_SIZE
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
#define ASN1_OCTET_STRING
#define POLARSSL_ERR_PKCS5_BAD_INPUT_DATA
Bad input parameters to function.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
#define OID_HMAC_SHA1
id-hmacWithSHA1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(...
Message digest information.
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
unsigned int iv_size
IV/NONCE size, in bytes.
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
Generic message digest context.