aboutsummaryrefslogtreecommitdiff
path: root/core/include/crypto/crypto_impl.h
blob: 1ae9e7a3373e50ba2af4a946866bfc6ba958a75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (c) 2019, Linaro Limited
 */

#ifndef __CRYPTO_CRYPTO_IMPL_H
#define __CRYPTO_CRYPTO_IMPL_H

#include <tee_api_types.h>

/*
 * The crypto context used by the crypto_hash_*() functions is defined by
 * struct crypto_hash_ctx.
 */
struct crypto_hash_ctx {
	const struct crypto_hash_ops *ops;
};

struct crypto_hash_ops {
	TEE_Result (*init)(struct crypto_hash_ctx *ctx);
	TEE_Result (*update)(struct crypto_hash_ctx *ctx, const uint8_t *data,
			     size_t len);
	TEE_Result (*final)(struct crypto_hash_ctx *ctx, uint8_t *digest,
			    size_t len);
	void (*free_ctx)(struct crypto_hash_ctx *ctx);
	void (*copy_state)(struct crypto_hash_ctx *dst_ctx,
			   struct crypto_hash_ctx *src_ctx);
};

#define CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(name, type) \
	static inline TEE_Result \
	crypto_##name##_alloc_ctx(struct crypto_##type##_ctx **ctx __unused) \
	{ return TEE_ERROR_NOT_IMPLEMENTED; }

#if defined(CFG_CRYPTO_MD5)
TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(md5, hash)
#endif

#if defined(CFG_CRYPTO_SHA1)
TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha1, hash)
#endif

#if defined(CFG_CRYPTO_SHA224)
TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha224, hash)
#endif

#if defined(CFG_CRYPTO_SHA256)
TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha256, hash)
#endif

#if defined(CFG_CRYPTO_SHA384)
TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha384, hash)
#endif

#if defined(CFG_CRYPTO_SHA512)
TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha512, hash)
#endif

/*
 * The crypto context used by the crypto_mac_*() functions is defined by
 * struct crypto_mac_ctx.
 */
struct crypto_mac_ctx {
	const struct crypto_mac_ops *ops;
};

struct crypto_mac_ops {
	TEE_Result (*init)(struct crypto_mac_ctx *ctx, const uint8_t *key,
			   size_t len);
	TEE_Result (*update)(struct crypto_mac_ctx *ctx, const uint8_t *data,
			     size_t len);
	TEE_Result (*final)(struct crypto_mac_ctx *ctx, uint8_t *digest,
			    size_t len);
	void (*free_ctx)(struct crypto_mac_ctx *ctx);
	void (*copy_state)(struct crypto_mac_ctx *dst_ctx,
			   struct crypto_mac_ctx *src_ctx);
};

#if defined(CFG_CRYPTO_HMAC)
TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_md5, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha1, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha224, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha256, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha384, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha512, mac)
#endif

#if defined(CFG_CRYPTO_CBC_MAC)
TEE_Result crypto_aes_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_aes_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_des_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_des_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_des3_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx);
TEE_Result crypto_des3_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_nopad, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_pkcs5, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_nopad, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_pkcs5, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_nopad, mac)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_pkcs5, mac)
#endif

#if defined(CFG_CRYPTO_CMAC)
TEE_Result crypto_aes_cmac_alloc_ctx(struct crypto_mac_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cmac, mac)
#endif

/*
 * The crypto context used by the crypto_cipher_*() functions is defined by
 * struct crypto_cipher_ctx.
 */
struct crypto_cipher_ctx {
	const struct crypto_cipher_ops *ops;
};

struct crypto_cipher_ops {
	TEE_Result (*init)(struct crypto_cipher_ctx *ctx,
			   TEE_OperationMode mode,
			   const uint8_t *key1, size_t key1_len,
			   const uint8_t *key2, size_t key2_len,
			   const uint8_t *iv, size_t iv_len);
	TEE_Result (*update)(struct crypto_cipher_ctx *ctx, bool last_block,
			     const uint8_t *data, size_t len, uint8_t *dst);
	void (*final)(struct crypto_cipher_ctx *ctx);

	void (*free_ctx)(struct crypto_cipher_ctx *ctx);
	void (*copy_state)(struct crypto_cipher_ctx *dst_ctx,
			   struct crypto_cipher_ctx *src_ctx);
};

#if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_ECB)
TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ecb, cipher)
#endif

#if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CBC)
TEE_Result crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc, cipher)
#endif

#if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTR)
TEE_Result crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ctr, cipher)
#endif

#if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTS)
TEE_Result crypto_aes_cts_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cts, cipher)
#endif

#if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_XTS)
TEE_Result crypto_aes_xts_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_xts, cipher)
#endif

#if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_ECB)
TEE_Result crypto_des_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
TEE_Result crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_ecb, cipher)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_ecb, cipher)
#endif

#if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_CBC)
TEE_Result crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
TEE_Result crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
#else
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc, cipher)
CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc, cipher)
#endif

/*
 * The crypto context used by the crypto_authen_*() functions below is
 * defined by struct crypto_authenc_ctx.
 */
struct crypto_authenc_ctx {
	const struct crypto_authenc_ops *ops;
};

struct crypto_authenc_ops {
	TEE_Result (*init)(struct crypto_authenc_ctx *ctx,
			   TEE_OperationMode mode,
			   const uint8_t *key, size_t key_len,
			   const uint8_t *nonce, size_t nonce_len,
			   size_t tag_len, size_t aad_len,
			   size_t payload_len);
	TEE_Result (*update_aad)(struct crypto_authenc_ctx *ctx,
				 const uint8_t *data, size_t len);
	TEE_Result (*update_payload)(struct crypto_authenc_ctx *ctx,
				     TEE_OperationMode mode,
				     const uint8_t *src_data, size_t len,
				     uint8_t *dst_data);
	TEE_Result (*enc_final)(struct crypto_authenc_ctx *ctx,
				const uint8_t *src_data, size_t len,
				uint8_t *dst_data, uint8_t *dst_tag,
				size_t *dst_tag_len);
	TEE_Result (*dec_final)(struct crypto_authenc_ctx *ctx,
				const uint8_t *src_data, size_t len,
				uint8_t *dst_data, const uint8_t *tag,
				size_t tag_len);
	void (*final)(struct crypto_authenc_ctx *ctx);
	void (*free_ctx)(struct crypto_authenc_ctx *ctx);
	void (*copy_state)(struct crypto_authenc_ctx *dst_ctx,
			   struct crypto_authenc_ctx *src_ctx);
};

TEE_Result crypto_aes_ccm_alloc_ctx(struct crypto_authenc_ctx **ctx);
TEE_Result crypto_aes_gcm_alloc_ctx(struct crypto_authenc_ctx **ctx);
#endif /*__CRYPTO_CRYPTO_IMPL_H*/