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 #ifndef CRYPT_SM4_H 17 #define CRYPT_SM4_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_SM4 21 22 #include <stdint.h> 23 #include <string.h> 24 #include <stdbool.h> 25 #include "crypt_types.h" 26 #include "crypt_local_types.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif /* __cplusplus */ 31 32 #define CRYPT_SM4_BLOCKSIZE 16 33 #define CRYPT_SM4_BLOCKSIZE_16 256 34 #define CRYPT_SM4_ROUNDS 32 35 36 typedef struct { 37 uint8_t iv[CRYPT_SM4_BLOCKSIZE]; 38 uint32_t rk[CRYPT_SM4_ROUNDS]; 39 } CRYPT_SM4_Ctx; 40 41 /** 42 * @brief SM4 Set the encryption and decryption key. 43 * 44 * @param [IN] ctx SM4 context 45 * @param [IN] key Key 46 * @param [IN] keyLen Key length 47 * @return Success: CRYPT_SUCCESS 48 * Other error codes are returned if the operation fails. 49 */ 50 int32_t CRYPT_SM4_SetKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t keyLen); 51 52 /** 53 * @brief SM4 encryption. The data length must be an integer multiple of 16. 54 * 55 * @param [IN] ctx SM4 context 56 * @param [IN] in Data to be encrypted 57 * @param [OUT] out Encrypted data 58 * @param [IN] length Data length 59 * @return Success: CRYPT_SUCCESS 60 * Other error codes are returned if the operation fails. 61 */ 62 int32_t CRYPT_SM4_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length); 63 64 /** 65 * @brief SM4 decryption. The data length must be an integer multiple of 16. 66 * 67 * @param [IN] ctx SM4 context 68 * @param [IN] in Data to be decrypted 69 * @param [OUT] out Decrypted Data 70 * @param [IN] length Data length 71 * @return Success: CRYPT_SUCCESS 72 * Other error codes are returned if the operation fails. 73 */ 74 int32_t CRYPT_SM4_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length); 75 76 /** 77 * @brief Clear the SM4 context 78 * 79 * @param [IN] ctx sm4 context 80 */ 81 void CRYPT_SM4_Clean(CRYPT_SM4_Ctx *ctx); 82 83 #ifdef HITLS_CRYPTO_XTS 84 /** 85 * @brief SM4 Set the encryption key. 86 * 87 * @param ctx [IN] sm4 Context 88 * @param key [IN] Key. The first 16 bytes are data_key, and the last 16 bytes are tweak_key. 89 * @param keyLen [IN] Key length 90 * 91 * @retval #CRYPT_SUCCESS succeeded. 92 * @retval #CRYPT_NULL_INPUT ctx or key is NULL. 93 * @retval #CRYPT_SM4_ERR_KEY_LEN The key length is not equal to 32. 94 */ 95 int32_t CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); 96 97 /** 98 * @brief SM4 Set the decryption key. 99 * 100 * @param ctx [IN] sm4 Context 101 * @param key [IN] Key 102 * @param keyLen [IN] Key length 103 * @return Success: CRYPT_SUCCESS 104 * Other error codes are returned if the operation fails. 105 */ 106 int32_t CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); 107 108 /** 109 * @brief Clear SM4_xts context 110 * 111 * @param [IN] ctx sm4 context 112 */ 113 void CRYPT_SM4_XTS_Clean(CRYPT_SM4_Ctx *ctx); 114 115 /** 116 * @brief SM4 XTS mode encryption 117 * @param ctx [IN] sm4 Context 118 * @param in [IN] Data to be decrypted 119 * @param out [OUT] Decrypted data 120 * @param len [IN] Length of the decrypted data 121 * @param iv [IN] Set IV 122 * 123 * @retval #CRYPT_SUCCESS succeeded. 124 * @retval #CRYPT_NULL_INPUT ctx,in,out is NULL 125 * @retval #CRYPT_SM4_DATALEN_ERROR The length of the decrypted data is less than 16 bytes. 126 */ 127 int32_t CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 128 129 /** 130 * @brief SM4 XTS mode encryption 131 * @param ctx [IN] sm4 Context 132 * @param in [IN] Data to be encrypted 133 * @param out [OUT] Encrypted data 134 * @param len [IN] Length of the encrypted data 135 * @param iv [IN] Set IV 136 * 137 * @retval #CRYPT_SUCCESS succeeded. 138 * @retval #CRYPT_NULL_INPUT ctx/in/out is NULL 139 * @retval #CRYPT_SM4_DATALEN_ERROR The length of the encrypted data is less than 16. 140 */ 141 int32_t CRYPT_SM4_XTS_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 142 #endif 143 144 /** 145 * @brief SM4 Set the encryption key (optimized). 146 * 147 * @param [IN] ctx SM4 context 148 * @param [IN] key Key 149 * @param [IN] len Key length 150 * @return Success: CRYPT_SUCCESS 151 * Other error codes are returned if the operation fails. 152 */ 153 int32_t CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); 154 155 /** 156 * @brief SM4 Set the decryption key (optimized). 157 * 158 * @param [IN] ctx SM4 context 159 * @param [IN] key Key 160 * @param [IN] len Key length 161 * @return Success: CRYPT_SUCCESS 162 * Other error codes are returned if the operation fails. 163 */ 164 int32_t CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); 165 166 #ifdef HITLS_CRYPTO_ECB 167 /** 168 * @brief SM4 ECB mode encryption (optimized). 169 * @param ctx [IN] sm4 Context 170 * @param in [IN] Data to be encrypted 171 * @param out [OUT] Encrypted data 172 * @param len [IN] Length of the encrypted data 173 * 174 * @return Success: CRYPT_SUCCESS 175 * Other error codes are returned if the operation fails. 176 */ 177 int32_t CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); 178 179 /** 180 * @brief SM4 ECB mode decryption (optimized). 181 * @param ctx [IN] sm4 Context 182 * @param in [IN] Data to be decrypted 183 * @param out [OUT] Decrypted data 184 * @param len [IN] Length of the decrypted data 185 * 186 * @return Success: CRYPT_SUCCESS 187 * Other error codes are returned if the operation fails. 188 */ 189 int32_t CRYPT_SM4_ECB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); 190 #endif 191 192 #ifdef HITLS_CRYPTO_CBC 193 /** 194 * @brief SM4 CBC mode encryption (optimized). 195 * @param ctx [IN] sm4 Context 196 * @param in [IN] Data to be encrypted 197 * @param out [OUT] Encrypted data 198 * @param len [IN] Length of the encrypted data 199 * @param iv [IN] Set IV 200 * 201 * @return Success: CRYPT_SUCCESS 202 * Other error codes are returned if the operation fails. 203 */ 204 int32_t CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 205 206 /** 207 * @brief SM4 CBC mode decryption (optimized). 208 * @param ctx [IN] sm4 Context 209 * @param in [IN] Data to be decrypted 210 * @param out [OUT] decrypted data 211 * @param len [IN] Length of the decrypted data 212 * @param iv [IN] Set IV 213 * 214 * @return Success: CRYPT_SUCCESS 215 * Other error codes are returned if the operation fails. 216 */ 217 int32_t CRYPT_SM4_CBC_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 218 #endif 219 220 #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) 221 /** 222 * @brief SM4 CTR mode encryption (optimized). 223 * @param ctx [IN] sm4 Context 224 * @param in [IN] Data to be encrypted 225 * @param out [OUT] Encrypted data 226 * @param len [IN] Length of the encrypted data 227 * @param iv [IN] Set IV 228 * 229 * @return Success: CRYPT_SUCCESS 230 * Other error codes are returned if the operation fails. 231 */ 232 int32_t CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 233 234 /** 235 * @brief SM4 CTR mode decryption (optimized). 236 * @param ctx [IN] sm4 Context 237 * @param in [IN] Data to be decrypted 238 * @param out [OUT] decrypted data 239 * @param len [IN] Length of the decrypted data 240 * @param iv [IN] Set IV 241 * 242 * @return Success: CRYPT_SUCCESS 243 * Other error codes are returned if the operation fails. 244 */ 245 int32_t CRYPT_SM4_CTR_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 246 #endif 247 248 #ifdef HITLS_CRYPTO_OFB 249 /** 250 * @brief SM4 OFB mode encryption (optimized). 251 * @param ctx [IN] sm4 Context 252 * @param in [IN] Data to be encrypted 253 * @param out [OUT] Encrypted data 254 * @param len [IN] Length of the encrypted data 255 * @param iv [IN] Set IV 256 * @param offset [OUT] Length of less than one block 257 * 258 * @return Success: CRYPT_SUCCESS 259 * Other error codes are returned if the operation fails. 260 */ 261 int32_t CRYPT_SM4_OFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset); 262 263 /** 264 * @brief SM4 OFB mode decryption (optimized). 265 * @param ctx [IN] sm4 Context 266 * @param in [IN] Data to be decrypted 267 * @param out [OUT] decrypted data 268 * @param len [IN] Length of the decrypted data 269 * @param iv [IN] Set IV 270 * @param offset [OUT] Length of less than one block 271 * 272 * @return Success: CRYPT_SUCCESS 273 * Other error codes are returned if the operation fails. 274 */ 275 int32_t CRYPT_SM4_OFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset); 276 #endif 277 278 #ifdef HITLS_CRYPTO_CFB 279 /** 280 * @brief SM4 CFB mode encryption (optimized). 281 * @param ctx [IN] sm4 Context 282 * @param in [IN] Data to be encrypted 283 * @param out [OUT] Encrypted data 284 * @param len [IN] Length of the encrypted data 285 * @param iv [IN] Set IV 286 * @param offset [OUT] Length of less than one block. 287 * 288 * @return Success: CRYPT_SUCCESS 289 * Other error codes are returned if the operation fails. 290 */ 291 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); 292 293 /** 294 * @brief SM4 CFB mode decryption (optimized). 295 * @param ctx [IN] sm4 Context 296 * @param in [IN] Data to be decrypted 297 * @param out [OUT] decrypted data 298 * @param len [IN] Length of the decrypted data 299 * @param iv [IN] Set IV 300 * @param offset [OUT] Length of less than one block. 301 * 302 * @return Success: CRYPT_SUCCESS 303 * Other error codes are returned if the operation fails. 304 */ 305 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); 306 #endif 307 308 #ifdef __cplusplus 309 } 310 #endif /* __cplusplus */ 311 312 #endif // HITLS_CRYPTO_SM4 313 314 #endif // CRYPT_SM4_H