1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (c) 2014-2019, Linaro Limited
4 */
5
6 #include <assert.h>
7 #include <crypto/crypto.h>
8 #include <crypto/crypto_impl.h>
9 #include <stdlib.h>
10 #include <tee_api_types.h>
11 #include <tomcrypt_private.h>
12 #include <util.h>
13
14 struct ltc_ctr_ctx {
15 struct crypto_cipher_ctx ctx;
16 int cipher_idx;
17 int (*update)(const unsigned char *src, unsigned char *dst,
18 unsigned long len, symmetric_CTR *ctr);
19 symmetric_CTR state;
20 };
21
22 static const struct crypto_cipher_ops ltc_ctr_ops;
23
to_ctr_ctx(struct crypto_cipher_ctx * ctx)24 static struct ltc_ctr_ctx *to_ctr_ctx(struct crypto_cipher_ctx *ctx)
25 {
26 assert(ctx && ctx->ops == <c_ctr_ops);
27
28 return container_of(ctx, struct ltc_ctr_ctx, ctx);
29 }
30
ltc_ctr_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)31 static TEE_Result ltc_ctr_init(struct crypto_cipher_ctx *ctx,
32 TEE_OperationMode mode, const uint8_t *key1,
33 size_t key1_len, const uint8_t *key2 __unused,
34 size_t key2_len __unused,
35 const uint8_t *iv __unused,
36 size_t iv_len __unused)
37 {
38 struct ltc_ctr_ctx *c = to_ctr_ctx(ctx);
39
40 if ((int)iv_len != cipher_descriptor[c->cipher_idx]->block_length)
41 return TEE_ERROR_BAD_PARAMETERS;
42
43 if (mode == TEE_MODE_ENCRYPT)
44 c->update = ctr_encrypt;
45 else
46 c->update = ctr_decrypt;
47
48 if (ctr_start(c->cipher_idx, iv, key1, key1_len, 0,
49 CTR_COUNTER_BIG_ENDIAN, &c->state) == CRYPT_OK)
50 return TEE_SUCCESS;
51 else
52 return TEE_ERROR_BAD_STATE;
53 }
54
ltc_ctr_update(struct crypto_cipher_ctx * ctx,bool last_block __unused,const uint8_t * data,size_t len,uint8_t * dst)55 static TEE_Result ltc_ctr_update(struct crypto_cipher_ctx *ctx,
56 bool last_block __unused,
57 const uint8_t *data, size_t len, uint8_t *dst)
58 {
59 struct ltc_ctr_ctx *c = to_ctr_ctx(ctx);
60
61 if (c->update && c->update(data, dst, len, &c->state) == CRYPT_OK)
62 return TEE_SUCCESS;
63 else
64 return TEE_ERROR_BAD_STATE;
65 }
66
ltc_ctr_final(struct crypto_cipher_ctx * ctx)67 static void ltc_ctr_final(struct crypto_cipher_ctx *ctx)
68 {
69 ctr_done(&to_ctr_ctx(ctx)->state);
70 }
71
ltc_ctr_free_ctx(struct crypto_cipher_ctx * ctx)72 static void ltc_ctr_free_ctx(struct crypto_cipher_ctx *ctx)
73 {
74 free(to_ctr_ctx(ctx));
75 }
76
ltc_ctr_copy_state(struct crypto_cipher_ctx * dst_ctx,struct crypto_cipher_ctx * src_ctx)77 static void ltc_ctr_copy_state(struct crypto_cipher_ctx *dst_ctx,
78 struct crypto_cipher_ctx *src_ctx)
79 {
80 struct ltc_ctr_ctx *src = to_ctr_ctx(src_ctx);
81 struct ltc_ctr_ctx *dst = to_ctr_ctx(dst_ctx);
82
83 assert(src->cipher_idx == dst->cipher_idx);
84 dst->update = src->update;
85 dst->state = src->state;
86 }
87
88 static const struct crypto_cipher_ops ltc_ctr_ops = {
89 .init = ltc_ctr_init,
90 .update = ltc_ctr_update,
91 .final = ltc_ctr_final,
92 .free_ctx = ltc_ctr_free_ctx,
93 .copy_state = ltc_ctr_copy_state,
94 };
95
crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx ** ctx_ret)96 TEE_Result crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx_ret)
97 {
98 struct ltc_ctr_ctx *c = NULL;
99 int cipher_idx = find_cipher("aes");
100
101 if (cipher_idx < 0)
102 return TEE_ERROR_NOT_SUPPORTED;
103
104 c = calloc(1, sizeof(*c));
105 if (!c)
106 return TEE_ERROR_OUT_OF_MEMORY;
107
108 c->ctx.ops = <c_ctr_ops;
109 c->cipher_idx = cipher_idx;
110 *ctx_ret = &c->ctx;
111
112 return TEE_SUCCESS;
113 }
114