API参考
载入中...
搜索中...
未找到
crypt_sm4.h
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
29extern "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
36typedef struct {
37 uint8_t iv[CRYPT_SM4_BLOCKSIZE];
38 uint32_t rk[CRYPT_SM4_ROUNDS];
39} CRYPT_SM4_Ctx;
40
50int32_t CRYPT_SM4_SetKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t keyLen);
51
62int32_t CRYPT_SM4_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length);
63
74int32_t CRYPT_SM4_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length);
75
81void CRYPT_SM4_Clean(CRYPT_SM4_Ctx *ctx);
82
83#ifdef HITLS_CRYPTO_XTS
95int32_t CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len);
96
106int32_t CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len);
107
113void CRYPT_SM4_XTS_Clean(CRYPT_SM4_Ctx *ctx);
114
127int32_t CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv);
128
141int32_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
153int32_t CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len);
154
164int32_t CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len);
165
166#ifdef HITLS_CRYPTO_ECB
177int32_t CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
178
189int32_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
204int32_t CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv);
205
217int32_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)
232int32_t CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv);
233
245int32_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
261int32_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
275int32_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
291int32_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
305int32_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