Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PKCS#12 Personal Information Exchange Syntax |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | /* |
| 23 | * The PKCS #12 Personal Information Exchange Syntax Standard v1.1 |
| 24 | * |
| 25 | * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf |
| 26 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn |
| 27 | */ |
| 28 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 30 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #else |
| 32 | #include POLARSSL_CONFIG_FILE |
| 33 | #endif |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 34 | |
| 35 | #if defined(POLARSSL_PKCS12_C) |
| 36 | |
| 37 | #include "polarssl/pkcs12.h" |
| 38 | #include "polarssl/asn1.h" |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 39 | #include "polarssl/cipher.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 40 | |
| 41 | #if defined(POLARSSL_ARC4_C) |
| 42 | #include "polarssl/arc4.h" |
| 43 | #endif |
| 44 | |
| 45 | #if defined(POLARSSL_DES_C) |
| 46 | #include "polarssl/des.h" |
| 47 | #endif |
| 48 | |
Paul Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 49 | /* Implementation that should never be optimized out by the compiler */ |
| 50 | static void polarssl_zeroize( void *v, size_t n ) { |
| 51 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 52 | } |
| 53 | |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 54 | static int pkcs12_parse_pbe_params( asn1_buf *params, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 55 | asn1_buf *salt, int *iterations ) |
| 56 | { |
| 57 | int ret; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 58 | unsigned char **p = ¶ms->p; |
| 59 | const unsigned char *end = params->p + params->len; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * pkcs-12PbeParams ::= SEQUENCE { |
| 63 | * salt OCTET STRING, |
| 64 | * iterations INTEGER |
| 65 | * } |
| 66 | * |
| 67 | */ |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 68 | if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) |
| 69 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + |
| 70 | POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 71 | |
| 72 | if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 ) |
| 73 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
| 74 | |
| 75 | salt->p = *p; |
| 76 | *p += salt->len; |
| 77 | |
| 78 | if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 ) |
| 79 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
| 80 | |
| 81 | if( *p != end ) |
| 82 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + |
| 83 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 84 | |
| 85 | return( 0 ); |
| 86 | } |
| 87 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 88 | static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 89 | const unsigned char *pwd, size_t pwdlen, |
| 90 | unsigned char *key, size_t keylen, |
| 91 | unsigned char *iv, size_t ivlen ) |
| 92 | { |
| 93 | int ret, iterations; |
| 94 | asn1_buf salt; |
| 95 | size_t i; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 96 | unsigned char unipwd[258]; |
| 97 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 98 | memset( &salt, 0, sizeof(asn1_buf) ); |
| 99 | memset( &unipwd, 0, sizeof(unipwd) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 100 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 101 | if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt, |
| 102 | &iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 103 | return( ret ); |
| 104 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 105 | for( i = 0; i < pwdlen; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 106 | unipwd[i * 2 + 1] = pwd[i]; |
| 107 | |
| 108 | if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 109 | salt.p, salt.len, md_type, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 110 | PKCS12_DERIVE_KEY, iterations ) ) != 0 ) |
| 111 | { |
| 112 | return( ret ); |
| 113 | } |
| 114 | |
| 115 | if( iv == NULL || ivlen == 0 ) |
| 116 | return( 0 ); |
| 117 | |
| 118 | if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 119 | salt.p, salt.len, md_type, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 120 | PKCS12_DERIVE_IV, iterations ) ) != 0 ) |
| 121 | { |
| 122 | return( ret ); |
| 123 | } |
| 124 | return( 0 ); |
| 125 | } |
| 126 | |
| 127 | int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode, |
| 128 | const unsigned char *pwd, size_t pwdlen, |
| 129 | const unsigned char *data, size_t len, |
| 130 | unsigned char *output ) |
| 131 | { |
| 132 | #if !defined(POLARSSL_ARC4_C) |
| 133 | ((void) pbe_params); |
| 134 | ((void) mode); |
| 135 | ((void) pwd); |
| 136 | ((void) pwdlen); |
| 137 | ((void) data); |
| 138 | ((void) len); |
| 139 | ((void) output); |
| 140 | return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 141 | #else |
| 142 | int ret; |
| 143 | unsigned char key[16]; |
| 144 | arc4_context ctx; |
| 145 | ((void) mode); |
| 146 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 147 | arc4_init( &ctx ); |
| 148 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 149 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1, |
| 150 | pwd, pwdlen, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 151 | key, 16, NULL, 0 ) ) != 0 ) |
| 152 | { |
| 153 | return( ret ); |
| 154 | } |
| 155 | |
| 156 | arc4_setup( &ctx, key, 16 ); |
| 157 | if( ( ret = arc4_crypt( &ctx, len, data, output ) ) != 0 ) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 158 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 159 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 160 | exit: |
| 161 | polarssl_zeroize( key, sizeof( key ) ); |
| 162 | arc4_free( &ctx ); |
| 163 | |
| 164 | return( ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 165 | #endif /* POLARSSL_ARC4_C */ |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 166 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 167 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 168 | int pkcs12_pbe( asn1_buf *pbe_params, int mode, |
| 169 | cipher_type_t cipher_type, md_type_t md_type, |
| 170 | const unsigned char *pwd, size_t pwdlen, |
| 171 | const unsigned char *data, size_t len, |
| 172 | unsigned char *output ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 173 | { |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 174 | int ret, keylen = 0; |
| 175 | unsigned char key[32]; |
| 176 | unsigned char iv[16]; |
| 177 | const cipher_info_t *cipher_info; |
| 178 | cipher_context_t cipher_ctx; |
| 179 | size_t olen = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 180 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 181 | cipher_info = cipher_info_from_type( cipher_type ); |
| 182 | if( cipher_info == NULL ) |
| 183 | return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 184 | |
| 185 | keylen = cipher_info->key_length / 8; |
| 186 | |
| 187 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen, |
| 188 | key, keylen, |
| 189 | iv, cipher_info->iv_size ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 190 | { |
| 191 | return( ret ); |
| 192 | } |
| 193 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 194 | cipher_init( &cipher_ctx ); |
| 195 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 196 | if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 197 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | dd0f57f | 2013-09-16 11:47:43 +0200 | [diff] [blame] | 199 | if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 200 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 202 | if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 203 | goto exit; |
| 204 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 205 | if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 206 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 207 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 208 | if( ( ret = cipher_update( &cipher_ctx, data, len, |
| 209 | output, &olen ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 210 | { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 211 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 214 | if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 215 | ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 216 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 217 | exit: |
Paul Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 218 | polarssl_zeroize( key, sizeof( key ) ); |
| 219 | polarssl_zeroize( iv, sizeof( iv ) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 220 | cipher_free( &cipher_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 221 | |
| 222 | return( ret ); |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 223 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 224 | |
| 225 | static void pkcs12_fill_buffer( unsigned char *data, size_t data_len, |
| 226 | const unsigned char *filler, size_t fill_len ) |
| 227 | { |
| 228 | unsigned char *p = data; |
| 229 | size_t use_len; |
| 230 | |
| 231 | while( data_len > 0 ) |
| 232 | { |
| 233 | use_len = ( data_len > fill_len ) ? fill_len : data_len; |
| 234 | memcpy( p, filler, use_len ); |
| 235 | p += use_len; |
| 236 | data_len -= use_len; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | int pkcs12_derivation( unsigned char *data, size_t datalen, |
| 241 | const unsigned char *pwd, size_t pwdlen, |
| 242 | const unsigned char *salt, size_t saltlen, |
| 243 | md_type_t md_type, int id, int iterations ) |
| 244 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 245 | int ret; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 246 | unsigned int j; |
| 247 | |
| 248 | unsigned char diversifier[128]; |
| 249 | unsigned char salt_block[128], pwd_block[128], hash_block[128]; |
| 250 | unsigned char hash_output[POLARSSL_MD_MAX_SIZE]; |
| 251 | unsigned char *p; |
| 252 | unsigned char c; |
| 253 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 254 | size_t hlen, use_len, v, i; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 255 | |
| 256 | const md_info_t *md_info; |
| 257 | md_context_t md_ctx; |
| 258 | |
| 259 | // This version only allows max of 64 bytes of password or salt |
| 260 | if( datalen > 128 || pwdlen > 64 || saltlen > 64 ) |
| 261 | return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA ); |
| 262 | |
| 263 | md_info = md_info_from_type( md_type ); |
| 264 | if( md_info == NULL ) |
| 265 | return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 266 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 267 | md_init( &md_ctx ); |
| 268 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 269 | if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 270 | return( ret ); |
| 271 | hlen = md_get_size( md_info ); |
| 272 | |
| 273 | if( hlen <= 32 ) |
| 274 | v = 64; |
| 275 | else |
| 276 | v = 128; |
| 277 | |
| 278 | memset( diversifier, (unsigned char) id, v ); |
| 279 | |
| 280 | pkcs12_fill_buffer( salt_block, v, salt, saltlen ); |
| 281 | pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen ); |
| 282 | |
| 283 | p = data; |
| 284 | while( datalen > 0 ) |
| 285 | { |
| 286 | // Calculate hash( diversifier || salt_block || pwd_block ) |
| 287 | if( ( ret = md_starts( &md_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 288 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 289 | |
| 290 | if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 291 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 292 | |
| 293 | if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 294 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 295 | |
| 296 | if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 297 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 298 | |
| 299 | if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 300 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 301 | |
| 302 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 303 | for( i = 1; i < (size_t) iterations; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 304 | { |
| 305 | if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 306 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | use_len = ( datalen > hlen ) ? hlen : datalen; |
| 310 | memcpy( p, hash_output, use_len ); |
| 311 | datalen -= use_len; |
| 312 | p += use_len; |
| 313 | |
| 314 | if( datalen == 0 ) |
| 315 | break; |
| 316 | |
| 317 | // Concatenating copies of hash_output into hash_block (B) |
| 318 | pkcs12_fill_buffer( hash_block, v, hash_output, hlen ); |
| 319 | |
| 320 | // B += 1 |
| 321 | for( i = v; i > 0; i-- ) |
| 322 | if( ++hash_block[i - 1] != 0 ) |
| 323 | break; |
| 324 | |
| 325 | // salt_block += B |
| 326 | c = 0; |
| 327 | for( i = v; i > 0; i-- ) |
| 328 | { |
| 329 | j = salt_block[i - 1] + hash_block[i - 1] + c; |
| 330 | c = (unsigned char) (j >> 8); |
| 331 | salt_block[i - 1] = j & 0xFF; |
| 332 | } |
| 333 | |
| 334 | // pwd_block += B |
| 335 | c = 0; |
| 336 | for( i = v; i > 0; i-- ) |
| 337 | { |
| 338 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
| 339 | c = (unsigned char) (j >> 8); |
| 340 | pwd_block[i - 1] = j & 0xFF; |
| 341 | } |
| 342 | } |
| 343 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 344 | ret = 0; |
| 345 | |
| 346 | exit: |
Paul Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 347 | polarssl_zeroize( salt_block, sizeof( salt_block ) ); |
| 348 | polarssl_zeroize( pwd_block, sizeof( pwd_block ) ); |
| 349 | polarssl_zeroize( hash_block, sizeof( hash_block ) ); |
| 350 | polarssl_zeroize( hash_output, sizeof( hash_output ) ); |
| 351 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 352 | md_free( &md_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 353 | |
| 354 | return( ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | #endif /* POLARSSL_PKCS12_C */ |