This commit is contained in:
qzh
2025-09-01 14:35:38 +08:00
parent ec0fc7b2c4
commit 4873142634
3 changed files with 408 additions and 195 deletions

View File

@@ -9,29 +9,40 @@
typedef uint32_t ZUC_UINT31; // 31位无符号整数
typedef uint8_t ZUC_UINT7; // 7位无符号整数
typedef uint8_t ZUC_UINT6; // 6位无符号整数
typedef uint32_t ZUC_UINT32; // 32位无符号整数
// ZUC状态结构体
typedef struct {
ZUC_UINT31 LFSR[16]; // 线性反馈移位寄存器
uint32_t R1; // 寄存器1
uint32_t R2; // 寄存器2
} ZUC256_STATE;
} ZUC_STATE;
typedef ZUC_STATE ZUC256_STATE;
// 加密上下文结构体(用于分阶段处理)
typedef struct {
ZUC256_STATE state; // 基础ZUC状态
ZUC_STATE state; // 基础ZUC状态
uint8_t buf[4]; // 输入缓冲区(处理非4字节对齐数据)
size_t buflen; // 缓冲区中有效字节数
} ZUC256_ENCRYPT_CTX;
// ZUC256 MAC 上下文结构体
typedef struct {
ZUC_UINT31 LFSR[16]; // ZUC256 线性反馈移位寄存器
uint32_t R1; // 非线性函数寄存器R1
uint32_t R2; // 非线性函数寄存器R2
uint8_t buf[4]; // 数据缓存处理不足4字节的待认证数据
size_t buflen; // 缓存中有效数据长度0~3
uint32_t T[4]; // MAC 累加器支持最大128位MAC4个32位字
uint32_t K0[4]; // MAC 初始密钥字与T长度匹配
int macbits; // MAC 输出位数32/64/128按32位对齐
} ZUC256_MAC_CTX;
// 初始化ZUC256状态
void zuc256_init(ZUC256_STATE *state, const uint8_t K[32], const uint8_t IV[23]);
void zuc256_init(ZUC_STATE *state, const uint8_t K[32], const uint8_t IV[23]);
// 生成单个密钥字
uint32_t zuc256_generate_keyword(ZUC256_STATE *state);
uint32_t zuc256_generate_keyword(ZUC_STATE *state);
// 生成指定长度的密钥流
void zuc256_generate_keystream(ZUC256_STATE *state, size_t nwords, uint32_t *keystream);
void zuc256_generate_keystream(ZUC_STATE *state, size_t nwords, uint32_t *keystream);
// 初始化加密上下文
void zuc256_encrypt_init(ZUC256_ENCRYPT_CTX *ctx, const uint8_t K[32], const uint8_t IV[23]);
@@ -43,8 +54,7 @@ void zuc256_encrypt_update(ZUC256_ENCRYPT_CTX *ctx, const uint8_t *in, size_t in
void zuc256_encrypt_finish(ZUC256_ENCRYPT_CTX *ctx, uint8_t *out);
// 一次性加密函数
void zuc256_crypt(ZUC256_STATE *state, const uint8_t *in, size_t inlen, uint8_t *out);
void zuc256_crypt(ZUC_STATE *state, const uint8_t *in, size_t inlen, uint8_t *out);
void extract_iv(const uint8_t *input_25byte, uint8_t *output_23byte);
#endif /*__ZUC256_H */