1 |
4 |
dj1471 |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// XTEA IP Core ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This file is part of the xtea project ////
|
6 |
|
|
//// http://www.opencores.org/projects.cgi/web/xtea/overview ////
|
7 |
|
|
//// ////
|
8 |
|
|
//// An implementation of the XTEA encryption algorithm. ////
|
9 |
|
|
//// ////
|
10 |
|
|
//// TODO: ////
|
11 |
|
|
//// * Write a spec ////
|
12 |
|
|
//// * Wishbone compliance ////
|
13 |
|
|
//// ////
|
14 |
|
|
//// Author: David Johnson, dj@david-web.co.uk ////
|
15 |
|
|
//// ////
|
16 |
|
|
//////////////////////////////////////////////////////////////////////
|
17 |
|
|
//// ////
|
18 |
|
|
//// Copyright (C) 2006 David Johnson ////
|
19 |
|
|
//// ////
|
20 |
|
|
//// This source file is free software; you can redistribute it ////
|
21 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
22 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
23 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
24 |
|
|
//// later version. ////
|
25 |
|
|
//// ////
|
26 |
|
|
//// This source is distributed in the hope that it will be ////
|
27 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
28 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
29 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
30 |
|
|
//// details. ////
|
31 |
|
|
//// ////
|
32 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
33 |
|
|
//// Public License along with this source; if not, write to the ////
|
34 |
|
|
//// Free Software Foundation, Inc., 51 Franklin Street, Fifth ////
|
35 |
|
|
//// Floor, Boston, MA 02110-1301 USA ////
|
36 |
|
|
//// ////
|
37 |
|
|
//////////////////////////////////////////////////////////////////////
|
38 |
|
|
//
|
39 |
|
|
// CVS Revision History
|
40 |
|
|
//
|
41 |
|
|
// $Log: not supported by cvs2svn $
|
42 |
|
|
//
|
43 |
|
|
|
44 |
|
|
module xtea(clock, reset, mode, data_in1, data_in2, key_in, data_out1, data_out2, all_done);
|
45 |
|
|
|
46 |
|
|
parameter s0 = 8'd0, s1 = 8'd1, s2 = 8'd2, s3 = 8'd3, s4 = 8'd4, s5 = 8'd5, s6 = 8'd6, s7 = 8'd7, s8 = 8'd8, s9 = 8'd9, s10 = 8'd10,
|
47 |
|
|
s11 = 8'd11, s12 = 8'd12, s13 = 8'd13, s14 = 8'd14, s15 = 8'd15, s16 = 8'd16, s17 = 8'd17;
|
48 |
|
|
|
49 |
|
|
input clock, reset, mode;
|
50 |
|
|
input[31:0] data_in1, data_in2;
|
51 |
|
|
input[127:0] key_in;
|
52 |
|
|
output[31:0] data_out1, data_out2;
|
53 |
|
|
output all_done;
|
54 |
|
|
|
55 |
|
|
wire clock, reset;
|
56 |
|
|
wire[31:0] data_in1, data_in2;
|
57 |
|
|
wire[127:0] key_in;
|
58 |
|
|
reg all_done, while_flag, modereg;
|
59 |
|
|
reg[1:0] selectslice;
|
60 |
|
|
reg[7:0] state;
|
61 |
|
|
reg[7:0] x;
|
62 |
|
|
reg[31:0] data_out1, data_out2, sum, workunit1, workunit2, delta;
|
63 |
|
|
|
64 |
|
|
always @(posedge clock or posedge reset)
|
65 |
|
|
begin
|
66 |
|
|
if (reset)
|
67 |
|
|
//reset state
|
68 |
|
|
state = s0;
|
69 |
|
|
else begin
|
70 |
|
|
case (state)
|
71 |
|
|
s0: state = s1;
|
72 |
|
|
s1: state = s2;
|
73 |
|
|
s2: state = s3;
|
74 |
|
|
s3: state = while_flag ? s4 : s14;
|
75 |
|
|
s4: state = modereg ? s10 : s5;
|
76 |
|
|
s5: state = s6;
|
77 |
|
|
s6: state = s7;
|
78 |
|
|
s7: state = s8;
|
79 |
|
|
s8: state = s9;
|
80 |
|
|
s9: state = s2;
|
81 |
|
|
s10: state = s11;
|
82 |
|
|
s11: state = s12;
|
83 |
|
|
s12: state = s13;
|
84 |
|
|
s13: state = s14;
|
85 |
|
|
s14: state = s2;
|
86 |
|
|
s15: state = s16;
|
87 |
|
|
s16: state = s17;
|
88 |
|
|
s17: state = s17;
|
89 |
|
|
default: state = 4'bxxxx;
|
90 |
|
|
endcase
|
91 |
|
|
end
|
92 |
|
|
end
|
93 |
|
|
|
94 |
|
|
always @(posedge clock or posedge reset)
|
95 |
|
|
begin
|
96 |
|
|
if (reset) begin
|
97 |
|
|
//reset all our outputs and registers
|
98 |
|
|
data_out1 = 32'h00000000;
|
99 |
|
|
data_out2 = 32'h00000000;
|
100 |
|
|
x = 8'b00000000;
|
101 |
|
|
sum = 32'h00000000;
|
102 |
|
|
while_flag = 1'b0;
|
103 |
|
|
workunit1 = 32'h00000000;
|
104 |
|
|
workunit2 = 32'h00000000;
|
105 |
|
|
selectslice = 1'b0;
|
106 |
|
|
all_done = 1'b0;
|
107 |
|
|
delta = 32'h00000000;
|
108 |
|
|
modereg = 1'b0;
|
109 |
|
|
end
|
110 |
|
|
else begin
|
111 |
|
|
case (state)
|
112 |
|
|
s1: begin
|
113 |
|
|
//store input values to registers in case they're not stable
|
114 |
|
|
workunit1 = data_in1;
|
115 |
|
|
workunit2 = data_in2;
|
116 |
|
|
delta = 32'h9E3779B9;
|
117 |
|
|
sum = 32'hc6ef3720;
|
118 |
|
|
modereg = mode;
|
119 |
|
|
end
|
120 |
|
|
s2: if (x < 8'd32) while_flag = 1'b1; else while_flag = 1'b0;
|
121 |
|
|
s3: begin
|
122 |
|
|
//This null state was necessary to fix a timing issue.
|
123 |
|
|
//s2 sets while_flag and previously the control path read it in the same state
|
124 |
|
|
//(but in the next clock cycle), however the reg wasn't set when we tried to
|
125 |
|
|
//read it, so this state was inserted to add a delay. This was when running @25MHz.
|
126 |
|
|
//FIXME: there's got to be a better solution to this...
|
127 |
|
|
end
|
128 |
|
|
s4: begin
|
129 |
|
|
//This state does nothing in the data path; it's used for an if statement in the
|
130 |
|
|
//control path.
|
131 |
|
|
end
|
132 |
|
|
/* States 5-9 used for decipher operations */
|
133 |
|
|
s5: selectslice = (sum >> 32'd11 & 32'd3);
|
134 |
|
|
s6: case (selectslice)
|
135 |
|
|
2'b00: workunit2 = workunit2 - (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[127:96]));
|
136 |
|
|
2'b01: workunit2 = workunit2 - (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[95:64]));
|
137 |
|
|
2'b10: workunit2 = workunit2 - (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[63:32]));
|
138 |
|
|
2'b11: workunit2 = workunit2 - (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[31:0]));
|
139 |
|
|
default: workunit2 = 32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
|
140 |
|
|
endcase
|
141 |
|
|
s7: sum = sum - delta;
|
142 |
|
|
s8: selectslice = (sum & 32'd3);
|
143 |
|
|
s9: begin
|
144 |
|
|
case (selectslice)
|
145 |
|
|
2'b00: workunit1 = workunit1 - (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[127:96]));
|
146 |
|
|
2'b01: workunit1 = workunit1 - (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[95:64]));
|
147 |
|
|
2'b10: workunit1 = workunit1 - (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[63:32]));
|
148 |
|
|
2'b11: workunit1 = workunit1 - (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[31:0]));
|
149 |
|
|
default: workunit1 = 32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
|
150 |
|
|
endcase
|
151 |
|
|
x = x + 1'b1;
|
152 |
|
|
end
|
153 |
|
|
/* States 10-14 used for encipher operations */
|
154 |
|
|
s10: selectslice = (sum & 32'd3);
|
155 |
|
|
s11: case (selectslice)
|
156 |
|
|
2'b00: workunit1 = workunit1 + (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[127:96]));
|
157 |
|
|
2'b01: workunit1 = workunit1 + (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[95:64]));
|
158 |
|
|
2'b10: workunit1 = workunit1 + (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[63:32]));
|
159 |
|
|
2'b11: workunit1 = workunit1 + (((workunit2 << 4 ^ workunit2 >> 5) + workunit2) ^ (sum + key_in[31:0]));
|
160 |
|
|
default: workunit1 = 32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
|
161 |
|
|
endcase
|
162 |
|
|
s12: sum = sum + delta;
|
163 |
|
|
s13: selectslice = (sum >> 32'd11 & 32'd3);
|
164 |
|
|
s14: begin
|
165 |
|
|
case (selectslice)
|
166 |
|
|
2'b00: workunit2 = workunit2 + (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[127:96]));
|
167 |
|
|
2'b01: workunit2 = workunit2 + (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[95:64]));
|
168 |
|
|
2'b10: workunit2 = workunit2 + (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[63:32]));
|
169 |
|
|
2'b11: workunit2 = workunit2 + (((workunit1 << 4 ^ workunit1 >> 5) + workunit1) ^ (sum + key_in[31:0]));
|
170 |
|
|
default: workunit2 = 32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
|
171 |
|
|
endcase
|
172 |
|
|
x = x + 1'b1;
|
173 |
|
|
end
|
174 |
|
|
s15: begin
|
175 |
|
|
//This state was added to fix a timing issue.
|
176 |
|
|
//Same issue as above - trying to read workunit1 & workunit2 before they've settled.
|
177 |
|
|
end
|
178 |
|
|
s16: begin
|
179 |
|
|
//set the outputs to the working registers
|
180 |
|
|
data_out1 = workunit1;
|
181 |
|
|
data_out2 = workunit2;
|
182 |
|
|
end
|
183 |
|
|
s17: all_done = 1'b1;
|
184 |
|
|
default: begin
|
185 |
|
|
data_out1 = 32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
|
186 |
|
|
data_out2 = 32'bzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
|
187 |
|
|
end
|
188 |
|
|
endcase
|
189 |
|
|
end
|
190 |
|
|
end
|
191 |
|
|
|
192 |
|
|
endmodule
|