xref: /optee_os/core/include/crypto/crypto.h (revision fb7ef469dfeb735e60383ad0e7410fe62dd97eb1)
1 /*
2  * Copyright (c) 2014-2017, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * This is the Cryptographic Provider API (CP API).
30  *
31  * This defines how most crypto syscalls that implement the Cryptographic
32  * Operations API can invoke the actual providers of cryptographic algorithms
33  * (such as LibTomCrypt).
34  *
35  * To add a new provider, you need to provide an implementation of this
36  * interface.
37  *
38  * The following parameters are commonly used.
39  *
40  * @ctx: context allocated by the syscall, for later use by the algorithm
41  * @algo: algorithm identifier (TEE_ALG_*)
42  */
43 
44 #ifndef __CRYPTO_CRYPTO_H
45 #define __CRYPTO_CRYPTO_H
46 
47 #include <tee_api_types.h>
48 
49 TEE_Result crypto_init(void);
50 
51 /* Message digest functions */
52 TEE_Result crypto_hash_get_ctx_size(uint32_t algo, size_t *size);
53 TEE_Result crypto_hash_init(void *ctx, uint32_t algo);
54 TEE_Result crypto_hash_update(void *ctx, uint32_t algo, const uint8_t *data,
55 			      size_t len);
56 TEE_Result crypto_hash_final(void *ctx, uint32_t algo, uint8_t *digest,
57 			     size_t len);
58 
59 /* Symmetric ciphers */
60 TEE_Result crypto_cipher_get_ctx_size(uint32_t algo, size_t *size);
61 TEE_Result crypto_cipher_init(void *ctx, uint32_t algo, TEE_OperationMode mode,
62 			      const uint8_t *key1, size_t key1_len,
63 			      const uint8_t *key2, size_t key2_len,
64 			      const uint8_t *iv, size_t iv_len);
65 TEE_Result crypto_cipher_update(void *ctx, uint32_t algo,
66 				TEE_OperationMode mode, bool last_block,
67 				const uint8_t *data, size_t len, uint8_t *dst);
68 void crypto_cipher_final(void *ctx, uint32_t algo);
69 TEE_Result crypto_cipher_get_block_size(uint32_t algo, size_t *size);
70 
71 /* Message Authentication Code functions */
72 TEE_Result crypto_mac_get_ctx_size(uint32_t algo, size_t *size);
73 TEE_Result crypto_mac_init(void *ctx, uint32_t algo, const uint8_t *key,
74 			   size_t len);
75 TEE_Result crypto_mac_update(void *ctx, uint32_t algo, const uint8_t *data,
76 			     size_t len);
77 TEE_Result crypto_mac_final(void *ctx, uint32_t algo, uint8_t *digest,
78 			    size_t digest_len);
79 
80 /* Authenticated encryption */
81 TEE_Result crypto_authenc_get_ctx_size(uint32_t algo, size_t *size);
82 TEE_Result crypto_authenc_init(void *ctx, uint32_t algo, TEE_OperationMode mode,
83 			       const uint8_t *key, size_t key_len,
84 			       const uint8_t *nonce, size_t nonce_len,
85 			       size_t tag_len, size_t aad_len,
86 			       size_t payload_len);
87 TEE_Result crypto_authenc_update_aad(void *ctx, uint32_t algo,
88 				     TEE_OperationMode mode,
89 				     const uint8_t *data, size_t len);
90 TEE_Result crypto_authenc_update_payload(void *ctx, uint32_t algo,
91 					 TEE_OperationMode mode,
92 					 const uint8_t *src_data,
93 					 size_t src_len, uint8_t *dst_data,
94 					 size_t *dst_len);
95 TEE_Result crypto_authenc_enc_final(void *ctx, uint32_t algo,
96 				    const uint8_t *src_data, size_t src_len,
97 				    uint8_t *dst_data, size_t *dst_len,
98 				    uint8_t *dst_tag, size_t *dst_tag_len);
99 TEE_Result crypto_authenc_dec_final(void *ctx, uint32_t algo,
100 				    const uint8_t *src_data, size_t src_len,
101 				    uint8_t *dst_data, size_t *dst_len,
102 				    const uint8_t *tag, size_t tag_len);
103 void crypto_authenc_final(void *ctx, uint32_t algo);
104 
105 /* Implementation-defined big numbers */
106 
107 /*
108  * Allocate a bignum capable of holding an unsigned integer value of
109  * up to bitsize bits
110  */
111 struct bignum *crypto_bignum_allocate(size_t size_bits);
112 TEE_Result crypto_bignum_bin2bn(const uint8_t *from, size_t fromsize,
113 				struct bignum *to);
114 size_t crypto_bignum_num_bytes(struct bignum *a);
115 size_t crypto_bignum_num_bits(struct bignum *a);
116 void crypto_bignum_bn2bin(const struct bignum *from, uint8_t *to);
117 void crypto_bignum_copy(struct bignum *to, const struct bignum *from);
118 void crypto_bignum_free(struct bignum *a);
119 void crypto_bignum_clear(struct bignum *a);
120 
121 /* return -1 if a<b, 0 if a==b, +1 if a>b */
122 int32_t crypto_bignum_compare(struct bignum *a, struct bignum *b);
123 
124 /* Asymmetric algorithms */
125 
126 struct rsa_keypair {
127 	struct bignum *e;	/* Public exponent */
128 	struct bignum *d;	/* Private exponent */
129 	struct bignum *n;	/* Modulus */
130 
131 	/* Optional CRT parameters (all NULL if unused) */
132 	struct bignum *p;	/* N = pq */
133 	struct bignum *q;
134 	struct bignum *qp;	/* 1/q mod p */
135 	struct bignum *dp;	/* d mod (p-1) */
136 	struct bignum *dq;	/* d mod (q-1) */
137 };
138 
139 struct rsa_public_key {
140 	struct bignum *e;	/* Public exponent */
141 	struct bignum *n;	/* Modulus */
142 };
143 
144 struct dsa_keypair {
145 	struct bignum *g;	/* Generator of subgroup (public) */
146 	struct bignum *p;	/* Prime number (public) */
147 	struct bignum *q;	/* Order of subgroup (public) */
148 	struct bignum *y;	/* Public key */
149 	struct bignum *x;	/* Private key */
150 };
151 
152 struct dsa_public_key {
153 	struct bignum *g;	/* Generator of subgroup (public) */
154 	struct bignum *p;	/* Prime number (public) */
155 	struct bignum *q;	/* Order of subgroup (public) */
156 	struct bignum *y;	/* Public key */
157 };
158 
159 struct dh_keypair {
160 	struct bignum *g;	/* Generator of Z_p (shared) */
161 	struct bignum *p;	/* Prime modulus (shared) */
162 	struct bignum *x;	/* Private key */
163 	struct bignum *y;	/* Public key y = g^x */
164 
165 	/*
166 	 * Optional parameters used by key generation.
167 	 * When not used, q == NULL and xbits == 0
168 	 */
169 	struct bignum *q;	/* x must be in the range [2, q-2] */
170 	uint32_t xbits;		/* Number of bits in the private key */
171 };
172 
173 struct ecc_public_key {
174 	struct bignum *x;	/* Public value x */
175 	struct bignum *y;	/* Public value y */
176 	uint32_t curve;	        /* Curve type */
177 };
178 
179 struct ecc_keypair {
180 	struct bignum *d;	/* Private value */
181 	struct bignum *x;	/* Public value x */
182 	struct bignum *y;	/* Public value y */
183 	uint32_t curve;	        /* Curve type */
184 };
185 
186 /*
187  * Key allocation functions
188  * Allocate the bignum's inside a key structure.
189  * TEE core will later use crypto_bignum_free().
190  */
191 TEE_Result crypto_acipher_alloc_rsa_keypair(struct rsa_keypair *s,
192 				size_t key_size_bits);
193 TEE_Result crypto_acipher_alloc_rsa_public_key(struct rsa_public_key *s,
194 				   size_t key_size_bits);
195 void crypto_acipher_free_rsa_public_key(struct rsa_public_key *s);
196 TEE_Result crypto_acipher_alloc_dsa_keypair(struct dsa_keypair *s,
197 				size_t key_size_bits);
198 TEE_Result crypto_acipher_alloc_dsa_public_key(struct dsa_public_key *s,
199 				   size_t key_size_bits);
200 TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s,
201 			       size_t key_size_bits);
202 TEE_Result crypto_acipher_alloc_ecc_public_key(struct ecc_public_key *s,
203 				   size_t key_size_bits);
204 TEE_Result crypto_acipher_alloc_ecc_keypair(struct ecc_keypair *s,
205 				size_t key_size_bits);
206 void crypto_acipher_free_ecc_public_key(struct ecc_public_key *s);
207 
208 /*
209  * Key generation functions
210  */
211 TEE_Result crypto_acipher_gen_rsa_key(struct rsa_keypair *key, size_t key_size);
212 TEE_Result crypto_acipher_gen_dsa_key(struct dsa_keypair *key, size_t key_size);
213 TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key, struct bignum *q,
214 				     size_t xbits);
215 TEE_Result crypto_acipher_gen_ecc_key(struct ecc_keypair *key);
216 
217 TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key,
218 					   struct bignum *public_key,
219 					   struct bignum *secret);
220 
221 TEE_Result crypto_acipher_rsanopad_decrypt(struct rsa_keypair *key,
222 					   const uint8_t *src, size_t src_len,
223 					   uint8_t *dst, size_t *dst_len);
224 TEE_Result crypto_acipher_rsanopad_encrypt(struct rsa_public_key *key,
225 					   const uint8_t *src, size_t src_len,
226 					   uint8_t *dst, size_t *dst_len);
227 TEE_Result crypto_acipher_rsaes_decrypt(uint32_t algo, struct rsa_keypair *key,
228 					const uint8_t *label, size_t label_len,
229 					const uint8_t *src, size_t src_len,
230 					uint8_t *dst, size_t *dst_len);
231 TEE_Result crypto_acipher_rsaes_encrypt(uint32_t algo,
232 					struct rsa_public_key *key,
233 					const uint8_t *label, size_t label_len,
234 					const uint8_t *src, size_t src_len,
235 					uint8_t *dst, size_t *dst_len);
236 /* RSA SSA sign/verify: if salt_len == -1, use default value */
237 TEE_Result crypto_acipher_rsassa_sign(uint32_t algo, struct rsa_keypair *key,
238 				      int salt_len, const uint8_t *msg,
239 				      size_t msg_len, uint8_t *sig,
240 				      size_t *sig_len);
241 TEE_Result crypto_acipher_rsassa_verify(uint32_t algo,
242 					struct rsa_public_key *key,
243 					int salt_len, const uint8_t *msg,
244 					size_t msg_len, const uint8_t *sig,
245 					size_t sig_len);
246 TEE_Result crypto_acipher_dsa_sign(uint32_t algo, struct dsa_keypair *key,
247 				   const uint8_t *msg, size_t msg_len,
248 				   uint8_t *sig, size_t *sig_len);
249 TEE_Result crypto_acipher_dsa_verify(uint32_t algo, struct dsa_public_key *key,
250 				     const uint8_t *msg, size_t msg_len,
251 				     const uint8_t *sig, size_t sig_len);
252 TEE_Result crypto_acipher_ecc_sign(uint32_t algo, struct ecc_keypair *key,
253 				   const uint8_t *msg, size_t msg_len,
254 				   uint8_t *sig, size_t *sig_len);
255 TEE_Result crypto_acipher_ecc_verify(uint32_t algo, struct ecc_public_key *key,
256 				     const uint8_t *msg, size_t msg_len,
257 				     const uint8_t *sig, size_t sig_len);
258 TEE_Result crypto_acipher_ecc_shared_secret(struct ecc_keypair *private_key,
259 					    struct ecc_public_key *public_key,
260 					    void *secret,
261 					    unsigned long *secret_len);
262 
263 /*
264  * Verifies a SHA-256 hash, doesn't require crypto_init() to be called in
265  * advance and has as few dependencies as possible.
266  *
267  * This function is primarily used by pager and early initialization code
268  * where the complete crypto library isn't available.
269  */
270 TEE_Result hash_sha256_check(const uint8_t *hash, const uint8_t *data,
271 		size_t data_size);
272 
273 /* Add entropy to PRNG entropy pool. */
274 TEE_Result crypto_rng_add_entropy(const uint8_t *inbuf, size_t len);
275 
276 /* To read random data from PRNG implementation. */
277 TEE_Result crypto_rng_read(void *buf, size_t blen);
278 
279 TEE_Result rng_generate(void *buffer, size_t len);
280 
281 TEE_Result crypto_aes_expand_enc_key(const void *key, size_t key_len,
282 				     void *enc_key, unsigned int *rounds);
283 void crypto_aes_enc_block(const void *enc_key, unsigned int rounds,
284 			  const void *src, void *dst);
285 
286 #endif /* __CRYPTO_CRYPTO_H */
287