Files
se-algo/inc/zuc256.h
2025-08-31 22:54:06 +08:00

59 lines
1.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __ZUC256_H
#define __ZUC256_H
#include <stdint.h>
#include <stdlib.h>
// 上下文结构体:存储算法状态
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;
// 错误码定义
#define ZUC256_SUCCESS 0 // 成功
#define ZUC256_ERR_NULL -1 // 空指针错误
#define ZUC256_ERR_KEYLEN -2 // 密钥长度错误(需32字节)
#define ZUC256_ERR_IVLEN -3 // IV长度错误(需16字节)
// 公共接口声明
/**
* @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);
/**
* @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);
/**
* @brief 结束处理(流密码无实际操作,仅对齐接口)
* @param ctx 上下文指针
* @param out 输出缓冲区(可为NULL)
* @return 成功返回ZUC256_SUCCESS失败返回错误码
*/
int ZUC256_Final(ZUC256_CTX *ctx, uint8_t *out);
/**
* @brief 清理上下文(清空敏感数据)
* @param ctx 上下文指针
*/
void ZUC256_Cleanup(ZUC256_CTX *ctx);
#endif /*__ZUC256_H */