1 |
5 |
muhammedko |
----------------------------------------------------------------------------------
|
2 |
|
|
-- Company:
|
3 |
|
|
-- Engineer: MUHAMMED KOCAOGLU
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 12/29/2021 12:07:32 AM
|
6 |
|
|
-- Design Name:
|
7 |
|
|
-- Module Name: AES_pkg - Behavioral
|
8 |
|
|
-- Project Name:
|
9 |
|
|
-- Target Devices:
|
10 |
|
|
-- Tool Versions:
|
11 |
|
|
-- Description:
|
12 |
|
|
--
|
13 |
|
|
-- Dependencies:
|
14 |
|
|
--
|
15 |
|
|
-- Revision:
|
16 |
|
|
-- Revision 0.01 - File Created
|
17 |
|
|
-- Additional Comments:
|
18 |
|
|
--
|
19 |
|
|
----------------------------------------------------------------------------------
|
20 |
|
|
LIBRARY IEEE;
|
21 |
|
|
USE IEEE.STD_LOGIC_1164.ALL;
|
22 |
|
|
USE IEEE.STD_LOGIC_ARITH.ALL;
|
23 |
|
|
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
|
24 |
|
|
|
25 |
|
|
-- Uncomment the following library declaration if using
|
26 |
|
|
-- arithmetic functions with Signed or Unsigned values
|
27 |
|
|
--use IEEE.NUMERIC_STD.ALL;
|
28 |
|
|
|
29 |
|
|
-- Uncomment the following library declaration if instantiating
|
30 |
|
|
-- any Xilinx leaf cells in this code.
|
31 |
|
|
--library UNISIM;
|
32 |
|
|
--use UNISIM.VComponents.all;
|
33 |
|
|
|
34 |
|
|
PACKAGE AES_pkg IS
|
35 |
|
|
TYPE array2D8 IS ARRAY (NATURAL RANGE 0 TO 3) OF STD_LOGIC_VECTOR(7 DOWNTO 0);
|
36 |
|
|
TYPE array3D8 IS ARRAY (NATURAL RANGE 0 TO 3) OF array2D8;
|
37 |
|
|
|
38 |
|
|
FUNCTION convert1D_to_2D(din : IN STD_LOGIC_VECTOR(127 DOWNTO 0)) RETURN array3D8;
|
39 |
|
|
FUNCTION generateSubKey(din : IN STD_LOGIC_VECTOR(127 DOWNTO 0); stage : IN INTEGER) RETURN array3D8;
|
40 |
|
|
FUNCTION generateSubKey(din : IN array3D8; stage : IN INTEGER) RETURN array3D8;
|
41 |
|
|
FUNCTION encryptMessage(aes_message : IN STD_LOGIC_VECTOR(127 DOWNTO 0); aes_key : IN STD_LOGIC_VECTOR(127 DOWNTO 0)) RETURN array3D8;
|
42 |
|
|
FUNCTION encryptMessage(aes_message : IN array3D8; aes_key : IN array3D8) RETURN array3D8;
|
43 |
|
|
FUNCTION mixcolumnOneByte(i1, i2, i3, i4 : IN STD_LOGIC_VECTOR (7 DOWNTO 0)) RETURN STD_LOGIC_VECTOR;
|
44 |
|
|
FUNCTION encyrptFinal(aes_message : IN array3D8; aes_key : IN array3D8) RETURN STD_LOGIC_VECTOR;
|
45 |
|
|
|
46 |
|
|
TYPE sBoxArray IS ARRAY (NATURAL RANGE 0 TO 255) OF STD_LOGIC_VECTOR(7 DOWNTO 0);
|
47 |
|
|
CONSTANT sBox : sBoxArray := (
|
48 |
|
|
x"63", x"7c", x"77", x"7b", x"f2", x"6b", x"6f", x"c5", x"30", x"01", x"67", x"2b", x"fe", x"d7", x"ab", x"76",
|
49 |
|
|
x"ca", x"82", x"c9", x"7d", x"fa", x"59", x"47", x"f0", x"ad", x"d4", x"a2", x"af", x"9c", x"a4", x"72", x"c0",
|
50 |
|
|
x"b7", x"fd", x"93", x"26", x"36", x"3f", x"f7", x"cc", x"34", x"a5", x"e5", x"f1", x"71", x"d8", x"31", x"15",
|
51 |
|
|
x"04", x"c7", x"23", x"c3", x"18", x"96", x"05", x"9a", x"07", x"12", x"80", x"e2", x"eb", x"27", x"b2", x"75",
|
52 |
|
|
x"09", x"83", x"2c", x"1a", x"1b", x"6e", x"5a", x"a0", x"52", x"3b", x"d6", x"b3", x"29", x"e3", x"2f", x"84",
|
53 |
|
|
x"53", x"d1", x"00", x"ed", x"20", x"fc", x"b1", x"5b", x"6a", x"cb", x"be", x"39", x"4a", x"4c", x"58", x"cf",
|
54 |
|
|
x"d0", x"ef", x"aa", x"fb", x"43", x"4d", x"33", x"85", x"45", x"f9", x"02", x"7f", x"50", x"3c", x"9f", x"a8",
|
55 |
|
|
x"51", x"a3", x"40", x"8f", x"92", x"9d", x"38", x"f5", x"bc", x"b6", x"da", x"21", x"10", x"ff", x"f3", x"d2",
|
56 |
|
|
x"cd", x"0c", x"13", x"ec", x"5f", x"97", x"44", x"17", x"c4", x"a7", x"7e", x"3d", x"64", x"5d", x"19", x"73",
|
57 |
|
|
x"60", x"81", x"4f", x"dc", x"22", x"2a", x"90", x"88", x"46", x"ee", x"b8", x"14", x"de", x"5e", x"0b", x"db",
|
58 |
|
|
x"e0", x"32", x"3a", x"0a", x"49", x"06", x"24", x"5c", x"c2", x"d3", x"ac", x"62", x"91", x"95", x"e4", x"79",
|
59 |
|
|
x"e7", x"c8", x"37", x"6d", x"8d", x"d5", x"4e", x"a9", x"6c", x"56", x"f4", x"ea", x"65", x"7a", x"ae", x"08",
|
60 |
|
|
x"ba", x"78", x"25", x"2e", x"1c", x"a6", x"b4", x"c6", x"e8", x"dd", x"74", x"1f", x"4b", x"bd", x"8b", x"8a",
|
61 |
|
|
x"70", x"3e", x"b5", x"66", x"48", x"03", x"f6", x"0e", x"61", x"35", x"57", x"b9", x"86", x"c1", x"1d", x"9e",
|
62 |
|
|
x"e1", x"f8", x"98", x"11", x"69", x"d9", x"8e", x"94", x"9b", x"1e", x"87", x"e9", x"ce", x"55", x"28", x"df",
|
63 |
|
|
x"8c", x"a1", x"89", x"0d", x"bf", x"e6", x"42", x"68", x"41", x"99", x"2d", x"0f", x"b0", x"54", x"bb", x"16"
|
64 |
|
|
);
|
65 |
|
|
|
66 |
|
|
TYPE array2D8_sbox IS ARRAY (NATURAL RANGE 0 TO 3) OF STD_LOGIC_VECTOR(7 DOWNTO 0);
|
67 |
|
|
TYPE array3D8_sbox IS ARRAY (NATURAL RANGE 0 TO 9) OF array2D8_sbox;
|
68 |
|
|
CONSTANT rcon : array3D8_sbox := (
|
69 |
|
|
(x"01", x"00", x"00", x"00"),
|
70 |
|
|
(x"02", x"00", x"00", x"00"),
|
71 |
|
|
(x"04", x"00", x"00", x"00"),
|
72 |
|
|
(x"08", x"00", x"00", x"00"),
|
73 |
|
|
(x"10", x"00", x"00", x"00"),
|
74 |
|
|
(x"20", x"00", x"00", x"00"),
|
75 |
|
|
(x"40", x"00", x"00", x"00"),
|
76 |
|
|
(x"80", x"00", x"00", x"00"),
|
77 |
|
|
(x"1B", x"00", x"00", x"00"),
|
78 |
|
|
(x"36", x"00", x"00", x"00")
|
79 |
|
|
);
|
80 |
|
|
END PACKAGE;
|
81 |
|
|
PACKAGE BODY AES_pkg IS
|
82 |
|
|
FUNCTION convert1D_to_2D(din : IN STD_LOGIC_VECTOR(127 DOWNTO 0)) RETURN array3D8 IS
|
83 |
|
|
VARIABLE result : array3D8;
|
84 |
|
|
BEGIN
|
85 |
|
|
result(0) := (din(16 * 8 - 1 DOWNTO 15 * 8), din(12 * 8 - 1 DOWNTO 11 * 8), din(8 * 8 - 1 DOWNTO 7 * 8), din(4 * 8 - 1 DOWNTO 3 * 8));
|
86 |
|
|
result(1) := (din(15 * 8 - 1 DOWNTO 14 * 8), din(11 * 8 - 1 DOWNTO 10 * 8), din(7 * 8 - 1 DOWNTO 6 * 8), din(3 * 8 - 1 DOWNTO 2 * 8));
|
87 |
|
|
result(2) := (din(14 * 8 - 1 DOWNTO 13 * 8), din(10 * 8 - 1 DOWNTO 9 * 8), din(6 * 8 - 1 DOWNTO 5 * 8), din(2 * 8 - 1 DOWNTO 1 * 8));
|
88 |
|
|
result(3) := (din(13 * 8 - 1 DOWNTO 12 * 8), din(9 * 8 - 1 DOWNTO 8 * 8), din(5 * 8 - 1 DOWNTO 4 * 8), din(1 * 8 - 1 DOWNTO 0 * 8));
|
89 |
|
|
RETURN result;
|
90 |
|
|
END;
|
91 |
|
|
|
92 |
|
|
FUNCTION generateSubKey(din : IN STD_LOGIC_VECTOR(127 DOWNTO 0); stage : IN INTEGER) RETURN array3D8 IS
|
93 |
|
|
VARIABLE result : array3D8;
|
94 |
|
|
BEGIN
|
95 |
|
|
result(0)(0) := din(16 * 8 - 1 DOWNTO 15 * 8) XOR sBox(conv_integer(unsigned(din(3 * 8 - 1 DOWNTO 2 * 8)))) XOR rcon(stage)(0);
|
96 |
|
|
result(0)(1) := din(16 * 8 - 1 DOWNTO 15 * 8) XOR sBox(conv_integer(unsigned(din(3 * 8 - 1 DOWNTO 2 * 8)))) XOR rcon(stage)(0) XOR din(12 * 8 - 1 DOWNTO 11 * 8);
|
97 |
|
|
result(0)(2) := din(16 * 8 - 1 DOWNTO 15 * 8) XOR sBox(conv_integer(unsigned(din(3 * 8 - 1 DOWNTO 2 * 8)))) XOR rcon(stage)(0) XOR din(12 * 8 - 1 DOWNTO 11 * 8) XOR din(8 * 8 - 1 DOWNTO 7 * 8);
|
98 |
|
|
result(0)(3) := din(16 * 8 - 1 DOWNTO 15 * 8) XOR sBox(conv_integer(unsigned(din(3 * 8 - 1 DOWNTO 2 * 8)))) XOR rcon(stage)(0) XOR din(12 * 8 - 1 DOWNTO 11 * 8) XOR din(8 * 8 - 1 DOWNTO 7 * 8) XOR din(4 * 8 - 1 DOWNTO 3 * 8);
|
99 |
|
|
|
100 |
|
|
result(1)(0) := din(15 * 8 - 1 DOWNTO 14 * 8) XOR sBox(conv_integer(unsigned(din(2 * 8 - 1 DOWNTO 1 * 8)))) XOR rcon(stage)(1);
|
101 |
|
|
result(1)(1) := din(15 * 8 - 1 DOWNTO 14 * 8) XOR sBox(conv_integer(unsigned(din(2 * 8 - 1 DOWNTO 1 * 8)))) XOR rcon(stage)(1) XOR din(11 * 8 - 1 DOWNTO 10 * 8);
|
102 |
|
|
result(1)(2) := din(15 * 8 - 1 DOWNTO 14 * 8) XOR sBox(conv_integer(unsigned(din(2 * 8 - 1 DOWNTO 1 * 8)))) XOR rcon(stage)(1) XOR din(11 * 8 - 1 DOWNTO 10 * 8) XOR din(7 * 8 - 1 DOWNTO 6 * 8);
|
103 |
|
|
result(1)(3) := din(15 * 8 - 1 DOWNTO 14 * 8) XOR sBox(conv_integer(unsigned(din(2 * 8 - 1 DOWNTO 1 * 8)))) XOR rcon(stage)(1) XOR din(11 * 8 - 1 DOWNTO 10 * 8) XOR din(7 * 8 - 1 DOWNTO 6 * 8) XOR din(3 * 8 - 1 DOWNTO 2 * 8);
|
104 |
|
|
|
105 |
|
|
result(2)(0) := din(14 * 8 - 1 DOWNTO 13 * 8) XOR sBox(conv_integer(unsigned(din(1 * 8 - 1 DOWNTO 0 * 8)))) XOR rcon(stage)(2);
|
106 |
|
|
result(2)(1) := din(14 * 8 - 1 DOWNTO 13 * 8) XOR sBox(conv_integer(unsigned(din(1 * 8 - 1 DOWNTO 0 * 8)))) XOR rcon(stage)(2) XOR din(10 * 8 - 1 DOWNTO 9 * 8);
|
107 |
|
|
result(2)(2) := din(14 * 8 - 1 DOWNTO 13 * 8) XOR sBox(conv_integer(unsigned(din(1 * 8 - 1 DOWNTO 0 * 8)))) XOR rcon(stage)(2) XOR din(10 * 8 - 1 DOWNTO 9 * 8) XOR din(6 * 8 - 1 DOWNTO 5 * 8);
|
108 |
|
|
result(2)(3) := din(14 * 8 - 1 DOWNTO 13 * 8) XOR sBox(conv_integer(unsigned(din(1 * 8 - 1 DOWNTO 0 * 8)))) XOR rcon(stage)(2) XOR din(10 * 8 - 1 DOWNTO 9 * 8) XOR din(6 * 8 - 1 DOWNTO 5 * 8) XOR din(2 * 8 - 1 DOWNTO 1 * 8);
|
109 |
|
|
|
110 |
|
|
result(3)(0) := din(13 * 8 - 1 DOWNTO 12 * 8) XOR sBox(conv_integer(unsigned(din(4 * 8 - 1 DOWNTO 3 * 8)))) XOR rcon(stage)(3);
|
111 |
|
|
result(3)(1) := din(13 * 8 - 1 DOWNTO 12 * 8) XOR sBox(conv_integer(unsigned(din(4 * 8 - 1 DOWNTO 3 * 8)))) XOR rcon(stage)(3) XOR din(9 * 8 - 1 DOWNTO 8 * 8);
|
112 |
|
|
result(3)(2) := din(13 * 8 - 1 DOWNTO 12 * 8) XOR sBox(conv_integer(unsigned(din(4 * 8 - 1 DOWNTO 3 * 8)))) XOR rcon(stage)(3) XOR din(9 * 8 - 1 DOWNTO 8 * 8) XOR din(5 * 8 - 1 DOWNTO 4 * 8);
|
113 |
|
|
result(3)(3) := din(13 * 8 - 1 DOWNTO 12 * 8) XOR sBox(conv_integer(unsigned(din(4 * 8 - 1 DOWNTO 3 * 8)))) XOR rcon(stage)(3) XOR din(9 * 8 - 1 DOWNTO 8 * 8) XOR din(5 * 8 - 1 DOWNTO 4 * 8) XOR din(1 * 8 - 1 DOWNTO 0 * 8);
|
114 |
|
|
RETURN result;
|
115 |
|
|
END;
|
116 |
|
|
|
117 |
|
|
FUNCTION generateSubKey(din : IN array3D8; stage : IN INTEGER) RETURN array3D8 IS
|
118 |
|
|
VARIABLE result : array3D8;
|
119 |
|
|
BEGIN
|
120 |
|
|
result(0)(0) := din(0)(0) XOR sBox(conv_integer(unsigned(din(1)(3)))) XOR rcon(stage)(0);
|
121 |
|
|
result(0)(1) := din(0)(0) XOR sBox(conv_integer(unsigned(din(1)(3)))) XOR rcon(stage)(0) XOR din(0)(1);
|
122 |
|
|
result(0)(2) := din(0)(0) XOR sBox(conv_integer(unsigned(din(1)(3)))) XOR rcon(stage)(0) XOR din(0)(1) XOR din(0)(2);
|
123 |
|
|
result(0)(3) := din(0)(0) XOR sBox(conv_integer(unsigned(din(1)(3)))) XOR rcon(stage)(0) XOR din(0)(1) XOR din(0)(2) XOR din(0)(3);
|
124 |
|
|
|
125 |
|
|
result(1)(0) := din(1)(0) XOR sBox(conv_integer(unsigned(din(2)(3)))) XOR rcon(stage)(1);
|
126 |
|
|
result(1)(1) := din(1)(0) XOR sBox(conv_integer(unsigned(din(2)(3)))) XOR rcon(stage)(1) XOR din(1)(1);
|
127 |
|
|
result(1)(2) := din(1)(0) XOR sBox(conv_integer(unsigned(din(2)(3)))) XOR rcon(stage)(1) XOR din(1)(1) XOR din(1)(2);
|
128 |
|
|
result(1)(3) := din(1)(0) XOR sBox(conv_integer(unsigned(din(2)(3)))) XOR rcon(stage)(1) XOR din(1)(1) XOR din(1)(2) XOR din(1)(3);
|
129 |
|
|
|
130 |
|
|
result(2)(0) := din(2)(0) XOR sBox(conv_integer(unsigned(din(3)(3)))) XOR rcon(stage)(2);
|
131 |
|
|
result(2)(1) := din(2)(0) XOR sBox(conv_integer(unsigned(din(3)(3)))) XOR rcon(stage)(2) XOR din(2)(1);
|
132 |
|
|
result(2)(2) := din(2)(0) XOR sBox(conv_integer(unsigned(din(3)(3)))) XOR rcon(stage)(2) XOR din(2)(1) XOR din(2)(2);
|
133 |
|
|
result(2)(3) := din(2)(0) XOR sBox(conv_integer(unsigned(din(3)(3)))) XOR rcon(stage)(2) XOR din(2)(1) XOR din(2)(2) XOR din(2)(3);
|
134 |
|
|
|
135 |
|
|
result(3)(0) := din(3)(0) XOR sBox(conv_integer(unsigned(din(0)(3)))) XOR rcon(stage)(3);
|
136 |
|
|
result(3)(1) := din(3)(0) XOR sBox(conv_integer(unsigned(din(0)(3)))) XOR rcon(stage)(3) XOR din(3)(1);
|
137 |
|
|
result(3)(2) := din(3)(0) XOR sBox(conv_integer(unsigned(din(0)(3)))) XOR rcon(stage)(3) XOR din(3)(1) XOR din(3)(2);
|
138 |
|
|
result(3)(3) := din(3)(0) XOR sBox(conv_integer(unsigned(din(0)(3)))) XOR rcon(stage)(3) XOR din(3)(1) XOR din(3)(2) XOR din(3)(3);
|
139 |
|
|
RETURN result;
|
140 |
|
|
|
141 |
|
|
END;
|
142 |
|
|
FUNCTION encryptMessage(aes_message : IN STD_LOGIC_VECTOR(127 DOWNTO 0); aes_key : IN STD_LOGIC_VECTOR(127 DOWNTO 0)) RETURN array3D8 IS
|
143 |
|
|
VARIABLE result : array3D8;
|
144 |
|
|
BEGIN
|
145 |
|
|
result(0)(0) := aes_message(16 * 8 - 1 DOWNTO 15 * 8) XOR aes_key(16 * 8 - 1 DOWNTO 15 * 8);
|
146 |
|
|
result(0)(1) := aes_message(12 * 8 - 1 DOWNTO 11 * 8) XOR aes_key(12 * 8 - 1 DOWNTO 11 * 8);
|
147 |
|
|
result(0)(2) := aes_message(8 * 8 - 1 DOWNTO 7 * 8) XOR aes_key(8 * 8 - 1 DOWNTO 7 * 8);
|
148 |
|
|
result(0)(3) := aes_message(4 * 8 - 1 DOWNTO 3 * 8) XOR aes_key(4 * 8 - 1 DOWNTO 3 * 8);
|
149 |
|
|
|
150 |
|
|
result(1)(0) := aes_message(15 * 8 - 1 DOWNTO 14 * 8) XOR aes_key(15 * 8 - 1 DOWNTO 14 * 8);
|
151 |
|
|
result(1)(1) := aes_message(11 * 8 - 1 DOWNTO 10 * 8) XOR aes_key(11 * 8 - 1 DOWNTO 10 * 8);
|
152 |
|
|
result(1)(2) := aes_message(7 * 8 - 1 DOWNTO 6 * 8) XOR aes_key(7 * 8 - 1 DOWNTO 6 * 8);
|
153 |
|
|
result(1)(3) := aes_message(3 * 8 - 1 DOWNTO 2 * 8) XOR aes_key(3 * 8 - 1 DOWNTO 2 * 8);
|
154 |
|
|
|
155 |
|
|
result(2)(0) := aes_message(14 * 8 - 1 DOWNTO 13 * 8) XOR aes_key(14 * 8 - 1 DOWNTO 13 * 8);
|
156 |
|
|
result(2)(1) := aes_message(10 * 8 - 1 DOWNTO 9 * 8) XOR aes_key(10 * 8 - 1 DOWNTO 9 * 8);
|
157 |
|
|
result(2)(2) := aes_message(6 * 8 - 1 DOWNTO 5 * 8) XOR aes_key(6 * 8 - 1 DOWNTO 5 * 8);
|
158 |
|
|
result(2)(3) := aes_message(2 * 8 - 1 DOWNTO 1 * 8) XOR aes_key(2 * 8 - 1 DOWNTO 1 * 8);
|
159 |
|
|
|
160 |
|
|
result(3)(0) := aes_message(13 * 8 - 1 DOWNTO 12 * 8) XOR aes_key(13 * 8 - 1 DOWNTO 12 * 8);
|
161 |
|
|
result(3)(1) := aes_message(9 * 8 - 1 DOWNTO 8 * 8) XOR aes_key(9 * 8 - 1 DOWNTO 8 * 8);
|
162 |
|
|
result(3)(2) := aes_message(5 * 8 - 1 DOWNTO 4 * 8) XOR aes_key(5 * 8 - 1 DOWNTO 4 * 8);
|
163 |
|
|
result(3)(3) := aes_message(1 * 8 - 1 DOWNTO 0 * 8) XOR aes_key(1 * 8 - 1 DOWNTO 0 * 8);
|
164 |
|
|
RETURN result;
|
165 |
|
|
END;
|
166 |
|
|
|
167 |
|
|
FUNCTION encryptMessage(aes_message : IN array3D8; aes_key : IN array3D8) RETURN array3D8 IS
|
168 |
|
|
VARIABLE result : array3D8;
|
169 |
|
|
BEGIN
|
170 |
|
|
result(0)(0) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(0)(0)))), sBox(conv_integer(unsigned(aes_message(1)(1)))), sBox(conv_integer(unsigned(aes_message(2)(2)))), sBox(conv_integer(unsigned(aes_message(3)(3))))) XOR aes_key(0)(0);
|
171 |
|
|
result(1)(0) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(1)(1)))), sBox(conv_integer(unsigned(aes_message(2)(2)))), sBox(conv_integer(unsigned(aes_message(3)(3)))), sBox(conv_integer(unsigned(aes_message(0)(0))))) XOR aes_key(1)(0);
|
172 |
|
|
result(2)(0) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(2)(2)))), sBox(conv_integer(unsigned(aes_message(3)(3)))), sBox(conv_integer(unsigned(aes_message(0)(0)))), sBox(conv_integer(unsigned(aes_message(1)(1))))) XOR aes_key(2)(0);
|
173 |
|
|
result(3)(0) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(3)(3)))), sBox(conv_integer(unsigned(aes_message(0)(0)))), sBox(conv_integer(unsigned(aes_message(1)(1)))), sBox(conv_integer(unsigned(aes_message(2)(2))))) XOR aes_key(3)(0);
|
174 |
|
|
|
175 |
|
|
result(0)(1) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(0)(1)))), sBox(conv_integer(unsigned(aes_message(1)(2)))), sBox(conv_integer(unsigned(aes_message(2)(3)))), sBox(conv_integer(unsigned(aes_message(3)(0))))) XOR aes_key(0)(1);
|
176 |
|
|
result(1)(1) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(1)(2)))), sBox(conv_integer(unsigned(aes_message(2)(3)))), sBox(conv_integer(unsigned(aes_message(3)(0)))), sBox(conv_integer(unsigned(aes_message(0)(1))))) XOR aes_key(1)(1);
|
177 |
|
|
result(2)(1) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(2)(3)))), sBox(conv_integer(unsigned(aes_message(3)(0)))), sBox(conv_integer(unsigned(aes_message(0)(1)))), sBox(conv_integer(unsigned(aes_message(1)(2))))) XOR aes_key(2)(1);
|
178 |
|
|
result(3)(1) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(3)(0)))), sBox(conv_integer(unsigned(aes_message(0)(1)))), sBox(conv_integer(unsigned(aes_message(1)(2)))), sBox(conv_integer(unsigned(aes_message(2)(3))))) XOR aes_key(3)(1);
|
179 |
|
|
|
180 |
|
|
result(0)(2) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(0)(2)))), sBox(conv_integer(unsigned(aes_message(1)(3)))), sBox(conv_integer(unsigned(aes_message(2)(0)))), sBox(conv_integer(unsigned(aes_message(3)(1))))) XOR aes_key(0)(2);
|
181 |
|
|
result(1)(2) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(1)(3)))), sBox(conv_integer(unsigned(aes_message(2)(0)))), sBox(conv_integer(unsigned(aes_message(3)(1)))), sBox(conv_integer(unsigned(aes_message(0)(2))))) XOR aes_key(1)(2);
|
182 |
|
|
result(2)(2) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(2)(0)))), sBox(conv_integer(unsigned(aes_message(3)(1)))), sBox(conv_integer(unsigned(aes_message(0)(2)))), sBox(conv_integer(unsigned(aes_message(1)(3))))) XOR aes_key(2)(2);
|
183 |
|
|
result(3)(2) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(3)(1)))), sBox(conv_integer(unsigned(aes_message(0)(2)))), sBox(conv_integer(unsigned(aes_message(1)(3)))), sBox(conv_integer(unsigned(aes_message(2)(0))))) XOR aes_key(3)(2);
|
184 |
|
|
|
185 |
|
|
result(0)(3) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(0)(3)))), sBox(conv_integer(unsigned(aes_message(1)(0)))), sBox(conv_integer(unsigned(aes_message(2)(1)))), sBox(conv_integer(unsigned(aes_message(3)(2))))) XOR aes_key(0)(3);
|
186 |
|
|
result(1)(3) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(1)(0)))), sBox(conv_integer(unsigned(aes_message(2)(1)))), sBox(conv_integer(unsigned(aes_message(3)(2)))), sBox(conv_integer(unsigned(aes_message(0)(3))))) XOR aes_key(1)(3);
|
187 |
|
|
result(2)(3) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(2)(1)))), sBox(conv_integer(unsigned(aes_message(3)(2)))), sBox(conv_integer(unsigned(aes_message(0)(3)))), sBox(conv_integer(unsigned(aes_message(1)(0))))) XOR aes_key(2)(3);
|
188 |
|
|
result(3)(3) := mixcolumnOneByte(sBox(conv_integer(unsigned(aes_message(3)(2)))), sBox(conv_integer(unsigned(aes_message(0)(3)))), sBox(conv_integer(unsigned(aes_message(1)(0)))), sBox(conv_integer(unsigned(aes_message(2)(1))))) XOR aes_key(3)(3);
|
189 |
|
|
RETURN result;
|
190 |
|
|
END;
|
191 |
|
|
-- as the order of i1, i2, i3, i4 change, the return value change
|
192 |
|
|
FUNCTION mixcolumnOneByte(i1, i2, i3, i4 : IN STD_LOGIC_VECTOR (7 DOWNTO 0)) RETURN STD_LOGIC_VECTOR IS
|
193 |
|
|
VARIABLE data_out : STD_LOGIC_VECTOR(7 DOWNTO 0);
|
194 |
|
|
BEGIN
|
195 |
|
|
data_out(7) := i1(6) XOR i2(6) XOR i2(7) XOR i3(7) XOR i4(7);
|
196 |
|
|
data_out(6) := i1(5) XOR i2(5) XOR i2(6) XOR i3(6) XOR i4(6);
|
197 |
|
|
data_out(5) := i1(4) XOR i2(4) XOR i2(5) XOR i3(5) XOR i4(5);
|
198 |
|
|
data_out(4) := i1(3) XOR i1(7) XOR i2(3) XOR i2(4) XOR i2(7) XOR i3(4) XOR i4(4);
|
199 |
|
|
data_out(3) := i1(2) XOR i1(7) XOR i2(2) XOR i2(3) XOR i2(7) XOR i3(3) XOR i4(3);
|
200 |
|
|
data_out(2) := i1(1) XOR i2(1) XOR i2(2) XOR i3(2) XOR i4(2);
|
201 |
|
|
data_out(1) := i1(0) XOR i1(7) XOR i2(0) XOR i2(1) XOR i2(7) XOR i3(1) XOR i4(1);
|
202 |
|
|
data_out(0) := i1(7) XOR i2(7) XOR i2(0) XOR i3(0) XOR i4(0);
|
203 |
|
|
RETURN data_out;
|
204 |
|
|
END;
|
205 |
|
|
|
206 |
|
|
FUNCTION encyrptFinal(aes_message : IN array3D8; aes_key : IN array3D8) RETURN STD_LOGIC_VECTOR IS
|
207 |
|
|
VARIABLE result : STD_LOGIC_VECTOR(127 DOWNTO 0);
|
208 |
|
|
BEGIN
|
209 |
|
|
result(16 * 8 - 1 DOWNTO 15 * 8) := sBox(conv_integer(unsigned(aes_message(0)(0)))) XOR aes_key(0)(0);
|
210 |
|
|
result(12 * 8 - 1 DOWNTO 11 * 8) := sBox(conv_integer(unsigned(aes_message(0)(1)))) XOR aes_key(0)(1);
|
211 |
|
|
result(8 * 8 - 1 DOWNTO 7 * 8) := sBox(conv_integer(unsigned(aes_message(0)(2)))) XOR aes_key(0)(2);
|
212 |
|
|
result(4 * 8 - 1 DOWNTO 3 * 8) := sBox(conv_integer(unsigned(aes_message(0)(3)))) XOR aes_key(0)(3);
|
213 |
|
|
|
214 |
|
|
result(15 * 8 - 1 DOWNTO 14 * 8) := sBox(conv_integer(unsigned(aes_message(1)(1)))) XOR aes_key(1)(0);
|
215 |
|
|
result(11 * 8 - 1 DOWNTO 10 * 8) := sBox(conv_integer(unsigned(aes_message(1)(2)))) XOR aes_key(1)(1);
|
216 |
|
|
result(7 * 8 - 1 DOWNTO 6 * 8) := sBox(conv_integer(unsigned(aes_message(1)(3)))) XOR aes_key(1)(2);
|
217 |
|
|
result(3 * 8 - 1 DOWNTO 2 * 8) := sBox(conv_integer(unsigned(aes_message(1)(0)))) XOR aes_key(1)(3);
|
218 |
|
|
|
219 |
|
|
result(14 * 8 - 1 DOWNTO 13 * 8) := sBox(conv_integer(unsigned(aes_message(2)(2)))) XOR aes_key(2)(0);
|
220 |
|
|
result(10 * 8 - 1 DOWNTO 9 * 8) := sBox(conv_integer(unsigned(aes_message(2)(3)))) XOR aes_key(2)(1);
|
221 |
|
|
result(6 * 8 - 1 DOWNTO 5 * 8) := sBox(conv_integer(unsigned(aes_message(2)(0)))) XOR aes_key(2)(2);
|
222 |
|
|
result(2 * 8 - 1 DOWNTO 1 * 8) := sBox(conv_integer(unsigned(aes_message(2)(1)))) XOR aes_key(2)(3);
|
223 |
|
|
|
224 |
|
|
result(13 * 8 - 1 DOWNTO 12 * 8) := sBox(conv_integer(unsigned(aes_message(3)(3)))) XOR aes_key(3)(0);
|
225 |
|
|
result(9 * 8 - 1 DOWNTO 8 * 8) := sBox(conv_integer(unsigned(aes_message(3)(0)))) XOR aes_key(3)(1);
|
226 |
|
|
result(5 * 8 - 1 DOWNTO 4 * 8) := sBox(conv_integer(unsigned(aes_message(3)(1)))) XOR aes_key(3)(2);
|
227 |
|
|
result(1 * 8 - 1 DOWNTO 0 * 8) := sBox(conv_integer(unsigned(aes_message(3)(2)))) XOR aes_key(3)(3);
|
228 |
|
|
RETURN result;
|
229 |
|
|
END;
|
230 |
|
|
END PACKAGE BODY;
|