380 likes | 615 Views
Implementation of S-DES by verilog. 2010. 5. 18 조 승훈. Index. Overview of Encryption S-DES Architecture operations Structure Design Implementation Simulation Synthesize. Overview of Encryption. 암호 정의 정보를 보호하기 위한 언어학적 및 수학적 방법론 초기의 암호 메시지 보안에 초점 군사 또는 외교적 목적으로 사용 현대의 암호
E N D
Implementation of S-DESby verilog 2010. 5. 18 조 승훈
Index • Overview of Encryption • S-DES Architecture • operations • Structure Design • Implementation • Simulation • Synthesize
Overview of Encryption • 암호 • 정의 • 정보를 보호하기 위한 언어학적 및 수학적 방법론 • 초기의 암호 • 메시지 보안에 초점 • 군사 또는 외교적 목적으로 사용 • 현대의 암호 • 암호 시스템, 암호 분석, 인증 및 전자 서명 등 • 현금 지급기의 사용, 컴퓨터 패스워드, 전자 상거래 등
Overview of Encryption • 암호화의 목적 • 암호 시스템의 종류 • 대칭키 암호 시스템 ex, DES (Data Encryption Standard) • 공개키 암호 시스템 ex, RSA (Rivest Shamir Adleman) • 양자 암호 시스템
Overview of Encryption • 대칭키 암호 시스템 • 정의 • 암호화 때 사용하는 키와 복호화 때 사용하는 키가 동일한 암호 시스템 • 특징 • 키의 길이, 키의 안전한 관리에 상대적 의존성이 높음 • 알고리즘이 간단하고, 속도가 월등히 빠름 • 폐쇄적인 특성을 갖는 사용자 그룹에 적합 • 동일한 암호키를 비밀리에 관리해야 되기 때문 • 문제점 • 키 관리의 어려움 • 사용자가 관리해야 할 키의 수가 너무 많아 짐 • N명이 가입한 시스템에서 n!/2의 키가 필요 • 각 사용자는 n – 1개의 키를 관리 해야 함
Overview of Encryption • DES (Data Encryption Standard) • 대칭 키 암호화 방식 • Lucifer 시스템을 개선하여 개발 • 1977년 NBS1에서 미국 표준 암호 알고리즘으로 채택 • 64bit 블록의 입력, 64bit 블록의 출력 • 64bit의 키 중 56bit는 실제 키, 8비트는 검사용 비트 • 16라운드의 feistel연산2 미국 상무성의 국립 표준국 (National Bureau of Standard) Feistel연산 : 각각 t비트인 두 블록 L0, R0로 이루어진 2t비트 평문 블록이 r라운드 (r≥1)를 거쳐 암호문으로변환되는 연산 과정
Overview of Encryption • DES의 문제점 • 56bit의 키 길이는 현재 컴퓨터 환경에 비해 너무 짧음 • 1998년 EFF에서 무차별 대입 공격1 하드웨어 제작 • 최대 56시간 안에 해독 가능 • 1999년 22시간 15분 안에 해독하는 하드웨어 제작 • Triple DES는 DES에 비해 안전한 것으로 알려짐 • 현재는 AES(advanced Encryption Standard)가 새 표준 • 2002년 5월 26일부터 표준으로 효력 발휘 • 벨기에의 존 대먼과빈센트라이먼이 제작 무차별 대입 공격(brute force attack) : 특정한 암호를 풀기 위해 가능한 모든 값을 대입
Overview of Encryption • S-DES (Simplified Data Encryption Standard) • 교육용으로 DES를 단순화한 알고리즘 • Santa Clara 대학의 Edward Schaefer 개발 • 8bit 블록 입력, 8bit 블록 출력 • 암호 알고리즘 : 8bit 평문과10bit 키 입력 -> 8bit 암호문 출력 • 복호 알고리즘 : 8bit 암호문과 10bit 키 입력 -> 8bit 평문 출력 • 10bit 키 사용 (검사용 비트 생략) • 2라운드 feistel연산
S-DES Architecture (Operation) • Key Generate Module • P10 • 초기 키의 값은 바꾸지 않고, bit들의 인덱스만 변환 (순열 변환) • 98765_43210 -> 24163_90875 • ex) 0x190 -> 01000_00110 • LS-1 (Left Shift 1bit) • 좌측과 우측을 5bit씩 나누어 각 5bit를 개별적으로 1bit Left Shift • ex) 01000 -> 10000, 00110 -> 01100
S-DES Architecture (Operation) • Key Generate Module (cont’) • P8 • LS-1의 두 결과를 합친 후,8bit로 치환 • 5263_7409 -> 7654_3210 (1, 2, 8 생략) • ex) 10000_01100 -> 0101_0001 (Sub_Key 1) • LS-2 (Left Shift 2bit) • 좌측과 우측을 5bit씩 나누어 각 5bit를 개별적으로 2bit Left Shift • ex) 10000 -> 00010, 01100 -> 10001 • P8 • LS-2의 두 결과를 합친 후,8bit로 치환 • ex) 00010_10001 -> 00001_00110 (Sub_Key 2)
S-DES Architecture (Operation) • Encryption Module • IP (Initial Permutation) • 초기 치환 • 7654_3210 -> 6257_4031 • ex) 0111_1001 -> 1010_1110 • E/P (Extend / Permutation) • 우측 4bit를 8bit로 확장 / 치환 • 3210 -> 0321_2103 • ex) 1110 -> 0111_1101 • XOR • Sub_Key1과 E/P 결과를 XOR • ex)0111_1101 ^ 0101_0001 = 0010_1100
S-DES Architecture (Operation) S-Box 0 S-Box 1 • Encryption Module (Cont’) • S0 and S1 (S-Box0 and S-Box1) • S-Box 연산 • ex)0010 -> 00, 1100 -> 01 • P4 (Permutation) • 4bit 통합 치환 • 32 + 10 -> 2013 • ex) 00 + 01 -> 0100 • XOR • IP 좌측 4bit와 P4의 결과를 XOR • ex) 1010 ^ 0100 -> 1110
S-DES Architecture (Operation) • Encryption Module (Cont’) • SW (Switching) • 좌측 4bit와 우측 4bit를 교환 • ex) 1110 exchange 1110 = 1110_1110 • IP-1 (Inverse Initial Permutation) • 초기 치환의 역치환 • 7654_3210 -> 4753_1602 • ex) 0010_1110 -> 0011_1001
Structure Design ext_input ext_output i_text i_key i_DE i_enable i_valid Register 8 10 TOP w_LastRound Control r_text r_valid r_valid r_DE r_key o_valid r_en r_valid 8 P10 r_inv IP o_CText MUX FSM MUX D_I/O K_I/O 8 10 w_key 8 w_PText w_CText w_round 10 w_SKey Fk SHIFT CLK 8 r_shift P8 reset r_SW w_SubK Encryption Key Gen
Structure Design (Enc Module) REG From IP or Previous Fk Module 8-Bit Plain-Text 4 4 E/P IP 8 8 Sub_Key 4 4 0S IS Fk 2 2 P4 Fk 4 4 IP-1 SW 8-Bit Cipher-Text
Structure Design (Key Module) REG Module 10-Bit Key P10 From 10-Bit Key or Previous K_gen shift K_gen K1 Sub_Key P8 r_shift K_gen K2
1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 1 1 1 Structure Design (Timing) clk reset i_en 3 clk i_valid r_en 3 clk r_valid i_text i_key r_text r_key sub_key w_PText o_text o_valid N_State EN NOP R0 R1 EN READY INIT READY INIT R0 R1 NOP C_State NOP EN NOP R0 R1 EN READY INIT READY INIT R0 R1 NOP
Structure Design (State Machine) r_enable r_valid (0,0) (0,0) NOP EN READY (1,0) (1,1) i_en or reset (1,0) (1,1) R1 R0 INIT
Implementation (CTL) • /* current state */ • always@ (posedgeclk or posedge reset) • begin • if(reset) • r_state <= `NOP; • else begin • r_state <= r_next; • end • end • /* next state */ • always@ (posedge reset or r_en or r_valid or r_state) • begin • if(reset) • r_next <= `NOP; • else begin • case(r_state) • `NOP : if(r_en) r_next <= `EN; else r_next <= `NOP; • `EN : case({r_en, r_valid}) • 2'b00 : r_next <= `NOP; • 2'b10 : r_next <= `EN; • 2'b11 : r_next <= `READY; • endcase • `READY : r_next <= `INIT; • `INIT : r_next <= `R0; • `R0 : r_next <= `R1; • `R1 : case({r_en, r_valid}) • 2'b10 : r_next <= `EN; • 2'b11 : r_next <= `READY; • endcase • endcase • end • end `define NOP 16 `define EN 8 `define READY 4 `define INIT 2 `define R1 1 `define R0 0 module CTL(clk, reset, i_enable, i_valid, o_round, o_lastround); input clk; input reset; input i_enable; input i_valid; output o_round; output o_lastround; regr_en; regr_valid; regr_round; regr_last; regr_d_last; reg [5:0] r_next; reg [5:0] r_state; assign o_round = r_round; assign o_lastround = r_last; i_en i_valid Control r_valid r_en w_lastround FSM w_round
Implementation (CTL) (cont’) • /* generate round signal */ • always @(posedgeclk or posedge reset) • begin • if(reset) • r_round <= 1'b0; • else begin • if(r_state == `INIT) • r_round <= 1'b1; • else • r_round <= 1'b0; • end • end • /* generate lastround */ • always @(posedgeclk or posedge reset) • begin • if(reset) • r_last <= 1'b0; • else begin • if(r_state == `R1) • r_last <= 1'b1; • else • r_last <= 1'b0; • end • end • endmodule /* save enable */ always@ (posedgeclk or posedge reset) begin if(reset) r_en <= 1'b0; else begin if(i_enable) r_en <= 1'b1; else r_en <= i_enable; end end /* save valid */ always@ (posedgeclk or posedge reset) begin if(reset) r_valid <= 1'b0; else begin r_valid <= i_valid; end end i_en i_valid Control r_valid r_en w_lastround FSM w_round
Implementation (K_IO) • /* save valid */ • always @(posedgeclk or posedge reset) • begin • if(reset) • r_val <= 1'b0; • else begin • r_val <= i_valid; • end • end • /* save key */ • always@ (posedgeclk or posedge reset) • begin • if(reset) • r_key <= 10'd0; • else begin • if(r_val) • r_key <= i_key; • end • end module K_IO(clk, reset, i_valid, i_DE, i_key, i_round, i_SKey, o_key); input clk; input reset; input i_valid; input i_DE; input [9:0] i_key; input i_round; input [9:0] i_SKey; output [9:0] o_key; regr_val; regr_DE; reg [9:0] r_key; reg [9:0] r_P10; wire [9:0] w_P10; /* select o_key */ assign o_key = (i_round == 1'b0) ? r_P10 : i_SKey; 10 i_ED i_key i_valid r_valid r_key r_ED w_round P10 10 MUX w_SKey 10 w_key
Implementation (K_IO) (cont’) /* save D/E */ always @(posedgeclk or posedge reset) begin if(reset) r_DE <= 1'b0; else begin r_DE <= i_DE; end end /* P10 */ always @(posedgeclk or posedge reset) begin if(reset) r_P10 <= 10'd0; else begin r_P10 <= {r_key[2], r_key[4], r_key[1], r_key[6], r_key[3], r_key[9], r_key[0], r_key[8], r_key[7], r_key[5]}; end end endmodule 10 i_ED i_key i_valid r_valid r_key r_ED w_round P10 10 MUX w_SKey 10 w_key
Implementation (Key_Gen) • always @(posedgeclk or posedge reset) • begin • if(reset) • r_shift <= 10'd0; • else begin • r_shift <= w_shift; • end • end • endmodule module Key_Gen (clk, reset, i_key, i_round, o_SKey, o_SubK); input clk; input reset; input [9:0] i_key; input i_round; output [9:0] o_SKey; output [7:0] o_SubK; reg [9:0] r_shift; wire [9:0] w_shift; /* n-bit shift */ assign w_shift = (i_round == 1'b0) ? {i_key[8:5], i_key[9], i_key[3:0], i_key[4]} : {i_key[7:5], i_key[9:8], i_key[2:0], i_key[4:3]}; /* P8 operation */ assign o_SubK = {w_shift[5], w_shift[2], w_shift[6], w_shift[3], w_shift[7], w_shift[4], w_shift[0], w_shift[9]}; /* for write back */ assign o_SKey = r_shift; 10 w_key SHIFT w_SKey w_SubK 10 10 P8 r_shift
Implementation (D_IO) • /* save valid */ • always@ (posedgeclk or posedge reset) • begin • if(reset) • r_val <= 1'b0; • else begin • r_val <= i_valid; • end • end • /* save text */ • always@ (posedgeclk or posedge reset) • begin • if(reset) • r_text <= 8'd0; • else begin • if(r_val) • r_text <= i_text; • end • end module D_IO(clk, reset, i_valid, i_round, i_lastround, i_text, i_CText, o_PText, o_valid, o_CText); input clk; input reset; input i_valid; input i_round; input i_lastround; input [7:0] i_text; input [7:0] i_CText; output [7:0] o_PText; output o_valid; output [7:0] o_CText; reg [7:0] r_text; regr_val; reg [7:0] r_IP; reg [7:0] r_inv; assign o_PText = (i_round == 1'b0) ? r_IP : i_CText; assign o_valid = (i_lastround == 1'b1) ? 1'b1 : 1'b0; assign o_CText = r_inv;
Implementation (D_IO) (cont’) /* IP */ always@ (posedgeclk or posedge reset) begin if(reset) r_IP <= 8'd0; else begin r_IP <= {r_text[6], r_text[2], r_text[5], r_text[7], r_text[4], r_text[0], r_text[3], r_text[1]}; end end /* IP inverse */ always@ (posedgeclk or posedge reset) begin if(reset) r_inv <= 8'd0; else begin r_inv <= {i_CText[4], i_CText[7], i_CText[5], i_CText[3], i_CText[1], i_CText[6], i_CText[0], i_CText[2]}; end end endmodule 8 i_text i_valid o_valid r_text r_valid w_lastround o_CText 8 IP IP w_round w_CText 8 MUX 8 w_PText
Implementation (ENC) /* Switching */ assign w_SW = (i_round == 1'b0) ? {i_PText[3:0], w_Lxor} : {w_Lxor, i_PText[3:0]}; /* write back */ assign o_CText = r_SW; /* S-BOX */ S0 u0(.index(r_Kxor[7:4]), .out(w_S0)); S1 u1(.index(r_Kxor[3:0]), .out(w_S1)); /* Sub-key ^ E/P */ always @(w_EP or posedge reset or i_SubK) begin if(reset) r_Kxor <= 8'd0; else begin r_Kxor <= w_EP ^ i_SubK; end end /* save swtching */ always @(posedgeclk or posedge reset) begin if(reset) r_SW <= 8'd0; else begin r_SW <= w_SW; end end endmodule module ENC(clk, reset, i_round, i_PText, i_SubK, o_CText); input clk; input reset; input i_round; input [7:0] i_PText; input [7:0] i_SubK; output [7:0] o_CText; reg [7:0] r_Kxor; reg [7:0] r_SW; wire [7:0] w_EP; wire [1:0] w_S0; wire [1:0] w_S1; wire [3:0] w_P4; wire [3:0] w_Lxor; wire [7:0] w_SW; /* E/P operation */ assign w_EP = {i_PText[0], i_PText[3], i_PText[2], i_PText[1], i_PText[2], i_PText[1], i_PText[0], i_PText[3]}; /* left ^ (previous or IP) */ assign w_Lxor = i_PText[7:4] ^ w_P4; /* P4 operation */ assign w_P4 = {w_S0[0], w_S1[0], w_S1[1], w_S0[1]}; 8 w_PText w_round Fk w_SubK 8 r_SW 8 w_CText
Implementation (S_BOX) • module S1(index, out); • input [3:0] index; • output [1:0] out; • reg [1:0] out; • always @(index) • begin • case({index[3], index[0], index[2:1]}) • 4'd00 : out = 2'd0; • 4'd01 : out = 2'd1; • 4'd02 : out = 2'd2; • 4'd03 : out = 2'd3; • 4'd04 : out = 2'd2; • 4'd05 : out = 2'd0; • 4'd06 : out = 2'd1; • 4'd07 : out = 2'd3; • 4'd08 : out = 2'd3; • 4'd09 : out = 2'd0; • 4'd10 : out = 2'd1; • 4'd11 : out = 2'd0; • 4'd12 : out = 2'd2; • 4'd13 : out = 2'd1; • 4'd14 : out = 2'd0; • 4'd15 : out = 2'd3; • endcase • end • endmodule module S0(index, out); input [3:0] index; output [1:0] out; reg [1:0] out; always @(index) begin case({index[3], index[0], index[2:1]}) 4'd00 : out = 2'd1; 4'd01 : out = 2'd0; 4'd02 : out = 2'd3; 4'd03 : out = 2'd2; 4'd04 : out = 2'd3; 4'd05 : out = 2'd2; 4'd06 : out = 2'd1; 4'd07 : out = 2'd0; 4'd08 : out = 2'd0; 4'd09 : out = 2'd2; 4'd10 : out = 2'd1; 4'd11 : out = 2'd3; 4'd12 : out = 2'd3; 4'd13 : out = 2'd1; 4'd14 : out = 2'd3; 4'd15 : out = 2'd2; endcase end endmodule
Implementation (TOP) • CTL ctl (.clk(clk), .reset(reset), .i_enable(i_en), .i_valid(i_val), • .o_round(w_round), .o_lastround(w_last)); • K_IO kio • (.clk(clk), .reset(reset), .i_valid(i_val), .i_DE(i_DE), • .i_key(i_key), .i_round(w_round), .i_SKey(w_SKey), • .o_key(w_key)); • Key_Genkgen (.clk(clk), .reset(reset), .i_key(w_key), .i_round(w_round), .o_SKey(w_SKey), .o_SubK(w_SubK)); • D_IO dio (.clk(clk), .reset(reset), .i_valid(i_val), .i_round(w_round), .i_lastround(w_last), .i_text(i_text), .i_CText(w_CText), • .o_PText(w_PText), .o_valid(o_val), .o_CText(o_CText)); • ENC enc • (.clk(clk), .reset(reset), .i_round(w_round), • .i_PText(w_PText), .i_SubK(w_SubK), .o_CText(w_CText)); • endmodule module TOP(clk, reset, i_en, i_val, i_DE, i_key, i_text, o_val, o_CText); input clk; input reset; input i_en; input i_val; input i_DE; input [9:0] i_key; input [7:0] i_text; output o_val; output [7:0] o_CText; //ctl wire wire w_round; wire w_last; //key wire wire [7:0] w_SubK; wire [9:0] w_SKey; wire [9:0] w_key; //Enc wire wire [7:0] w_PText; wire [7:0] w_CText;
Implementation (TEST_BENCH) • always @(posedgeclk or posedge reset) • begin • if(reset) • cont <= 16'd0; • else begin • cont <= cont + 1'b1; • if(cont == 2) • begin • en <= 1'b1; • end • else if(cont == 3) • begin • val <= 1'b1; • key <= 10'h190; • text <= 8'h79; • end • else if(cont == 4) • begin • val <= 1'b0; • end • else if(cont == 5) • begin • key <= 10'h0; • text <=8'h0; • end • else if(cont == 7) • begin • val <= 1'b1; • key <= 10'h190; • text <= 8'h90; • end • else if(cont == 8) • begin • val <= 1'b0; • end • else if(cont == 9) • begin • key <= 10'h0; • text <= 8'h0; • end • else if(cont == 20) • begin • $stop; • end • end • end initial begin clk <= 1; forever #1 clk <= ~clk; end initial begin reset = 1'b1; #100 reset = 1'b0; end initial begin en = 1'b0; val = 1'b0; DE = 1'b0; text = 8'd0; key = 10'd0; end
Simulation STATE 16 – NOP 8 – EN 4 – READY 2 – INIT 1 – R1 0 – R0