| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file pkcs5.c | 
|  | 3 | * | 
|  | 4 | * \brief PKCS#5 functions | 
|  | 5 | * | 
|  | 6 | * \author Mathias Olsson <mathias@kompetensum.com> | 
|  | 7 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 8 | *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 9 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 10 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 11 | * | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 34 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 35 | #else | 
|  | 36 | #include POLARSSL_CONFIG_FILE | 
|  | 37 | #endif | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 38 |  | 
|  | 39 | #if defined(POLARSSL_PKCS5_C) | 
|  | 40 |  | 
|  | 41 | #include "polarssl/pkcs5.h" | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 42 | #include "polarssl/asn1.h" | 
|  | 43 | #include "polarssl/cipher.h" | 
| Paul Bakker | 9b5e885 | 2013-06-28 16:12:50 +0200 | [diff] [blame] | 44 | #include "polarssl/oid.h" | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 45 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 46 | #include <string.h> | 
|  | 47 |  | 
| Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 48 | #if defined(POLARSSL_PLATFORM_C) | 
|  | 49 | #include "polarssl/platform.h" | 
|  | 50 | #else | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 51 | #include <stdio.h> | 
| Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 52 | #define polarssl_printf printf | 
|  | 53 | #endif | 
|  | 54 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 55 | static int pkcs5_parse_pbkdf2_params( const asn1_buf *params, | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 56 | asn1_buf *salt, int *iterations, | 
|  | 57 | int *keylen, md_type_t *md_type ) | 
|  | 58 | { | 
|  | 59 | int ret; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 60 | asn1_buf prf_alg_oid; | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 61 | unsigned char *p = params->p; | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 62 | const unsigned char *end = params->p + params->len; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 63 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 64 | if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) | 
|  | 65 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + | 
|  | 66 | POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 67 | /* | 
|  | 68 | *  PBKDF2-params ::= SEQUENCE { | 
|  | 69 | *    salt              OCTET STRING, | 
|  | 70 | *    iterationCount    INTEGER, | 
|  | 71 | *    keyLength         INTEGER OPTIONAL | 
|  | 72 | *    prf               AlgorithmIdentifier DEFAULT algid-hmacWithSHA1 | 
|  | 73 | *  } | 
|  | 74 | * | 
|  | 75 | */ | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 76 | if( ( ret = asn1_get_tag( &p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 77 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret ); | 
|  | 78 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 79 | salt->p = p; | 
|  | 80 | p += salt->len; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 81 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 82 | if( ( ret = asn1_get_int( &p, end, iterations ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 83 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret ); | 
|  | 84 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 85 | if( p == end ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 86 | return( 0 ); | 
|  | 87 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 88 | if( ( ret = asn1_get_int( &p, end, keylen ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 89 | { | 
|  | 90 | if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) | 
|  | 91 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret ); | 
|  | 92 | } | 
|  | 93 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 94 | if( p == end ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 95 | return( 0 ); | 
|  | 96 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 97 | if( ( ret = asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 98 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret ); | 
|  | 99 |  | 
|  | 100 | if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) ) | 
|  | 101 | return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
|  | 102 |  | 
|  | 103 | *md_type = POLARSSL_MD_SHA1; | 
|  | 104 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 105 | if( p != end ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 106 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + | 
|  | 107 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); | 
|  | 108 |  | 
|  | 109 | return( 0 ); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | int pkcs5_pbes2( asn1_buf *pbe_params, int mode, | 
|  | 113 | const unsigned char *pwd,  size_t pwdlen, | 
|  | 114 | const unsigned char *data, size_t datalen, | 
|  | 115 | unsigned char *output ) | 
|  | 116 | { | 
|  | 117 | int ret, iterations = 0, keylen = 0; | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 118 | unsigned char *p, *end; | 
|  | 119 | asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params; | 
|  | 120 | asn1_buf salt; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 121 | md_type_t md_type = POLARSSL_MD_SHA1; | 
|  | 122 | unsigned char key[32], iv[32]; | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 123 | size_t olen = 0; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 124 | const md_info_t *md_info; | 
|  | 125 | const cipher_info_t *cipher_info; | 
|  | 126 | md_context_t md_ctx; | 
| Paul Bakker | 9b5e885 | 2013-06-28 16:12:50 +0200 | [diff] [blame] | 127 | cipher_type_t cipher_alg; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 128 | cipher_context_t cipher_ctx; | 
|  | 129 |  | 
|  | 130 | p = pbe_params->p; | 
|  | 131 | end = p + pbe_params->len; | 
|  | 132 |  | 
|  | 133 | /* | 
|  | 134 | *  PBES2-params ::= SEQUENCE { | 
|  | 135 | *    keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, | 
|  | 136 | *    encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} | 
|  | 137 | *  } | 
|  | 138 | */ | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 139 | if( pbe_params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) | 
|  | 140 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + | 
|  | 141 | POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); | 
|  | 142 |  | 
|  | 143 | if( ( ret = asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 144 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 145 |  | 
|  | 146 | // Only PBKDF2 supported at the moment | 
|  | 147 | // | 
|  | 148 | if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) ) | 
|  | 149 | return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
|  | 150 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 151 | if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params, | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 152 | &salt, &iterations, &keylen, | 
|  | 153 | &md_type ) ) != 0 ) | 
|  | 154 | { | 
|  | 155 | return( ret ); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | md_info = md_info_from_type( md_type ); | 
|  | 159 | if( md_info == NULL ) | 
|  | 160 | return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
|  | 161 |  | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 162 | if( ( ret = asn1_get_alg( &p, end, &enc_scheme_oid, | 
|  | 163 | &enc_scheme_params ) ) != 0 ) | 
|  | 164 | { | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 165 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret ); | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 166 | } | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 167 |  | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 168 | if( oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 169 | return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
|  | 170 |  | 
| Paul Bakker | 9b5e885 | 2013-06-28 16:12:50 +0200 | [diff] [blame] | 171 | cipher_info = cipher_info_from_type( cipher_alg ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 172 | if( cipher_info == NULL ) | 
|  | 173 | return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
|  | 174 |  | 
| Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 175 | /* | 
|  | 176 | * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored | 
|  | 177 | * since it is optional and we don't know if it was set or not | 
|  | 178 | */ | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 179 | keylen = cipher_info->key_length / 8; | 
|  | 180 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 181 | if( enc_scheme_params.tag != ASN1_OCTET_STRING || | 
|  | 182 | enc_scheme_params.len != cipher_info->iv_size ) | 
|  | 183 | { | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 184 | return( POLARSSL_ERR_PKCS5_INVALID_FORMAT ); | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 185 | } | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 186 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 187 | md_init( &md_ctx ); | 
|  | 188 | cipher_init( &cipher_ctx ); | 
|  | 189 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 190 | memcpy( iv, enc_scheme_params.p, enc_scheme_params.len ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 191 |  | 
|  | 192 | if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 ) | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 193 | goto exit; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 194 |  | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 195 | if( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len, | 
|  | 196 | iterations, keylen, key ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 197 | { | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 198 | goto exit; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 201 | if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 ) | 
|  | 202 | goto exit; | 
|  | 203 |  | 
| Manuel Pégourié-Gonnard | a273371 | 2015-02-10 17:32:14 +0100 | [diff] [blame] | 204 | if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, (operation_t) mode ) ) != 0 ) | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 205 | goto exit; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 206 |  | 
| Manuel Pégourié-Gonnard | 90dac90 | 2014-06-12 17:04:24 +0200 | [diff] [blame] | 207 | if( ( ret = cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len, | 
|  | 208 | data, datalen, output, &olen ) ) != 0 ) | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 209 | ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 210 |  | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 211 | exit: | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 212 | md_free( &md_ctx ); | 
|  | 213 | cipher_free( &cipher_ctx ); | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 214 |  | 
|  | 215 | return( ret ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 216 | } | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 217 |  | 
|  | 218 | int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, | 
|  | 219 | size_t plen, const unsigned char *salt, size_t slen, | 
|  | 220 | unsigned int iteration_count, | 
|  | 221 | uint32_t key_length, unsigned char *output ) | 
|  | 222 | { | 
|  | 223 | int ret, j; | 
|  | 224 | unsigned int i; | 
|  | 225 | unsigned char md1[POLARSSL_MD_MAX_SIZE]; | 
|  | 226 | unsigned char work[POLARSSL_MD_MAX_SIZE]; | 
|  | 227 | unsigned char md_size = md_get_size( ctx->md_info ); | 
|  | 228 | size_t use_len; | 
|  | 229 | unsigned char *out_p = output; | 
|  | 230 | unsigned char counter[4]; | 
|  | 231 |  | 
|  | 232 | memset( counter, 0, 4 ); | 
|  | 233 | counter[3] = 1; | 
|  | 234 |  | 
|  | 235 | if( iteration_count > 0xFFFFFFFF ) | 
|  | 236 | return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA ); | 
|  | 237 |  | 
|  | 238 | while( key_length ) | 
|  | 239 | { | 
|  | 240 | // U1 ends up in work | 
|  | 241 | // | 
|  | 242 | if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 ) | 
|  | 243 | return( ret ); | 
|  | 244 |  | 
|  | 245 | if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 ) | 
|  | 246 | return( ret ); | 
|  | 247 |  | 
|  | 248 | if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 ) | 
|  | 249 | return( ret ); | 
|  | 250 |  | 
|  | 251 | if( ( ret = md_hmac_finish( ctx, work ) ) != 0 ) | 
|  | 252 | return( ret ); | 
|  | 253 |  | 
|  | 254 | memcpy( md1, work, md_size ); | 
|  | 255 |  | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 256 | for( i = 1; i < iteration_count; i++ ) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 257 | { | 
|  | 258 | // U2 ends up in md1 | 
|  | 259 | // | 
|  | 260 | if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 ) | 
|  | 261 | return( ret ); | 
|  | 262 |  | 
|  | 263 | if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 ) | 
|  | 264 | return( ret ); | 
|  | 265 |  | 
|  | 266 | if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 ) | 
|  | 267 | return( ret ); | 
|  | 268 |  | 
|  | 269 | // U1 xor U2 | 
|  | 270 | // | 
|  | 271 | for( j = 0; j < md_size; j++ ) | 
|  | 272 | work[j] ^= md1[j]; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | use_len = ( key_length < md_size ) ? key_length : md_size; | 
|  | 276 | memcpy( out_p, work, use_len ); | 
|  | 277 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 278 | key_length -= (uint32_t) use_len; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 279 | out_p += use_len; | 
|  | 280 |  | 
|  | 281 | for( i = 4; i > 0; i-- ) | 
|  | 282 | if( ++counter[i - 1] != 0 ) | 
|  | 283 | break; | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | return( 0 ); | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | #if defined(POLARSSL_SELF_TEST) | 
|  | 290 |  | 
| Manuel Pégourié-Gonnard | 2a8afa9 | 2014-06-12 12:00:44 +0200 | [diff] [blame] | 291 | #if !defined(POLARSSL_SHA1_C) | 
|  | 292 | int pkcs5_self_test( int verbose ) | 
|  | 293 | { | 
|  | 294 | if( verbose != 0 ) | 
|  | 295 | polarssl_printf( "  PBKDF2 (SHA1): skipped\n\n" ); | 
|  | 296 |  | 
|  | 297 | return( 0 ); | 
|  | 298 | } | 
|  | 299 | #else | 
|  | 300 |  | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 301 | #define MAX_TESTS   6 | 
|  | 302 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 303 | static const size_t plen[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 304 | { 8, 8, 8, 24, 9 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 305 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 306 | static const unsigned char password[MAX_TESTS][32] = | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 307 | { | 
|  | 308 | "password", | 
|  | 309 | "password", | 
|  | 310 | "password", | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 311 | "passwordPASSWORDpassword", | 
|  | 312 | "pass\0word", | 
|  | 313 | }; | 
|  | 314 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 315 | static const size_t slen[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 316 | { 4, 4, 4, 36, 5 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 317 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 318 | static const unsigned char salt[MAX_TESTS][40] = | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 319 | { | 
|  | 320 | "salt", | 
|  | 321 | "salt", | 
|  | 322 | "salt", | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 323 | "saltSALTsaltSALTsaltSALTsaltSALTsalt", | 
|  | 324 | "sa\0lt", | 
|  | 325 | }; | 
|  | 326 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 327 | static const uint32_t it_cnt[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 328 | { 1, 2, 4096, 4096, 4096 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 329 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 330 | static const uint32_t key_len[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 331 | { 20, 20, 20, 25, 16 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 332 |  | 
| Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 333 | static const unsigned char result_key[MAX_TESTS][32] = | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 334 | { | 
|  | 335 | { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, | 
|  | 336 | 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, | 
|  | 337 | 0x2f, 0xe0, 0x37, 0xa6 }, | 
|  | 338 | { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, | 
|  | 339 | 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, | 
|  | 340 | 0xd8, 0xde, 0x89, 0x57 }, | 
|  | 341 | { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, | 
|  | 342 | 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, | 
|  | 343 | 0x65, 0xa4, 0x29, 0xc1 }, | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 344 | { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, | 
|  | 345 | 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, | 
|  | 346 | 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, | 
|  | 347 | 0x38 }, | 
|  | 348 | { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, | 
|  | 349 | 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 }, | 
|  | 350 | }; | 
|  | 351 |  | 
|  | 352 | int pkcs5_self_test( int verbose ) | 
|  | 353 | { | 
|  | 354 | md_context_t sha1_ctx; | 
|  | 355 | const md_info_t *info_sha1; | 
|  | 356 | int ret, i; | 
|  | 357 | unsigned char key[64]; | 
|  | 358 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 359 | md_init( &sha1_ctx ); | 
|  | 360 |  | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 361 | info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 ); | 
|  | 362 | if( info_sha1 == NULL ) | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 363 | { | 
|  | 364 | ret = 1; | 
|  | 365 | goto exit; | 
|  | 366 | } | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 367 |  | 
|  | 368 | if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 ) | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 369 | { | 
|  | 370 | ret = 1; | 
|  | 371 | goto exit; | 
|  | 372 | } | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 373 |  | 
|  | 374 | for( i = 0; i < MAX_TESTS; i++ ) | 
|  | 375 | { | 
| Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 376 | if( verbose != 0 ) | 
|  | 377 | polarssl_printf( "  PBKDF2 (SHA1) #%d: ", i ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 378 |  | 
|  | 379 | ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i], | 
|  | 380 | slen[i], it_cnt[i], key_len[i], key ); | 
|  | 381 | if( ret != 0 || | 
|  | 382 | memcmp( result_key[i], key, key_len[i] ) != 0 ) | 
|  | 383 | { | 
|  | 384 | if( verbose != 0 ) | 
| Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 385 | polarssl_printf( "failed\n" ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 386 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 387 | ret = 1; | 
|  | 388 | goto exit; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
|  | 391 | if( verbose != 0 ) | 
| Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 392 | polarssl_printf( "passed\n" ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 393 | } | 
|  | 394 |  | 
| Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 395 | polarssl_printf( "\n" ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 396 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 397 | exit: | 
|  | 398 | md_free( &sha1_ctx ); | 
| Paul Bakker | f863485 | 2013-07-03 13:31:52 +0200 | [diff] [blame] | 399 |  | 
| Alfred Klomp | 1b4eda3 | 2014-07-14 22:07:34 +0200 | [diff] [blame] | 400 | return( ret ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 401 | } | 
| Manuel Pégourié-Gonnard | 2a8afa9 | 2014-06-12 12:00:44 +0200 | [diff] [blame] | 402 | #endif /* POLARSSL_SHA1_C */ | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 403 |  | 
|  | 404 | #endif /* POLARSSL_SELF_TEST */ | 
|  | 405 |  | 
|  | 406 | #endif /* POLARSSL_PKCS5_C */ |