1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (C) 2018, ARM Limited 4 * Copyright (C) 2019, Linaro Limited 5 */ 6 7 #include <assert.h> 8 #include <compiler.h> 9 #include <crypto/crypto_accel.h> 10 #include <crypto/crypto.h> 11 #include <kernel/panic.h> 12 #include <mbedtls/aes.h> 13 #include <mbedtls/platform_util.h> 14 #include <string.h> 15 16 #if defined(MBEDTLS_AES_ALT) 17 void mbedtls_aes_init(mbedtls_aes_context *ctx) 18 { 19 assert(ctx); 20 memset(ctx, 0, sizeof(*ctx)); 21 } 22 23 void mbedtls_aes_free( mbedtls_aes_context *ctx ) 24 { 25 if (ctx) 26 mbedtls_platform_zeroize(ctx, sizeof(*ctx)); 27 } 28 29 int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, 30 unsigned int keybits) 31 { 32 assert(ctx && key); 33 34 if (keybits != 128 && keybits != 192 && keybits != 256) 35 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; 36 37 if (crypto_accel_aes_expand_keys(key, keybits / 8, ctx->key, NULL, 38 sizeof(ctx->key), &ctx->round_count)) 39 return MBEDTLS_ERR_AES_BAD_INPUT_DATA; 40 41 return 0; 42 } 43 44 int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, 45 unsigned int keybits) 46 { 47 uint32_t enc_key[sizeof(ctx->key)] = { 0 }; 48 49 assert(ctx && key); 50 51 if (keybits != 128 && keybits != 192 && keybits != 256) 52 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; 53 54 if (crypto_accel_aes_expand_keys(key, keybits / 8, enc_key, ctx->key, 55 sizeof(ctx->key), &ctx->round_count)) 56 return MBEDTLS_ERR_AES_BAD_INPUT_DATA; 57 58 return 0; 59 } 60 #endif /*MBEDTLS_AES_ALT*/ 61