使用数字电路方式安全实现轻量级算法Ascon,支持Ascon-128a、Ascon-HASH、Ascon-HASHa四种参数配置。代码
时间: 2023-06-10 18:06:39 浏览: 181
以下是使用数字电路方式实现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
阅读全文