1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2019, Linaro Limited 4 * Copyright (c) 2021, SumUp Services GmbH 5 */ 6 7 #ifndef __CRYPTO_CRYPTO_IMPL_H 8 #define __CRYPTO_CRYPTO_IMPL_H 9 10 #include <crypto/crypto.h> 11 #include <tee_api_types.h> 12 13 /* 14 * The crypto context used by the crypto_hash_*() functions is defined by 15 * struct crypto_hash_ctx. 16 */ 17 struct crypto_hash_ctx { 18 const struct crypto_hash_ops *ops; 19 }; 20 21 struct crypto_hash_ops { 22 TEE_Result (*init)(struct crypto_hash_ctx *ctx); 23 TEE_Result (*update)(struct crypto_hash_ctx *ctx, const uint8_t *data, 24 size_t len); 25 TEE_Result (*final)(struct crypto_hash_ctx *ctx, uint8_t *digest, 26 size_t len); 27 void (*free_ctx)(struct crypto_hash_ctx *ctx); 28 void (*copy_state)(struct crypto_hash_ctx *dst_ctx, 29 struct crypto_hash_ctx *src_ctx); 30 }; 31 32 #define CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(name, type) \ 33 static inline TEE_Result \ 34 crypto_##name##_alloc_ctx(struct crypto_##type##_ctx **ctx __unused) \ 35 { return TEE_ERROR_NOT_IMPLEMENTED; } 36 37 #if defined(CFG_CRYPTO_MD5) 38 TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx); 39 #else 40 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(md5, hash) 41 #endif 42 43 #if defined(CFG_CRYPTO_SHA1) 44 TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx); 45 #else 46 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha1, hash) 47 #endif 48 49 #if defined(CFG_CRYPTO_SHA224) 50 TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx); 51 #else 52 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha224, hash) 53 #endif 54 55 #if defined(CFG_CRYPTO_SHA256) 56 TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx); 57 #else 58 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha256, hash) 59 #endif 60 61 #if defined(CFG_CRYPTO_SHA384) 62 TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx); 63 #else 64 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha384, hash) 65 #endif 66 67 #if defined(CFG_CRYPTO_SHA512) 68 TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx); 69 #else 70 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha512, hash) 71 #endif 72 73 #if defined(CFG_CRYPTO_SM3) 74 TEE_Result crypto_sm3_alloc_ctx(struct crypto_hash_ctx **ctx); 75 #else 76 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm3, hash) 77 #endif 78 79 /* 80 * The crypto context used by the crypto_mac_*() functions is defined by 81 * struct crypto_mac_ctx. 82 */ 83 struct crypto_mac_ctx { 84 const struct crypto_mac_ops *ops; 85 }; 86 87 struct crypto_mac_ops { 88 TEE_Result (*init)(struct crypto_mac_ctx *ctx, const uint8_t *key, 89 size_t len); 90 TEE_Result (*update)(struct crypto_mac_ctx *ctx, const uint8_t *data, 91 size_t len); 92 TEE_Result (*final)(struct crypto_mac_ctx *ctx, uint8_t *digest, 93 size_t len); 94 void (*free_ctx)(struct crypto_mac_ctx *ctx); 95 void (*copy_state)(struct crypto_mac_ctx *dst_ctx, 96 struct crypto_mac_ctx *src_ctx); 97 }; 98 99 #if defined(CFG_CRYPTO_HMAC) 100 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx); 101 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx); 102 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx); 103 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx); 104 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx); 105 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx); 106 #else 107 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_md5, mac) 108 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha1, mac) 109 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha224, mac) 110 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha256, mac) 111 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha384, mac) 112 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha512, mac) 113 #endif 114 115 #if defined(CFG_CRYPTO_SM3) && defined(CFG_CRYPTO_HMAC) 116 TEE_Result crypto_hmac_sm3_alloc_ctx(struct crypto_mac_ctx **ctx); 117 #else 118 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sm3, mac) 119 #endif 120 121 #if defined(CFG_CRYPTO_CBC_MAC) 122 TEE_Result crypto_aes_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx); 123 TEE_Result crypto_aes_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx); 124 TEE_Result crypto_des_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx); 125 TEE_Result crypto_des_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx); 126 TEE_Result crypto_des3_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx); 127 TEE_Result crypto_des3_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx); 128 #else 129 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_nopad, mac) 130 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_pkcs5, mac) 131 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_nopad, mac) 132 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_pkcs5, mac) 133 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_nopad, mac) 134 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_pkcs5, mac) 135 #endif 136 137 #if defined(CFG_CRYPTO_CMAC) 138 TEE_Result crypto_aes_cmac_alloc_ctx(struct crypto_mac_ctx **ctx); 139 TEE_Result crypto_des3_cmac_alloc_ctx(struct crypto_mac_ctx **ctx); 140 #else 141 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cmac, mac) 142 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cmac, mac) 143 #endif 144 145 /* 146 * The crypto context used by the crypto_cipher_*() functions is defined by 147 * struct crypto_cipher_ctx. 148 */ 149 struct crypto_cipher_ctx { 150 const struct crypto_cipher_ops *ops; 151 }; 152 153 struct crypto_cipher_ops { 154 TEE_Result (*init)(struct crypto_cipher_ctx *ctx, 155 TEE_OperationMode mode, 156 const uint8_t *key1, size_t key1_len, 157 const uint8_t *key2, size_t key2_len, 158 const uint8_t *iv, size_t iv_len); 159 TEE_Result (*update)(struct crypto_cipher_ctx *ctx, bool last_block, 160 const uint8_t *data, size_t len, uint8_t *dst); 161 void (*final)(struct crypto_cipher_ctx *ctx); 162 163 void (*free_ctx)(struct crypto_cipher_ctx *ctx); 164 void (*copy_state)(struct crypto_cipher_ctx *dst_ctx, 165 struct crypto_cipher_ctx *src_ctx); 166 }; 167 168 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_ECB) 169 TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 170 #else 171 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ecb, cipher) 172 #endif 173 174 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CBC) 175 TEE_Result crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 176 #else 177 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc, cipher) 178 #endif 179 180 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTR) 181 TEE_Result crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx); 182 #else 183 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ctr, cipher) 184 #endif 185 186 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTS) 187 TEE_Result crypto_aes_cts_alloc_ctx(struct crypto_cipher_ctx **ctx); 188 #else 189 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cts, cipher) 190 #endif 191 192 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_XTS) 193 TEE_Result crypto_aes_xts_alloc_ctx(struct crypto_cipher_ctx **ctx); 194 #else 195 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_xts, cipher) 196 #endif 197 198 #if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_ECB) 199 TEE_Result crypto_des_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 200 TEE_Result crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 201 #else 202 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_ecb, cipher) 203 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_ecb, cipher) 204 #endif 205 206 #if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_CBC) 207 TEE_Result crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 208 TEE_Result crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 209 #else 210 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc, cipher) 211 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc, cipher) 212 #endif 213 214 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_ECB) 215 TEE_Result crypto_sm4_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 216 #else 217 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_ecb, cipher) 218 #endif 219 220 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_CBC) 221 TEE_Result crypto_sm4_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 222 #else 223 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_cbc, cipher) 224 #endif 225 226 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_CTR) 227 TEE_Result crypto_sm4_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx); 228 #else 229 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_ctr, cipher) 230 #endif 231 232 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_XTS) 233 TEE_Result crypto_sm4_xts_alloc_ctx(struct crypto_cipher_ctx **ctx); 234 #else 235 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_xts, cipher) 236 #endif 237 238 /* 239 * The crypto context used by the crypto_authen_*() functions below is 240 * defined by struct crypto_authenc_ctx. 241 */ 242 struct crypto_authenc_ctx { 243 const struct crypto_authenc_ops *ops; 244 }; 245 246 struct crypto_authenc_ops { 247 TEE_Result (*init)(struct crypto_authenc_ctx *ctx, 248 TEE_OperationMode mode, 249 const uint8_t *key, size_t key_len, 250 const uint8_t *nonce, size_t nonce_len, 251 size_t tag_len, size_t aad_len, 252 size_t payload_len); 253 TEE_Result (*update_aad)(struct crypto_authenc_ctx *ctx, 254 const uint8_t *data, size_t len); 255 TEE_Result (*update_payload)(struct crypto_authenc_ctx *ctx, 256 TEE_OperationMode mode, 257 const uint8_t *src_data, size_t len, 258 uint8_t *dst_data); 259 TEE_Result (*enc_final)(struct crypto_authenc_ctx *ctx, 260 const uint8_t *src_data, size_t len, 261 uint8_t *dst_data, uint8_t *dst_tag, 262 size_t *dst_tag_len); 263 TEE_Result (*dec_final)(struct crypto_authenc_ctx *ctx, 264 const uint8_t *src_data, size_t len, 265 uint8_t *dst_data, const uint8_t *tag, 266 size_t tag_len); 267 void (*final)(struct crypto_authenc_ctx *ctx); 268 void (*free_ctx)(struct crypto_authenc_ctx *ctx); 269 void (*copy_state)(struct crypto_authenc_ctx *dst_ctx, 270 struct crypto_authenc_ctx *src_ctx); 271 }; 272 273 TEE_Result crypto_aes_ccm_alloc_ctx(struct crypto_authenc_ctx **ctx); 274 TEE_Result crypto_aes_gcm_alloc_ctx(struct crypto_authenc_ctx **ctx); 275 276 #ifdef CFG_CRYPTO_DRV_HASH 277 TEE_Result drvcrypt_hash_alloc_ctx(struct crypto_hash_ctx **ctx, uint32_t algo); 278 #else 279 static inline TEE_Result 280 drvcrypt_hash_alloc_ctx(struct crypto_hash_ctx **ctx __unused, 281 uint32_t algo __unused) 282 { 283 return TEE_ERROR_NOT_IMPLEMENTED; 284 } 285 #endif /* CFG_CRYPTO_DRV_HASH */ 286 287 #ifdef CFG_CRYPTO_DRV_CIPHER 288 TEE_Result drvcrypt_cipher_alloc_ctx(struct crypto_cipher_ctx **ctx, 289 uint32_t algo); 290 #else 291 static inline TEE_Result 292 drvcrypt_cipher_alloc_ctx(struct crypto_cipher_ctx **ctx __unused, 293 uint32_t algo __unused) 294 { 295 return TEE_ERROR_NOT_IMPLEMENTED; 296 } 297 #endif /* CFG_CRYPTO_DRV_CIPHER */ 298 299 #ifdef CFG_CRYPTO_DRV_MAC 300 /* Cryptographic MAC driver context allocation */ 301 TEE_Result drvcrypt_mac_alloc_ctx(struct crypto_mac_ctx **ctx, uint32_t algo); 302 #else 303 static inline TEE_Result 304 drvcrypt_mac_alloc_ctx(struct crypto_mac_ctx **ctx __unused, 305 uint32_t algo __unused) 306 { 307 return TEE_ERROR_NOT_IMPLEMENTED; 308 } 309 #endif /* CFG_CRYPTO_DRV_MAC */ 310 311 #ifdef CFG_CRYPTO_DRV_AUTHENC 312 /* Cryptographic Authenticated Encryption driver context allocation */ 313 TEE_Result drvcrypt_authenc_alloc_ctx(struct crypto_authenc_ctx **ctx, 314 uint32_t algo); 315 #else 316 static inline TEE_Result 317 drvcrypt_authenc_alloc_ctx(struct crypto_authenc_ctx **ctx __unused, 318 uint32_t algo __unused) 319 { 320 return TEE_ERROR_NOT_IMPLEMENTED; 321 } 322 #endif /* CFG_CRYPTO_DRV_AUTHENC */ 323 /* 324 * The ECC public key operations used by the crypto_acipher_ecc_*() and 325 * crypto_acipher_free_ecc_*() functions. 326 * Reference set in ecc_public_key when key allocated. 327 * 328 * @free is mandatory 329 * @verify is optional 330 * @encrypt is optional 331 */ 332 struct crypto_ecc_public_ops { 333 void (*free)(struct ecc_public_key *key); 334 TEE_Result (*verify)(uint32_t algo, struct ecc_public_key *key, 335 const uint8_t *msg, size_t msg_len, 336 const uint8_t *sig, size_t sig_len); 337 TEE_Result (*encrypt)(struct ecc_public_key *key, const uint8_t *src, 338 size_t src_len, uint8_t *dst, size_t *dst_len); 339 }; 340 341 /* 342 * The ECC keypair operations used by the crypto_acipher_ecc_*() and 343 * crypto_acipher_gen_ecc_*() functions. 344 * Reference set in ecc_keypair when key allocated. 345 * 346 * @generate is mandatory 347 * @sign is optional 348 * @shared_secret is optional 349 * @decrypt is optional 350 */ 351 struct crypto_ecc_keypair_ops { 352 TEE_Result (*generate)(struct ecc_keypair *key, size_t key_size_bits); 353 TEE_Result (*sign)(uint32_t algo, struct ecc_keypair *key, 354 const uint8_t *msg, size_t msg_len, uint8_t *sig, 355 size_t *sig_len); 356 TEE_Result (*shared_secret)(struct ecc_keypair *private_key, 357 struct ecc_public_key *public_key, 358 void *secret, unsigned long *secret_len); 359 TEE_Result (*decrypt)(struct ecc_keypair *key, const uint8_t *src, 360 size_t src_len, uint8_t *dst, size_t *dst_len); 361 }; 362 363 #ifdef CFG_CRYPTO_ECC 364 const struct crypto_ecc_keypair_ops * 365 crypto_asym_get_ecc_keypair_ops(uint32_t key_type); 366 367 const struct crypto_ecc_public_ops * 368 crypto_asym_get_ecc_public_ops(uint32_t key_type); 369 370 TEE_Result crypto_asym_alloc_ecc_public_key(struct ecc_public_key *key, 371 uint32_t key_type, 372 size_t key_size_bits); 373 TEE_Result crypto_asym_alloc_ecc_keypair(struct ecc_keypair *key, 374 uint32_t key_type, 375 size_t key_size_bits); 376 #else 377 static inline TEE_Result 378 crypto_asym_alloc_ecc_public_key(struct ecc_public_key *key __unused, 379 uint32_t key_type __unused, 380 size_t key_size_bits __unused) 381 { 382 return TEE_ERROR_NOT_IMPLEMENTED; 383 } 384 385 static inline const struct crypto_ecc_keypair_ops * 386 crypto_asym_get_keypair_ops(uint32_t key_type __unused) 387 { 388 return NULL; 389 } 390 391 static inline const struct crypto_ecc_public_ops * 392 crypto_asym_get_ecc_public_ops(uint32_t key_type __unused) 393 { 394 return NULL; 395 } 396 397 static inline TEE_Result 398 crypto_asym_alloc_ecc_keypair(struct ecc_keypair *key __unused, 399 uint32_t key_type __unused, 400 size_t key_size_bits __unused) 401 { 402 return TEE_ERROR_NOT_IMPLEMENTED; 403 } 404 #endif /* CFG_CRYPTO_ECC */ 405 406 #ifdef CFG_CRYPTO_DRV_ECC 407 TEE_Result drvcrypt_asym_alloc_ecc_public_key(struct ecc_public_key *key, 408 uint32_t key_type, 409 size_t key_size_bits); 410 TEE_Result drvcrypt_asym_alloc_ecc_keypair(struct ecc_keypair *key, 411 uint32_t key_type, 412 size_t key_size_bits); 413 #else 414 static inline TEE_Result 415 drvcrypt_asym_alloc_ecc_public_key(struct ecc_public_key *key __unused, 416 uint32_t key_type __unused, 417 size_t key_size_bits __unused) 418 { 419 return TEE_ERROR_NOT_IMPLEMENTED; 420 } 421 422 static inline TEE_Result 423 drvcrypt_asym_alloc_ecc_keypair(struct ecc_keypair *key __unused, 424 uint32_t key_type __unused, 425 size_t key_size_bits __unused) 426 { 427 return TEE_ERROR_NOT_IMPLEMENTED; 428 } 429 #endif /* CFG_CRYPTO_DRV_ECC */ 430 431 TEE_Result sw_crypto_acipher_alloc_rsa_keypair(struct rsa_keypair *s, 432 size_t key_size_bits); 433 434 TEE_Result sw_crypto_acipher_alloc_rsa_public_key(struct rsa_public_key *s, 435 size_t key_size_bits); 436 437 void sw_crypto_acipher_free_rsa_public_key(struct rsa_public_key *s); 438 439 void sw_crypto_acipher_free_rsa_keypair(struct rsa_keypair *s); 440 441 TEE_Result sw_crypto_acipher_gen_rsa_key(struct rsa_keypair *key, 442 size_t key_size); 443 444 TEE_Result sw_crypto_acipher_rsanopad_decrypt(struct rsa_keypair *key, 445 const uint8_t *src, 446 size_t src_len, uint8_t *dst, 447 size_t *dst_len); 448 TEE_Result sw_crypto_acipher_rsanopad_encrypt(struct rsa_public_key *key, 449 const uint8_t *src, 450 size_t src_len, uint8_t *dst, 451 size_t *dst_len); 452 TEE_Result sw_crypto_acipher_rsaes_decrypt(uint32_t algo, 453 struct rsa_keypair *key, 454 const uint8_t *label, 455 size_t label_len, const uint8_t *src, 456 size_t src_len, uint8_t *dst, 457 size_t *dst_len); 458 459 TEE_Result sw_crypto_acipher_rsaes_encrypt(uint32_t algo, 460 struct rsa_public_key *key, 461 const uint8_t *label, 462 size_t label_len, const uint8_t *src, 463 size_t src_len, uint8_t *dst, 464 size_t *dst_len); 465 466 TEE_Result sw_crypto_acipher_rsassa_sign(uint32_t algo, struct rsa_keypair *key, 467 int salt_len, const uint8_t *msg, 468 size_t msg_len, uint8_t *sig, 469 size_t *sig_len); 470 471 TEE_Result sw_crypto_acipher_rsassa_verify(uint32_t algo, 472 struct rsa_public_key *key, 473 int salt_len, const uint8_t *msg, 474 size_t msg_len, const uint8_t *sig, 475 size_t sig_len); 476 #endif /*__CRYPTO_CRYPTO_IMPL_H*/ 477