mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/cryptocell/FEATURE_CRYPTOCELL310/cmac_alt.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* |
kenjiArai | 1:9db0e321a9f4 | 2 | * cmac_alt.c |
kenjiArai | 1:9db0e321a9f4 | 3 | * |
kenjiArai | 1:9db0e321a9f4 | 4 | * Copyright (C) 2019, Arm Limited, All Rights Reserved |
kenjiArai | 1:9db0e321a9f4 | 5 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 6 | * |
kenjiArai | 1:9db0e321a9f4 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kenjiArai | 1:9db0e321a9f4 | 8 | * not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 9 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 12 | * |
kenjiArai | 1:9db0e321a9f4 | 13 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kenjiArai | 1:9db0e321a9f4 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 16 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 17 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 18 | * |
kenjiArai | 1:9db0e321a9f4 | 19 | */ |
kenjiArai | 1:9db0e321a9f4 | 20 | |
kenjiArai | 1:9db0e321a9f4 | 21 | #include "mbedtls/cmac.h" |
kenjiArai | 1:9db0e321a9f4 | 22 | #if defined(MBEDTLS_CMAC_ALT) |
kenjiArai | 1:9db0e321a9f4 | 23 | #include "mbedtls/platform.h" |
kenjiArai | 1:9db0e321a9f4 | 24 | #include "mbedtls/platform_util.h" |
kenjiArai | 1:9db0e321a9f4 | 25 | #if defined(MBEDTLS_AES_C) |
kenjiArai | 1:9db0e321a9f4 | 26 | #include "mbedtls/aes.h" |
kenjiArai | 1:9db0e321a9f4 | 27 | #endif |
kenjiArai | 1:9db0e321a9f4 | 28 | #include "ssi_aes_defs.h" |
kenjiArai | 1:9db0e321a9f4 | 29 | #include <string.h> |
kenjiArai | 1:9db0e321a9f4 | 30 | |
kenjiArai | 1:9db0e321a9f4 | 31 | static int init_cc( mbedtls_cmac_context_t *cmac_ctx ) |
kenjiArai | 1:9db0e321a9f4 | 32 | { |
kenjiArai | 1:9db0e321a9f4 | 33 | int ret = 0; |
kenjiArai | 1:9db0e321a9f4 | 34 | SaSiAesUserKeyData_t CC_KeyData; |
kenjiArai | 1:9db0e321a9f4 | 35 | if( SaSi_AesInit( &cmac_ctx->CC_Context, SASI_AES_ENCRYPT, |
kenjiArai | 1:9db0e321a9f4 | 36 | SASI_AES_MODE_CMAC, SASI_AES_PADDING_NONE ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 37 | { |
kenjiArai | 1:9db0e321a9f4 | 38 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
kenjiArai | 1:9db0e321a9f4 | 39 | } |
kenjiArai | 1:9db0e321a9f4 | 40 | |
kenjiArai | 1:9db0e321a9f4 | 41 | CC_KeyData.pKey = cmac_ctx->CC_Key; |
kenjiArai | 1:9db0e321a9f4 | 42 | CC_KeyData.keySize = cmac_ctx->CC_keySizeInBytes; |
kenjiArai | 1:9db0e321a9f4 | 43 | |
kenjiArai | 1:9db0e321a9f4 | 44 | if( SaSi_AesSetKey( &cmac_ctx->CC_Context, SASI_AES_USER_KEY, |
kenjiArai | 1:9db0e321a9f4 | 45 | &CC_KeyData, sizeof( CC_KeyData ) ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 46 | { |
kenjiArai | 1:9db0e321a9f4 | 47 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 48 | goto exit; |
kenjiArai | 1:9db0e321a9f4 | 49 | } |
kenjiArai | 1:9db0e321a9f4 | 50 | |
kenjiArai | 1:9db0e321a9f4 | 51 | cmac_ctx->is_cc_initiated = 1; |
kenjiArai | 1:9db0e321a9f4 | 52 | |
kenjiArai | 1:9db0e321a9f4 | 53 | exit: |
kenjiArai | 1:9db0e321a9f4 | 54 | return( ret ); |
kenjiArai | 1:9db0e321a9f4 | 55 | } |
kenjiArai | 1:9db0e321a9f4 | 56 | |
kenjiArai | 1:9db0e321a9f4 | 57 | static int deinit_cc( mbedtls_cmac_context_t *cmac_ctx ) |
kenjiArai | 1:9db0e321a9f4 | 58 | { |
kenjiArai | 1:9db0e321a9f4 | 59 | if( cmac_ctx->is_cc_initiated == 1 && |
kenjiArai | 1:9db0e321a9f4 | 60 | SaSi_AesFree( &cmac_ctx->CC_Context ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 61 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
kenjiArai | 1:9db0e321a9f4 | 62 | |
kenjiArai | 1:9db0e321a9f4 | 63 | return( 0 ); |
kenjiArai | 1:9db0e321a9f4 | 64 | } |
kenjiArai | 1:9db0e321a9f4 | 65 | |
kenjiArai | 1:9db0e321a9f4 | 66 | int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, |
kenjiArai | 1:9db0e321a9f4 | 67 | const unsigned char *key, size_t keybits ) |
kenjiArai | 1:9db0e321a9f4 | 68 | { |
kenjiArai | 1:9db0e321a9f4 | 69 | mbedtls_cmac_context_t *cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 70 | mbedtls_cipher_type_t type; |
kenjiArai | 1:9db0e321a9f4 | 71 | |
kenjiArai | 1:9db0e321a9f4 | 72 | if( ctx == NULL || ctx->cipher_info == NULL || key == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 73 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 74 | |
kenjiArai | 1:9db0e321a9f4 | 75 | type = ctx->cipher_info->type; |
kenjiArai | 1:9db0e321a9f4 | 76 | |
kenjiArai | 1:9db0e321a9f4 | 77 | switch( type ) |
kenjiArai | 1:9db0e321a9f4 | 78 | { |
kenjiArai | 1:9db0e321a9f4 | 79 | case MBEDTLS_CIPHER_AES_128_ECB: |
kenjiArai | 1:9db0e321a9f4 | 80 | break; |
kenjiArai | 1:9db0e321a9f4 | 81 | case MBEDTLS_CIPHER_AES_192_ECB: |
kenjiArai | 1:9db0e321a9f4 | 82 | case MBEDTLS_CIPHER_AES_256_ECB: |
kenjiArai | 1:9db0e321a9f4 | 83 | case MBEDTLS_CIPHER_DES_EDE3_ECB: |
kenjiArai | 1:9db0e321a9f4 | 84 | return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ); |
kenjiArai | 1:9db0e321a9f4 | 85 | default: |
kenjiArai | 1:9db0e321a9f4 | 86 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 87 | } |
kenjiArai | 1:9db0e321a9f4 | 88 | |
kenjiArai | 1:9db0e321a9f4 | 89 | |
kenjiArai | 1:9db0e321a9f4 | 90 | switch( keybits ) |
kenjiArai | 1:9db0e321a9f4 | 91 | { |
kenjiArai | 1:9db0e321a9f4 | 92 | case 128: |
kenjiArai | 1:9db0e321a9f4 | 93 | /* Allocated and initialise in the cipher context memory for the CMAC |
kenjiArai | 1:9db0e321a9f4 | 94 | * context |
kenjiArai | 1:9db0e321a9f4 | 95 | */ |
kenjiArai | 1:9db0e321a9f4 | 96 | cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) ); |
kenjiArai | 1:9db0e321a9f4 | 97 | if( cmac_ctx == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 98 | return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED ); |
kenjiArai | 1:9db0e321a9f4 | 99 | cmac_ctx->CC_keySizeInBytes = ( keybits / 8 ); |
kenjiArai | 1:9db0e321a9f4 | 100 | memcpy( cmac_ctx->CC_Key, key, cmac_ctx->CC_keySizeInBytes ); |
kenjiArai | 1:9db0e321a9f4 | 101 | break; |
kenjiArai | 1:9db0e321a9f4 | 102 | case 192: |
kenjiArai | 1:9db0e321a9f4 | 103 | case 256: |
kenjiArai | 1:9db0e321a9f4 | 104 | return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ); |
kenjiArai | 1:9db0e321a9f4 | 105 | default: |
kenjiArai | 1:9db0e321a9f4 | 106 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 107 | } |
kenjiArai | 1:9db0e321a9f4 | 108 | |
kenjiArai | 1:9db0e321a9f4 | 109 | ctx->cmac_ctx = cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 110 | return( init_cc( cmac_ctx ) ); |
kenjiArai | 1:9db0e321a9f4 | 111 | } |
kenjiArai | 1:9db0e321a9f4 | 112 | |
kenjiArai | 1:9db0e321a9f4 | 113 | int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, |
kenjiArai | 1:9db0e321a9f4 | 114 | const unsigned char *input, size_t ilen ) |
kenjiArai | 1:9db0e321a9f4 | 115 | { |
kenjiArai | 1:9db0e321a9f4 | 116 | mbedtls_cmac_context_t *cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 117 | int ret = 0; |
kenjiArai | 1:9db0e321a9f4 | 118 | size_t block_size; |
kenjiArai | 1:9db0e321a9f4 | 119 | |
kenjiArai | 1:9db0e321a9f4 | 120 | if( ctx == NULL || ctx->cipher_info == NULL || input == NULL || |
kenjiArai | 1:9db0e321a9f4 | 121 | ctx->cmac_ctx == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 122 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 123 | |
kenjiArai | 1:9db0e321a9f4 | 124 | if( ctx == NULL || ctx->cipher_info == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 125 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 126 | |
kenjiArai | 1:9db0e321a9f4 | 127 | block_size = ctx->cipher_info->block_size; |
kenjiArai | 1:9db0e321a9f4 | 128 | if( block_size != SASI_AES_BLOCK_SIZE_IN_BYTES ) |
kenjiArai | 1:9db0e321a9f4 | 129 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 130 | |
kenjiArai | 1:9db0e321a9f4 | 131 | cmac_ctx = ctx->cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 132 | |
kenjiArai | 1:9db0e321a9f4 | 133 | /* Is there data still to process from the last call? |
kenjiArai | 1:9db0e321a9f4 | 134 | */ |
kenjiArai | 1:9db0e321a9f4 | 135 | if( cmac_ctx->unprocessed_len > 0 ) |
kenjiArai | 1:9db0e321a9f4 | 136 | { |
kenjiArai | 1:9db0e321a9f4 | 137 | const size_t size_to_copy = ilen > ( block_size - cmac_ctx->unprocessed_len ) ? |
kenjiArai | 1:9db0e321a9f4 | 138 | block_size - cmac_ctx->unprocessed_len : ilen; |
kenjiArai | 1:9db0e321a9f4 | 139 | memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len], |
kenjiArai | 1:9db0e321a9f4 | 140 | input, size_to_copy ); |
kenjiArai | 1:9db0e321a9f4 | 141 | cmac_ctx->unprocessed_len += size_to_copy; |
kenjiArai | 1:9db0e321a9f4 | 142 | input += size_to_copy; |
kenjiArai | 1:9db0e321a9f4 | 143 | ilen -= size_to_copy; |
kenjiArai | 1:9db0e321a9f4 | 144 | |
kenjiArai | 1:9db0e321a9f4 | 145 | /* |
kenjiArai | 1:9db0e321a9f4 | 146 | * Process the unproccessed data, in case it reached a full AES block, |
kenjiArai | 1:9db0e321a9f4 | 147 | * and there is still input data. |
kenjiArai | 1:9db0e321a9f4 | 148 | */ |
kenjiArai | 1:9db0e321a9f4 | 149 | if( cmac_ctx->unprocessed_len == SASI_AES_BLOCK_SIZE_IN_BYTES && ilen > 0 ) |
kenjiArai | 1:9db0e321a9f4 | 150 | { |
kenjiArai | 1:9db0e321a9f4 | 151 | if( SaSi_AesBlock( &cmac_ctx->CC_Context, cmac_ctx->unprocessed_block, |
kenjiArai | 1:9db0e321a9f4 | 152 | SASI_AES_BLOCK_SIZE_IN_BYTES, NULL ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 153 | { |
kenjiArai | 1:9db0e321a9f4 | 154 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 155 | goto exit; |
kenjiArai | 1:9db0e321a9f4 | 156 | } |
kenjiArai | 1:9db0e321a9f4 | 157 | cmac_ctx->unprocessed_len = 0; |
kenjiArai | 1:9db0e321a9f4 | 158 | } |
kenjiArai | 1:9db0e321a9f4 | 159 | } |
kenjiArai | 1:9db0e321a9f4 | 160 | |
kenjiArai | 1:9db0e321a9f4 | 161 | if( ilen > 0 ) |
kenjiArai | 1:9db0e321a9f4 | 162 | { |
kenjiArai | 1:9db0e321a9f4 | 163 | const size_t size_to_store = ( ilen % SASI_AES_BLOCK_SIZE_IN_BYTES == 0 ) ? |
kenjiArai | 1:9db0e321a9f4 | 164 | SASI_AES_BLOCK_SIZE_IN_BYTES : ilen % SASI_AES_BLOCK_SIZE_IN_BYTES; |
kenjiArai | 1:9db0e321a9f4 | 165 | memcpy( cmac_ctx->unprocessed_block, |
kenjiArai | 1:9db0e321a9f4 | 166 | input + ilen - size_to_store, |
kenjiArai | 1:9db0e321a9f4 | 167 | size_to_store ); |
kenjiArai | 1:9db0e321a9f4 | 168 | cmac_ctx->unprocessed_len = size_to_store; |
kenjiArai | 1:9db0e321a9f4 | 169 | ilen -= size_to_store; |
kenjiArai | 1:9db0e321a9f4 | 170 | if( ilen > 0 ) |
kenjiArai | 1:9db0e321a9f4 | 171 | { |
kenjiArai | 1:9db0e321a9f4 | 172 | if( SaSi_AesBlock( &cmac_ctx->CC_Context, (uint8_t *)input, |
kenjiArai | 1:9db0e321a9f4 | 173 | ilen, NULL ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 174 | { |
kenjiArai | 1:9db0e321a9f4 | 175 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 176 | goto exit; |
kenjiArai | 1:9db0e321a9f4 | 177 | } |
kenjiArai | 1:9db0e321a9f4 | 178 | } |
kenjiArai | 1:9db0e321a9f4 | 179 | } |
kenjiArai | 1:9db0e321a9f4 | 180 | |
kenjiArai | 1:9db0e321a9f4 | 181 | exit: |
kenjiArai | 1:9db0e321a9f4 | 182 | if( ret != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 183 | { |
kenjiArai | 1:9db0e321a9f4 | 184 | deinit_cc( cmac_ctx ); |
kenjiArai | 1:9db0e321a9f4 | 185 | mbedtls_platform_zeroize( cmac_ctx, sizeof( *cmac_ctx ) ); |
kenjiArai | 1:9db0e321a9f4 | 186 | mbedtls_free( cmac_ctx ); |
kenjiArai | 1:9db0e321a9f4 | 187 | } |
kenjiArai | 1:9db0e321a9f4 | 188 | return( ret ); |
kenjiArai | 1:9db0e321a9f4 | 189 | } |
kenjiArai | 1:9db0e321a9f4 | 190 | |
kenjiArai | 1:9db0e321a9f4 | 191 | int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, |
kenjiArai | 1:9db0e321a9f4 | 192 | unsigned char *output ) |
kenjiArai | 1:9db0e321a9f4 | 193 | { |
kenjiArai | 1:9db0e321a9f4 | 194 | mbedtls_cmac_context_t *cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 195 | int ret = 0; |
kenjiArai | 1:9db0e321a9f4 | 196 | size_t olen = SASI_AES_BLOCK_SIZE_IN_BYTES; |
kenjiArai | 1:9db0e321a9f4 | 197 | |
kenjiArai | 1:9db0e321a9f4 | 198 | if( ctx == NULL || ctx->cipher_info == NULL || |
kenjiArai | 1:9db0e321a9f4 | 199 | ctx->cmac_ctx == NULL || output == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 200 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 201 | |
kenjiArai | 1:9db0e321a9f4 | 202 | cmac_ctx = ctx->cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 203 | |
kenjiArai | 1:9db0e321a9f4 | 204 | if( ( ret = SaSi_AesFinish( &cmac_ctx->CC_Context, cmac_ctx->unprocessed_len, |
kenjiArai | 1:9db0e321a9f4 | 205 | cmac_ctx->unprocessed_block, |
kenjiArai | 1:9db0e321a9f4 | 206 | cmac_ctx->unprocessed_len, output, &olen ) ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 207 | { |
kenjiArai | 1:9db0e321a9f4 | 208 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 209 | goto exit; |
kenjiArai | 1:9db0e321a9f4 | 210 | } |
kenjiArai | 1:9db0e321a9f4 | 211 | |
kenjiArai | 1:9db0e321a9f4 | 212 | exit: |
kenjiArai | 1:9db0e321a9f4 | 213 | if( deinit_cc( cmac_ctx ) && ret == 0 ) |
kenjiArai | 1:9db0e321a9f4 | 214 | { |
kenjiArai | 1:9db0e321a9f4 | 215 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 216 | } |
kenjiArai | 1:9db0e321a9f4 | 217 | |
kenjiArai | 1:9db0e321a9f4 | 218 | return( ret ); |
kenjiArai | 1:9db0e321a9f4 | 219 | } |
kenjiArai | 1:9db0e321a9f4 | 220 | |
kenjiArai | 1:9db0e321a9f4 | 221 | int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ) |
kenjiArai | 1:9db0e321a9f4 | 222 | { |
kenjiArai | 1:9db0e321a9f4 | 223 | mbedtls_cmac_context_t *cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 224 | |
kenjiArai | 1:9db0e321a9f4 | 225 | if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 226 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 227 | |
kenjiArai | 1:9db0e321a9f4 | 228 | cmac_ctx = ctx->cmac_ctx; |
kenjiArai | 1:9db0e321a9f4 | 229 | |
kenjiArai | 1:9db0e321a9f4 | 230 | /* Reset the internal state */ |
kenjiArai | 1:9db0e321a9f4 | 231 | cmac_ctx->unprocessed_len = 0; |
kenjiArai | 1:9db0e321a9f4 | 232 | mbedtls_platform_zeroize( cmac_ctx->unprocessed_block, |
kenjiArai | 1:9db0e321a9f4 | 233 | sizeof( cmac_ctx->unprocessed_block ) ); |
kenjiArai | 1:9db0e321a9f4 | 234 | |
kenjiArai | 1:9db0e321a9f4 | 235 | if( deinit_cc( cmac_ctx ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 236 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
kenjiArai | 1:9db0e321a9f4 | 237 | |
kenjiArai | 1:9db0e321a9f4 | 238 | return( init_cc( cmac_ctx ) ); |
kenjiArai | 1:9db0e321a9f4 | 239 | } |
kenjiArai | 1:9db0e321a9f4 | 240 | |
kenjiArai | 1:9db0e321a9f4 | 241 | int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, |
kenjiArai | 1:9db0e321a9f4 | 242 | const unsigned char *key, size_t keylen, |
kenjiArai | 1:9db0e321a9f4 | 243 | const unsigned char *input, size_t ilen, |
kenjiArai | 1:9db0e321a9f4 | 244 | unsigned char *output ) |
kenjiArai | 1:9db0e321a9f4 | 245 | { |
kenjiArai | 1:9db0e321a9f4 | 246 | int ret = 0; |
kenjiArai | 1:9db0e321a9f4 | 247 | mbedtls_cipher_context_t ctx; |
kenjiArai | 1:9db0e321a9f4 | 248 | size_t olen = SASI_AES_BLOCK_SIZE_IN_BYTES; |
kenjiArai | 1:9db0e321a9f4 | 249 | |
kenjiArai | 1:9db0e321a9f4 | 250 | if( cipher_info == NULL || key == NULL || |
kenjiArai | 1:9db0e321a9f4 | 251 | input == NULL || output == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 252 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
kenjiArai | 1:9db0e321a9f4 | 253 | |
kenjiArai | 1:9db0e321a9f4 | 254 | mbedtls_cipher_init( &ctx ); |
kenjiArai | 1:9db0e321a9f4 | 255 | |
kenjiArai | 1:9db0e321a9f4 | 256 | if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 257 | goto exit; |
kenjiArai | 1:9db0e321a9f4 | 258 | |
kenjiArai | 1:9db0e321a9f4 | 259 | ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen ); |
kenjiArai | 1:9db0e321a9f4 | 260 | if( ret != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 261 | goto exit; |
kenjiArai | 1:9db0e321a9f4 | 262 | |
kenjiArai | 1:9db0e321a9f4 | 263 | if( SaSi_AesFinish( &ctx.cmac_ctx->CC_Context, ilen, ( uint8_t * ) input, |
kenjiArai | 1:9db0e321a9f4 | 264 | ilen, output, &olen ) != 0 ) |
kenjiArai | 1:9db0e321a9f4 | 265 | { |
kenjiArai | 1:9db0e321a9f4 | 266 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 267 | goto clear_cc; |
kenjiArai | 1:9db0e321a9f4 | 268 | } |
kenjiArai | 1:9db0e321a9f4 | 269 | |
kenjiArai | 1:9db0e321a9f4 | 270 | clear_cc: |
kenjiArai | 1:9db0e321a9f4 | 271 | if( deinit_cc( ctx.cmac_ctx ) != 0 && ret == 0 ) |
kenjiArai | 1:9db0e321a9f4 | 272 | { |
kenjiArai | 1:9db0e321a9f4 | 273 | ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
kenjiArai | 1:9db0e321a9f4 | 274 | } |
kenjiArai | 1:9db0e321a9f4 | 275 | |
kenjiArai | 1:9db0e321a9f4 | 276 | exit: |
kenjiArai | 1:9db0e321a9f4 | 277 | mbedtls_cipher_free( &ctx ); |
kenjiArai | 1:9db0e321a9f4 | 278 | |
kenjiArai | 1:9db0e321a9f4 | 279 | return( ret ); |
kenjiArai | 1:9db0e321a9f4 | 280 | |
kenjiArai | 1:9db0e321a9f4 | 281 | } |
kenjiArai | 1:9db0e321a9f4 | 282 | |
kenjiArai | 1:9db0e321a9f4 | 283 | #if defined(MBEDTLS_AES_C) |
kenjiArai | 1:9db0e321a9f4 | 284 | |
kenjiArai | 1:9db0e321a9f4 | 285 | int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len, |
kenjiArai | 1:9db0e321a9f4 | 286 | const unsigned char *input, size_t in_len, |
kenjiArai | 1:9db0e321a9f4 | 287 | unsigned char output[16] ) |
kenjiArai | 1:9db0e321a9f4 | 288 | { |
kenjiArai | 1:9db0e321a9f4 | 289 | return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ); |
kenjiArai | 1:9db0e321a9f4 | 290 | } |
kenjiArai | 1:9db0e321a9f4 | 291 | #endif /* MBEDTLS_AES_C */ |
kenjiArai | 1:9db0e321a9f4 | 292 | |
kenjiArai | 1:9db0e321a9f4 | 293 | |
kenjiArai | 1:9db0e321a9f4 | 294 | #endif /* MBEDTLS_CMAC_ALT */ |