API参考
载入中...
搜索中...
未找到
crypt_modes_gcm.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_MODES_GCM_H
17#define CRYPT_MODES_GCM_H
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_GCM
21
22#include "crypt_types.h"
23#include "crypt_modes.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif // __cplusplus
28#define GCM_MAX_COMBINED_LENGTH (((uint64_t)1 << 36) - 32)
29#define GCM_MAX_INVOCATIONS_TIMES ((uint32_t)(-1))
30#define GCM_BLOCK_MASK (0xfffffff0)
31typedef struct {
32 uint64_t h;
33 uint64_t l;
34} MODES_GCM_GF128;
35#define GCM_BLOCKSIZE 16
36typedef struct {
37 uint8_t iv[GCM_BLOCKSIZE]; // Processed IV information. The length is 16 bytes.
38 uint8_t ghash[GCM_BLOCKSIZE]; // Intermediate data for tag calculation.
39 MODES_GCM_GF128 hTable[16]; // The window uses 4 bits, 2 ^ 4 = 16 entries need to be pre-calculated.
40 void *ciphCtx; // Context defined by each symmetric algorithm.
41 const EAL_SymMethod *ciphMeth; // algorithm method
42 uint8_t tagLen;
43 uint32_t cryptCnt; // Indicate the number of encryption times that the key can be used.
44 uint8_t last[GCM_BLOCKSIZE]; // ctr mode last
45 uint8_t remCt[GCM_BLOCKSIZE]; // Remaining ciphertext
46 uint8_t ek0[GCM_BLOCKSIZE]; // ek0
47 uint64_t plaintextLen; // use for calc tag
48 uint32_t aadLen; // use for calc tag
49 uint32_t lastLen; // ctr mode lastLen
50} MODES_CipherGCMCtx;
51
52struct ModesGcmCtx {
53 int32_t algId;
54 MODES_CipherGCMCtx gcmCtx;
55 bool enc;
56};
57
58typedef struct ModesGcmCtx MODES_GCM_Ctx;
59
60// GCM mode universal implementation
61MODES_GCM_Ctx *MODES_GCM_NewCtx(int32_t algId);
62MODES_GCM_Ctx *MODES_GCM_NewCtxEx(void *libCtx, int32_t algId);
63int32_t MODES_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
64 uint32_t ivLen, bool enc);
65
66int32_t MODES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
67int32_t MODES_GCM_Final(MODES_GCM_Ctx *modeCtx, uint8_t *out, uint32_t *outLen);
68int32_t MODES_GCM_DeInitCtx(MODES_GCM_Ctx *modeCtx);
69int32_t MODES_GCM_Ctrl(MODES_GCM_Ctx *modeCtx, int32_t cmd, void *val, uint32_t len);
70void MODES_GCM_FreeCtx(MODES_GCM_Ctx *modeCtx);
71
72// AES GCM optimization implementation
73int32_t AES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
74
75// SM4 GCM optimization implementation
76int32_t SM4_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
77 uint32_t ivLen, bool enc);
78int32_t SM4_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
79
80int32_t MODES_GCM_InitCtxEx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv,
81 uint32_t ivLen, void *param, bool enc);
82
83int32_t MODES_GCM_UpdateEx(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
84
85int32_t MODES_GCM_InitHashTable(MODES_CipherGCMCtx *ctx);
86int32_t MODES_GCM_SetKey(MODES_CipherGCMCtx *ctx, const uint8_t *key, uint32_t len);
87MODES_GCM_Ctx *MODES_GCM_DupCtx(const MODES_GCM_Ctx *modeCtx);
88
89#ifdef __cplusplus
90}
91#endif // __cplusplus
92
93#endif // HITLS_CRYPTO_GCM
94
95#endif // CRYPT_MODES_GCM_H
定义 crypt_local_types.h:241