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