|
| 1 | +/* |
| 2 | + * Hardware aes implementation for STM32F4 STM32F7 and STM32L4 families |
| 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 | + case 128: |
| 30 | + ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B; |
| 31 | + memcpy(ctx->aes_key, key, 16); |
| 32 | + break; |
| 33 | + case 192: |
| 34 | + ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B; |
| 35 | + memcpy(ctx->aes_key, key, 24); |
| 36 | + break; |
| 37 | + case 256: |
| 38 | + ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B; |
| 39 | + memcpy(ctx->aes_key, key, 32); |
| 40 | + break; |
| 41 | + default : |
| 42 | + return (MBEDTLS_ERR_AES_INVALID_KEY_LENGTH); |
| 43 | + } |
| 44 | + |
| 45 | + ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B; |
| 46 | + ctx->hcryp_aes.Instance = CRYP; |
| 47 | + |
| 48 | + /* Deinitializes the CRYP peripheral */ |
| 49 | + if (HAL_CRYP_DeInit(&ctx->hcryp_aes) == HAL_ERROR) { |
| 50 | + return (HAL_ERROR); |
| 51 | + } |
| 52 | + |
| 53 | + /* Enable CRYP clock */ |
| 54 | + __HAL_RCC_CRYP_CLK_ENABLE(); |
| 55 | + |
| 56 | + ctx->hcryp_aes.Init.pKey = ctx->aes_key; |
| 57 | + if (HAL_CRYP_Init(&ctx->hcryp_aes) == HAL_ERROR) { |
| 58 | + return (HAL_ERROR); |
| 59 | + } |
| 60 | + |
| 61 | + /* allow multi-instance of CRYP use: save context for CRYP HW module CR */ |
| 62 | + ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; |
| 63 | + return (0); |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +/* Implementation that should never be optimized out by the compiler */ |
| 68 | +static void mbedtls_zeroize(void *v, size_t n) |
| 69 | +{ |
| 70 | + volatile unsigned char *p = (unsigned char *)v; |
| 71 | + while (n--) { |
| 72 | + *p++ = 0; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +void mbedtls_aes_init(mbedtls_aes_context *ctx) |
| 78 | +{ |
| 79 | + memset(ctx, 0, sizeof(mbedtls_aes_context)); |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +void mbedtls_aes_free(mbedtls_aes_context *ctx) |
| 85 | +{ |
| 86 | + if (ctx == NULL) { |
| 87 | + return; |
| 88 | + } |
| 89 | +#if defined(DUAL_CORE) |
| 90 | + uint32_t timeout = HSEM_TIMEOUT; |
| 91 | + while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID) && (--timeout != 0)) { |
| 92 | + } |
| 93 | +#endif /* DUAL_CORE */ |
| 94 | + /* Force the CRYP Periheral Clock Reset */ |
| 95 | + __HAL_RCC_CRYP_FORCE_RESET(); |
| 96 | + |
| 97 | + /* Release the CRYP Periheral Clock Reset */ |
| 98 | + __HAL_RCC_CRYP_RELEASE_RESET(); |
| 99 | +#if defined(DUAL_CORE) |
| 100 | + LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT); |
| 101 | +#endif /* DUAL_CORE */ |
| 102 | + |
| 103 | + mbedtls_zeroize(ctx, sizeof(mbedtls_aes_context)); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, |
| 108 | + unsigned int keybits) |
| 109 | +{ |
| 110 | + int ret_val = 0; |
| 111 | + ret_val = aes_set_key(ctx, key, keybits); |
| 112 | + return (ret_val); |
| 113 | +} |
| 114 | + |
| 115 | +int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, |
| 116 | + unsigned int keybits) |
| 117 | +{ |
| 118 | + int ret_val = 0; |
| 119 | + ret_val = aes_set_key(ctx, key, keybits); |
| 120 | + return (ret_val); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, |
| 125 | + int mode, |
| 126 | + const unsigned char input[16], |
| 127 | + unsigned char output[16]) |
| 128 | +{ |
| 129 | + |
| 130 | + /* allow multi-instance of CRYP use: restore context for CRYP hw module */ |
| 131 | + ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr; |
| 132 | + ctx->hcryp_aes.Phase = HAL_CRYP_PHASE_READY; |
| 133 | + ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B; |
| 134 | + ctx->hcryp_aes.Init.pKey = ctx->aes_key; |
| 135 | + |
| 136 | + if (mode == MBEDTLS_AES_DECRYPT) { /* AES decryption */ |
| 137 | + if (mbedtls_internal_aes_decrypt(ctx, input, output)) { |
| 138 | + return ST_ERR_AES_BUSY; |
| 139 | + } |
| 140 | + } else { /* AES encryption */ |
| 141 | + if (mbedtls_internal_aes_encrypt(ctx, input, output)) { |
| 142 | + return ST_ERR_AES_BUSY; |
| 143 | + } |
| 144 | + } |
| 145 | + /* allow multi-instance of CRYP use: save context for CRYP HW module CR */ |
| 146 | + ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; |
| 147 | + |
| 148 | + return (0); |
| 149 | +} |
| 150 | + |
| 151 | +#if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 152 | +static int st_cbc_restore_context(mbedtls_aes_context *ctx) |
| 153 | +{ |
| 154 | + /* allow multi-instance of CRYP use: restore context for CRYP hw module */ |
| 155 | + ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr; |
| 156 | + /* Re-initialize AES processor with proper parameters |
| 157 | + and (re-)apply key and IV for multi context usecases */ |
| 158 | + if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK) { |
| 159 | + return ST_ERR_AES_BUSY; |
| 160 | + } |
| 161 | + if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK) { |
| 162 | + return ST_ERR_AES_BUSY; |
| 163 | + } |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, |
| 168 | + int mode, |
| 169 | + size_t length, |
| 170 | + unsigned char iv[16], |
| 171 | + const unsigned char *input, |
| 172 | + unsigned char *output) |
| 173 | +{ |
| 174 | + uint32_t tickstart; |
| 175 | + uint32_t *iv_ptr = (uint32_t *)&iv[0]; |
| 176 | + if (length % 16) { |
| 177 | + return (MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
| 178 | + } |
| 179 | + ctx->hcryp_aes.Init.pInitVect = &iv[0]; |
| 180 | + if (st_cbc_restore_context(ctx) != 0) { |
| 181 | + return (ST_ERR_AES_BUSY); |
| 182 | + } |
| 183 | + |
| 184 | + if (mode == MBEDTLS_AES_DECRYPT) { |
| 185 | + if (HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) { |
| 186 | + return ST_ERR_AES_BUSY; |
| 187 | + } |
| 188 | + /* Save the internal IV vector for multi context purpose */ |
| 189 | + tickstart = HAL_GetTick(); |
| 190 | + while ((ctx->hcryp_aes.Instance->SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY)) != CRYP_SR_IFEM) { |
| 191 | + if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) { |
| 192 | + return ST_ERR_AES_BUSY; // timeout: CRYP processor is busy |
| 193 | + } |
| 194 | + } |
| 195 | + ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; // save here before overwritten |
| 196 | + ctx->hcryp_aes.Instance->CR &= ~CRYP_CR_CRYPEN; |
| 197 | + *iv_ptr++ = ctx->hcryp_aes.Instance->IV0LR; |
| 198 | + *iv_ptr++ = ctx->hcryp_aes.Instance->IV0RR; |
| 199 | + *iv_ptr++ = ctx->hcryp_aes.Instance->IV1LR; |
| 200 | + *iv_ptr++ = ctx->hcryp_aes.Instance->IV1RR; |
| 201 | + } else { |
| 202 | + if (HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) { |
| 203 | + return ST_ERR_AES_BUSY; |
| 204 | + } |
| 205 | + memcpy(iv, output, 16); /* current output is the IV vector for the next call */ |
| 206 | + ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; |
| 207 | + } |
| 208 | + |
| 209 | + return 0; |
| 210 | +} |
| 211 | +#endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 212 | + |
| 213 | +#if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 214 | +int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, |
| 215 | + int mode, |
| 216 | + size_t length, |
| 217 | + size_t *iv_off, |
| 218 | + unsigned char iv[16], |
| 219 | + const unsigned char *input, |
| 220 | + unsigned char *output) |
| 221 | +{ |
| 222 | + int c; |
| 223 | + size_t n = *iv_off; |
| 224 | + |
| 225 | + if (mode == MBEDTLS_AES_DECRYPT) { |
| 226 | + while (length--) { |
| 227 | + if (n == 0) |
| 228 | + if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) { |
| 229 | + return ST_ERR_AES_BUSY; |
| 230 | + } |
| 231 | + |
| 232 | + c = *input++; |
| 233 | + *output++ = (unsigned char)(c ^ iv[n]); |
| 234 | + iv[n] = (unsigned char) c; |
| 235 | + |
| 236 | + n = (n + 1) & 0x0F; |
| 237 | + } |
| 238 | + } else { |
| 239 | + while (length--) { |
| 240 | + if (n == 0) |
| 241 | + if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) { |
| 242 | + return ST_ERR_AES_BUSY; |
| 243 | + } |
| 244 | + |
| 245 | + iv[n] = *output++ = (unsigned char)(iv[n] ^ *input++); |
| 246 | + |
| 247 | + n = (n + 1) & 0x0F; |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + *iv_off = n; |
| 252 | + |
| 253 | + return (0); |
| 254 | +} |
| 255 | + |
| 256 | + |
| 257 | +int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, |
| 258 | + int mode, |
| 259 | + size_t length, |
| 260 | + unsigned char iv[16], |
| 261 | + const unsigned char *input, |
| 262 | + unsigned char *output) |
| 263 | +{ |
| 264 | + unsigned char c; |
| 265 | + unsigned char ov[17]; |
| 266 | + |
| 267 | + while (length--) { |
| 268 | + memcpy(ov, iv, 16); |
| 269 | + if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) { |
| 270 | + return ST_ERR_AES_BUSY; |
| 271 | + } |
| 272 | + |
| 273 | + if (mode == MBEDTLS_AES_DECRYPT) { |
| 274 | + ov[16] = *input; |
| 275 | + } |
| 276 | + |
| 277 | + c = *output++ = (unsigned char)(iv[0] ^ *input++); |
| 278 | + |
| 279 | + if (mode == MBEDTLS_AES_ENCRYPT) { |
| 280 | + ov[16] = c; |
| 281 | + } |
| 282 | + |
| 283 | + memcpy(iv, ov + 1, 16); |
| 284 | + } |
| 285 | + |
| 286 | + return (0); |
| 287 | +} |
| 288 | + |
| 289 | +#endif /*MBEDTLS_CIPHER_MODE_CFB */ |
| 290 | + |
| 291 | +#if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 292 | +int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, |
| 293 | + size_t length, |
| 294 | + size_t *nc_off, |
| 295 | + unsigned char nonce_counter[16], |
| 296 | + unsigned char stream_block[16], |
| 297 | + const unsigned char *input, |
| 298 | + unsigned char *output) |
| 299 | +{ |
| 300 | + int c, i; |
| 301 | + size_t n = *nc_off; |
| 302 | + |
| 303 | + while (length--) { |
| 304 | + if (n == 0) { |
| 305 | + if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block) != 0) { |
| 306 | + return ST_ERR_AES_BUSY; |
| 307 | + } |
| 308 | + |
| 309 | + for (i = 16; i > 0; i--) |
| 310 | + if (++nonce_counter[i - 1] != 0) { |
| 311 | + break; |
| 312 | + } |
| 313 | + } |
| 314 | + c = *input++; |
| 315 | + *output++ = (unsigned char)(c ^ stream_block[n]); |
| 316 | + |
| 317 | + n = (n + 1) & 0x0F; |
| 318 | + } |
| 319 | + |
| 320 | + *nc_off = n; |
| 321 | + |
| 322 | + return (0); |
| 323 | +} |
| 324 | +#endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 325 | + |
| 326 | +int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx, |
| 327 | + const unsigned char input[16], |
| 328 | + unsigned char output[16]) |
| 329 | +{ |
| 330 | + if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) { |
| 331 | + // error found |
| 332 | + return ST_ERR_AES_BUSY; |
| 333 | + } |
| 334 | + return 0; |
| 335 | + |
| 336 | +} |
| 337 | + |
| 338 | +int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx, |
| 339 | + const unsigned char input[16], |
| 340 | + unsigned char output[16]) |
| 341 | +{ |
| 342 | + if (HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) { |
| 343 | + // error found |
| 344 | + return ST_ERR_AES_BUSY; |
| 345 | + } |
| 346 | + return 0; |
| 347 | +} |
| 348 | + |
| 349 | +#if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 350 | +void mbedtls_aes_encrypt(mbedtls_aes_context *ctx, |
| 351 | + const unsigned char input[16], |
| 352 | + unsigned char output[16]) |
| 353 | +{ |
| 354 | + mbedtls_internal_aes_encrypt(ctx, input, output); |
| 355 | +} |
| 356 | + |
| 357 | +void mbedtls_aes_decrypt(mbedtls_aes_context *ctx, |
| 358 | + const unsigned char input[16], |
| 359 | + unsigned char output[16]) |
| 360 | +{ |
| 361 | + mbedtls_internal_aes_decrypt(ctx, input, output); |
| 362 | +} |
| 363 | +#endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 364 | +#endif /*MBEDTLS_AES_ALT*/ |
0 commit comments