1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #ifdef HITLS_CRYPTO_SM4
18
19 #include "crypt_sm4_x86_64.h"
20 #include "crypt_sm4.h"
21 #include "bsl_err_internal.h"
22 #include "crypt_utils.h"
23 #include "crypt_errno.h"
24 #include "securec.h"
25
26 #define XTS_KEY_LEN 32
27 #define SM4_KEY_LEN 16
28 #define XTS_POLYNOMIAL 0xe1
29 #define LAST_BLOCK_HEAD 240
30 #define BYTE_MOST_SIG 128
31 #define BYTE 8
32
SM4_XTS_Calculate_Tweak(unsigned char * t,const unsigned int idx)33 void SM4_XTS_Calculate_Tweak(unsigned char *t, const unsigned int idx)
34 {
35 uint32_t j;
36 uint8_t tweak_in, tweak_out;
37
38 tweak_in = 0;
39 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
40 tweak_out = (t[idx + j] << (BYTE - 1)) & BYTE_MOST_SIG;
41 t[j] = (t[idx + j] >> 1) + tweak_in;
42 tweak_in = tweak_out;
43 }
44 if (tweak_out) {
45 t[0] ^= XTS_POLYNOMIAL;
46 }
47 }
48
SM4_XTS_Encrypt_Helper(uint32_t left,const uint32_t dataLen,uint8_t * t,uint8_t * x,const uint8_t * plain,uint8_t * cipher,const uint32_t * dataRk)49 static void SM4_XTS_Encrypt_Helper(uint32_t left, const uint32_t dataLen, uint8_t* t, uint8_t *x,
50 const uint8_t* plain, uint8_t* cipher, const uint32_t* dataRk)
51 {
52 uint32_t i, j;
53 uint32_t init;
54
55 init = dataLen - left;
56 if (left >= CRYPT_SM4_BLOCKSIZE) {
57 left = left % CRYPT_SM4_BLOCKSIZE;
58
59 for (i = init; i < (dataLen - left); i += CRYPT_SM4_BLOCKSIZE) {
60 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
61 t[j + CRYPT_SM4_BLOCKSIZE] = t[j];
62 }
63 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
64 x[j] = plain[i + j] ^ t[j];
65 }
66
67 SM4_Encrypt(x, cipher + i, dataRk);
68
69 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
70 cipher[i + j] = cipher[i + j] ^ t[j];
71 }
72 SM4_XTS_Calculate_Tweak(t, CRYPT_SM4_BLOCKSIZE);
73 }
74 }
75 init = dataLen - left;
76
77 if (left != 0) {
78 for (i = 0; i < left; i++) {
79 cipher[init + i] = cipher[init - CRYPT_SM4_BLOCKSIZE + i];
80 x[i] = plain[init + i];
81 }
82 for (i = left; i < CRYPT_SM4_BLOCKSIZE; i++) {
83 x[i] = cipher[init - CRYPT_SM4_BLOCKSIZE + i];
84 }
85 for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) {
86 x[i] = x[i] ^ t[i];
87 }
88
89 SM4_Encrypt(x, cipher + init - CRYPT_SM4_BLOCKSIZE, dataRk);
90 for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) {
91 cipher[init - CRYPT_SM4_BLOCKSIZE + i] = cipher[init - CRYPT_SM4_BLOCKSIZE + i] ^ t[i];
92 }
93 }
94 }
95
SM4_XTS_En(uint8_t * cipher,const uint8_t * plain,const uint32_t * dataRk,const uint8_t * tweak,const uint32_t dataLen)96 int32_t SM4_XTS_En(uint8_t* cipher, const uint8_t* plain, const uint32_t* dataRk,
97 const uint8_t* tweak, const uint32_t dataLen)
98 {
99 uint32_t left;
100
101 uint8_t x[CRYPT_SM4_BLOCKSIZE_16] = {0};
102 uint8_t t[CRYPT_SM4_BLOCKSIZE_16] = {0};
103
104 if (dataLen < CRYPT_SM4_BLOCKSIZE) {
105 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN);
106 return CRYPT_SM4_ERR_MSG_LEN;
107 }
108 left = dataLen % CRYPT_SM4_BLOCKSIZE_16;
109
110 // MODES_XTS_Ctrl has TW = Enc_K2(iv) done
111 memcpy_s(t, CRYPT_SM4_BLOCKSIZE_16, tweak, CRYPT_SM4_BLOCKSIZE);
112
113 if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) {
114 SM4_XTS_Encrypt_Blocks(plain, cipher, dataLen, dataRk, t);
115 }
116
117 if (left == 0) {
118 return CRYPT_SUCCESS;
119 } else {
120 if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) {
121 SM4_XTS_Calculate_Tweak(t, LAST_BLOCK_HEAD);
122 }
123 SM4_XTS_Encrypt_Helper(left, dataLen, t, x, plain, cipher, dataRk);
124 }
125 return CRYPT_SUCCESS;
126 }
127
SM4_XTS_Decrypt_Helper(uint32_t left,const uint32_t dataLen,uint8_t * t,uint8_t * x,uint8_t * plain,const uint8_t * cipher,const uint32_t * dataRk)128 static void SM4_XTS_Decrypt_Helper(uint32_t left, const uint32_t dataLen, uint8_t* t, uint8_t *x,
129 uint8_t* plain, const uint8_t* cipher, const uint32_t* dataRk)
130 {
131 uint32_t i, j;
132 uint32_t init;
133
134 init = dataLen - left;
135 if (left >= CRYPT_SM4_BLOCKSIZE) {
136 left = left % CRYPT_SM4_BLOCKSIZE;
137
138 for (i = init; i < (dataLen - left); i += CRYPT_SM4_BLOCKSIZE) {
139 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
140 t[j + CRYPT_SM4_BLOCKSIZE] = t[j];
141 }
142 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
143 x[j] = cipher[i + j] ^ t[j];
144 }
145
146 SM4_Decrypt(x, plain + i, dataRk);
147
148 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
149 plain[i + j] = plain[i + j] ^ t[j];
150 }
151 SM4_XTS_Calculate_Tweak(t, CRYPT_SM4_BLOCKSIZE);
152 }
153 }
154
155 init = dataLen - left;
156
157 if (left != 0) {
158 // recompute
159 // m-T
160 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
161 x[j] = cipher[init - CRYPT_SM4_BLOCKSIZE + j] ^ t[j];
162 }
163 SM4_Decrypt(x, plain + init - CRYPT_SM4_BLOCKSIZE, dataRk);
164
165 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
166 plain[init - CRYPT_SM4_BLOCKSIZE + j] = plain[init - CRYPT_SM4_BLOCKSIZE + j] ^ t[j];
167 }
168 for (i = 0; i < left; i++) {
169 plain[init + i] = plain[init - CRYPT_SM4_BLOCKSIZE + i];
170 x[i] = cipher[init + i];
171 }
172 for (i = left; i < CRYPT_SM4_BLOCKSIZE; i++) {
173 x[i] = plain[init - CRYPT_SM4_BLOCKSIZE + i];
174 }
175 // (m-1)-T
176 for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) {
177 x[i] = x[i] ^ t[CRYPT_SM4_BLOCKSIZE + i];
178 }
179
180 SM4_Decrypt(x, plain + init - CRYPT_SM4_BLOCKSIZE, dataRk);
181
182 for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) {
183 plain[init - CRYPT_SM4_BLOCKSIZE + i] = plain[init - CRYPT_SM4_BLOCKSIZE + i]
184 ^ t[CRYPT_SM4_BLOCKSIZE + i];
185 }
186 }
187 }
188
SM4_XTS_De(uint8_t * plain,const uint8_t * cipher,const uint32_t * dataRk,const uint8_t * tweak,const uint32_t dataLen)189 int32_t SM4_XTS_De(uint8_t* plain, const uint8_t* cipher, const uint32_t* dataRk,
190 const uint8_t* tweak, const uint32_t dataLen)
191 {
192 uint32_t j;
193 uint32_t left;
194
195 uint8_t t[CRYPT_SM4_BLOCKSIZE_16] = {0};
196 uint8_t x[CRYPT_SM4_BLOCKSIZE_16] = {0};
197
198 if (dataLen < CRYPT_SM4_BLOCKSIZE) {
199 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); // need push error code for error point
200 return CRYPT_SM4_ERR_MSG_LEN;
201 }
202 left = dataLen % CRYPT_SM4_BLOCKSIZE_16;
203
204 // MODES_XTS_Ctrl has TW = Enc_K2(iv) done
205 (void)memcpy_s(t, CRYPT_SM4_BLOCKSIZE_16, tweak, CRYPT_SM4_BLOCKSIZE);
206
207 if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) {
208 SM4_XTS_Encrypt_Blocks(cipher, plain, dataLen, dataRk, t);
209 }
210
211 if (left != 0) {
212 if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) {
213 SM4_XTS_Calculate_Tweak(t, LAST_BLOCK_HEAD);
214 for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) {
215 t[j + CRYPT_SM4_BLOCKSIZE] = t[j + LAST_BLOCK_HEAD];
216 }
217 }
218 SM4_XTS_Decrypt_Helper(left, dataLen, t, x, plain, cipher, dataRk);
219 }
220 return CRYPT_SUCCESS;
221 }
222
223 // key[0..16]: data key
224 // key[16..32]: tweak key
CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx * ctx,const uint8_t * key,uint32_t len)225 int32_t CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len)
226 {
227 CRYPT_SM4_Ctx *tmk = NULL;
228 if (ctx == NULL || key == NULL) {
229 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
230 return CRYPT_NULL_INPUT;
231 }
232
233 if (len != XTS_KEY_LEN) {
234 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN);
235 return CRYPT_SM4_ERR_KEY_LEN;
236 }
237
238 if (memcmp(key, key + CRYPT_SM4_BLOCKSIZE, CRYPT_SM4_BLOCKSIZE) == 0) {
239 BSL_ERR_PUSH_ERROR(CRYPT_SM4_UNSAFE_KEY);
240 return CRYPT_SM4_UNSAFE_KEY;
241 }
242
243 tmk = (CRYPT_SM4_Ctx *)&ctx[1];
244 SM4_SetEncKey(key, ctx->rk);
245 SM4_SetEncKey(key + CRYPT_SM4_BLOCKSIZE, tmk->rk);
246
247 return CRYPT_SUCCESS;
248 }
249
CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx * ctx,const uint8_t * key,uint32_t len)250 int32_t CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len)
251 {
252 CRYPT_SM4_Ctx *tmk = NULL;
253 if (ctx == NULL || key == NULL) {
254 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
255 return CRYPT_NULL_INPUT;
256 }
257
258 if (len != XTS_KEY_LEN) {
259 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN);
260 return CRYPT_SM4_ERR_KEY_LEN;
261 }
262
263 if (memcmp(key, key + CRYPT_SM4_BLOCKSIZE, CRYPT_SM4_BLOCKSIZE) == 0) {
264 BSL_ERR_PUSH_ERROR(CRYPT_SM4_UNSAFE_KEY);
265 return CRYPT_SM4_UNSAFE_KEY;
266 }
267
268 tmk = (CRYPT_SM4_Ctx *)&ctx[1];
269 SM4_SetDecKey(key, ctx->rk);
270 SM4_SetEncKey(key + CRYPT_SM4_BLOCKSIZE, tmk->rk);
271
272 return CRYPT_SUCCESS;
273 }
274
CRYPT_SM4_XTS_Encrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv)275 int32_t CRYPT_SM4_XTS_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv)
276 {
277 if (ctx == NULL || iv == NULL) {
278 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
279 return CRYPT_NULL_INPUT;
280 }
281
282 return SM4_XTS_En(out, in, ctx->rk, iv, len);
283 }
284
CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv)285 int32_t CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv)
286 {
287 if (ctx == NULL || iv == NULL) {
288 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
289 return CRYPT_NULL_INPUT;
290 }
291
292 return SM4_XTS_De(out, in, ctx->rk, iv, len);
293 }
294
CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx * ctx,const uint8_t * key,uint32_t len)295 int32_t CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len)
296 {
297 if (ctx == NULL || key == NULL) {
298 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
299 return CRYPT_NULL_INPUT;
300 }
301 if (len != SM4_KEY_LEN) {
302 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN);
303 return CRYPT_SM4_ERR_KEY_LEN;
304 }
305
306 SM4_SetEncKey(key, ctx->rk);
307
308 return CRYPT_SUCCESS;
309 }
310
CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx * ctx,const uint8_t * key,uint32_t len)311 int32_t CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len)
312 {
313 if (ctx == NULL || key == NULL) {
314 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
315 return CRYPT_NULL_INPUT;
316 }
317 if (len != SM4_KEY_LEN) {
318 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN);
319 return CRYPT_SM4_ERR_KEY_LEN;
320 }
321
322 SM4_SetDecKey(key, ctx->rk);
323
324 return CRYPT_SUCCESS;
325 }
326
327 #ifdef HITLS_CRYPTO_ECB
SM4_ECB_Crypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)328 int32_t SM4_ECB_Crypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
329 {
330 if (ctx == NULL) {
331 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
332 return CRYPT_NULL_INPUT;
333 }
334 if (len < CRYPT_SM4_BLOCKSIZE) {
335 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN);
336 return CRYPT_SM4_ERR_MSG_LEN;
337 }
338 SM4_ECB_Encrypt(in, out, len, ctx->rk);
339 return CRYPT_SUCCESS;
340 }
341
CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)342 int32_t CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
343 {
344 return SM4_ECB_Crypt(ctx, in, out, len);
345 }
346
CRYPT_SM4_ECB_Decrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)347 int32_t CRYPT_SM4_ECB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
348 {
349 return SM4_ECB_Crypt(ctx, in, out, len);
350 }
351 #endif
352
353 #ifdef HITLS_CRYPTO_CBC
CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv)354 int32_t CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv)
355 {
356 if (ctx == NULL) {
357 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
358 return CRYPT_NULL_INPUT;
359 }
360 if (len < CRYPT_SM4_BLOCKSIZE) {
361 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN);
362 return CRYPT_SM4_ERR_MSG_LEN;
363 }
364 SM4_CBC_Encrypt(in, out, len, ctx->rk, iv, 1);
365 return CRYPT_SUCCESS;
366 }
367
CRYPT_SM4_CBC_Decrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv)368 int32_t CRYPT_SM4_CBC_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv)
369 {
370 if (ctx == NULL) {
371 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
372 return CRYPT_NULL_INPUT;
373 }
374 if (len < CRYPT_SM4_BLOCKSIZE) {
375 BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN);
376 return CRYPT_SM4_ERR_MSG_LEN;
377 }
378 SM4_CBC_Encrypt(in, out, len, ctx->rk, iv, 0);
379 return CRYPT_SUCCESS;
380 }
381 #endif
382
383 #ifdef HITLS_CRYPTO_OFB
SM4_OFB_Crypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv,uint8_t * offset)384 int32_t SM4_OFB_Crypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset)
385 {
386 if (ctx == NULL) {
387 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
388 return CRYPT_NULL_INPUT;
389 }
390 int tmp = *offset;
391 SM4_OFB_Encrypt(in, out, len, ctx->rk, iv, &tmp);
392 *offset = (uint8_t)tmp;
393 return CRYPT_SUCCESS;
394 }
395
CRYPT_SM4_OFB_Encrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv,uint8_t * offset)396 int32_t CRYPT_SM4_OFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len,
397 uint8_t *iv, uint8_t *offset)
398 {
399 return SM4_OFB_Crypt(ctx, in, out, len, iv, offset);
400 }
401
CRYPT_SM4_OFB_Decrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv,uint8_t * offset)402 int32_t CRYPT_SM4_OFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len,
403 uint8_t *iv, uint8_t *offset)
404 {
405 return SM4_OFB_Crypt(ctx, in, out, len, iv, offset);
406 }
407 #endif
408
409 #ifdef HITLS_CRYPTO_CFB
CRYPT_SM4_CFB_Encrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv,uint8_t * offset)410 int32_t CRYPT_SM4_CFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset)
411 {
412 if (ctx == NULL) {
413 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
414 return CRYPT_NULL_INPUT;
415 }
416 int tmp = *offset;
417 SM4_CFB128_Encrypt(in, out, len, ctx->rk, iv, &tmp);
418 *offset = (uint8_t)tmp;
419 return CRYPT_SUCCESS;
420 }
421
CRYPT_SM4_CFB_Decrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv,uint8_t * offset)422 int32_t CRYPT_SM4_CFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset)
423 {
424 if (ctx == NULL) {
425 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
426 return CRYPT_NULL_INPUT;
427 }
428 int tmp = *offset;
429 SM4_CFB128_Decrypt(in, out, len, ctx->rk, iv, &tmp);
430 *offset = (uint8_t)tmp;
431 return CRYPT_SUCCESS;
432 }
433 #endif
434
435 #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM)
CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv)436 int32_t CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv)
437 {
438 if (ctx == NULL) {
439 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
440 return CRYPT_NULL_INPUT;
441 }
442 SM4_CTR_EncryptBlocks(in, out, len, ctx->rk, iv);
443 return CRYPT_SUCCESS;
444 }
445
CRYPT_SM4_CTR_Decrypt(CRYPT_SM4_Ctx * ctx,const uint8_t * in,uint8_t * out,uint32_t len,uint8_t * iv)446 int32_t CRYPT_SM4_CTR_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv)
447 {
448 if (ctx == NULL) {
449 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
450 return CRYPT_NULL_INPUT;
451 }
452 SM4_CTR_EncryptBlocks(in, out, len, ctx->rk, iv);
453 return CRYPT_SUCCESS;
454 }
455 #endif
456
457 #endif /* HITLS_CRYPTO_SM4 */
458