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