This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:f7c60d3e7b8a 1 /*
maclobdell 0:f7c60d3e7b8a 2 * PKCS#12 Personal Information Exchange Syntax
maclobdell 0:f7c60d3e7b8a 3 *
maclobdell 0:f7c60d3e7b8a 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
maclobdell 0:f7c60d3e7b8a 5 * SPDX-License-Identifier: Apache-2.0
maclobdell 0:f7c60d3e7b8a 6 *
maclobdell 0:f7c60d3e7b8a 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
maclobdell 0:f7c60d3e7b8a 8 * not use this file except in compliance with the License.
maclobdell 0:f7c60d3e7b8a 9 * You may obtain a copy of the License at
maclobdell 0:f7c60d3e7b8a 10 *
maclobdell 0:f7c60d3e7b8a 11 * http://www.apache.org/licenses/LICENSE-2.0
maclobdell 0:f7c60d3e7b8a 12 *
maclobdell 0:f7c60d3e7b8a 13 * Unless required by applicable law or agreed to in writing, software
maclobdell 0:f7c60d3e7b8a 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
maclobdell 0:f7c60d3e7b8a 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maclobdell 0:f7c60d3e7b8a 16 * See the License for the specific language governing permissions and
maclobdell 0:f7c60d3e7b8a 17 * limitations under the License.
maclobdell 0:f7c60d3e7b8a 18 *
maclobdell 0:f7c60d3e7b8a 19 * This file is part of mbed TLS (https://tls.mbed.org)
maclobdell 0:f7c60d3e7b8a 20 */
maclobdell 0:f7c60d3e7b8a 21 /*
maclobdell 0:f7c60d3e7b8a 22 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
maclobdell 0:f7c60d3e7b8a 23 *
maclobdell 0:f7c60d3e7b8a 24 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
maclobdell 0:f7c60d3e7b8a 25 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
maclobdell 0:f7c60d3e7b8a 26 */
maclobdell 0:f7c60d3e7b8a 27
maclobdell 0:f7c60d3e7b8a 28 #if !defined(MBEDTLS_CONFIG_FILE)
maclobdell 0:f7c60d3e7b8a 29 #include "mbedtls/config.h"
maclobdell 0:f7c60d3e7b8a 30 #else
maclobdell 0:f7c60d3e7b8a 31 #include MBEDTLS_CONFIG_FILE
maclobdell 0:f7c60d3e7b8a 32 #endif
maclobdell 0:f7c60d3e7b8a 33
maclobdell 0:f7c60d3e7b8a 34 #if defined(MBEDTLS_PKCS12_C)
maclobdell 0:f7c60d3e7b8a 35
maclobdell 0:f7c60d3e7b8a 36 #include "mbedtls/pkcs12.h"
maclobdell 0:f7c60d3e7b8a 37 #include "mbedtls/asn1.h"
maclobdell 0:f7c60d3e7b8a 38 #include "mbedtls/cipher.h"
maclobdell 0:f7c60d3e7b8a 39
maclobdell 0:f7c60d3e7b8a 40 #include <string.h>
maclobdell 0:f7c60d3e7b8a 41
maclobdell 0:f7c60d3e7b8a 42 #if defined(MBEDTLS_ARC4_C)
maclobdell 0:f7c60d3e7b8a 43 #include "mbedtls/arc4.h"
maclobdell 0:f7c60d3e7b8a 44 #endif
maclobdell 0:f7c60d3e7b8a 45
maclobdell 0:f7c60d3e7b8a 46 #if defined(MBEDTLS_DES_C)
maclobdell 0:f7c60d3e7b8a 47 #include "mbedtls/des.h"
maclobdell 0:f7c60d3e7b8a 48 #endif
maclobdell 0:f7c60d3e7b8a 49
maclobdell 0:f7c60d3e7b8a 50 /* Implementation that should never be optimized out by the compiler */
maclobdell 0:f7c60d3e7b8a 51 static void mbedtls_zeroize( void *v, size_t n ) {
maclobdell 0:f7c60d3e7b8a 52 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
maclobdell 0:f7c60d3e7b8a 53 }
maclobdell 0:f7c60d3e7b8a 54
maclobdell 0:f7c60d3e7b8a 55 static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
maclobdell 0:f7c60d3e7b8a 56 mbedtls_asn1_buf *salt, int *iterations )
maclobdell 0:f7c60d3e7b8a 57 {
maclobdell 0:f7c60d3e7b8a 58 int ret;
maclobdell 0:f7c60d3e7b8a 59 unsigned char **p = &params->p;
maclobdell 0:f7c60d3e7b8a 60 const unsigned char *end = params->p + params->len;
maclobdell 0:f7c60d3e7b8a 61
maclobdell 0:f7c60d3e7b8a 62 /*
maclobdell 0:f7c60d3e7b8a 63 * pkcs-12PbeParams ::= SEQUENCE {
maclobdell 0:f7c60d3e7b8a 64 * salt OCTET STRING,
maclobdell 0:f7c60d3e7b8a 65 * iterations INTEGER
maclobdell 0:f7c60d3e7b8a 66 * }
maclobdell 0:f7c60d3e7b8a 67 *
maclobdell 0:f7c60d3e7b8a 68 */
maclobdell 0:f7c60d3e7b8a 69 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
maclobdell 0:f7c60d3e7b8a 70 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
maclobdell 0:f7c60d3e7b8a 71 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
maclobdell 0:f7c60d3e7b8a 72
maclobdell 0:f7c60d3e7b8a 73 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 74 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
maclobdell 0:f7c60d3e7b8a 75
maclobdell 0:f7c60d3e7b8a 76 salt->p = *p;
maclobdell 0:f7c60d3e7b8a 77 *p += salt->len;
maclobdell 0:f7c60d3e7b8a 78
maclobdell 0:f7c60d3e7b8a 79 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 80 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
maclobdell 0:f7c60d3e7b8a 81
maclobdell 0:f7c60d3e7b8a 82 if( *p != end )
maclobdell 0:f7c60d3e7b8a 83 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
maclobdell 0:f7c60d3e7b8a 84 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
maclobdell 0:f7c60d3e7b8a 85
maclobdell 0:f7c60d3e7b8a 86 return( 0 );
maclobdell 0:f7c60d3e7b8a 87 }
maclobdell 0:f7c60d3e7b8a 88
maclobdell 0:f7c60d3e7b8a 89 #define PKCS12_MAX_PWDLEN 128
maclobdell 0:f7c60d3e7b8a 90
maclobdell 0:f7c60d3e7b8a 91 static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
maclobdell 0:f7c60d3e7b8a 92 const unsigned char *pwd, size_t pwdlen,
maclobdell 0:f7c60d3e7b8a 93 unsigned char *key, size_t keylen,
maclobdell 0:f7c60d3e7b8a 94 unsigned char *iv, size_t ivlen )
maclobdell 0:f7c60d3e7b8a 95 {
maclobdell 0:f7c60d3e7b8a 96 int ret, iterations;
maclobdell 0:f7c60d3e7b8a 97 mbedtls_asn1_buf salt;
maclobdell 0:f7c60d3e7b8a 98 size_t i;
maclobdell 0:f7c60d3e7b8a 99 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
maclobdell 0:f7c60d3e7b8a 100
maclobdell 0:f7c60d3e7b8a 101 if( pwdlen > PKCS12_MAX_PWDLEN )
maclobdell 0:f7c60d3e7b8a 102 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
maclobdell 0:f7c60d3e7b8a 103
maclobdell 0:f7c60d3e7b8a 104 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
maclobdell 0:f7c60d3e7b8a 105 memset( &unipwd, 0, sizeof(unipwd) );
maclobdell 0:f7c60d3e7b8a 106
maclobdell 0:f7c60d3e7b8a 107 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
maclobdell 0:f7c60d3e7b8a 108 &iterations ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 109 return( ret );
maclobdell 0:f7c60d3e7b8a 110
maclobdell 0:f7c60d3e7b8a 111 for( i = 0; i < pwdlen; i++ )
maclobdell 0:f7c60d3e7b8a 112 unipwd[i * 2 + 1] = pwd[i];
maclobdell 0:f7c60d3e7b8a 113
maclobdell 0:f7c60d3e7b8a 114 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
maclobdell 0:f7c60d3e7b8a 115 salt.p, salt.len, md_type,
maclobdell 0:f7c60d3e7b8a 116 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 117 {
maclobdell 0:f7c60d3e7b8a 118 return( ret );
maclobdell 0:f7c60d3e7b8a 119 }
maclobdell 0:f7c60d3e7b8a 120
maclobdell 0:f7c60d3e7b8a 121 if( iv == NULL || ivlen == 0 )
maclobdell 0:f7c60d3e7b8a 122 return( 0 );
maclobdell 0:f7c60d3e7b8a 123
maclobdell 0:f7c60d3e7b8a 124 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
maclobdell 0:f7c60d3e7b8a 125 salt.p, salt.len, md_type,
maclobdell 0:f7c60d3e7b8a 126 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 127 {
maclobdell 0:f7c60d3e7b8a 128 return( ret );
maclobdell 0:f7c60d3e7b8a 129 }
maclobdell 0:f7c60d3e7b8a 130 return( 0 );
maclobdell 0:f7c60d3e7b8a 131 }
maclobdell 0:f7c60d3e7b8a 132
maclobdell 0:f7c60d3e7b8a 133 #undef PKCS12_MAX_PWDLEN
maclobdell 0:f7c60d3e7b8a 134
maclobdell 0:f7c60d3e7b8a 135 int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
maclobdell 0:f7c60d3e7b8a 136 const unsigned char *pwd, size_t pwdlen,
maclobdell 0:f7c60d3e7b8a 137 const unsigned char *data, size_t len,
maclobdell 0:f7c60d3e7b8a 138 unsigned char *output )
maclobdell 0:f7c60d3e7b8a 139 {
maclobdell 0:f7c60d3e7b8a 140 #if !defined(MBEDTLS_ARC4_C)
maclobdell 0:f7c60d3e7b8a 141 ((void) pbe_params);
maclobdell 0:f7c60d3e7b8a 142 ((void) mode);
maclobdell 0:f7c60d3e7b8a 143 ((void) pwd);
maclobdell 0:f7c60d3e7b8a 144 ((void) pwdlen);
maclobdell 0:f7c60d3e7b8a 145 ((void) data);
maclobdell 0:f7c60d3e7b8a 146 ((void) len);
maclobdell 0:f7c60d3e7b8a 147 ((void) output);
maclobdell 0:f7c60d3e7b8a 148 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
maclobdell 0:f7c60d3e7b8a 149 #else
maclobdell 0:f7c60d3e7b8a 150 int ret;
maclobdell 0:f7c60d3e7b8a 151 unsigned char key[16];
maclobdell 0:f7c60d3e7b8a 152 mbedtls_arc4_context ctx;
maclobdell 0:f7c60d3e7b8a 153 ((void) mode);
maclobdell 0:f7c60d3e7b8a 154
maclobdell 0:f7c60d3e7b8a 155 mbedtls_arc4_init( &ctx );
maclobdell 0:f7c60d3e7b8a 156
maclobdell 0:f7c60d3e7b8a 157 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
maclobdell 0:f7c60d3e7b8a 158 pwd, pwdlen,
maclobdell 0:f7c60d3e7b8a 159 key, 16, NULL, 0 ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 160 {
maclobdell 0:f7c60d3e7b8a 161 return( ret );
maclobdell 0:f7c60d3e7b8a 162 }
maclobdell 0:f7c60d3e7b8a 163
maclobdell 0:f7c60d3e7b8a 164 mbedtls_arc4_setup( &ctx, key, 16 );
maclobdell 0:f7c60d3e7b8a 165 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 166 goto exit;
maclobdell 0:f7c60d3e7b8a 167
maclobdell 0:f7c60d3e7b8a 168 exit:
maclobdell 0:f7c60d3e7b8a 169 mbedtls_zeroize( key, sizeof( key ) );
maclobdell 0:f7c60d3e7b8a 170 mbedtls_arc4_free( &ctx );
maclobdell 0:f7c60d3e7b8a 171
maclobdell 0:f7c60d3e7b8a 172 return( ret );
maclobdell 0:f7c60d3e7b8a 173 #endif /* MBEDTLS_ARC4_C */
maclobdell 0:f7c60d3e7b8a 174 }
maclobdell 0:f7c60d3e7b8a 175
maclobdell 0:f7c60d3e7b8a 176 int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
maclobdell 0:f7c60d3e7b8a 177 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
maclobdell 0:f7c60d3e7b8a 178 const unsigned char *pwd, size_t pwdlen,
maclobdell 0:f7c60d3e7b8a 179 const unsigned char *data, size_t len,
maclobdell 0:f7c60d3e7b8a 180 unsigned char *output )
maclobdell 0:f7c60d3e7b8a 181 {
maclobdell 0:f7c60d3e7b8a 182 int ret, keylen = 0;
maclobdell 0:f7c60d3e7b8a 183 unsigned char key[32];
maclobdell 0:f7c60d3e7b8a 184 unsigned char iv[16];
maclobdell 0:f7c60d3e7b8a 185 const mbedtls_cipher_info_t *cipher_info;
maclobdell 0:f7c60d3e7b8a 186 mbedtls_cipher_context_t cipher_ctx;
maclobdell 0:f7c60d3e7b8a 187 size_t olen = 0;
maclobdell 0:f7c60d3e7b8a 188
maclobdell 0:f7c60d3e7b8a 189 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
maclobdell 0:f7c60d3e7b8a 190 if( cipher_info == NULL )
maclobdell 0:f7c60d3e7b8a 191 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
maclobdell 0:f7c60d3e7b8a 192
maclobdell 0:f7c60d3e7b8a 193 keylen = cipher_info->key_bitlen / 8;
maclobdell 0:f7c60d3e7b8a 194
maclobdell 0:f7c60d3e7b8a 195 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
maclobdell 0:f7c60d3e7b8a 196 key, keylen,
maclobdell 0:f7c60d3e7b8a 197 iv, cipher_info->iv_size ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 198 {
maclobdell 0:f7c60d3e7b8a 199 return( ret );
maclobdell 0:f7c60d3e7b8a 200 }
maclobdell 0:f7c60d3e7b8a 201
maclobdell 0:f7c60d3e7b8a 202 mbedtls_cipher_init( &cipher_ctx );
maclobdell 0:f7c60d3e7b8a 203
maclobdell 0:f7c60d3e7b8a 204 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 205 goto exit;
maclobdell 0:f7c60d3e7b8a 206
maclobdell 0:f7c60d3e7b8a 207 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 208 goto exit;
maclobdell 0:f7c60d3e7b8a 209
maclobdell 0:f7c60d3e7b8a 210 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 211 goto exit;
maclobdell 0:f7c60d3e7b8a 212
maclobdell 0:f7c60d3e7b8a 213 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 214 goto exit;
maclobdell 0:f7c60d3e7b8a 215
maclobdell 0:f7c60d3e7b8a 216 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
maclobdell 0:f7c60d3e7b8a 217 output, &olen ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 218 {
maclobdell 0:f7c60d3e7b8a 219 goto exit;
maclobdell 0:f7c60d3e7b8a 220 }
maclobdell 0:f7c60d3e7b8a 221
maclobdell 0:f7c60d3e7b8a 222 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 223 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
maclobdell 0:f7c60d3e7b8a 224
maclobdell 0:f7c60d3e7b8a 225 exit:
maclobdell 0:f7c60d3e7b8a 226 mbedtls_zeroize( key, sizeof( key ) );
maclobdell 0:f7c60d3e7b8a 227 mbedtls_zeroize( iv, sizeof( iv ) );
maclobdell 0:f7c60d3e7b8a 228 mbedtls_cipher_free( &cipher_ctx );
maclobdell 0:f7c60d3e7b8a 229
maclobdell 0:f7c60d3e7b8a 230 return( ret );
maclobdell 0:f7c60d3e7b8a 231 }
maclobdell 0:f7c60d3e7b8a 232
maclobdell 0:f7c60d3e7b8a 233 static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
maclobdell 0:f7c60d3e7b8a 234 const unsigned char *filler, size_t fill_len )
maclobdell 0:f7c60d3e7b8a 235 {
maclobdell 0:f7c60d3e7b8a 236 unsigned char *p = data;
maclobdell 0:f7c60d3e7b8a 237 size_t use_len;
maclobdell 0:f7c60d3e7b8a 238
maclobdell 0:f7c60d3e7b8a 239 while( data_len > 0 )
maclobdell 0:f7c60d3e7b8a 240 {
maclobdell 0:f7c60d3e7b8a 241 use_len = ( data_len > fill_len ) ? fill_len : data_len;
maclobdell 0:f7c60d3e7b8a 242 memcpy( p, filler, use_len );
maclobdell 0:f7c60d3e7b8a 243 p += use_len;
maclobdell 0:f7c60d3e7b8a 244 data_len -= use_len;
maclobdell 0:f7c60d3e7b8a 245 }
maclobdell 0:f7c60d3e7b8a 246 }
maclobdell 0:f7c60d3e7b8a 247
maclobdell 0:f7c60d3e7b8a 248 int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
maclobdell 0:f7c60d3e7b8a 249 const unsigned char *pwd, size_t pwdlen,
maclobdell 0:f7c60d3e7b8a 250 const unsigned char *salt, size_t saltlen,
maclobdell 0:f7c60d3e7b8a 251 mbedtls_md_type_t md_type, int id, int iterations )
maclobdell 0:f7c60d3e7b8a 252 {
maclobdell 0:f7c60d3e7b8a 253 int ret;
maclobdell 0:f7c60d3e7b8a 254 unsigned int j;
maclobdell 0:f7c60d3e7b8a 255
maclobdell 0:f7c60d3e7b8a 256 unsigned char diversifier[128];
maclobdell 0:f7c60d3e7b8a 257 unsigned char salt_block[128], pwd_block[128], hash_block[128];
maclobdell 0:f7c60d3e7b8a 258 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
maclobdell 0:f7c60d3e7b8a 259 unsigned char *p;
maclobdell 0:f7c60d3e7b8a 260 unsigned char c;
maclobdell 0:f7c60d3e7b8a 261
maclobdell 0:f7c60d3e7b8a 262 size_t hlen, use_len, v, i;
maclobdell 0:f7c60d3e7b8a 263
maclobdell 0:f7c60d3e7b8a 264 const mbedtls_md_info_t *md_info;
maclobdell 0:f7c60d3e7b8a 265 mbedtls_md_context_t md_ctx;
maclobdell 0:f7c60d3e7b8a 266
maclobdell 0:f7c60d3e7b8a 267 // This version only allows max of 64 bytes of password or salt
maclobdell 0:f7c60d3e7b8a 268 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
maclobdell 0:f7c60d3e7b8a 269 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
maclobdell 0:f7c60d3e7b8a 270
maclobdell 0:f7c60d3e7b8a 271 md_info = mbedtls_md_info_from_type( md_type );
maclobdell 0:f7c60d3e7b8a 272 if( md_info == NULL )
maclobdell 0:f7c60d3e7b8a 273 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
maclobdell 0:f7c60d3e7b8a 274
maclobdell 0:f7c60d3e7b8a 275 mbedtls_md_init( &md_ctx );
maclobdell 0:f7c60d3e7b8a 276
maclobdell 0:f7c60d3e7b8a 277 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 278 return( ret );
maclobdell 0:f7c60d3e7b8a 279 hlen = mbedtls_md_get_size( md_info );
maclobdell 0:f7c60d3e7b8a 280
maclobdell 0:f7c60d3e7b8a 281 if( hlen <= 32 )
maclobdell 0:f7c60d3e7b8a 282 v = 64;
maclobdell 0:f7c60d3e7b8a 283 else
maclobdell 0:f7c60d3e7b8a 284 v = 128;
maclobdell 0:f7c60d3e7b8a 285
maclobdell 0:f7c60d3e7b8a 286 memset( diversifier, (unsigned char) id, v );
maclobdell 0:f7c60d3e7b8a 287
maclobdell 0:f7c60d3e7b8a 288 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
maclobdell 0:f7c60d3e7b8a 289 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
maclobdell 0:f7c60d3e7b8a 290
maclobdell 0:f7c60d3e7b8a 291 p = data;
maclobdell 0:f7c60d3e7b8a 292 while( datalen > 0 )
maclobdell 0:f7c60d3e7b8a 293 {
maclobdell 0:f7c60d3e7b8a 294 // Calculate hash( diversifier || salt_block || pwd_block )
maclobdell 0:f7c60d3e7b8a 295 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 296 goto exit;
maclobdell 0:f7c60d3e7b8a 297
maclobdell 0:f7c60d3e7b8a 298 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 299 goto exit;
maclobdell 0:f7c60d3e7b8a 300
maclobdell 0:f7c60d3e7b8a 301 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 302 goto exit;
maclobdell 0:f7c60d3e7b8a 303
maclobdell 0:f7c60d3e7b8a 304 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 305 goto exit;
maclobdell 0:f7c60d3e7b8a 306
maclobdell 0:f7c60d3e7b8a 307 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 308 goto exit;
maclobdell 0:f7c60d3e7b8a 309
maclobdell 0:f7c60d3e7b8a 310 // Perform remaining ( iterations - 1 ) recursive hash calculations
maclobdell 0:f7c60d3e7b8a 311 for( i = 1; i < (size_t) iterations; i++ )
maclobdell 0:f7c60d3e7b8a 312 {
maclobdell 0:f7c60d3e7b8a 313 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 314 goto exit;
maclobdell 0:f7c60d3e7b8a 315 }
maclobdell 0:f7c60d3e7b8a 316
maclobdell 0:f7c60d3e7b8a 317 use_len = ( datalen > hlen ) ? hlen : datalen;
maclobdell 0:f7c60d3e7b8a 318 memcpy( p, hash_output, use_len );
maclobdell 0:f7c60d3e7b8a 319 datalen -= use_len;
maclobdell 0:f7c60d3e7b8a 320 p += use_len;
maclobdell 0:f7c60d3e7b8a 321
maclobdell 0:f7c60d3e7b8a 322 if( datalen == 0 )
maclobdell 0:f7c60d3e7b8a 323 break;
maclobdell 0:f7c60d3e7b8a 324
maclobdell 0:f7c60d3e7b8a 325 // Concatenating copies of hash_output into hash_block (B)
maclobdell 0:f7c60d3e7b8a 326 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
maclobdell 0:f7c60d3e7b8a 327
maclobdell 0:f7c60d3e7b8a 328 // B += 1
maclobdell 0:f7c60d3e7b8a 329 for( i = v; i > 0; i-- )
maclobdell 0:f7c60d3e7b8a 330 if( ++hash_block[i - 1] != 0 )
maclobdell 0:f7c60d3e7b8a 331 break;
maclobdell 0:f7c60d3e7b8a 332
maclobdell 0:f7c60d3e7b8a 333 // salt_block += B
maclobdell 0:f7c60d3e7b8a 334 c = 0;
maclobdell 0:f7c60d3e7b8a 335 for( i = v; i > 0; i-- )
maclobdell 0:f7c60d3e7b8a 336 {
maclobdell 0:f7c60d3e7b8a 337 j = salt_block[i - 1] + hash_block[i - 1] + c;
maclobdell 0:f7c60d3e7b8a 338 c = (unsigned char) (j >> 8);
maclobdell 0:f7c60d3e7b8a 339 salt_block[i - 1] = j & 0xFF;
maclobdell 0:f7c60d3e7b8a 340 }
maclobdell 0:f7c60d3e7b8a 341
maclobdell 0:f7c60d3e7b8a 342 // pwd_block += B
maclobdell 0:f7c60d3e7b8a 343 c = 0;
maclobdell 0:f7c60d3e7b8a 344 for( i = v; i > 0; i-- )
maclobdell 0:f7c60d3e7b8a 345 {
maclobdell 0:f7c60d3e7b8a 346 j = pwd_block[i - 1] + hash_block[i - 1] + c;
maclobdell 0:f7c60d3e7b8a 347 c = (unsigned char) (j >> 8);
maclobdell 0:f7c60d3e7b8a 348 pwd_block[i - 1] = j & 0xFF;
maclobdell 0:f7c60d3e7b8a 349 }
maclobdell 0:f7c60d3e7b8a 350 }
maclobdell 0:f7c60d3e7b8a 351
maclobdell 0:f7c60d3e7b8a 352 ret = 0;
maclobdell 0:f7c60d3e7b8a 353
maclobdell 0:f7c60d3e7b8a 354 exit:
maclobdell 0:f7c60d3e7b8a 355 mbedtls_zeroize( salt_block, sizeof( salt_block ) );
maclobdell 0:f7c60d3e7b8a 356 mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
maclobdell 0:f7c60d3e7b8a 357 mbedtls_zeroize( hash_block, sizeof( hash_block ) );
maclobdell 0:f7c60d3e7b8a 358 mbedtls_zeroize( hash_output, sizeof( hash_output ) );
maclobdell 0:f7c60d3e7b8a 359
maclobdell 0:f7c60d3e7b8a 360 mbedtls_md_free( &md_ctx );
maclobdell 0:f7c60d3e7b8a 361
maclobdell 0:f7c60d3e7b8a 362 return( ret );
maclobdell 0:f7c60d3e7b8a 363 }
maclobdell 0:f7c60d3e7b8a 364
maclobdell 0:f7c60d3e7b8a 365 #endif /* MBEDTLS_PKCS12_C */