|
| 1 | +/* |
| 2 | + * Hardware aes collector for the STM32F4 family |
| 3 | + ******************************************************************************* |
| 4 | + * Copyright (c) 2017, STMicroelectronics |
| 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 <string.h> |
| 22 | +#include "mbedtls/aes.h" |
| 23 | + |
| 24 | +#if defined(MBEDTLS_AES_ALT) |
| 25 | + |
| 26 | +static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits ) |
| 27 | +{ |
| 28 | + switch( keybits ) |
| 29 | + { |
| 30 | + case 128: |
| 31 | + ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B; |
| 32 | + memcpy(ctx->aes_key, key, 16); |
| 33 | + break; |
| 34 | + case 192: |
| 35 | + ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B; |
| 36 | + memcpy(ctx->aes_key, key, 24); |
| 37 | + break; |
| 38 | + case 256: |
| 39 | + ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B; |
| 40 | + memcpy(ctx->aes_key, key, 32); |
| 41 | + break; |
| 42 | + default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH ); |
| 43 | + } |
| 44 | + |
| 45 | + /* Deinitializes the CRYP peripheral */ |
| 46 | + if (HAL_CRYP_DeInit(&ctx->hcryp_aes) == HAL_ERROR) |
| 47 | + return (HAL_ERROR); |
| 48 | + |
| 49 | + ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B; |
| 50 | + ctx->hcryp_aes.Instance = CRYP; |
| 51 | + /* Enable CRYP clock */ |
| 52 | + __HAL_RCC_CRYP_CLK_ENABLE(); |
| 53 | + |
| 54 | + ctx->hcryp_aes.Init.pKey = ctx->aes_key; |
| 55 | + if (HAL_CRYP_Init(&ctx->hcryp_aes) == HAL_ERROR) |
| 56 | + return (HAL_ERROR); |
| 57 | + |
| 58 | + /* allow multi-instance of CRYP use: save context for CRYP HW module CR */ |
| 59 | + ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; |
| 60 | + return(0); |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +/* Implementation that should never be optimized out by the compiler */ |
| 65 | +static void mbedtls_zeroize( void *v, size_t n ) { |
| 66 | + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +void mbedtls_aes_init( mbedtls_aes_context *ctx ) |
| 71 | +{ |
| 72 | + memset( ctx, 0, sizeof( mbedtls_aes_context ) ); |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +void mbedtls_aes_free( mbedtls_aes_context *ctx ) |
| 78 | +{ |
| 79 | + if( ctx == NULL ) |
| 80 | + return; |
| 81 | + /* Force the CRYP Periheral Clock Reset */ |
| 82 | + __HAL_RCC_CRYP_FORCE_RESET(); |
| 83 | + |
| 84 | + /* Release the CRYP Periheral Clock Reset */ |
| 85 | + __HAL_RCC_CRYP_RELEASE_RESET(); |
| 86 | + |
| 87 | + mbedtls_zeroize( ctx, sizeof( mbedtls_aes_context ) ); |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, |
| 92 | + unsigned int keybits ) |
| 93 | +{ |
| 94 | + int ret_val = 0; |
| 95 | + ret_val = aes_set_key(ctx, key, keybits); |
| 96 | + return(ret_val); |
| 97 | +} |
| 98 | + |
| 99 | +int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, |
| 100 | + unsigned int keybits ) |
| 101 | +{ |
| 102 | + int ret_val = 0; |
| 103 | + ret_val = aes_set_key(ctx, key, keybits); |
| 104 | + return( ret_val ); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, |
| 109 | + int mode, |
| 110 | + const unsigned char input[16], |
| 111 | + unsigned char output[16] ) |
| 112 | +{ |
| 113 | + |
| 114 | + /* allow multi-instance of CRYP use: restore context for CRYP hw module */ |
| 115 | + ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr; |
| 116 | + |
| 117 | + if(mode == MBEDTLS_AES_DECRYPT) /* AES decryption */ |
| 118 | + { |
| 119 | + ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B; |
| 120 | + ctx->hcryp_aes.Init.pKey = ctx->aes_key; |
| 121 | + mbedtls_aes_decrypt( ctx, input, output ); |
| 122 | + } |
| 123 | + else /* AES encryption */ |
| 124 | + { |
| 125 | + ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B; |
| 126 | + ctx->hcryp_aes.Init.pKey = ctx->aes_key; |
| 127 | + mbedtls_aes_encrypt( ctx, input, output ); |
| 128 | + } |
| 129 | + /* allow multi-instance of CRYP use: save context for CRYP HW module CR */ |
| 130 | + ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; |
| 131 | + |
| 132 | + return( 0 ); |
| 133 | +} |
| 134 | + |
| 135 | +#if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 136 | + |
| 137 | +int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, |
| 138 | + int mode, |
| 139 | + size_t length, |
| 140 | + unsigned char iv[16], |
| 141 | + const unsigned char *input, |
| 142 | + unsigned char *output ) |
| 143 | +{ |
| 144 | + int status=0; |
| 145 | + if( length % 16 ) |
| 146 | + return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); |
| 147 | + |
| 148 | + if( mode == MBEDTLS_AES_DECRYPT ) |
| 149 | + { |
| 150 | + ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init |
| 151 | + |
| 152 | + status = HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init |
| 157 | + |
| 158 | + status = HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10); |
| 159 | + } |
| 160 | + return( status ); |
| 161 | +} |
| 162 | +#endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 163 | + |
| 164 | +#if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 165 | +int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, |
| 166 | + int mode, |
| 167 | + size_t length, |
| 168 | + size_t *iv_off, |
| 169 | + unsigned char iv[16], |
| 170 | + const unsigned char *input, |
| 171 | + unsigned char *output ) |
| 172 | +{ |
| 173 | + int c; |
| 174 | + size_t n = *iv_off; |
| 175 | + |
| 176 | + if( mode == MBEDTLS_AES_DECRYPT ) |
| 177 | + { |
| 178 | + while( length-- ) |
| 179 | + { |
| 180 | + if( n == 0 ) |
| 181 | + mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
| 182 | + |
| 183 | + c = *input++; |
| 184 | + *output++ = (unsigned char)( c ^ iv[n] ); |
| 185 | + iv[n] = (unsigned char) c; |
| 186 | + |
| 187 | + n = ( n + 1 ) & 0x0F; |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + while( length-- ) |
| 193 | + { |
| 194 | + if( n == 0 ) |
| 195 | + mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
| 196 | + |
| 197 | + iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); |
| 198 | + |
| 199 | + n = ( n + 1 ) & 0x0F; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + *iv_off = n; |
| 204 | + |
| 205 | + return( 0 ); |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | +int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, |
| 210 | + int mode, |
| 211 | + size_t length, |
| 212 | + unsigned char iv[16], |
| 213 | + const unsigned char *input, |
| 214 | + unsigned char *output ) |
| 215 | +{ |
| 216 | + unsigned char c; |
| 217 | + unsigned char ov[17]; |
| 218 | + |
| 219 | + while( length-- ) |
| 220 | + { |
| 221 | + memcpy( ov, iv, 16 ); |
| 222 | + mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
| 223 | + |
| 224 | + if( mode == MBEDTLS_AES_DECRYPT ) |
| 225 | + ov[16] = *input; |
| 226 | + |
| 227 | + c = *output++ = (unsigned char)( iv[0] ^ *input++ ); |
| 228 | + |
| 229 | + if( mode == MBEDTLS_AES_ENCRYPT ) |
| 230 | + ov[16] = c; |
| 231 | + |
| 232 | + memcpy( iv, ov + 1, 16 ); |
| 233 | + } |
| 234 | + |
| 235 | + return( 0 ); |
| 236 | +} |
| 237 | + |
| 238 | +#endif /*MBEDTLS_CIPHER_MODE_CFB */ |
| 239 | + |
| 240 | +#if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 241 | +int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, |
| 242 | + size_t length, |
| 243 | + size_t *nc_off, |
| 244 | + unsigned char nonce_counter[16], |
| 245 | + unsigned char stream_block[16], |
| 246 | + const unsigned char *input, |
| 247 | + unsigned char *output ) |
| 248 | +{ |
| 249 | + int c, i; |
| 250 | + size_t n = *nc_off; |
| 251 | + |
| 252 | + while( length-- ) |
| 253 | + { |
| 254 | + if( n == 0 ) { |
| 255 | + mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block ); |
| 256 | + |
| 257 | + for( i = 16; i > 0; i-- ) |
| 258 | + if( ++nonce_counter[i - 1] != 0 ) |
| 259 | + break; |
| 260 | + } |
| 261 | + c = *input++; |
| 262 | + *output++ = (unsigned char)( c ^ stream_block[n] ); |
| 263 | + |
| 264 | + n = ( n + 1 ) & 0x0F; |
| 265 | + } |
| 266 | + |
| 267 | + *nc_off = n; |
| 268 | + |
| 269 | + return( 0 ); |
| 270 | +} |
| 271 | +#endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 272 | + |
| 273 | +void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, |
| 274 | + const unsigned char input[16], |
| 275 | + unsigned char output[16] ) |
| 276 | +{ |
| 277 | + |
| 278 | + if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) !=0) { |
| 279 | + // error found to be returned |
| 280 | + } |
| 281 | + |
| 282 | +} |
| 283 | + |
| 284 | +void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, |
| 285 | + const unsigned char input[16], |
| 286 | + unsigned char output[16] ) |
| 287 | +{ |
| 288 | + |
| 289 | + if(HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10)) { |
| 290 | + // error found to be returned |
| 291 | + } |
| 292 | +} |
| 293 | + |
| 294 | + |
| 295 | +#endif /*MBEDTLS_AES_ALT*/ |
0 commit comments