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