初始化:添加本地配置文件
new file: .gitignore new file: .vscode/settings.json new file: LICENSE new file: README.md new file: SConscript new file: SConstruct new file: algo.py new file: inc/type.h new file: inc/zuc256.h new file: run.sh new file: src/main.c new file: src/zuc256.c
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
build
|
||||
__pycache__
|
||||
.sconsign.dblite
|
||||
.vscode
|
||||
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"uart.h": "c",
|
||||
"ram_test.h": "c",
|
||||
"stdio.h": "c"
|
||||
}
|
||||
}
|
||||
18
LICENSE
Normal file
18
LICENSE
Normal file
@@ -0,0 +1,18 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 gj
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||
portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
12
README.md
Normal file
12
README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# 算法c语言代码
|
||||
|
||||
## 📦 环境准备
|
||||
|
||||
确保本地已安装:
|
||||
- scons 构建工具,用于自动化编译流程。
|
||||
- build-essential 包含 gcc、make 等基础编译工具的依赖包
|
||||
```bash
|
||||
# Ubuntu/Debian
|
||||
sudo apt-get install scons
|
||||
sudo apt-get install build-essential
|
||||
```
|
||||
40
SConscript
Normal file
40
SConscript
Normal file
@@ -0,0 +1,40 @@
|
||||
import os
|
||||
from SCons.Script import *
|
||||
|
||||
# 导入环境
|
||||
Import('env')
|
||||
|
||||
# 项目根目录
|
||||
CWD = os.getcwd()
|
||||
|
||||
# 收集源码(.c 和 .S 文件)
|
||||
sources = Glob(os.path.join(CWD, 'src/*.c')) + Glob(os.path.join(CWD, 'src/*.S'))
|
||||
|
||||
# 编译选项
|
||||
cpppath = [
|
||||
CWD,
|
||||
os.path.join(CWD, 'inc'), # 仅使用项目内的头文件目录
|
||||
]
|
||||
|
||||
# 确保 build 目录存在
|
||||
BUILD_DIR = 'build'
|
||||
if not os.path.exists(BUILD_DIR):
|
||||
os.makedirs(BUILD_DIR)
|
||||
|
||||
# 逐个编译源码生成目标文件(输出到 build 目录)
|
||||
objs = []
|
||||
for src in sources:
|
||||
src_path = str(src) # 转换为字符串路径
|
||||
obj_name = os.path.basename(src_path).replace('.c', '.o').replace('.S', '.o')
|
||||
obj_path = os.path.join(CWD, BUILD_DIR, obj_name)
|
||||
|
||||
obj = env.Object(
|
||||
target=obj_path,
|
||||
source=src_path,
|
||||
CPPPATH=cpppath,
|
||||
)
|
||||
objs.append(obj)
|
||||
|
||||
# 返回目标文件列表,给 SConstruct 链接用
|
||||
Return('objs')
|
||||
|
||||
47
SConstruct
Normal file
47
SConstruct
Normal file
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
from SCons.Script import *
|
||||
|
||||
# 确保 build 目录存在
|
||||
BUILD_DIR = 'build'
|
||||
if not os.path.exists(BUILD_DIR):
|
||||
os.makedirs(BUILD_DIR)
|
||||
|
||||
# 导入配置(修改为导入algo.py)
|
||||
from algo import *
|
||||
|
||||
# 初始化构建环境
|
||||
env = Environment(
|
||||
tools=['default'],
|
||||
CC=CC,
|
||||
CXX=CXX,
|
||||
AS=AS,
|
||||
AR=AR,
|
||||
LINK=LINK,
|
||||
CCFLAGS=CFLAGS,
|
||||
CXXFLAGS=CFLAGS,
|
||||
ASFLAGS=AFLAGS,
|
||||
LINKFLAGS=LFLAGS,
|
||||
CPPPATH=['inc'], # 仅使用项目内的头文件目录
|
||||
)
|
||||
|
||||
# 递归构建源码(SConscript 负责收集编译文件)
|
||||
src_objs = env.SConscript('SConscript', exports='env')
|
||||
|
||||
# 链接生成 elf 文件(输出到 build 目录)
|
||||
elf_target = os.path.join(BUILD_DIR, TARGET_NAME)
|
||||
elf_file = env.Program(elf_target, src_objs)
|
||||
|
||||
# 后处理:生成 bin、asm 等文件
|
||||
post_commands = env.Command(
|
||||
os.path.join(BUILD_DIR, 'post_actions'), # 虚拟目标,确保命令执行
|
||||
elf_file,
|
||||
POST_ACTION
|
||||
)
|
||||
|
||||
# 关联默认构建目标(执行 scons 时默认编译+后处理)
|
||||
Default(elf_file)
|
||||
Default(post_commands)
|
||||
|
||||
# 清理规则(删除 build 目录及内容)
|
||||
Clean(elf_file, BUILD_DIR)
|
||||
|
||||
37
algo.py
Executable file
37
algo.py
Executable file
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
from SCons.Script import *
|
||||
|
||||
# 工具链配置(Ubuntu gcc环境)
|
||||
CROSS_TOOL = 'gcc'
|
||||
PREFIX = '' # 本地编译不需要交叉编译前缀
|
||||
|
||||
# 目标配置
|
||||
TARGET_NAME = 'app' # 更改为通用应用名称
|
||||
BUILD_DIR = 'build' # 统一构建输出目录
|
||||
|
||||
# 工具链路径(使用系统默认的gcc工具链)
|
||||
CC = PREFIX + 'gcc'
|
||||
CXX = PREFIX + 'g++'
|
||||
AS = PREFIX + 'gcc'
|
||||
AR = PREFIX + 'ar'
|
||||
LINK = PREFIX + 'gcc'
|
||||
SIZE = PREFIX + 'size'
|
||||
OBJDUMP = PREFIX + 'objdump'
|
||||
OBJCPY = PREFIX + 'objcopy'
|
||||
|
||||
# 编译、链接选项(适用于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 目录)
|
||||
ASM_FILE = os.path.join(BUILD_DIR, TARGET_NAME + '.asm')
|
||||
BIN_FILE = os.path.join(BUILD_DIR, TARGET_NAME + '.bin')
|
||||
|
||||
DUMP_ACTION = f'{OBJDUMP} -D -S $SOURCE > {ASM_FILE}'
|
||||
POST_ACTION = f'{OBJCPY} -O binary $SOURCE {BIN_FILE}\n'
|
||||
POST_ACTION += f'{SIZE} $SOURCE\n'
|
||||
POST_ACTION += DUMP_ACTION
|
||||
|
||||
28
inc/type.h
Normal file
28
inc/type.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#ifndef __TYPE_H
|
||||
#define __TYPE_H
|
||||
|
||||
#include <stdint.h>
|
||||
/* IO definitions */
|
||||
#define __I volatile const /* defines 'read only' permissions */
|
||||
|
||||
#define __O volatile /* defines 'write only' permissions */
|
||||
#define __IO volatile /* defines 'read / write' permissions */
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef __IO uint64_t vu64;
|
||||
typedef __IO uint32_t vu32;
|
||||
typedef __IO uint16_t vu16;
|
||||
typedef __IO uint8_t vu8;
|
||||
|
||||
typedef __I uint64_t vuc64; /* Read Only */
|
||||
typedef __I uint32_t vuc32; /* Read Only */
|
||||
typedef __I uint16_t vuc16; /* Read Only */
|
||||
typedef __I uint8_t vuc8; /* Read Only */
|
||||
|
||||
|
||||
#endif /*__TYPE_H*/
|
||||
36
inc/zuc256.h
Normal file
36
inc/zuc256.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef ZUC256_H
|
||||
#define ZUC256_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// ZUC256算法上下文结构
|
||||
typedef struct {
|
||||
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;
|
||||
|
||||
/**
|
||||
* @brief 初始化ZUC256算法上下文
|
||||
*
|
||||
* @param ctx 算法上下文指针
|
||||
* @param key 256位密钥(32字节)
|
||||
* @param iv 128位初始向量(16字节)
|
||||
*/
|
||||
void zuc256_init(zuc256_context *ctx, const uint8_t *key, const uint8_t *iv);
|
||||
|
||||
/**
|
||||
* @brief 使用ZUC256算法进行加密或解密
|
||||
*
|
||||
* @param ctx 算法上下文指针
|
||||
* @param input 输入数据
|
||||
* @param output 输出数据(与输入数据长度相同)
|
||||
* @param length 数据长度(字节数)
|
||||
*/
|
||||
void zuc256_crypt(zuc256_context *ctx, const uint8_t *input, uint8_t *output, size_t length);
|
||||
|
||||
#endif // ZUC256_H
|
||||
|
||||
42
run.sh
Executable file
42
run.sh
Executable file
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
# Author: qinzhenghui
|
||||
# Description: 编译项目脚本
|
||||
# Update Date: 2025-06-18
|
||||
|
||||
# 定义函数用于处理错误并退出
|
||||
handle_error() {
|
||||
local error_message=$1
|
||||
echo "错误: $error_message"
|
||||
return 1
|
||||
}
|
||||
|
||||
scons --clean
|
||||
|
||||
|
||||
scons -j3
|
||||
if [ $? -ne 0 ]; then
|
||||
handle_error "scons -j3 编译失败"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
files=("build/app.map" "build/app.asm" "build/app.elf" "build/app.bin")
|
||||
declare -a file_infos
|
||||
|
||||
for file in "${files[@]}"; do
|
||||
if [ -f "$file" ]; then
|
||||
file_name=$(basename "$file")
|
||||
# 调整此处提取时间,只保留到秒
|
||||
modification_time=$(stat -c %y "$file" | awk '{print $1 " " substr($2, 1, 8)}')
|
||||
file_size=$(stat -c %s "$file")
|
||||
file_size=$((file_size))
|
||||
file_infos+=("$file_name $modification_time $file_size")
|
||||
fi
|
||||
done
|
||||
echo -e "文件名称\t修改时间\t文件大小(B)"
|
||||
for info in "${file_infos[@]}"; do
|
||||
IFS=' ' read -ra parts <<< "$info"
|
||||
echo -e "${parts[0]}\t\t${parts[1]}/${parts[2]}\t${parts[3]}"
|
||||
done | column -t
|
||||
|
||||
|
||||
exit 0
|
||||
39
src/main.c
Normal file
39
src/main.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "zuc256.h"
|
||||
#include <stdio.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
|
||||
};
|
||||
|
||||
// 128位初始向量(16字节)
|
||||
uint8_t iv[16] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
|
||||
// 待加密数据
|
||||
uint8_t plaintext[] = "Hello, ZUC256 algorithm!";
|
||||
uint8_t ciphertext[sizeof(plaintext)];
|
||||
uint8_t decrypted[sizeof(plaintext)];
|
||||
|
||||
zuc256_context ctx;
|
||||
|
||||
// 加密过程
|
||||
zuc256_init(&ctx, key, iv);
|
||||
zuc256_crypt(&ctx, plaintext, ciphertext, sizeof(plaintext));
|
||||
|
||||
// 解密过程(使用相同的密钥和IV重新初始化)
|
||||
zuc256_init(&ctx, key, iv);
|
||||
zuc256_crypt(&ctx, ciphertext, decrypted, sizeof(plaintext));
|
||||
|
||||
// 输出结果
|
||||
printf("Original: %s\n", plaintext);
|
||||
printf("Decrypted: %s\n", decrypted);
|
||||
|
||||
return 0;
|
||||
}
|
||||
244
src/zuc256.c
Normal file
244
src/zuc256.c
Normal file
@@ -0,0 +1,244 @@
|
||||
#include "zuc256.h"
|
||||
#include <string.h>
|
||||
|
||||
// S盒置换表
|
||||
static const uint8_t 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,
|
||||
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
||||
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
||||
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
||||
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
|
||||
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
|
||||
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
||||
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
|
||||
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
|
||||
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
||||
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
||||
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
||||
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
||||
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
|
||||
};
|
||||
|
||||
static const uint8_t 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,
|
||||
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
|
||||
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
|
||||
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
||||
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
|
||||
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
|
||||
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
||||
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
|
||||
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
|
||||
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
||||
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
|
||||
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
|
||||
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
||||
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) {
|
||||
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;
|
||||
uint8_t b[4], c[4];
|
||||
|
||||
// 分解X0为字节
|
||||
b[0] = (X0 >> 24) & 0xFF;
|
||||
b[1] = (X0 >> 16) & 0xFF;
|
||||
b[2] = (X0 >> 8) & 0xFF;
|
||||
b[3] = X0 & 0xFF;
|
||||
|
||||
// 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为字节
|
||||
c[0] = (X1 >> 24) & 0xFF;
|
||||
c[1] = (X1 >> 16) & 0xFF;
|
||||
c[2] = (X1 >> 8) & 0xFF;
|
||||
c[3] = X1 & 0xFF;
|
||||
|
||||
// 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);
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 计算F函数输入
|
||||
X0 = ctx->NFSR[15];
|
||||
X1 = ctx->LFSR[15] ^ ctx->NFSR[11];
|
||||
X2 = ctx->LFSR[14];
|
||||
|
||||
// 计算F函数结果
|
||||
f_result = F(X0, X1, X2);
|
||||
|
||||
// 更新R1和R2
|
||||
ctx->R1 = rotate_left(ctx->R1, 1) ^ (f_result >> 1);
|
||||
ctx->R2 = rotate_left(ctx->R2, 1) ^ (f_result & 0x1);
|
||||
|
||||
// 生成密钥流字
|
||||
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];
|
||||
}
|
||||
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];
|
||||
}
|
||||
ctx->NFSR[0] = nfsr_feedback;
|
||||
|
||||
return z;
|
||||
}
|
||||
|
||||
// 生成密钥流并与数据异或(加密/解密)
|
||||
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);
|
||||
}
|
||||
|
||||
// 异或操作
|
||||
output[i] = input[i] ^ ks_bytes[i % 4];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user