forked from scarter93/RSA-Encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_exp_comparison.vhd
More file actions
238 lines (191 loc) · 5.91 KB
/
mod_exp_comparison.vhd
File metadata and controls
238 lines (191 loc) · 5.91 KB
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
-- Entity name: modular_exponentiation
-- Author: Luis Gallet
-- Contact: luis.galletzambrano@mail.mcgill.ca
-- Date: March 28th, 2016
-- Description:
-- Module responsible of performing encryption and decryption. It uses the
-- montgomery_comparison.vhd to perform multiplication and modulo operations.
library ieee;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
library lpm;
use lpm.lpm_components.all;
entity mod_exp_comparison is
generic(WIDTH_IN : integer := 32
);
port(
N : in unsigned(WIDTH_IN-1 downto 0); --Number
--Exp : in unsigned(WIDTH_IN-1 downto 0); --Exponent
--M : in unsigned(WIDTH_IN-1 downto 0); --Modulus
enc_dec: in std_logic;
clk : in std_logic;
reset : in std_logic;
C : out unsigned(WIDTH_IN-1 downto 0)
);
end entity;
architecture behavior of mod_exp_comparison is
constant zero : unsigned(WIDTH_IN-1 downto 0) := (others => '0');
--------------------------------------32 bit constants------------------------------------------------
--constant K : unsigned (WIDTH_IN-1 downto 0) := "00000110010001101110000100100000101111011101110010111101100011001010101111001011011010101000010100001011000100011101101000011110";
constant M : unsigned (WIDTH_IN-1 downto 0) := "10000100010001111000010010000101";
constant dec_Exp : unsigned(WIDTH_IN-1 downto 0) := "00101010110001011001000101000101";
constant enc_Exp : unsigned(WIDTH_IN-1 downto 0) := "00000000000000010000000000000001";
-------------------------------------------------------------------------------------------------------
-- Intermidiate signals
signal temp_A1,temp_A2 : unsigned(WIDTH_IN-1 downto 0) := (WIDTH_IN-1 downto 0 => '0');
signal temp_B1, temp_B2 : unsigned(WIDTH_IN-1 downto 0) := (WIDTH_IN-1 downto 0 => '0');
signal temp_d_ready, temp_d_ready2 : std_logic := '0';
signal temp_M1, temp_M2 : unsigned(WIDTH_IN-1 downto 0) := (WIDTH_IN-1 downto 0 => '0');
signal latch_in, latch_in2 : std_logic := '0';
signal temp_M : unsigned(WIDTH_IN-1 downto 0) := (WIDTH_IN-1 downto 0 => '0');
signal temp_C : unsigned(WIDTH_IN-1 downto 0):= (WIDTH_IN-1 downto 0 => '0');
-- FSM states
type STATE_TYPE is (s0, s1, s2, s3, s4, s5, s6, s7, s8);
signal state: STATE_TYPE := s0;
component montgomery_comparison
Generic(WIDTH_IN : integer := 8
);
Port( A : in unsigned(WIDTH_IN-1 downto 0);
B : in unsigned(WIDTH_IN-1 downto 0);
N : in unsigned(WIDTH_IN-1 downto 0);
latch : in std_logic;
clk : in std_logic;
reset : in std_logic;
data_ready : out std_logic;
M : out unsigned(WIDTH_IN-1 downto 0)
);
end component;
begin
-- Montgomery comparison components
mont_mult_1: montgomery_comparison
generic map(WIDTH_IN => WIDTH_IN)
port map(
A => temp_A1,
B => temp_B1,
N => temp_M,
latch => latch_in,
clk => clk,
reset => reset,
data_ready => temp_d_ready,
M => temp_M1
);
mont_mult_2: montgomery_comparison
generic map(WIDTH_IN => WIDTH_IN)
port map(
A => temp_A2,
B => temp_B2,
N => temp_M,
latch => latch_in2,
clk => clk,
reset => reset,
data_ready => temp_d_ready2,
M => temp_M2
);
C <= temp_C;
sqr_mult : Process(clk, reset, N)
variable count : integer := 0;
variable shift_count : integer := 0;
variable temp_N : unsigned(WIDTH_IN-1 downto 0):= (WIDTH_IN-1 downto 0 => '0');
variable P : unsigned(WIDTH_IN-1 downto 0):= (WIDTH_IN-1 downto 0 => '0');
variable P_old : unsigned(WIDTH_IN-1 downto 0):= (WIDTH_IN-1 downto 0 => '0');
variable R : unsigned(WIDTH_IN-1 downto 0):= (WIDTH_IN-1 downto 0 => '0');
variable temp_Exp : unsigned(WIDTH_IN-1 downto 0);
variable temp_mod : unsigned(WIDTH_IN-1 downto 0);
begin
if reset = '1' then
count := 0;
shift_count := 0;
temp_N := (others => '0');
P := (others => '0');
R := (others => '0');
temp_Exp := (others => '0');
temp_mod := (others => '0');
temp_M <= (others => '0');
state <= s0;
elsif rising_edge(clk) then
case state is
-- Check if there are new inputs available
when s0 =>
--(M = zero) OR (Exp = zero) OR
if((N = zero)) OR ((temp_M = M) AND (temp_N = N)) then
state <= s0;
else
temp_mod := M;
state <= s1;
end if;
-- If MSB of modulus is not 1 then shift it left until a 1 is found and count how many times it was shifted
when s1 =>
if(temp_mod(WIDTH_IN-1) = '1')then
if(enc_dec = '1')then
temp_Exp := enc_Exp;
else
temp_Exp := dec_Exp;
end if;
--temp_Exp := Exp;
temp_M <= M;
temp_N := N;
state <= s2;
else
temp_mod := (shift_left(temp_mod,natural(1)));
shift_count := shift_count + 1;
state <= s1;
end if;
-- Assign initial values to P and R
when s2 =>
P_old := temp_N;
R := to_unsigned(1,WIDTH_IN);
state <= s3;
-- Check Listing 1 in report. This operation is inside the for loop and it is always performed
when s3 =>
temp_A1 <= P_old;
temp_B1 <= P_old;
latch_in <= '1';
if(temp_d_ready = '0')then
state <= s4;
end if;
-- If LSB of the exponent is 1 then compute R, else go to state 7
when s4 =>
latch_in <= '0';
if(temp_d_ready = '1')then
P := temp_M1;
if(temp_Exp(0) = '1')then
state <= s5;
else
state <= s7;
end if;
end if;
when s5 =>
temp_A2 <= R;
temp_B2 <= P_old;
latch_in2 <= '1';
if(temp_d_ready2 = '0')then
state <= s6;
end if;
when s6 =>
latch_in2 <= '0';
if(temp_d_ready2 = '1') then
R := temp_M2;
state <= s7;
end if;
-- If the statement is true, it means that we have checked all bits in the exponent or exponent is zero and we compute the output
when s7 =>
if (count = (WIDTH_IN-1)-shift_count) OR (temp_Exp = zero) then
temp_C <= R;
state <= s8;
else -- if the statement is false, then we shift the exponent right and increment count
temp_Exp := (shift_right(temp_Exp, natural(1)));
P_old := P;
count := count + 1;
state <= s3;
end if;
when s8 =>
count := 0;
shift_count := 0;
P := (others => '0');
R := (others => '0');
temp_mod := (others => '0');
state <= s0;
end case;
end if;
end process sqr_mult;
end behavior;