• 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_OFB
18 
19 #include "securec.h"
20 #include "bsl_err_internal.h"
21 #include "crypt_utils.h"
22 #include "crypt_errno.h"
23 #include "modes_local.h"
24 #include "crypt_modes_ofb.h"
25 
MODES_OFB_Crypt(MODES_CipherCommonCtx * ctx,const uint8_t * in,uint8_t * out,uint32_t len)26 int32_t MODES_OFB_Crypt(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 
33     int32_t ret;
34     const uint8_t *input = in;
35     uint32_t blockSize = ctx->blockSize;
36     uint32_t left = len;
37     uint8_t *output = out;
38     uint32_t i;
39 
40     // If the remaining encrypted iv is not used up last time, use that part to perform XOR.
41     while (left > 0 && ctx->offset > 0) {
42         *(output++) = ctx->iv[ctx->offset] ^ *(input++);
43         left--;
44         ctx->offset = (ctx->offset + 1) % blockSize;
45     }
46 
47     while (left > 0) {
48         // Encrypt the IV.
49         ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->iv, blockSize);
50         if (ret != CRYPT_SUCCESS) {
51             BSL_ERR_PUSH_ERROR(ret);
52             return ret;
53         }
54         if (left >= blockSize) {
55             DATA32_XOR(input, ctx->iv, output, blockSize);
56             UPDATE_VALUES(left, input, output, blockSize);
57         } else {
58             for (i = 0; i < left; i++) {
59                 output[i] = input[i] ^ ctx->iv[i];
60             }
61             ctx->offset = (uint8_t)left;
62             left = 0;
63         }
64     }
65 
66     return CRYPT_SUCCESS;
67 }
68 
MODES_OFB_NewCtx(int32_t algId)69 MODES_CipherCtx *MODES_OFB_NewCtx(int32_t algId)
70 {
71     return MODES_CipherNewCtx(algId);
72 }
73 
MODES_OFB_InitCtx(MODES_CipherCtx * modeCtx,const uint8_t * key,uint32_t keyLen,const uint8_t * iv,uint32_t ivLen,bool enc)74 int32_t MODES_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
75     uint32_t ivLen, bool enc)
76 {
77     return MODES_CipherInitCtx(modeCtx, modeCtx->commonCtx.ciphMeth->setEncryptKey,
78         modeCtx->commonCtx.ciphCtx, key, keyLen, iv, ivLen, enc);
79 }
80 
MODES_OFB_Update(MODES_CipherCtx * modeCtx,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)81 int32_t MODES_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
82 {
83     return MODES_CipherStreamProcess(MODES_OFB_Crypt, &modeCtx->commonCtx,
84         in, inLen, out, outLen);
85 }
86 
MODES_OFB_Final(MODES_CipherCtx * modeCtx,uint8_t * out,uint32_t * outLen)87 int32_t MODES_OFB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen)
88 {
89     (void) modeCtx;
90     (void) out;
91     *outLen = 0;
92     return CRYPT_SUCCESS;
93 }
94 
MODES_OFB_DeInitCtx(MODES_CipherCtx * modeCtx)95 int32_t MODES_OFB_DeInitCtx(MODES_CipherCtx *modeCtx)
96 {
97     return MODES_CipherDeInitCtx(modeCtx);
98 }
99 
MODES_OFB_Ctrl(MODES_CipherCtx * modeCtx,int32_t cmd,void * val,uint32_t valLen)100 int32_t MODES_OFB_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen)
101 {
102     if (modeCtx == NULL) {
103         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
104         return CRYPT_NULL_INPUT;
105     }
106     switch (cmd) {
107         case CRYPT_CTRL_GET_BLOCKSIZE:
108             if (val == NULL || valLen != sizeof(uint32_t)) {
109                 return CRYPT_INVALID_ARG;
110             }
111             *(int32_t *)val = 1;
112             return CRYPT_SUCCESS;
113         default:
114             return MODES_CipherCtrl(modeCtx, cmd, val, valLen);;
115     }
116 }
117 
MODES_OFB_FreeCtx(MODES_CipherCtx * modeCtx)118 void MODES_OFB_FreeCtx(MODES_CipherCtx *modeCtx)
119 {
120     MODES_CipherFreeCtx(modeCtx);
121 }
122 
MODES_OFB_InitCtxEx(MODES_CipherCtx * modeCtx,const uint8_t * key,uint32_t keyLen,const uint8_t * iv,uint32_t ivLen,void * param,bool enc)123 int32_t MODES_OFB_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
124     uint32_t ivLen, void *param, bool enc)
125 {
126     (void) param;
127     if (modeCtx == NULL) {
128         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
129         return CRYPT_NULL_INPUT;
130     }
131     switch (modeCtx->algId) {
132         case CRYPT_CIPHER_SM4_OFB:
133 #ifdef HITLS_CRYPTO_SM4
134             return SM4_OFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc);
135 #else
136             return CRYPT_EAL_ALG_NOT_SUPPORT;
137 #endif
138         default:
139             return MODES_OFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc);
140     }
141 }
142 
MODES_OFB_UpdateEx(MODES_CipherCtx * modeCtx,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)143 int32_t MODES_OFB_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
144 {
145     if (modeCtx == NULL) {
146         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
147         return CRYPT_NULL_INPUT;
148     }
149     switch (modeCtx->algId) {
150         case CRYPT_CIPHER_SM4_OFB:
151 #ifdef HITLS_CRYPTO_SM4
152             return SM4_OFB_Update(modeCtx, in, inLen, out, outLen);
153 #else
154             return CRYPT_EAL_ALG_NOT_SUPPORT;
155 #endif
156         default:
157             return MODES_OFB_Update(modeCtx, in, inLen, out, outLen);
158     }
159 }
160 
161 #endif // HITLS_CRYPTO_OFB