blob: 0526dc30119b0bc967dde5b00cf8b4816b1c3a17 [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 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker19bd2972013-06-14 12:06:45 +02009 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker19bd2972013-06-14 12:06:45 +020011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26/*
27 * PKCS#5 includes PBKDF2 and more
28 *
29 * http://tools.ietf.org/html/rfc2898 (Specification)
30 * http://tools.ietf.org/html/rfc6070 (Test vectors)
31 */
32
33#include "polarssl/config.h"
34
35#if defined(POLARSSL_PKCS5_C)
36
37#include "polarssl/pkcs5.h"
Paul Bakker1fd43212013-06-17 15:14:42 +020038#include "polarssl/asn1.h"
39#include "polarssl/cipher.h"
40
41#define OID_CMP(oid_str, oid_buf) \
42 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
43 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
44
45static int pkcs5_parse_pbkdf2_params( unsigned char **p,
46 const unsigned char *end,
47 asn1_buf *salt, int *iterations,
48 int *keylen, md_type_t *md_type )
49{
50 int ret;
51 size_t len = 0;
52 asn1_buf prf_alg_oid;
53
54 /*
55 * PBKDF2-params ::= SEQUENCE {
56 * salt OCTET STRING,
57 * iterationCount INTEGER,
58 * keyLength INTEGER OPTIONAL
59 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
60 * }
61 *
62 */
63 if( ( ret = asn1_get_tag( p, end, &len,
64 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
65 {
66 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
67 }
68
69 end = *p + len;
70
71 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
72 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
73
74 salt->p = *p;
75 *p += salt->len;
76
77 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
78 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
79
80 if( *p == end )
81 return( 0 );
82
83 if( ( ret = asn1_get_int( p, end, keylen ) ) != 0 )
84 {
85 if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
86 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
87 }
88
89 if( *p == end )
90 return( 0 );
91
92 if( ( ret = asn1_get_tag( p, end, &prf_alg_oid.len, ASN1_OID ) ) != 0 )
93 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
94
95 if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) )
96 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
97
98 *md_type = POLARSSL_MD_SHA1;
99
100 if( *p != end )
101 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
102 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
103
104 return( 0 );
105}
106
107int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
108 const unsigned char *pwd, size_t pwdlen,
109 const unsigned char *data, size_t datalen,
110 unsigned char *output )
111{
112 int ret, iterations = 0, keylen = 0;
113 unsigned char *p, *end, *end2;
114 asn1_buf kdf_alg_oid, enc_scheme_oid, salt;
115 md_type_t md_type = POLARSSL_MD_SHA1;
116 unsigned char key[32], iv[32];
117 size_t len = 0, olen = 0;
118 const md_info_t *md_info;
119 const cipher_info_t *cipher_info;
120 md_context_t md_ctx;
121 cipher_context_t cipher_ctx;
122
123 p = pbe_params->p;
124 end = p + pbe_params->len;
125
126 /*
127 * PBES2-params ::= SEQUENCE {
128 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
129 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
130 * }
131 */
132 if( ( ret = asn1_get_tag( &p, end, &len,
133 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
134 {
135 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
136 }
137
138 if( ( ret = asn1_get_tag( &p, end, &len,
139 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
140 {
141 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
142 }
143
144 end2 = p + len;
145
146 if( ( ret = asn1_get_tag( &p, end2, &kdf_alg_oid.len, ASN1_OID ) ) != 0 )
147 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
148
149 kdf_alg_oid.p = p;
150 p += kdf_alg_oid.len;
151
152 // Only PBKDF2 supported at the moment
153 //
154 if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) )
155 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
156
157 if( ( ret = pkcs5_parse_pbkdf2_params( &p, end2,
158 &salt, &iterations, &keylen,
159 &md_type ) ) != 0 )
160 {
161 return( ret );
162 }
163
164 md_info = md_info_from_type( md_type );
165 if( md_info == NULL )
166 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
167
168 if( ( ret = asn1_get_tag( &p, end, &len,
169 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
170 {
171 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
172 }
173
174 end2 = p + len;
175
176 if( ( ret = asn1_get_tag( &p, end2, &enc_scheme_oid.len, ASN1_OID ) ) != 0 )
177 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
178
179 enc_scheme_oid.p = p;
180 p += enc_scheme_oid.len;
181
182#if defined(POLARSSL_DES_C)
183 // Only DES-CBC and DES-EDE3-CBC supported at the moment
184 //
185 if( OID_CMP( OID_DES_EDE3_CBC, &enc_scheme_oid ) )
186 {
187 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
188 }
189 else if( OID_CMP( OID_DES_CBC, &enc_scheme_oid ) )
190 {
191 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
192 }
193 else
194#endif /* POLARSSL_DES_C */
195 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
196
197 if( cipher_info == NULL )
198 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
199
200 keylen = cipher_info->key_length / 8;
201
202 if( ( ret = asn1_get_tag( &p, end2, &len, ASN1_OCTET_STRING ) ) != 0 )
203 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
204
205 if( len != cipher_info->iv_size )
206 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
207
208 memcpy( iv, p, len );
209
210 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
211 return( ret );
212
213 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100214 {
215 md_free_ctx( &md_ctx );
Paul Bakker1fd43212013-06-17 15:14:42 +0200216 return( ret );
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100217 }
Paul Bakker1fd43212013-06-17 15:14:42 +0200218
219 if ( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
220 iterations, keylen, key ) ) != 0 )
221 {
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100222 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200223 }
224
225 if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100226 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200227
228 if( ( ret = cipher_reset( &cipher_ctx, iv ) ) != 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_update( &cipher_ctx, data, datalen,
232 output, &olen ) ) != 0 )
233 {
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100234 goto cleanup;
Paul Bakker1fd43212013-06-17 15:14:42 +0200235 }
236
237 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100238 {
239 ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
240 goto cleanup;
241 }
Paul Bakker1fd43212013-06-17 15:14:42 +0200242
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100243cleanup:
244 md_free_ctx( &md_ctx );
245 cipher_free_ctx( &cipher_ctx );
246
247 return( ret );
Paul Bakker1fd43212013-06-17 15:14:42 +0200248}
Paul Bakker19bd2972013-06-14 12:06:45 +0200249
250int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
251 size_t plen, const unsigned char *salt, size_t slen,
252 unsigned int iteration_count,
253 uint32_t key_length, unsigned char *output )
254{
255 int ret, j;
256 unsigned int i;
257 unsigned char md1[POLARSSL_MD_MAX_SIZE];
258 unsigned char work[POLARSSL_MD_MAX_SIZE];
259 unsigned char md_size = md_get_size( ctx->md_info );
260 size_t use_len;
261 unsigned char *out_p = output;
262 unsigned char counter[4];
263
264 memset( counter, 0, 4 );
265 counter[3] = 1;
266
267 if( iteration_count > 0xFFFFFFFF )
268 return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
269
270 while( key_length )
271 {
272 // U1 ends up in work
273 //
274 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
275 return( ret );
276
277 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
278 return( ret );
279
280 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
281 return( ret );
282
283 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
284 return( ret );
285
286 memcpy( md1, work, md_size );
287
288 for ( i = 1; i < iteration_count; i++ )
289 {
290 // U2 ends up in md1
291 //
292 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
293 return( ret );
294
295 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
296 return( ret );
297
298 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
299 return( ret );
300
301 // U1 xor U2
302 //
303 for( j = 0; j < md_size; j++ )
304 work[j] ^= md1[j];
305 }
306
307 use_len = ( key_length < md_size ) ? key_length : md_size;
308 memcpy( out_p, work, use_len );
309
310 key_length -= use_len;
311 out_p += use_len;
312
313 for( i = 4; i > 0; i-- )
314 if( ++counter[i - 1] != 0 )
315 break;
316 }
317
318 return( 0 );
319}
320
321#if defined(POLARSSL_SELF_TEST)
322
323#include <stdio.h>
324
325#define MAX_TESTS 6
326
327size_t plen[MAX_TESTS] =
328 { 8, 8, 8, 8, 24, 9 };
329
330unsigned char password[MAX_TESTS][32] =
331{
332 "password",
333 "password",
334 "password",
335 "password",
336 "passwordPASSWORDpassword",
337 "pass\0word",
338};
339
340size_t slen[MAX_TESTS] =
341 { 4, 4, 4, 4, 36, 5 };
342
343unsigned char salt[MAX_TESTS][40] =
344{
345 "salt",
346 "salt",
347 "salt",
348 "salt",
349 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
350 "sa\0lt",
351};
352
353uint32_t it_cnt[MAX_TESTS] =
354 { 1, 2, 4096, 16777216, 4096, 4096 };
355
356uint32_t key_len[MAX_TESTS] =
357 { 20, 20, 20, 20, 25, 16 };
358
359
360unsigned char result_key[MAX_TESTS][32] =
361{
362 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
363 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
364 0x2f, 0xe0, 0x37, 0xa6 },
365 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
366 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
367 0xd8, 0xde, 0x89, 0x57 },
368 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
369 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
370 0x65, 0xa4, 0x29, 0xc1 },
371 { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
372 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
373 0x26, 0x34, 0xe9, 0x84 },
374 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
375 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
376 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
377 0x38 },
378 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
379 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
380};
381
382int pkcs5_self_test( int verbose )
383{
384 md_context_t sha1_ctx;
385 const md_info_t *info_sha1;
386 int ret, i;
387 unsigned char key[64];
388
389 info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
390 if( info_sha1 == NULL )
391 return( 1 );
392
393 if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
394 return( 1 );
395
396 for( i = 0; i < MAX_TESTS; i++ )
397 {
398 printf( " PBKDF2 (SHA1) #%d: ", i );
399
400 ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
401 slen[i], it_cnt[i], key_len[i], key );
402 if( ret != 0 ||
403 memcmp( result_key[i], key, key_len[i] ) != 0 )
404 {
405 if( verbose != 0 )
406 printf( "failed\n" );
407
408 return( 1 );
409 }
410
411 if( verbose != 0 )
412 printf( "passed\n" );
413 }
414
415 printf( "\n" );
416
Alfred Klompec993732014-07-14 22:07:34 +0200417 return( ret );
Paul Bakker19bd2972013-06-14 12:06:45 +0200418}
419
420#endif /* POLARSSL_SELF_TEST */
421
422#endif /* POLARSSL_PKCS5_C */