Advertisement
redsees

Synchronized S-DES Design

Jan 19th, 2017
111
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], plaintext[3] ^ Key1[1], plaintext[7] ^ Key1[2], plaintext[4] ^ Key1[3] };
  52.                 xor_f2  <= { plaintext[7] ^ Key1[4], plaintext[4] ^ Key1[5], plaintext[6] ^ Key1[6], plaintext[3] ^ Key1[7] };
  53.             end
  54.             else
  55.             begin
  56.                 xor_f1  <= { plaintext[6] ^ Key2[0], plaintext[3] ^ Key2[1], plaintext[7] ^ Key2[2], plaintext[4] ^ Key2[3] };
  57.                 xor_f2  <= { plaintext[7] ^ Key2[4], plaintext[4] ^ Key2[5], plaintext[6] ^ Key2[6], plaintext[3] ^ Key2[7] };     
  58.             end
  59.            
  60.             p4_in  <= {s0_out, s1_out};
  61.            
  62.             cnt <= cnt + 1'b1;
  63.            
  64.         end
  65.     end
  66.    
  67.     // Feistel Function Round Two  
  68.     always@(posedge CLK)
  69.     begin
  70.    
  71.         if(cnt == 3'b011)
  72.         begin
  73.        
  74.             if(encrypt)
  75.             begin
  76.                 xor_f1 <= { p4_in[0] ^ plaintext[0] ^ Key2[0], p4_in[1] ^ plaintext[1] ^ Key2[1], p4_in[3] ^ plaintext[5] ^ Key2[2], p4_in[2] ^ plaintext[2] ^ Key2[3] };
  77.                 xor_f2 <= { p4_in[3] ^ plaintext[5] ^ Key2[4], p4_in[2] ^ plaintext[2] ^ Key2[5], p4_in[0] ^ plaintext[0] ^ Key2[6], p4_in[1] ^ plaintext[1] ^ Key2[7] };
  78.             end
  79.             else
  80.             begin
  81.                 xor_f1 <= { p4_in[0] ^ plaintext[0] ^ Key1[0], p4_in[1] ^ plaintext[1] ^ Key1[1], p4_in[3] ^ plaintext[5] ^ Key1[2], p4_in[2] ^ plaintext[2] ^ Key1[3] };
  82.                 xor_f2 <= { p4_in[3] ^ plaintext[5] ^ Key1[4], p4_in[2] ^ plaintext[2] ^ Key1[5], p4_in[0] ^ plaintext[0] ^ Key1[6], p4_in[1] ^ plaintext[1] ^ Key1[7] };
  83.             end
  84.                    
  85.             p4_in <= {s0_out, s1_out};
  86.            
  87.             iip_out <= {    p4_in[0] ^ plaintext[6],
  88.                             p4_in[1] ^ plaintext[3],
  89.                             p4_in[2] ^ plaintext[4],
  90.                             p4_in[1] ^ plaintext[1],
  91.                             p4_in[2] ^ plaintext[2],
  92.                             p4_in[3] ^ plaintext[7],
  93.                             p4_in[0] ^ plaintext[0],
  94.                             p4_in[3] ^ plaintext[5] };
  95.                            
  96.             cnt <= cnt + 1'b1;
  97.         end
  98.     end
  99.        
  100.     // S0 Box
  101.     always@(xor_f1)
  102.     begin
  103.         case(xor_f1)
  104.             4'b0000: s0_out = 2'b01;
  105.             4'b0001: s0_out = 2'b11;
  106.             4'b0010: s0_out = 2'b00;
  107.             4'b0011: s0_out = 2'b10;
  108.             4'b0100: s0_out = 2'b11;
  109.             4'b0101: s0_out = 2'b01;
  110.             4'b0110: s0_out = 2'b10;
  111.             4'b0111: s0_out = 2'b00;
  112.             4'b1000: s0_out = 2'b00;
  113.             4'b1001: s0_out = 2'b11;
  114.             4'b1010: s0_out = 2'b10;
  115.             4'b1011: s0_out = 2'b01;
  116.             4'b1100: s0_out = 2'b01;
  117.             4'b1101: s0_out = 2'b11;
  118.             4'b1110: s0_out = 2'b11;
  119.             4'b1111: s0_out = 2'b10;
  120.         endcase
  121.     end
  122.  
  123.     // S1 Box
  124.     always@(xor_f2)
  125.     begin
  126.         case(xor_f2)
  127.             4'b0000: s1_out = 2'b00;
  128.             4'b0001: s1_out = 2'b10;
  129.             4'b0010: s1_out = 2'b01;
  130.             4'b0011: s1_out = 2'b00;
  131.             4'b0100: s1_out = 2'b10;
  132.             4'b0101: s1_out = 2'b01;
  133.             4'b0110: s1_out = 2'b11;
  134.             4'b0111: s1_out = 2'b11;
  135.             4'b1000: s1_out = 2'b11;
  136.             4'b1001: s1_out = 2'b10;
  137.             4'b1010: s1_out = 2'b00;
  138.             4'b1011: s1_out = 2'b01;
  139.             4'b1100: s1_out = 2'b01;
  140.             4'b1101: s1_out = 2'b00;
  141.             4'b1110: s1_out = 2'b00;
  142.             4'b1111: s1_out = 2'b11;
  143.         endcase
  144.     end
  145.    
  146. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement