From 46f1b9295b5e74902a78540aa77d0673a6d4dcbd Mon Sep 17 00:00:00 2001 From: tmeissner Date: Fri, 29 May 2015 19:14:16 +0200 Subject: [PATCH] merge last changes from amc mini repo --- aes/rtl/vhdl/aes_pkg.vhd | 42 +++++------ aes/sim/vhdl/tb_aes.vhd | 108 ++++++++++++++++++++++++++++- cbcmac_des/rtl/vhdl/cbcmac_des.vhd | 26 +++---- des/rtl/verilog/des.v | 2 +- 4 files changed, 143 insertions(+), 35 deletions(-) diff --git a/aes/rtl/vhdl/aes_pkg.vhd b/aes/rtl/vhdl/aes_pkg.vhd index ed4baa6..72eb140 100644 --- a/aes/rtl/vhdl/aes_pkg.vhd +++ b/aes/rtl/vhdl/aes_pkg.vhd @@ -52,24 +52,24 @@ package aes_pkg is (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"), -- D (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"), -- E (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")); -- F - + constant c_sbox_invers : t_stable2d := ( -- 0 1 2 3 4 5 6 7 8 9 A B C D E F (x"52", x"09", x"6a", x"d5", x"30", x"36", x"a5", x"38", x"bf", x"40", x"a3", x"9e", x"81", x"f3", x"d7", x"fb"), -- 0 - (x"7c", x"e3", x"39", x"82", x"9b", x"2f", x"ff", x"87", x"34", x"8e", x"43", x"44", x"c4", x"de", x"e9", x"cb"), -- 1 - (x"54", x"7b", x"94", x"32", x"a6", x"c2", x"23", x"3d", x"ee", x"4c", x"95", x"0b", x"42", x"fa", x"c3", x"4e"), -- 2 - (x"08", x"2e", x"a1", x"66", x"28", x"d9", x"24", x"b2", x"76", x"5b", x"a2", x"49", x"6d", x"8b", x"d1", x"25"), -- 3 - (x"72", x"f8", x"f6", x"64", x"86", x"68", x"98", x"16", x"d4", x"a4", x"5c", x"cc", x"5d", x"65", x"b6", x"92"), -- 4 - (x"6c", x"70", x"48", x"50", x"fd", x"ed", x"b9", x"da", x"5e", x"15", x"46", x"57", x"a7", x"8d", x"9d", x"84"), -- 5 - (x"90", x"d8", x"ab", x"00", x"8c", x"bc", x"d3", x"0a", x"f7", x"e4", x"58", x"05", x"b8", x"b3", x"45", x"06"), -- 6 - (x"d0", x"2c", x"1e", x"8f", x"ca", x"3f", x"0f", x"02", x"c1", x"af", x"bd", x"03", x"01", x"13", x"8a", x"6b"), -- 7 - (x"3a", x"91", x"11", x"41", x"4f", x"67", x"dc", x"ea", x"97", x"f2", x"cf", x"ce", x"f0", x"b4", x"e6", x"73"), -- 8 - (x"96", x"ac", x"74", x"22", x"e7", x"ad", x"35", x"85", x"e2", x"f9", x"37", x"e8", x"1c", x"75", x"df", x"6e"), -- 9 - (x"47", x"f1", x"1a", x"71", x"1d", x"29", x"c5", x"89", x"6f", x"b7", x"62", x"0e", x"aa", x"18", x"be", x"1b"), -- A - (x"fc", x"56", x"3e", x"4b", x"c6", x"d2", x"79", x"20", x"9a", x"db", x"c0", x"fe", x"78", x"cd", x"5a", x"f4"), -- B - (x"1f", x"dd", x"a8", x"33", x"88", x"07", x"c7", x"31", x"b1", x"12", x"10", x"59", x"27", x"80", x"ec", x"5f"), -- C - (x"60", x"51", x"7f", x"a9", x"19", x"b5", x"4a", x"0d", x"2d", x"e5", x"7a", x"9f", x"93", x"c9", x"9c", x"ef"), -- D - (x"a0", x"e0", x"3b", x"4d", x"ae", x"2a", x"f5", x"b0", x"c8", x"eb", x"bb", x"3c", x"83", x"53", x"99", x"61"), -- E + (x"7c", x"e3", x"39", x"82", x"9b", x"2f", x"ff", x"87", x"34", x"8e", x"43", x"44", x"c4", x"de", x"e9", x"cb"), -- 1 + (x"54", x"7b", x"94", x"32", x"a6", x"c2", x"23", x"3d", x"ee", x"4c", x"95", x"0b", x"42", x"fa", x"c3", x"4e"), -- 2 + (x"08", x"2e", x"a1", x"66", x"28", x"d9", x"24", x"b2", x"76", x"5b", x"a2", x"49", x"6d", x"8b", x"d1", x"25"), -- 3 + (x"72", x"f8", x"f6", x"64", x"86", x"68", x"98", x"16", x"d4", x"a4", x"5c", x"cc", x"5d", x"65", x"b6", x"92"), -- 4 + (x"6c", x"70", x"48", x"50", x"fd", x"ed", x"b9", x"da", x"5e", x"15", x"46", x"57", x"a7", x"8d", x"9d", x"84"), -- 5 + (x"90", x"d8", x"ab", x"00", x"8c", x"bc", x"d3", x"0a", x"f7", x"e4", x"58", x"05", x"b8", x"b3", x"45", x"06"), -- 6 + (x"d0", x"2c", x"1e", x"8f", x"ca", x"3f", x"0f", x"02", x"c1", x"af", x"bd", x"03", x"01", x"13", x"8a", x"6b"), -- 7 + (x"3a", x"91", x"11", x"41", x"4f", x"67", x"dc", x"ea", x"97", x"f2", x"cf", x"ce", x"f0", x"b4", x"e6", x"73"), -- 8 + (x"96", x"ac", x"74", x"22", x"e7", x"ad", x"35", x"85", x"e2", x"f9", x"37", x"e8", x"1c", x"75", x"df", x"6e"), -- 9 + (x"47", x"f1", x"1a", x"71", x"1d", x"29", x"c5", x"89", x"6f", x"b7", x"62", x"0e", x"aa", x"18", x"be", x"1b"), -- A + (x"fc", x"56", x"3e", x"4b", x"c6", x"d2", x"79", x"20", x"9a", x"db", x"c0", x"fe", x"78", x"cd", x"5a", x"f4"), -- B + (x"1f", x"dd", x"a8", x"33", x"88", x"07", x"c7", x"31", x"b1", x"12", x"10", x"59", x"27", x"80", x"ec", x"5f"), -- C + (x"60", x"51", x"7f", x"a9", x"19", x"b5", x"4a", x"0d", x"2d", x"e5", x"7a", x"9f", x"93", x"c9", x"9c", x"ef"), -- D + (x"a0", x"e0", x"3b", x"4d", x"ae", x"2a", x"f5", x"b0", x"c8", x"eb", x"bb", x"3c", x"83", x"53", x"99", x"61"), -- E (x"17", x"2b", x"04", x"7e", x"ba", x"77", x"d6", x"26", x"e1", x"69", x"14", x"63", x"55", x"21", x"0c", x"7d"));-- F @@ -78,14 +78,14 @@ package aes_pkg is function shiftrow (input : t_datatable2d) return t_datatable2d; function invshiftrow (input : t_datatable2d) return t_datatable2d; - + function mixcolumns (input : t_datatable2d; column : natural) return t_datatable2d; function sortdata (input : std_logic_vector(127 downto 0)) return t_datatable2d; function gmul (a : std_logic_vector(7 downto 0); b : std_logic_vector(7 downto 0)) return std_logic_vector; - function addroundkey (data : in std_logic_vector(127 downto 0), key ) + --function addroundkey (data : in std_logic_vector(127 downto 0), key ) end package aes_pkg; @@ -186,10 +186,10 @@ package body aes_pkg is variable v_data : t_datatable2d; begin for index in 0 to 3 loop - v_data(index)(0) := gmul(x"02",input(index)(0)) xor gmul(x"03",input(index)(1)) xor input(index)(2) xor input(index)(3); - v_data(index)(1) := input(index)(0) xor gmul(x"02",input(index)(1)) xor gmul(x"03",input(index)(2)) xor input(index)(3); - v_data(index)(2) := input(index)(0) xor input(index)(1) xor gmul(x"02",input(index)(2)) xor gmul(x"03",input(index)(3)); - v_data(index)(3) := gmul(x"03", input(index)(0)) xor input(index)(1) xor input(index)(2) xor gmul(x"02",input(index)(3)); + v_data(index)(0) := gmul(x"02",input(index)(0)) xor gmul(x"03",input(index)(1)) xor input(index)(2) xor input(index)(3); + v_data(index)(1) := input(index)(0) xor gmul(x"02",input(index)(1)) xor gmul(x"03",input(index)(2)) xor input(index)(3); + v_data(index)(2) := input(index)(0) xor input(index)(1) xor gmul(x"02",input(index)(2)) xor gmul(x"03",input(index)(3)); + v_data(index)(3) := gmul(x"03", input(index)(0)) xor input(index)(1) xor input(index)(2) xor gmul(x"02",input(index)(3)); end loop; return v_data; end function mixcolumns; diff --git a/aes/sim/vhdl/tb_aes.vhd b/aes/sim/vhdl/tb_aes.vhd index 1c74e09..f00ad96 100644 --- a/aes/sim/vhdl/tb_aes.vhd +++ b/aes/sim/vhdl/tb_aes.vhd @@ -24,6 +24,8 @@ library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; +use work.aes_pkg.all; + entity tb_aes is @@ -57,6 +59,93 @@ architecture rtl of tb_aes is ); end component aes; + type t_byte_array is array (natural range <>) of std_logic_vector(7 downto 0); + + constant C_LTABLE : t_byte_array := ( + x"00", x"ff", x"c8", x"08", x"91", x"10", x"d0", x"36", + x"5a", x"3e", x"d8", x"43", x"99", x"77", x"fe", x"18", + x"23", x"20", x"07", x"70", x"a1", x"6c", x"0c", x"7f", + x"62", x"8b", x"40", x"46", x"c7", x"4b", x"e0", x"0e", + x"eb", x"16", x"e8", x"ad", x"cf", x"cd", x"39", x"53", + x"6a", x"27", x"35", x"93", x"d4", x"4e", x"48", x"c3", + x"2b", x"79", x"54", x"28", x"09", x"78", x"0f", x"21", + x"90", x"87", x"14", x"2a", x"a9", x"9c", x"d6", x"74", + x"b4", x"7c", x"de", x"ed", x"b1", x"86", x"76", x"a4", + x"98", x"e2", x"96", x"8f", x"02", x"32", x"1c", x"c1", + x"33", x"ee", x"ef", x"81", x"fd", x"30", x"5c", x"13", + x"9d", x"29", x"17", x"c4", x"11", x"44", x"8c", x"80", + x"f3", x"73", x"42", x"1e", x"1d", x"b5", x"f0", x"12", + x"d1", x"5b", x"41", x"a2", x"d7", x"2c", x"e9", x"d5", + x"59", x"cb", x"50", x"a8", x"dc", x"fc", x"f2", x"56", + x"72", x"a6", x"65", x"2f", x"9f", x"9b", x"3d", x"ba", + x"7d", x"c2", x"45", x"82", x"a7", x"57", x"b6", x"a3", + x"7a", x"75", x"4f", x"ae", x"3f", x"37", x"6d", x"47", + x"61", x"be", x"ab", x"d3", x"5f", x"b0", x"58", x"af", + x"ca", x"5e", x"fa", x"85", x"e4", x"4d", x"8a", x"05", + x"fb", x"60", x"b7", x"7b", x"b8", x"26", x"4a", x"67", + x"c6", x"1a", x"f8", x"69", x"25", x"b3", x"db", x"bd", + x"66", x"dd", x"f1", x"d2", x"df", x"03", x"8d", x"34", + x"d9", x"92", x"0d", x"63", x"55", x"aa", x"49", x"ec", + x"bc", x"95", x"3c", x"84", x"0b", x"f5", x"e6", x"e7", + x"e5", x"ac", x"7e", x"6e", x"b9", x"f9", x"da", x"8e", + x"9a", x"c9", x"24", x"e1", x"0a", x"15", x"6b", x"3a", + x"a0", x"51", x"f4", x"ea", x"b2", x"97", x"9e", x"5d", + x"22", x"88", x"94", x"ce", x"19", x"01", x"71", x"4c", + x"a5", x"e3", x"c5", x"31", x"bb", x"cc", x"1f", x"2d", + x"3b", x"52", x"6f", x"f6", x"2e", x"89", x"f7", x"c0", + x"68", x"1b", x"64", x"04", x"06", x"bf", x"83", x"38"); + + constant C_ATABLE : t_byte_array := ( + x"01", x"e5", x"4c", x"b5", x"fb", x"9f", x"fc", x"12", + x"03", x"34", x"d4", x"c4", x"16", x"ba", x"1f", x"36", + x"05", x"5c", x"67", x"57", x"3a", x"d5", x"21", x"5a", + x"0f", x"e4", x"a9", x"f9", x"4e", x"64", x"63", x"ee", + x"11", x"37", x"e0", x"10", x"d2", x"ac", x"a5", x"29", + x"33", x"59", x"3b", x"30", x"6d", x"ef", x"f4", x"7b", + x"55", x"eb", x"4d", x"50", x"b7", x"2a", x"07", x"8d", + x"ff", x"26", x"d7", x"f0", x"c2", x"7e", x"09", x"8c", + x"1a", x"6a", x"62", x"0b", x"5d", x"82", x"1b", x"8f", + x"2e", x"be", x"a6", x"1d", x"e7", x"9d", x"2d", x"8a", + x"72", x"d9", x"f1", x"27", x"32", x"bc", x"77", x"85", + x"96", x"70", x"08", x"69", x"56", x"df", x"99", x"94", + x"a1", x"90", x"18", x"bb", x"fa", x"7a", x"b0", x"a7", + x"f8", x"ab", x"28", x"d6", x"15", x"8e", x"cb", x"f2", + x"13", x"e6", x"78", x"61", x"3f", x"89", x"46", x"0d", + x"35", x"31", x"88", x"a3", x"41", x"80", x"ca", x"17", + x"5f", x"53", x"83", x"fe", x"c3", x"9b", x"45", x"39", + x"e1", x"f5", x"9e", x"19", x"5e", x"b6", x"cf", x"4b", + x"38", x"04", x"b9", x"2b", x"e2", x"c1", x"4a", x"dd", + x"48", x"0c", x"d0", x"7d", x"3d", x"58", x"de", x"7c", + x"d8", x"14", x"6b", x"87", x"47", x"e8", x"79", x"84", + x"73", x"3c", x"bd", x"92", x"c9", x"23", x"8b", x"97", + x"95", x"44", x"dc", x"ad", x"40", x"65", x"86", x"a2", + x"a4", x"cc", x"7f", x"ec", x"c0", x"af", x"91", x"fd", + x"f7", x"4f", x"81", x"2f", x"5b", x"ea", x"a8", x"1c", + x"02", x"d1", x"98", x"71", x"ed", x"25", x"e3", x"24", + x"06", x"68", x"b3", x"93", x"2c", x"6f", x"3e", x"6c", + x"0a", x"b8", x"ce", x"ae", x"74", x"b1", x"42", x"b4", + x"1e", x"d3", x"49", x"e9", x"9c", x"c8", x"c6", x"c7", + x"22", x"6e", x"db", x"20", x"bf", x"43", x"51", x"52", + x"66", x"b2", x"76", x"60", x"da", x"c5", x"f3", x"f6", + x"aa", x"cd", x"9a", x"a0", x"75", x"54", x"0e", x"01"); + + + function gmul_a(a : std_logic_vector(7 downto 0); b : std_logic_vector(7 downto 0)) return std_logic_vector is + variable v_s : unsigned(7 downto 0) := x"00"; + begin + v_s := unsigned(C_LTABLE(to_integer(unsigned(a)))) + unsigned(C_LTABLE(to_integer(unsigned(b)))); + v_s := unsigned(C_ATABLE(to_integer(v_s))); + if (a = x"00" or b = x"00") then + return x"00"; + else + return std_logic_vector(v_s); + end if; + end function gmul_a; + + signal s_a : std_logic_vector(7 downto 0); + signal s_b : std_logic_vector(7 downto 0); + signal s_i : std_logic_vector(7 downto 0); + signal s_j : std_logic_vector(7 downto 0); begin @@ -72,10 +161,27 @@ begin mode_i => s_mode, key_i => s_key, data_i => s_datain, - valid_i => s_validin, + valid_i => s_validin, data_o => s_dataout, valid_o => s_validout ); + -- check gmul function + gmulcheckP : process is + variable v_a : std_logic_vector(7 downto 0) := x"00"; + variable v_b : std_logic_vector(7 downto 0) := x"00"; + begin + for i in 0 to 255 loop + for j in 0 to 255 loop + s_i <= std_logic_vector(to_unsigned(i, 8)); + s_j <= std_logic_vector(to_unsigned(j, 8)); + wait until rising_edge(s_clk); + s_a <= gmul(s_i, s_j); + s_b <= gmul_a(s_i, s_j); + end loop; + end loop; + end process gmulcheckP; + + end architecture rtl; diff --git a/cbcmac_des/rtl/vhdl/cbcmac_des.vhd b/cbcmac_des/rtl/vhdl/cbcmac_des.vhd index 601ab87..06def84 100644 --- a/cbcmac_des/rtl/vhdl/cbcmac_des.vhd +++ b/cbcmac_des/rtl/vhdl/cbcmac_des.vhd @@ -24,6 +24,7 @@ library ieee; use work.des_pkg.all; + entity cbcmac_des is port ( reset_i : in std_logic; -- low active async reset @@ -32,14 +33,15 @@ entity cbcmac_des is key_i : in std_logic_vector(0 to 63); -- key input data_i : in std_logic_vector(0 to 63); -- data input valid_i : in std_logic; -- input key/data valid flag - accept_o : out std_logic; + accept_o : out std_logic; -- input accept data_o : out std_logic_vector(0 tO 63); -- data output valid_o : out std_logic; -- output data valid flag - accept_i : in std_logic + accept_i : in std_logic -- output accept ); end entity cbcmac_des; + architecture rtl of cbcmac_des is @@ -49,11 +51,11 @@ architecture rtl of cbcmac_des is ); port ( reset_i : in std_logic; - clk_i : IN std_logic; - mode_i : IN std_logic; - key_i : IN std_logic_vector(0 to 63); - data_i : IN std_logic_vector(0 to 63); - valid_i : IN std_logic; + clk_i : in std_logic; + mode_i : in std_logic; + key_i : in std_logic_vector(0 to 63); + data_i : in std_logic_vector(0 to 63); + valid_i : in std_logic; accept_o : out std_logic; data_o : out std_logic_vector(0 to 63); valid_o : out std_logic; @@ -76,12 +78,12 @@ architecture rtl of cbcmac_des is begin - s_des_datain <= C_IV xor data_i when start_i = '1' else - s_des_dataout_d xor data_i when start_i = '0'; + s_des_datain <= C_IV xor data_i when start_i = '1' else + s_des_dataout_d xor data_i; - data_o <= s_des_dataout; + data_o <= s_des_dataout; - s_des_key <= key_i when start_i = '1' else s_key; + s_des_key <= key_i when start_i = '1' else s_key; accept_o <= s_des_accept; @@ -130,4 +132,4 @@ begin ); -end architecture rtl; \ No newline at end of file +end architecture rtl; diff --git a/des/rtl/verilog/des.v b/des/rtl/verilog/des.v index b7bf77c..50df7fb 100644 --- a/des/rtl/verilog/des.v +++ b/des/rtl/verilog/des.v @@ -37,7 +37,7 @@ module des ); -`include "../../rtl/verilog/des_pkg.v" +`include "../../../des/rtl/verilog/des_pkg.v" `ifdef PIPE