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

📄 rijndaelimplemetation.vhdl

📁 rijndael算法的一个vhdl语言编写的程序,可供学习者参考交流
💻 VHDL
📖 第 1 页 / 共 4 页
字号:
      when "01" =>         if mod3_table(bank) = 0 then            new_w(-4) := KS_SBOX_FUNCT( cv_size,                                        '1',            std_logic_vector(TO_UNSIGNED(SBOX_INDEX192(bank),16)),                                         w_in(-6),                                         w_in(-1) );            new_w(-3) := new_w(-4) xor w_in(-5);            new_w(-2) := new_w(-3) xor w_in(-4);            new_w(-1) := new_w(-2) xor w_in(-3);         elsif mod3_table(bank) = 1 then            new_w(-4) := w_in(-1) xor w_in(-6);            new_w(-3) := new_w(-4) xor w_in(-5);            new_w(-2) := KS_SBOX_FUNCT( cv_size,                                        '1',            std_logic_vector(TO_UNSIGNED(SBOX_INDEX192(bank),16)),                                         w_in(-4),                                         new_w(-3) );            new_w(-1) := new_w(-2) xor w_in(-3);         else            new_w(-4) := w_in(-1) xor w_in(-6);            new_w(-3) := new_w(-4) xor w_in(-5);            new_w(-2) := new_w(-3) xor w_in(-4);            new_w(-1) := new_w(-2) xor w_in(-3);         end if; -- i mod 6 = 0      when others =>         if bank mod 2 = 0 then            new_w(-4) := KS_SBOX_FUNCT( cv_size,                                        '1',                    std_logic_vector(TO_UNSIGNED( bank/2,16)),                                        w_in(-8),                                        w_in(-1) );         else            new_w(-4) := SBOX32_FUNCT( w_in(-1)) xor                                       w_in(-8);         end if; -- i mod 2 = 0         new_w(-3) := new_w(-4) xor w_in(-7);         new_w(-2) := new_w(-3) xor w_in(-6);         new_w(-1) := new_w(-2) xor w_in(-5);   end case;   for i in -8 to -5 loop      new_w(i) := w_in(i+4);   end loop;   return new_w;end EXPANSION_FUNCT;-- ==========================================================================---- function KS_SBOX_FUNCT---- This function performs the sbox key expansion component of the-- key schedule.---- ==========================================================================function KS_SBOX_FUNCT ( cv_size : SLV_2;                         encrypt : std_logic;                         i       : SLV_16;                         w_far   : SLV_32;                         w_near  : SLV_32 )                         return SLV_32 is-- pragma map_to_operator KS_SBOX_FUNCT_dw_op-- pragma return_port_name KS_SBOX_FUNCT_outvariable temp      : SLV_32;variable byte1     : SLV_8;variable byte2     : SLV_8;variable byte3     : SLV_8;variable byte4     : SLV_8;variable RconValue : integer;begin   byte1 := SBOX_LOOKUP(w_near(31 downto 24));   byte2 := SBOX_LOOKUP(w_near(23 downto 16));   byte3 := SBOX_LOOKUP(w_near(15 downto  8));   byte4 := SBOX_LOOKUP(w_near( 7 downto  0));   temp := byte2 & byte3 & byte4 & byte1;  -- ROTL by a byte   case cv_size is      when "00" =>         if encrypt = '1' then            RconValue := TO_INTEGER(unsigned(i));         elsif TO_INTEGER(unsigned(i)) > 9 then            RconValue := 0;        -- handle unused conditions         else            RconValue := 9-TO_INTEGER(unsigned(i));         end if;      when "01" =>         if encrypt = '1' then            RconValue := TO_INTEGER(unsigned(i));         elsif TO_INTEGER(unsigned(i)) > 7 then            RconValue := 0;        -- handle unused conditions         else            RconValue := 7-TO_INTEGER(unsigned(i));         end if;      when others =>          if encrypt = '1' then            RconValue := TO_INTEGER(unsigned(i));         elsif TO_INTEGER(unsigned(i)) > 6 then            RconValue := 0;        -- handle unused conditions         else            RconValue := 6-TO_INTEGER(unsigned(i));         end if;   end case;   temp := temp xor w_far xor Rcon(RconValue) & X"000000";   return temp;end KS_SBOX_FUNCT;-- ====================================================================-- Procedure KS_SBOX-- -- Wrapper for KS_SBOX_FUNCT-- ====================================================================procedure KS_SBOX( encrypt : std_logic;                   cv_size : SLV_2;                   i       : in  SLV_16;                   w_far   : in  SLV_32;                   w_near  : in  SLV_32;            signal w_box   : out SLV_32 ) isbegin   w_box <= KS_SBOX_FUNCT( cv_size, encrypt, i, w_far, w_near );end KS_SBOX;-- ==========================================================================---- function KS_ROUND_FUNCT---- This function performs the key expansion component of the-- key schedule by calling the s-box routines and implementing the linear-- addition for the W registers.---- ==========================================================================function KS_ROUND_FUNCT ( cv_size : SLV_2;                          encrypt : std_logic;                          i       : SLV_16;                          w       : W_TYPE )                          return W_TYPE is-- pragma map_to_operator KS_ROUND_FUNCT_dw_op-- pragma return_port_name KS_ROUND_FUNCT_outvariable index       : integer;variable w_temp      : W_TYPE;variable sbox_round  : SLV_16;variable sbox_in1    : SLV_32;variable sbox_in2    : SLV_32;variable sbox_result : SLV_32;begin  for index in -8 to -1 loop     w_temp(index) := w(index);  end loop;  if encrypt = '1' then     case cv_size is        when CV128 =>           sbox_round := i;           sbox_in1   := w(-4);           sbox_in2   := w(-1);        when CV192 =>           sbox_round := std_logic_vector(                            TO_UNSIGNED(SBOX_INDEX192(                            TO_INTEGER(unsigned(i))),16));           if mod3_table(TO_INTEGER(unsigned(i))) = 0 then              sbox_in1   := w(-6);              sbox_in2   := w(-1);           else              sbox_in1   := w(-4);              sbox_in2   := w(-1) xor w(-6) xor w(-5);           end if;        when others =>            sbox_round := std_logic_vector(                               TO_UNSIGNED(TO_INTEGER(unsigned(i))/2, 16));            sbox_in1   := w(-8);            sbox_in2   := w(-1);     end case;  else      case cv_size is        when CV128 =>           sbox_round := std_logic_vector(                            TO_UNSIGNED(9-TO_INTEGER(unsigned(i)), 16));           sbox_in1   := w_temp(-1);           sbox_in2   := w_temp(-3) xor w_temp(-4);        when CV192 =>           sbox_round := std_logic_vector(TO_UNSIGNED(                              7 - SBOX_INDEX(TO_INTEGER(unsigned(i))), 16));           if mod3_table(TO_INTEGER(unsigned(i))) = 2 then              sbox_in1   := w(-5);              sbox_in2   := w(-4);           else              sbox_in1   := w(-3);              sbox_in2   := w(-2);           end if;        when others =>            sbox_round := std_logic_vector(                          TO_UNSIGNED(6-TO_INTEGER(unsigned(i))/2, 16));            sbox_in1   := w(-5);            sbox_in2   := w(-4);     end case;  end if;   sbox_result := KS_SBOX_FUNCT( cv_size, '1', sbox_round, sbox_in1, sbox_in2 );  for index in -8 to -1 loop     w_temp(index) := w(index);  end loop;   if encrypt = '1' then      case cv_size is         when CV128 =>            w_temp(-4) := sbox_result;            w_temp(-3) := w_temp(-4) xor w_temp(-3);            w_temp(-2) := w_temp(-3) xor w_temp(-2);            w_temp(-1) := w_temp(-2) xor w_temp(-1);         when CV192 =>            for index in -8 to -5 loop               w_temp(index) := w(index+4);    -- shift previous 4 keys to top            end loop;            if mod3_table(TO_INTEGER(unsigned(i))) = 0 then               w_temp(-4) := sbox_result;               w_temp(-3) := w_temp(-4) xor w(-5);               w_temp(-2) := w(-4) xor w_temp(-3);               w_temp(-1) := w(-3) xor w_temp(-2);            elsif mod3_table(TO_INTEGER(unsigned(i))) = 1 then               w_temp(-4) := w(-1) xor w(-6);               w_temp(-3) := w_temp(-4) xor w(-5);               w_temp(-2) := sbox_result;               w_temp(-1) := w(-3) xor w_temp(-2);            else               w_temp(-4) := w(-1) xor w(-6);               w_temp(-3) := w_temp(-4) xor w(-5);               w_temp(-2) := w(-4) xor w_temp(-3);               w_temp(-1) := w(-3) xor w_temp(-2);            end if;         when others =>            for index in -8 to -5 loop               w_temp(index) := w(index+4);    -- shift previous 4 keys to top            end loop;            if TO_INTEGER(unsigned(i)) mod 2 = 0 then               w_temp(-4) := sbox_result;               w_temp(-3) := w(-7) xor w_temp(-4);               w_temp(-2) := w(-6) xor w_temp(-3);               w_temp(-1) := w(-5) xor w_temp(-2);            else               w_temp(-4) := SBOX32_FUNCT(W(-1)) xor W(-8);               w_temp(-3) := w(-7) xor w_temp(-4);               w_temp(-2) := w(-6) xor w_temp(-3);               w_temp(-1) := w(-5) xor w_temp(-2);            end if;      end case;   else     case cv_size is         when CV128 =>            w_temp(-4) := w_temp(-3) xor w_temp(-4);            w_temp(-3) := w_temp(-2) xor w_temp(-3);            w_temp(-2) := w_temp(-1) xor w_temp(-2);            w_temp(-1) := sbox_result;         when CV192 =>            for index in -8 to -5 loop               w_temp(index) := w(index+4);  -- shift previous 4 keys to top            end loop;            if mod3_table(TO_INTEGER(unsigned(i))) = 1 then               w_temp(-4) := w(-6) xor w(-5);               w_temp(-3) := w(-5) xor w(-4);               w_temp(-2) := w(-4) xor w(-3);               w_temp(-1) := w(-3) xor w(-2);            elsif mod3_table(TO_INTEGER(unsigned(i))) = 2 then               w_temp(-4) := w(-6) xor w(-5);               w_temp(-3) := sbox_result;               w_temp(-2) := w(-4) xor w(-3);               w_temp(-1) := w(-3) xor w(-2);            else               w_temp(-4) := w(-6) xor w(-5);               w_temp(-3) := w(-5) xor w(-4);               w_temp(-2) := w(-4) xor w(-3);               w_temp(-1) := sbox_result;            end if;         when others =>            for index in -8 to -5 loop               w_temp(index) := w(index+4);  -- shift previous 4 keys to top            end loop;            if TO_INTEGER(unsigned(i)) mod 2 = 0 then               w_temp(-4) := w(-8) xor w(-7);               w_temp(-3) := w(-7) xor w(-6);               w_temp(-2) := w(-6) xor w(-5);               w_temp(-1) := sbox_result;            else               w_temp(-4) := w(-8) xor w(-7);               w_temp(-3) := w(-7) xor w(-6);               w_temp(-2) := w(-6) xor w(-5);               w_temp(-1) := w(-5) xor SBOX32_FUNCT(w(-4));            end if;      end case;   end if; -- encrypt = '1'   return w_temp;end KS_ROUND_FUNCT;-- ==========================================================================end rijndael_pack;

⌨️ 快捷键说明

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