blob: 19a8f41cc76f3e8a4313d6d1866f0c25de122ea6 [file] [log] [blame]
Paul Bakkerf518b162012-08-23 13:03:18 +00001/**
2 * \file pbkdf2.c
3 *
4 * \brief Password-Based Key Derivation Function 2 (from PKCS#5)
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
8 * Copyright (C) 2006-2012, 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 * PBKDF2 is part of PKCS#5
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_PBKDF2_C)
39
40#include "polarssl/pbkdf2.h"
41
42int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen,
43 const unsigned char *salt, size_t slen,
44 unsigned int iteration_count,
45 unsigned long key_length, unsigned char *output )
46{
47 int ret, j;
48 unsigned int i;
49 unsigned char md1[POLARSSL_MD_MAX_SIZE];
50 unsigned char work[POLARSSL_MD_MAX_SIZE];
51 unsigned char md_size = md_get_size( ctx->md_info );
52 size_t use_len;
53 unsigned char *out_p = output;
54 unsigned char counter[4];
55
56 memset( counter, 0, 4 );
57 counter[3] = 1;
58
59 if( iteration_count > 0xFFFFFFFF )
60 return( POLARSSL_ERR_PBKDF2_BAD_INPUT_DATA );
61
62 while( key_length )
63 {
64 // U1 ends up in work
65 //
66 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
67 return( ret );
68
69 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
70 return( ret );
71
72 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
73 return( ret );
74
75 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
76 return( ret );
77
78 memcpy( md1, work, md_size );
79
80 for ( i = 1; i < iteration_count; i++ )
81 {
82 // U2 ends up in md1
83 //
84 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
85 return( ret );
86
87 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
88 return( ret );
89
90 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
91 return( ret );
92
93 // U1 xor U2
94 //
95 for( j = 0; j < md_size; j++ )
96 work[j] ^= md1[j];
97 }
98
99 use_len = ( key_length < md_size ) ? key_length : md_size;
100 memcpy( out_p, work, use_len );
101
102 key_length -= use_len;
103 out_p += use_len;
104
105 for( i = 4; i > 0; i-- )
106 if( ++counter[i - 1] != 0 )
107 break;
108 }
109
110 return( 0 );
111}
112
113
114#if defined(POLARSSL_SELF_TEST)
115
116#include <stdio.h>
117
118#define MAX_TESTS 6
119
120size_t plen[MAX_TESTS] =
121 { 8, 8, 8, 8, 24, 9 };
122
123unsigned char password[MAX_TESTS][32] =
124{
125 "password",
126 "password",
127 "password",
128 "password",
129 "passwordPASSWORDpassword",
130 "pass\0word",
131};
132
133size_t slen[MAX_TESTS] =
134 { 4, 4, 4, 4, 36, 5 };
135
136unsigned char salt[MAX_TESTS][40] =
137{
138 "salt",
139 "salt",
140 "salt",
141 "salt",
142 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
143 "sa\0lt",
144};
145
146unsigned long it_cnt[MAX_TESTS] =
147 { 1, 2, 4096, 16777216, 4096, 4096 };
148
149unsigned long key_len[MAX_TESTS] =
150 { 20, 20, 20, 20, 25, 16 };
151
152
153unsigned char result_key[MAX_TESTS][32] =
154{
155 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
156 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
157 0x2f, 0xe0, 0x37, 0xa6 },
158 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
159 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
160 0xd8, 0xde, 0x89, 0x57 },
161 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
162 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
163 0x65, 0xa4, 0x29, 0xc1 },
164 { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
165 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
166 0x26, 0x34, 0xe9, 0x84 },
167 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
168 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
169 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
170 0x38 },
171 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
172 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
173};
174
175int pbkdf2_self_test( int verbose )
176{
177 md_context_t sha1_ctx;
178 const md_info_t *info_sha1;
179 int ret, i;
180 unsigned char key[64];
181
182 info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
183 if( info_sha1 == NULL )
184 return( 1 );
185
186 if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
187 return( 1 );
188
189 for( i = 0; i < MAX_TESTS; i++ )
190 {
191 printf( " PBKDF2 (SHA1) #%d: ", i );
192
193 ret = pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i], slen[i],
194 it_cnt[i], key_len[i], key );
195 if( ret != 0 ||
196 memcmp( result_key[i], key, key_len[i] ) != 0 )
197 {
198 if( verbose != 0 )
199 printf( "failed\n" );
200
201 return( 1 );
202 }
203
204 if( verbose != 0 )
205 printf( "passed\n" );
206 }
207
208 printf( "\n" );
209
210 return( 0 );
211}
212
213#endif /* POLARSSL_SELF_TEST */
214
215#endif /* POLARSSL_PBKDF2_C */