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