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