xref: /optee_os/lib/libmbedtls/core/aes.c (revision 08caee9b57e779513b762a7aef25d3119a577f88)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2018, ARM Limited
4  * Copyright (C) 2019, Linaro Limited
5  */
6 
7 #include <crypto/crypto.h>
8 #include <kernel/panic.h>
9 #include <mbedtls/aes.h>
10 #include <string.h>
11 
12 TEE_Result crypto_aes_expand_enc_key(const void *key, size_t key_len,
13 				     void *enc_key, size_t enc_keylen,
14 				     unsigned int *rounds)
15 {
16 	mbedtls_aes_context ctx;
17 
18 	memset(&ctx, 0, sizeof(ctx));
19 	mbedtls_aes_init(&ctx);
20 	if (mbedtls_aes_setkey_enc(&ctx, key, key_len * 8) != 0)
21 		return TEE_ERROR_BAD_PARAMETERS;
22 
23 	if (enc_keylen > sizeof(ctx.buf))
24 		return TEE_ERROR_BAD_PARAMETERS;
25 	memcpy(enc_key, ctx.buf, enc_keylen);
26 	*rounds = ctx.nr;
27 	mbedtls_aes_free(&ctx);
28 	return TEE_SUCCESS;
29 }
30 
31 void crypto_aes_enc_block(const void *enc_key, size_t enc_keylen,
32 			  unsigned int rounds, const void *src, void *dst)
33 {
34 	mbedtls_aes_context ctx;
35 
36 	memset(&ctx, 0, sizeof(ctx));
37 	mbedtls_aes_init(&ctx);
38 	if (enc_keylen > sizeof(ctx.buf))
39 		panic();
40 	memcpy(ctx.buf, enc_key, enc_keylen);
41 	ctx.rk = ctx.buf;
42 	ctx.nr = rounds;
43 	mbedtls_aes_encrypt(&ctx, src, dst);
44 	mbedtls_aes_free(&ctx);
45 }
46