/
ascon_pkg.vhdl
executable file
·184 lines (166 loc) · 5.97 KB
/
ascon_pkg.vhdl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
-------------------------------------------------------------------------------
-- Title : Types and functions
-- Project :
-------------------------------------------------------------------------------
-- File : ascon_pkg.vhdl
-- Author : Hannes Gross
-- Company : Graz University of Technology
-- Created : 2016-11-17
-- Last update: 2017-01-24
-- Platform :
-- Standard : VHDL'93/02
-------------------------------------------------------------------------------
-- Description: Types and functions required for implementation
-------------------------------------------------------------------------------
-- Copyright (c) 2016
-------------------------------------------------------------------------------
-- Revisions :
-- Date Version Author Description
-- 2016-11-17 1.0 hgross Created
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
package ascon_pkg is
-- Configuration
type ascon_t is (Ascon128, Ascon128a);
constant VARIANT : ascon_t := Ascon128; -- Ascon128 or Ascon128a
constant SBOX_VARIANT : string := "LOW_RADOMNESS"; -- "DOM" or "LOW_RADOMNESS"
constant D : natural := 1; -- protection order
constant PARALLEL_SBOXES : integer := 1; -- number of parallel
-- S/boxes (1-64)
constant IMPLICIT_AFFINE : string := "no"; -- Sbox affine transform. shall be
-- done during round const addition
-- and shifting? only makes sense when
-- 64 parallel sboxes are used!
-- Functions
function NUM_PIPELINE_REGS (USE_BELAID_OPTIMIZATION : integer) return natural; -- how deep is the main pipeline in the Sbox?
function NUM_MASKS return natural; -- return number of masks required
-- Calculate number of required masks for selected variant of AND with
-- reduced randomness cost
function NUM_MASKS_REDUCED (USE_BELAID_OPTIMIZATION : integer) return integer;
function DATA_SIZE return natural; -- return the size of one data block
function YESORNO_TO_0OR1 (PARAM : string) return integer; -- convert 0 or 1 to "yes"
-- or "no"
-- Rotate a 64-bit word
function ROTATE_STATE_WORD (
word : std_logic_vector(63 downto 0);
constant rotate : integer)
return std_logic_vector;
-- Create a 0 bit vector of arbitrary length
function ZEROS (
constant WIDTH : natural)
return std_logic_vector;
-- Types
type t_shared_bit is array (natural range <>) of std_logic;
type t_shared_data is array (natural range <>) of std_logic_vector(DATA_SIZE-1 downto 0);
type t_shared_state_var is array (natural range <>) of std_logic_vector(63 downto 0);
type t_shared_key_var is array (natural range <>) of std_logic_vector(127 downto 0);
type t_shared_tag is array (natural range <>) of std_logic_vector(127 downto 0);
type sbox_data_t is array (natural range <>) of t_shared_bit(D downto 0);
type random_t is array (natural range <>) of t_shared_bit(NUM_MASKS -1 downto 0);
-- Ascons FSM states
type ascon_fsm_t is (IDLE, INIT, ASSOCIATED_DATA, PTCT_DATA, FINALIZE, READ_TAG);
end ascon_pkg;
package body ascon_pkg is
-- Functions
function NUM_PIPELINE_REGS (USE_BELAID_OPTIMIZATION : integer) return natural is
begin
if D = 0 then
return 0;
end if;
if SBOX_VARIANT = "LOW_RADOMNESS" then
-- LR SBOX variant
if D = 1 then
return 1;
elsif D = 2 then
if USE_BELAID_OPTIMIZATION = 1 then
return 3;
else
return 2;
end if;
elsif D = 3 then
return 4;
else
return 5;
end if;
else
-- DOM
return 1;
end if;
end function;
function NUM_MASKS return natural is
begin
if SBOX_VARIANT = "LOW_RADOMNESS" then
return NUM_MASKS_REDUCED (1);
else
if D = 0 then
return 1;
else
return D*(D+1)/2;
end if;
end if;
end function;
function NUM_MASKS_REDUCED (USE_BELAID_OPTIMIZATION : integer) return integer is
variable num_masks : integer := 0;
begin
num_masks := (D+1) * (D/4); -- for complete blocks
-- pseudo complete?
if (D mod 4) = 3 then
num_masks := num_masks + (D+1);
-- half complete
elsif ((D mod 4) = 2) then
-- Optimization applicable?
if (USE_BELAID_OPTIMIZATION = 1) and (D = 2) then
num_masks := num_masks + (2*(D+1))/3;
else
num_masks := num_masks + (D+1);
end if;
-- incomplete
elsif ((D mod 4) = 1) then
-- Optimization applicable?
--if (USE_BELAID_OPTIMIZATION = 1) and (((D+1) mod 3) = 0) then
--num_masks := num_masks + (2*(D+1))/3;
--else
num_masks := num_masks + (D+1)/2;
--end if;
end if;
return num_masks;
end function;
function DATA_SIZE return natural is
begin
if VARIANT = Ascon128 then
return 64;
else
return 128;
end if;
end function;
function ROTATE_STATE_WORD (
word : std_logic_vector(63 downto 0);
constant rotate : integer)
return std_logic_vector is
variable x : std_logic_vector(63 downto 0);
begin -- ROTATE_STATE_WORD
x := word(ROTATE-1 downto 0) & word(63 downto ROTATE);
return x;
end ROTATE_STATE_WORD;
function ZEROS (
constant WIDTH : natural)
return std_logic_vector is
variable x : std_logic_vector(WIDTH-1 downto 0);
begin -- ZEROS
x := (others => '0');
return x;
end ZEROS;
function YESORNO_TO_0OR1 (
PARAM : string)
return integer is
begin
if PARAM = "no" then
return 0;
elsif PARAM = "yes" then
return 1;
else
return -1;
end if;
end YESORNO_TO_0OR1;
end ascon_pkg;