• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CBC
18 
19 #include "securec.h"
20 #include "bsl_err_internal.h"
21 #include "crypt_utils.h"
22 #include "crypt_errno.h"
23 #include "crypt_modes_cbc.h"
24 #include "modes_local.h"
25 
26 #define CBC_UPDATE_VALUES(l, i, o, len) \
27     do { \
28         (l) -= (len); \
29         (i) += (len); \
30         (o) += (len); \
31     } while (false)
32 
33 
MODES_CBC_Encrypt(MODES_CipherCommonCtx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)34 int32_t MODES_CBC_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
35 {
36     uint32_t blockSize = ctx->blockSize;
37     int32_t ret;
38     uint8_t *iv = ctx->iv;
39     uint8_t *tmp = ctx->buf;
40     uint32_t left = len;
41     const uint8_t *input = in;
42     uint8_t *output = out;
43     // The ctx, in, and out pointers have been determined at the EAL layer and are not determined again.
44     if ((left % blockSize) != 0) {
45         BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN);
46         return CRYPT_MODE_ERR_INPUT_LEN;
47     }
48 
49     while (left >= blockSize) {
50         /* Plaintext XOR IV. BlockSize must be an integer multiple of 4 bytes. */
51         DATA32_XOR(input, iv, tmp, blockSize);
52 
53         ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, tmp, output, blockSize);
54         if (ret != CRYPT_SUCCESS) {
55             BSL_ERR_PUSH_ERROR(ret);
56             return ret;
57         }
58         /* The current encryption result is used as the next IV value. */
59         iv = output;
60 
61         /* Offset length is the size of integer multiple blocks */
62         CBC_UPDATE_VALUES(left, input, output, blockSize);
63     }
64 
65     if (memcpy_s(ctx->iv, MODES_MAX_IV_LENGTH, iv, blockSize) != EOK) {
66         BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL);
67         return CRYPT_SECUREC_FAIL;
68     }
69 
70     return CRYPT_SUCCESS;
71 }
72 
MODES_CBC_Decrypt(MODES_CipherCommonCtx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)73 int32_t MODES_CBC_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
74 {
75     const uint8_t *iv = ctx->iv;
76     uint8_t *tmp = ctx->buf;
77     uint32_t blockSize = ctx->blockSize;
78     uint32_t left = len;
79     uint8_t *output = out;
80     uint8_t tmpChar;
81     const uint8_t *input = in;
82 
83     // The ctx, in, and out pointers have been determined at the EAL layer and are not determined again.
84     if ((left % blockSize) != 0) {
85         BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN);
86         return CRYPT_MODE_ERR_INPUT_LEN;
87     }
88 
89     // In the case where the input and output are at the same address,
90     // the judgment should be placed outside the while loop. Otherwise, the performance will be affected.
91     if (in != out) {
92         while (left >= blockSize) {
93             int32_t ret = ctx->ciphMeth->decryptBlock(ctx->ciphCtx, input, tmp, blockSize);
94             if (ret != CRYPT_SUCCESS) {
95                 BSL_ERR_PUSH_ERROR(ret);
96                 return ret;
97             }
98             /* The ciphertext is used as the next IV value. BlockSize must be an integer multiple of 4 bytes. */
99             DATA32_XOR(iv, tmp, output, blockSize);
100             iv = input;
101 
102             CBC_UPDATE_VALUES(left, input, output, blockSize);
103         }
104         if (iv != ctx->iv) {
105             (void)memcpy_s(ctx->iv, MODES_MAX_IV_LENGTH, iv, blockSize);
106         }
107     } else {
108         while (left >= blockSize) {
109             int32_t ret = ctx->ciphMeth->decryptBlock(ctx->ciphCtx, input, tmp, blockSize);
110             if (ret != CRYPT_SUCCESS) {
111                 BSL_ERR_PUSH_ERROR(ret);
112                 return ret;
113             }
114 
115             for (uint32_t i = 0; i < blockSize; i++) {
116                 tmpChar = input[i];
117                 output[i] = tmp[i] ^ ctx->iv[i];
118                 ctx->iv[i] = tmpChar;
119             }
120 
121             CBC_UPDATE_VALUES(left, input, output, blockSize);
122         }
123     }
124 
125     return CRYPT_SUCCESS;
126 }
127 
MODES_CBC_NewCtx(int32_t algId)128 MODES_CipherCtx *MODES_CBC_NewCtx(int32_t algId)
129 {
130     return MODES_CipherNewCtx(algId);
131 }
132 
MODES_CBC_InitCtx(MODES_CipherCtx * modeCtx,const uint8_t * key,uint32_t keyLen,const uint8_t * iv,uint32_t ivLen,bool enc)133 int32_t MODES_CBC_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
134     uint32_t ivLen, bool enc)
135 {
136     void *setKeyFuncs = enc ? modeCtx->commonCtx.ciphMeth->setEncryptKey : modeCtx->commonCtx.ciphMeth->setDecryptKey;
137     return MODES_CipherInitCtx(modeCtx, setKeyFuncs, modeCtx->commonCtx.ciphCtx, key, keyLen, iv, ivLen, enc);
138 }
139 
MODES_CBC_Update(MODES_CipherCtx * modeCtx,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)140 int32_t MODES_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
141 {
142     return MODES_CipherUpdate(modeCtx, modeCtx->enc ? MODES_CBC_Encrypt : MODES_CBC_Decrypt,
143         in, inLen, out, outLen);
144 }
145 
MODES_CBC_Final(MODES_CipherCtx * modeCtx,uint8_t * out,uint32_t * outLen)146 int32_t MODES_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen)
147 {
148     return MODES_CipherFinal(modeCtx, modeCtx->enc ? MODES_CBC_Encrypt : MODES_CBC_Decrypt, out, outLen);
149 }
150 
MODES_CBC_DeInitCtx(MODES_CipherCtx * modeCtx)151 int32_t MODES_CBC_DeInitCtx(MODES_CipherCtx *modeCtx)
152 {
153     return MODES_CipherDeInitCtx(modeCtx);
154 }
155 
MODES_CBC_Ctrl(MODES_CipherCtx * modeCtx,int32_t cmd,void * val,uint32_t valLen)156 int32_t MODES_CBC_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen)
157 {
158     int32_t ret;
159     if (modeCtx == NULL) {
160         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
161         return CRYPT_NULL_INPUT;
162     }
163 
164     switch (cmd) {
165         case CRYPT_CTRL_SET_PADDING:
166             if (val == NULL || valLen != sizeof(int32_t)) {
167                 BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
168                 return CRYPT_INVALID_ARG;
169             }
170             ret = MODES_SetPaddingCheck(*(int32_t *)val);
171             if (ret != CRYPT_SUCCESS) {
172                 return ret;
173             }
174             modeCtx->pad = *(int32_t *)val;
175             return CRYPT_SUCCESS;
176         case CRYPT_CTRL_GET_PADDING:
177             if (val == NULL || valLen != sizeof(int32_t)) {
178                 BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
179                 return CRYPT_INVALID_ARG;
180             }
181             *(int32_t *)val = modeCtx->pad;
182             return CRYPT_SUCCESS;
183         case CRYPT_CTRL_GET_BLOCKSIZE:
184             if (val == NULL || valLen != sizeof(uint32_t)) {
185                 BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN);
186                 return CRYPT_MODE_ERR_INPUT_LEN;
187             }
188             *(int32_t *)val = modeCtx->commonCtx.ciphMeth->blockSize;
189             return CRYPT_SUCCESS;
190         default:
191             return MODES_CipherCtrl(modeCtx, cmd, val, valLen);
192     }
193 }
194 
MODES_CBC_FreeCtx(MODES_CipherCtx * modeCtx)195 void MODES_CBC_FreeCtx(MODES_CipherCtx *modeCtx)
196 {
197     MODES_CipherFreeCtx(modeCtx);
198 }
199 
MODES_CBC_UpdateEx(MODES_CipherCtx * modeCtx,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)200 int32_t MODES_CBC_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
201 {
202     if (modeCtx == NULL) {
203         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
204         return CRYPT_NULL_INPUT;
205     }
206     switch (modeCtx->algId) {
207         case CRYPT_CIPHER_AES128_CBC:
208         case CRYPT_CIPHER_AES192_CBC:
209         case CRYPT_CIPHER_AES256_CBC:
210 #ifdef HITLS_CRYPTO_AES
211             return AES_CBC_Update(modeCtx, in, inLen, out, outLen);
212 #else
213             return CRYPT_EAL_ALG_NOT_SUPPORT;
214 #endif
215         case CRYPT_CIPHER_SM4_CBC:
216 #ifdef HITLS_CRYPTO_SM4
217             return SM4_CBC_Update(modeCtx, in, inLen, out, outLen);
218 #else
219             return CRYPT_EAL_ALG_NOT_SUPPORT;
220 #endif
221         default:
222             return MODES_CBC_Update(modeCtx, in, inLen, out, outLen);
223     }
224 }
MODES_CBC_InitCtxEx(MODES_CipherCtx * modeCtx,const uint8_t * key,uint32_t keyLen,const uint8_t * iv,uint32_t ivLen,void * param,bool enc)225 int32_t MODES_CBC_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
226     uint32_t ivLen, void *param, bool enc)
227 {
228     (void)param;
229     if (modeCtx == NULL) {
230         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
231         return CRYPT_NULL_INPUT;
232     }
233     switch (modeCtx->algId) {
234         case CRYPT_CIPHER_SM4_CBC:
235 #ifdef HITLS_CRYPTO_SM4
236             return SM4_CBC_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc);
237 #else
238             return CRYPT_EAL_ALG_NOT_SUPPORT;
239 #endif
240         default:
241             return MODES_CBC_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc);
242     }
243 }
244 
MODES_CBC_FinalEx(MODES_CipherCtx * modeCtx,uint8_t * out,uint32_t * outLen)245 int32_t MODES_CBC_FinalEx(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen)
246 {
247     if (modeCtx == NULL) {
248         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
249         return CRYPT_NULL_INPUT;
250     }
251     switch (modeCtx->algId) {
252         case CRYPT_CIPHER_AES128_CBC:
253         case CRYPT_CIPHER_AES192_CBC:
254         case CRYPT_CIPHER_AES256_CBC:
255 #ifdef HITLS_CRYPTO_AES
256             return AES_CBC_Final(modeCtx, out, outLen);
257 #else
258             return CRYPT_EAL_ALG_NOT_SUPPORT;
259 #endif
260         case CRYPT_CIPHER_SM4_CBC:
261 #ifdef HITLS_CRYPTO_SM4
262             return SM4_CBC_Final(modeCtx, out, outLen);
263 #else
264             return CRYPT_EAL_ALG_NOT_SUPPORT;
265 #endif
266         default:
267             return MODES_CBC_Final(modeCtx, out, outLen);
268     }
269 }
270 
271 #endif // HITLS_CRYPTO_CBC
272