low_cost_design_authentication_for_spartan_3e.vhd

来自「基于spartan3e的串口调试和检测程序,可直接烧写,检测结果将同时通过LCD」· VHDL 代码 · 共 911 行 · 第 1/3 页

VHD
911 行
字号
      if baud_count=325 then
         baud_count <= 0;
         en_16_x_baud <= '1';
       else
         baud_count <= baud_count + 1;
         en_16_x_baud <= '0';
      end if;
    end if;
  end process baud_timer;


  --
  ----------------------------------------------------------------------------------------------------------------------------------
  -- Security KCPSM3 and program memory 
  ----------------------------------------------------------------------------------------------------------------------------------
  --

  security_processor: kcpsm3
    port map(      address => security_address,
               instruction => security_instruction,
                   port_id => security_port_id,
              write_strobe => security_write_strobe,
                  out_port => security_out_port,
               read_strobe => security_read_strobe,
                   in_port => security_in_port,
                 interrupt => security_interrupt,        --interrupt signal generated by application processor
             interrupt_ack => security_interrupt_ack,
                     reset => security_kcpsm3_reset,
                       clk => clk);

  security_kcpsm3_reset <= '0';  


  security_program_rom: security
    port map(      address => security_address,
               instruction => security_instruction,
             check_address => anti_tamper_address,       --additional access port for content verification
                check_data => anti_tamper_data,
                       clk => clk);

  --
  --
  ----------------------------------------------------------------------------------------------------------------------------------
  -- Security KCPSM3 output ports 
  ----------------------------------------------------------------------------------------------------------------------------------
  --
  -- Adding the output registers to the processor.
  -- A mixture of one-hot and binary port address decoding is used to support more than 8 output 'registers'.
  -- This ensures that all decoding (including the write_strobe) will fit a 4-input function for smallest size and max performance.  
  --
 
  security_output_ports: process(clk)
  begin

    if clk'event and clk='1' then
      if security_write_strobe='1' then

        -- Data to be written to StrataFlash at port 80 hex 
        if ( (security_port_id(7)='1') and (security_port_id(1 downto 0)="00") )  then
          write_data <= security_out_port;
        end if;

        -- The 24-bit address to the StrataFLASH memory requires 3 ports.

        -- Address [7:0] at port 81 hex 
        if ( (security_port_id(7)='1') and (security_port_id(1 downto 0)="01") )  then
          strataflash_a(7 downto 0) <= security_out_port;
        end if;

        -- Address [15:8] at port 82 hex 
        if ( (security_port_id(7)='1') and (security_port_id(1 downto 0)="10") )  then
          strataflash_a(15 downto 8) <= security_out_port;
        end if;

        -- Address [23:16] at port 83 hex 
        if ( (security_port_id(7)='1') and (security_port_id(1 downto 0)="11") )  then
          strataflash_a(23 downto 16) <= security_out_port;
        end if;

        -- StrataFlash control signals at port 40 hex 
        if security_port_id(6)='1' then
          strataflash_read <= security_out_port(0);  --Active High and used to control data bus direction and OE
          strataflash_ce <= security_out_port(1);    --Active Low StrataFLASH device enable
          strataflash_we <= security_out_port(2);    --Active Low StrataFLASH write enable
        end if;

        -- LCD data output and controls at address 20 hex.
        if security_port_id(5)='1' then
          lcd_output_data <= security_out_port(7 downto 4);
          lcd_drive <= security_out_port(3);  
          lcd_rs <= security_out_port(2);
          lcd_rw_control <= security_out_port(1);
          lcd_e <= security_out_port(0);
        end if;

        -- Design authentication enable signals at address 10 hex.
        if security_port_id(4)='1' then
          security_disable_interrupts <= security_out_port(0);
          security_disable_outputs <= security_out_port(1);
        end if;

        -- Write to UART at address 08 hex (see combinatoral decode below).

        -- Write to link FIFO buffer at address 04 hex (see combinatoral decode below).

        -- Hint: Do not use security_port_id(0) and security_port_id(1) for one-hot decodes as they
        --       are used to define the StrataFLASH ports 80 to 83 hex.

      end if; 
    end if; 

  end process security_output_ports;

  --
  -- write to UART transmitter FIFO buffer at address 08 hex.
  -- This is a combinatorial decode because the FIFO is the 'port register'.
  --

  write_to_uart <= '1' when (security_write_strobe='1' and security_port_id(3)='1') else '0';

  --
  -- write to link FIFO buffer at address 04 hex.
  -- This is a combinatorial decode because the FIFO is the 'port register'.
  --

  write_link_fifo <= '1' when (security_write_strobe='1' and security_port_id(2)='1') else '0';



  --
  ----------------------------------------------------------------------------------------------------------------------------------
  -- Security KCPSM3 input ports 
  ----------------------------------------------------------------------------------------------------------------------------------
  --
  -- The inputs connect via a pipelined multiplexer
  --

  security_input_ports: process(clk)
  begin
    if clk'event and clk='1' then

      case security_port_id(2 downto 0) is
        
        -- Read UART status signals at address 00 hex
        -- Also a place keeper for StrataFLASH STS signal but not actually used by software provided 
        when "000" =>    security_in_port <= strataflash_sts & "00" & rx_full & rx_half_full & rx_data_present & tx_full & tx_half_full;

        -- Read UART receive data at address 01 hex
        when "001" =>    security_in_port <= rx_data;

        -- read StrataFLASH memory data at address 02 hex
        when "010" =>    security_in_port <= strataflash_d;

        -- read LCD data at address 03 hex
        when "011" =>    security_in_port <= lcd_d & "0000";

        -- read psuedo random value at address 04 hex
        when "100" =>    security_in_port(0) <= psuedo_random_lfsr_counter(2);
                         security_in_port(1) <= psuedo_random_lfsr_counter(16);
                         security_in_port(2) <= psuedo_random_lfsr_counter(7);
                         security_in_port(3) <= psuedo_random_lfsr_counter(11);
                         security_in_port(4) <= psuedo_random_lfsr_counter(6);
                         security_in_port(5) <= psuedo_random_lfsr_counter(4);
                         security_in_port(6) <= psuedo_random_lfsr_counter(9);
                         security_in_port(7) <= psuedo_random_lfsr_counter(12);


        -- Don't care used for all other addresses to ensure minimum logic implementation
        when others =>    security_in_port <= "XXXXXXXX";  

      end case;

      -- Form read strobe for UART receiver FIFO buffer at address 01 hex.
      -- The fact that the read strobe will occur after the actual data is read by 
      -- the KCPSM3 is acceptable because it is really means 'I have read you'!
 
      if (security_read_strobe='1' and security_port_id(2 downto 0)="001") then 
        read_from_uart <= '1';
       else 
        read_from_uart <= '0';
      end if;

    end if;

  end process security_input_ports;


  --
  ----------------------------------------------------------------------------------------------------------------------------------
  -- Pseudo Random Number Generator 
  ----------------------------------------------------------------------------------------------------------------------------------
  --
  --
  -- This simple LFSR counter is used to generate pseudo random values which the security algorithm uses to help 
  -- hide the actual authentication value inside.
  --
  -- More details on implementation of LFSR counters can be found in the Xilinx application note XAPP052.  
  --
  -- This is an 16-bit LFSR counter using XNOR feedback from taps 4, 13, 15 and 16. The counter is free running with the 
  -- clock and therefore the interaction of the intermittent reading by the processor will help enhance the random 
  -- effect although still not truly random. Reading only 8 of the 16-bits also adds to the random effect and means 
  -- that all values including 'FF' may be generated ('FF' is not generated by an 8-bit LFSR).
  --

  pn_generator: process(clk)
  begin
    if clk'event and clk='1' then
      psuedo_random_lfsr_counter <= psuedo_random_lfsr_counter(15 downto 1) &
                                              ( psuedo_random_lfsr_counter(4)  xnor
                                                psuedo_random_lfsr_counter(13) xnor
                                                psuedo_random_lfsr_counter(15) xnor
                                                psuedo_random_lfsr_counter(16)         );
    end if;
  end process pn_generator;

  --
  ----------------------------------------------------------------------------------------------------------------------------------
  -- ANTI-TAMPERING PROTECTION  
  ----------------------------------------------------------------------------------------------------------------------------------
  --
  -- A potential way to attack the authentication process of this design would be to modify the PicoBlaze 
  -- program stored in a Block Memory. To do this, the attacker would first need to reverse engineer the 
  -- configuration image and isolate those bits defining the contents of the PicoBlaze program memory. 
  -- Then these bits would need to be disassembled and the program understood adequately to generate a 
  -- modified program that bypassed the actual authentication but continued to provide the required 
  -- authentication values and enable signals to the application design. 
  --
  -- To help prevent this attack, the following circuit computes a value by reading the program memory 
  -- contents via the second port of the Block Memory. It tests this value against the expected value
  -- and if it does not match it disables the 50MHz clock to the whole device. To keep this circuit 
  -- small and reliable, not all memory locations are included in the test but checking the majority 
  -- should be adequate. 
  --
  -- Hint - Simulation is a good way to determine the computed value generated by your circuit. Then 
  --        modify your VHDL to reflect the correct value.
  --

  anti_tamper_scan: process(clk)
  begin
    if clk'event and clk='1' then

      -- Scan through all addresses of the program ROM using a simple counter.
      -- The counter also acts as the state machine from which the other actions 
      -- are derived. 

      anti_tamper_address <= anti_tamper_address + 1;

      -- Initialise computation circuit when address is 001 hex as this ensures 
      -- reliable operation even after initial power up and configuration of the 
      -- Spartan device. This means that first few locations are excluded from the test.

      if anti_tamper_address = "00000000001" then    
         anti_tamper_compute <= X"ABCD";                --use a value to seed the computation       
       else
       
        -- When the address reaches 7FF hex the value stored in the computation circuit
        -- is compared with the expected value and the clock enable/disable activated 
        -- appropriately.

        if anti_tamper_address = "11111111111" then    
          if anti_tamper_compute = X"E990" then         --expected value        
            security_disable_clock <= '0';
           else
            security_disable_clock <= '1';
          end if;
 
         else  

         -- The computation is a modified accumulation in which bits are applied in a 
         -- scrambled way and inverted dynamically. This makes the expected value 
         -- difficult to generate other than with the correct PicoBlaze program image. 

          anti_tamper_compute <= anti_tamper_compute + (
                                                          anti_tamper_data(3 downto 0) 
                                                        & anti_tamper_data(7 downto 4)
                                                        & (anti_tamper_data(7 downto 0) xor anti_tamper_compute(11 downto 4))
                                                        ); 
        end if;
      end if;
    end if;
  end process anti_tamper_scan;

  
  clock_buffer: BUFGMUX
  port map ( I0 => clk_50mhz,
             I1 => '0',
              S => security_disable_clock,       --When signal is High the clock will be stopped
              O => clk);



  --
  ----------------------------------------------------------------------------------------------------------------------------------

end Behavioral;

------------------------------------------------------------------------------------------------------------------------------------
--
-- END OF FILE low_cost_design_authentication_for_spartan_3e.vhd
--
------------------------------------------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

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