-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathascon.vhdl
executable file
·409 lines (369 loc) · 15 KB
/
ascon.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
-------------------------------------------------------------------------------
-- Title : Side-Channel protected Ascon variants
-- Project :
-------------------------------------------------------------------------------
-- File : ascon.vhdl
-- Author : Hannes Gross
-- Company : Graz University of Technology
-- Created : 2016-11-24
-- Last update: 2016-11-30
-- Platform :
-- Standard : VHDL'93/02
-------------------------------------------------------------------------------
-- Description:
-------------------------------------------------------------------------------
-- Copyright (c) 2016
-------------------------------------------------------------------------------
-- Revisions :
-- Date Version Author Description
-- 2016-11-24 1.0 hgross Created
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.ascon_pkg.all;
-------------------------------------------------------------------------------
entity ascon is
generic (
-- see Ascon package for configuration
ASCON : ascon_t := VARIANT;
SBOX_INSTANCES : integer := PARALLEL_SBOXES;
IMPLICIT_AFFINE : string := IMPLICIT_AFFINE
);
port (
ClkxCI : in std_logic;
RstxBI : in std_logic;
-- Data Inputs
KeyxDI : in t_shared_key_var(D downto 0);
DataxDI : in t_shared_data (D downto 0);
NoncexDI : in std_logic_vector(127 downto 0);
-- Command Inputs
StartEncryptionxSI : in std_logic;
StartDecryptionxSI : in std_logic;
AssociatedDataRDYxSI : in std_logic;
PTCTDataRDYxSI : in std_logic;
FinalizexSI : in std_logic;
TagReceivedxSI : in std_logic;
-- Fresh random Z masks
Z0xDI : in random_t(SBOX_INSTANCES-1 downto 0);
Z1xDI : in random_t(SBOX_INSTANCES-1 downto 0);
Z2xDI : in random_t(SBOX_INSTANCES-1 downto 0);
Z3xDI : in random_t(SBOX_INSTANCES-1 downto 0);
Z4xDI : in random_t(SBOX_INSTANCES-1 downto 0);
-- Outputs
BusyxSO : out std_logic;
DataxDO : out t_shared_data(D downto 0);
DataReadyxSO : out std_logic;
TagxDO : out t_shared_tag (D downto 0)
);
end entity ascon;
-------------------------------------------------------------------------------
architecture str of ascon is
-----------------------------------------------------------------------------
-- Internal signal declarations
-----------------------------------------------------------------------------
-- Ascon's FSM
signal FSMxDN, FSMxDP : ascon_fsm_t;
-- 4-bit Round counter
signal RoundCounter : integer range 0 to 15;
signal doCountxS : std_logic;
signal RoundConstxS : std_logic_vector(3 downto 0);
-- Status and control registers for correct handling of data
signal isEncryptionxSN : std_logic;
signal isEncryptionxSP : std_logic;
signal addedKeyAtInitxSN : std_logic;
signal addedKeyAtInitxSP : std_logic;
signal addedDomainSeparationxSN : std_logic;
signal addedDomainSeparationxSP : std_logic;
-- Control signals to state
signal doInitializeStatexS : std_logic;
signal doRoundTransfromationxS : std_logic;
signal isFirstRoundOfTransformxS : std_logic;
-- ... from state
signal doIncrementRoundCounterxS : std_logic;
signal StateBusyxS : std_logic;
begin -- architecture str
-----------------------------------------------------------------------------
-- Status and control registers
-----------------------------------------------------------------------------
-- register process
status_control_reg_p: process (ClkxCI, RstxBI) is
begin -- process status_control_reg-P
if RstxBI = '0' then -- asynchronous reset (active low)
isEncryptionxSP <= '0';
addedKeyAtInitxSP <= '0';
addedDomainSeparationxSP <= '0';
elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
isEncryptionxSP <= isEncryptionxSN;
addedKeyAtInitxSP <= addedKeyAtInitxSN;
addedDomainSeparationxSP <= addedDomainSeparationxSN;
end if;
end process status_control_reg_p;
-----------------------------------------------------------------------------
-- Round Counter and signaling of first round of transformation
-----------------------------------------------------------------------------
round_counter_p : process (ClkxCI, RoundCounter, RstxBI) is
begin -- process round_counter_p
if RstxBI = '0' then -- asynchronous reset (active low)
RoundCounter <= 0;
isFirstRoundOfTransformxS <= '0';
elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
-- Reset on IDLE
if(FSMxDP = IDLE) then
RoundCounter <= 0;
isFirstRoundOfTransformxS <= '0';
else
-- Count up
if (doCountxS = '1') then
-- Overflow
if (RoundCounter = 12) then
-- Finalize?
if FSMxDN = FINALIZE then
RoundCounter <= 0;
else
-- Ascon128a requires more rounds for transformation
if (ASCON = Ascon128a) then
RoundCounter <= 4;
else
RoundCounter <= 6;
end if; -- Ascon128a?
end if; -- Finalize?
-- Signalize that this is the first round!
isFirstRoundOfTransformxS <= '1';
else -- not an overflow --> increment
RoundCounter <= RoundCounter + 1;
isFirstRoundOfTransformxS <= '0';
end if; -- Overflow
end if; -- Count up
end if; -- Reset on IDLE
end if; -- Clock handling
-- Convert counter value to 4-bit bit vector
RoundConstxS <= std_logic_vector(to_unsigned(RoundCounter, 4));
end process round_counter_p;
-----------------------------------------------------------------------------
-- Ascon's FSM
-----------------------------------------------------------------------------
ascon_fsm_p : process (AssociatedDataRDYxSI, FSMxDP, FinalizexSI,
PTCTDataRDYxSI, RoundCounter, StartDecryptionxSI,
StartEncryptionxSI, StateBusyxS, TagReceivedxSI,
addedDomainSeparationxSP, addedKeyAtInitxSP,
doIncrementRoundCounterxS, isEncryptionxSP,
isFirstRoundOfTransformxS) is
begin -- process ascon_fsm_p
-- Register defaults --> previous state
FSMxDN <= FSMxDP;
isEncryptionxSN <= isEncryptionxSP;
addedKeyAtInitxSN <= addedKeyAtInitxSP;
addedDomainSeparationxSN <= addedDomainSeparationxSP;
-- Control signals defaults
doInitializeStatexS <= '0';
doRoundTransfromationxS <= '0';
BusyxSO <= '1'; -- busy per default
-- State controls counter per default
doCountxS <= doIncrementRoundCounterxS;
-----------------------
-- State transitions --
-----------------------
case FSMxDP is
-- *** IDLE ***
when IDLE =>
if (StartEncryptionxSI = '1') or (StartDecryptionxSI = '1') then
FSMxDN <= INIT;
end if;
-- *** INIT ***
when INIT =>
-- End of init?
if (RoundCounter = 12) then
if AssociatedDataRDYxSI = '1' then
FSMxDN <= ASSOCIATED_DATA;
elsif PTCTDataRDYxSI = '1' then
FSMxDN <= PTCT_DATA;
elsif (FinalizexSI = '1') then
FSMxDN <= FINALIZE;
end if;
end if;
-- *** ASSOCIATED_DATA ***
when ASSOCIATED_DATA =>
-- Repeat or proceed?
if (RoundCounter = 12) then
if PTCTDataRDYxSI = '1' then
FSMxDN <= PTCT_DATA;
elsif (FinalizexSI = '1') then
FSMxDN <= FINALIZE;
end if;
end if;
-- *** PTCT_DATA ***
when PTCT_DATA =>
-- Repeat or proceed?
if (RoundCounter = 12) and (FinalizexSI = '1') then
FSMxDN <= FINALIZE;
end if;
-- *** FINALIZE ***
when FINALIZE =>
if RoundCounter = 12 then
FSMxDN <= READ_TAG;
end if;
-- *** READ_TAG ***
when READ_TAG =>
if TagReceivedxSI = '1' then
FSMxDN <= IDLE;
end if;
-- default
when others => null;
end case; -- State transitions
------------------
-- Output logic --
------------------
case FSMxDP is
-- *** IDLE ***
when IDLE =>
-- Reset on IDLE
addedKeyAtInitxSN <= '0';
addedDomainSeparationxSN <= '0';
BusyxSO <= '0';
doInitializeStatexS <= '1';
-- Memorize if encryption or decryption was selected
isEncryptionxSN <= StartEncryptionxSI;
-- *** INIT ***
when INIT =>
-- End of transformation?
if (RoundCounter = 12) then
if AssociatedDataRDYxSI = '1' or PTCTDataRDYxSI = '1' or FinalizexSI = '1' then
doCountxS <= '1';
end if;
-- Busy only when State is busy
BusyxSO <= StateBusyxS;
else
doRoundTransfromationxS <= '1';
end if;
-- *** ASSOCIATED_DATA ***
when ASSOCIATED_DATA =>
-- set if key was already added to state after init
if not(isFirstRoundOfTransformxS = '1') then
addedKeyAtInitxSN <= '1';
end if;
-- Repeat or proceed?
if (RoundCounter = 12) then
if AssociatedDataRDYxSI = '1' or PTCTDataRDYxSI = '1' or FinalizexSI = '1' then
doCountxS <= '1';
end if;
-- Busy only when State is busy
BusyxSO <= StateBusyxS;
else
doRoundTransfromationxS <= '1';
end if;
-- *** PTCT_DATA ***
when PTCT_DATA =>
-- set if key was already added to state after init
if not(isFirstRoundOfTransformxS = '1') then
addedKeyAtInitxSN <= '1';
addedDomainSeparationxSN <= '1';
end if;
-- Repeat or proceed?
if (RoundCounter = 12) then
if PTCTDataRDYxSI = '1' or (FinalizexSI = '1') then
doCountxS <= '1';
end if;
-- Busy only when State is busy
BusyxSO <= StateBusyxS;
else
doRoundTransfromationxS <= '1';
end if;
-- *** FINALIZE ***
when FINALIZE =>
-- Finished?
if not(RoundCounter = 12) then
doRoundTransfromationxS <= '1';
end if;
-- *** READ_TAG ***
when READ_TAG =>
-- Busy only when State is busy
BusyxSO <= StateBusyxS;
-- default
when others => null;
end case; -- Output logic
end process ascon_fsm_p;
-- Register process for FSM
fsm_reg_p: process (ClkxCI, RstxBI) is
begin -- process fsm_reg_p
if RstxBI = '0' then -- asynchronous reset (active low)
FSMxDP <= IDLE;
elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
FSMxDP <= FSMxDN;
end if;
end process fsm_reg_p;
-----------------------------------------------------------------------------
-- State module for Ascon128 or Ascon128a
-----------------------------------------------------------------------------
ascon128_g: if ASCON = Ascon128 generate
ascons_state: entity work.state
generic map (
DATA_BLOCK_SIZE => 64,
KEY_SIZE => 128,
ROUNDS_A => 12,
ROUNDS_B => 6,
SBOX_INSTANCES => SBOX_INSTANCES,
IMPLICIT_AFFINE => IMPLICIT_AFFINE,
PIPELINED => "yes")
port map (
ClkxCI => ClkxCI,
RstxBI => RstxBI,
KeyxDI => KeyxDI,
NoncexDI => NoncexDI,
DataxDI => DataxDI,
RoundConstxDI => RoundConstxS,
Z0xDI => Z0xDI,
Z1xDI => Z1xDI,
Z2xDI => Z2xDI,
Z3xDI => Z3xDI,
Z4xDI => Z4xDI,
DoInitializeStatexSI => doInitializeStatexS,
DoRoundTransfromationxSI => doRoundTransfromationxS,
isEncryptionxSI => isEncryptionxSP,
addedKeyAtInitxSI => addedKeyAtInitxSP,
addedDomainSeparationxSI => addedDomainSeparationxSP,
doIncrementRoundCounterxSO => doIncrementRoundCounterxS,
isFirstRoundOfTransformxSI => isFirstRoundOfTransformxS,
FSMxDI => FSMxDP,
DataxDO => DataxDO,
DataReadyxSO => DataReadyxSO,
TagxDO => TagxDO,
StateBusyxSO => StateBusyxS);
end generate ascon128_g;
ascon128a_g: if ASCON = Ascon128a generate
ascons_state: entity work.state
generic map (
DATA_BLOCK_SIZE => 128,
KEY_SIZE => 128,
ROUNDS_A => 12,
ROUNDS_B => 8,
SBOX_INSTANCES => SBOX_INSTANCES,
IMPLICIT_AFFINE => IMPLICIT_AFFINE,
PIPELINED => "yes")
port map (
ClkxCI => ClkxCI,
RstxBI => RstxBI,
KeyxDI => KeyxDI,
NoncexDI => NoncexDI,
DataxDI => DataxDI,
RoundConstxDI => RoundConstxS,
Z0xDI => Z0xDI,
Z1xDI => Z1xDI,
Z2xDI => Z2xDI,
Z3xDI => Z3xDI,
Z4xDI => Z4xDI,
DoInitializeStatexSI => doInitializeStatexS,
DoRoundTransfromationxSI => doRoundTransfromationxS,
isEncryptionxSI => isEncryptionxSP,
addedKeyAtInitxSI => addedKeyAtInitxSP,
addedDomainSeparationxSI => addedDomainSeparationxSP,
doIncrementRoundCounterxSO => doIncrementRoundCounterxS,
isFirstRoundOfTransformxSI => isFirstRoundOfTransformxS,
FSMxDI => FSMxDP,
DataxDO => DataxDO,
DataReadyxSO => DataReadyxSO,
TagxDO => TagxDO,
StateBusyxSO => StateBusyxS);
end generate ascon128a_g;
end architecture str;
-------------------------------------------------------------------------------