1 /**
2 * \file pkcs5.c
3 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25 /*
26 * PKCS#5 includes PBKDF2 and more
27 *
28 * http://tools.ietf.org/html/rfc2898 (Specification)
29 * http://tools.ietf.org/html/rfc6070 (Test vectors)
30 */
31
32 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include "mbedtls/config.h"
34 #else
35 #include MBEDTLS_CONFIG_FILE
36 #endif
37
38 #if defined(MBEDTLS_PKCS5_C)
39
40 #include "mbedtls/pkcs5.h"
41 #include "mbedtls/asn1.h"
42 #include "mbedtls/cipher.h"
43 #include "mbedtls/oid.h"
44
45 #include <stdio.h>
46 #include <string.h>
47
pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf * params,mbedtls_asn1_buf * salt,int * iterations,int * keylen,mbedtls_md_type_t * md_type)48 static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
49 mbedtls_asn1_buf *salt, int *iterations,
50 int *keylen, mbedtls_md_type_t *md_type )
51 {
52 int ret;
53 mbedtls_asn1_buf prf_alg_oid;
54 unsigned char *p = params->p;
55 const unsigned char *end = params->p + params->len;
56
57 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
58 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
59 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
60 /*
61 * PBKDF2-params ::= SEQUENCE {
62 * salt OCTET STRING,
63 * iterationCount INTEGER,
64 * keyLength INTEGER OPTIONAL
65 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
66 * }
67 *
68 */
69 if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
70 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
71
72 salt->p = p;
73 p += salt->len;
74
75 if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
76 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
77
78 if( p == end )
79 return( 0 );
80
81 if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
82 {
83 if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
84 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
85 }
86
87 if( p == end )
88 return( 0 );
89
90 if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
91 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
92
93 if( MBEDTLS_OID_CMP( MBEDTLS_OID_HMAC_SHA1, &prf_alg_oid ) != 0 )
94 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
95
96 *md_type = MBEDTLS_MD_SHA1;
97
98 if( p != end )
99 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
100 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
101
102 return( 0 );
103 }
104
mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf * pbe_params,int mode,const unsigned char * pwd,size_t pwdlen,const unsigned char * data,size_t datalen,unsigned char * output)105 int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
106 const unsigned char *pwd, size_t pwdlen,
107 const unsigned char *data, size_t datalen,
108 unsigned char *output )
109 {
110 int ret, iterations = 0, keylen = 0;
111 unsigned char *p, *end;
112 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
113 mbedtls_asn1_buf salt;
114 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
115 unsigned char key[32], iv[32];
116 size_t olen = 0;
117 const mbedtls_md_info_t *md_info;
118 const mbedtls_cipher_info_t *cipher_info;
119 mbedtls_md_context_t md_ctx;
120 mbedtls_cipher_type_t cipher_alg;
121 mbedtls_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( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
133 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
134 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
135
136 if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
137 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
138
139 /* Only PBKDF2 supported at the moment */
140 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
141 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
142
143 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
144 &salt, &iterations, &keylen,
145 &md_type ) ) != 0 )
146 {
147 return( ret );
148 }
149
150 md_info = mbedtls_md_info_from_type( md_type );
151 if( md_info == NULL )
152 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
153
154 if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
155 &enc_scheme_params ) ) != 0 )
156 {
157 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
158 }
159
160 if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
161 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
162
163 cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
164 if( cipher_info == NULL )
165 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
166
167 /*
168 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
169 * since it is optional and we don't know if it was set or not
170 */
171 keylen = cipher_info->key_bitlen / 8;
172
173 if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
174 enc_scheme_params.len != cipher_info->iv_size )
175 {
176 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
177 }
178
179 mbedtls_md_init( &md_ctx );
180 mbedtls_cipher_init( &cipher_ctx );
181
182 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
183
184 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
185 goto exit;
186
187 if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
188 iterations, keylen, key ) ) != 0 )
189 {
190 goto exit;
191 }
192
193 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
194 goto exit;
195
196 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
197 goto exit;
198
199 if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
200 data, datalen, output, &olen ) ) != 0 )
201 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
202
203 exit:
204 mbedtls_md_free( &md_ctx );
205 mbedtls_cipher_free( &cipher_ctx );
206
207 return( ret );
208 }
209
mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t * ctx,const unsigned char * password,size_t plen,const unsigned char * salt,size_t slen,unsigned int iteration_count,uint32_t key_length,unsigned char * output)210 int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
211 size_t plen, const unsigned char *salt, size_t slen,
212 unsigned int iteration_count,
213 uint32_t key_length, unsigned char *output )
214 {
215 int ret, j;
216 unsigned int i;
217 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
218 unsigned char work[MBEDTLS_MD_MAX_SIZE];
219 unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
220 size_t use_len;
221 unsigned char *out_p = output;
222 unsigned char counter[4];
223
224 memset( counter, 0, 4 );
225 counter[3] = 1;
226
227 if( iteration_count > 0xFFFFFFFF )
228 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
229
230 while( key_length )
231 {
232 /* U1 ends up in work */
233 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
234 return( ret );
235
236 if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
237 return( ret );
238
239 if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
240 return( ret );
241
242 if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
243 return( ret );
244
245 memcpy( md1, work, md_size );
246
247 for( i = 1; i < iteration_count; i++ )
248 {
249 /* U2 ends up in md1 */
250 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
251 return( ret );
252
253 if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
254 return( ret );
255
256 if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
257 return( ret );
258
259 /* U1 xor U2 */
260 for( j = 0; j < md_size; j++ )
261 work[j] ^= md1[j];
262 }
263
264 use_len = ( key_length < md_size ) ? key_length : md_size;
265 memcpy( out_p, work, use_len );
266
267 key_length -= (uint32_t) use_len;
268 out_p += use_len;
269
270 for( i = 4; i > 0; i-- )
271 if( ++counter[i - 1] != 0 )
272 break;
273 }
274
275 return( 0 );
276 }
277
278 #endif /* MBEDTLS_PKCS5_C */
279