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 #if defined(HITLS_CRYPTO_SM4) && defined(HITLS_CRYPTO_CTR)
18
19 #include "bsl_err_internal.h"
20 #include "crypt_sm4.h"
21 #include "crypt_utils.h"
22 #include "crypt_errno.h"
23 #include "crypt_modes_ctr.h"
24 #include "modes_local.h"
25
MODE_SM4_CTR_Encrypt(MODES_CipherCommonCtx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)26 int32_t MODE_SM4_CTR_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
27 {
28 if (ctx == NULL || in == NULL || out == NULL) {
29 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
30 return CRYPT_NULL_INPUT;
31 }
32 // ctx, in, and out pointers have been determined at the EAL layer and will not be determined again
33 if (len == 0) {
34 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
35 return CRYPT_NULL_INPUT;
36 }
37 uint32_t offset = MODES_CTR_LastHandle(ctx, in, out, len);
38 uint32_t left = len - offset;
39 const uint8_t *tmpIn = in + offset;
40 uint8_t *tmpOut = out + offset;
41
42 uint32_t blockSize = ctx->blockSize; // ctr supports only 16-byte block size
43 uint32_t blocks, beCtr32;
44 while (left >= blockSize) {
45 blocks = left >> 4; // Shift rightwards by 4 bytes to obtain the number of blocks.
46 beCtr32 = GET_UINT32_BE(ctx->iv, 12); // offset of 12 bytes to obtain the IV in lower 32 bits
47 beCtr32 += blocks;
48 if (beCtr32 < blocks) {
49 blocks -= beCtr32;
50 beCtr32 = 0;
51 }
52 // Shift leftwards by 4 bytes to obtain the length of the data involved in the calculation.
53 uint32_t calLen = blocks << 4;
54 (void)CRYPT_SM4_CTR_Encrypt(ctx->ciphCtx, tmpIn, tmpOut, calLen / ctx->blockSize, ctx->iv);
55 left -= calLen;
56 tmpIn += calLen;
57 tmpOut += calLen;
58 if (beCtr32 == 0) {
59 // 16 - 4, The lower 32 bits are carried, and the upper 12 bytes are increased by 1.
60 MODE_IncCounter(ctx->iv, blockSize - 4);
61 }
62 }
63 MODES_CTR_RemHandle(ctx, tmpIn, tmpOut, left);
64 return CRYPT_SUCCESS;
65 }
66
SM4_CTR_InitCtx(MODES_CipherCtx * modeCtx,const uint8_t * key,uint32_t keyLen,const uint8_t * iv,uint32_t ivLen,bool enc)67 int32_t SM4_CTR_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
68 uint32_t ivLen, bool enc)
69 {
70 return MODES_CipherInitCtx(modeCtx, MODES_SM4_SetEncryptKey, &modeCtx->commonCtx, key, keyLen, iv, ivLen, enc);
71 }
72
SM4_CTR_Update(MODES_CipherCtx * modeCtx,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)73 int32_t SM4_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
74 {
75 return MODES_CipherStreamProcess(MODE_SM4_CTR_Encrypt, &modeCtx->commonCtx, in, inLen, out, outLen);
76 }
77
78 #endif