1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #ifndef HW_ECC_H_
19 #define HW_ECC_H_
20 
21 #include "algorithm/ecc/hw_ecc.h"
22 #include "algorithm/ecc/ecc_curve.h"
23 
24 #if ( (MCU_CORE_TYPE == MCU_CORE_827x) || (MCU_CORE_TYPE == MCU_CORE_9518) )
25 
26 /* hECC_RNG_Function type
27 The RNG function should fill 'size' random bytes into 'dest'. It should return 1 if
28 'dest' was filled with random data, or 0 if the random data could not be generated.
29 The filled-in values should be either truly random, or from a cryptographically-secure PRNG.
30 A correctly functioning RNG function must be set (using hECC_set_rng()) before calling
31 hECC_make_key(). */
32 
33 typedef int (*hECC_rng_func)(unsigned char *dest, unsigned size);
34 
35 
36 /**
37  * @brief		The function that will be used to generate random bytes.
38  * @param[in]	resister predefined TRNG function
39  * @return		none
40  */
41 void			hwECC_set_rng(hECC_rng_func rng_func);
42 
43 
44 /**
45  * @brief		get ECCP key pair(the key pair could be used in ECDH).
46  * @param[out]	public_key	- public key, big--endian.
47  * @param[out]	private_key	- private key, big--endian.
48  * @param[in]	curve_sel	- ecc_curve select, e.g.: p-256r1.
49  * @return		1(success), 0(error).
50  */
51 unsigned char 	hwECC_make_key(unsigned char *public_key, unsigned char *private_key, ecc_curve_t curve_sel);
52 
53 
54 /**
55  * @brief		ECDH compute key.
56  * @param[in]	local_prikey	- local private key, big--endian.
57  * @param[in]	public_key		- peer public key, big--endian.
58  * @param[out]	dhkey			- output dhkey, big--endian.
59  * @param[in]	curve_sel		- ecc_curve select, e.g.: p-256r1.
60  * @Return		1(success); 0(error).
61  */
62 unsigned char	hwECC_shared_secret(const unsigned char *public_key, const unsigned char *private_key,
63 									unsigned char *secret, ecc_curve_t curve_sel);
64 
65 
66 
67 
68 #endif /* HW_ECC_H_ */
69 
70 #endif /* The end of #if((MCU_CORE_TYPE == MCU_CORE_827x) || (MCU_CORE_TYPE == MCU_CORE_9518)) */
71 
72 
73