1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014-2017, Linaro Limited 4 */ 5 6 /* 7 * This is the Cryptographic Provider API (CP API). 8 * 9 * This defines how most crypto syscalls that implement the Cryptographic 10 * Operations API can invoke the actual providers of cryptographic algorithms 11 * (such as LibTomCrypt). 12 * 13 * To add a new provider, you need to provide an implementation of this 14 * interface. 15 * 16 * The following parameters are commonly used. 17 * 18 * @ctx: context allocated by the syscall, for later use by the algorithm 19 * @algo: algorithm identifier (TEE_ALG_*) 20 */ 21 22 #ifndef __CRYPTO_CRYPTO_H 23 #define __CRYPTO_CRYPTO_H 24 25 #include <tee_api_types.h> 26 27 TEE_Result crypto_init(void); 28 29 /* Message digest functions */ 30 TEE_Result crypto_hash_alloc_ctx(void **ctx, uint32_t algo); 31 TEE_Result crypto_hash_init(void *ctx); 32 TEE_Result crypto_hash_update(void *ctx, const uint8_t *data, size_t len); 33 TEE_Result crypto_hash_final(void *ctx, uint8_t *digest, size_t len); 34 void crypto_hash_free_ctx(void *ctx); 35 void crypto_hash_copy_state(void *dst_ctx, void *src_ctx); 36 37 /* Symmetric ciphers */ 38 TEE_Result crypto_cipher_alloc_ctx(void **ctx, uint32_t algo); 39 TEE_Result crypto_cipher_init(void *ctx, TEE_OperationMode mode, 40 const uint8_t *key1, size_t key1_len, 41 const uint8_t *key2, size_t key2_len, 42 const uint8_t *iv, size_t iv_len); 43 TEE_Result crypto_cipher_update(void *ctx, TEE_OperationMode mode, 44 bool last_block, const uint8_t *data, 45 size_t len, uint8_t *dst); 46 void crypto_cipher_final(void *ctx); 47 TEE_Result crypto_cipher_get_block_size(uint32_t algo, size_t *size); 48 void crypto_cipher_free_ctx(void *ctx); 49 void crypto_cipher_copy_state(void *dst_ctx, void *src_ctx); 50 51 /* Message Authentication Code functions */ 52 TEE_Result crypto_mac_alloc_ctx(void **ctx, uint32_t algo); 53 TEE_Result crypto_mac_init(void *ctx, const uint8_t *key, size_t len); 54 TEE_Result crypto_mac_update(void *ctx, const uint8_t *data, size_t len); 55 TEE_Result crypto_mac_final(void *ctx, uint8_t *digest, size_t digest_len); 56 void crypto_mac_free_ctx(void *ctx); 57 void crypto_mac_copy_state(void *dst_ctx, void *src_ctx); 58 59 /* Authenticated encryption */ 60 TEE_Result crypto_authenc_alloc_ctx(void **ctx, uint32_t algo); 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 void crypto_authenc_free_ctx(void *ctx, uint32_t algo); 84 void crypto_authenc_copy_state(void *dst_ctx, void *src_ctx, uint32_t algo); 85 86 /* Implementation-defined big numbers */ 87 88 /* 89 * Allocate a bignum capable of holding an unsigned integer value of 90 * up to bitsize bits 91 */ 92 struct bignum *crypto_bignum_allocate(size_t size_bits); 93 TEE_Result crypto_bignum_bin2bn(const uint8_t *from, size_t fromsize, 94 struct bignum *to); 95 size_t crypto_bignum_num_bytes(struct bignum *a); 96 size_t crypto_bignum_num_bits(struct bignum *a); 97 void crypto_bignum_bn2bin(const struct bignum *from, uint8_t *to); 98 void crypto_bignum_copy(struct bignum *to, const struct bignum *from); 99 void crypto_bignum_free(struct bignum *a); 100 void crypto_bignum_clear(struct bignum *a); 101 102 /* return -1 if a<b, 0 if a==b, +1 if a>b */ 103 int32_t crypto_bignum_compare(struct bignum *a, struct bignum *b); 104 105 /* Asymmetric algorithms */ 106 107 struct rsa_keypair { 108 struct bignum *e; /* Public exponent */ 109 struct bignum *d; /* Private exponent */ 110 struct bignum *n; /* Modulus */ 111 112 /* Optional CRT parameters (all NULL if unused) */ 113 struct bignum *p; /* N = pq */ 114 struct bignum *q; 115 struct bignum *qp; /* 1/q mod p */ 116 struct bignum *dp; /* d mod (p-1) */ 117 struct bignum *dq; /* d mod (q-1) */ 118 }; 119 120 struct rsa_public_key { 121 struct bignum *e; /* Public exponent */ 122 struct bignum *n; /* Modulus */ 123 }; 124 125 struct dsa_keypair { 126 struct bignum *g; /* Generator of subgroup (public) */ 127 struct bignum *p; /* Prime number (public) */ 128 struct bignum *q; /* Order of subgroup (public) */ 129 struct bignum *y; /* Public key */ 130 struct bignum *x; /* Private key */ 131 }; 132 133 struct dsa_public_key { 134 struct bignum *g; /* Generator of subgroup (public) */ 135 struct bignum *p; /* Prime number (public) */ 136 struct bignum *q; /* Order of subgroup (public) */ 137 struct bignum *y; /* Public key */ 138 }; 139 140 struct dh_keypair { 141 struct bignum *g; /* Generator of Z_p (shared) */ 142 struct bignum *p; /* Prime modulus (shared) */ 143 struct bignum *x; /* Private key */ 144 struct bignum *y; /* Public key y = g^x */ 145 146 /* 147 * Optional parameters used by key generation. 148 * When not used, q == NULL and xbits == 0 149 */ 150 struct bignum *q; /* x must be in the range [2, q-2] */ 151 uint32_t xbits; /* Number of bits in the private key */ 152 }; 153 154 struct ecc_public_key { 155 struct bignum *x; /* Public value x */ 156 struct bignum *y; /* Public value y */ 157 uint32_t curve; /* Curve type */ 158 }; 159 160 struct ecc_keypair { 161 struct bignum *d; /* Private value */ 162 struct bignum *x; /* Public value x */ 163 struct bignum *y; /* Public value y */ 164 uint32_t curve; /* Curve type */ 165 }; 166 167 /* 168 * Key allocation functions 169 * Allocate the bignum's inside a key structure. 170 * TEE core will later use crypto_bignum_free(). 171 */ 172 TEE_Result crypto_acipher_alloc_rsa_keypair(struct rsa_keypair *s, 173 size_t key_size_bits); 174 TEE_Result crypto_acipher_alloc_rsa_public_key(struct rsa_public_key *s, 175 size_t key_size_bits); 176 void crypto_acipher_free_rsa_public_key(struct rsa_public_key *s); 177 TEE_Result crypto_acipher_alloc_dsa_keypair(struct dsa_keypair *s, 178 size_t key_size_bits); 179 TEE_Result crypto_acipher_alloc_dsa_public_key(struct dsa_public_key *s, 180 size_t key_size_bits); 181 TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s, 182 size_t key_size_bits); 183 TEE_Result crypto_acipher_alloc_ecc_public_key(struct ecc_public_key *s, 184 size_t key_size_bits); 185 TEE_Result crypto_acipher_alloc_ecc_keypair(struct ecc_keypair *s, 186 size_t key_size_bits); 187 void crypto_acipher_free_ecc_public_key(struct ecc_public_key *s); 188 189 /* 190 * Key generation functions 191 */ 192 TEE_Result crypto_acipher_gen_rsa_key(struct rsa_keypair *key, size_t key_size); 193 TEE_Result crypto_acipher_gen_dsa_key(struct dsa_keypair *key, size_t key_size); 194 TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key, struct bignum *q, 195 size_t xbits); 196 TEE_Result crypto_acipher_gen_ecc_key(struct ecc_keypair *key); 197 198 TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key, 199 struct bignum *public_key, 200 struct bignum *secret); 201 202 TEE_Result crypto_acipher_rsanopad_decrypt(struct rsa_keypair *key, 203 const uint8_t *src, size_t src_len, 204 uint8_t *dst, size_t *dst_len); 205 TEE_Result crypto_acipher_rsanopad_encrypt(struct rsa_public_key *key, 206 const uint8_t *src, size_t src_len, 207 uint8_t *dst, size_t *dst_len); 208 TEE_Result crypto_acipher_rsaes_decrypt(uint32_t algo, struct rsa_keypair *key, 209 const uint8_t *label, size_t label_len, 210 const uint8_t *src, size_t src_len, 211 uint8_t *dst, size_t *dst_len); 212 TEE_Result crypto_acipher_rsaes_encrypt(uint32_t algo, 213 struct rsa_public_key *key, 214 const uint8_t *label, size_t label_len, 215 const uint8_t *src, size_t src_len, 216 uint8_t *dst, size_t *dst_len); 217 /* RSA SSA sign/verify: if salt_len == -1, use default value */ 218 TEE_Result crypto_acipher_rsassa_sign(uint32_t algo, struct rsa_keypair *key, 219 int salt_len, const uint8_t *msg, 220 size_t msg_len, uint8_t *sig, 221 size_t *sig_len); 222 TEE_Result crypto_acipher_rsassa_verify(uint32_t algo, 223 struct rsa_public_key *key, 224 int salt_len, const uint8_t *msg, 225 size_t msg_len, const uint8_t *sig, 226 size_t sig_len); 227 TEE_Result crypto_acipher_dsa_sign(uint32_t algo, struct dsa_keypair *key, 228 const uint8_t *msg, size_t msg_len, 229 uint8_t *sig, size_t *sig_len); 230 TEE_Result crypto_acipher_dsa_verify(uint32_t algo, struct dsa_public_key *key, 231 const uint8_t *msg, size_t msg_len, 232 const uint8_t *sig, size_t sig_len); 233 TEE_Result crypto_acipher_ecc_sign(uint32_t algo, struct ecc_keypair *key, 234 const uint8_t *msg, size_t msg_len, 235 uint8_t *sig, size_t *sig_len); 236 TEE_Result crypto_acipher_ecc_verify(uint32_t algo, struct ecc_public_key *key, 237 const uint8_t *msg, size_t msg_len, 238 const uint8_t *sig, size_t sig_len); 239 TEE_Result crypto_acipher_ecc_shared_secret(struct ecc_keypair *private_key, 240 struct ecc_public_key *public_key, 241 void *secret, 242 unsigned long *secret_len); 243 244 /* 245 * Verifies a SHA-256 hash, doesn't require crypto_init() to be called in 246 * advance and has as few dependencies as possible. 247 * 248 * This function is primarily used by pager and early initialization code 249 * where the complete crypto library isn't available. 250 */ 251 TEE_Result hash_sha256_check(const uint8_t *hash, const uint8_t *data, 252 size_t data_size); 253 254 /* 255 * Computes a SHA-512/256 hash, vetted conditioner as per NIST.SP.800-90B. 256 * It doesn't require crypto_init() to be called in advance and has as few 257 * dependencies as possible. 258 * 259 * This function could be used inside interrupt context where the crypto 260 * library can't be used due to mutex handling. 261 */ 262 TEE_Result hash_sha512_256_compute(uint8_t *digest, const uint8_t *data, 263 size_t data_size); 264 265 #define CRYPTO_RNG_SRC_IS_QUICK(sid) (!!((sid) & 1)) 266 267 /* 268 * enum crypto_rng_src - RNG entropy source 269 * 270 * Identifiers for different RNG entropy sources. The lowest bit indicates 271 * if the source is to be merely queued (bit is 1) or if it's delivered 272 * directly to the pool. The difference is that in the latter case RPC to 273 * normal world can be performed and in the former it must not. 274 */ 275 enum crypto_rng_src { 276 CRYPTO_RNG_SRC_JITTER_SESSION = (0 << 1 | 0), 277 CRYPTO_RNG_SRC_JITTER_RPC = (1 << 1 | 1), 278 CRYPTO_RNG_SRC_NONSECURE = (1 << 1 | 0), 279 }; 280 281 /* 282 * crypto_rng_init() - initialize the RNG 283 * @data: buffer with initial seed 284 * @dlen: length of @data 285 */ 286 TEE_Result crypto_rng_init(const void *data, size_t dlen); 287 288 /* 289 * crypto_rng_add_event() - supply entropy to RNG from a source 290 * @sid: Source identifier, should be unique for a specific source 291 * @pnum: Pool number, acquired using crypto_rng_get_next_pool_num() 292 * @data: Data associated with the event 293 * @dlen: Length of @data 294 * 295 * @sid controls whether the event is merly queued in a ring buffer or if 296 * it's added to one of the pools directly. If CRYPTO_RNG_SRC_IS_QUICK() is 297 * true (lowest bit set) events are queue otherwise added to corresponding 298 * pool. If CRYPTO_RNG_SRC_IS_QUICK() is false, eventual queued events are 299 * added to their queues too. 300 */ 301 void crypto_rng_add_event(enum crypto_rng_src sid, unsigned int *pnum, 302 const void *data, size_t dlen); 303 304 /* 305 * crypto_rng_read() - read cryptograhically secure RNG 306 * @buf: Buffer to hold the data 307 * @len: Length of buffer. 308 * 309 * Eventual queued events are also added to their pools during this 310 * function call. 311 */ 312 TEE_Result crypto_rng_read(void *buf, size_t len); 313 314 /* 315 * crypto_aes_expand_enc_key() - Expand an AES key 316 * @key: AES key buffer 317 * @key_len: Size of the the @key buffer in bytes 318 * @enc_key: Expanded AES encryption key buffer 319 * @enc_keylen: Size of the @enc_key buffer in bytes 320 * @rounds: Number of rounds to be used during encryption 321 */ 322 TEE_Result crypto_aes_expand_enc_key(const void *key, size_t key_len, 323 void *enc_key, size_t enc_keylen, 324 unsigned int *rounds); 325 326 /* 327 * crypto_aes_enc_block() - Encrypt an AES block 328 * @enc_key: Expanded AES encryption key 329 * @enc_keylen: Size of @enc_key in bytes 330 * @rounds: Number of rounds 331 * @src: Source buffer of one AES block (16 bytes) 332 * @dst: Destination buffer of one AES block (16 bytes) 333 */ 334 void crypto_aes_enc_block(const void *enc_key, size_t enc_keylen, 335 unsigned int rounds, const void *src, void *dst); 336 337 #endif /* __CRYPTO_CRYPTO_H */ 338