1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014-2019, Linaro Limited
4  */
5 
6 #include <crypto/crypto_accel.h>
7 #include <crypto/crypto.h>
8 #include <kernel/panic.h>
9 #include <tee_api_defines.h>
10 #include <tee_api_types.h>
11 #include <tomcrypt_private.h>
12 
crypto_aes_expand_enc_key(const void * key,size_t key_len,void * enc_key,size_t enc_keylen,unsigned int * rounds)13 TEE_Result crypto_aes_expand_enc_key(const void *key, size_t key_len,
14 				     void *enc_key, size_t enc_keylen,
15 				     unsigned int *rounds)
16 {
17 #ifdef _CFG_CORE_LTC_AES_ACCEL
18 	return crypto_accel_aes_expand_keys(key, key_len, enc_key, NULL,
19 					    enc_keylen, rounds);
20 #else
21 	symmetric_key skey;
22 
23 	if (enc_keylen < sizeof(skey.rijndael.eK))
24 		return TEE_ERROR_BAD_PARAMETERS;
25 
26 	if (aes_setup(key, key_len, 0, &skey))
27 		return TEE_ERROR_BAD_PARAMETERS;
28 
29 	memcpy(enc_key, skey.rijndael.eK, sizeof(skey.rijndael.eK));
30 	*rounds = skey.rijndael.Nr;
31 #endif
32 	return TEE_SUCCESS;
33 }
34 
crypto_aes_enc_block(const void * enc_key,size_t enc_keylen __maybe_unused,unsigned int rounds,const void * src,void * dst)35 void crypto_aes_enc_block(const void *enc_key, size_t enc_keylen __maybe_unused,
36 			  unsigned int rounds, const void *src, void *dst)
37 {
38 #ifdef _CFG_CORE_LTC_AES_ACCEL
39 	crypto_accel_aes_ecb_enc(dst, src, enc_key, rounds, 1);
40 #else
41 	symmetric_key skey;
42 
43 	assert(enc_keylen >= sizeof(skey.rijndael.eK));
44 	memcpy(skey.rijndael.eK, enc_key, sizeof(skey.rijndael.eK));
45 	skey.rijndael.Nr = rounds;
46 	if (aes_ecb_encrypt(src, dst, &skey))
47 		panic();
48 #endif
49 }
50