xref: /optee_os/core/include/crypto/crypto.h (revision 90040fa4c81cdb7bd396c265e2fe78134e3a9aa1)
11bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */
2e1770e71SJens Wiklander /*
3e1770e71SJens Wiklander  * Copyright (c) 2014-2017, Linaro Limited
4e1770e71SJens Wiklander  */
5e1770e71SJens Wiklander 
6e1770e71SJens Wiklander /*
7e1770e71SJens Wiklander  * This is the Cryptographic Provider API (CP API).
8e1770e71SJens Wiklander  *
9e1770e71SJens Wiklander  * This defines how most crypto syscalls that implement the Cryptographic
10e1770e71SJens Wiklander  * Operations API can invoke the actual providers of cryptographic algorithms
11e1770e71SJens Wiklander  * (such as LibTomCrypt).
12e1770e71SJens Wiklander  *
13e1770e71SJens Wiklander  * To add a new provider, you need to provide an implementation of this
14e1770e71SJens Wiklander  * interface.
15e1770e71SJens Wiklander  *
16e1770e71SJens Wiklander  * The following parameters are commonly used.
17e1770e71SJens Wiklander  *
18e1770e71SJens Wiklander  * @ctx: context allocated by the syscall, for later use by the algorithm
19e1770e71SJens Wiklander  * @algo: algorithm identifier (TEE_ALG_*)
20e1770e71SJens Wiklander  */
21e1770e71SJens Wiklander 
22e1770e71SJens Wiklander #ifndef __CRYPTO_CRYPTO_H
23e1770e71SJens Wiklander #define __CRYPTO_CRYPTO_H
24e1770e71SJens Wiklander 
25e1770e71SJens Wiklander #include <tee_api_types.h>
26e1770e71SJens Wiklander 
27e1770e71SJens Wiklander TEE_Result crypto_init(void);
28e1770e71SJens Wiklander 
29e1770e71SJens Wiklander /* Message digest functions */
30ecf2e014SJens Wiklander TEE_Result crypto_hash_alloc_ctx(void **ctx, uint32_t algo);
316b3a371cSJens Wiklander TEE_Result crypto_hash_init(void *ctx);
326b3a371cSJens Wiklander TEE_Result crypto_hash_update(void *ctx, const uint8_t *data, size_t len);
336b3a371cSJens Wiklander TEE_Result crypto_hash_final(void *ctx, uint8_t *digest, size_t len);
346b3a371cSJens Wiklander void crypto_hash_free_ctx(void *ctx);
356b3a371cSJens Wiklander void crypto_hash_copy_state(void *dst_ctx, void *src_ctx);
36e1770e71SJens Wiklander 
37e1770e71SJens Wiklander /* Symmetric ciphers */
3872a9b1a0SJens Wiklander TEE_Result crypto_cipher_alloc_ctx(void **ctx, uint32_t algo);
39cbda7091SJens Wiklander TEE_Result crypto_cipher_init(void *ctx, TEE_OperationMode mode,
40e1770e71SJens Wiklander 			      const uint8_t *key1, size_t key1_len,
41e1770e71SJens Wiklander 			      const uint8_t *key2, size_t key2_len,
42e1770e71SJens Wiklander 			      const uint8_t *iv, size_t iv_len);
43cbda7091SJens Wiklander TEE_Result crypto_cipher_update(void *ctx, TEE_OperationMode mode,
44cbda7091SJens Wiklander 				bool last_block, const uint8_t *data,
45cbda7091SJens Wiklander 				size_t len, uint8_t *dst);
46cbda7091SJens Wiklander void crypto_cipher_final(void *ctx);
47e1770e71SJens Wiklander TEE_Result crypto_cipher_get_block_size(uint32_t algo, size_t *size);
48cbda7091SJens Wiklander void crypto_cipher_free_ctx(void *ctx);
49cbda7091SJens Wiklander void crypto_cipher_copy_state(void *dst_ctx, void *src_ctx);
50e1770e71SJens Wiklander 
51e1770e71SJens Wiklander /* Message Authentication Code functions */
5282ef73bcSJens Wiklander TEE_Result crypto_mac_alloc_ctx(void **ctx, uint32_t algo);
53c69bc615SJens Wiklander TEE_Result crypto_mac_init(void *ctx, const uint8_t *key, size_t len);
54c69bc615SJens Wiklander TEE_Result crypto_mac_update(void *ctx, const uint8_t *data, size_t len);
55c69bc615SJens Wiklander TEE_Result crypto_mac_final(void *ctx, uint8_t *digest, size_t digest_len);
56c69bc615SJens Wiklander void crypto_mac_free_ctx(void *ctx);
57c69bc615SJens Wiklander void crypto_mac_copy_state(void *dst_ctx, void *src_ctx);
58e1770e71SJens Wiklander 
59e1770e71SJens Wiklander /* Authenticated encryption */
60d7ac7d0fSJens Wiklander TEE_Result crypto_authenc_alloc_ctx(void **ctx, uint32_t algo);
6152ee414bSJens Wiklander TEE_Result crypto_authenc_init(void *ctx, TEE_OperationMode mode,
62e1770e71SJens Wiklander 			       const uint8_t *key, size_t key_len,
63e1770e71SJens Wiklander 			       const uint8_t *nonce, size_t nonce_len,
64e1770e71SJens Wiklander 			       size_t tag_len, size_t aad_len,
65e1770e71SJens Wiklander 			       size_t payload_len);
6652ee414bSJens Wiklander TEE_Result crypto_authenc_update_aad(void *ctx, TEE_OperationMode mode,
67e1770e71SJens Wiklander 				     const uint8_t *data, size_t len);
6852ee414bSJens Wiklander TEE_Result crypto_authenc_update_payload(void *ctx, TEE_OperationMode mode,
69e1770e71SJens Wiklander 					 const uint8_t *src_data,
70e1770e71SJens Wiklander 					 size_t src_len, uint8_t *dst_data,
71e1770e71SJens Wiklander 					 size_t *dst_len);
7252ee414bSJens Wiklander TEE_Result crypto_authenc_enc_final(void *ctx, const uint8_t *src_data,
7352ee414bSJens Wiklander 				    size_t src_len, uint8_t *dst_data,
7452ee414bSJens Wiklander 				    size_t *dst_len, uint8_t *dst_tag,
7552ee414bSJens Wiklander 				    size_t *dst_tag_len);
7652ee414bSJens Wiklander TEE_Result crypto_authenc_dec_final(void *ctx, const uint8_t *src_data,
7752ee414bSJens Wiklander 				    size_t src_len, uint8_t *dst_data,
7852ee414bSJens Wiklander 				    size_t *dst_len, const uint8_t *tag,
7952ee414bSJens Wiklander 				    size_t tag_len);
8052ee414bSJens Wiklander void crypto_authenc_final(void *ctx);
8152ee414bSJens Wiklander void crypto_authenc_free_ctx(void *ctx);
8252ee414bSJens Wiklander void crypto_authenc_copy_state(void *dst_ctx, void *src_ctx);
83e1770e71SJens Wiklander 
845dfe86d0SJorge Ramirez-Ortiz /* Informs crypto that the data in the buffer will be removed from storage */
855dfe86d0SJorge Ramirez-Ortiz void crypto_storage_obj_del(uint8_t *data, size_t len);
865dfe86d0SJorge Ramirez-Ortiz 
87e1770e71SJens Wiklander /* Implementation-defined big numbers */
88e1770e71SJens Wiklander 
89e1770e71SJens Wiklander /*
90e1770e71SJens Wiklander  * Allocate a bignum capable of holding an unsigned integer value of
91e1770e71SJens Wiklander  * up to bitsize bits
92e1770e71SJens Wiklander  */
93e1770e71SJens Wiklander struct bignum *crypto_bignum_allocate(size_t size_bits);
94e1770e71SJens Wiklander TEE_Result crypto_bignum_bin2bn(const uint8_t *from, size_t fromsize,
95e1770e71SJens Wiklander 				struct bignum *to);
96e1770e71SJens Wiklander size_t crypto_bignum_num_bytes(struct bignum *a);
97e1770e71SJens Wiklander size_t crypto_bignum_num_bits(struct bignum *a);
98e1770e71SJens Wiklander void crypto_bignum_bn2bin(const struct bignum *from, uint8_t *to);
99e1770e71SJens Wiklander void crypto_bignum_copy(struct bignum *to, const struct bignum *from);
100e1770e71SJens Wiklander void crypto_bignum_free(struct bignum *a);
101e1770e71SJens Wiklander void crypto_bignum_clear(struct bignum *a);
102e1770e71SJens Wiklander 
103e1770e71SJens Wiklander /* return -1 if a<b, 0 if a==b, +1 if a>b */
104e1770e71SJens Wiklander int32_t crypto_bignum_compare(struct bignum *a, struct bignum *b);
105e1770e71SJens Wiklander 
106e1770e71SJens Wiklander /* Asymmetric algorithms */
107e1770e71SJens Wiklander 
108e1770e71SJens Wiklander struct rsa_keypair {
109e1770e71SJens Wiklander 	struct bignum *e;	/* Public exponent */
110e1770e71SJens Wiklander 	struct bignum *d;	/* Private exponent */
111e1770e71SJens Wiklander 	struct bignum *n;	/* Modulus */
112e1770e71SJens Wiklander 
113e1770e71SJens Wiklander 	/* Optional CRT parameters (all NULL if unused) */
114e1770e71SJens Wiklander 	struct bignum *p;	/* N = pq */
115e1770e71SJens Wiklander 	struct bignum *q;
116e1770e71SJens Wiklander 	struct bignum *qp;	/* 1/q mod p */
117e1770e71SJens Wiklander 	struct bignum *dp;	/* d mod (p-1) */
118e1770e71SJens Wiklander 	struct bignum *dq;	/* d mod (q-1) */
119e1770e71SJens Wiklander };
120e1770e71SJens Wiklander 
121e1770e71SJens Wiklander struct rsa_public_key {
122e1770e71SJens Wiklander 	struct bignum *e;	/* Public exponent */
123e1770e71SJens Wiklander 	struct bignum *n;	/* Modulus */
124e1770e71SJens Wiklander };
125e1770e71SJens Wiklander 
126e1770e71SJens Wiklander struct dsa_keypair {
127e1770e71SJens Wiklander 	struct bignum *g;	/* Generator of subgroup (public) */
128e1770e71SJens Wiklander 	struct bignum *p;	/* Prime number (public) */
129e1770e71SJens Wiklander 	struct bignum *q;	/* Order of subgroup (public) */
130e1770e71SJens Wiklander 	struct bignum *y;	/* Public key */
131e1770e71SJens Wiklander 	struct bignum *x;	/* Private key */
132e1770e71SJens Wiklander };
133e1770e71SJens Wiklander 
134e1770e71SJens Wiklander struct dsa_public_key {
135e1770e71SJens Wiklander 	struct bignum *g;	/* Generator of subgroup (public) */
136e1770e71SJens Wiklander 	struct bignum *p;	/* Prime number (public) */
137e1770e71SJens Wiklander 	struct bignum *q;	/* Order of subgroup (public) */
138e1770e71SJens Wiklander 	struct bignum *y;	/* Public key */
139e1770e71SJens Wiklander };
140e1770e71SJens Wiklander 
141e1770e71SJens Wiklander struct dh_keypair {
142e1770e71SJens Wiklander 	struct bignum *g;	/* Generator of Z_p (shared) */
143e1770e71SJens Wiklander 	struct bignum *p;	/* Prime modulus (shared) */
144e1770e71SJens Wiklander 	struct bignum *x;	/* Private key */
145e1770e71SJens Wiklander 	struct bignum *y;	/* Public key y = g^x */
146e1770e71SJens Wiklander 
147e1770e71SJens Wiklander 	/*
148e1770e71SJens Wiklander 	 * Optional parameters used by key generation.
149e1770e71SJens Wiklander 	 * When not used, q == NULL and xbits == 0
150e1770e71SJens Wiklander 	 */
151e1770e71SJens Wiklander 	struct bignum *q;	/* x must be in the range [2, q-2] */
152e1770e71SJens Wiklander 	uint32_t xbits;		/* Number of bits in the private key */
153e1770e71SJens Wiklander };
154e1770e71SJens Wiklander 
155e1770e71SJens Wiklander struct ecc_public_key {
156e1770e71SJens Wiklander 	struct bignum *x;	/* Public value x */
157e1770e71SJens Wiklander 	struct bignum *y;	/* Public value y */
158e1770e71SJens Wiklander 	uint32_t curve;	        /* Curve type */
159df00cf59SCedric Neveux 	const struct crypto_ecc_public_ops *ops; /* Key Operations */
160e1770e71SJens Wiklander };
161e1770e71SJens Wiklander 
162e1770e71SJens Wiklander struct ecc_keypair {
163e1770e71SJens Wiklander 	struct bignum *d;	/* Private value */
164e1770e71SJens Wiklander 	struct bignum *x;	/* Public value x */
165e1770e71SJens Wiklander 	struct bignum *y;	/* Public value y */
166e1770e71SJens Wiklander 	uint32_t curve;	        /* Curve type */
167df00cf59SCedric Neveux 	const struct crypto_ecc_keypair_ops *ops; /* Key Operations */
168e1770e71SJens Wiklander };
169e1770e71SJens Wiklander 
170*90040fa4SSohaib ul Hassan struct x25519_keypair {
171*90040fa4SSohaib ul Hassan 	uint8_t *priv;	/* Private value */
172*90040fa4SSohaib ul Hassan 	uint8_t *pub;	/* Public value */
173*90040fa4SSohaib ul Hassan };
174*90040fa4SSohaib ul Hassan 
175e1770e71SJens Wiklander /*
176e1770e71SJens Wiklander  * Key allocation functions
177e1770e71SJens Wiklander  * Allocate the bignum's inside a key structure.
178e1770e71SJens Wiklander  * TEE core will later use crypto_bignum_free().
179e1770e71SJens Wiklander  */
180e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_rsa_keypair(struct rsa_keypair *s,
181e1770e71SJens Wiklander 				size_t key_size_bits);
182e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_rsa_public_key(struct rsa_public_key *s,
183e1770e71SJens Wiklander 				   size_t key_size_bits);
184e1770e71SJens Wiklander void crypto_acipher_free_rsa_public_key(struct rsa_public_key *s);
185a1d5c81fSElias von Däniken void crypto_acipher_free_rsa_keypair(struct rsa_keypair *s);
186e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_dsa_keypair(struct dsa_keypair *s,
187e1770e71SJens Wiklander 				size_t key_size_bits);
188e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_dsa_public_key(struct dsa_public_key *s,
189e1770e71SJens Wiklander 				   size_t key_size_bits);
190e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s,
191e1770e71SJens Wiklander 			       size_t key_size_bits);
192e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_ecc_public_key(struct ecc_public_key *s,
193df00cf59SCedric Neveux 					       uint32_t key_type,
194e1770e71SJens Wiklander 					       size_t key_size_bits);
195e1770e71SJens Wiklander TEE_Result crypto_acipher_alloc_ecc_keypair(struct ecc_keypair *s,
196df00cf59SCedric Neveux 					    uint32_t key_type,
197e1770e71SJens Wiklander 					    size_t key_size_bits);
198e1770e71SJens Wiklander void crypto_acipher_free_ecc_public_key(struct ecc_public_key *s);
199*90040fa4SSohaib ul Hassan TEE_Result crypto_acipher_alloc_x25519_keypair(struct x25519_keypair *s,
200*90040fa4SSohaib ul Hassan 					       size_t key_size_bits);
201e1770e71SJens Wiklander 
202e1770e71SJens Wiklander /*
203e1770e71SJens Wiklander  * Key generation functions
204e1770e71SJens Wiklander  */
205e1770e71SJens Wiklander TEE_Result crypto_acipher_gen_rsa_key(struct rsa_keypair *key, size_t key_size);
206e1770e71SJens Wiklander TEE_Result crypto_acipher_gen_dsa_key(struct dsa_keypair *key, size_t key_size);
207e1770e71SJens Wiklander TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key, struct bignum *q,
2089f4dcefbSJerome Forissier 				     size_t xbits, size_t key_size);
20921282baeSJerome Forissier TEE_Result crypto_acipher_gen_ecc_key(struct ecc_keypair *key, size_t key_size);
210*90040fa4SSohaib ul Hassan TEE_Result crypto_acipher_gen_x25519_key(struct x25519_keypair *key,
211*90040fa4SSohaib ul Hassan 					 size_t key_size);
212e1770e71SJens Wiklander 
213e1770e71SJens Wiklander TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key,
214e1770e71SJens Wiklander 					   struct bignum *public_key,
215e1770e71SJens Wiklander 					   struct bignum *secret);
216e1770e71SJens Wiklander 
217e1770e71SJens Wiklander TEE_Result crypto_acipher_rsanopad_decrypt(struct rsa_keypair *key,
218e1770e71SJens Wiklander 					   const uint8_t *src, size_t src_len,
219e1770e71SJens Wiklander 					   uint8_t *dst, size_t *dst_len);
220e1770e71SJens Wiklander TEE_Result crypto_acipher_rsanopad_encrypt(struct rsa_public_key *key,
221e1770e71SJens Wiklander 					   const uint8_t *src, size_t src_len,
222e1770e71SJens Wiklander 					   uint8_t *dst, size_t *dst_len);
223e1770e71SJens Wiklander TEE_Result crypto_acipher_rsaes_decrypt(uint32_t algo, struct rsa_keypair *key,
224e1770e71SJens Wiklander 					const uint8_t *label, size_t label_len,
225e1770e71SJens Wiklander 					const uint8_t *src, size_t src_len,
226e1770e71SJens Wiklander 					uint8_t *dst, size_t *dst_len);
227e1770e71SJens Wiklander TEE_Result crypto_acipher_rsaes_encrypt(uint32_t algo,
228e1770e71SJens Wiklander 					struct rsa_public_key *key,
229e1770e71SJens Wiklander 					const uint8_t *label, size_t label_len,
230e1770e71SJens Wiklander 					const uint8_t *src, size_t src_len,
231e1770e71SJens Wiklander 					uint8_t *dst, size_t *dst_len);
232e1770e71SJens Wiklander /* RSA SSA sign/verify: if salt_len == -1, use default value */
233e1770e71SJens Wiklander TEE_Result crypto_acipher_rsassa_sign(uint32_t algo, struct rsa_keypair *key,
234e1770e71SJens Wiklander 				      int salt_len, const uint8_t *msg,
235e1770e71SJens Wiklander 				      size_t msg_len, uint8_t *sig,
236e1770e71SJens Wiklander 				      size_t *sig_len);
237e1770e71SJens Wiklander TEE_Result crypto_acipher_rsassa_verify(uint32_t algo,
238e1770e71SJens Wiklander 					struct rsa_public_key *key,
239e1770e71SJens Wiklander 					int salt_len, const uint8_t *msg,
240e1770e71SJens Wiklander 					size_t msg_len, const uint8_t *sig,
241e1770e71SJens Wiklander 					size_t sig_len);
242e1770e71SJens Wiklander TEE_Result crypto_acipher_dsa_sign(uint32_t algo, struct dsa_keypair *key,
243e1770e71SJens Wiklander 				   const uint8_t *msg, size_t msg_len,
244e1770e71SJens Wiklander 				   uint8_t *sig, size_t *sig_len);
245e1770e71SJens Wiklander TEE_Result crypto_acipher_dsa_verify(uint32_t algo, struct dsa_public_key *key,
246e1770e71SJens Wiklander 				     const uint8_t *msg, size_t msg_len,
247e1770e71SJens Wiklander 				     const uint8_t *sig, size_t sig_len);
248e1770e71SJens Wiklander TEE_Result crypto_acipher_ecc_sign(uint32_t algo, struct ecc_keypair *key,
249e1770e71SJens Wiklander 				   const uint8_t *msg, size_t msg_len,
250e1770e71SJens Wiklander 				   uint8_t *sig, size_t *sig_len);
251e1770e71SJens Wiklander TEE_Result crypto_acipher_ecc_verify(uint32_t algo, struct ecc_public_key *key,
252e1770e71SJens Wiklander 				     const uint8_t *msg, size_t msg_len,
253e1770e71SJens Wiklander 				     const uint8_t *sig, size_t sig_len);
254e1770e71SJens Wiklander TEE_Result crypto_acipher_ecc_shared_secret(struct ecc_keypair *private_key,
255e1770e71SJens Wiklander 					    struct ecc_public_key *public_key,
256e1770e71SJens Wiklander 					    void *secret,
257e1770e71SJens Wiklander 					    unsigned long *secret_len);
25891fc6bd8SJerome Forissier TEE_Result crypto_acipher_sm2_pke_decrypt(struct ecc_keypair *key,
25991fc6bd8SJerome Forissier 					  const uint8_t *src, size_t src_len,
26091fc6bd8SJerome Forissier 					  uint8_t *dst, size_t *dst_len);
26191fc6bd8SJerome Forissier TEE_Result crypto_acipher_sm2_pke_encrypt(struct ecc_public_key *key,
26291fc6bd8SJerome Forissier 					  const uint8_t *src, size_t src_len,
26391fc6bd8SJerome Forissier 					  uint8_t *dst, size_t *dst_len);
264*90040fa4SSohaib ul Hassan TEE_Result crypto_acipher_x25519_shared_secret(struct x25519_keypair
265*90040fa4SSohaib ul Hassan 					       *private_key,
266*90040fa4SSohaib ul Hassan 					       void *public_key, void *secret,
267*90040fa4SSohaib ul Hassan 					       unsigned long *secret_len);
268e1770e71SJens Wiklander 
2695b385b3fSJerome Forissier struct sm2_kep_parms {
2705b385b3fSJerome Forissier 	uint8_t *out;
2715b385b3fSJerome Forissier 	size_t out_len;
2725b385b3fSJerome Forissier 	bool is_initiator;
2735b385b3fSJerome Forissier 	const uint8_t *initiator_id;
2745b385b3fSJerome Forissier 	size_t initiator_id_len;
2755b385b3fSJerome Forissier 	const uint8_t *responder_id;
2765b385b3fSJerome Forissier 	size_t responder_id_len;
2775b385b3fSJerome Forissier 	const uint8_t *conf_in;
2785b385b3fSJerome Forissier 	size_t conf_in_len;
2795b385b3fSJerome Forissier 	uint8_t *conf_out;
2805b385b3fSJerome Forissier 	size_t conf_out_len;
2815b385b3fSJerome Forissier };
2825b385b3fSJerome Forissier 
2835b385b3fSJerome Forissier TEE_Result crypto_acipher_sm2_kep_derive(struct ecc_keypair *my_key,
2845b385b3fSJerome Forissier 					 struct ecc_keypair *my_eph_key,
2855b385b3fSJerome Forissier 					 struct ecc_public_key *peer_key,
2865b385b3fSJerome Forissier 					 struct ecc_public_key *peer_eph_key,
2875b385b3fSJerome Forissier 					 struct sm2_kep_parms *p);
2885b385b3fSJerome Forissier 
289e1770e71SJens Wiklander /*
290e1770e71SJens Wiklander  * Verifies a SHA-256 hash, doesn't require crypto_init() to be called in
291e1770e71SJens Wiklander  * advance and has as few dependencies as possible.
292e1770e71SJens Wiklander  *
293e1770e71SJens Wiklander  * This function is primarily used by pager and early initialization code
294e1770e71SJens Wiklander  * where the complete crypto library isn't available.
295e1770e71SJens Wiklander  */
296e1770e71SJens Wiklander TEE_Result hash_sha256_check(const uint8_t *hash, const uint8_t *data,
297e1770e71SJens Wiklander 		size_t data_size);
298e1770e71SJens Wiklander 
299b8bb0afaSSumit Garg /*
300b8bb0afaSSumit Garg  * Computes a SHA-512/256 hash, vetted conditioner as per NIST.SP.800-90B.
301b8bb0afaSSumit Garg  * It doesn't require crypto_init() to be called in advance and has as few
302b8bb0afaSSumit Garg  * dependencies as possible.
303b8bb0afaSSumit Garg  *
304b8bb0afaSSumit Garg  * This function could be used inside interrupt context where the crypto
305b8bb0afaSSumit Garg  * library can't be used due to mutex handling.
306b8bb0afaSSumit Garg  */
307b8bb0afaSSumit Garg TEE_Result hash_sha512_256_compute(uint8_t *digest, const uint8_t *data,
308b8bb0afaSSumit Garg 		size_t data_size);
309b8bb0afaSSumit Garg 
3106e954a6eSJens Wiklander #define CRYPTO_RNG_SRC_IS_QUICK(sid) (!!((sid) & 1))
311e1770e71SJens Wiklander 
3126e954a6eSJens Wiklander /*
3136e954a6eSJens Wiklander  * enum crypto_rng_src - RNG entropy source
3146e954a6eSJens Wiklander  *
3156e954a6eSJens Wiklander  * Identifiers for different RNG entropy sources. The lowest bit indicates
3166e954a6eSJens Wiklander  * if the source is to be merely queued (bit is 1) or if it's delivered
3176e954a6eSJens Wiklander  * directly to the pool. The difference is that in the latter case RPC to
3186e954a6eSJens Wiklander  * normal world can be performed and in the former it must not.
3196e954a6eSJens Wiklander  */
3206e954a6eSJens Wiklander enum crypto_rng_src {
3216e954a6eSJens Wiklander 	CRYPTO_RNG_SRC_JITTER_SESSION	= (0 << 1 | 0),
3226e954a6eSJens Wiklander 	CRYPTO_RNG_SRC_JITTER_RPC	= (1 << 1 | 1),
3236e954a6eSJens Wiklander 	CRYPTO_RNG_SRC_NONSECURE	= (1 << 1 | 0),
3246e954a6eSJens Wiklander };
325e1770e71SJens Wiklander 
3266e954a6eSJens Wiklander /*
3276e954a6eSJens Wiklander  * crypto_rng_init() - initialize the RNG
3286e954a6eSJens Wiklander  * @data:	buffer with initial seed
3296e954a6eSJens Wiklander  * @dlen:	length of @data
3306e954a6eSJens Wiklander  */
3316e954a6eSJens Wiklander TEE_Result crypto_rng_init(const void *data, size_t dlen);
3326e954a6eSJens Wiklander 
3336e954a6eSJens Wiklander /*
3346e954a6eSJens Wiklander  * crypto_rng_add_event() - supply entropy to RNG from a source
3356e954a6eSJens Wiklander  * @sid:	Source identifier, should be unique for a specific source
3366e954a6eSJens Wiklander  * @pnum:	Pool number, acquired using crypto_rng_get_next_pool_num()
3376e954a6eSJens Wiklander  * @data:	Data associated with the event
3386e954a6eSJens Wiklander  * @dlen:	Length of @data
3396e954a6eSJens Wiklander  *
3406e954a6eSJens Wiklander  * @sid controls whether the event is merly queued in a ring buffer or if
3416e954a6eSJens Wiklander  * it's added to one of the pools directly. If CRYPTO_RNG_SRC_IS_QUICK() is
3426e954a6eSJens Wiklander  * true (lowest bit set) events are queue otherwise added to corresponding
3436e954a6eSJens Wiklander  * pool. If CRYPTO_RNG_SRC_IS_QUICK() is false, eventual queued events are
3446e954a6eSJens Wiklander  * added to their queues too.
3456e954a6eSJens Wiklander  */
3466e954a6eSJens Wiklander void crypto_rng_add_event(enum crypto_rng_src sid, unsigned int *pnum,
3476e954a6eSJens Wiklander 			  const void *data, size_t dlen);
3486e954a6eSJens Wiklander 
3496e954a6eSJens Wiklander /*
3506e954a6eSJens Wiklander  * crypto_rng_read() - read cryptograhically secure RNG
3516e954a6eSJens Wiklander  * @buf:	Buffer to hold the data
3526e954a6eSJens Wiklander  * @len:	Length of buffer.
3536e954a6eSJens Wiklander  *
3546e954a6eSJens Wiklander  * Eventual queued events are also added to their pools during this
3556e954a6eSJens Wiklander  * function call.
3566e954a6eSJens Wiklander  */
3576e954a6eSJens Wiklander TEE_Result crypto_rng_read(void *buf, size_t len);
358e1770e71SJens Wiklander 
359e7dbc357SSummer Qin /*
360e7dbc357SSummer Qin  * crypto_aes_expand_enc_key() - Expand an AES key
361e7dbc357SSummer Qin  * @key:	AES key buffer
3624746d394SMarkus S. Wamser  * @key_len:	Size of the @key buffer in bytes
363e7dbc357SSummer Qin  * @enc_key:	Expanded AES encryption key buffer
364e7dbc357SSummer Qin  * @enc_keylen: Size of the @enc_key buffer in bytes
365e7dbc357SSummer Qin  * @rounds:	Number of rounds to be used during encryption
366e7dbc357SSummer Qin  */
3670d360202SJens Wiklander TEE_Result crypto_aes_expand_enc_key(const void *key, size_t key_len,
368e7dbc357SSummer Qin 				     void *enc_key, size_t enc_keylen,
369e7dbc357SSummer Qin 				     unsigned int *rounds);
370e7dbc357SSummer Qin 
371e7dbc357SSummer Qin /*
372e7dbc357SSummer Qin  * crypto_aes_enc_block() - Encrypt an AES block
373e7dbc357SSummer Qin  * @enc_key:	Expanded AES encryption key
374e7dbc357SSummer Qin  * @enc_keylen:	Size of @enc_key in bytes
375e7dbc357SSummer Qin  * @rounds:	Number of rounds
376e7dbc357SSummer Qin  * @src:	Source buffer of one AES block (16 bytes)
377e7dbc357SSummer Qin  * @dst:	Destination buffer of one AES block (16 bytes)
378e7dbc357SSummer Qin  */
379e7dbc357SSummer Qin void crypto_aes_enc_block(const void *enc_key, size_t enc_keylen,
380e7dbc357SSummer Qin 			  unsigned int rounds, const void *src, void *dst);
3810d360202SJens Wiklander 
382e1770e71SJens Wiklander #endif /* __CRYPTO_CRYPTO_H */
383