xref: /optee_os/lib/libmbedtls/core/aes_ecb.c (revision b99a4a1850c2ce661156ebc25f48d47efa8a41c1)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2019, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <compiler.h>
8 #include <crypto/crypto.h>
9 #include <crypto/crypto_impl.h>
10 #include <mbedtls/aes.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <tee_api_types.h>
14 #include <utee_defines.h>
15 #include <util.h>
16 
17 struct mbed_aes_ecb_ctx {
18 	struct crypto_cipher_ctx ctx;
19 	int mbed_mode;
20 	mbedtls_aes_context aes_ctx;
21 };
22 
23 static const struct crypto_cipher_ops mbed_aes_ecb_ops;
24 
25 static struct mbed_aes_ecb_ctx *to_aes_ecb_ctx(struct crypto_cipher_ctx *ctx)
26 {
27 	assert(ctx && ctx->ops == &mbed_aes_ecb_ops);
28 
29 	return container_of(ctx, struct mbed_aes_ecb_ctx, ctx);
30 }
31 
32 static TEE_Result mbed_aes_ecb_init(struct crypto_cipher_ctx *ctx,
33 				    TEE_OperationMode mode, const uint8_t *key1,
34 				    size_t key1_len,
35 				    const uint8_t *key2 __unused,
36 				    size_t key2_len __unused,
37 				    const uint8_t *iv __unused,
38 				    size_t iv_len  __unused)
39 {
40 	struct mbed_aes_ecb_ctx *c = to_aes_ecb_ctx(ctx);
41 	int mbed_res = 0;
42 
43 	mbedtls_aes_init(&c->aes_ctx);
44 
45 	if (mode == TEE_MODE_ENCRYPT) {
46 		c->mbed_mode = MBEDTLS_AES_ENCRYPT;
47 		mbed_res = mbedtls_aes_setkey_enc(&c->aes_ctx, key1,
48 						  key1_len * 8);
49 	} else {
50 		c->mbed_mode = MBEDTLS_AES_DECRYPT;
51 		mbed_res = mbedtls_aes_setkey_dec(&c->aes_ctx, key1,
52 						  key1_len * 8);
53 	}
54 
55 	if (mbed_res)
56 		return TEE_ERROR_BAD_STATE;
57 
58 	return TEE_SUCCESS;
59 }
60 
61 static TEE_Result mbed_aes_ecb_update(struct crypto_cipher_ctx *ctx,
62 				      bool last_block __unused,
63 				      const uint8_t *data, size_t len,
64 				      uint8_t *dst)
65 {
66 	struct mbed_aes_ecb_ctx *c = to_aes_ecb_ctx(ctx);
67 	size_t block_size = TEE_AES_BLOCK_SIZE;
68 	size_t offs = 0;
69 
70 	if (len % block_size)
71 		return TEE_ERROR_BAD_PARAMETERS;
72 
73 	for (offs = 0; offs < len; offs += block_size) {
74 		if (mbedtls_aes_crypt_ecb(&c->aes_ctx, c->mbed_mode,
75 					  data + offs, dst + offs))
76 			return TEE_ERROR_BAD_STATE;
77 	}
78 
79 	return TEE_SUCCESS;
80 }
81 
82 static void mbed_aes_ecb_final(struct crypto_cipher_ctx *ctx)
83 {
84 	mbedtls_aes_free(&to_aes_ecb_ctx(ctx)->aes_ctx);
85 }
86 
87 static void mbed_aes_ecb_free_ctx(struct crypto_cipher_ctx *ctx)
88 {
89 	free(to_aes_ecb_ctx(ctx));
90 }
91 
92 static void mbed_aes_ecb_copy_state(struct crypto_cipher_ctx *dst_ctx,
93 				    struct crypto_cipher_ctx *src_ctx)
94 {
95 	struct mbed_aes_ecb_ctx *src = to_aes_ecb_ctx(src_ctx);
96 	struct mbed_aes_ecb_ctx *dst = to_aes_ecb_ctx(dst_ctx);
97 
98 	dst->mbed_mode = src->mbed_mode;
99 	dst->aes_ctx = src->aes_ctx;
100 }
101 
102 static const struct crypto_cipher_ops mbed_aes_ecb_ops = {
103 	.init = mbed_aes_ecb_init,
104 	.update = mbed_aes_ecb_update,
105 	.final = mbed_aes_ecb_final,
106 	.free_ctx = mbed_aes_ecb_free_ctx,
107 	.copy_state = mbed_aes_ecb_copy_state,
108 };
109 
110 TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx_ret)
111 {
112 	struct mbed_aes_ecb_ctx *c = NULL;
113 
114 	c = calloc(1, sizeof(*c));
115 	if (!c)
116 		return TEE_ERROR_OUT_OF_MEMORY;
117 
118 	c->ctx.ops = &mbed_aes_ecb_ops;
119 	*ctx_ret = &c->ctx;
120 
121 	return TEE_SUCCESS;
122 }
123