更新代码方案
This commit is contained in:
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"uart.h": "c",
|
||||
"ram_test.h": "c",
|
||||
"stdio.h": "c"
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
## 📦 环境准备
|
||||
|
||||
确保本地已安装:
|
||||
确保本地已安装:
|
||||
- scons 构建工具,用于自动化编译流程。
|
||||
- build-essential 包含 gcc、make 等基础编译工具的依赖包
|
||||
```bash
|
||||
|
||||
@@ -7,7 +7,7 @@ Import('env')
|
||||
# 项目根目录
|
||||
CWD = os.getcwd()
|
||||
|
||||
# 收集源码(.c 和 .S 文件)
|
||||
# 收集源码(.c 和 .S 文件)
|
||||
sources = Glob(os.path.join(CWD, 'src/*.c')) + Glob(os.path.join(CWD, 'src/*.S'))
|
||||
|
||||
# 编译选项
|
||||
@@ -21,7 +21,7 @@ BUILD_DIR = 'build'
|
||||
if not os.path.exists(BUILD_DIR):
|
||||
os.makedirs(BUILD_DIR)
|
||||
|
||||
# 逐个编译源码生成目标文件(输出到 build 目录)
|
||||
# 逐个编译源码生成目标文件(输出到 build 目录)
|
||||
objs = []
|
||||
for src in sources:
|
||||
src_path = str(src) # 转换为字符串路径
|
||||
|
||||
10
SConstruct
10
SConstruct
@@ -6,7 +6,7 @@ BUILD_DIR = 'build'
|
||||
if not os.path.exists(BUILD_DIR):
|
||||
os.makedirs(BUILD_DIR)
|
||||
|
||||
# 导入配置(修改为导入algo.py)
|
||||
# 导入配置(修改为导入algo.py)
|
||||
from algo import *
|
||||
|
||||
# 初始化构建环境
|
||||
@@ -27,21 +27,21 @@ env = Environment(
|
||||
# 递归构建源码(SConscript 负责收集编译文件)
|
||||
src_objs = env.SConscript('SConscript', exports='env')
|
||||
|
||||
# 链接生成 elf 文件(输出到 build 目录)
|
||||
# 链接生成 elf 文件(输出到 build 目录)
|
||||
elf_target = os.path.join(BUILD_DIR, TARGET_NAME)
|
||||
elf_file = env.Program(elf_target, src_objs)
|
||||
|
||||
# 后处理:生成 bin、asm 等文件
|
||||
# 后处理:生成 bin、asm 等文件
|
||||
post_commands = env.Command(
|
||||
os.path.join(BUILD_DIR, 'post_actions'), # 虚拟目标,确保命令执行
|
||||
elf_file,
|
||||
POST_ACTION
|
||||
)
|
||||
|
||||
# 关联默认构建目标(执行 scons 时默认编译+后处理)
|
||||
# 关联默认构建目标(执行 scons 时默认编译+后处理)
|
||||
Default(elf_file)
|
||||
Default(post_commands)
|
||||
|
||||
# 清理规则(删除 build 目录及内容)
|
||||
# 清理规则(删除 build 目录及内容)
|
||||
Clean(elf_file, BUILD_DIR)
|
||||
|
||||
|
||||
8
algo.py
8
algo.py
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
from SCons.Script import *
|
||||
|
||||
# 工具链配置(Ubuntu gcc环境)
|
||||
# 工具链配置(Ubuntu gcc环境)
|
||||
CROSS_TOOL = 'gcc'
|
||||
PREFIX = '' # 本地编译不需要交叉编译前缀
|
||||
|
||||
@@ -9,7 +9,7 @@ PREFIX = '' # 本地编译不需要交叉编译前缀
|
||||
TARGET_NAME = 'app' # 更改为通用应用名称
|
||||
BUILD_DIR = 'build' # 统一构建输出目录
|
||||
|
||||
# 工具链路径(使用系统默认的gcc工具链)
|
||||
# 工具链路径(使用系统默认的gcc工具链)
|
||||
CC = PREFIX + 'gcc'
|
||||
CXX = PREFIX + 'g++'
|
||||
AS = PREFIX + 'gcc'
|
||||
@@ -19,14 +19,14 @@ SIZE = PREFIX + 'size'
|
||||
OBJDUMP = PREFIX + 'objdump'
|
||||
OBJCPY = PREFIX + 'objcopy'
|
||||
|
||||
# 编译、链接选项(适用于Ubuntu gcc环境)
|
||||
# 编译、链接选项(适用于Ubuntu gcc环境)
|
||||
DEVICE = '-std=c99 -g -O2 -fvisibility=hidden -fno-common'
|
||||
CFLAGS = DEVICE + ' -Wall -Wextra' # 添加更多警告选项
|
||||
AFLAGS = '-c ' + DEVICE + ' -x assembler-with-cpp'
|
||||
LFLAGS = DEVICE + ' -Wl,--gc-sections,-cref,-Map=' + os.path.join(BUILD_DIR, TARGET_NAME + '.map')
|
||||
|
||||
|
||||
# 构建后处理命令(输出到 build 目录)
|
||||
# 构建后处理命令(输出到 build 目录)
|
||||
ASM_FILE = os.path.join(BUILD_DIR, TARGET_NAME + '.asm')
|
||||
BIN_FILE = os.path.join(BUILD_DIR, TARGET_NAME + '.bin')
|
||||
|
||||
|
||||
67
inc/zuc256.h
67
inc/zuc256.h
@@ -1,36 +1,59 @@
|
||||
#ifndef ZUC256_H
|
||||
#define ZUC256_H
|
||||
#ifndef __ZUC256_H
|
||||
#define __ZUC256_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// ZUC256算法上下文结构
|
||||
// 上下文结构体:存储算法状态
|
||||
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 key[8]; // 256位密钥 (8*32)
|
||||
uint32_t iv[4]; // 128位初始向量 (4*32)
|
||||
} zuc256_context;
|
||||
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 初始化ZUC256算法上下文
|
||||
*
|
||||
* @param ctx 算法上下文指针
|
||||
* @param key 256位密钥(32字节)
|
||||
* @param iv 128位初始向量(16字节)
|
||||
* @brief 分块处理数据(加密/解密)
|
||||
* @param ctx 已初始化的上下文
|
||||
* @param out 输出缓冲区(长度需≥in_len)
|
||||
* @param in 输入数据
|
||||
* @param in_len 输入数据长度(字节)
|
||||
* @return 成功返回ZUC256_SUCCESS,失败返回错误码
|
||||
*/
|
||||
void zuc256_init(zuc256_context *ctx, const uint8_t *key, const uint8_t *iv);
|
||||
int ZUC256_Update(ZUC256_CTX *ctx, uint8_t *out, const uint8_t *in, size_t in_len);
|
||||
|
||||
/**
|
||||
* @brief 使用ZUC256算法进行加密或解密
|
||||
*
|
||||
* @param ctx 算法上下文指针
|
||||
* @param input 输入数据
|
||||
* @param output 输出数据(与输入数据长度相同)
|
||||
* @param length 数据长度(字节数)
|
||||
* @brief 结束处理(流密码无实际操作,仅对齐接口)
|
||||
* @param ctx 上下文指针
|
||||
* @param out 输出缓冲区(可为NULL)
|
||||
* @return 成功返回ZUC256_SUCCESS,失败返回错误码
|
||||
*/
|
||||
void zuc256_crypt(zuc256_context *ctx, const uint8_t *input, uint8_t *output, size_t length);
|
||||
int ZUC256_Final(ZUC256_CTX *ctx, uint8_t *out);
|
||||
|
||||
#endif // ZUC256_H
|
||||
/**
|
||||
* @brief 清理上下文(清空敏感数据)
|
||||
* @param ctx 上下文指针
|
||||
*/
|
||||
void ZUC256_Cleanup(ZUC256_CTX *ctx);
|
||||
|
||||
#endif /*__ZUC256_H */
|
||||
|
||||
91
src/main.c
91
src/main.c
@@ -1,39 +1,74 @@
|
||||
#include "zuc256.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "zuc256.h"
|
||||
|
||||
int main(void) {
|
||||
// 256位密钥(32字节)
|
||||
uint8_t key[32] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
||||
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
|
||||
};
|
||||
// 测试ZUC256加密解密功能
|
||||
int main() {
|
||||
// 1. 测试参数(256位密钥=32字节,128位IV=16字节)
|
||||
uint8_t key[32] = "0123456789abcdef0123456789abcdef"; // 32字节密钥
|
||||
uint8_t iv[16] = "0123456789abcdef"; // 16字节IV
|
||||
uint8_t plaintext[] = "Hello ZUC256! This is a test of the encryption algorithm.";
|
||||
size_t plaintext_len = strlen((const char *)plaintext);
|
||||
uint8_t ciphertext[1024] = {0}; // 密文缓冲区
|
||||
uint8_t decrypted[1024] = {0}; // 解密后缓冲区
|
||||
|
||||
// 128位初始向量(16字节)
|
||||
uint8_t iv[16] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
// 2. 初始化加密上下文并加密
|
||||
ZUC256_CTX enc_ctx;
|
||||
int ret = ZUC256_Init(&enc_ctx, key, iv);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("加密初始化失败!错误码: %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 待加密数据
|
||||
uint8_t plaintext[] = "Hello, ZUC256 algorithm!";
|
||||
uint8_t ciphertext[sizeof(plaintext)];
|
||||
uint8_t decrypted[sizeof(plaintext)];
|
||||
ret = ZUC256_Update(&enc_ctx, ciphertext, plaintext, plaintext_len);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("加密处理失败!错误码: %d\n", ret);
|
||||
ZUC256_Cleanup(&enc_ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
zuc256_context ctx;
|
||||
ret = ZUC256_Final(&enc_ctx, NULL);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("加密结束失败!错误码: %d\n", ret);
|
||||
}
|
||||
ZUC256_Cleanup(&enc_ctx); // 清理加密上下文(敏感数据)
|
||||
|
||||
// 加密过程
|
||||
zuc256_init(&ctx, key, iv);
|
||||
zuc256_crypt(&ctx, plaintext, ciphertext, sizeof(plaintext));
|
||||
// 3. 初始化解密上下文并解密(流密码加密解密使用相同接口)
|
||||
ZUC256_CTX dec_ctx;
|
||||
ret = ZUC256_Init(&dec_ctx, key, iv);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("解密初始化失败!错误码: %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 解密过程(使用相同的密钥和IV重新初始化)
|
||||
zuc256_init(&ctx, key, iv);
|
||||
zuc256_crypt(&ctx, ciphertext, decrypted, sizeof(plaintext));
|
||||
ret = ZUC256_Update(&dec_ctx, decrypted, ciphertext, plaintext_len);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("解密处理失败!错误码: %d\n", ret);
|
||||
ZUC256_Cleanup(&dec_ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 输出结果
|
||||
printf("Original: %s\n", plaintext);
|
||||
printf("Decrypted: %s\n", decrypted);
|
||||
ret = ZUC256_Final(&dec_ctx, NULL);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("解密结束失败!错误码: %d\n", ret);
|
||||
}
|
||||
ZUC256_Cleanup(&dec_ctx); // 清理解密上下文
|
||||
|
||||
// 4. 输出结果并验证
|
||||
printf("原文: %s\n", plaintext);
|
||||
printf("密文: ");
|
||||
for (size_t i = 0; i < plaintext_len; i++) {
|
||||
printf("%02x", ciphertext[i]); // 密文以十六进制显示
|
||||
}
|
||||
printf("\n解密后: %s\n", decrypted);
|
||||
|
||||
// 验证解密结果是否与原文一致
|
||||
if (memcmp(plaintext, decrypted, plaintext_len) == 0) {
|
||||
printf("\n测试通过:加密解密一致!\n");
|
||||
} else {
|
||||
printf("\n测试失败:解密结果与原文不一致!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
350
src/zuc256.c
350
src/zuc256.c
@@ -1,8 +1,10 @@
|
||||
#include "zuc256.h"
|
||||
#include <string.h>
|
||||
|
||||
// S盒置换表
|
||||
static const uint8_t S0[256] = {
|
||||
// 内部工具函数和常量(static隐藏,不暴露给外部)
|
||||
|
||||
// S盒(ZUC标准S盒与AES一致)
|
||||
static const uint8_t ZUC256_S0[256] = {
|
||||
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
||||
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
||||
@@ -21,7 +23,7 @@ static const uint8_t S0[256] = {
|
||||
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
|
||||
};
|
||||
|
||||
static const uint8_t S1[256] = {
|
||||
static const uint8_t ZUC256_S1[256] = {
|
||||
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
||||
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
|
||||
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||
@@ -40,205 +42,221 @@ static const uint8_t S1[256] = {
|
||||
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
|
||||
};
|
||||
|
||||
// 循环左移
|
||||
static uint32_t rotate_left(uint32_t value, uint8_t shift) {
|
||||
// 循环左移工具函数
|
||||
static uint32_t zuc256_rotl(uint32_t value, uint8_t shift) {
|
||||
return ((value << shift) | (value >> (32 - shift)));
|
||||
}
|
||||
|
||||
// 非线性函数F
|
||||
static uint32_t F(uint32_t X0, uint32_t X1, uint32_t X2) {
|
||||
uint32_t W, W1, W2, temp;
|
||||
// 非线性函数F(ZUC核心变换)
|
||||
static uint32_t zuc256_F(uint32_t X0, uint32_t X1, uint32_t X2) {
|
||||
uint32_t W1, W2, temp;
|
||||
uint8_t b[4], c[4];
|
||||
|
||||
// 分解X0为字节
|
||||
// 分解X0→字节→S0置换
|
||||
b[0] = (X0 >> 24) & 0xFF;
|
||||
b[1] = (X0 >> 16) & 0xFF;
|
||||
b[2] = (X0 >> 8) & 0xFF;
|
||||
b[3] = X0 & 0xFF;
|
||||
b[0] = ZUC256_S0[b[0]];
|
||||
b[1] = ZUC256_S0[b[1]];
|
||||
b[2] = ZUC256_S0[b[2]];
|
||||
b[3] = ZUC256_S0[b[3]];
|
||||
W1 = ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3];
|
||||
|
||||
// S0置换
|
||||
b[0] = S0[b[0]];
|
||||
b[1] = S0[b[1]];
|
||||
b[2] = S0[b[2]];
|
||||
b[3] = S0[b[3]];
|
||||
|
||||
// 重组为W1
|
||||
W1 = ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
|
||||
((uint32_t)b[2] << 8) | (uint32_t)b[3];
|
||||
|
||||
// 分解X1为字节
|
||||
// 分解X1→字节→S1置换
|
||||
c[0] = (X1 >> 24) & 0xFF;
|
||||
c[1] = (X1 >> 16) & 0xFF;
|
||||
c[2] = (X1 >> 8) & 0xFF;
|
||||
c[3] = X1 & 0xFF;
|
||||
c[0] = ZUC256_S1[c[0]];
|
||||
c[1] = ZUC256_S1[c[1]];
|
||||
c[2] = ZUC256_S1[c[2]];
|
||||
c[3] = ZUC256_S1[c[3]];
|
||||
W2 = ((uint32_t)c[0] << 24) | ((uint32_t)c[1] << 16) | ((uint32_t)c[2] << 8) | c[3];
|
||||
|
||||
// S1置换
|
||||
c[0] = S1[c[0]];
|
||||
c[1] = S1[c[1]];
|
||||
c[2] = S1[c[2]];
|
||||
c[3] = S1[c[3]];
|
||||
|
||||
// 重组为W2
|
||||
W2 = ((uint32_t)c[0] << 24) | ((uint32_t)c[1] << 16) |
|
||||
((uint32_t)c[2] << 8) | (uint32_t)c[3];
|
||||
|
||||
// 计算W
|
||||
W = W1 ^ W2;
|
||||
|
||||
// 与X2混合
|
||||
temp = W ^ rotate_left(W, 16);
|
||||
temp ^= rotate_left(W, 12);
|
||||
temp ^= rotate_left(W, 8);
|
||||
temp ^= rotate_left(W, 7);
|
||||
|
||||
// ZUC标准F函数计算
|
||||
temp = (W1 ^ W2) ^ zuc256_rotl(W1 ^ W2, 16) ^ zuc256_rotl(W1 ^ W2, 12)
|
||||
^ zuc256_rotl(W1 ^ W2, 8) ^ zuc256_rotl(W1 ^ W2, 7);
|
||||
return temp ^ X2;
|
||||
}
|
||||
|
||||
// 初始化LFSR和NFSR
|
||||
static void initialize_registers(zuc256_context *ctx) {
|
||||
int i;
|
||||
uint32_t f_result;
|
||||
|
||||
// 加载密钥和IV到LFSR和NFSR
|
||||
for (i = 0; i < 8; i++) {
|
||||
ctx->LFSR[i] = ctx->key[i];
|
||||
ctx->NFSR[i] = ctx->key[i];
|
||||
// 生成32位密钥流字(内部使用)
|
||||
static int zuc256_gen_ks_word(ZUC256_CTX *ctx, uint32_t *ks_word) {
|
||||
if (ctx == NULL || ks_word == NULL) {
|
||||
return ZUC256_ERR_NULL;
|
||||
}
|
||||
|
||||
for (i = 8; i < 16; i++) {
|
||||
ctx->LFSR[i] = ctx->iv[i - 8];
|
||||
ctx->NFSR[i] = ctx->iv[i - 8];
|
||||
}
|
||||
|
||||
// 初始化阶段:32次迭代
|
||||
for (i = 0; i < 32; i++) {
|
||||
// 计算F函数输入
|
||||
uint32_t X0 = ctx->NFSR[15];
|
||||
uint32_t X1 = ctx->LFSR[15] ^ ctx->NFSR[11];
|
||||
uint32_t X2 = ctx->LFSR[14];
|
||||
|
||||
// 计算F函数结果
|
||||
f_result = F(X0, X1, X2);
|
||||
|
||||
// LFSR移位
|
||||
uint32_t lfsr_feedback = ctx->LFSR[15] ^
|
||||
(ctx->LFSR[11] & ctx->LFSR[12]) ^
|
||||
ctx->LFSR[9] ^ f_result;
|
||||
|
||||
for (int j = 15; j > 0; j--) {
|
||||
ctx->LFSR[j] = ctx->LFSR[j - 1];
|
||||
}
|
||||
ctx->LFSR[0] = lfsr_feedback;
|
||||
|
||||
// NFSR移位
|
||||
uint32_t nfsr_feedback = ctx->NFSR[15] ^
|
||||
ctx->NFSR[13] ^
|
||||
ctx->NFSR[10] ^
|
||||
ctx->NFSR[0] ^
|
||||
(ctx->NFSR[0] & ctx->NFSR[2]) ^
|
||||
(ctx->NFSR[1] & ctx->NFSR[3]) ^
|
||||
(ctx->NFSR[0] & ctx->NFSR[1] & ctx->NFSR[4]) ^
|
||||
f_result;
|
||||
|
||||
for (int j = 15; j > 0; j--) {
|
||||
ctx->NFSR[j] = ctx->NFSR[j - 1];
|
||||
}
|
||||
ctx->NFSR[0] = nfsr_feedback;
|
||||
}
|
||||
|
||||
// 初始化R1和R2
|
||||
ctx->R1 = 0;
|
||||
ctx->R2 = 0;
|
||||
}
|
||||
|
||||
// 初始化ZUC256上下文
|
||||
void zuc256_init(zuc256_context *ctx, const uint8_t *key, const uint8_t *iv) {
|
||||
int i;
|
||||
|
||||
// 清空上下文
|
||||
memset(ctx, 0, sizeof(zuc256_context));
|
||||
|
||||
// 加载256位密钥 (8个32位字)
|
||||
for (i = 0; i < 8; i++) {
|
||||
ctx->key[i] = ((uint32_t)key[4*i] << 24) |
|
||||
((uint32_t)key[4*i + 1] << 16) |
|
||||
((uint32_t)key[4*i + 2] << 8) |
|
||||
(uint32_t)key[4*i + 3];
|
||||
}
|
||||
|
||||
// 加载128位IV (4个32位字)
|
||||
for (i = 0; i < 4; i++) {
|
||||
ctx->iv[i] = ((uint32_t)iv[4*i] << 24) |
|
||||
((uint32_t)iv[4*i + 1] << 16) |
|
||||
((uint32_t)iv[4*i + 2] << 8) |
|
||||
(uint32_t)iv[4*i + 3];
|
||||
}
|
||||
|
||||
// 初始化寄存器
|
||||
initialize_registers(ctx);
|
||||
}
|
||||
|
||||
// 生成32位密钥流
|
||||
static uint32_t generate_keystream_word(zuc256_context *ctx) {
|
||||
uint32_t X0, X1, X2, f_result;
|
||||
uint32_t z;
|
||||
uint32_t X0, X1, X2, F_res;
|
||||
|
||||
// 计算F函数输入
|
||||
X0 = ctx->NFSR[15];
|
||||
X1 = ctx->LFSR[15] ^ ctx->NFSR[11];
|
||||
X2 = ctx->LFSR[14];
|
||||
F_res = zuc256_F(X0, X1, X2);
|
||||
|
||||
// 计算F函数结果
|
||||
f_result = F(X0, X1, X2);
|
||||
// 更新辅助寄存器R1/R2
|
||||
ctx->R1 = zuc256_rotl(ctx->R1, 1) ^ (F_res >> 1);
|
||||
ctx->R2 = zuc256_rotl(ctx->R2, 1) ^ (F_res & 0x1);
|
||||
|
||||
// 更新R1和R2
|
||||
ctx->R1 = rotate_left(ctx->R1, 1) ^ (f_result >> 1);
|
||||
ctx->R2 = rotate_left(ctx->R2, 1) ^ (f_result & 0x1);
|
||||
// 生成32位密钥流
|
||||
*ks_word = ctx->LFSR[15] ^ ctx->R1 ^ ctx->R2;
|
||||
|
||||
// 生成密钥流字
|
||||
z = ctx->LFSR[15] ^ ctx->R1 ^ ctx->R2;
|
||||
|
||||
// LFSR移位
|
||||
uint32_t lfsr_feedback = ctx->LFSR[15] ^
|
||||
(ctx->LFSR[11] & ctx->LFSR[12]) ^
|
||||
ctx->LFSR[9];
|
||||
|
||||
for (int j = 15; j > 0; j--) {
|
||||
ctx->LFSR[j] = ctx->LFSR[j - 1];
|
||||
}
|
||||
// LFSR移位更新(ZUC标准反馈多项式)
|
||||
uint32_t lfsr_feedback = ctx->LFSR[15] ^ (ctx->LFSR[11] & ctx->LFSR[12]) ^ ctx->LFSR[9];
|
||||
memmove(&ctx->LFSR[1], &ctx->LFSR[0], sizeof(ctx->LFSR) - sizeof(uint32_t));
|
||||
ctx->LFSR[0] = lfsr_feedback;
|
||||
|
||||
// NFSR移位
|
||||
uint32_t nfsr_feedback = ctx->NFSR[15] ^
|
||||
ctx->NFSR[13] ^
|
||||
ctx->NFSR[10] ^
|
||||
ctx->NFSR[0] ^
|
||||
(ctx->NFSR[0] & ctx->NFSR[2]) ^
|
||||
(ctx->NFSR[1] & ctx->NFSR[3]) ^
|
||||
(ctx->NFSR[0] & ctx->NFSR[1] & ctx->NFSR[4]);
|
||||
|
||||
for (int j = 15; j > 0; j--) {
|
||||
ctx->NFSR[j] = ctx->NFSR[j - 1];
|
||||
}
|
||||
// NFSR移位更新(ZUC标准反馈多项式)
|
||||
uint32_t nfsr_feedback = ctx->NFSR[15] ^ ctx->NFSR[13] ^ ctx->NFSR[10] ^ ctx->NFSR[0]
|
||||
^ (ctx->NFSR[0] & ctx->NFSR[2]) ^ (ctx->NFSR[1] & ctx->NFSR[3])
|
||||
^ (ctx->NFSR[0] & ctx->NFSR[1] & ctx->NFSR[4]);
|
||||
memmove(&ctx->NFSR[1], &ctx->NFSR[0], sizeof(ctx->NFSR) - sizeof(uint32_t));
|
||||
ctx->NFSR[0] = nfsr_feedback;
|
||||
|
||||
return z;
|
||||
return ZUC256_SUCCESS;
|
||||
}
|
||||
|
||||
// 生成密钥流并与数据异或(加密/解密)
|
||||
void zuc256_crypt(zuc256_context *ctx, const uint8_t *input, uint8_t *output, size_t length) {
|
||||
uint32_t keystream;
|
||||
uint8_t *ks_bytes = (uint8_t *)&keystream;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
// 每4字节生成一次密钥流
|
||||
if ((i % 4) == 0) {
|
||||
keystream = generate_keystream_word(ctx);
|
||||
// 初始化LFSR/NFSR寄存器
|
||||
static int zuc256_init_regs(ZUC256_CTX *ctx) {
|
||||
if (ctx == NULL) {
|
||||
return ZUC256_ERR_NULL;
|
||||
}
|
||||
|
||||
// 异或操作
|
||||
output[i] = input[i] ^ ks_bytes[i % 4];
|
||||
uint32_t F_res;
|
||||
int i;
|
||||
|
||||
// 加载密钥到寄存器前8位(0~7)
|
||||
for (i = 0; i < 8; i++) {
|
||||
ctx->LFSR[i] = ctx->key[i];
|
||||
ctx->NFSR[i] = ctx->key[i];
|
||||
}
|
||||
|
||||
// 修复:用IV循环填充寄存器后8位(8~15),避免越界
|
||||
// IV共4个元素(0~3),通过(i-8)%4循环访问
|
||||
for (i = 8; i < 16; i++) {
|
||||
ctx->LFSR[i] = ctx->iv[(i - 8) % 4]; // 索引范围:0~3,安全访问
|
||||
ctx->NFSR[i] = ctx->iv[(i - 8) % 4];
|
||||
}
|
||||
|
||||
// 32次迭代打乱初始状态(ZUC强制步骤)
|
||||
for (i = 0; i < 32; i++) {
|
||||
uint32_t X0 = ctx->NFSR[15];
|
||||
uint32_t X1 = ctx->LFSR[15] ^ ctx->NFSR[11];
|
||||
uint32_t X2 = ctx->LFSR[14];
|
||||
F_res = zuc256_F(X0, X1, X2);
|
||||
|
||||
// LFSR移位(带F函数反馈)
|
||||
uint32_t lfsr_feedback = ctx->LFSR[15] ^ (ctx->LFSR[11] & ctx->LFSR[12]) ^ ctx->LFSR[9] ^ F_res;
|
||||
memmove(&ctx->LFSR[1], &ctx->LFSR[0], sizeof(ctx->LFSR) - sizeof(uint32_t));
|
||||
ctx->LFSR[0] = lfsr_feedback;
|
||||
|
||||
// NFSR移位(带F函数反馈)
|
||||
uint32_t nfsr_feedback = ctx->NFSR[15] ^ ctx->NFSR[13] ^ ctx->NFSR[10] ^ ctx->NFSR[0]
|
||||
^ (ctx->NFSR[0] & ctx->NFSR[2]) ^ (ctx->NFSR[1] & ctx->NFSR[3])
|
||||
^ (ctx->NFSR[0] & ctx->NFSR[1] & ctx->NFSR[4]) ^ F_res;
|
||||
memmove(&ctx->NFSR[1], &ctx->NFSR[0], sizeof(ctx->NFSR) - sizeof(uint32_t));
|
||||
ctx->NFSR[0] = nfsr_feedback;
|
||||
}
|
||||
|
||||
// 初始化辅助寄存器和密钥流缓存
|
||||
ctx->R1 = 0;
|
||||
ctx->R2 = 0;
|
||||
ctx->ks_buf = 0;
|
||||
ctx->ks_buf_len = 0;
|
||||
|
||||
return ZUC256_SUCCESS;
|
||||
}
|
||||
|
||||
int ZUC256_Init(ZUC256_CTX *ctx, const uint8_t *key, const uint8_t *iv) {
|
||||
// 参数合法性检查
|
||||
if (ctx == NULL || key == NULL || iv == NULL) {
|
||||
return ZUC256_ERR_NULL;
|
||||
}
|
||||
|
||||
// 清空上下文(避免敏感数据残留)
|
||||
memset(ctx, 0, sizeof(ZUC256_CTX));
|
||||
|
||||
// 加载密钥(32字节→8个32位字,大端序)
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ctx->key[i] = ((uint32_t)key[4*i] << 24) | ((uint32_t)key[4*i + 1] << 16)
|
||||
| ((uint32_t)key[4*i + 2] << 8) | (uint32_t)key[4*i + 3];
|
||||
}
|
||||
|
||||
// 加载IV(16字节→4个32位字,大端序)
|
||||
for (int i = 0; i < 4; i++) {
|
||||
ctx->iv[i] = ((uint32_t)iv[4*i] << 24) | ((uint32_t)iv[4*i + 1] << 16)
|
||||
| ((uint32_t)iv[4*i + 2] << 8) | (uint32_t)iv[4*i + 3];
|
||||
}
|
||||
|
||||
// 初始化寄存器
|
||||
return zuc256_init_regs(ctx);
|
||||
}
|
||||
|
||||
|
||||
int ZUC256_Update(ZUC256_CTX *ctx, uint8_t *out, const uint8_t *in, size_t in_len) {
|
||||
if (ctx == NULL || out == NULL || in == NULL) {
|
||||
return ZUC256_ERR_NULL;
|
||||
}
|
||||
|
||||
uint8_t *ks_buf_ptr = (uint8_t *)&ctx->ks_buf;
|
||||
size_t i = 0;
|
||||
|
||||
// 处理缓存中残留的密钥流(解决非4字节对齐数据)
|
||||
if (ctx->ks_buf_len > 0 && in_len > 0) {
|
||||
for (; i < in_len && ctx->ks_buf_len < 4; i++, ctx->ks_buf_len++) {
|
||||
out[i] = in[i] ^ ks_buf_ptr[ctx->ks_buf_len];
|
||||
}
|
||||
// 缓存用尽,重置长度
|
||||
if (ctx->ks_buf_len == 4) {
|
||||
ctx->ks_buf_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理完整的4字节块
|
||||
if (i < in_len) {
|
||||
size_t remaining = in_len - i;
|
||||
size_t full_blocks = remaining / 4;
|
||||
size_t rem_bytes = remaining % 4;
|
||||
|
||||
// 完整块处理
|
||||
for (size_t b = 0; b < full_blocks; b++) {
|
||||
uint32_t ks_word;
|
||||
if (zuc256_gen_ks_word(ctx, &ks_word) != ZUC256_SUCCESS) {
|
||||
return ZUC256_ERR_NULL;
|
||||
}
|
||||
// 密钥流与数据异或(流密码加解密统一逻辑)
|
||||
out[i + 4*b + 0] = in[i + 4*b + 0] ^ ((ks_word >> 24) & 0xFF);
|
||||
out[i + 4*b + 1] = in[i + 4*b + 1] ^ ((ks_word >> 16) & 0xFF);
|
||||
out[i + 4*b + 2] = in[i + 4*b + 2] ^ ((ks_word >> 8) & 0xFF);
|
||||
out[i + 4*b + 3] = in[i + 4*b + 3] ^ (ks_word & 0xFF);
|
||||
}
|
||||
|
||||
// 处理剩余不足4字节的数据(存入缓存)
|
||||
if (rem_bytes > 0) {
|
||||
uint32_t ks_word;
|
||||
if (zuc256_gen_ks_word(ctx, &ks_word) != ZUC256_SUCCESS) {
|
||||
return ZUC256_ERR_NULL;
|
||||
}
|
||||
ctx->ks_buf = ks_word;
|
||||
for (size_t r = 0; r < rem_bytes; r++) {
|
||||
out[i + 4*full_blocks + r] = in[i + 4*full_blocks + r] ^ ks_buf_ptr[r];
|
||||
}
|
||||
ctx->ks_buf_len = rem_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
return ZUC256_SUCCESS;
|
||||
}
|
||||
|
||||
int ZUC256_Final(ZUC256_CTX *ctx, uint8_t *out) {
|
||||
// 流密码无收尾操作,仅检查上下文合法性
|
||||
(void)out; // 未使用参数,避免编译警告
|
||||
return (ctx == NULL) ? ZUC256_ERR_NULL : ZUC256_SUCCESS;
|
||||
}
|
||||
|
||||
void ZUC256_Cleanup(ZUC256_CTX *ctx) {
|
||||
if (ctx != NULL) {
|
||||
memset(ctx, 0, sizeof(ZUC256_CTX)); // 覆盖敏感数据,防止泄露
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user