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 #include "des2_key.h"
15
16 struct ltc_ecb_ctx {
17 struct crypto_cipher_ctx ctx;
18 int cipher_idx;
19 bool des3;
20 int (*update)(const unsigned char *src, unsigned char *dst,
21 unsigned long len, symmetric_ECB *ecb);
22 symmetric_ECB state;
23 };
24
25 static const struct crypto_cipher_ops ltc_ecb_ops;
26
to_ecb_ctx(struct crypto_cipher_ctx * ctx)27 static struct ltc_ecb_ctx *to_ecb_ctx(struct crypto_cipher_ctx *ctx)
28 {
29 assert(ctx && ctx->ops == <c_ecb_ops);
30
31 return container_of(ctx, struct ltc_ecb_ctx, ctx);
32 }
33
ltc_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)34 static TEE_Result ltc_ecb_init(struct crypto_cipher_ctx *ctx,
35 TEE_OperationMode mode, const uint8_t *key1,
36 size_t key1_len, const uint8_t *key2 __unused,
37 size_t key2_len __unused,
38 const uint8_t *iv __unused,
39 size_t iv_len __unused)
40 {
41 struct ltc_ecb_ctx *c = to_ecb_ctx(ctx);
42 uint8_t tmp[24] = { 0 };
43 const uint8_t *k = key1;
44 size_t kl = key1_len;
45
46 if (mode == TEE_MODE_ENCRYPT)
47 c->update = ecb_encrypt;
48 else
49 c->update = ecb_decrypt;
50
51 if (c->des3)
52 get_des2_key(&k, &kl, tmp);
53
54 if (ecb_start(c->cipher_idx, k, kl, 0, &c->state) == CRYPT_OK)
55 return TEE_SUCCESS;
56 else
57 return TEE_ERROR_BAD_STATE;
58 }
59
ltc_ecb_update(struct crypto_cipher_ctx * ctx,bool last_block __unused,const uint8_t * data,size_t len,uint8_t * dst)60 static TEE_Result ltc_ecb_update(struct crypto_cipher_ctx *ctx,
61 bool last_block __unused,
62 const uint8_t *data, size_t len, uint8_t *dst)
63 {
64 struct ltc_ecb_ctx *c = to_ecb_ctx(ctx);
65
66 if (c->update && c->update(data, dst, len, &c->state) == CRYPT_OK)
67 return TEE_SUCCESS;
68 else
69 return TEE_ERROR_BAD_STATE;
70 }
71
ltc_ecb_final(struct crypto_cipher_ctx * ctx)72 static void ltc_ecb_final(struct crypto_cipher_ctx *ctx)
73 {
74 ecb_done(&to_ecb_ctx(ctx)->state);
75 }
76
ltc_ecb_free_ctx(struct crypto_cipher_ctx * ctx)77 static void ltc_ecb_free_ctx(struct crypto_cipher_ctx *ctx)
78 {
79 free(to_ecb_ctx(ctx));
80 }
81
ltc_ecb_copy_state(struct crypto_cipher_ctx * dst_ctx,struct crypto_cipher_ctx * src_ctx)82 static void ltc_ecb_copy_state(struct crypto_cipher_ctx *dst_ctx,
83 struct crypto_cipher_ctx *src_ctx)
84 {
85 struct ltc_ecb_ctx *src = to_ecb_ctx(src_ctx);
86 struct ltc_ecb_ctx *dst = to_ecb_ctx(dst_ctx);
87
88 assert(src->cipher_idx == dst->cipher_idx);
89 dst->update = src->update;
90 dst->state = src->state;
91 }
92
93 static const struct crypto_cipher_ops ltc_ecb_ops = {
94 .init = ltc_ecb_init,
95 .update = ltc_ecb_update,
96 .final = ltc_ecb_final,
97 .free_ctx = ltc_ecb_free_ctx,
98 .copy_state = ltc_ecb_copy_state,
99 };
100
ltc_ecb_alloc_ctx(struct crypto_cipher_ctx ** ctx_ret,int cipher_idx,bool des3)101 static TEE_Result ltc_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx_ret,
102 int cipher_idx, bool des3)
103 {
104 struct ltc_ecb_ctx *c = NULL;
105
106 if (cipher_idx < 0)
107 return TEE_ERROR_NOT_SUPPORTED;
108
109 c = calloc(1, sizeof(*c));
110 if (!c)
111 return TEE_ERROR_OUT_OF_MEMORY;
112
113 c->ctx.ops = <c_ecb_ops;
114 c->cipher_idx = cipher_idx;
115 c->des3 = des3;
116 *ctx_ret = &c->ctx;
117
118 return TEE_SUCCESS;
119 }
120
121 #if defined(_CFG_CORE_LTC_AES)
crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx ** ctx)122 TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx)
123 {
124 return ltc_ecb_alloc_ctx(ctx, find_cipher("aes"), false);
125 }
126 #endif
127
128 #if defined(_CFG_CORE_LTC_DES)
crypto_des_ecb_alloc_ctx(struct crypto_cipher_ctx ** ctx)129 TEE_Result crypto_des_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx)
130 {
131 return ltc_ecb_alloc_ctx(ctx, find_cipher("des"), false);
132 }
133
crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx ** ctx)134 TEE_Result crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx)
135 {
136 return ltc_ecb_alloc_ctx(ctx, find_cipher("3des"), true);
137 }
138 #endif
139