1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* Copyright (C) 2017-2021 Alibaba Group Holding Limited
*/
/******************************************************************************
* @file sec_crypt_sm2.h
* @brief Header File for SM2
* @version V1.0
* @date 20. Jul 2021
* @model sm2
******************************************************************************/
#ifndef _SC_SM2_H_
#define _SC_SM2_H_
#ifdef CONFIG_SEC_CRYPTO_SM2
#ifdef __cplusplus
extern "C" {
#endif
#include "drv/sm2.h"
typedef struct {
uint32_t sm2_curve : 1; ///< supports 256bits curve
} sc_sm2_capabilities_t;
/**
\brief SM2 ciphertext order
*/
typedef enum {
SC_SM2_C1C3C2 = 0,
SC_SM2_C1C2C3,
} sc_sm2_cipher_order_e;
typedef enum {
SC_SM2_ENDIAN_LITTLE = 0, ///< Little Endian
SC_SM2_ENDIAN_BIG ///< Big Endian
} sc_sm2_endian_mode_e;
/**
\brief SM2 status
*/
typedef struct {
uint32_t busy : 1; ///< Calculate busy flag
} sc_sm2_state_t;
/**
\brief SM2 key exchange role
*/
typedef enum { SC_SM2_Role_Sponsor = 0, SC_SM2_Role_Responsor } sc_sm2_exchange_role_e;
/****** SM2 Event *****/
typedef enum {
SC_SM2_EVENT_MAKE_KEY_COMPLETE = 0, ///< Make key completed
SC_SM2_EVENT_ENCRYPT_COMPLETE, ///< Encrypt completed
SC_SM2_EVENT_DECRYPT_COMPLETE, ///< Decrypt completed
SC_SM2_EVENT_SIGN_COMPLETE, ///< Sign completed
SC_SM2_EVENT_VERIFY_COMPLETE, ///< Verify completed
SC_SM2_EVENT_EXCHANGE_KEY_COMPLETE, ///< Exchange key completed
} sc_sm2_event_e;
typedef struct {
#ifdef CONFIG_CSI_V2
csi_sm2_t sm2;
#endif
} sc_sm2_t;
///< Pointer to \ref sc_sm2_callback_t : SM2 Event call back.
typedef void (*sc_sm2_callback_t)(sc_sm2_event_e event);
/**
\brief Initialize SM2.
\param[in] sm2 sm2 handle to operate.
\param[in] idx device id
\return \ref uint32_t
*/
uint32_t sc_sm2_init(sc_sm2_t *sm2, uint32_t idx);
/**
\brief De-initialize SM2 Interface. stops operation and releases the
software resources used by the interface \param[in] sm2 sm2 handle to
operate. \return none
*/
void sc_sm2_uninit(sc_sm2_t *sm2);
/**
\brief sm2 get capability.
\param[in] sm2 Operate handle.
\return \ref uint32_t
*/
uint32_t sc_sm2_config(sc_sm2_t *sm2, sc_sm2_cipher_order_e co,
sc_sm2_endian_mode_e endian);
/**
\brief Attach the callback handler to SM2
\param[in] sm2 Operate handle.
\param[in] cb Callback function
\param[in] arg User can define it by himself as callback's param
\return Error code \ref uint32_t
*/
uint32_t sc_sm2_attach_callback(sc_sm2_t *sm2, sc_sm2_callback_t cb, void *arg);
/**
\brief Detach the callback handler
\param[in] sm2 Operate handle.
*/
void sc_sm2_detach_callback(sc_sm2_t *sm2);
/**
\brief sm2 get capability.
\param[in] sm2 Operate handle.
\param[out] cap Pointer of sc_sm2_capabilities_t.
\return \ref uint32_t
*/
uint32_t sc_sm2_get_capabilities(sc_sm2_t *sm2, sc_sm2_capabilities_t *cap);
uint32_t sc_sm2_check_keypair(sc_sm2_t *sm2, uint8_t pubkey[65],
uint8_t prikey[32]);
/**
\brief generate sm2 key.
\param[in] sm2 sm2 handle to operate.
\param[out] private Pointer to the sm2 private key, alloc by caller.
\param[out] public Pointer to the sm2 public key, alloc by caller.
\return \ref uint32_t
*/
uint32_t sc_sm2_gen_key(sc_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]);
/**
\brief sm2 sign
\param[in] sm2 sm2 handle to operate.
\param[in] d Pointer to the digest.
\param[out] privkey Pointer to the private key
\param[out] s Pointer to the signature
\return \ref uint32_t
*/
uint32_t sc_sm2_sign(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32],
uint8_t s[64]);
/**
\brief sm2 sign
\param[in] sm2 sm2 handle to operate.
\param[in] d Pointer to the digest.
\param[out] privkey Pointer to the private key
\param[out] s Pointer to the signature
\return \ref uint32_t
*/
uint32_t sc_sm2_sign_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32],
uint8_t s[64]);
/* TODO */
/**
\brief sm2 verify
\param[in] sm2 sm2 handle to operate.
\param[in] d Pointer to the digest.
\param[out] privkey Pointer to the private key
\param[out] s Pointer to the signature
\return verify result
*/
bool sc_sm2_verify(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65],
uint8_t s[64]);
/**
\brief sm2 verify
\param[in] sm2 sm2 handle to operate.
\param[in] d Pointer to the digest.
\param[out] privkey Pointer to the private key
\param[out] s Pointer to the signature
\return verify result
*/
bool sc_sm2_verify_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65],
uint8_t s[64]);
/**
\brief sm2 encrypto
\param[in] sm2 sm2 handle to operate.
\param[in] plain Pointer to the plaintext.
\param[in] PlainByteLen plaintext len
\param[in] pubKey public key.
\param[out] cipher Pointer to the chipher
\param[out] cipher_byte_len Pointer to the chipher len.
\return uint32_t
*/
uint32_t sc_sm2_encrypt(sc_sm2_t *sm2, uint8_t *plain, uint32_t plain_len,
uint8_t pubKey[65], uint8_t *cipher,
uint32_t *cipher_len);
/**
\brief sm2 encrypto
\param[in] sm2 sm2 handle to operate.
\param[in] cipher Pointer to the chipher
\param[in] CipherByteLen chipher len.
\param[in] prikey private key.
\param[out] plain Pointer to the plaintext.
\param[out] PlainByteLen plaintext len
\return uint32_t
*/
uint32_t sc_sm2_decrypt(sc_sm2_t *sm2, uint8_t *cipher, uint32_t cipher_len,
uint8_t prikey[32], uint8_t *plain,
uint32_t *plain_len);
/**
\brief sm2 key exchange
\param[in] sm2 sm2 handle to operate.
\return uint32_t
*/
uint32_t sc_sm2_exchangekey(sc_sm2_t *sm2, sc_sm2_exchange_role_e role,
uint8_t *da, uint8_t *pb, uint8_t *ra1, uint8_t *ra,
uint8_t *rb, uint8_t *za, uint8_t *zb,
uint32_t k_len, uint8_t *ka, uint8_t *s1,
uint8_t *sa);
/**
\brief sm2 key exchange get Z.
\param[in] sm2 sm2 handle to operate.
\return uint32_t
*/
uint32_t sc_sm2_getZ(sc_sm2_t *sm2, uint8_t *id, uint32_t id_len,
uint8_t pubkey[65], uint8_t z[32]);
/**
\brief sm2 key exchange get E
\param[in] sm2 sm2 handle to operate.
\return uint32_t
*/
uint32_t sc_sm2_getE(sc_sm2_t *sm2, uint8_t *m, uint32_t len, uint8_t z[32],
uint8_t e[32]);
/**
\brief Get SM2 state.
\param[in] sm2 SM2 handle to operate.
\param[out] state SM2 state \ref sc_sm2_state_t.
\return Error code \ref uint32_t
*/
uint32_t sc_sm2_get_state(sc_sm2_t *sm2, sc_sm2_state_t *state);
#ifdef __cplusplus
extern "C" {
#endif
#endif
#endif /* _SC_SM2_H_ */
|