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_ecb_ctx {
18 	struct crypto_cipher_ctx ctx;
19 	mbedtls_des3_context des3_ctx;
20 };
21 
22 static const struct crypto_cipher_ops mbed_des3_ecb_ops;
23 
to_des3_ecb_ctx(struct crypto_cipher_ctx * ctx)24 static struct mbed_des3_ecb_ctx *to_des3_ecb_ctx(struct crypto_cipher_ctx *ctx)
25 {
26 	assert(ctx && ctx->ops == &mbed_des3_ecb_ops);
27 
28 	return container_of(ctx, struct mbed_des3_ecb_ctx, ctx);
29 }
30 
mbed_des3_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)31 static TEE_Result mbed_des3_ecb_init(struct crypto_cipher_ctx *ctx,
32 				     TEE_OperationMode mode,
33 				     const uint8_t *key1, size_t key1_len,
34 				     const uint8_t *key2 __unused,
35 				     size_t key2_len __unused,
36 				     const uint8_t *iv __unused,
37 				     size_t iv_len  __unused)
38 {
39 	struct mbed_des3_ecb_ctx *c = to_des3_ecb_ctx(ctx);
40 	int mbed_res = 0;
41 
42 	if (key1_len != MBEDTLS_DES_KEY_SIZE * 2 &&
43 	    key1_len != MBEDTLS_DES_KEY_SIZE * 3)
44 		return TEE_ERROR_BAD_PARAMETERS;
45 
46 	mbedtls_des3_init(&c->des3_ctx);
47 
48 	if (key1_len == MBEDTLS_DES_KEY_SIZE * 3) {
49 		if (mode == TEE_MODE_ENCRYPT)
50 			mbed_res = mbedtls_des3_set3key_enc(&c->des3_ctx, key1);
51 		else
52 			mbed_res = mbedtls_des3_set3key_dec(&c->des3_ctx, key1);
53 	} else {
54 		if (mode == TEE_MODE_ENCRYPT)
55 			mbed_res = mbedtls_des3_set2key_enc(&c->des3_ctx, key1);
56 		else
57 			mbed_res = mbedtls_des3_set2key_dec(&c->des3_ctx, key1);
58 	}
59 
60 	if (mbed_res)
61 		return TEE_ERROR_BAD_STATE;
62 
63 	return TEE_SUCCESS;
64 }
65 
mbed_des3_ecb_update(struct crypto_cipher_ctx * ctx,bool last_block __unused,const uint8_t * data,size_t len,uint8_t * dst)66 static TEE_Result mbed_des3_ecb_update(struct crypto_cipher_ctx *ctx,
67 				       bool last_block __unused,
68 				       const uint8_t *data, size_t len,
69 				       uint8_t *dst)
70 {
71 	struct mbed_des3_ecb_ctx *c = to_des3_ecb_ctx(ctx);
72 	size_t block_size = TEE_DES_BLOCK_SIZE;
73 	size_t offs = 0;
74 
75 	if (len % block_size)
76 		return TEE_ERROR_BAD_PARAMETERS;
77 
78 	for (offs = 0; offs < len; offs += block_size) {
79 		if (mbedtls_des3_crypt_ecb(&c->des3_ctx, data + offs,
80 					   dst + offs))
81 			return TEE_ERROR_BAD_STATE;
82 	}
83 
84 	return TEE_SUCCESS;
85 }
86 
mbed_des3_ecb_final(struct crypto_cipher_ctx * ctx)87 static void mbed_des3_ecb_final(struct crypto_cipher_ctx *ctx)
88 {
89 	mbedtls_des3_free(&to_des3_ecb_ctx(ctx)->des3_ctx);
90 }
91 
mbed_des3_ecb_free_ctx(struct crypto_cipher_ctx * ctx)92 static void mbed_des3_ecb_free_ctx(struct crypto_cipher_ctx *ctx)
93 {
94 	free(to_des3_ecb_ctx(ctx));
95 }
96 
mbed_des3_ecb_copy_state(struct crypto_cipher_ctx * dst_ctx,struct crypto_cipher_ctx * src_ctx)97 static void mbed_des3_ecb_copy_state(struct crypto_cipher_ctx *dst_ctx,
98 				     struct crypto_cipher_ctx *src_ctx)
99 {
100 	struct mbed_des3_ecb_ctx *src = to_des3_ecb_ctx(src_ctx);
101 	struct mbed_des3_ecb_ctx *dst = to_des3_ecb_ctx(dst_ctx);
102 
103 	dst->des3_ctx = src->des3_ctx;
104 }
105 
106 static const struct crypto_cipher_ops mbed_des3_ecb_ops = {
107 	.init = mbed_des3_ecb_init,
108 	.update = mbed_des3_ecb_update,
109 	.final = mbed_des3_ecb_final,
110 	.free_ctx = mbed_des3_ecb_free_ctx,
111 	.copy_state = mbed_des3_ecb_copy_state,
112 };
113 
crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx ** ctx_ret)114 TEE_Result crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx_ret)
115 {
116 	struct mbed_des3_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_des3_ecb_ops;
123 	*ctx_ret = &c->ctx;
124 
125 	return TEE_SUCCESS;
126 }
127