blob: fa9111541e1d8628eae143f605223af436f85882 [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 *
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 Bakker28144de2013-06-24 19:28:55 +020041#include "polarssl/asn1.h"
42#include "polarssl/cipher.h"
43
44static int pkcs5_parse_pbkdf2_params( unsigned char **p,
45 const unsigned char *end,
46 asn1_buf *salt, int *iterations,
47 int *keylen, md_type_t *md_type )
48{
49 int ret;
50 size_t len = 0;
51 asn1_buf prf_alg_oid;
52
53 /*
54 * PBKDF2-params ::= SEQUENCE {
55 * salt OCTET STRING,
56 * iterationCount INTEGER,
57 * keyLength INTEGER OPTIONAL
58 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
59 * }
60 *
61 */
62 if( ( ret = asn1_get_tag( p, end, &len,
63 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
64 {
65 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
66 }
67
68 end = *p + len;
69
70 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
71 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
72
73 salt->p = *p;
74 *p += salt->len;
75
76 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
77 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
78
79 if( *p == end )
80 return( 0 );
81
82 if( ( ret = asn1_get_int( p, end, keylen ) ) != 0 )
83 {
84 if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
85 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
86 }
87
88 if( *p == end )
89 return( 0 );
90
91 if( ( ret = asn1_get_tag( p, end, &prf_alg_oid.len, ASN1_OID ) ) != 0 )
92 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
93
94 if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) )
95 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
96
97 *md_type = POLARSSL_MD_SHA1;
98
99 if( *p != end )
100 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
101 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
102
103 return( 0 );
104}
105
106int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
107 const unsigned char *pwd, size_t pwdlen,
108 const unsigned char *data, size_t datalen,
109 unsigned char *output )
110{
111 int ret, iterations = 0, keylen = 0;
112 unsigned char *p, *end, *end2;
113 asn1_buf kdf_alg_oid, enc_scheme_oid, salt;
114 md_type_t md_type = POLARSSL_MD_SHA1;
115 unsigned char key[32], iv[32];
116 size_t len = 0, olen = 0;
117 const md_info_t *md_info;
118 const cipher_info_t *cipher_info;
119 md_context_t md_ctx;
120 cipher_context_t cipher_ctx;
121
122 p = pbe_params->p;
123 end = p + pbe_params->len;
124
125 /*
126 * PBES2-params ::= SEQUENCE {
127 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
128 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
129 * }
130 */
131 if( ( ret = asn1_get_tag( &p, end, &len,
132 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
133 {
134 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
135 }
136
137 if( ( ret = asn1_get_tag( &p, end, &len,
138 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
139 {
140 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
141 }
142
143 end2 = p + len;
144
145 if( ( ret = asn1_get_tag( &p, end2, &kdf_alg_oid.len, ASN1_OID ) ) != 0 )
146 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
147
148 kdf_alg_oid.p = p;
149 p += kdf_alg_oid.len;
150
151 // Only PBKDF2 supported at the moment
152 //
153 if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) )
154 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
155
156 if( ( ret = pkcs5_parse_pbkdf2_params( &p, end2,
157 &salt, &iterations, &keylen,
158 &md_type ) ) != 0 )
159 {
160 return( ret );
161 }
162
163 md_info = md_info_from_type( md_type );
164 if( md_info == NULL )
165 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
166
167 if( ( ret = asn1_get_tag( &p, end, &len,
168 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
169 {
170 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
171 }
172
173 end2 = p + len;
174
175 if( ( ret = asn1_get_tag( &p, end2, &enc_scheme_oid.len, ASN1_OID ) ) != 0 )
176 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
177
178 enc_scheme_oid.p = p;
179 p += enc_scheme_oid.len;
180
181#if defined(POLARSSL_DES_C)
182 // Only DES-CBC and DES-EDE3-CBC supported at the moment
183 //
184 if( OID_CMP( OID_DES_EDE3_CBC, &enc_scheme_oid ) )
185 {
186 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
187 }
188 else if( OID_CMP( OID_DES_CBC, &enc_scheme_oid ) )
189 {
190 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
191 }
192 else
193#endif /* POLARSSL_DES_C */
194 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
195
196 if( cipher_info == NULL )
197 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
198
199 keylen = cipher_info->key_length / 8;
200
201 if( ( ret = asn1_get_tag( &p, end2, &len, ASN1_OCTET_STRING ) ) != 0 )
202 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
203
204 if( len != cipher_info->iv_size )
205 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
206
207 memcpy( iv, p, len );
208
209 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
210 return( ret );
211
212 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
213 return( ret );
214
215 if ( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
216 iterations, keylen, key ) ) != 0 )
217 {
218 return( ret );
219 }
220
221 if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
222 return( ret );
223
224 if( ( ret = cipher_reset( &cipher_ctx, iv ) ) != 0 )
225 return( ret );
226
227 if( ( ret = cipher_update( &cipher_ctx, data, datalen,
228 output, &olen ) ) != 0 )
229 {
230 return( ret );
231 }
232
233 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
234 return( POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH );
235
236 return( 0 );
237}
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200238
239int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
240 size_t plen, const unsigned char *salt, size_t slen,
241 unsigned int iteration_count,
242 uint32_t key_length, unsigned char *output )
243{
244 int ret, j;
245 unsigned int i;
246 unsigned char md1[POLARSSL_MD_MAX_SIZE];
247 unsigned char work[POLARSSL_MD_MAX_SIZE];
248 unsigned char md_size = md_get_size( ctx->md_info );
249 size_t use_len;
250 unsigned char *out_p = output;
251 unsigned char counter[4];
252
253 memset( counter, 0, 4 );
254 counter[3] = 1;
255
256 if( iteration_count > 0xFFFFFFFF )
257 return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
258
259 while( key_length )
260 {
261 // U1 ends up in work
262 //
263 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
264 return( ret );
265
266 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
267 return( ret );
268
269 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
270 return( ret );
271
272 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
273 return( ret );
274
275 memcpy( md1, work, md_size );
276
277 for ( i = 1; i < iteration_count; i++ )
278 {
279 // U2 ends up in md1
280 //
281 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
282 return( ret );
283
284 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
285 return( ret );
286
287 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
288 return( ret );
289
290 // U1 xor U2
291 //
292 for( j = 0; j < md_size; j++ )
293 work[j] ^= md1[j];
294 }
295
296 use_len = ( key_length < md_size ) ? key_length : md_size;
297 memcpy( out_p, work, use_len );
298
299 key_length -= use_len;
300 out_p += use_len;
301
302 for( i = 4; i > 0; i-- )
303 if( ++counter[i - 1] != 0 )
304 break;
305 }
306
307 return( 0 );
308}
309
310#if defined(POLARSSL_SELF_TEST)
311
312#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
349unsigned char result_key[MAX_TESTS][32] =
350{
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
385 for( i = 0; i < MAX_TESTS; i++ )
386 {
387 printf( " PBKDF2 (SHA1) #%d: ", i );
388
389 ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
390 slen[i], it_cnt[i], key_len[i], key );
391 if( ret != 0 ||
392 memcmp( result_key[i], key, key_len[i] ) != 0 )
393 {
394 if( verbose != 0 )
395 printf( "failed\n" );
396
397 return( 1 );
398 }
399
400 if( verbose != 0 )
401 printf( "passed\n" );
402 }
403
404 printf( "\n" );
405
406 return( 0 );
407}
408
409#endif /* POLARSSL_SELF_TEST */
410
411#endif /* POLARSSL_PKCS5_C */