blob: 061f4738e81c9c200583d1c1634f10cd60cc0342 [file] [log] [blame]
Paul Bakkerb0c19a42013-06-24 19:26:38 +02001/**
2 * \file pkcs5.c
3 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb0c19a42013-06-24 19:26:38 +02009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29/*
30 * PKCS#5 includes PBKDF2 and more
31 *
32 * http://tools.ietf.org/html/rfc2898 (Specification)
33 * http://tools.ietf.org/html/rfc6070 (Test vectors)
34 */
35
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020037#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#else
39#include POLARSSL_CONFIG_FILE
40#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +020041
42#if defined(POLARSSL_PKCS5_C)
43
44#include "polarssl/pkcs5.h"
Paul Bakker28144de2013-06-24 19:28:55 +020045#include "polarssl/asn1.h"
46#include "polarssl/cipher.h"
Paul Bakker9b5e8852013-06-28 16:12:50 +020047#include "polarssl/oid.h"
Paul Bakker28144de2013-06-24 19:28:55 +020048
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
51#else
52#define polarssl_printf printf
53#endif
54
Paul Bakkerf8d018a2013-06-29 12:16:17 +020055static int pkcs5_parse_pbkdf2_params( asn1_buf *params,
Paul Bakker28144de2013-06-24 19:28:55 +020056 asn1_buf *salt, int *iterations,
57 int *keylen, md_type_t *md_type )
58{
59 int ret;
Paul Bakker28144de2013-06-24 19:28:55 +020060 asn1_buf prf_alg_oid;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020061 unsigned char **p = &params->p;
62 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020063
Paul Bakkerf8d018a2013-06-29 12:16:17 +020064 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
65 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
66 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker28144de2013-06-24 19:28:55 +020067 /*
68 * PBKDF2-params ::= SEQUENCE {
69 * salt OCTET STRING,
70 * iterationCount INTEGER,
71 * keyLength INTEGER OPTIONAL
72 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
73 * }
74 *
75 */
Paul Bakker28144de2013-06-24 19:28:55 +020076 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
77 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
78
79 salt->p = *p;
80 *p += salt->len;
81
82 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
83 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
84
85 if( *p == end )
86 return( 0 );
87
88 if( ( ret = asn1_get_int( p, end, keylen ) ) != 0 )
89 {
90 if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
91 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
92 }
93
94 if( *p == end )
95 return( 0 );
96
Paul Bakkerf8d018a2013-06-29 12:16:17 +020097 if( ( ret = asn1_get_alg_null( p, end, &prf_alg_oid ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +020098 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
99
100 if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) )
101 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
102
103 *md_type = POLARSSL_MD_SHA1;
104
105 if( *p != end )
106 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
107 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
108
109 return( 0 );
110}
111
112int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
113 const unsigned char *pwd, size_t pwdlen,
114 const unsigned char *data, size_t datalen,
115 unsigned char *output )
116{
117 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200118 unsigned char *p, *end;
119 asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
120 asn1_buf salt;
Paul Bakker28144de2013-06-24 19:28:55 +0200121 md_type_t md_type = POLARSSL_MD_SHA1;
122 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200123 size_t olen = 0;
Paul Bakker28144de2013-06-24 19:28:55 +0200124 const md_info_t *md_info;
125 const cipher_info_t *cipher_info;
126 md_context_t md_ctx;
Paul Bakker9b5e8852013-06-28 16:12:50 +0200127 cipher_type_t cipher_alg;
Paul Bakker28144de2013-06-24 19:28:55 +0200128 cipher_context_t cipher_ctx;
129
130 p = pbe_params->p;
131 end = p + pbe_params->len;
132
Paul Bakker46320832013-07-03 14:01:52 +0200133 memset( &md_ctx, 0, sizeof(md_context_t) );
134 memset( &cipher_ctx, 0, sizeof(cipher_context_t) );
135
Paul Bakker28144de2013-06-24 19:28:55 +0200136 /*
137 * PBES2-params ::= SEQUENCE {
138 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
139 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
140 * }
141 */
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200142 if( pbe_params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
143 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
144 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
145
146 if( ( ret = asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200147 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200148
149 // Only PBKDF2 supported at the moment
150 //
151 if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) )
152 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
153
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200154 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
Paul Bakker28144de2013-06-24 19:28:55 +0200155 &salt, &iterations, &keylen,
156 &md_type ) ) != 0 )
157 {
158 return( ret );
159 }
160
161 md_info = md_info_from_type( md_type );
162 if( md_info == NULL )
163 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
164
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200165 if( ( ret = asn1_get_alg( &p, end, &enc_scheme_oid,
166 &enc_scheme_params ) ) != 0 )
167 {
Paul Bakker28144de2013-06-24 19:28:55 +0200168 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200169 }
Paul Bakker28144de2013-06-24 19:28:55 +0200170
Paul Bakker66d5d072014-06-17 16:39:18 +0200171 if( oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200172 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
173
Paul Bakker9b5e8852013-06-28 16:12:50 +0200174 cipher_info = cipher_info_from_type( cipher_alg );
Paul Bakker28144de2013-06-24 19:28:55 +0200175 if( cipher_info == NULL )
176 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
177
Manuel Pégourié-Gonnard66aca932014-06-12 13:14:55 +0200178 /*
179 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
180 * since it is optional and we don't know if it was set or not
181 */
Paul Bakker28144de2013-06-24 19:28:55 +0200182 keylen = cipher_info->key_length / 8;
183
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200184 if( enc_scheme_params.tag != ASN1_OCTET_STRING ||
185 enc_scheme_params.len != cipher_info->iv_size )
186 {
Paul Bakker28144de2013-06-24 19:28:55 +0200187 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200188 }
Paul Bakker28144de2013-06-24 19:28:55 +0200189
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200190 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
Paul Bakker28144de2013-06-24 19:28:55 +0200191
192 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200193 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200194
Paul Bakker66d5d072014-06-17 16:39:18 +0200195 if( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
196 iterations, keylen, key ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200197 {
Paul Bakker46320832013-07-03 14:01:52 +0200198 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200199 }
200
Paul Bakker46320832013-07-03 14:01:52 +0200201 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
202 goto exit;
203
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200204 if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200205 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200206
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200207 if( ( ret = cipher_set_iv( &cipher_ctx, iv, enc_scheme_params.len ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200208 goto exit;
209
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200210 if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200211 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200212
213 if( ( ret = cipher_update( &cipher_ctx, data, datalen,
214 output, &olen ) ) != 0 )
215 {
Paul Bakker46320832013-07-03 14:01:52 +0200216 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200217 }
218
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200219 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200220 ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
Paul Bakker28144de2013-06-24 19:28:55 +0200221
Paul Bakker46320832013-07-03 14:01:52 +0200222exit:
223 md_free_ctx( &md_ctx );
224 cipher_free_ctx( &cipher_ctx );
225
226 return( ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200227}
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200228
229int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
230 size_t plen, const unsigned char *salt, size_t slen,
231 unsigned int iteration_count,
232 uint32_t key_length, unsigned char *output )
233{
234 int ret, j;
235 unsigned int i;
236 unsigned char md1[POLARSSL_MD_MAX_SIZE];
237 unsigned char work[POLARSSL_MD_MAX_SIZE];
238 unsigned char md_size = md_get_size( ctx->md_info );
239 size_t use_len;
240 unsigned char *out_p = output;
241 unsigned char counter[4];
242
243 memset( counter, 0, 4 );
244 counter[3] = 1;
245
246 if( iteration_count > 0xFFFFFFFF )
247 return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
248
249 while( key_length )
250 {
251 // U1 ends up in work
252 //
253 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
254 return( ret );
255
256 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
257 return( ret );
258
259 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
260 return( ret );
261
262 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
263 return( ret );
264
265 memcpy( md1, work, md_size );
266
Paul Bakker66d5d072014-06-17 16:39:18 +0200267 for( i = 1; i < iteration_count; i++ )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200268 {
269 // U2 ends up in md1
270 //
271 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
272 return( ret );
273
274 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
275 return( ret );
276
277 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
278 return( ret );
279
280 // U1 xor U2
281 //
282 for( j = 0; j < md_size; j++ )
283 work[j] ^= md1[j];
284 }
285
286 use_len = ( key_length < md_size ) ? key_length : md_size;
287 memcpy( out_p, work, use_len );
288
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200289 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200290 out_p += use_len;
291
292 for( i = 4; i > 0; i-- )
293 if( ++counter[i - 1] != 0 )
294 break;
295 }
296
297 return( 0 );
298}
299
300#if defined(POLARSSL_SELF_TEST)
301
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200302#if !defined(POLARSSL_SHA1_C)
303int pkcs5_self_test( int verbose )
304{
305 if( verbose != 0 )
306 polarssl_printf( " PBKDF2 (SHA1): skipped\n\n" );
307
308 return( 0 );
309}
310#else
311
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200312#include <stdio.h>
313
314#define MAX_TESTS 6
315
316size_t plen[MAX_TESTS] =
317 { 8, 8, 8, 8, 24, 9 };
318
319unsigned char password[MAX_TESTS][32] =
320{
321 "password",
322 "password",
323 "password",
324 "password",
325 "passwordPASSWORDpassword",
326 "pass\0word",
327};
328
329size_t slen[MAX_TESTS] =
330 { 4, 4, 4, 4, 36, 5 };
331
332unsigned char salt[MAX_TESTS][40] =
333{
334 "salt",
335 "salt",
336 "salt",
337 "salt",
338 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
339 "sa\0lt",
340};
341
342uint32_t it_cnt[MAX_TESTS] =
343 { 1, 2, 4096, 16777216, 4096, 4096 };
344
345uint32_t key_len[MAX_TESTS] =
346 { 20, 20, 20, 20, 25, 16 };
347
348
Paul Bakker9af723c2014-05-01 13:03:14 +0200349unsigned char result_key[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200350{
351 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
352 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
353 0x2f, 0xe0, 0x37, 0xa6 },
354 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
355 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
356 0xd8, 0xde, 0x89, 0x57 },
357 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
358 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
359 0x65, 0xa4, 0x29, 0xc1 },
360 { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
361 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
362 0x26, 0x34, 0xe9, 0x84 },
363 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
364 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
365 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
366 0x38 },
367 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
368 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
369};
370
371int pkcs5_self_test( int verbose )
372{
373 md_context_t sha1_ctx;
374 const md_info_t *info_sha1;
375 int ret, i;
376 unsigned char key[64];
377
378 info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
379 if( info_sha1 == NULL )
380 return( 1 );
381
382 if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
383 return( 1 );
384
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100385 if( verbose != 0 )
Paul Bakkerc3f89aa2014-05-01 10:56:03 +0200386 polarssl_printf( " PBKDF2 note: test #3 may be slow!\n" );
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100387
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200388 for( i = 0; i < MAX_TESTS; i++ )
389 {
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100390 if( verbose != 0 )
391 polarssl_printf( " PBKDF2 (SHA1) #%d: ", i );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200392
393 ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
394 slen[i], it_cnt[i], key_len[i], key );
395 if( ret != 0 ||
396 memcmp( result_key[i], key, key_len[i] ) != 0 )
397 {
398 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100399 polarssl_printf( "failed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200400
401 return( 1 );
402 }
403
404 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100405 polarssl_printf( "passed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200406 }
407
Paul Bakker7dc4c442014-02-01 22:50:26 +0100408 polarssl_printf( "\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200409
Paul Bakker46320832013-07-03 14:01:52 +0200410 if( ( ret = md_free_ctx( &sha1_ctx ) ) != 0 )
411 return( 1 );
Paul Bakkerf8634852013-07-03 13:31:52 +0200412
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200413 return( 0 );
414}
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200415#endif /* POLARSSL_SHA1_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200416
417#endif /* POLARSSL_SELF_TEST */
418
419#endif /* POLARSSL_PKCS5_C */