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_cbc_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_CBC *cbc);
22 	symmetric_CBC state;
23 };
24 
25 static const struct crypto_cipher_ops ltc_cbc_ops;
26 
to_cbc_ctx(struct crypto_cipher_ctx * ctx)27 static struct ltc_cbc_ctx *to_cbc_ctx(struct crypto_cipher_ctx *ctx)
28 {
29 	assert(ctx && ctx->ops == &ltc_cbc_ops);
30 
31 	return container_of(ctx, struct ltc_cbc_ctx, ctx);
32 }
33 
ltc_cbc_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,size_t iv_len)34 static TEE_Result ltc_cbc_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, size_t iv_len)
39 {
40 	struct ltc_cbc_ctx *c = to_cbc_ctx(ctx);
41 	uint8_t tmp[24] = { 0 };
42 	const uint8_t *k = key1;
43 	size_t kl = key1_len;
44 
45 	if ((int)iv_len != cipher_descriptor[c->cipher_idx]->block_length)
46 		return TEE_ERROR_BAD_PARAMETERS;
47 
48 	if (mode == TEE_MODE_ENCRYPT)
49 		c->update = cbc_encrypt;
50 	else
51 		c->update = cbc_decrypt;
52 
53 	if (c->des3)
54 		get_des2_key(&k, &kl, tmp);
55 
56 	if (cbc_start(c->cipher_idx, iv, k, kl, 0, &c->state) == CRYPT_OK)
57 		return TEE_SUCCESS;
58 	else
59 		return TEE_ERROR_BAD_STATE;
60 }
61 
ltc_cbc_update(struct crypto_cipher_ctx * ctx,bool last_block __unused,const uint8_t * data,size_t len,uint8_t * dst)62 static TEE_Result ltc_cbc_update(struct crypto_cipher_ctx *ctx,
63 				 bool last_block __unused,
64 				 const uint8_t *data, size_t len, uint8_t *dst)
65 {
66 	struct ltc_cbc_ctx *c = to_cbc_ctx(ctx);
67 
68 	if (c->update && c->update(data, dst, len, &c->state) == CRYPT_OK)
69 		return TEE_SUCCESS;
70 	else
71 		return TEE_ERROR_BAD_STATE;
72 }
73 
ltc_cbc_final(struct crypto_cipher_ctx * ctx)74 static void ltc_cbc_final(struct crypto_cipher_ctx *ctx)
75 {
76 	cbc_done(&to_cbc_ctx(ctx)->state);
77 }
78 
ltc_cbc_free_ctx(struct crypto_cipher_ctx * ctx)79 static void ltc_cbc_free_ctx(struct crypto_cipher_ctx *ctx)
80 {
81 	free(to_cbc_ctx(ctx));
82 }
83 
ltc_cbc_copy_state(struct crypto_cipher_ctx * dst_ctx,struct crypto_cipher_ctx * src_ctx)84 static void ltc_cbc_copy_state(struct crypto_cipher_ctx *dst_ctx,
85 			       struct crypto_cipher_ctx *src_ctx)
86 {
87 	struct ltc_cbc_ctx *src = to_cbc_ctx(src_ctx);
88 	struct ltc_cbc_ctx *dst = to_cbc_ctx(dst_ctx);
89 
90 	assert(src->cipher_idx == dst->cipher_idx);
91 	dst->update = src->update;
92 	dst->state = src->state;
93 }
94 
95 static const struct crypto_cipher_ops ltc_cbc_ops = {
96 	.init = ltc_cbc_init,
97 	.update = ltc_cbc_update,
98 	.final = ltc_cbc_final,
99 	.free_ctx = ltc_cbc_free_ctx,
100 	.copy_state = ltc_cbc_copy_state,
101 };
102 
ltc_cbc_alloc_ctx(struct crypto_cipher_ctx ** ctx_ret,int cipher_idx,bool des3)103 static TEE_Result ltc_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx_ret,
104 				    int cipher_idx, bool des3)
105 {
106 	struct ltc_cbc_ctx *c = NULL;
107 
108 	if (cipher_idx < 0)
109 		return TEE_ERROR_NOT_SUPPORTED;
110 
111 	c = calloc(1, sizeof(*c));
112 	if (!c)
113 		return TEE_ERROR_OUT_OF_MEMORY;
114 
115 	c->ctx.ops = &ltc_cbc_ops;
116 	c->cipher_idx = cipher_idx;
117 	c->des3 = des3;
118 	*ctx_ret = &c->ctx;
119 
120 	return TEE_SUCCESS;
121 }
122 
123 #if defined(_CFG_CORE_LTC_AES)
crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx ** ctx)124 TEE_Result crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx)
125 {
126 	return ltc_cbc_alloc_ctx(ctx, find_cipher("aes"), false);
127 }
128 #endif
129 
130 #if defined(_CFG_CORE_LTC_DES)
crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx ** ctx)131 TEE_Result crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx)
132 {
133 	return ltc_cbc_alloc_ctx(ctx, find_cipher("des"), false);
134 }
135 
crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx ** ctx)136 TEE_Result crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx)
137 {
138 	return ltc_cbc_alloc_ctx(ctx, find_cipher("3des"), true);
139 }
140 #endif
141