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