符合国密ZCU256标准的算法,与网站对比
https://seehttps.com/gm/zuc256 https://tools.huijusa.cn/seq
This commit is contained in:
79
inc/zuc256.h
79
inc/zuc256.h
@@ -3,57 +3,48 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 上下文结构体:存储算法状态
|
||||
// 类型定义
|
||||
typedef uint32_t ZUC_UINT31; // 31位无符号整数
|
||||
typedef uint8_t ZUC_UINT7; // 7位无符号整数
|
||||
typedef uint8_t ZUC_UINT6; // 6位无符号整数
|
||||
|
||||
// ZUC状态结构体
|
||||
typedef struct {
|
||||
uint32_t key[8]; // 256位密钥(拆分为8个32位字)
|
||||
uint32_t iv[4]; // 128位IV(拆分为4个32位字)
|
||||
uint32_t LFSR[16]; // 线性反馈移位寄存器
|
||||
uint32_t NFSR[16]; // 非线性反馈移位寄存器
|
||||
uint32_t R1, R2; // 密钥流生成辅助寄存器
|
||||
uint32_t ks_buf; // 密钥流缓存(处理非4字节对齐数据)
|
||||
size_t ks_buf_len; // 缓存的密钥流字节数(0~3)
|
||||
} ZUC256_CTX;
|
||||
ZUC_UINT31 LFSR[16]; // 线性反馈移位寄存器
|
||||
uint32_t R1; // 寄存器1
|
||||
uint32_t R2; // 寄存器2
|
||||
} ZUC256_STATE;
|
||||
|
||||
// 错误码定义
|
||||
#define ZUC256_SUCCESS 0 // 成功
|
||||
#define ZUC256_ERR_NULL -1 // 空指针错误
|
||||
#define ZUC256_ERR_KEYLEN -2 // 密钥长度错误(需32字节)
|
||||
#define ZUC256_ERR_IVLEN -3 // IV长度错误(需16字节)
|
||||
// 加密上下文结构体(用于分阶段处理)
|
||||
typedef struct {
|
||||
ZUC256_STATE state; // 基础ZUC状态
|
||||
uint8_t buf[4]; // 输入缓冲区(处理非4字节对齐数据)
|
||||
size_t buflen; // 缓冲区中有效字节数
|
||||
} ZUC256_ENCRYPT_CTX;
|
||||
|
||||
// 公共接口声明
|
||||
/**
|
||||
* @brief 初始化ZUC256上下文
|
||||
* @param ctx 上下文指针(需提前分配内存)
|
||||
* @param key 256位密钥(32字节)
|
||||
* @param iv 128位初始向量(16字节)
|
||||
* @return 成功返回ZUC256_SUCCESS,失败返回错误码
|
||||
*/
|
||||
int ZUC256_Init(ZUC256_CTX *ctx, const uint8_t *key, const uint8_t *iv);
|
||||
// 初始化ZUC256状态
|
||||
void zuc256_init(ZUC256_STATE *state, const uint8_t K[32], const uint8_t IV[23]);
|
||||
|
||||
/**
|
||||
* @brief 分块处理数据(加密/解密)
|
||||
* @param ctx 已初始化的上下文
|
||||
* @param out 输出缓冲区(长度需≥in_len)
|
||||
* @param in 输入数据
|
||||
* @param in_len 输入数据长度(字节)
|
||||
* @return 成功返回ZUC256_SUCCESS,失败返回错误码
|
||||
*/
|
||||
int ZUC256_Update(ZUC256_CTX *ctx, uint8_t *out, const uint8_t *in, size_t in_len);
|
||||
// 生成单个密钥字
|
||||
uint32_t zuc256_generate_keyword(ZUC256_STATE *state);
|
||||
|
||||
/**
|
||||
* @brief 结束处理(流密码无实际操作,仅对齐接口)
|
||||
* @param ctx 上下文指针
|
||||
* @param out 输出缓冲区(可为NULL)
|
||||
* @return 成功返回ZUC256_SUCCESS,失败返回错误码
|
||||
*/
|
||||
int ZUC256_Final(ZUC256_CTX *ctx, uint8_t *out);
|
||||
// 生成指定长度的密钥流
|
||||
void zuc256_generate_keystream(ZUC256_STATE *state, size_t nwords, uint32_t *keystream);
|
||||
|
||||
/**
|
||||
* @brief 清理上下文(清空敏感数据)
|
||||
* @param ctx 上下文指针
|
||||
*/
|
||||
void ZUC256_Cleanup(ZUC256_CTX *ctx);
|
||||
// 初始化加密上下文
|
||||
void zuc256_encrypt_init(ZUC256_ENCRYPT_CTX *ctx, const uint8_t K[32], const uint8_t IV[23]);
|
||||
|
||||
// 分阶段处理加密数据(支持流式输入)
|
||||
void zuc256_encrypt_update(ZUC256_ENCRYPT_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
|
||||
// 完成加密处理(处理剩余数据并清理上下文)
|
||||
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 extract_iv(const uint8_t *input_25byte, uint8_t *output_23byte);
|
||||
#endif /*__ZUC256_H */
|
||||
|
||||
Reference in New Issue
Block a user