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/des.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_des3_cbc_ctx {
18 	struct crypto_cipher_ctx ctx;
19 	int mbed_mode;
20 	mbedtls_des3_context des3_ctx;
21 	unsigned char iv[TEE_DES_BLOCK_SIZE];
22 };
23 
24 static const struct crypto_cipher_ops mbed_des3_cbc_ops;
25 
to_des3_cbc_ctx(struct crypto_cipher_ctx * ctx)26 static struct mbed_des3_cbc_ctx *to_des3_cbc_ctx(struct crypto_cipher_ctx *ctx)
27 {
28 	assert(ctx && ctx->ops == &mbed_des3_cbc_ops);
29 
30 	return container_of(ctx, struct mbed_des3_cbc_ctx, ctx);
31 }
32 
mbed_des3_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 __unused,size_t iv_len __unused)33 static TEE_Result mbed_des3_cbc_init(struct crypto_cipher_ctx *ctx,
34 				     TEE_OperationMode mode,
35 				     const uint8_t *key1, size_t key1_len,
36 				     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 mbed_des3_cbc_ctx *c = to_des3_cbc_ctx(ctx);
42 	int mbed_res = 0;
43 
44 	if (key1_len != MBEDTLS_DES_KEY_SIZE * 2 &&
45 	    key1_len != MBEDTLS_DES_KEY_SIZE * 3)
46 		return TEE_ERROR_BAD_PARAMETERS;
47 	if (iv_len != sizeof(c->iv))
48 		return TEE_ERROR_BAD_PARAMETERS;
49 	memcpy(c->iv, iv, sizeof(c->iv));
50 
51 	mbedtls_des3_init(&c->des3_ctx);
52 
53 	if (mode == TEE_MODE_ENCRYPT) {
54 		c->mbed_mode = MBEDTLS_DES_ENCRYPT;
55 		if (key1_len == MBEDTLS_DES_KEY_SIZE * 3)
56 			mbed_res = mbedtls_des3_set3key_enc(&c->des3_ctx, key1);
57 		else
58 			mbed_res = mbedtls_des3_set2key_enc(&c->des3_ctx, key1);
59 	} else {
60 		c->mbed_mode = MBEDTLS_DES_DECRYPT;
61 		if (key1_len == MBEDTLS_DES_KEY_SIZE * 3)
62 			mbed_res = mbedtls_des3_set3key_dec(&c->des3_ctx, key1);
63 		else
64 			mbed_res = mbedtls_des3_set2key_dec(&c->des3_ctx, key1);
65 	}
66 
67 	if (mbed_res)
68 		return TEE_ERROR_BAD_STATE;
69 
70 	return TEE_SUCCESS;
71 }
72 
mbed_des3_cbc_update(struct crypto_cipher_ctx * ctx,bool last_block __unused,const uint8_t * data,size_t len,uint8_t * dst)73 static TEE_Result mbed_des3_cbc_update(struct crypto_cipher_ctx *ctx,
74 				       bool last_block __unused,
75 				       const uint8_t *data, size_t len,
76 				       uint8_t *dst)
77 {
78 	struct mbed_des3_cbc_ctx *c = to_des3_cbc_ctx(ctx);
79 
80 	if (mbedtls_des3_crypt_cbc(&c->des3_ctx, c->mbed_mode, len, c->iv,
81 				   data, dst))
82 		return TEE_ERROR_BAD_STATE;
83 
84 	return TEE_SUCCESS;
85 }
86 
mbed_des3_cbc_final(struct crypto_cipher_ctx * ctx)87 static void mbed_des3_cbc_final(struct crypto_cipher_ctx *ctx)
88 {
89 	mbedtls_des3_free(&to_des3_cbc_ctx(ctx)->des3_ctx);
90 }
91 
mbed_des3_cbc_free_ctx(struct crypto_cipher_ctx * ctx)92 static void mbed_des3_cbc_free_ctx(struct crypto_cipher_ctx *ctx)
93 {
94 	free(to_des3_cbc_ctx(ctx));
95 }
96 
mbed_des3_cbc_copy_state(struct crypto_cipher_ctx * dst_ctx,struct crypto_cipher_ctx * src_ctx)97 static void mbed_des3_cbc_copy_state(struct crypto_cipher_ctx *dst_ctx,
98 				     struct crypto_cipher_ctx *src_ctx)
99 {
100 	struct mbed_des3_cbc_ctx *src = to_des3_cbc_ctx(src_ctx);
101 	struct mbed_des3_cbc_ctx *dst = to_des3_cbc_ctx(dst_ctx);
102 
103 	memcpy(dst->iv, src->iv, sizeof(dst->iv));
104 	dst->mbed_mode = src->mbed_mode;
105 	dst->des3_ctx = src->des3_ctx;
106 }
107 
108 static const struct crypto_cipher_ops mbed_des3_cbc_ops = {
109 	.init = mbed_des3_cbc_init,
110 	.update = mbed_des3_cbc_update,
111 	.final = mbed_des3_cbc_final,
112 	.free_ctx = mbed_des3_cbc_free_ctx,
113 	.copy_state = mbed_des3_cbc_copy_state,
114 };
115 
crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx ** ctx_ret)116 TEE_Result crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx_ret)
117 {
118 	struct mbed_des3_cbc_ctx *c = NULL;
119 
120 	c = calloc(1, sizeof(*c));
121 	if (!c)
122 		return TEE_ERROR_OUT_OF_MEMORY;
123 
124 	c->ctx.ops = &mbed_des3_cbc_ops;
125 	*ctx_ret = &c->ctx;
126 
127 	return TEE_SUCCESS;
128 }
129