blob: 969f4dc618536273f1a703a2c423795709af148d [file] [log] [blame]
Paul Bakker19bd2972013-06-14 12:06:45 +02001/**
2 * \file pkcs5.c
3 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
8 * Copyright (C) 2006-2013, Brainspark B.V.
9 *
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 Bakker1fd43212013-06-17 15:14:42 +020041#include "polarssl/asn1.h"
42#include "polarssl/cipher.h"
43
44#define OID_CMP(oid_str, oid_buf) \
45 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
46 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
47
48static int pkcs5_parse_pbkdf2_params( unsigned char **p,
49 const unsigned char *end,
50 asn1_buf *salt, int *iterations,
51 int *keylen, md_type_t *md_type )
52{
53 int ret;
54 size_t len = 0;
55 asn1_buf prf_alg_oid;
56
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 = asn1_get_tag( p, end, &len,
67 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
68 {
69 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
70 }
71
72 end = *p + len;
73
74 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
75 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
76
77 salt->p = *p;
78 *p += salt->len;
79
80 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
81 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
82
83 if( *p == end )
84 return( 0 );
85
86 if( ( ret = asn1_get_int( p, end, keylen ) ) != 0 )
87 {
88 if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
89 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
90 }
91
92 if( *p == end )
93 return( 0 );
94
95 if( ( ret = asn1_get_tag( p, end, &prf_alg_oid.len, ASN1_OID ) ) != 0 )
96 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
97
98 if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) )
99 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
100
101 *md_type = POLARSSL_MD_SHA1;
102
103 if( *p != end )
104 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
105 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
106
107 return( 0 );
108}
109
110int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
111 const unsigned char *pwd, size_t pwdlen,
112 const unsigned char *data, size_t datalen,
113 unsigned char *output )
114{
115 int ret, iterations = 0, keylen = 0;
116 unsigned char *p, *end, *end2;
117 asn1_buf kdf_alg_oid, enc_scheme_oid, salt;
118 md_type_t md_type = POLARSSL_MD_SHA1;
119 unsigned char key[32], iv[32];
120 size_t len = 0, olen = 0;
121 const md_info_t *md_info;
122 const cipher_info_t *cipher_info;
123 md_context_t md_ctx;
124 cipher_context_t cipher_ctx;
125
126 p = pbe_params->p;
127 end = p + pbe_params->len;
128
129 /*
130 * PBES2-params ::= SEQUENCE {
131 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
132 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
133 * }
134 */
135 if( ( ret = asn1_get_tag( &p, end, &len,
136 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
137 {
138 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
139 }
140
141 if( ( ret = asn1_get_tag( &p, end, &len,
142 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
143 {
144 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
145 }
146
147 end2 = p + len;
148
149 if( ( ret = asn1_get_tag( &p, end2, &kdf_alg_oid.len, ASN1_OID ) ) != 0 )
150 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
151
152 kdf_alg_oid.p = p;
153 p += kdf_alg_oid.len;
154
155 // Only PBKDF2 supported at the moment
156 //
157 if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) )
158 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
159
160 if( ( ret = pkcs5_parse_pbkdf2_params( &p, end2,
161 &salt, &iterations, &keylen,
162 &md_type ) ) != 0 )
163 {
164 return( ret );
165 }
166
167 md_info = md_info_from_type( md_type );
168 if( md_info == NULL )
169 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
170
171 if( ( ret = asn1_get_tag( &p, end, &len,
172 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
173 {
174 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
175 }
176
177 end2 = p + len;
178
179 if( ( ret = asn1_get_tag( &p, end2, &enc_scheme_oid.len, ASN1_OID ) ) != 0 )
180 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
181
182 enc_scheme_oid.p = p;
183 p += enc_scheme_oid.len;
184
185#if defined(POLARSSL_DES_C)
186 // Only DES-CBC and DES-EDE3-CBC supported at the moment
187 //
188 if( OID_CMP( OID_DES_EDE3_CBC, &enc_scheme_oid ) )
189 {
190 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
191 }
192 else if( OID_CMP( OID_DES_CBC, &enc_scheme_oid ) )
193 {
194 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
195 }
196 else
197#endif /* POLARSSL_DES_C */
198 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
199
200 if( cipher_info == NULL )
201 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
202
203 keylen = cipher_info->key_length / 8;
204
205 if( ( ret = asn1_get_tag( &p, end2, &len, ASN1_OCTET_STRING ) ) != 0 )
206 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
207
208 if( len != cipher_info->iv_size )
209 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
210
211 memcpy( iv, p, len );
212
213 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
214 return( ret );
215
216 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100217 {
218 md_free_ctx( &md_ctx );
Paul Bakker1fd43212013-06-17 15:14:42 +0200219 return( ret );
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100220 }
Paul Bakker1fd43212013-06-17 15:14:42 +0200221
222 if ( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
223 iterations, keylen, key ) ) != 0 )
224 {
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100225 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200226 }
227
228 if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100229 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200230
231 if( ( ret = cipher_reset( &cipher_ctx, iv ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100232 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200233
234 if( ( ret = cipher_update( &cipher_ctx, data, datalen,
235 output, &olen ) ) != 0 )
236 {
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100237 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200238 }
239
240 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100241 {
242 ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
243 goto cleanup;
244 }
Paul Bakker1fd43212013-06-17 15:14:42 +0200245
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100246cleanup:
247 md_free_ctx( &md_ctx );
248 cipher_free_ctx( &cipher_ctx );
249
250 return( ret );
Paul Bakker1fd43212013-06-17 15:14:42 +0200251}
Paul Bakker19bd2972013-06-14 12:06:45 +0200252
253int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
254 size_t plen, const unsigned char *salt, size_t slen,
255 unsigned int iteration_count,
256 uint32_t key_length, unsigned char *output )
257{
258 int ret, j;
259 unsigned int i;
260 unsigned char md1[POLARSSL_MD_MAX_SIZE];
261 unsigned char work[POLARSSL_MD_MAX_SIZE];
262 unsigned char md_size = md_get_size( ctx->md_info );
263 size_t use_len;
264 unsigned char *out_p = output;
265 unsigned char counter[4];
266
267 memset( counter, 0, 4 );
268 counter[3] = 1;
269
270 if( iteration_count > 0xFFFFFFFF )
271 return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
272
273 while( key_length )
274 {
275 // U1 ends up in work
276 //
277 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
278 return( ret );
279
280 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
281 return( ret );
282
283 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
284 return( ret );
285
286 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
287 return( ret );
288
289 memcpy( md1, work, md_size );
290
291 for ( i = 1; i < iteration_count; i++ )
292 {
293 // U2 ends up in md1
294 //
295 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
296 return( ret );
297
298 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
299 return( ret );
300
301 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
302 return( ret );
303
304 // U1 xor U2
305 //
306 for( j = 0; j < md_size; j++ )
307 work[j] ^= md1[j];
308 }
309
310 use_len = ( key_length < md_size ) ? key_length : md_size;
311 memcpy( out_p, work, use_len );
312
313 key_length -= use_len;
314 out_p += use_len;
315
316 for( i = 4; i > 0; i-- )
317 if( ++counter[i - 1] != 0 )
318 break;
319 }
320
321 return( 0 );
322}
323
324#if defined(POLARSSL_SELF_TEST)
325
326#include <stdio.h>
327
328#define MAX_TESTS 6
329
330size_t plen[MAX_TESTS] =
331 { 8, 8, 8, 8, 24, 9 };
332
333unsigned char password[MAX_TESTS][32] =
334{
335 "password",
336 "password",
337 "password",
338 "password",
339 "passwordPASSWORDpassword",
340 "pass\0word",
341};
342
343size_t slen[MAX_TESTS] =
344 { 4, 4, 4, 4, 36, 5 };
345
346unsigned char salt[MAX_TESTS][40] =
347{
348 "salt",
349 "salt",
350 "salt",
351 "salt",
352 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
353 "sa\0lt",
354};
355
356uint32_t it_cnt[MAX_TESTS] =
357 { 1, 2, 4096, 16777216, 4096, 4096 };
358
359uint32_t key_len[MAX_TESTS] =
360 { 20, 20, 20, 20, 25, 16 };
361
362
363unsigned char result_key[MAX_TESTS][32] =
364{
365 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
366 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
367 0x2f, 0xe0, 0x37, 0xa6 },
368 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
369 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
370 0xd8, 0xde, 0x89, 0x57 },
371 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
372 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
373 0x65, 0xa4, 0x29, 0xc1 },
374 { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
375 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
376 0x26, 0x34, 0xe9, 0x84 },
377 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
378 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
379 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
380 0x38 },
381 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
382 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
383};
384
385int pkcs5_self_test( int verbose )
386{
387 md_context_t sha1_ctx;
388 const md_info_t *info_sha1;
389 int ret, i;
390 unsigned char key[64];
391
392 info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
393 if( info_sha1 == NULL )
394 return( 1 );
395
396 if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
397 return( 1 );
398
399 for( i = 0; i < MAX_TESTS; i++ )
400 {
401 printf( " PBKDF2 (SHA1) #%d: ", i );
402
403 ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
404 slen[i], it_cnt[i], key_len[i], key );
405 if( ret != 0 ||
406 memcmp( result_key[i], key, key_len[i] ) != 0 )
407 {
408 if( verbose != 0 )
409 printf( "failed\n" );
410
411 return( 1 );
412 }
413
414 if( verbose != 0 )
415 printf( "passed\n" );
416 }
417
418 printf( "\n" );
419
Alfred Klompec993732014-07-14 22:07:34 +0200420 return( ret );
Paul Bakker19bd2972013-06-14 12:06:45 +0200421}
422
423#endif /* POLARSSL_SELF_TEST */
424
425#endif /* POLARSSL_PKCS5_C */