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_AES) && defined(HITLS_CRYPTO_XTS)
18 #include "securec.h"
19 #include "bsl_sal.h"
20 #include "bsl_err_internal.h"
21 #include "crypt_utils.h"
22 #include "crypt_errno.h"
23 #include "crypt_aes.h"
24 #include "crypt_modes_xts.h"
25 #include "modes_local.h"
26
MODES_AES_XTS_Encrypt(MODES_CipherXTSCtx * xtsCtx,const uint8_t * in,uint8_t * out,uint32_t len)27 int32_t MODES_AES_XTS_Encrypt(MODES_CipherXTSCtx *xtsCtx, const uint8_t *in, uint8_t *out, uint32_t len)
28 {
29 if (xtsCtx == NULL || in == NULL || out == NULL) {
30 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
31 return CRYPT_NULL_INPUT;
32 }
33 if (len < xtsCtx->blockSize) {
34 BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN);
35 return CRYPT_MODE_ERR_INPUT_LEN;
36 }
37
38 (void)CRYPT_AES_XTS_Encrypt(xtsCtx->ciphCtx, in, out, len, xtsCtx->tweak);
39 return CRYPT_SUCCESS;
40 }
41
MODES_AES_XTS_Decrypt(MODES_CipherXTSCtx * xtsCtx,const uint8_t * in,uint8_t * out,uint32_t len)42 int32_t MODES_AES_XTS_Decrypt(MODES_CipherXTSCtx *xtsCtx, const uint8_t *in, uint8_t *out, uint32_t len)
43 {
44 if (xtsCtx == NULL || in == NULL || out == NULL) {
45 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
46 return CRYPT_NULL_INPUT;
47 }
48 if (len < xtsCtx->blockSize) {
49 BSL_ERR_PUSH_ERROR(CRYPT_MODE_BUFF_LEN_NOT_ENOUGH);
50 return CRYPT_MODE_BUFF_LEN_NOT_ENOUGH;
51 }
52 (void)CRYPT_AES_XTS_Decrypt(xtsCtx->ciphCtx, in, out, len, xtsCtx->tweak);
53 return CRYPT_SUCCESS;
54 }
55
AES_XTS_Update(MODES_XTS_Ctx * modeCtx,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)56 int32_t AES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
57 {
58 return MODES_CipherStreamProcess(modeCtx->enc ? MODES_AES_XTS_Encrypt : MODES_AES_XTS_Decrypt, &modeCtx->xtsCtx,
59 in, inLen, out, outLen);
60 }
61
62
63 #endif