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