1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file       drv/ecdh.h
21  * @brief      Header File for ECDH Driver
22  * @version    V1.0
23  * @date       9. May 2023
24  * @model      ecdh
25  ******************************************************************************/
26 
27 #ifndef _DRV_ECDH_H_
28 #define _DRV_ECDH_H_
29 
30 #include <drv/common.h>
31 #include <drv/ecdsa.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /**
38 \brief ECDH Ctrl Block
39 */
40 typedef struct {
41     csi_dev_t       dev;
42     void            *priv;
43 } csi_ecdh_t;
44 
45 /**
46   \brief       Initialize ECDH interface. Initializes the resources needed for the ECDH interface
47   \param[in]   ecdh    Handle to operate
48   \param[in]   idx    Device id
49   \return      Error code \ref csi_error_t
50 */
51 csi_error_t csi_ecdh_init(csi_ecdh_t *ecdh, uint32_t idx);
52 
53 /**
54   \brief       De-initialize ECDH interface. Stops operation and releases the software resources used by the interface
55   \param[in]   ecdh    Dandle to operate
56   \return      None
57 */
58 void csi_ecdh_uninit(csi_ecdh_t *ecdh);
59 
60 /**
61   \brief       Load curve param to engin
62   \param[in]   ecdh      Handle to operate
63   \param[in]   type       Pointer to \ref csi_curve_type_t
64   \return      Error code \ref csi_error_t
65 */
66 csi_error_t csi_ecdh_load_curve(csi_ecdh_t *ecdh, csi_curve_type_t type);
67 
68 /**
69   \brief       ECDH generate key pair
70   \param[in]   ecdh        Handle to operate
71   \param[out]  prikey      Pointer to the private key buf
72   \param[out]  pubkey      Pointer to the public key buf
73   \return      Error code \ref Csi_error_t
74 */
75 csi_error_t csi_ecdh_gen_keypair(csi_ecdh_t *ecdh, uint8_t *prikey, uint8_t *pubkey);
76 
77 /**
78   \brief       ECDH generate secret key
79   \param[in]   ecdh        Handle to operate
80   \param[in]   prikey      Pointer to the private key buf
81   \param[in]   pubkey      Pointer to the public key buf
82   \param[out]  sk          Pointer to the secret key buf
83   \param[out]  sk_len      The secret key length
84   \return      Error code \ref Csi_error_t
85 */
86 csi_error_t csi_ecdh_calc_secret(csi_ecdh_t *ecdh, const uint8_t *privkey, const uint8_t *pubkey, uint8_t *sk, uint32_t *sk_len);
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif /* _DRV_ECDH_H_ */
93