⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rijndaelimplemetation.vhdl

📁 rijndael算法的一个vhdl语言编写的程序,可供学习者参考交流
💻 VHDL
📖 第 1 页 / 共 4 页
字号:
-- pragma map_to_operator SBOX_LOOKUP_dw_op-- pragma return_port_name SBOX_LOOKUP_outbegin   return std_logic_vector(TO_UNSIGNED                          (SBOX(TO_INTEGER(unsigned(a(7 downto 0)))), 8));end SBOX_LOOKUP;-- ==========================================================================----  function SBOX32_FUNCT----  Performs the sbox function implemented as a lookup table. There--  are 4 copies of the 8-bit sbox to cover 32 bits of input/output.---- ==========================================================================function SBOX32_FUNCT ( w : SLV_32 )                        return SLV_32 is-- pragma map_to_operator SBOX32_FUNCT_dw_op-- pragma return_port_name SBOX32_FUNCT_outbegin   return std_logic_vector(               TO_UNSIGNED(SBOX(TO_INTEGER(unsigned(w(31 downto 24)))), 8) &               TO_UNSIGNED(SBOX(TO_INTEGER(unsigned(w(23 downto 16)))), 8) &               TO_UNSIGNED(SBOX(TO_INTEGER(unsigned(w(15 downto  8)))), 8) &               TO_UNSIGNED(SBOX(TO_INTEGER(unsigned(w( 7 downto  0)))), 8)   );end SBOX32_FUNCT;-- ==========================================================================----  function INV_SBOX_LOOKUP----  Performs the inverse sbox function implemented as a lookup table.--  There are 4 copies of the 8-bit sbox to cover 32 bits of input/output.---- ==========================================================================function INV_SBOX_LOOKUP ( a : SLV_8 )                           return SLV_8 is-- pragma map_to_operator INV_SBOX_LOOKUP_dw_op-- pragma return_port_name INV_SBOX_LOOKUP_outbegin   return std_logic_vector(TO_UNSIGNED                          (InvSBOX(TO_INTEGER(unsigned(a(7 downto 0)))), 8));end INV_SBOX_LOOKUP;-- ==========================================================================----  function BYTE_SUB_FUNCT----  Performs the byte sub function implemented as combinational logic --  as described in the RIJNDAEL algorithm specification.---- ==========================================================================function BYTE_SUB_FUNCT ( state : STATE_TYPE )                          return STATE_TYPE is-- pragma map_to_operator BYTE_SUB_FUNCT_dw_op-- pragma return_port_name BYTE_SUB_FUNCT_outvariable row    : integer range 0 to 3;variable column : integer range 0 to 3;variable b      : STATE_TYPE;begin   for row in 0 to 3 loop      for column in 0 to 3 loop         b(row)(column) := SBOX_LOOKUP( state(row)(column) );      end loop;   end loop;   return b;end BYTE_SUB_FUNCT;-- ==========================================================================----  function INV_BYTE_SUB_FUNCT----  Performs the inverse byte sub function implemented as combinational --  logic as described in the RIJNDAEL algorithm specification.---- ==========================================================================function INV_BYTE_SUB_FUNCT ( state : STATE_TYPE )                              return STATE_TYPE is-- pragma map_to_operator INV_BYTE_SUB_FUNCT_dw_op-- pragma return_port_name INV_BYTE_SUB_FUNCT_outvariable row    : integer range 0 to 3;variable column : integer range 0 to 3;variable b      : STATE_TYPE;begin   for row in 0 to 3 loop      for column in 0 to 3 loop         b(row)(column) := INV_SBOX_LOOKUP( state(row)(column) );      end loop;   end loop;   return b;end INV_BYTE_SUB_FUNCT;-- ==========================================================================----  function SHIFT_ROW_FUNCT----  Performs the row shift function implemented as combinational logic --  as described in the RIJNDAEL algorithm specification.---- ==========================================================================function SHIFT_ROW_FUNCT ( state : STATE_TYPE )                            return STATE_TYPE is-- pragma map_to_operator SHIFT_ROW_FUNCT_dw_op-- pragma return_port_name SHIFT_ROW_FUNCT_outvariable a      : STATE_TYPE;variable row    : integer range 0 to 3;variable column : integer range 0 to 3;variable temp   : TEMP_TYPE;begin   a := state;        -- This variable added to stay consistent with C Code   for row in 1 to 3 loop      for column in 0 to 3 loop         temp(column) := a(row) ((column + shifts(row)(0)) mod 4);      end loop;      for column in 0 to 3 loop         a(row) (column) := temp(column);      end loop;   end loop;   return a;end SHIFT_ROW_FUNCT;-- ==========================================================================----  function INV_SHIFT_ROW_FUNCT----  Performs the inverse row shift function implemented as combinational --  logic as described in the RIJNDAEL algorithm specification.---- ==========================================================================function INV_SHIFT_ROW_FUNCT ( state : STATE_TYPE )                               return STATE_TYPE is-- pragma map_to_operator INV_SHIFT_ROW_FUNCT_dw_op-- pragma return_port_name INV_SHIFT_ROW_FUNCT_outvariable a      : STATE_TYPE;variable row    : integer range 0 to 3;variable column : integer range 0 to 3;variable temp   : TEMP_TYPE;begin   a := state;        -- This variable added to stay consistent with C Code   for row in 1 to 3 loop      for column in 0 to 3 loop         temp(column) := a(row) ((column + shifts(row)(1)) mod 4);      end loop;      for column in 0 to 3 loop         a(row) (column) := temp(column);      end loop;   end loop;   return a;end INV_SHIFT_ROW_FUNCT;-- ==========================================================================----  function MIX_COLUMN_FUNCT----  Performs the column mixing function implemented as combinational logic --  as described in the RIJNDAEL algorithm specification.---- ==========================================================================function MIX_COLUMN_FUNCT ( state : STATE_TYPE )                            return STATE_TYPE is-- pragma map_to_operator MIX_COLUMN_FUNCT_dw_op-- pragma return_port_name MIX_COLUMN_FUNCT_outvariable row    : integer range 0 to 3;variable column : integer range 0 to 3;variable b      : STATE_TYPE;begin   for row in 0 to 3 loop      for column in 0 to 3 loop         b(row)(column) :=            POLY_MULTE_FUNCT ( "00000010", state(row)(column) ) xor            POLY_MULTE_FUNCT ( "00000011", state((row + 1) mod 4)(column) ) xor            state ((row + 2) mod 4)(column) xor            state ((row + 3) mod 4)(column);      end loop;  -- column   end loop; -- row   return b;end MIX_COLUMN_FUNCT;-- ==========================================================================----  function INV_MIX_COLUMN_FUNCT----  Performs the inverse column mixing function implemented as combinational--  logic as described in the RIJNDAEL algorithm specification.---- ==========================================================================function INV_MIX_COLUMN_FUNCT ( state : STATE_TYPE )                                return STATE_TYPE is-- pragma map_to_operator INV_MIX_COLUMN_FUNCT_dw_op-- pragma return_port_name INV_MIX_COLUMN_FUNCT_outvariable row    : integer range 0 to 3;variable column : integer range 0 to 3;variable b      : STATE_TYPE;begin   for row in 0 to 3 loop      for column in 0 to 3 loop         b(row)(column) :=            POLY_MULTD_FUNCT ( "00001110", state(row)(column) ) xor            POLY_MULTD_FUNCT ( "00001011", state((row + 1) mod 4)(column) ) xor            POLY_MULTD_FUNCT ( "00001101", state((row + 2) mod 4)(column) ) xor            POLY_MULTD_FUNCT ( "00001001", state((row + 3) mod 4)(column) );      end loop;  -- column   end loop; -- row   return b;end INV_MIX_COLUMN_FUNCT;-- ==========================================================================----  function POLY_MULTE_FUNCT----  Performs the polynomial multiply function implemented as combinational--  logic as described in the RIJNDAEL algorithm specification.---- ==========================================================================function POLY_MULTE_FUNCT ( a : SLV_8;                           b : SLV_8 )                           return SLV_8 is-- pragma map_to_operator POLY_MULTE_FUNCT_dw_op-- pragma return_port_name POLY_MULTE_FUNCT_outvariable temp     : SLV_8;variable temp1    : SLV_8;variable temp2    : SLV_8;variable temp3    : SLV_8;variable and_mask : SLV_8;begin   and_mask := b(7) & b(7) & b(7) & b(7) & b(7) & b(7) & b(7) & b(7);   case a(3 downto 0) is      when "0001" =>          temp := b;      when "0010" =>         temp := (b(6 downto 0) & '0') xor (("00011011") and and_mask);      when "0011"=>         temp := (b(6 downto 0) & '0') xor (("00011011") and and_mask) xor b;      when others =>         temp := ( others => '0' );   end case;   return temp; end POLY_MULTE_FUNCT;-- ==========================================================================----  function POLY_MULTD_FUNCT----  Performs the polynomial multiply function implemented as combinational--  logic as described in the RIJNDAEL algorithm specification.---- ==========================================================================function POLY_MULTD_FUNCT ( a : SLV_8;                           b : SLV_8 )                           return SLV_8 is-- pragma map_to_operator POLY_MULTD_FUNCT_dw_op-- pragma return_port_name POLY_MULTD_FUNCT_outvariable temp     : SLV_8;variable temp1    : SLV_8;variable temp2    : SLV_8;variable temp3    : SLV_8;variable and_mask : SLV_8;begin   and_mask := b(7) & b(7) & b(7) & b(7) & b(7) & b(7) & b(7) & b(7);   case a(3 downto 0) is      when "1001"=>         temp1    := (b(6 downto 0) & '0') xor (("00011011") and and_mask);         and_mask := temp1(7) & temp1(7) & temp1(7) & temp1(7) &                      temp1(7) & temp1(7) & temp1(7) & temp1(7);         temp2    := (temp1(6 downto 0) & '0') xor (("00011011") and and_mask);         and_mask := temp2(7) & temp2(7) & temp2(7) & temp2(7) &                      temp2(7) & temp2(7) & temp2(7) & temp2(7);         temp3    := (temp2(6 downto 0) & '0') xor (("00011011") and and_mask);         temp     := temp3 xor b;      when "1011"=>         temp1    := (b(6 downto 0) & '0') xor (("00011011") and and_mask);         and_mask := temp1(7) & temp1(7) & temp1(7) & temp1(7) &                      temp1(7) & temp1(7) & temp1(7) & temp1(7);         temp2    := (temp1(6 downto 0) & '0') xor (("00011011") and and_mask);         and_mask := temp2(7) & temp2(7) & temp2(7) & temp2(7) &                      temp2(7) & temp2(7) & temp2(7) & temp2(7);         temp3    := (temp2(6 downto 0) & '0') xor (("00011011") and and_mask);         temp     := temp1 xor temp3 xor b;      when "1101"=>         temp1    := (b(6 downto 0) & '0') xor (("00011011") and and_mask);         and_mask := temp1(7) & temp1(7) & temp1(7) & temp1(7) &                      temp1(7) & temp1(7) & temp1(7) & temp1(7);         temp2    := (temp1(6 downto 0) & '0') xor (("00011011") and and_mask);         and_mask := temp2(7) & temp2(7) & temp2(7) & temp2(7) &                      temp2(7) & temp2(7) & temp2(7) & temp2(7);         temp3    := (temp2(6 downto 0) & '0') xor (("00011011") and and_mask);         temp     := temp2 xor temp3 xor b;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -