summaryrefslogtreecommitdiff
path: root/src/add_vhdltbc/i/chiffrement.vhd
blob: 1d5eb495ccd7aa8e73c39b1637a483ac00800376 (plain)
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
-- Implementation of the Lilliput-TBC tweakable block cipher by the
-- Lilliput-AE team, hereby denoted as "the implementer".
--
-- For more information, feedback or questions, refer to our website:
-- https://paclido.fr/lilliput-ae
--
-- To the extent possible under law, the implementer has waived all copyright
-- and related or neighboring rights to the source code in this file.
-- http://creativecommons.org/publicdomain/zero/1.0/

library IEEE;
library work;
use IEEE.numeric_std.ALL;
use IEEE.STD_LOGIC_1164.ALL;
use work.crypt_pack.ALL;

entity chiffrement is
	port (
		chiffrement_i : in  type_state;
		permutation_i : in  std_logic;
		round_key_i   : in  type_key;
		chiffrement_o : out type_state;
		decrypt_i     : in  std_logic
	);

end chiffrement;

architecture chiffrement_arch of chiffrement is

	signal non_linear_s  : type_half_state;
	signal non_linear_s1 : type_half_state;
	signal linear_s      : type_half_state;
	signal linear_tmp_s  : type_half_state;
	signal linear        : bit8;

	component sbox
		port (
			sbox_i : in  bit8;
			sbox_o : out bit8
		);
	end component;


begin


	non_linear_s(0)(0) <= chiffrement_i(1)(3) xor round_key_i(1)(3);
	non_linear_s(0)(1) <= chiffrement_i(1)(2) xor round_key_i(1)(2);
	non_linear_s(0)(2) <= chiffrement_i(1)(1) xor round_key_i(1)(1);
	non_linear_s(0)(3) <= chiffrement_i(1)(0) xor round_key_i(1)(0);
	non_linear_s(1)(0) <= chiffrement_i(0)(3) xor round_key_i(0)(3);
	non_linear_s(1)(1) <= chiffrement_i(0)(2) xor round_key_i(0)(2);
	non_linear_s(1)(2) <= chiffrement_i(0)(1) xor round_key_i(0)(1);
	non_linear_s(1)(3) <= chiffrement_i(0)(0) xor round_key_i(0)(0);


	boucle_ligne : for i in 0 to 1 generate
		boucle_colonne : for j in 0 to 3 generate
				sboxx : sbox port map(
					sbox_i => non_linear_s(i)(j),
					sbox_o => non_linear_s1(i)(j)
				);
		end generate;
	end generate;

	linear_tmp_s(0)(0) <= chiffrement_i(2)(0);
	linear_tmp_s(0)(1) <= chiffrement_i(2)(1) xor chiffrement_i(1)(3);
	linear_tmp_s(0)(2) <= chiffrement_i(2)(2) xor chiffrement_i(1)(3);
	linear_tmp_s(0)(3) <= chiffrement_i(2)(3) xor chiffrement_i(1)(3);
	linear_tmp_s(1)(0) <= chiffrement_i(3)(0) xor chiffrement_i(1)(3);
	linear_tmp_s(1)(1) <= chiffrement_i(3)(1) xor chiffrement_i(1)(3);
	linear_tmp_s(1)(2) <= chiffrement_i(3)(2) xor chiffrement_i(1)(3);
	linear_tmp_s(1)(3) <= chiffrement_i(3)(3) xor chiffrement_i(1)(0) xor chiffrement_i(1)(1) xor chiffrement_i(1)(2);
	linear             <= chiffrement_i(0)(3) xor chiffrement_i(0)(1) xor chiffrement_i(0)(2) xor chiffrement_i(1)(3);

	linear_s(0)(0) <= non_linear_s1(0)(0) xor linear_tmp_s(0)(0);
	linear_s(0)(1) <= non_linear_s1(0)(1) xor linear_tmp_s(0)(1);
	linear_s(0)(2) <= non_linear_s1(0)(2) xor linear_tmp_s(0)(2);
	linear_s(0)(3) <= non_linear_s1(0)(3) xor linear_tmp_s(0)(3);
	linear_s(1)(0) <= non_linear_s1(1)(0) xor linear_tmp_s(1)(0);
	linear_s(1)(1) <= non_linear_s1(1)(1) xor linear_tmp_s(1)(1);
	linear_s(1)(2) <= non_linear_s1(1)(2) xor linear_tmp_s(1)(2);
	linear_s(1)(3) <= non_linear_s1(1)(3) xor linear xor linear_tmp_s(1)(3);

	chiffrement_o(0)(0) <= linear_s(1)(2) when permutation_i='1' and decrypt_i='0' else
		linear_s(1)(1) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(0)(0);
	chiffrement_o(0)(1) <= linear_s(0)(3) when permutation_i='1' and decrypt_i='0' else
		linear_s(0)(1) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(0)(1);
	chiffrement_o(0)(2) <= linear_s(1)(0) when permutation_i='1' and decrypt_i='0' else
		linear_s(1)(2) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(0)(2);
	chiffrement_o(0)(3) <= linear_s(0)(2) when permutation_i='1' and decrypt_i='0' else
		linear_s(0)(0) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(0)(3);
	chiffrement_o(1)(0) <= linear_s(0)(0) when permutation_i='1' and decrypt_i='0' else
		linear_s(0)(2) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(1)(0);
	chiffrement_o(1)(1) <= linear_s(0)(1) when permutation_i='1' and decrypt_i='0' else
		linear_s(0)(3) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(1)(1);
	chiffrement_o(1)(2) <= linear_s(1)(1) when permutation_i='1' and decrypt_i='0' else
		linear_s(1)(0) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(1)(2);
	chiffrement_o(1)(3) <= linear_s(1)(3) when permutation_i='1' and decrypt_i='0' else
		linear_s(1)(3) when permutation_i ='1' and decrypt_i='1' else chiffrement_i(1)(3);
	chiffrement_o(2)(0) <= chiffrement_i(0)(3) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(1)(0) when permutation_i ='1' and decrypt_i='1' else linear_s(0)(0);
	chiffrement_o(2)(1) <= chiffrement_i(0)(1) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(1)(1) when permutation_i ='1' and decrypt_i='1' else linear_s(0)(1);
	chiffrement_o(2)(2) <= chiffrement_i(1)(0) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(0)(3) when permutation_i ='1' and decrypt_i='1' else linear_s(0)(2);
	chiffrement_o(2)(3) <= chiffrement_i(1)(1) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(0)(1) when permutation_i ='1' and decrypt_i='1' else linear_s(0)(3);
	chiffrement_o(3)(0) <= chiffrement_i(1)(2) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(0)(2) when permutation_i ='1' and decrypt_i='1' else linear_s(1)(0);
	chiffrement_o(3)(1) <= chiffrement_i(0)(0) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(1)(2) when permutation_i ='1' and decrypt_i='1' else linear_s(1)(1);
	chiffrement_o(3)(2) <= chiffrement_i(0)(2) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(0)(0) when permutation_i ='1' and decrypt_i='1' else linear_s(1)(2);
	chiffrement_o(3)(3) <= chiffrement_i(1)(3) when permutation_i='1' and decrypt_i='0' else
		chiffrement_i(1)(3) when permutation_i ='1' and decrypt_i='1' else linear_s(1)(3);

end chiffrement_arch;

configuration chiffrement_conf of chiffrement is
	for chiffrement_arch
		for boucle_ligne
			for boucle_colonne
				for all : sbox
					use entity work.sbox( sbox_arch );
				end for;
			end for;
		end for;
	end for;
end configuration chiffrement_conf ;