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 #ifdef _CFG_CORE_LTC_SM2_DSA 57 TEE_Result sm2_ltc_dsa_sign(uint32_t algo, struct ecc_keypair *key, 58 const uint8_t *msg, size_t msg_len, uint8_t *sig, 59 size_t *sig_len); 60 61 TEE_Result sm2_ltc_dsa_verify(uint32_t algo, struct ecc_public_key *key, 62 const uint8_t *msg, size_t msg_len, 63 const uint8_t *sig, size_t sig_len); 64 #else 65 static inline TEE_Result 66 sm2_ltc_dsa_sign(uint32_t algo __unused, struct ecc_keypair *key __unused, 67 const uint8_t *msg __unused, size_t msg_len __unused, 68 uint8_t *sig __unused, size_t *sig_len __unused) 69 { 70 return TEE_ERROR_NOT_IMPLEMENTED; 71 } 72 73 static inline TEE_Result 74 sm2_ltc_dsa_verify(uint32_t algo __unused, struct ecc_public_key *key __unused, 75 const uint8_t *msg __unused, size_t msg_len __unused, 76 const uint8_t *sig __unused, size_t sig_len __unused) 77 { 78 return TEE_ERROR_NOT_IMPLEMENTED; 79 } 80 #endif 81 82 #ifdef _CFG_CORE_LTC_SM2_PKE 83 TEE_Result sm2_ltc_pke_decrypt(struct ecc_keypair *key, const uint8_t *src, 84 size_t src_len, uint8_t *dst, size_t *dst_len); 85 86 TEE_Result sm2_ltc_pke_encrypt(struct ecc_public_key *key, const uint8_t *src, 87 size_t src_len, uint8_t *dst, size_t *dst_len); 88 89 #else 90 static inline TEE_Result sm2_ltc_pke_decrypt(struct ecc_keypair *key __unused, 91 const uint8_t *src __unused, 92 size_t src_len __unused, 93 uint8_t *dst __unused, 94 size_t *dst_len __unused) 95 { 96 return TEE_ERROR_NOT_IMPLEMENTED; 97 } 98 99 static inline TEE_Result 100 sm2_ltc_pke_encrypt(struct ecc_public_key *key __unused, 101 const uint8_t *src __unused, size_t src_len __unused, 102 uint8_t *dst __unused, size_t *dst_len __unused) 103 { 104 return TEE_ERROR_NOT_IMPLEMENTED; 105 } 106 #endif 107 #endif /* ACIPHER_HELPERS_H */ 108