Advertisement
redsees

Synchronized S-DES Design

Jan 19th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Simplified Data Encryption Standard (S-DES)
  3.     8-bit 2-round block cipher symmetric-key algorithm using 10-bit key.
  4.     Encryption/Decryption routine takes total of 7 clock cycles
  5. */
  6.  
  7.  
  8. module SDES(    en,             // Enable Bit
  9.                 CLK,            // Clock signal
  10.                 Key,            // 10-bit Key
  11.                 plaintext,      // 8-bit data block
  12.                 encrypt,        // Encryption flag, 1 for encryption, 0 for decryption
  13.                 ciphertext      // 8-bit data block
  14.                 );
  15.                
  16.     input           en;
  17.     input           encrypt;
  18.     input           CLK;
  19.     input   [0:7]   plaintext;
  20.     input   [0:9]   Key;
  21.     output  [0:7]   ciphertext;
  22.    
  23.     // Internal signals
  24.     reg     [0:7]   sw_out, iip_out, xor_f1, xor_f2, p4_in, s0_out, s1_out;
  25.     reg     [0:2]   cnt = 3'b000;
  26.     reg     [0:7]   Key1, Key2;
  27.    
  28.     assign ciphertext = (en)?(iip_out):(8'b0);
  29.    
  30.     always@(cnt)
  31.     begin
  32.         if(cnt == 3'b110)
  33.             cnt = 3'b000;
  34.     end
  35.    
  36.     always@(posedge CLK)
  37.     begin
  38.         Key1 = {Key[0], Key[6], Key[8], Key[3], Key[7], Key[2], Key[9], Key[5]};
  39.         Key2 = {Key[7], Key[2], Key[5], Key[4], Key[9], Key[1], Key[8], Key[0]};
  40.     end
  41.    
  42.     // Feistel Function Round One
  43.     always@(posedge CLK)
  44.     begin
  45.        
  46.         if(cnt < 3'b011)
  47.         begin
  48.            
  49.             if(encrypt)
  50.             begin
  51.                 xor_f1  <= { plaintext[6] ^ Key1[0],
  52.                              plaintext[3] ^ Key1[1],
  53.                              plaintext[7] ^ Key1[2],
  54.                              plaintext[4] ^ Key1[3] };
  55.                              
  56.                 xor_f2  <= { plaintext[7] ^ Key1[4],
  57.                              plaintext[4] ^ Key1[5],
  58.                              plaintext[6] ^ Key1[6],
  59.                              plaintext[3] ^ Key1[7] };
  60.             end
  61.             else
  62.             begin
  63.                 xor_f1  <= { plaintext[6] ^ Key2[0],
  64.                              plaintext[3] ^ Key2[1],
  65.                              plaintext[7] ^ Key2[2],
  66.                              plaintext[4] ^ Key2[3] };
  67.                              
  68.                 xor_f2  <= { plaintext[7] ^ Key2[4],
  69.                              plaintext[4] ^ Key2[5],
  70.                              plaintext[6] ^ Key2[6],
  71.                              plaintext[3] ^ Key2[7] };     
  72.             end
  73.            
  74.             p4_in  <= {s0_out, s1_out};
  75.            
  76.             cnt <= cnt + 1'b1;
  77.            
  78.         end
  79.     end
  80.    
  81.     // Feistel Function Round Two  
  82.     always@(posedge CLK)
  83.     begin
  84.    
  85.         if(cnt == 3'b011)
  86.         begin
  87.        
  88.             if(encrypt)
  89.             begin
  90.                 xor_f1 <= { p4_in[0] ^ plaintext[0] ^ Key2[0],
  91.                             p4_in[1] ^ plaintext[1] ^ Key2[1],
  92.                             p4_in[3] ^ plaintext[5] ^ Key2[2],
  93.                             p4_in[2] ^ plaintext[2] ^ Key2[3] };
  94.                            
  95.                 xor_f2 <= { p4_in[3] ^ plaintext[5] ^ Key2[4],
  96.                             p4_in[2] ^ plaintext[2] ^ Key2[5],
  97.                             p4_in[0] ^ plaintext[0] ^ Key2[6],
  98.                             p4_in[1] ^ plaintext[1] ^ Key2[7] };
  99.             end
  100.             else
  101.             begin
  102.                 xor_f1 <= { p4_in[0] ^ plaintext[0] ^ Key1[0],
  103.                             p4_in[1] ^ plaintext[1] ^ Key1[1],
  104.                             p4_in[3] ^ plaintext[5] ^ Key1[2],
  105.                             p4_in[2] ^ plaintext[2] ^ Key1[3] };
  106.                            
  107.                 xor_f2 <= { p4_in[3] ^ plaintext[5] ^ Key1[4],
  108.                             p4_in[2] ^ plaintext[2] ^ Key1[5],
  109.                             p4_in[0] ^ plaintext[0] ^ Key1[6],
  110.                             p4_in[1] ^ plaintext[1] ^ Key1[7] };
  111.             end
  112.                    
  113.             p4_in <= {s0_out, s1_out};
  114.            
  115.             iip_out <= {    p4_in[0] ^ plaintext[6],
  116.                             p4_in[1] ^ plaintext[3],
  117.                             p4_in[2] ^ plaintext[4],
  118.                             p4_in[1] ^ plaintext[1],
  119.                             p4_in[2] ^ plaintext[2],
  120.                             p4_in[3] ^ plaintext[7],
  121.                             p4_in[0] ^ plaintext[0],
  122.                             p4_in[3] ^ plaintext[5] };
  123.                            
  124.             cnt <= cnt + 1'b1;
  125.         end
  126.     end
  127.        
  128.     // S0 Box
  129.     always@(xor_f1)
  130.     begin
  131.         case(xor_f1)
  132.             4'b0000: s0_out = 2'b01;
  133.             4'b0001: s0_out = 2'b11;
  134.             4'b0010: s0_out = 2'b00;
  135.             4'b0011: s0_out = 2'b10;
  136.             4'b0100: s0_out = 2'b11;
  137.             4'b0101: s0_out = 2'b01;
  138.             4'b0110: s0_out = 2'b10;
  139.             4'b0111: s0_out = 2'b00;
  140.             4'b1000: s0_out = 2'b00;
  141.             4'b1001: s0_out = 2'b11;
  142.             4'b1010: s0_out = 2'b10;
  143.             4'b1011: s0_out = 2'b01;
  144.             4'b1100: s0_out = 2'b01;
  145.             4'b1101: s0_out = 2'b11;
  146.             4'b1110: s0_out = 2'b11;
  147.             4'b1111: s0_out = 2'b10;
  148.         endcase
  149.     end
  150.  
  151.     // S1 Box
  152.     always@(xor_f2)
  153.     begin
  154.         case(xor_f2)
  155.             4'b0000: s1_out = 2'b00;
  156.             4'b0001: s1_out = 2'b10;
  157.             4'b0010: s1_out = 2'b01;
  158.             4'b0011: s1_out = 2'b00;
  159.             4'b0100: s1_out = 2'b10;
  160.             4'b0101: s1_out = 2'b01;
  161.             4'b0110: s1_out = 2'b11;
  162.             4'b0111: s1_out = 2'b11;
  163.             4'b1000: s1_out = 2'b11;
  164.             4'b1001: s1_out = 2'b10;
  165.             4'b1010: s1_out = 2'b00;
  166.             4'b1011: s1_out = 2'b01;
  167.             4'b1100: s1_out = 2'b01;
  168.             4'b1101: s1_out = 2'b00;
  169.             4'b1110: s1_out = 2'b00;
  170.             4'b1111: s1_out = 2'b11;
  171.         endcase
  172.     end
  173.    
  174. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement