使用数字电路方式安全实现轻量级算法Ascon,支持Ascon-128a、Ascon-HASH、Ascon-HASHa四种参数配置。代码

时间: 2023-06-10 17:06:39 浏览: 88
以下是使用数字电路方式实现Ascon-128a、Ascon-HASH、Ascon-HASHa的代码: ```verilog module ascon_128a(input clk, input reset, input [127:0] plaintext, input [127:0] key, output reg [127:0] ciphertext); parameter rounds = 12; reg [127:0] state [0:5]; reg [127:0] roundkey [0:rounds+1]; always @(posedge clk) begin if (reset) begin state <= '{128'h00}; roundkey <= '{128'h00}; end else begin state[0] <= plaintext ^ roundkey[0]; for (int i = 0; i < rounds; i = i + 1) begin state <= ascon_permutation(state, roundkey[i+1]); end ciphertext <= state[0] ^ roundkey[rounds+1]; end end function [127:0] ascon_permutation(input [127:0] state, input [127:0] roundkey); input [127:0] t; t = state[0] ^ roundkey; t = ascon_substitution(t); t = ascon_shift_rows(t); t = ascon_mix_columns(t); t[0:127] = t[0:127] ^ roundkey; return t; endfunction function [127:0] ascon_substitution(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+7:i*32] = ascon_sbox(t[i*32+7:i*32]); end return t; endfunction function [127:0] ascon_shift_rows(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+0:i*32+31] = ascon_rotate_left(t[i*32+0:i*32+31], i); end return t; endfunction function [127:0] ascon_mix_columns(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+0:i*32+31] = ascon_mix_column(t[i*32+0:i*32+31], t[((i+1)*32)%160+0:((i+1)*32)%160+31], t[((i+2)*32)%160+0:((i+2)*32)%160+31], t[((i+3)*32)%160+0:((i+3)*32)%160+31]); end return t; endfunction function [127:0] ascon_sbox(input [7:0] x); input [7:0] t; t[0] = x[0] ^ x[4] ^ x[5] ^ x[6] ^ x[7] ^ 0x9e; t[1] = x[1] ^ x[5] ^ x[6] ^ x[7] ^ x[0] ^ 0x5b; t[2] = x[2] ^ x[6] ^ x[7] ^ x[0] ^ x[1] ^ 0x5d; t[3] = x[3] ^ x[7] ^ x[0] ^ x[1] ^ x[2] ^ 0x3e; t[4] = x[4] ^ x[0] ^ x[1] ^ x[2] ^ x[3] ^ 0x76; t[5] = x[5] ^ x[1] ^ x[2] ^ x[3] ^ x[4] ^ 0x1f; t[6] = x[6] ^ x[2] ^ x[3] ^ x[4] ^ x[5] ^ 0x3b; t[7] = x[7] ^ x[3] ^ x[4] ^ x[5] ^ x[6] ^ 0x4f; return t; endfunction function [127:0] ascon_rotate_left(input [127:0] x, input [3:0] n); input [127:0] t; t[0:127] = {x[31:0], x[63:32], x[95:64], x[127:96]}; t[0:127] = {t[31-n:0], t[127-n:32]}; return t; endfunction function [127:0] ascon_mix_column(input [31:0] a, input [31:0] b, input [31:0] c, input [31:0] d); input [31:0] t; t[0:31] = a[0:7] ^ b[8:15] ^ c[16:23] ^ d[24:31]; t[32:63] = a[8:15] ^ b[16:23] ^ c[24:31] ^ d[0:7]; t[64:95] = a[16:23] ^ b[24:31] ^ c[0:7] ^ d[8:15]; t[96:127] = a[24:31] ^ b[0:7] ^ c[8:15] ^ d[16:23]; return t; endfunction always @(*) begin roundkey[0] = key; for (int i = 0; i < rounds; i = i + 1) begin roundkey[i+1] = ascon_round_constant(i) ^ roundkey[i]; roundkey[i+1][0:7] = ascon_sbox(roundkey[i+1][0:7]); end roundkey[rounds+1] = ascon_round_constant(rounds) ^ roundkey[rounds]; end function [127:0] ascon_round_constant(input [3:0] r); input [127:0] t; t[0:7] = 0x01; t[8:15] = 0x82; t[16:23] = r; t[24:31] = 0x00; t[32:39] = 0x40; t[40:47] = 0x00; t[48:55] = 0x00; t[56:63] = 0x00; t[64:71] = 0x00; t[72:79] = 0x00; t[80:87] = 0x00; t[88:95] = 0x00; t[96:103] = 0x00; t[104:111] = 0x00; t[112:119] = 0x00; t[120:127] = 0x00; return t; endfunction endmodule module ascon_hash(input clk, input reset, input [127:0] message, output reg [127:0] hash); parameter rounds = 12; reg [127:0] state [0:5]; reg [127:0] roundconstant [0:rounds+1]; reg [127:0] block; always @(posedge clk) begin if (reset) begin state <= '{128'h00}; roundconstant <= '{128'h00}; end else begin state[0] <= state[0] ^ message; for (int i = 0; i < rounds; i = i + 1) begin state <= ascon_permutation(state, roundconstant[i+1]); end hash <= state[0]; end end always @(*) begin roundconstant[0] = '{128'h00}; for (int i = 0; i < rounds; i = i + 1) begin roundconstant[i+1] = ascon_round_constant(i) ^ roundconstant[i]; roundconstant[i+1][0:7] = ascon_sbox(roundconstant[i+1][0:7]); end roundconstant[rounds+1] = ascon_round_constant(rounds) ^ roundconstant[rounds]; end function [127:0] ascon_permutation(input [127:0] state, input [127:0] roundconstant); input [127:0] t; t = state[0] ^ roundconstant; t = ascon_substitution(t); t = ascon_shift_rows(t); t = ascon_mix_columns(t); t[0:127] = t[0:127] ^ roundconstant; return t; endfunction function [127:0] ascon_substitution(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+7:i*32] = ascon_sbox(t[i*32+7:i*32]); end return t; endfunction function [127:0] ascon_shift_rows(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+0:i*32+31] = ascon_rotate_left(t[i*32+0:i*32+31], i); end return t; endfunction function [127:0] ascon_mix_columns(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+0:i*32+31] = ascon_mix_column(t[i*32+0:i*32+31], t[((i+1)*32)%160+0:((i+1)*32)%160+31], t[((i+2)*32)%160+0:((i+2)*32)%160+31], t[((i+3)*32)%160+0:((i+3)*32)%160+31]); end return t; endfunction function [127:0] ascon_sbox(input [7:0] x); input [7:0] t; t[0] = x[0] ^ x[4] ^ x[5] ^ x[6] ^ x[7] ^ 0x9e; t[1] = x[1] ^ x[5] ^ x[6] ^ x[7] ^ x[0] ^ 0x5b; t[2] = x[2] ^ x[6] ^ x[7] ^ x[0] ^ x[1] ^ 0x5d; t[3] = x[3] ^ x[7] ^ x[0] ^ x[1] ^ x[2] ^ 0x3e; t[4] = x[4] ^ x[0] ^ x[1] ^ x[2] ^ x[3] ^ 0x76; t[5] = x[5] ^ x[1] ^ x[2] ^ x[3] ^ x[4] ^ 0x1f; t[6] = x[6] ^ x[2] ^ x[3] ^ x[4] ^ x[5] ^ 0x3b; t[7] = x[7] ^ x[3] ^ x[4] ^ x[5] ^ x[6] ^ 0x4f; return t; endfunction function [127:0] ascon_rotate_left(input [127:0] x, input [3:0] n); input [127:0] t; t[0:127] = {x[31:0], x[63:32], x[95:64], x[127:96]}; t[0:127] = {t[31-n:0], t[127-n:32]}; return t; endfunction function [127:0] ascon_mix_column(input [31:0] a, input [31:0] b, input [31:0] c, input [31:0] d); input [31:0] t; t[0:31] = a[0:7] ^ b[8:15] ^ c[16:23] ^ d[24:31]; t[32:63] = a[8:15] ^ b[16:23] ^ c[24:31] ^ d[0:7]; t[64:95] = a[16:23] ^ b[24:31] ^ c[0:7] ^ d[8:15]; t[96:127] = a[24:31] ^ b[0:7] ^ c[8:15] ^ d[16:23]; return t; endfunction function [127:0] ascon_round_constant(input [3:0] r); input [127:0] t; t[0:7] = 0x01; t[8:15] = 0x82; t[16:23] = r; t[24:31] = 0x00; t[32:39] = 0x40; t[40:47] = 0x00; t[48:55] = 0x00; t[56:63] = 0x00; t[64:71] = 0x00; t[72:79] = 0x00; t[80:87] = 0x00; t[88:95] = 0x00; t[96:103] = 0x00; t[104:111] = 0x00; t[112:119] = 0x00; t[120:127] = 0x00; return t; endfunction always @(posedge clk) begin if (reset) begin block <= '{128'h00}; end else begin block <= block ^ message; end end endmodule module ascon_hasha(input clk, input reset, input [127:0] message, output reg [127:0] hash); parameter rounds = 12; reg [127:0] state [0:5]; reg [127:0] roundconstant [0:rounds+1]; reg [127:0] block; reg [127:0] count; always @(posedge clk) begin if (reset) begin state <= '{128'h00}; roundconstant <= '{128'h00}; end else begin if (count == 0) begin state[0] <= state[0] ^ block[0:127]; for (int i = 0; i < rounds; i = i + 1) begin state <= ascon_permutation(state, roundconstant[i+1]); end end count <= count + 1; if (count == 4) begin count <= 0; block <= '{128'h00}; end hash <= state[0]; end end always @(*) begin roundconstant[0] = '{128'h00}; for (int i = 0; i < rounds; i = i + 1) begin roundconstant[i+1] = ascon_round_constant(i) ^ roundconstant[i]; roundconstant[i+1][0:7] = ascon_sbox(roundconstant[i+1][0:7]); end roundconstant[rounds+1] = ascon_round_constant(rounds) ^ roundconstant[rounds]; end function [127:0] ascon_permutation(input [127:0] state, input [127:0] roundconstant); input [127:0] t; t = state[0] ^ roundconstant; t = ascon_substitution(t); t = ascon_shift_rows(t); t = ascon_mix_columns(t); t[0:127] = t[0:127] ^ roundconstant; return t; endfunction function [127:0] ascon_substitution(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+7:i*32] = ascon_sbox(t[i*32+7:i*32]); end return t; endfunction function [127:0] ascon_shift_rows(input [127:0] state); input [127:0] t; t = state; for (int i = 0; i < 5; i = i + 1) begin t[i*32+0:i*32+31] = ascon_rotate_left(t[i*32+0:i*32+31], i); end return t; endfunction function [127:0] ascon_mix_columns(input

相关推荐

最新推荐

recommend-type

华中科技大学电信专业 课程资料 作业 代码 实验报告-数据结构-内含源码和说明书.zip

华中科技大学电信专业 课程资料 作业 代码 实验报告-数据结构-内含源码和说明书.zip
recommend-type

java 游戏飞翔的小鸟

java 制作游戏 飞翔的小鸟
recommend-type

setuptools-25.3.0.zip

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

学生课设,C++数据结构实验,图的实现,vs2022完整项目,包含完整代码,开箱即用

适用数据结构课程,大学生必备资源。 ALGraphAlgo.h 定义了图数据结构相关的函数,包括无向图的创建、获取顶点数据、邻接边及遍历操作。 ALGraphDef.h 定义了图的邻接列表数据结构,包括顶点、边的结构体和图的数据结构,以及队列结构。 pubuse.h 包含基本的标准库导入,定义了布尔常量、枚举和类型别名,用于项目中的通用操作。 ALGraphUse.cpp 实现了一个交互式应用,允许用户创建、探索并遍历图,使用了上述头文件中定义的数据结构和函数。 整个程序的功能是:提供一个基于命令行的图形数据结构交互式工具,用户可以创建无向图,查询顶点信息和邻接边,并进行深度优先和广度优先遍历。 这些文件整体上构成了一个C++实现的图数据结构库,包含图的定义、算法实现以及一个示例应用,让用户能够创建、操作和遍历无向图。
recommend-type

JAVA+SQL离散数学题库管理系统(源代码+论文+外文翻译).zip

JAVA+SQL离散数学题库管理系统是一个用Java编程语言和SQL数据库管理系统构建的应用程序,旨在帮助用户管理离散数学题库。该系统主要包括以下功能: 题库管理:允许用户添加、编辑、删除离散数学题目,包括题目内容、选项、答案等信息。用户可以根据需要对题目进行分类、标记或搜索。 用户管理:支持用户注册、登录、注销等功能,保障系统安全性和个性化服务。管理员可以管理用户权限,如分配不同的角色和权限。 练习和测试:用户可以通过系统进行练习和测试,选择特定题目或随机生成题目进行答题。系统会自动批改答案并记录用户的答题历史和成绩。 数据统计和分析:系统可以对用户的答题情况进行统计和分析,包括答题时间、正确率、题目难度等,帮助用户了解自己的学习情况并进行有效的学习计划。 系统设置:提供系统参数设置、题目难度调整、数据备份等功能,保障系统稳定运行和数据安全。 通过以上功能,JAVA+SQL离散数学题库管理系统能够有效地帮助用户管理离散数学学习过程中的题目资源,提高学习效率和成绩。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。