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