blob: c4cdf0354aca7e7379bdb402cc446259925cf633 [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
36#include "polarssl/config.h"
37
38#if defined(POLARSSL_PKCS5_C)
39
40#include "polarssl/pkcs5.h"
Paul Bakker28144de2013-06-24 19:28:55 +020041#include "polarssl/asn1.h"
42#include "polarssl/cipher.h"
Paul Bakker9b5e8852013-06-28 16:12:50 +020043#include "polarssl/oid.h"
Paul Bakker28144de2013-06-24 19:28:55 +020044
Paul Bakker7dc4c442014-02-01 22:50:26 +010045#if defined(POLARSSL_PLATFORM_C)
46#include "polarssl/platform.h"
47#else
48#define polarssl_printf printf
49#endif
50
Paul Bakkerf8d018a2013-06-29 12:16:17 +020051static int pkcs5_parse_pbkdf2_params( asn1_buf *params,
Paul Bakker28144de2013-06-24 19:28:55 +020052 asn1_buf *salt, int *iterations,
53 int *keylen, md_type_t *md_type )
54{
55 int ret;
Paul Bakker28144de2013-06-24 19:28:55 +020056 asn1_buf prf_alg_oid;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020057 unsigned char **p = &params->p;
58 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020059
Paul Bakkerf8d018a2013-06-29 12:16:17 +020060 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
61 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
62 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker28144de2013-06-24 19:28:55 +020063 /*
64 * PBKDF2-params ::= SEQUENCE {
65 * salt OCTET STRING,
66 * iterationCount INTEGER,
67 * keyLength INTEGER OPTIONAL
68 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
69 * }
70 *
71 */
Paul Bakker28144de2013-06-24 19:28:55 +020072 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
73 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
74
75 salt->p = *p;
76 *p += salt->len;
77
78 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
79 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
80
81 if( *p == end )
82 return( 0 );
83
84 if( ( ret = asn1_get_int( p, end, keylen ) ) != 0 )
85 {
86 if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
87 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
88 }
89
90 if( *p == end )
91 return( 0 );
92
Paul Bakkerf8d018a2013-06-29 12:16:17 +020093 if( ( ret = asn1_get_alg_null( p, end, &prf_alg_oid ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +020094 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
95
96 if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) )
97 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
98
99 *md_type = POLARSSL_MD_SHA1;
100
101 if( *p != end )
102 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
103 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
104
105 return( 0 );
106}
107
108int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
109 const unsigned char *pwd, size_t pwdlen,
110 const unsigned char *data, size_t datalen,
111 unsigned char *output )
112{
113 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200114 unsigned char *p, *end;
115 asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
116 asn1_buf salt;
Paul Bakker28144de2013-06-24 19:28:55 +0200117 md_type_t md_type = POLARSSL_MD_SHA1;
118 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200119 size_t olen = 0;
Paul Bakker28144de2013-06-24 19:28:55 +0200120 const md_info_t *md_info;
121 const cipher_info_t *cipher_info;
122 md_context_t md_ctx;
Paul Bakker9b5e8852013-06-28 16:12:50 +0200123 cipher_type_t cipher_alg;
Paul Bakker28144de2013-06-24 19:28:55 +0200124 cipher_context_t cipher_ctx;
125
126 p = pbe_params->p;
127 end = p + pbe_params->len;
128
Paul Bakker46320832013-07-03 14:01:52 +0200129 memset( &md_ctx, 0, sizeof(md_context_t) );
130 memset( &cipher_ctx, 0, sizeof(cipher_context_t) );
131
Paul Bakker28144de2013-06-24 19:28:55 +0200132 /*
133 * PBES2-params ::= SEQUENCE {
134 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
135 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
136 * }
137 */
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200138 if( pbe_params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
139 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
140 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
141
142 if( ( ret = asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200143 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200144
145 // Only PBKDF2 supported at the moment
146 //
147 if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) )
148 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
149
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200150 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
Paul Bakker28144de2013-06-24 19:28:55 +0200151 &salt, &iterations, &keylen,
152 &md_type ) ) != 0 )
153 {
154 return( ret );
155 }
156
157 md_info = md_info_from_type( md_type );
158 if( md_info == NULL )
159 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
160
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200161 if( ( ret = asn1_get_alg( &p, end, &enc_scheme_oid, &enc_scheme_params ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200162 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200163
Paul Bakker9b5e8852013-06-28 16:12:50 +0200164 if ( oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200165 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
166
Paul Bakker9b5e8852013-06-28 16:12:50 +0200167 cipher_info = cipher_info_from_type( cipher_alg );
Paul Bakker28144de2013-06-24 19:28:55 +0200168 if( cipher_info == NULL )
169 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
170
171 keylen = cipher_info->key_length / 8;
172
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200173 if( enc_scheme_params.tag != ASN1_OCTET_STRING ||
174 enc_scheme_params.len != cipher_info->iv_size )
175 {
Paul Bakker28144de2013-06-24 19:28:55 +0200176 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200177 }
Paul Bakker28144de2013-06-24 19:28:55 +0200178
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200179 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
Paul Bakker28144de2013-06-24 19:28:55 +0200180
181 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200182 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200183
184 if ( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
185 iterations, keylen, key ) ) != 0 )
186 {
Paul Bakker46320832013-07-03 14:01:52 +0200187 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200188 }
189
Paul Bakker46320832013-07-03 14:01:52 +0200190 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
191 goto exit;
192
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200193 if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200194 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200195
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200196 if( ( ret = cipher_set_iv( &cipher_ctx, iv, enc_scheme_params.len ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200197 goto exit;
198
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200199 if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200200 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200201
202 if( ( ret = cipher_update( &cipher_ctx, data, datalen,
203 output, &olen ) ) != 0 )
204 {
Paul Bakker46320832013-07-03 14:01:52 +0200205 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200206 }
207
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200208 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200209 ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
Paul Bakker28144de2013-06-24 19:28:55 +0200210
Paul Bakker46320832013-07-03 14:01:52 +0200211exit:
212 md_free_ctx( &md_ctx );
213 cipher_free_ctx( &cipher_ctx );
214
215 return( ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200216}
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200217
218int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
219 size_t plen, const unsigned char *salt, size_t slen,
220 unsigned int iteration_count,
221 uint32_t key_length, unsigned char *output )
222{
223 int ret, j;
224 unsigned int i;
225 unsigned char md1[POLARSSL_MD_MAX_SIZE];
226 unsigned char work[POLARSSL_MD_MAX_SIZE];
227 unsigned char md_size = md_get_size( ctx->md_info );
228 size_t use_len;
229 unsigned char *out_p = output;
230 unsigned char counter[4];
231
232 memset( counter, 0, 4 );
233 counter[3] = 1;
234
235 if( iteration_count > 0xFFFFFFFF )
236 return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
237
238 while( key_length )
239 {
240 // U1 ends up in work
241 //
242 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
243 return( ret );
244
245 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
246 return( ret );
247
248 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
249 return( ret );
250
251 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
252 return( ret );
253
254 memcpy( md1, work, md_size );
255
256 for ( i = 1; i < iteration_count; i++ )
257 {
258 // U2 ends up in md1
259 //
260 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
261 return( ret );
262
263 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
264 return( ret );
265
266 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
267 return( ret );
268
269 // U1 xor U2
270 //
271 for( j = 0; j < md_size; j++ )
272 work[j] ^= md1[j];
273 }
274
275 use_len = ( key_length < md_size ) ? key_length : md_size;
276 memcpy( out_p, work, use_len );
277
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200278 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200279 out_p += use_len;
280
281 for( i = 4; i > 0; i-- )
282 if( ++counter[i - 1] != 0 )
283 break;
284 }
285
286 return( 0 );
287}
288
289#if defined(POLARSSL_SELF_TEST)
290
291#include <stdio.h>
292
293#define MAX_TESTS 6
294
295size_t plen[MAX_TESTS] =
296 { 8, 8, 8, 8, 24, 9 };
297
298unsigned char password[MAX_TESTS][32] =
299{
300 "password",
301 "password",
302 "password",
303 "password",
304 "passwordPASSWORDpassword",
305 "pass\0word",
306};
307
308size_t slen[MAX_TESTS] =
309 { 4, 4, 4, 4, 36, 5 };
310
311unsigned char salt[MAX_TESTS][40] =
312{
313 "salt",
314 "salt",
315 "salt",
316 "salt",
317 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
318 "sa\0lt",
319};
320
321uint32_t it_cnt[MAX_TESTS] =
322 { 1, 2, 4096, 16777216, 4096, 4096 };
323
324uint32_t key_len[MAX_TESTS] =
325 { 20, 20, 20, 20, 25, 16 };
326
327
328unsigned char result_key[MAX_TESTS][32] =
329{
330 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
331 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
332 0x2f, 0xe0, 0x37, 0xa6 },
333 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
334 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
335 0xd8, 0xde, 0x89, 0x57 },
336 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
337 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
338 0x65, 0xa4, 0x29, 0xc1 },
339 { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
340 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
341 0x26, 0x34, 0xe9, 0x84 },
342 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
343 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
344 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
345 0x38 },
346 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
347 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
348};
349
350int pkcs5_self_test( int verbose )
351{
352 md_context_t sha1_ctx;
353 const md_info_t *info_sha1;
354 int ret, i;
355 unsigned char key[64];
356
357 info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
358 if( info_sha1 == NULL )
359 return( 1 );
360
361 if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
362 return( 1 );
363
364 for( i = 0; i < MAX_TESTS; i++ )
365 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100366 polarssl_printf( " PBKDF2 (SHA1) #%d: ", i );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200367
368 ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
369 slen[i], it_cnt[i], key_len[i], key );
370 if( ret != 0 ||
371 memcmp( result_key[i], key, key_len[i] ) != 0 )
372 {
373 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100374 polarssl_printf( "failed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200375
376 return( 1 );
377 }
378
379 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100380 polarssl_printf( "passed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200381 }
382
Paul Bakker7dc4c442014-02-01 22:50:26 +0100383 polarssl_printf( "\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200384
Paul Bakker46320832013-07-03 14:01:52 +0200385 if( ( ret = md_free_ctx( &sha1_ctx ) ) != 0 )
386 return( 1 );
Paul Bakkerf8634852013-07-03 13:31:52 +0200387
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200388 return( 0 );
389}
390
391#endif /* POLARSSL_SELF_TEST */
392
393#endif /* POLARSSL_PKCS5_C */