1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014-2019, Linaro Limited 4 */ 5 6 #ifndef ACIPHER_HELPERS_H 7 #define ACIPHER_HELPERS_H 8 9 #include <crypto/crypto.h> 10 #include <tee_api_defines.h> 11 #include <tee_api_types.h> 12 #include <tomcrypt_private.h> 13 #include <types_ext.h> 14 15 static inline bool bn_alloc_max(struct bignum **s) 16 { 17 *s = crypto_bignum_allocate(_CFG_CORE_LTC_BIGNUM_MAX_BITS); 18 19 return *s; 20 } 21 22 static inline TEE_Result convert_ltc_verify_status(int ltc_res, int ltc_stat) 23 { 24 switch (ltc_res) { 25 case CRYPT_OK: 26 if (ltc_stat == 1) 27 return TEE_SUCCESS; 28 else 29 return TEE_ERROR_SIGNATURE_INVALID; 30 case CRYPT_INVALID_PACKET: 31 case CRYPT_PK_INVALID_SIZE: 32 return TEE_ERROR_SIGNATURE_INVALID; 33 default: 34 return TEE_ERROR_GENERIC; 35 } 36 } 37 38 #ifdef CFG_CRYPTOLIB_NAME_tomcrypt 39 TEE_Result ecc_populate_ltc_private_key(ecc_key *ltc_key, 40 struct ecc_keypair *key, 41 uint32_t algo, size_t *key_size_bytes); 42 TEE_Result ecc_populate_ltc_public_key(ecc_key *ltc_key, 43 struct ecc_public_key *key, 44 uint32_t algo, size_t *key_size_bytes); 45 #endif 46 47 /* Write bignum to fixed size buffer in big endian order */ 48 #define mp_to_unsigned_bin2(a, b, c) \ 49 do { \ 50 void *_a = (a); \ 51 mp_to_unsigned_bin(_a, (b) + (c) - mp_unsigned_bin_size(_a)); \ 52 } while(0) 53 54 TEE_Result sm2_kdf(const uint8_t *Z, size_t Z_len, uint8_t *t, size_t tlen); 55 56 #endif /* ACIPHER_HELPERS_H */ 57