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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* Copyright (C) 2017-2020 Alibaba Group Holding Limited
*/
/******************************************************************************
* @file drv/sm4.h
* @brief Header File for SM4 Driver
* @version V2.0
* @date 9. DEC 2020
* @model SM4
******************************************************************************/
#ifndef _DRV_SM4_H_
#define _DRV_SM4_H_
#include <stdint.h>
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SM4_KEY_LEN_BYTES_32 32
#define SM4_KEY_LEN_BYTES_24 24
#define SM4_KEY_LEN_BYTES_16 16
typedef enum {
SM4_KEY_LEN_BITS_128 = 0, /*128 Data bits*/
SM4_KEY_LEN_BITS_256 /*256 Data bits*/
} csi_sm4_key_bits_t;
typedef struct {
uint32_t busy : 1; ///< Calculate busy flag
uint32_t error : 1; ///< Calculate error flag
} csi_sm4_state_t;
typedef struct {
uint32_t key_len_byte;
uint8_t key[32]; ///< Data block being processed
uint32_t sca;
} csi_sm4_context_t;
/**
\brief SM4 Ctrl Block
*/
typedef struct {
csi_sm4_state_t state;
csi_sm4_context_t context;
csi_dev_t dev;
void * priv;
uint32_t is_kdf;
} csi_sm4_t;
// Function documentation
/**
\brief Initialize sm4 Interface. Initializes the resources needed for the sm4 interface
\param[in] sm4 operate handle
\param[in] idx device id
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_init(csi_sm4_t *sm4, uint32_t idx);
/**
\brief De-initialize sm4 Interface. stops operation and releases the software resources used by the interface
\param[in] sm4 handle to operate
\return None
*/
void csi_sm4_uninit(csi_sm4_t *sm4);
/**
\brief Set encrypt key
\param[in] sm4 handle to operate
\param[in] key Pointer to the key buf
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_set_encrypt_key(csi_sm4_t *sm4, uint8_t *key, csi_sm4_key_bits_t key_len);
/**
\brief Set decrypt key
\param[in] sm4 handle to operate
\param[in] key Pointer to the key buf
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_set_decrypt_key(csi_sm4_t *sm4, uint8_t *key, csi_sm4_key_bits_t key_len);
/**
\brief sm4 ecb encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_ecb_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size);
/**
\brief sm4 ecb decrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_ecb_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size);
/**
\brief sm4 cbc encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cbc_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv);
/**
\brief sm4 cbc decrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cbc_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv);
/**
\brief sm4 cfb1 encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cfb1_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv);
/**
\brief sm4 cfb1 decrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cfb1_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv);
/**
\brief sm4 cfb8 encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cfb8_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv);
/**
\brief sm4 cfb8 decrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cfb8_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv);
/**
\brief sm4 cfb128 decrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\param[out] num the number of the 128-bit block we have used
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cfb128_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv, uint32_t *num);
/**
\brief sm4 cfb128 encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\param[out] num the number of the 128-bit block we have used
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_cfb128_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv, uint32_t *num);
/**
\brief sm4 ofb encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\param[out] num the number of the 128-bit block we have used
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_ofb_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv, uint32_t *num);
/**
\brief sm4 ofb encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] iv init vector
\param[out] num the number of the 128-bit block we have used
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_ofb_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t *iv, uint32_t *num);
/**
\brief sm4 ctr encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] nonce_counter counter
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_ctr_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t nonce_counter[16]);
/**
\brief sm4 ctr encrypt
\param[in] sm4 handle to operate
\param[in] in Pointer to the Source data
\param[out] out Pointer to the Result data
\param[in] size the Source data size
\param[in] nonce_counter counter
\return error code \ref uint32_t
*/
csi_error_t csi_sm4_ctr_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out,
uint32_t size, uint8_t nonce_counter[16]);
/**
\brief Enable SM4 power manage
\param[in] sm4 Handle to operate
\return Error code \ref csi_error_t
*/
csi_error_t csi_sm4_enable_pm(csi_sm4_t *sm4);
/**
\brief Disable SM4 power manage
\param[in] sm4 Handle to operate
\return None
*/
void csi_sm4_disable_pm(csi_sm4_t *sm4);
#ifdef __cplusplus
extern "C" {
#endif
#endif // _DRV_SM4_H_
|