├── ciphers ├── sm4 │ ├── sm4_ck.v │ ├── sm4_dpc.v │ ├── sm4_core.v │ ├── sm4_keyex.v │ └── tb_sm4_core.v ├── des │ ├── des_core.v │ ├── tb_des_core.v │ ├── des_sbox1.v │ ├── des_sbox2.v │ ├── des_sbox3.v │ ├── des_sbox4.v │ ├── des_sbox5.v │ ├── des_sbox6.v │ ├── des_sbox7.v │ └── des_sbox8.v ├── rc5 │ ├── rc5_core.v │ ├── rc5_rol.v │ ├── rc5_keyex.v │ ├── tb_rc5_core.v │ └── rc5_dpc.v ├── rc6 │ ├── rc6_core.v │ ├── rc6_rol.v │ ├── tb_rc6_core.v │ ├── rc6_keyex.v │ └── rc6_dpc.v ├── aes │ ├── aes128_core.v │ ├── aes192_core.v │ ├── aes256_core.v │ ├── aes_mixcol_w.v │ ├── aes_mixcol_b.v │ ├── aes128_keyex.v │ ├── aes192_keyex.v │ ├── tb_aes128_core.v │ ├── tb_aes192_core.v │ ├── tb_aes256_core.v │ ├── aes256_keyex.v │ ├── aes128_dpc.v │ ├── aes192_dpc.v │ └── aes256_dpc.v ├── cast5 │ ├── cast5_core.v │ ├── cast5_gb.v │ ├── cast5_rol.v │ ├── tb_cast5_core.v │ └── cast5_dpc.v └── xtea │ ├── xtea_core.v │ ├── xtea_keyex.v │ ├── tb_xtea_core.v │ └── xtea_dpc.v ├── doc ├── rfc1319-md2.pdf ├── rfc1320-md4.pdf ├── rfc1321-md5.pdf ├── rfc3174-sha1.pdf ├── rfc6234-sha2.pdf ├── NIST.FIPS.197-AES.pdf ├── NIST.FIPS.202-SHA3.pdf └── SM3 Cryptographic Hash Algorithm.pdf ├── hashes ├── tiger │ ├── tiger_core.v │ ├── tiger_round.v │ ├── tiger_key_sch.v │ ├── tiger_sbox_a.v │ ├── tiger_sbox_b.v │ ├── tiger_sbox_c.v │ ├── tiger_sbox_d.v │ └── tb_tiger_core.v ├── sha2 │ ├── tb_sha256_core.v │ ├── tb_sha224_core.v │ ├── tb_sha512_224_core.v │ ├── tb_sha512_256_core.v │ ├── tb_sha384_core.v │ ├── tb_sha512_core.v │ └── sha256_core.v ├── sha1 │ ├── tb_sha1_core.v │ └── sha1_core.v └── sm3 │ └── sm3_core.v └── stream └── zuc └── tb_zuc_core.v /ciphers/sm4/sm4_ck.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/sm4/sm4_ck.v -------------------------------------------------------------------------------- /ciphers/sm4/sm4_dpc.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/sm4/sm4_dpc.v -------------------------------------------------------------------------------- /doc/rfc1319-md2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/rfc1319-md2.pdf -------------------------------------------------------------------------------- /doc/rfc1320-md4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/rfc1320-md4.pdf -------------------------------------------------------------------------------- /doc/rfc1321-md5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/rfc1321-md5.pdf -------------------------------------------------------------------------------- /doc/rfc3174-sha1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/rfc3174-sha1.pdf -------------------------------------------------------------------------------- /doc/rfc6234-sha2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/rfc6234-sha2.pdf -------------------------------------------------------------------------------- /ciphers/des/des_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/des/des_core.v -------------------------------------------------------------------------------- /ciphers/rc5/rc5_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/rc5/rc5_core.v -------------------------------------------------------------------------------- /ciphers/rc6/rc6_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/rc6/rc6_core.v -------------------------------------------------------------------------------- /ciphers/sm4/sm4_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/sm4/sm4_core.v -------------------------------------------------------------------------------- /ciphers/sm4/sm4_keyex.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/sm4/sm4_keyex.v -------------------------------------------------------------------------------- /ciphers/aes/aes128_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/aes/aes128_core.v -------------------------------------------------------------------------------- /ciphers/aes/aes192_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/aes/aes192_core.v -------------------------------------------------------------------------------- /ciphers/aes/aes256_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/aes/aes256_core.v -------------------------------------------------------------------------------- /ciphers/cast5/cast5_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/cast5/cast5_core.v -------------------------------------------------------------------------------- /ciphers/xtea/xtea_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/ciphers/xtea/xtea_core.v -------------------------------------------------------------------------------- /doc/NIST.FIPS.197-AES.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/NIST.FIPS.197-AES.pdf -------------------------------------------------------------------------------- /doc/NIST.FIPS.202-SHA3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/NIST.FIPS.202-SHA3.pdf -------------------------------------------------------------------------------- /hashes/tiger/tiger_core.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_core.v -------------------------------------------------------------------------------- /hashes/tiger/tiger_round.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_round.v -------------------------------------------------------------------------------- /hashes/tiger/tiger_key_sch.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_key_sch.v -------------------------------------------------------------------------------- /hashes/tiger/tiger_sbox_a.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_sbox_a.v -------------------------------------------------------------------------------- /hashes/tiger/tiger_sbox_b.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_sbox_b.v -------------------------------------------------------------------------------- /hashes/tiger/tiger_sbox_c.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_sbox_c.v -------------------------------------------------------------------------------- /hashes/tiger/tiger_sbox_d.v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/hashes/tiger/tiger_sbox_d.v -------------------------------------------------------------------------------- /doc/SM3 Cryptographic Hash Algorithm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crypt-xie/XCryptCore/HEAD/doc/SM3 Cryptographic Hash Algorithm.pdf -------------------------------------------------------------------------------- /ciphers/aes/aes_mixcol_w.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------------------------- 18 | // File name : aes_mixcol_w.v 19 | // Function : AES Cryptographic Algorithm Core [MixColumn-word] 20 | // ------------------------------------------------------------------------------------------------- 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------------------------- 26 | module aes_mixcol_w( 27 | input [31:0] i_din, 28 | output [31:0] o_dout_x, 29 | output [31:0] o_dout_y); 30 | 31 | wire [7:0] s_a,s_b,s_c,s_d; 32 | wire [7:0] s_x1,s_x2,s_x3,s_x4; 33 | wire [7:0] s_y1,s_y2,s_y3,s_y4; 34 | 35 | assign s_a = i_din[31:24]; 36 | assign s_b = i_din[23:16]; 37 | assign s_c = i_din[15:8]; 38 | assign s_d = i_din[7:0]; 39 | 40 | aes_mixcol_b u_bm1 (.i_a(s_a), .i_b(s_b), .i_c(s_c), .i_d(s_d), .o_x(s_x1), .o_y(s_y1)); 41 | aes_mixcol_b u_bm2 (.i_a(s_b), .i_b(s_c), .i_c(s_d), .i_d(s_a), .o_x(s_x2), .o_y(s_y2)); 42 | aes_mixcol_b u_bm3 (.i_a(s_c), .i_b(s_d), .i_c(s_a), .i_d(s_b), .o_x(s_x3), .o_y(s_y3)); 43 | aes_mixcol_b u_bm4 (.i_a(s_d), .i_b(s_a), .i_c(s_b), .i_d(s_c), .o_x(s_x4), .o_y(s_y4)); 44 | 45 | assign o_dout_x = {s_x1,s_x2,s_x3,s_x4}; 46 | assign o_dout_y = {s_y1,s_y2,s_y3,s_y4}; 47 | 48 | endmodule 49 | -------------------------------------------------------------------------------- /ciphers/aes/aes_mixcol_b.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ---------------------------------------------------------------------- 18 | // File name : aes_mixcol_b.v 19 | // Function : AES Cryptographic Algorithm Core [MixColumn-byte] 20 | // ---------------------------------------------------------------------- 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ---------------------------------------------------------------------- 26 | 27 | module aes_mixcol_b( 28 | input [7:0] i_a, 29 | input [7:0] i_b, 30 | input [7:0] i_c, 31 | input [7:0] i_d, 32 | output [7:0] o_x, 33 | output [7:0] o_y); 34 | 35 | wire [7:0] s_w1,s_w2,s_w3,s_w4; 36 | wire [7:0] s_w5,s_w6,s_w7,s_w8; 37 | 38 | function [7:0] xtime; 39 | input [7:0] in; 40 | reg [3:0] xtime_t; 41 | begin 42 | xtime[7:5] = in[6:4]; 43 | xtime_t[3] = in[7]; 44 | xtime_t[2] = in[7]; 45 | xtime_t[1] = 0; 46 | xtime_t[0] = in[7]; 47 | xtime[4:1] =xtime_t^in[3:0]; 48 | xtime[0] = in[7]; 49 | end 50 | endfunction 51 | 52 | assign s_w1 = i_a ^i_b; 53 | assign s_w2 = i_a ^i_c; 54 | assign s_w3 = i_c ^i_d; 55 | assign s_w4 = xtime(s_w1); 56 | assign s_w5 = xtime(s_w3); 57 | assign s_w6 = s_w2 ^s_w4 ^s_w5; 58 | assign s_w7 = xtime(s_w6); 59 | assign s_w8 = xtime(s_w7); 60 | 61 | assign o_x = i_b^s_w3^s_w4; 62 | assign o_y = s_w8^o_x; 63 | 64 | endmodule 65 | -------------------------------------------------------------------------------- /ciphers/cast5/cast5_gb.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : cast5_gb.v 19 | // Function : CAST5 Cryptographic Algorithm Core GB 20 | // : #define GB(x, i) (((x[(15-i)>>2])>>(unsigned)(8*((15-i)&3)))&255) 21 | // ------------------------------------------------------------------------------ 22 | // Author : Xie 23 | // Version : v-1.0 24 | // Date : 2019-2-3 25 | // Email : xcrypt@126.com 26 | // ------------------------------------------------------------------------------ 27 | 28 | `timescale 1ns / 1ps 29 | 30 | module cast5_gb( 31 | input [3:0] i_s, //i 32 | input [127:0] i_din, //x 33 | output [7:0] o_dout 34 | ); 35 | 36 | wire [31:0] s_dw; 37 | 38 | function [31:0] WS; 39 | input [127:0] D; 40 | input [3:0] S; 41 | reg [3:0] Sx; 42 | begin 43 | Sx = 15 - S; 44 | WS = (Sx[3:2] == 2'b00) ? D[31: 0] : 45 | ((Sx[3:2] == 2'b01) ? D[63:32] : 46 | ((Sx[3:2] == 2'b10) ? D[95:64] : 47 | ((Sx[3:2] == 2'b11) ? D[127:96]: 32'b0))); 48 | end 49 | endfunction 50 | 51 | function [31:0] SR; 52 | input [31:0] D; 53 | input [3:0] S; 54 | reg [3:0] Sx; 55 | begin 56 | Sx = 15 - S; 57 | SR = (Sx[1:0] == 2'b00) ? D[7: 0] : 58 | ((Sx[1:0] == 2'b01) ? D[15:8] : 59 | ((Sx[1:0] == 2'b10) ? D[23:16]: 60 | ((Sx[1:0] == 2'b11) ? D[31:24]: 8'b0))); 61 | end 62 | endfunction; 63 | 64 | assign s_dw = WS(i_din,i_s); 65 | assign o_dout = SR(s_dw,i_s); 66 | 67 | endmodule 68 | -------------------------------------------------------------------------------- /stream/zuc/tb_zuc_core.v: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // File name : tb_zuc_core.v 3 | // Function : ZUC Cryptographic Algorithm Core Simulate File 4 | // ------------------------------------------------------------------------------------------------- 5 | // Author : Xie 6 | // Version : v-1.0 7 | // Date : 2018-12-25 8 | // Email : xcrypt@126.com 9 | // copyright : XCrypt Studio 10 | // ------------------------------------------------------------------------------------------------- 11 | 12 | `timescale 1ns / 1ps 13 | 14 | module tb_zuc_core(); 15 | 16 | reg r_clk; 17 | reg r_rst; 18 | reg r_init; 19 | reg [127:0] r_key; 20 | reg [127:0] r_iv; 21 | reg r_ready; 22 | wire s_valid; 23 | wire [31:0] s_data; 24 | 25 | reg [127:0] KEY0 = 128'b0; 26 | reg [127:0] IV0 = 128'b0; 27 | 28 | reg [127:0] KEY1 = 128'hffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff; 29 | reg [127:0] IV1 = 128'hffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff; 30 | 31 | reg [127:0] KEY2 = 128'h3d4c_4be9_6a82_fdae_b58f_641d_b17b_455b; 32 | reg [127:0] IV2 = 128'h8431_9aa8_de69_15ca_1f6b_da6b_fbd8_c766; 33 | 34 | zuc_core u_core( 35 | .i_clk (r_clk ), 36 | .i_rst (r_rst ), 37 | .i_init (r_init ), 38 | .i_key (r_key ), 39 | .i_iv (r_iv ), 40 | .i_ready (r_ready ), 41 | .o_valid (s_valid ), 42 | .o_data (s_data ) 43 | ); 44 | 45 | initial begin 46 | r_clk <= 1'b0; 47 | forever #5 r_clk = ~r_clk; 48 | end 49 | 50 | initial begin 51 | r_rst = 1'b1; 52 | r_init = 1'b0; 53 | r_key = 128'b0; 54 | r_iv = 128'b0; 55 | r_ready = 1'b0; 56 | repeat(10) @(negedge r_clk); 57 | r_rst = 1'b0; 58 | /// 59 | repeat(10) @(negedge r_clk); 60 | r_key = KEY0; 61 | r_iv = IV0; 62 | r_init = 1'b1; 63 | repeat(2) @(negedge r_clk); 64 | r_init = 1'b0; 65 | r_ready = 1'b1; 66 | wait(s_valid); 67 | r_ready = 1'b0; 68 | repeat(2) @(negedge r_clk); 69 | r_ready = 1'b1; 70 | //delay 71 | repeat(1000) @(negedge r_clk); 72 | /// 73 | r_key = KEY1; 74 | r_iv = IV1; 75 | r_init = 1'b1; 76 | repeat(2) @(negedge r_clk); 77 | r_init = 1'b0; 78 | r_ready = 1'b1; 79 | //delay 80 | repeat(1000) @(negedge r_clk); 81 | /// 82 | r_key = KEY2; 83 | r_iv = IV2; 84 | r_init = 1'b1; 85 | repeat(2) @(negedge r_clk); 86 | r_init = 1'b0; 87 | r_ready = 1'b1; 88 | //delay 89 | repeat(1000) @(negedge r_clk); 90 | $stop; 91 | end 92 | 93 | endmodule 94 | -------------------------------------------------------------------------------- /ciphers/rc5/rc5_rol.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : rc5_rol.v 19 | // Function : RC5 Cryptographic Algorithm Core ROL 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module rc5_rol( 30 | input [4:0] round, 31 | input [31:0] din, 32 | output [31:0] dout 33 | ); 34 | reg [31:0] r_dout; 35 | assign dout = r_dout; 36 | always@(round or din) begin 37 | case(round) 38 | 5'h00 : r_dout = din; 39 | 5'h01 : r_dout = {din[30:0],din[31] }; 40 | 5'h02 : r_dout = {din[29:0],din[31:30]}; 41 | 5'h03 : r_dout = {din[28:0],din[31:29]}; 42 | 5'h04 : r_dout = {din[27:0],din[31:28]}; 43 | 5'h05 : r_dout = {din[26:0],din[31:27]}; 44 | 5'h06 : r_dout = {din[25:0],din[31:26]}; 45 | 5'h07 : r_dout = {din[24:0],din[31:25]}; 46 | 5'h08 : r_dout = {din[23:0],din[31:24]}; 47 | 5'h09 : r_dout = {din[22:0],din[31:23]}; 48 | 5'h0a : r_dout = {din[21:0],din[31:22]}; 49 | 5'h0b : r_dout = {din[20:0],din[31:21]}; 50 | 5'h0c : r_dout = {din[19:0],din[31:20]}; 51 | 5'h0d : r_dout = {din[18:0],din[31:19]}; 52 | 5'h0e : r_dout = {din[17:0],din[31:18]}; 53 | 5'h0f : r_dout = {din[16:0],din[31:17]}; 54 | 5'h10 : r_dout = {din[15:0],din[31:16]}; 55 | 5'h11 : r_dout = {din[14:0],din[31:15]}; 56 | 5'h12 : r_dout = {din[13:0],din[31:14]}; 57 | 5'h13 : r_dout = {din[12:0],din[31:13]}; 58 | 5'h14 : r_dout = {din[11:0],din[31:12]}; 59 | 5'h15 : r_dout = {din[10:0],din[31:11]}; 60 | 5'h16 : r_dout = {din[ 9:0],din[31:10]}; 61 | 5'h17 : r_dout = {din[ 8:0],din[31: 9]}; 62 | 5'h18 : r_dout = {din[ 7:0],din[31: 8]}; 63 | 5'h19 : r_dout = {din[ 6:0],din[31: 7]}; 64 | 5'h1a : r_dout = {din[ 5:0],din[31: 6]}; 65 | 5'h1b : r_dout = {din[ 4:0],din[31: 5]}; 66 | 5'h1c : r_dout = {din[ 3:0],din[31: 4]}; 67 | 5'h1d : r_dout = {din[ 2:0],din[31: 3]}; 68 | 5'h1e : r_dout = {din[ 1:0],din[31: 2]}; 69 | 5'h1f : r_dout = {din[0] ,din[31: 1]}; 70 | endcase 71 | end 72 | 73 | endmodule 74 | -------------------------------------------------------------------------------- /ciphers/rc6/rc6_rol.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : rc6_rol.v 19 | // Function : RC6 Cryptographic Algorithm Core ROL 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module rc6_rol( 30 | input [4:0] round, 31 | input [31:0] din, 32 | output [31:0] dout 33 | ); 34 | reg [31:0] r_dout; 35 | assign dout = r_dout; 36 | always@(round or din) begin 37 | case(round) 38 | 5'h00 : r_dout = din; 39 | 5'h01 : r_dout = {din[30:0],din[31] }; 40 | 5'h02 : r_dout = {din[29:0],din[31:30]}; 41 | 5'h03 : r_dout = {din[28:0],din[31:29]}; 42 | 5'h04 : r_dout = {din[27:0],din[31:28]}; 43 | 5'h05 : r_dout = {din[26:0],din[31:27]}; 44 | 5'h06 : r_dout = {din[25:0],din[31:26]}; 45 | 5'h07 : r_dout = {din[24:0],din[31:25]}; 46 | 5'h08 : r_dout = {din[23:0],din[31:24]}; 47 | 5'h09 : r_dout = {din[22:0],din[31:23]}; 48 | 5'h0a : r_dout = {din[21:0],din[31:22]}; 49 | 5'h0b : r_dout = {din[20:0],din[31:21]}; 50 | 5'h0c : r_dout = {din[19:0],din[31:20]}; 51 | 5'h0d : r_dout = {din[18:0],din[31:19]}; 52 | 5'h0e : r_dout = {din[17:0],din[31:18]}; 53 | 5'h0f : r_dout = {din[16:0],din[31:17]}; 54 | 5'h10 : r_dout = {din[15:0],din[31:16]}; 55 | 5'h11 : r_dout = {din[14:0],din[31:15]}; 56 | 5'h12 : r_dout = {din[13:0],din[31:14]}; 57 | 5'h13 : r_dout = {din[12:0],din[31:13]}; 58 | 5'h14 : r_dout = {din[11:0],din[31:12]}; 59 | 5'h15 : r_dout = {din[10:0],din[31:11]}; 60 | 5'h16 : r_dout = {din[ 9:0],din[31:10]}; 61 | 5'h17 : r_dout = {din[ 8:0],din[31: 9]}; 62 | 5'h18 : r_dout = {din[ 7:0],din[31: 8]}; 63 | 5'h19 : r_dout = {din[ 6:0],din[31: 7]}; 64 | 5'h1a : r_dout = {din[ 5:0],din[31: 6]}; 65 | 5'h1b : r_dout = {din[ 4:0],din[31: 5]}; 66 | 5'h1c : r_dout = {din[ 3:0],din[31: 4]}; 67 | 5'h1d : r_dout = {din[ 2:0],din[31: 3]}; 68 | 5'h1e : r_dout = {din[ 1:0],din[31: 2]}; 69 | 5'h1f : r_dout = {din[0] ,din[31: 1]}; 70 | endcase 71 | end 72 | 73 | endmodule 74 | -------------------------------------------------------------------------------- /ciphers/cast5/cast5_rol.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : cast5_rol.v 19 | // Function : CAST5 Cryptographic Algorithm Core ROL 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-3 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module cast5_rol( 30 | input [4:0] round, 31 | input [31:0] din, 32 | output [31:0] dout 33 | ); 34 | reg [31:0] r_dout; 35 | assign dout = r_dout; 36 | always@(round or din) begin 37 | case(round) 38 | 5'h00 : r_dout = din; 39 | 5'h01 : r_dout = {din[30:0],din[31] }; 40 | 5'h02 : r_dout = {din[29:0],din[31:30]}; 41 | 5'h03 : r_dout = {din[28:0],din[31:29]}; 42 | 5'h04 : r_dout = {din[27:0],din[31:28]}; 43 | 5'h05 : r_dout = {din[26:0],din[31:27]}; 44 | 5'h06 : r_dout = {din[25:0],din[31:26]}; 45 | 5'h07 : r_dout = {din[24:0],din[31:25]}; 46 | 5'h08 : r_dout = {din[23:0],din[31:24]}; 47 | 5'h09 : r_dout = {din[22:0],din[31:23]}; 48 | 5'h0a : r_dout = {din[21:0],din[31:22]}; 49 | 5'h0b : r_dout = {din[20:0],din[31:21]}; 50 | 5'h0c : r_dout = {din[19:0],din[31:20]}; 51 | 5'h0d : r_dout = {din[18:0],din[31:19]}; 52 | 5'h0e : r_dout = {din[17:0],din[31:18]}; 53 | 5'h0f : r_dout = {din[16:0],din[31:17]}; 54 | 5'h10 : r_dout = {din[15:0],din[31:16]}; 55 | 5'h11 : r_dout = {din[14:0],din[31:15]}; 56 | 5'h12 : r_dout = {din[13:0],din[31:14]}; 57 | 5'h13 : r_dout = {din[12:0],din[31:13]}; 58 | 5'h14 : r_dout = {din[11:0],din[31:12]}; 59 | 5'h15 : r_dout = {din[10:0],din[31:11]}; 60 | 5'h16 : r_dout = {din[ 9:0],din[31:10]}; 61 | 5'h17 : r_dout = {din[ 8:0],din[31: 9]}; 62 | 5'h18 : r_dout = {din[ 7:0],din[31: 8]}; 63 | 5'h19 : r_dout = {din[ 6:0],din[31: 7]}; 64 | 5'h1a : r_dout = {din[ 5:0],din[31: 6]}; 65 | 5'h1b : r_dout = {din[ 4:0],din[31: 5]}; 66 | 5'h1c : r_dout = {din[ 3:0],din[31: 4]}; 67 | 5'h1d : r_dout = {din[ 2:0],din[31: 3]}; 68 | 5'h1e : r_dout = {din[ 1:0],din[31: 2]}; 69 | 5'h1f : r_dout = {din[0] ,din[31: 1]}; 70 | endcase 71 | end 72 | 73 | endmodule 74 | -------------------------------------------------------------------------------- /ciphers/xtea/xtea_keyex.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : xtea_keyex.v 19 | // Function : XTEA Cryptographic Algorithm Core Caculate Round KEY 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-23 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module xtea_keyex( 28 | input i_clk, 29 | input i_rst, 30 | input [127:0] i_key, 31 | input i_key_en, 32 | output [1023:0] o_exkey_a, 33 | output [1023:0] o_exkey_b, 34 | output o_key_ok 35 | ); 36 | 37 | localparam DLY = 1; 38 | 39 | wire s_busy; 40 | wire [31:0] s_sum; 41 | wire [31:0] s_exka; 42 | wire [31:0] s_exkb; 43 | 44 | reg [31:0] r_sum; 45 | reg [1023:0] r_exkey_a; 46 | reg [1023:0] r_exkey_b; 47 | reg [4:0] r_count; 48 | reg r_key_ok; 49 | 50 | function [31:0] WS; 51 | input [127:0] D; 52 | input [1:0] S; 53 | begin 54 | WS = (S==2'd0) ? D[127:96]: 55 | ((S==2'd1) ? D[95:64]: 56 | ((S==2'd2) ? D[63:32]: 57 | ((S==2'd3) ? D[31:0]:32'b0))); 58 | end 59 | endfunction 60 | 61 | //assign s_key = {SWAP(i_key[127:96]),SWAP(i_key[95:64]),SWAP(i_key[63:32]),SWAP(i_key[31:0])}; 62 | assign s_sum = i_key_en ? 32'h9E37_79B9:(r_sum + 32'h9E37_79B9); 63 | 64 | assign s_exka = i_key_en ? WS(i_key,2'b0):(r_sum + WS(i_key,r_sum[1:0])); 65 | assign s_exkb = s_sum + WS(i_key,s_sum[12:11]); 66 | 67 | always@(posedge i_clk or posedge i_rst) begin 68 | if(i_rst) 69 | r_sum <= #DLY 32'b0; 70 | else if(s_busy) 71 | r_sum <= #DLY s_sum; 72 | end 73 | 74 | always@(posedge i_clk or posedge i_rst) begin 75 | if(i_rst) begin 76 | r_exkey_a <= #DLY 1024'b0; 77 | end else if(s_busy)begin 78 | r_exkey_a <= #DLY {r_exkey_a[991:0],s_exka}; 79 | end 80 | end 81 | 82 | always@(posedge i_clk or posedge i_rst) begin 83 | if(i_rst) begin 84 | r_exkey_b <= #DLY 1024'b0; 85 | end else if(s_busy)begin 86 | r_exkey_b <= #DLY {r_exkey_b[991:0],s_exkb}; 87 | end 88 | end 89 | 90 | always@(posedge i_clk or posedge i_rst) begin 91 | if(i_rst) 92 | r_count <= #DLY 5'd0; 93 | else if(r_count!=6'd0) 94 | r_count <= #DLY r_count + 5'b1; 95 | else if(i_key_en) 96 | r_count <= #DLY 5'b1; 97 | end 98 | 99 | assign o_exkey_a = r_exkey_a; 100 | assign o_exkey_b = r_exkey_b; 101 | 102 | assign s_busy = ((r_count!=5'd0)||(i_key_en==1'b1)) ? 1'b1 : 1'b0; 103 | 104 | always@(posedge i_clk or posedge i_rst) begin 105 | if(i_rst) 106 | r_key_ok <= #DLY 1'b0; 107 | else if(r_count=='d31) 108 | r_key_ok <= #DLY 1'b1; 109 | else if(i_key_en==1'b1) 110 | r_key_ok <= #DLY 1'b0; 111 | end 112 | 113 | assign o_key_ok = r_key_ok&(~i_key_en); 114 | 115 | endmodule -------------------------------------------------------------------------------- /hashes/sha2/tb_sha256_core.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: Xie(xiejianjiang@126.com) 5 | // 6 | // Create Date: 2019/01/05 06:16:40 7 | // Design Name: 8 | // Module Name: tb_sm3_core 9 | // Project Name: 10 | // Target Devices: 11 | // Tool Versions: 12 | // Description: SHA1 Cryptographic Hash Algorithm Simulate File 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | module tb_sha256_core(); 24 | 25 | reg r_clk; 26 | reg r_rst; 27 | reg r_start; 28 | reg [511:0] r_data; 29 | reg [255:0] r_vin; 30 | wire [255:0] s_vout; 31 | wire s_done; 32 | //SHA1("abc") = { "abc", 33 | // "0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 34 | // 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 35 | // 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 36 | // 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad " 37 | reg [255:0] INIT = {32'h6A09E667,32'hBB67AE85,32'h3C6EF372,32'hA54FF53A, 38 | 32'h510E527F,32'h9B05688C,32'h1F83D9AB,32'h5BE0CD19}; 39 | reg [511:0] DATA1 = {32'h61626380,416'h0,32'h0,32'h00000018}; 40 | //SHA256("123456789012345678901234567890123456789012345678901234567890123456 41 | //78901234567890") = 42 | // "f3,71,bc,4a,31,1f,2b,00, 43 | // 9e,ef,95,2d,d8,3c,a8,0e, 44 | // 2b,60,02,6c,8e,93,55,92, 45 | // d0,f9,c3,08,45,3c,81,3e" 46 | reg [511:0] DATA2_1 = {80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 47 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 48 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 49 | 80'h3132_3334_3536_3738_3930,32'h3132_3334}; 50 | reg [511:0] DATA2_2 = {48'h3536_3738_3930,80'h3132_3334_3536_3738_3930, 51 | 48'h8000_0000_0000,272'h0,64'h0000_0000_0000_0280}; 52 | 53 | sha256_core uut( 54 | .i_clk (r_clk), 55 | .i_rst (r_rst), 56 | .i_start (r_start), 57 | .i_data (r_data), 58 | .i_vin (r_vin), 59 | .o_vout (s_vout), 60 | .o_done (s_done)); 61 | 62 | initial begin 63 | r_clk = 0; 64 | forever #5 r_clk = ~r_clk; 65 | end 66 | 67 | initial begin 68 | r_rst = 1'b1; 69 | r_start = 1'b0; 70 | r_vin = 256'b0; 71 | r_data = 512'b0; 72 | repeat(50) @(posedge r_clk); 73 | r_rst = 1'b0; 74 | 75 | ////test data 1 76 | repeat(50) @(posedge r_clk); 77 | r_start = 1'b1; 78 | r_vin = INIT; //init 79 | r_data = DATA1; 80 | $display("vin=0x%x",r_vin); 81 | $display("data=0x%x",r_data); 82 | @(posedge r_clk); 83 | r_start = 1'b0; 84 | wait(s_done); 85 | $display("vout=0x%x",s_vout); 86 | 87 | /////test data 2 88 | repeat(50) @(posedge r_clk); 89 | r_start = 1'b1; 90 | r_vin = INIT; //init 91 | r_data = DATA2_1; 92 | //$display("vin=0x%x",r_vin); 93 | //$display("data=0x%x",r_data); 94 | @(posedge r_clk); 95 | r_start = 1'b0; 96 | wait(s_done); 97 | $display("vout=0x%x",s_vout); 98 | r_vin = s_vout; 99 | @(posedge r_clk); 100 | r_start = 1'b1; 101 | r_data= DATA2_2; 102 | @(posedge r_clk); 103 | r_start = 1'b0; 104 | wait(s_done); 105 | $display("vout=0x%x",s_vout); 106 | 107 | /////stop 108 | repeat(50) @(posedge r_clk); 109 | $stop; 110 | end 111 | 112 | endmodule 113 | -------------------------------------------------------------------------------- /ciphers/aes/aes128_keyex.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : aes128_keyex.v 19 | // Function : AES-128 Cryptographic Algorithm Core Caculate Round KEY 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module aes128_keyex( 28 | input i_clk, 29 | input i_rst, 30 | input [127:0] i_key, //key 31 | input i_key_en, //key init flag 32 | output [128*11-1:0] o_exkey, //round key 33 | output o_key_ok, //key init ok 34 | output o_sbox_use, 35 | output [31:0] o_sbox_din, 36 | input [31:0] i_sbox_dout 37 | ); 38 | 39 | localparam DLY = 1; 40 | 41 | wire [127:0] s_key; 42 | reg [127:0] r_key; 43 | reg [1279:0] r_exkey; 44 | reg [3:0] r_count; 45 | reg r_key_ok; 46 | wire s_busy; 47 | wire [127:0] s_exk; 48 | reg [31:0] r_rcon; 49 | 50 | //round left shift 51 | function [31:0] ROL; 52 | input [31:0] D; 53 | begin 54 | ROL = {D[23:0],D[31:24]}; 55 | end 56 | endfunction; 57 | 58 | always@(*) begin 59 | case(r_count) 60 | 4'd0: r_rcon = 32'h01000000; 61 | 4'd1: r_rcon = 32'h02000000; 62 | 4'd2: r_rcon = 32'h04000000; 63 | 4'd3: r_rcon = 32'h08000000; 64 | 4'd4: r_rcon = 32'h10000000; 65 | 4'd5: r_rcon = 32'h20000000; 66 | 4'd6: r_rcon = 32'h40000000; 67 | 4'd7: r_rcon = 32'h80000000; 68 | 4'd8: r_rcon = 32'h1B000000; 69 | 4'd9: r_rcon = 32'h36000000; 70 | default: r_rcon = 32'b0; 71 | endcase 72 | end 73 | 74 | assign s_key = i_key_en ? i_key : r_key; 75 | //left shift 1|2 bits 76 | assign o_sbox_use = s_busy; 77 | assign o_sbox_din = ROL(s_key[31:0]); 78 | // 79 | assign s_exk[127:96] = s_key[127:96]^i_sbox_dout^r_rcon; 80 | assign s_exk[95:64] = s_key[95:64]^s_exk[127:96]; 81 | assign s_exk[63:32] = s_key[63:32]^s_exk[95:64]; 82 | assign s_exk[31:0] = s_key[31:0]^s_exk[63:32]; 83 | 84 | always@(posedge i_clk or posedge i_rst) begin 85 | if(i_rst) 86 | r_key <= #DLY 128'b0; 87 | else if(s_busy) 88 | r_key <= #DLY s_exk; 89 | end 90 | 91 | always@(posedge i_clk or posedge i_rst) begin 92 | if(i_rst) begin 93 | r_exkey <= #DLY 1280'b0; 94 | end else if(s_busy)begin 95 | r_exkey <= #DLY {r_exkey[128*9-1:0],s_exk}; 96 | end 97 | end 98 | 99 | always@(posedge i_clk or posedge i_rst) begin 100 | if(i_rst) 101 | r_count <= #DLY 4'd0; 102 | else if(i_key_en) 103 | r_count <= #DLY 4'd1; 104 | else if(r_count ==4'd9) 105 | r_count <= #DLY 4'd0; 106 | else if(r_count!=4'd0) 107 | r_count <= #DLY r_count + 4'd1; 108 | end 109 | 110 | assign s_busy = ((r_count!=5'd0)||(i_key_en==1'b1)) ? 1'b1 : 1'b0; 111 | 112 | always@(posedge i_clk or posedge i_rst) begin 113 | if(i_rst) 114 | r_key_ok <= #DLY 1'b0; 115 | else if(r_count==4'd9) 116 | r_key_ok <= #DLY 1'b1; 117 | else if(i_key_en==1'b1) 118 | r_key_ok <= #DLY 1'b0; 119 | end 120 | 121 | assign o_key_ok = r_key_ok&(~i_key_en); 122 | assign o_exkey = {i_key,r_exkey}; 123 | 124 | endmodule 125 | -------------------------------------------------------------------------------- /hashes/sha1/tb_sha1_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sha1_core.v 19 | // Function : SHA1 Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sha1_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [511:0] r_data; 35 | reg [159:0] r_vin; 36 | wire [159:0] s_vout; 37 | wire s_done; 38 | //SHA1("abc") = "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D" 39 | reg [159:0] INIT = {32'h67452301,32'hEFCDAB89,32'h98BADCFE,32'h10325476,32'hC3D2E1F0}; 40 | reg [511:0] DATA1 = {32'h61626380,416'h0,32'h0,32'h00000018}; 41 | //MD4 ("123456789012345678901234567890123456789012345678901234567890123456 42 | //78901234567890") = "50,ab,f5,70,6a,15,09,90,a0,8b,2c,5e,a4,0f,a0,e5,85,55,47,32“ 43 | reg [511:0] DATA2_1 = {80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 44 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 45 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 46 | 80'h3132_3334_3536_3738_3930,32'h3132_3334}; 47 | reg [511:0] DATA2_2 = {48'h3536_3738_3930,80'h3132_3334_3536_3738_3930, 48 | 48'h8000_0000_0000,272'h0,64'h0000_0000_0000_0280}; 49 | 50 | sha1_core uut( 51 | .i_clk (r_clk), 52 | .i_rst (r_rst), 53 | .i_start (r_start), 54 | .i_data (r_data), 55 | .i_vin (r_vin), 56 | .o_vout (s_vout), 57 | .o_done (s_done)); 58 | 59 | initial begin 60 | r_clk = 0; 61 | forever #5 r_clk = ~r_clk; 62 | end 63 | 64 | initial begin 65 | r_rst = 1'b1; 66 | r_start = 1'b0; 67 | r_vin = 256'b0; 68 | r_data = 512'b0; 69 | repeat(50) @(posedge r_clk); 70 | r_rst = 1'b0; 71 | ////test data 1 72 | repeat(50) @(posedge r_clk); 73 | r_start = 1'b1; 74 | r_vin = INIT; //init 75 | r_data = DATA1; 76 | $display("vin=0x%x",r_vin); 77 | $display("data=0x%x",r_data); 78 | @(posedge r_clk); 79 | r_start = 1'b0; 80 | wait(s_done); 81 | $display("vout=0x%x",s_vout); 82 | 83 | /////test data 2 84 | repeat(50) @(posedge r_clk); 85 | r_start = 1'b1; 86 | r_vin = INIT; //init 87 | r_data = DATA2_1; 88 | //$display("vin=0x%x",r_vin); 89 | //$display("data=0x%x",r_data); 90 | @(posedge r_clk); 91 | r_start = 1'b0; 92 | wait(s_done); 93 | $display("vout=0x%x",s_vout); 94 | r_vin = s_vout; 95 | @(posedge r_clk); 96 | r_start = 1'b1; 97 | r_data= DATA2_2; 98 | @(posedge r_clk); 99 | r_start = 1'b0; 100 | wait(s_done); 101 | $display("vout=0x%x",s_vout); 102 | /////stop 103 | repeat(50) @(posedge r_clk); 104 | $stop; 105 | end 106 | 107 | endmodule 108 | -------------------------------------------------------------------------------- /ciphers/des/tb_des_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_des_core.v 19 | // Function : DES Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_des_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [63:0] r_key; 36 | reg r_din_en; 37 | reg [63:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [63:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [63:0] KEY1 = {32'hAABB_0918,32'h2736_CCDD}; 49 | reg [63:0] PT1 = {32'h1234_56AB,32'hCD13_2536}; 50 | reg [63:0] CT1 = {32'hC0B7_A8D0,32'h5F3A_829C}; 51 | 52 | des_core uut( 53 | .i_clk (r_clk ), 54 | .i_rst (r_rst ), 55 | .i_flag (r_flag ), //1-encrypt,0-decrypt 56 | .i_key (r_key ), 57 | .i_key_en (r_key_en ), 58 | .i_din (r_din ), 59 | .i_din_en (r_din_en ), 60 | .o_dout (s_dout ), 61 | .o_dout_en (s_dout_en ), 62 | .o_key_ok (s_key_ok ) 63 | ); 64 | 65 | initial begin 66 | r_clk = 0; 67 | forever #5 r_clk = ~r_clk; 68 | end 69 | 70 | always@(posedge r_clk or posedge r_rst) begin 71 | if(r_rst) begin 72 | r_count <= #DLY 3'd0; 73 | r_flag <= #DLY 1'b0; 74 | r_din_en <= #DLY 1'b0; 75 | r_din <= #DLY 'b0; 76 | r_key_en <= #DLY 1'b0; 77 | r_key <= #DLY 'b0; 78 | r_err <= #DLY 'b0; 79 | r_state <= #DLY 2'b0; 80 | end else begin 81 | case(r_state) 82 | 2'd0: begin 83 | if(r_test) begin 84 | r_key_en <= #DLY 1'b1; 85 | r_key <= #DLY KEY1; 86 | r_state <= #DLY 2'd1; 87 | end 88 | end 89 | 2'd1: begin 90 | r_key_en <= #DLY 1'b0; 91 | if(s_key_ok) begin 92 | r_din_en <= #DLY 1'b1; 93 | r_flag <= #DLY 1'b1; 94 | r_din <= #DLY PT1; 95 | r_state <= #DLY 2'd2; 96 | end 97 | end 98 | 2'd2: begin 99 | r_din_en <= #DLY 1'b0; 100 | if(s_dout_en) begin 101 | if(s_dout!=CT1) 102 | r_err <= #DLY r_err + 1'b1; 103 | r_din_en <= #DLY 1'b1; 104 | r_din <= #DLY CT1; 105 | r_flag <= #DLY 1'b0; 106 | r_state <= #DLY 2'd3; 107 | end 108 | end 109 | 2'd3: begin 110 | r_din_en <= #DLY 1'b0; 111 | if(s_dout_en) begin 112 | if(s_dout!=PT1) 113 | r_err <= #DLY r_err + 1'b1; 114 | r_count <= #DLY r_count + 1'b1; 115 | if(r_count == 'd7) 116 | r_state <= #DLY 2'd0; 117 | else 118 | r_state <= #DLY 2'd1; 119 | end 120 | end 121 | endcase 122 | end 123 | 124 | end 125 | 126 | initial begin 127 | r_rst = 1'b1; 128 | r_test = 1'b0; 129 | repeat(50) @(negedge r_clk); 130 | r_rst = 1'b0; 131 | repeat(10) @(negedge r_clk); 132 | r_test = 1'b1; 133 | repeat(5000) @(negedge r_clk); 134 | end 135 | 136 | endmodule 137 | -------------------------------------------------------------------------------- /ciphers/aes/aes192_keyex.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : aes192_keyex.v 19 | // Function : AES-192 Cryptographic Algorithm Core Caculate Round KEY 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-4-19 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module aes192_keyex( 30 | input i_clk, 31 | input i_rst, 32 | input [191:0] i_key, //key 33 | input i_key_en, //key init flag 34 | output [128*13-1:0] o_exkey, //round key(52 words) 35 | output o_key_ok, //key init ok 36 | output o_sbox_use, 37 | output [31:0] o_sbox_din, 38 | input [31:0] i_sbox_dout 39 | ); 40 | 41 | localparam DLY = 1; 42 | 43 | wire [191:0] s_key; 44 | reg [191:0] r_key; 45 | reg [1535:0] r_exkey; //192*8 46 | reg [3:0] r_count; 47 | reg r_key_ok; 48 | wire s_busy; 49 | wire [191:0] s_exk; 50 | reg [31:0] r_rcon; 51 | 52 | //round left shift 53 | function [31:0] ROL; 54 | input [31:0] D; 55 | begin 56 | ROL = {D[23:0],D[31:24]}; 57 | end 58 | endfunction 59 | 60 | always@(*) begin 61 | case(r_count) 62 | 4'd0: r_rcon = 32'h01000000; 63 | 4'd1: r_rcon = 32'h02000000; 64 | 4'd2: r_rcon = 32'h04000000; 65 | 4'd3: r_rcon = 32'h08000000; 66 | 4'd4: r_rcon = 32'h10000000; 67 | 4'd5: r_rcon = 32'h20000000; 68 | 4'd6: r_rcon = 32'h40000000; 69 | 4'd7: r_rcon = 32'h80000000; 70 | 4'd8: r_rcon = 32'h1B000000; 71 | 4'd9: r_rcon = 32'h36000000; 72 | default: r_rcon = 32'b0; 73 | endcase 74 | end 75 | 76 | assign s_key = i_key_en ? i_key : r_key; 77 | //left shift 1|2 bits 78 | assign o_sbox_use = s_busy; 79 | assign o_sbox_din = ROL(s_key[31:0]); 80 | // 81 | assign s_exk[191:160] = s_key[191:160]^i_sbox_dout^r_rcon; 82 | assign s_exk[159:128] = s_key[159:128]^s_exk[191:160]; 83 | assign s_exk[127:96] = s_key[127:96]^s_exk[159:128]; 84 | assign s_exk[95:64] = s_key[95:64]^s_exk[127:96]; 85 | assign s_exk[63:32] = s_key[63:32]^s_exk[95:64]; 86 | assign s_exk[31:0] = s_key[31:0]^s_exk[63:32]; 87 | 88 | always@(posedge i_clk or posedge i_rst) begin 89 | if(i_rst) 90 | r_key <= #DLY 192'b0; 91 | else if(s_busy) 92 | r_key <= #DLY s_exk; 93 | end 94 | 95 | always@(posedge i_clk or posedge i_rst) begin 96 | if(i_rst) begin 97 | r_exkey <= #DLY 1536'b0; 98 | end else if(s_busy)begin 99 | r_exkey <= #DLY {r_exkey[192*7-1:0],s_exk}; 100 | end 101 | end 102 | 103 | always@(posedge i_clk or posedge i_rst) begin 104 | if(i_rst) 105 | r_count <= #DLY 4'd0; 106 | else if(i_key_en) 107 | r_count <= #DLY 4'd1; 108 | else if(r_count ==4'd7) 109 | r_count <= #DLY 4'd0; 110 | else if(r_count!=4'd0) 111 | r_count <= #DLY r_count + 4'd1; 112 | end 113 | 114 | assign s_busy = ((r_count!=5'd0)||(i_key_en==1'b1)) ? 1'b1 : 1'b0; 115 | 116 | always@(posedge i_clk or posedge i_rst) begin 117 | if(i_rst) 118 | r_key_ok <= #DLY 1'b0; 119 | else if(r_count==4'd7) 120 | r_key_ok <= #DLY 1'b1; 121 | else if(i_key_en==1'b1) 122 | r_key_ok <= #DLY 1'b0; 123 | end 124 | 125 | assign o_key_ok = r_key_ok&(~i_key_en); 126 | assign o_exkey = {i_key,r_exkey[1535:64]}; 127 | 128 | endmodule 129 | -------------------------------------------------------------------------------- /ciphers/sm4/tb_sm4_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sm4_core.v 19 | // Function : SM4 Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sm4_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [127:0] r_key; 36 | reg r_din_en; 37 | reg [127:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [127:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [127:0] KEY1 ={32'h76543210,32'hfedcba98,32'h89abcdef,32'h01234567}; 49 | reg [127:0] PT1 = {32'h76543210,32'hfedcba98,32'h89abcdef,32'h01234567}; 50 | reg [127:0] CT1 = {32'h536e4246,32'h86b3e94f,32'hd206965e,32'h681edf34}; 51 | 52 | sm4_core uut( 53 | .i_clk (r_clk ), 54 | .i_rst (r_rst ), 55 | .i_flag (r_flag ), //1-encrypt,0-decrypt 56 | .i_key (r_key ), 57 | .i_key_en (r_key_en ), 58 | .i_din (r_din ), 59 | .i_din_en (r_din_en ), 60 | .o_dout (s_dout ), 61 | .o_dout_en (s_dout_en ), 62 | .o_key_ok (s_key_ok ) 63 | ); 64 | 65 | initial begin 66 | r_clk = 0; 67 | forever #5 r_clk = ~r_clk; 68 | end 69 | 70 | always@(posedge r_clk or posedge r_rst) begin 71 | if(r_rst) begin 72 | r_count <= #DLY 3'd0; 73 | r_flag <= #DLY 1'b0; 74 | r_din_en <= #DLY 1'b0; 75 | r_din <= #DLY 'b0; 76 | r_key_en <= #DLY 1'b0; 77 | r_key <= #DLY 'b0; 78 | r_err <= #DLY 'b0; 79 | r_state <= #DLY 2'b0; 80 | end else begin 81 | case(r_state) 82 | 2'd0: begin 83 | if(r_test) begin 84 | r_key_en <= #DLY 1'b1; 85 | r_key <= #DLY KEY1; 86 | r_state <= #DLY 2'd1; 87 | end 88 | end 89 | 2'd1: begin 90 | r_key_en <= #DLY 1'b0; 91 | if(s_key_ok) begin 92 | r_din_en <= #DLY 1'b1; 93 | r_flag <= #DLY 1'b1; 94 | r_din <= #DLY PT1; 95 | r_state <= #DLY 2'd2; 96 | end 97 | end 98 | 2'd2: begin 99 | r_din_en <= #DLY 1'b0; 100 | if(s_dout_en) begin 101 | if(s_dout!=CT1) 102 | r_err <= #DLY r_err + 1'b1; 103 | r_din_en <= #DLY 1'b1; 104 | r_din <= #DLY CT1; 105 | r_flag <= #DLY 1'b0; 106 | r_state <= #DLY 2'd3; 107 | end 108 | end 109 | 2'd3: begin 110 | r_din_en <= #DLY 1'b0; 111 | if(s_dout_en) begin 112 | if(s_dout!=PT1) 113 | r_err <= #DLY r_err + 1'b1; 114 | r_count <= #DLY r_count + 1'b1; 115 | if(r_count == 'd7) 116 | r_state <= #DLY 2'd0; 117 | else 118 | r_state <= #DLY 2'd1; 119 | end 120 | end 121 | endcase 122 | end 123 | 124 | end 125 | 126 | initial begin 127 | r_rst = 1'b1; 128 | r_test = 1'b0; 129 | repeat(50) @(negedge r_clk); 130 | r_rst = 1'b0; 131 | repeat(10) @(negedge r_clk); 132 | r_test = 1'b1; 133 | repeat(5000) @(negedge r_clk); 134 | end 135 | 136 | endmodule 137 | -------------------------------------------------------------------------------- /ciphers/aes/tb_aes128_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_aes128_core.v 19 | // Function : AES-128 Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_aes128_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [127:0] r_key; 36 | reg r_din_en; 37 | reg [127:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [127:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [127:0] KEY1 = {32'h2475_A2B3,32'h3475_5688,32'h31E2_1200,32'h13AA_5487}; 49 | reg [127:0] PT1 = {32'h0004_1214,32'h1204_1200,32'h0C00_1311,32'h0823_1919}; 50 | reg [127:0] CT1 = {32'hBC02_8BD3,32'hE0E3_B195,32'h550D_6DF8,32'hE6F1_8241}; 51 | 52 | aes128_core uut( 53 | .i_clk (r_clk ), 54 | .i_rst (r_rst ), 55 | .i_flag (r_flag ), //1-encrypt,0-decrypt 56 | .i_key (r_key ), 57 | .i_key_en (r_key_en ), 58 | .i_din (r_din ), 59 | .i_din_en (r_din_en ), 60 | .o_dout (s_dout ), 61 | .o_dout_en (s_dout_en ), 62 | .o_key_ok (s_key_ok ) 63 | ); 64 | 65 | initial begin 66 | r_clk = 0; 67 | forever #5 r_clk = ~r_clk; 68 | end 69 | 70 | always@(posedge r_clk or posedge r_rst) begin 71 | if(r_rst) begin 72 | r_count <= #DLY 3'd0; 73 | r_flag <= #DLY 1'b0; 74 | r_din_en <= #DLY 1'b0; 75 | r_din <= #DLY 'b0; 76 | r_key_en <= #DLY 1'b0; 77 | r_key <= #DLY 'b0; 78 | r_err <= #DLY 'b0; 79 | r_state <= #DLY 2'b0; 80 | end else begin 81 | case(r_state) 82 | 2'd0: begin 83 | if(r_test) begin 84 | r_key_en <= #DLY 1'b1; 85 | r_key <= #DLY KEY1; 86 | r_state <= #DLY 2'd1; 87 | end 88 | end 89 | 2'd1: begin 90 | r_key_en <= #DLY 1'b0; 91 | if(s_key_ok) begin 92 | r_din_en <= #DLY 1'b1; 93 | r_flag <= #DLY 1'b1; 94 | r_din <= #DLY PT1; 95 | r_state <= #DLY 2'd2; 96 | end 97 | end 98 | 2'd2: begin 99 | r_din_en <= #DLY 1'b0; 100 | if(s_dout_en) begin 101 | if(s_dout!=CT1) 102 | r_err <= #DLY r_err + 1'b1; 103 | r_din_en <= #DLY 1'b1; 104 | r_din <= #DLY CT1; 105 | r_flag <= #DLY 1'b0; 106 | r_state <= #DLY 2'd3; 107 | end 108 | end 109 | 2'd3: begin 110 | r_din_en <= #DLY 1'b0; 111 | if(s_dout_en) begin 112 | if(s_dout!=PT1) 113 | r_err <= #DLY r_err + 1'b1; 114 | r_count <= #DLY r_count + 1'b1; 115 | if(r_count == 'd7) 116 | r_state <= #DLY 2'd0; 117 | else 118 | r_state <= #DLY 2'd1; 119 | end 120 | end 121 | endcase 122 | end 123 | 124 | end 125 | 126 | initial begin 127 | r_rst = 1'b1; 128 | r_test = 1'b0; 129 | repeat(50) @(negedge r_clk); 130 | r_rst = 1'b0; 131 | repeat(10) @(negedge r_clk); 132 | r_test = 1'b1; 133 | repeat(5000) @(negedge r_clk); 134 | end 135 | 136 | endmodule 137 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox1.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox1.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox1( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd14; 41 | 6'h01 : r_dout = 4'd04; 42 | 6'h02 : r_dout = 4'd13; 43 | 6'h03 : r_dout = 4'd01; 44 | 6'h04 : r_dout = 4'd02; 45 | 6'h05 : r_dout = 4'd15; 46 | 6'h06 : r_dout = 4'd11; 47 | 6'h07 : r_dout = 4'd08; 48 | 6'h08 : r_dout = 4'd03; 49 | 6'h09 : r_dout = 4'd10; 50 | 6'h0a : r_dout = 4'd06; 51 | 6'h0b : r_dout = 4'd12; 52 | 6'h0c : r_dout = 4'd05; 53 | 6'h0d : r_dout = 4'd09; 54 | 6'h0e : r_dout = 4'd00; 55 | 6'h0f : r_dout = 4'd07; 56 | //line 1 57 | 6'h10 : r_dout = 4'd00; 58 | 6'h11 : r_dout = 4'd15; 59 | 6'h12 : r_dout = 4'd07; 60 | 6'h13 : r_dout = 4'd04; 61 | 6'h14 : r_dout = 4'd14; 62 | 6'h15 : r_dout = 4'd02; 63 | 6'h16 : r_dout = 4'd13; 64 | 6'h17 : r_dout = 4'd01; 65 | 6'h18 : r_dout = 4'd10; 66 | 6'h19 : r_dout = 4'd06; 67 | 6'h1a : r_dout = 4'd12; 68 | 6'h1b : r_dout = 4'd11; 69 | 6'h1c : r_dout = 4'd09; 70 | 6'h1d : r_dout = 4'd05; 71 | 6'h1e : r_dout = 4'd03; 72 | 6'h1f : r_dout = 4'd08; 73 | //line 2 74 | 6'h20 : r_dout = 4'd04; 75 | 6'h21 : r_dout = 4'd01; 76 | 6'h22 : r_dout = 4'd14; 77 | 6'h23 : r_dout = 4'd08; 78 | 6'h24 : r_dout = 4'd13; 79 | 6'h25 : r_dout = 4'd06; 80 | 6'h26 : r_dout = 4'd02; 81 | 6'h27 : r_dout = 4'd11; 82 | 6'h28 : r_dout = 4'd15; 83 | 6'h29 : r_dout = 4'd12; 84 | 6'h2a : r_dout = 4'd09; 85 | 6'h2b : r_dout = 4'd07; 86 | 6'h2c : r_dout = 4'd03; 87 | 6'h2d : r_dout = 4'd10; 88 | 6'h2e : r_dout = 4'd05; 89 | 6'h2f : r_dout = 4'd00; 90 | //line 3 91 | 6'h30 : r_dout = 4'd15; 92 | 6'h31 : r_dout = 4'd12; 93 | 6'h32 : r_dout = 4'd08; 94 | 6'h33 : r_dout = 4'd02; 95 | 6'h34 : r_dout = 4'd04; 96 | 6'h35 : r_dout = 4'd09; 97 | 6'h36 : r_dout = 4'd01; 98 | 6'h37 : r_dout = 4'd07; 99 | 6'h38 : r_dout = 4'd05; 100 | 6'h39 : r_dout = 4'd11; 101 | 6'h3a : r_dout = 4'd03; 102 | 6'h3b : r_dout = 4'd14; 103 | 6'h3c : r_dout = 4'd10; 104 | 6'h3d : r_dout = 4'd00; 105 | 6'h3e : r_dout = 4'd06; 106 | 6'h3f : r_dout = 4'd13; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox2.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox2.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox2( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd15; 41 | 6'h01 : r_dout = 4'd01; 42 | 6'h02 : r_dout = 4'd08; 43 | 6'h03 : r_dout = 4'd14; 44 | 6'h04 : r_dout = 4'd06; 45 | 6'h05 : r_dout = 4'd11; 46 | 6'h06 : r_dout = 4'd03; 47 | 6'h07 : r_dout = 4'd04; 48 | 6'h08 : r_dout = 4'd09; 49 | 6'h09 : r_dout = 4'd07; 50 | 6'h0a : r_dout = 4'd02; 51 | 6'h0b : r_dout = 4'd13; 52 | 6'h0c : r_dout = 4'd12; 53 | 6'h0d : r_dout = 4'd00; 54 | 6'h0e : r_dout = 4'd05; 55 | 6'h0f : r_dout = 4'd10; 56 | //line 1 57 | 6'h10 : r_dout = 4'd03; 58 | 6'h11 : r_dout = 4'd13; 59 | 6'h12 : r_dout = 4'd04; 60 | 6'h13 : r_dout = 4'd07; 61 | 6'h14 : r_dout = 4'd15; 62 | 6'h15 : r_dout = 4'd02; 63 | 6'h16 : r_dout = 4'd08; 64 | 6'h17 : r_dout = 4'd14; 65 | 6'h18 : r_dout = 4'd12; 66 | 6'h19 : r_dout = 4'd00; 67 | 6'h1a : r_dout = 4'd01; 68 | 6'h1b : r_dout = 4'd10; 69 | 6'h1c : r_dout = 4'd06; 70 | 6'h1d : r_dout = 4'd09; 71 | 6'h1e : r_dout = 4'd11; 72 | 6'h1f : r_dout = 4'd05; 73 | //line 2 74 | 6'h20 : r_dout = 4'd00; 75 | 6'h21 : r_dout = 4'd14; 76 | 6'h22 : r_dout = 4'd07; 77 | 6'h23 : r_dout = 4'd11; 78 | 6'h24 : r_dout = 4'd10; 79 | 6'h25 : r_dout = 4'd04; 80 | 6'h26 : r_dout = 4'd13; 81 | 6'h27 : r_dout = 4'd01; 82 | 6'h28 : r_dout = 4'd05; 83 | 6'h29 : r_dout = 4'd08; 84 | 6'h2a : r_dout = 4'd12; 85 | 6'h2b : r_dout = 4'd06; 86 | 6'h2c : r_dout = 4'd09; 87 | 6'h2d : r_dout = 4'd03; 88 | 6'h2e : r_dout = 4'd02; 89 | 6'h2f : r_dout = 4'd15; 90 | //line 3 91 | 6'h30 : r_dout = 4'd13; 92 | 6'h31 : r_dout = 4'd08; 93 | 6'h32 : r_dout = 4'd10; 94 | 6'h33 : r_dout = 4'd01; 95 | 6'h34 : r_dout = 4'd03; 96 | 6'h35 : r_dout = 4'd15; 97 | 6'h36 : r_dout = 4'd04; 98 | 6'h37 : r_dout = 4'd02; 99 | 6'h38 : r_dout = 4'd11; 100 | 6'h39 : r_dout = 4'd06; 101 | 6'h3a : r_dout = 4'd07; 102 | 6'h3b : r_dout = 4'd12; 103 | 6'h3c : r_dout = 4'd00; 104 | 6'h3d : r_dout = 4'd05; 105 | 6'h3e : r_dout = 4'd14; 106 | 6'h3f : r_dout = 4'd09; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox3.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox3.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox3( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd10; 41 | 6'h01 : r_dout = 4'd00; 42 | 6'h02 : r_dout = 4'd09; 43 | 6'h03 : r_dout = 4'd14; 44 | 6'h04 : r_dout = 4'd06; 45 | 6'h05 : r_dout = 4'd03; 46 | 6'h06 : r_dout = 4'd15; 47 | 6'h07 : r_dout = 4'd05; 48 | 6'h08 : r_dout = 4'd01; 49 | 6'h09 : r_dout = 4'd13; 50 | 6'h0a : r_dout = 4'd12; 51 | 6'h0b : r_dout = 4'd07; 52 | 6'h0c : r_dout = 4'd11; 53 | 6'h0d : r_dout = 4'd04; 54 | 6'h0e : r_dout = 4'd02; 55 | 6'h0f : r_dout = 4'd08; 56 | //line 1 57 | 6'h10 : r_dout = 4'd13; 58 | 6'h11 : r_dout = 4'd07; 59 | 6'h12 : r_dout = 4'd00; 60 | 6'h13 : r_dout = 4'd09; 61 | 6'h14 : r_dout = 4'd03; 62 | 6'h15 : r_dout = 4'd04; 63 | 6'h16 : r_dout = 4'd06; 64 | 6'h17 : r_dout = 4'd10; 65 | 6'h18 : r_dout = 4'd02; 66 | 6'h19 : r_dout = 4'd08; 67 | 6'h1a : r_dout = 4'd05; 68 | 6'h1b : r_dout = 4'd14; 69 | 6'h1c : r_dout = 4'd12; 70 | 6'h1d : r_dout = 4'd11; 71 | 6'h1e : r_dout = 4'd15; 72 | 6'h1f : r_dout = 4'd01; 73 | //line 2 74 | 6'h20 : r_dout = 4'd13; 75 | 6'h21 : r_dout = 4'd06; 76 | 6'h22 : r_dout = 4'd04; 77 | 6'h23 : r_dout = 4'd09; 78 | 6'h24 : r_dout = 4'd08; 79 | 6'h25 : r_dout = 4'd15; 80 | 6'h26 : r_dout = 4'd03; 81 | 6'h27 : r_dout = 4'd00; 82 | 6'h28 : r_dout = 4'd11; 83 | 6'h29 : r_dout = 4'd01; 84 | 6'h2a : r_dout = 4'd02; 85 | 6'h2b : r_dout = 4'd12; 86 | 6'h2c : r_dout = 4'd05; 87 | 6'h2d : r_dout = 4'd10; 88 | 6'h2e : r_dout = 4'd14; 89 | 6'h2f : r_dout = 4'd07; 90 | //line 3 91 | 6'h30 : r_dout = 4'd01; 92 | 6'h31 : r_dout = 4'd10; 93 | 6'h32 : r_dout = 4'd13; 94 | 6'h33 : r_dout = 4'd00; 95 | 6'h34 : r_dout = 4'd06; 96 | 6'h35 : r_dout = 4'd09; 97 | 6'h36 : r_dout = 4'd08; 98 | 6'h37 : r_dout = 4'd07; 99 | 6'h38 : r_dout = 4'd04; 100 | 6'h39 : r_dout = 4'd15; 101 | 6'h3a : r_dout = 4'd14; 102 | 6'h3b : r_dout = 4'd03; 103 | 6'h3c : r_dout = 4'd11; 104 | 6'h3d : r_dout = 4'd05; 105 | 6'h3e : r_dout = 4'd02; 106 | 6'h3f : r_dout = 4'd12; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox4.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox4.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox4( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd07; 41 | 6'h01 : r_dout = 4'd13; 42 | 6'h02 : r_dout = 4'd14; 43 | 6'h03 : r_dout = 4'd03; 44 | 6'h04 : r_dout = 4'd00; 45 | 6'h05 : r_dout = 4'd06; 46 | 6'h06 : r_dout = 4'd09; 47 | 6'h07 : r_dout = 4'd10; 48 | 6'h08 : r_dout = 4'd01; 49 | 6'h09 : r_dout = 4'd02; 50 | 6'h0a : r_dout = 4'd08; 51 | 6'h0b : r_dout = 4'd05; 52 | 6'h0c : r_dout = 4'd11; 53 | 6'h0d : r_dout = 4'd12; 54 | 6'h0e : r_dout = 4'd04; 55 | 6'h0f : r_dout = 4'd15; 56 | //line 1 57 | 6'h10 : r_dout = 4'd13; 58 | 6'h11 : r_dout = 4'd08; 59 | 6'h12 : r_dout = 4'd11; 60 | 6'h13 : r_dout = 4'd05; 61 | 6'h14 : r_dout = 4'd06; 62 | 6'h15 : r_dout = 4'd15; 63 | 6'h16 : r_dout = 4'd00; 64 | 6'h17 : r_dout = 4'd03; 65 | 6'h18 : r_dout = 4'd04; 66 | 6'h19 : r_dout = 4'd07; 67 | 6'h1a : r_dout = 4'd02; 68 | 6'h1b : r_dout = 4'd12; 69 | 6'h1c : r_dout = 4'd01; 70 | 6'h1d : r_dout = 4'd10; 71 | 6'h1e : r_dout = 4'd14; 72 | 6'h1f : r_dout = 4'd09; 73 | //line 2 74 | 6'h20 : r_dout = 4'd10; 75 | 6'h21 : r_dout = 4'd06; 76 | 6'h22 : r_dout = 4'd09; 77 | 6'h23 : r_dout = 4'd00; 78 | 6'h24 : r_dout = 4'd12; 79 | 6'h25 : r_dout = 4'd11; 80 | 6'h26 : r_dout = 4'd07; 81 | 6'h27 : r_dout = 4'd13; 82 | 6'h28 : r_dout = 4'd15; 83 | 6'h29 : r_dout = 4'd01; 84 | 6'h2a : r_dout = 4'd03; 85 | 6'h2b : r_dout = 4'd14; 86 | 6'h2c : r_dout = 4'd05; 87 | 6'h2d : r_dout = 4'd02; 88 | 6'h2e : r_dout = 4'd08; 89 | 6'h2f : r_dout = 4'd04; 90 | //line 3 91 | 6'h30 : r_dout = 4'd03; 92 | 6'h31 : r_dout = 4'd15; 93 | 6'h32 : r_dout = 4'd00; 94 | 6'h33 : r_dout = 4'd06; 95 | 6'h34 : r_dout = 4'd10; 96 | 6'h35 : r_dout = 4'd01; 97 | 6'h36 : r_dout = 4'd13; 98 | 6'h37 : r_dout = 4'd08; 99 | 6'h38 : r_dout = 4'd09; 100 | 6'h39 : r_dout = 4'd04; 101 | 6'h3a : r_dout = 4'd05; 102 | 6'h3b : r_dout = 4'd11; 103 | 6'h3c : r_dout = 4'd12; 104 | 6'h3d : r_dout = 4'd07; 105 | 6'h3e : r_dout = 4'd02; 106 | 6'h3f : r_dout = 4'd14; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox5.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox5.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox5( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd02; 41 | 6'h01 : r_dout = 4'd12; 42 | 6'h02 : r_dout = 4'd04; 43 | 6'h03 : r_dout = 4'd01; 44 | 6'h04 : r_dout = 4'd07; 45 | 6'h05 : r_dout = 4'd10; 46 | 6'h06 : r_dout = 4'd11; 47 | 6'h07 : r_dout = 4'd06; 48 | 6'h08 : r_dout = 4'd08; 49 | 6'h09 : r_dout = 4'd05; 50 | 6'h0a : r_dout = 4'd03; 51 | 6'h0b : r_dout = 4'd15; 52 | 6'h0c : r_dout = 4'd13; 53 | 6'h0d : r_dout = 4'd00; 54 | 6'h0e : r_dout = 4'd14; 55 | 6'h0f : r_dout = 4'd09; 56 | //line 1 57 | 6'h10 : r_dout = 4'd14; 58 | 6'h11 : r_dout = 4'd11; 59 | 6'h12 : r_dout = 4'd02; 60 | 6'h13 : r_dout = 4'd12; 61 | 6'h14 : r_dout = 4'd04; 62 | 6'h15 : r_dout = 4'd07; 63 | 6'h16 : r_dout = 4'd13; 64 | 6'h17 : r_dout = 4'd01; 65 | 6'h18 : r_dout = 4'd05; 66 | 6'h19 : r_dout = 4'd00; 67 | 6'h1a : r_dout = 4'd15; 68 | 6'h1b : r_dout = 4'd10; 69 | 6'h1c : r_dout = 4'd03; 70 | 6'h1d : r_dout = 4'd09; 71 | 6'h1e : r_dout = 4'd08; 72 | 6'h1f : r_dout = 4'd06; 73 | //line 2 74 | 6'h20 : r_dout = 4'd04; 75 | 6'h21 : r_dout = 4'd02; 76 | 6'h22 : r_dout = 4'd01; 77 | 6'h23 : r_dout = 4'd11; 78 | 6'h24 : r_dout = 4'd10; 79 | 6'h25 : r_dout = 4'd13; 80 | 6'h26 : r_dout = 4'd07; 81 | 6'h27 : r_dout = 4'd08; 82 | 6'h28 : r_dout = 4'd15; 83 | 6'h29 : r_dout = 4'd09; 84 | 6'h2a : r_dout = 4'd12; 85 | 6'h2b : r_dout = 4'd05; 86 | 6'h2c : r_dout = 4'd06; 87 | 6'h2d : r_dout = 4'd03; 88 | 6'h2e : r_dout = 4'd00; 89 | 6'h2f : r_dout = 4'd14; 90 | //line 3 91 | 6'h30 : r_dout = 4'd11; 92 | 6'h31 : r_dout = 4'd08; 93 | 6'h32 : r_dout = 4'd12; 94 | 6'h33 : r_dout = 4'd07; 95 | 6'h34 : r_dout = 4'd01; 96 | 6'h35 : r_dout = 4'd14; 97 | 6'h36 : r_dout = 4'd02; 98 | 6'h37 : r_dout = 4'd13; 99 | 6'h38 : r_dout = 4'd06; 100 | 6'h39 : r_dout = 4'd15; 101 | 6'h3a : r_dout = 4'd00; 102 | 6'h3b : r_dout = 4'd09; 103 | 6'h3c : r_dout = 4'd10; 104 | 6'h3d : r_dout = 4'd04; 105 | 6'h3e : r_dout = 4'd05; 106 | 6'h3f : r_dout = 4'd03; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox6.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox6.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox6( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd12; 41 | 6'h01 : r_dout = 4'd01; 42 | 6'h02 : r_dout = 4'd10; 43 | 6'h03 : r_dout = 4'd15; 44 | 6'h04 : r_dout = 4'd09; 45 | 6'h05 : r_dout = 4'd02; 46 | 6'h06 : r_dout = 4'd06; 47 | 6'h07 : r_dout = 4'd08; 48 | 6'h08 : r_dout = 4'd00; 49 | 6'h09 : r_dout = 4'd13; 50 | 6'h0a : r_dout = 4'd03; 51 | 6'h0b : r_dout = 4'd04; 52 | 6'h0c : r_dout = 4'd14; 53 | 6'h0d : r_dout = 4'd07; 54 | 6'h0e : r_dout = 4'd05; 55 | 6'h0f : r_dout = 4'd11; 56 | //line 1 57 | 6'h10 : r_dout = 4'd10; 58 | 6'h11 : r_dout = 4'd15; 59 | 6'h12 : r_dout = 4'd04; 60 | 6'h13 : r_dout = 4'd02; 61 | 6'h14 : r_dout = 4'd07; 62 | 6'h15 : r_dout = 4'd12; 63 | 6'h16 : r_dout = 4'd09; 64 | 6'h17 : r_dout = 4'd05; 65 | 6'h18 : r_dout = 4'd06; 66 | 6'h19 : r_dout = 4'd01; 67 | 6'h1a : r_dout = 4'd13; 68 | 6'h1b : r_dout = 4'd14; 69 | 6'h1c : r_dout = 4'd00; 70 | 6'h1d : r_dout = 4'd11; 71 | 6'h1e : r_dout = 4'd03; 72 | 6'h1f : r_dout = 4'd08; 73 | //line 2 74 | 6'h20 : r_dout = 4'd09; 75 | 6'h21 : r_dout = 4'd14; 76 | 6'h22 : r_dout = 4'd15; 77 | 6'h23 : r_dout = 4'd05; 78 | 6'h24 : r_dout = 4'd02; 79 | 6'h25 : r_dout = 4'd08; 80 | 6'h26 : r_dout = 4'd12; 81 | 6'h27 : r_dout = 4'd03; 82 | 6'h28 : r_dout = 4'd07; 83 | 6'h29 : r_dout = 4'd00; 84 | 6'h2a : r_dout = 4'd04; 85 | 6'h2b : r_dout = 4'd10; 86 | 6'h2c : r_dout = 4'd01; 87 | 6'h2d : r_dout = 4'd13; 88 | 6'h2e : r_dout = 4'd11; 89 | 6'h2f : r_dout = 4'd06; 90 | //line 3 91 | 6'h30 : r_dout = 4'd04; 92 | 6'h31 : r_dout = 4'd03; 93 | 6'h32 : r_dout = 4'd02; 94 | 6'h33 : r_dout = 4'd12; 95 | 6'h34 : r_dout = 4'd09; 96 | 6'h35 : r_dout = 4'd05; 97 | 6'h36 : r_dout = 4'd15; 98 | 6'h37 : r_dout = 4'd10; 99 | 6'h38 : r_dout = 4'd11; 100 | 6'h39 : r_dout = 4'd14; 101 | 6'h3a : r_dout = 4'd01; 102 | 6'h3b : r_dout = 4'd07; 103 | 6'h3c : r_dout = 4'd06; 104 | 6'h3d : r_dout = 4'd00; 105 | 6'h3e : r_dout = 4'd08; 106 | 6'h3f : r_dout = 4'd13; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox7.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox7.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox7( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd04; 41 | 6'h01 : r_dout = 4'd11; 42 | 6'h02 : r_dout = 4'd02; 43 | 6'h03 : r_dout = 4'd14; 44 | 6'h04 : r_dout = 4'd15; 45 | 6'h05 : r_dout = 4'd00; 46 | 6'h06 : r_dout = 4'd08; 47 | 6'h07 : r_dout = 4'd13; 48 | 6'h08 : r_dout = 4'd03; 49 | 6'h09 : r_dout = 4'd12; 50 | 6'h0a : r_dout = 4'd09; 51 | 6'h0b : r_dout = 4'd07; 52 | 6'h0c : r_dout = 4'd05; 53 | 6'h0d : r_dout = 4'd10; 54 | 6'h0e : r_dout = 4'd06; 55 | 6'h0f : r_dout = 4'd01; 56 | //line 1 57 | 6'h10 : r_dout = 4'd13; 58 | 6'h11 : r_dout = 4'd00; 59 | 6'h12 : r_dout = 4'd11; 60 | 6'h13 : r_dout = 4'd07; 61 | 6'h14 : r_dout = 4'd04; 62 | 6'h15 : r_dout = 4'd09; 63 | 6'h16 : r_dout = 4'd01; 64 | 6'h17 : r_dout = 4'd10; 65 | 6'h18 : r_dout = 4'd14; 66 | 6'h19 : r_dout = 4'd03; 67 | 6'h1a : r_dout = 4'd05; 68 | 6'h1b : r_dout = 4'd12; 69 | 6'h1c : r_dout = 4'd02; 70 | 6'h1d : r_dout = 4'd15; 71 | 6'h1e : r_dout = 4'd08; 72 | 6'h1f : r_dout = 4'd06; 73 | //line 2 74 | 6'h20 : r_dout = 4'd01; 75 | 6'h21 : r_dout = 4'd04; 76 | 6'h22 : r_dout = 4'd11; 77 | 6'h23 : r_dout = 4'd13; 78 | 6'h24 : r_dout = 4'd12; 79 | 6'h25 : r_dout = 4'd03; 80 | 6'h26 : r_dout = 4'd07; 81 | 6'h27 : r_dout = 4'd14; 82 | 6'h28 : r_dout = 4'd10; 83 | 6'h29 : r_dout = 4'd15; 84 | 6'h2a : r_dout = 4'd06; 85 | 6'h2b : r_dout = 4'd08; 86 | 6'h2c : r_dout = 4'd00; 87 | 6'h2d : r_dout = 4'd05; 88 | 6'h2e : r_dout = 4'd09; 89 | 6'h2f : r_dout = 4'd02; 90 | //line 3 91 | 6'h30 : r_dout = 4'd06; 92 | 6'h31 : r_dout = 4'd11; 93 | 6'h32 : r_dout = 4'd13; 94 | 6'h33 : r_dout = 4'd08; 95 | 6'h34 : r_dout = 4'd01; 96 | 6'h35 : r_dout = 4'd04; 97 | 6'h36 : r_dout = 4'd10; 98 | 6'h37 : r_dout = 4'd07; 99 | 6'h38 : r_dout = 4'd09; 100 | 6'h39 : r_dout = 4'd05; 101 | 6'h3a : r_dout = 4'd00; 102 | 6'h3b : r_dout = 4'd15; 103 | 6'h3c : r_dout = 4'd14; 104 | 6'h3d : r_dout = 4'd02; 105 | 6'h3e : r_dout = 4'd03; 106 | 6'h3f : r_dout = 4'd12; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/des/des_sbox8.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : des_sbox8.v 19 | // Function : DES Cryptographic Algorithm Core SBox 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-25 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module des_sbox8( 30 | input [5:0] din, 31 | output [3:0] dout 32 | ); 33 | 34 | reg [3:0] r_dout; 35 | assign dout = r_dout; 36 | // 37 | always@(din) begin 38 | case({din[5],din[0],din[4:1]}) 39 | //line 0 40 | 6'h00 : r_dout = 4'd13; 41 | 6'h01 : r_dout = 4'd02; 42 | 6'h02 : r_dout = 4'd08; 43 | 6'h03 : r_dout = 4'd04; 44 | 6'h04 : r_dout = 4'd06; 45 | 6'h05 : r_dout = 4'd15; 46 | 6'h06 : r_dout = 4'd11; 47 | 6'h07 : r_dout = 4'd01; 48 | 6'h08 : r_dout = 4'd10; 49 | 6'h09 : r_dout = 4'd09; 50 | 6'h0a : r_dout = 4'd03; 51 | 6'h0b : r_dout = 4'd14; 52 | 6'h0c : r_dout = 4'd05; 53 | 6'h0d : r_dout = 4'd00; 54 | 6'h0e : r_dout = 4'd12; 55 | 6'h0f : r_dout = 4'd07; 56 | //line 1 57 | 6'h10 : r_dout = 4'd01; 58 | 6'h11 : r_dout = 4'd15; 59 | 6'h12 : r_dout = 4'd13; 60 | 6'h13 : r_dout = 4'd08; 61 | 6'h14 : r_dout = 4'd10; 62 | 6'h15 : r_dout = 4'd03; 63 | 6'h16 : r_dout = 4'd07; 64 | 6'h17 : r_dout = 4'd04; 65 | 6'h18 : r_dout = 4'd12; 66 | 6'h19 : r_dout = 4'd05; 67 | 6'h1a : r_dout = 4'd06; 68 | 6'h1b : r_dout = 4'd11; 69 | 6'h1c : r_dout = 4'd00; 70 | 6'h1d : r_dout = 4'd14; 71 | 6'h1e : r_dout = 4'd09; 72 | 6'h1f : r_dout = 4'd02; 73 | //line 2 74 | 6'h20 : r_dout = 4'd07; 75 | 6'h21 : r_dout = 4'd11; 76 | 6'h22 : r_dout = 4'd04; 77 | 6'h23 : r_dout = 4'd01; 78 | 6'h24 : r_dout = 4'd09; 79 | 6'h25 : r_dout = 4'd12; 80 | 6'h26 : r_dout = 4'd14; 81 | 6'h27 : r_dout = 4'd02; 82 | 6'h28 : r_dout = 4'd00; 83 | 6'h29 : r_dout = 4'd06; 84 | 6'h2a : r_dout = 4'd10; 85 | 6'h2b : r_dout = 4'd13; 86 | 6'h2c : r_dout = 4'd15; 87 | 6'h2d : r_dout = 4'd03; 88 | 6'h2e : r_dout = 4'd05; 89 | 6'h2f : r_dout = 4'd08; 90 | //line 3 91 | 6'h30 : r_dout = 4'd02; 92 | 6'h31 : r_dout = 4'd01; 93 | 6'h32 : r_dout = 4'd14; 94 | 6'h33 : r_dout = 4'd07; 95 | 6'h34 : r_dout = 4'd04; 96 | 6'h35 : r_dout = 4'd10; 97 | 6'h36 : r_dout = 4'd08; 98 | 6'h37 : r_dout = 4'd13; 99 | 6'h38 : r_dout = 4'd15; 100 | 6'h39 : r_dout = 4'd12; 101 | 6'h3a : r_dout = 4'd09; 102 | 6'h3b : r_dout = 4'd00; 103 | 6'h3c : r_dout = 4'd03; 104 | 6'h3d : r_dout = 4'd05; 105 | 6'h3e : r_dout = 4'd06; 106 | 6'h3f : r_dout = 4'd11; 107 | endcase 108 | end 109 | 110 | endmodule 111 | -------------------------------------------------------------------------------- /ciphers/aes/tb_aes192_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_aes192_core.v 19 | // Function : AES-128 Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-4-19 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_aes192_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [191:0] r_key; 36 | reg r_din_en; 37 | reg [127:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [127:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [191:0] KEY1 = {32'h0001_0203,32'h0405_0607,32'h0809_0a0b,32'h0c0d_0e0f,32'h1011_1213,32'h1415_1617}; 49 | reg [127:0] PT1 = {32'h0011_2233,32'h4455_6677,32'h8899_aabb,32'hccdd_eeff}; 50 | reg [127:0] CT1 = {32'hdda9_7ca4,32'h864c_dfe0,32'h6eaf_70a0,32'hec0d_7191}; 51 | 52 | aes192_core uut( 53 | .i_clk (r_clk ), 54 | .i_rst (r_rst ), 55 | .i_flag (r_flag ), //1-encrypt,0-decrypt 56 | .i_key (r_key ), 57 | .i_key_en (r_key_en ), 58 | .i_din (r_din ), 59 | .i_din_en (r_din_en ), 60 | .o_dout (s_dout ), 61 | .o_dout_en (s_dout_en ), 62 | .o_key_ok (s_key_ok ) 63 | ); 64 | 65 | initial begin 66 | r_clk = 0; 67 | forever #5 r_clk = ~r_clk; 68 | end 69 | 70 | always@(posedge r_clk or posedge r_rst) begin 71 | if(r_rst) begin 72 | r_count <= #DLY 3'd0; 73 | r_flag <= #DLY 1'b0; 74 | r_din_en <= #DLY 1'b0; 75 | r_din <= #DLY 'b0; 76 | r_key_en <= #DLY 1'b0; 77 | r_key <= #DLY 'b0; 78 | r_err <= #DLY 'b0; 79 | r_state <= #DLY 2'b0; 80 | end else begin 81 | case(r_state) 82 | 2'd0: begin 83 | if(r_test) begin 84 | r_key_en <= #DLY 1'b1; 85 | r_key <= #DLY KEY1; 86 | r_state <= #DLY 2'd1; 87 | end 88 | end 89 | 2'd1: begin 90 | r_key_en <= #DLY 1'b0; 91 | if(s_key_ok) begin 92 | r_din_en <= #DLY 1'b1; 93 | r_flag <= #DLY 1'b1; 94 | r_din <= #DLY PT1; 95 | r_state <= #DLY 2'd2; 96 | end 97 | end 98 | 2'd2: begin 99 | r_din_en <= #DLY 1'b0; 100 | if(s_dout_en) begin 101 | if(s_dout!=CT1) 102 | r_err <= #DLY r_err + 1'b1; 103 | r_din_en <= #DLY 1'b1; 104 | r_din <= #DLY CT1; 105 | r_flag <= #DLY 1'b0; 106 | r_state <= #DLY 2'd3; 107 | end 108 | end 109 | 2'd3: begin 110 | r_din_en <= #DLY 1'b0; 111 | if(s_dout_en) begin 112 | if(s_dout!=PT1) 113 | r_err <= #DLY r_err + 1'b1; 114 | r_count <= #DLY r_count + 1'b1; 115 | if(r_count == 'd7) 116 | r_state <= #DLY 2'd0; 117 | else 118 | r_state <= #DLY 2'd1; 119 | end 120 | end 121 | endcase 122 | end 123 | 124 | end 125 | 126 | initial begin 127 | r_rst = 1'b1; 128 | r_test = 1'b0; 129 | repeat(50) @(negedge r_clk); 130 | r_rst = 1'b0; 131 | repeat(10) @(negedge r_clk); 132 | r_test = 1'b1; 133 | repeat(5000) @(negedge r_clk); 134 | $stop; 135 | end 136 | 137 | endmodule 138 | -------------------------------------------------------------------------------- /ciphers/rc5/rc5_keyex.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : rc5_keyex.v 19 | // Function : RC5 Cryptographic Algorithm Core Caculate Round KEY 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module rc5_keyex( 28 | input i_clk, 29 | input i_rst, 30 | input [127:0] i_key, //key 31 | input i_key_en, //key init flag 32 | output [32*26-1:0] o_exkey, //round key 33 | output o_key_ok //key init ok 34 | ); 35 | 36 | localparam DLY = 1; 37 | localparam [32*26-1:0] STAB = { 38 | 32'hb7e15163, 32'h5618cb1c, 32'hf45044d5, 32'h9287be8e, 39 | 32'h30bf3847, 32'hcef6b200, 32'h6d2e2bb9, 32'h0b65a572, 40 | 32'ha99d1f2b, 32'h47d498e4, 32'he60c129d, 32'h84438c56, 41 | 32'h227b060f, 32'hc0b27fc8, 32'h5ee9f981, 32'hfd21733a, 42 | 32'h9b58ecf3, 32'h399066ac, 32'hd7c7e065, 32'h75ff5a1e, 43 | 32'h1436d3d7, 32'hb26e4d90, 32'h50a5c749, 32'heedd4102, 44 | 32'h8d14babb, 32'h2b4c3474 }; 45 | 46 | wire [127:0] s_ikey; 47 | wire [31:0] s_sk; 48 | wire [31:0] s_lk; 49 | wire [31:0] s_a,s_ax; 50 | wire [31:0] s_b,s_bx; 51 | reg [127:0] r_key; 52 | reg [32*26-1:0] r_exkey; 53 | reg [6:0] r_count; 54 | reg r_key_ok; 55 | wire s_busy; 56 | wire [47:0] s_exk; 57 | wire [31:0] s_tmp; 58 | 59 | function [31:0] SWAP; 60 | input [31:0] D; 61 | begin 62 | SWAP = {D[7:0],D[15:8],D[23:16],D[31:24]}; 63 | end 64 | endfunction 65 | 66 | assign s_ikey = {SWAP(i_key[127:96]),SWAP(i_key[95:64]),SWAP(i_key[63:32]),SWAP(i_key[31:0])}; 67 | 68 | always@(posedge i_clk or posedge i_rst) begin 69 | if(i_rst) 70 | r_key <= #DLY 128'b0; 71 | else if(i_key_en) 72 | r_key <= #DLY {s_ikey[95:0],s_bx}; 73 | else if(s_busy) 74 | r_key <= #DLY {r_key[95:0],s_bx}; 75 | end 76 | 77 | always@(posedge i_clk or posedge i_rst) begin 78 | if(i_rst) 79 | r_exkey <= #DLY 832'b0; 80 | else if(i_key_en) 81 | r_exkey <= #DLY {STAB[32*25-1:0],s_ax}; 82 | else if(s_busy)begin 83 | r_exkey <= #DLY {r_exkey[32*25-1:0],s_ax}; 84 | end 85 | end 86 | 87 | assign s_a = i_key_en ? 32'b0 : r_exkey[31:0]; 88 | assign s_b = i_key_en ? 32'b0 : r_key[31:0]; 89 | assign s_lk = i_key_en ? s_ikey[127:96] : r_key[127:96]; 90 | assign s_sk = i_key_en ? STAB[32*26-1:32*25]: r_exkey[32*26-1:32*25]; 91 | assign s_tmp = s_ax+s_b; 92 | 93 | rc5_rol u_rol1(.round(5'd3),.din((s_sk + s_a + s_b)),.dout(s_ax)); //S 94 | rc5_rol u_rol2(.round(s_tmp[4:0]),.din((s_lk + s_ax + s_b)),.dout(s_bx)); //L 95 | 96 | always@(posedge i_clk or posedge i_rst) begin 97 | if(i_rst) 98 | r_count <= #DLY 7'd0; 99 | else if(i_key_en) 100 | r_count <= #DLY 7'd1; 101 | else if(r_count==7'd77) 102 | r_count <= #DLY 7'd0; 103 | else if(r_count!=7'd0) 104 | r_count <= #DLY r_count + 7'd1; 105 | end 106 | 107 | assign o_exkey = r_exkey; 108 | 109 | assign s_busy = ((r_count!=5'd0)||(i_key_en==1'b1)) ? 1'b1 : 1'b0; 110 | 111 | always@(posedge i_clk or posedge i_rst) begin 112 | if(i_rst) 113 | r_key_ok <= #DLY 1'b0; 114 | else if(r_count==7'd77) 115 | r_key_ok <= #DLY 1'b1; 116 | else if(i_key_en==1'b1) 117 | r_key_ok <= #DLY 1'b0; 118 | end 119 | 120 | assign o_key_ok = r_key_ok&(~i_key_en); 121 | 122 | endmodule 123 | -------------------------------------------------------------------------------- /ciphers/rc5/tb_rc5_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_rc5_core.v 19 | // Function : RC5 Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_rc5_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [127:0] r_key; 36 | reg r_din_en; 37 | reg [63:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [63:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [127:0] KEY1 = {8'h91, 8'h5f, 8'h46, 8'h19, 8'hbe, 8'h41, 8'hb2, 8'h51, 49 | 8'h63, 8'h55, 8'ha5, 8'h01, 8'h10, 8'ha9, 8'hce, 8'h91}; 50 | reg [63:0] PT1 = {8'h21, 8'ha5, 8'hdb, 8'hee, 8'h15, 8'h4b, 8'h8f, 8'h6d}; 51 | reg [63:0] CT1 = {8'hf7, 8'hc0, 8'h13, 8'hac, 8'h5b, 8'h2b, 8'h89, 8'h52}; 52 | 53 | rc5_core uut( 54 | .i_clk (r_clk ), 55 | .i_rst (r_rst ), 56 | .i_flag (r_flag ), //1-encrypt,0-decrypt 57 | .i_key (r_key ), 58 | .i_key_en (r_key_en ), 59 | .i_din (r_din ), 60 | .i_din_en (r_din_en ), 61 | .o_dout (s_dout ), 62 | .o_dout_en (s_dout_en ), 63 | .o_key_ok (s_key_ok ) 64 | ); 65 | 66 | initial begin 67 | r_clk = 0; 68 | forever #5 r_clk = ~r_clk; 69 | end 70 | 71 | always@(posedge r_clk or posedge r_rst) begin 72 | if(r_rst) begin 73 | r_count <= #DLY 3'd0; 74 | r_flag <= #DLY 1'b0; 75 | r_din_en <= #DLY 1'b0; 76 | r_din <= #DLY 'b0; 77 | r_key_en <= #DLY 1'b0; 78 | r_key <= #DLY 'b0; 79 | r_err <= #DLY 'b0; 80 | r_state <= #DLY 2'b0; 81 | end else begin 82 | case(r_state) 83 | 2'd0: begin 84 | if(r_test) begin 85 | r_key_en <= #DLY 1'b1; 86 | r_key <= #DLY KEY1; 87 | r_state <= #DLY 2'd1; 88 | end 89 | end 90 | 2'd1: begin 91 | r_key_en <= #DLY 1'b0; 92 | if(s_key_ok) begin 93 | r_din_en <= #DLY 1'b1; 94 | r_flag <= #DLY 1'b1; 95 | r_din <= #DLY PT1; 96 | r_state <= #DLY 2'd2; 97 | end 98 | end 99 | 2'd2: begin 100 | r_din_en <= #DLY 1'b0; 101 | if(s_dout_en) begin 102 | if(s_dout!=CT1) 103 | r_err <= #DLY r_err + 1'b1; 104 | r_din_en <= #DLY 1'b1; 105 | r_din <= #DLY CT1; 106 | r_flag <= #DLY 1'b0; 107 | r_state <= #DLY 2'd3; 108 | end 109 | end 110 | 2'd3: begin 111 | r_din_en <= #DLY 1'b0; 112 | if(s_dout_en) begin 113 | if(s_dout!=PT1) 114 | r_err <= #DLY r_err + 1'b1; 115 | r_count <= #DLY r_count + 1'b1; 116 | if(r_count == 'd7) 117 | r_state <= #DLY 2'd0; 118 | else 119 | r_state <= #DLY 2'd1; 120 | end 121 | end 122 | endcase 123 | end 124 | 125 | end 126 | 127 | initial begin 128 | r_rst = 1'b1; 129 | r_test = 1'b0; 130 | repeat(50) @(negedge r_clk); 131 | r_rst = 1'b0; 132 | repeat(10) @(negedge r_clk); 133 | r_test = 1'b1; 134 | repeat(5000) @(negedge r_clk); 135 | $stop; 136 | end 137 | 138 | endmodule 139 | -------------------------------------------------------------------------------- /ciphers/aes/tb_aes256_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_aes256_core.v 19 | // Function : AES-256 Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-4-20 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_aes256_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [255:0] r_key; 36 | reg r_din_en; 37 | reg [127:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [127:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [255:0] KEY1 = {32'h0001_0203,32'h0405_0607,32'h0809_0a0b,32'h0c0d_0e0f, 49 | 32'h1011_1213,32'h1415_1617,32'h1819_1a1b,32'h1c1d_1e1f}; 50 | reg [127:0] PT1 = {32'h0011_2233,32'h4455_6677,32'h8899_aabb,32'hccdd_eeff}; 51 | reg [127:0] CT1 = {32'h8ea2_b7ca,32'h5167_45bf,32'heafc_4990,32'h4b49_6089}; 52 | 53 | aes256_core uut( 54 | .i_clk (r_clk ), 55 | .i_rst (r_rst ), 56 | .i_flag (r_flag ), //1-encrypt,0-decrypt 57 | .i_key (r_key ), 58 | .i_key_en (r_key_en ), 59 | .i_din (r_din ), 60 | .i_din_en (r_din_en ), 61 | .o_dout (s_dout ), 62 | .o_dout_en (s_dout_en ), 63 | .o_key_ok (s_key_ok ) 64 | ); 65 | 66 | initial begin 67 | r_clk = 0; 68 | forever #5 r_clk = ~r_clk; 69 | end 70 | 71 | always@(posedge r_clk or posedge r_rst) begin 72 | if(r_rst) begin 73 | r_count <= #DLY 3'd0; 74 | r_flag <= #DLY 1'b0; 75 | r_din_en <= #DLY 1'b0; 76 | r_din <= #DLY 'b0; 77 | r_key_en <= #DLY 1'b0; 78 | r_key <= #DLY 'b0; 79 | r_err <= #DLY 'b0; 80 | r_state <= #DLY 2'b0; 81 | end else begin 82 | case(r_state) 83 | 2'd0: begin 84 | if(r_test) begin 85 | r_key_en <= #DLY 1'b1; 86 | r_key <= #DLY KEY1; 87 | r_state <= #DLY 2'd1; 88 | end 89 | end 90 | 2'd1: begin 91 | r_key_en <= #DLY 1'b0; 92 | if(s_key_ok) begin 93 | r_din_en <= #DLY 1'b1; 94 | r_flag <= #DLY 1'b1; 95 | r_din <= #DLY PT1; 96 | r_state <= #DLY 2'd2; 97 | end 98 | end 99 | 2'd2: begin 100 | r_din_en <= #DLY 1'b0; 101 | if(s_dout_en) begin 102 | if(s_dout!=CT1) 103 | r_err <= #DLY r_err + 1'b1; 104 | r_din_en <= #DLY 1'b1; 105 | r_din <= #DLY CT1; 106 | r_flag <= #DLY 1'b0; 107 | r_state <= #DLY 2'd3; 108 | end 109 | end 110 | 2'd3: begin 111 | r_din_en <= #DLY 1'b0; 112 | if(s_dout_en) begin 113 | if(s_dout!=PT1) 114 | r_err <= #DLY r_err + 1'b1; 115 | r_count <= #DLY r_count + 1'b1; 116 | if(r_count == 'd7) 117 | r_state <= #DLY 2'd0; 118 | else 119 | r_state <= #DLY 2'd1; 120 | end 121 | end 122 | endcase 123 | end 124 | 125 | end 126 | 127 | initial begin 128 | r_rst = 1'b1; 129 | r_test = 1'b0; 130 | repeat(50) @(negedge r_clk); 131 | r_rst = 1'b0; 132 | repeat(10) @(negedge r_clk); 133 | r_test = 1'b1; 134 | repeat(5000) @(negedge r_clk); 135 | $stop; 136 | end 137 | 138 | endmodule 139 | -------------------------------------------------------------------------------- /hashes/sm3/sm3_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : sm3_core.v 19 | // Function : SM3 Hash Algorithm Core 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.1 23 | // Date : 2019-4-19 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module sm3_core( 30 | input i_clk, //clock 31 | input i_rst, //reset high valid 32 | input i_start, //high valid(only one clock) 33 | input [511:0] i_data, //hash data input 34 | input [255:0] i_vin, //hash init value input(not change before o_done valid) 35 | output [255:0] o_vout, //hash value output 36 | output o_done); //high valid(only one clock) 37 | 38 | localparam DLY = 1; 39 | 40 | wire [31:0] A,B,C,D,E,F,G,H; 41 | wire [31:0] W0,W1,W2,W3,W4,W5,W6,W7,W8,W9,W10,W11,W12,W13,W14,W15; 42 | wire [31:0] W16x,W16,W0j,SS1x,SS1,SS2,TT1,TT2,Tjl; 43 | reg [31:0] r_A,r_B,r_C,r_D,r_E,r_F,r_G,r_H,r_Tjl; 44 | reg [511:0] r_W; 45 | reg [6:0] r_cnt; 46 | wire s_busy; 47 | 48 | assign {A,B,C,D,E,F,G,H} = i_start ? i_vin : {r_A,r_B,r_C,r_D,r_E,r_F,r_G,r_H}; 49 | 50 | assign {W0,W1,W2,W3,W4,W5,W6,W7} = i_start ? i_data[511:256]:r_W[511:256]; 51 | assign {W8,W9,W10,W11,W12,W13,W14,W15} = i_start ? i_data[255:0]:r_W[255:0]; 52 | 53 | assign W16x = W0^W7^{W13[16:0],W13[31:17]}; 54 | assign W16 = (W16x^{W16x[16:0],W16x[31:17]}^{W16x[8:0],W16x[31:9]})^{W3[24:0],W3[31:25]}^W10; 55 | assign W0j = W0^W4; 56 | assign Tjl = i_start ? 32'h79cc4519 : r_Tjl; 57 | assign SS1x = {A[19:0],A[31:20]} + E + Tjl; 58 | assign SS1 = {SS1x[24:0],SS1x[31:25]}; 59 | assign SS2 = SS1^{A[19:0],A[31:20]}; 60 | assign TT1 = (r_cnt<=7'd15) ? ((A^B^C)+D+SS2+W0j) : (((A&B)|(A&C)|(B&C))+D+SS2+W0j); 61 | assign TT2 = (r_cnt<=7'd15) ? ((E^F^G)+H+SS1+W0) : (((E&F)|((~E)&G))+H+SS1+W0); 62 | 63 | always@(posedge i_clk) begin 64 | if(i_rst) begin 65 | r_A <= #DLY 32'd0; 66 | r_B <= #DLY 32'd0; 67 | r_C <= #DLY 32'd0; 68 | r_D <= #DLY 32'd0; 69 | r_E <= #DLY 32'd0; 70 | r_F <= #DLY 32'd0; 71 | r_G <= #DLY 32'd0; 72 | r_H <= #DLY 32'd0; 73 | end else if(s_busy) begin 74 | r_D <= #DLY C; 75 | r_C <= #DLY {B[22:0],B[31:23]}; 76 | r_B <= #DLY A; 77 | r_A <= #DLY TT1; 78 | r_H <= #DLY G; 79 | r_G <= #DLY {F[12:0],F[31:13]}; 80 | r_F <= #DLY E; 81 | r_E <= #DLY (TT2^{TT2[22:0],TT2[31:23]}^{TT2[14:0],TT2[31:15]}); 82 | end 83 | end 84 | 85 | always@(posedge i_clk) begin 86 | if(i_rst) 87 | r_W <= #DLY 512'd0; 88 | else if(s_busy) 89 | r_W <= #DLY {W1,W2,W3,W4,W5,W6,W7,W8,W9,W10,W11,W12,W13,W14,W15,W16}; 90 | end 91 | 92 | always@(posedge i_clk) begin 93 | if(i_rst) 94 | r_cnt <= #DLY 7'd0; 95 | else if(i_start) 96 | r_cnt <= #DLY 7'd1; 97 | else if((r_cnt!=7'd0)&&(r_cnt!=7'd64)) 98 | r_cnt <= #DLY r_cnt + 7'd1; 99 | else 100 | r_cnt <= #DLY 7'd0; 101 | end 102 | 103 | always@(posedge i_clk) begin 104 | if(i_rst) 105 | r_Tjl <= #DLY 32'h0; 106 | else if(r_cnt==7'd15) 107 | r_Tjl <= #DLY 32'h9d8a7a87; //32'h7a879d8a<<16; 108 | else if(s_busy) 109 | r_Tjl <= #DLY {Tjl[30:0],Tjl[31]}; 110 | end 111 | 112 | assign s_busy = ((r_cnt!=7'd0)||(i_start==1'b1)) ? 1'b1 : 1'b0; 113 | assign o_done = (r_cnt==7'd64) ? 1'b1:1'b0; 114 | assign o_vout = (r_cnt==7'd64) ? i_vin^{A,B,C,D,E,F,G,H} : 256'b0; 115 | 116 | endmodule 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /ciphers/cast5/tb_cast5_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_cast5_core.v 19 | // Function : CAST5 Cryptographic Algorithm Core Simulate File [Keylen=16,Round=20] 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-2 24 | // Email : xcrypt@126.com 25 | // copyright : XCrypt Studio 26 | // ------------------------------------------------------------------------------ 27 | 28 | `timescale 1ns / 1ps 29 | 30 | module tb_cast5_core(); 31 | 32 | reg r_clk; 33 | reg r_rst; 34 | reg r_flag; 35 | reg r_key_en; 36 | reg [127:0] r_key; 37 | reg r_din_en; 38 | reg [63:0] r_din; 39 | reg [31:0] r_err; 40 | reg [2:0] r_count; 41 | reg r_test; 42 | wire s_dout_en; 43 | wire [63:0] s_dout; 44 | wire s_key_ok; 45 | reg [1:0] r_state; 46 | 47 | localparam DLY = 1; 48 | 49 | reg [127:0] KEY1 = {8'h01, 8'h23, 8'h45, 8'h67, 8'h12, 8'h34, 8'h56, 8'h78, 50 | 8'h23, 8'h45, 8'h67, 8'h89, 8'h34, 8'h56, 8'h78, 8'h9A}; 51 | reg [127:0] PT1 = {8'h01, 8'h23, 8'h45, 8'h67, 8'h89, 8'hAB, 8'hCD, 8'hEF}; 52 | reg [127:0] CT1 = {8'h23, 8'h8B, 8'h4F, 8'hE5, 8'h84, 8'h7E, 8'h44, 8'hB2}; 53 | 54 | cast5_core uut( 55 | .i_clk (r_clk ), 56 | .i_rst (r_rst ), 57 | .i_flag (r_flag ), //1-encrypt,0-decrypt 58 | .i_key (r_key ), 59 | .i_key_en (r_key_en ), 60 | .i_din (r_din ), 61 | .i_din_en (r_din_en ), 62 | .o_dout (s_dout ), 63 | .o_dout_en (s_dout_en ), 64 | .o_key_ok (s_key_ok ) 65 | ); 66 | 67 | initial begin 68 | r_clk = 0; 69 | forever #5 r_clk = ~r_clk; 70 | end 71 | 72 | always@(posedge r_clk or posedge r_rst) begin 73 | if(r_rst) begin 74 | r_count <= #DLY 3'd0; 75 | r_flag <= #DLY 1'b0; 76 | r_din_en <= #DLY 1'b0; 77 | r_din <= #DLY 'b0; 78 | r_key_en <= #DLY 1'b0; 79 | r_key <= #DLY 'b0; 80 | r_err <= #DLY 'b0; 81 | r_state <= #DLY 2'b0; 82 | end else begin 83 | case(r_state) 84 | 2'd0: begin 85 | if(r_test) begin 86 | r_key_en <= #DLY 1'b1; 87 | r_key <= #DLY KEY1; 88 | r_state <= #DLY 2'd1; 89 | end 90 | end 91 | 2'd1: begin 92 | r_key_en <= #DLY 1'b0; 93 | if(s_key_ok) begin 94 | r_din_en <= #DLY 1'b1; 95 | r_flag <= #DLY 1'b1; 96 | r_din <= #DLY PT1; 97 | r_state <= #DLY 2'd2; 98 | end 99 | end 100 | 2'd2: begin 101 | r_din_en <= #DLY 1'b0; 102 | if(s_dout_en) begin 103 | if(s_dout!=CT1) 104 | r_err <= #DLY r_err + 1'b1; 105 | r_din_en <= #DLY 1'b1; 106 | r_din <= #DLY CT1; 107 | r_flag <= #DLY 1'b0; 108 | r_state <= #DLY 2'd3; 109 | end 110 | end 111 | 2'd3: begin 112 | r_din_en <= #DLY 1'b0; 113 | if(s_dout_en) begin 114 | if(s_dout!=PT1) 115 | r_err <= #DLY r_err + 1'b1; 116 | r_count <= #DLY r_count + 1'b1; 117 | if(r_count == 'd7) 118 | r_state <= #DLY 2'd0; 119 | else 120 | r_state <= #DLY 2'd1; 121 | end 122 | end 123 | endcase 124 | end 125 | 126 | end 127 | 128 | initial begin 129 | r_rst = 1'b1; 130 | r_test = 1'b0; 131 | repeat(50) @(negedge r_clk); 132 | r_rst = 1'b0; 133 | repeat(10) @(negedge r_clk); 134 | r_test = 1'b1; 135 | repeat(5000) @(negedge r_clk); 136 | $stop; 137 | end 138 | 139 | endmodule 140 | -------------------------------------------------------------------------------- /ciphers/rc6/tb_rc6_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_rc6_core.v 19 | // Function : RC6 Cryptographic Algorithm Core Simulate File [Keylen=16,Round=20] 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-2 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_rc6_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [127:0] r_key; 36 | reg r_din_en; 37 | reg [127:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [127:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | reg [127:0] KEY1 = {8'h01, 8'h23, 8'h45, 8'h67, 8'h89, 8'hab, 8'hcd, 8'hef, 49 | 8'h01, 8'h12, 8'h23, 8'h34, 8'h45, 8'h56, 8'h67, 8'h78}; 50 | reg [127:0] PT1 = {8'h02, 8'h13, 8'h24, 8'h35, 8'h46, 8'h57, 8'h68, 8'h79, 51 | 8'h8a, 8'h9b, 8'hac, 8'hbd, 8'hce, 8'hdf, 8'he0, 8'hf1}; 52 | reg [127:0] CT1 = {8'h52, 8'h4e, 8'h19, 8'h2f, 8'h47, 8'h15, 8'hc6, 8'h23, 53 | 8'h1f, 8'h51, 8'hf6, 8'h36, 8'h7e, 8'ha4, 8'h3f, 8'h18}; 54 | 55 | rc6_core uut( 56 | .i_clk (r_clk ), 57 | .i_rst (r_rst ), 58 | .i_flag (r_flag ), //1-encrypt,0-decrypt 59 | .i_key (r_key ), 60 | .i_key_en (r_key_en ), 61 | .i_din (r_din ), 62 | .i_din_en (r_din_en ), 63 | .o_dout (s_dout ), 64 | .o_dout_en (s_dout_en ), 65 | .o_key_ok (s_key_ok ) 66 | ); 67 | 68 | initial begin 69 | r_clk = 0; 70 | forever #5 r_clk = ~r_clk; 71 | end 72 | 73 | always@(posedge r_clk or posedge r_rst) begin 74 | if(r_rst) begin 75 | r_count <= #DLY 3'd0; 76 | r_flag <= #DLY 1'b0; 77 | r_din_en <= #DLY 1'b0; 78 | r_din <= #DLY 'b0; 79 | r_key_en <= #DLY 1'b0; 80 | r_key <= #DLY 'b0; 81 | r_err <= #DLY 'b0; 82 | r_state <= #DLY 2'b0; 83 | end else begin 84 | case(r_state) 85 | 2'd0: begin 86 | if(r_test) begin 87 | r_key_en <= #DLY 1'b1; 88 | r_key <= #DLY KEY1; 89 | r_state <= #DLY 2'd1; 90 | end 91 | end 92 | 2'd1: begin 93 | r_key_en <= #DLY 1'b0; 94 | if(s_key_ok) begin 95 | r_din_en <= #DLY 1'b1; 96 | r_flag <= #DLY 1'b1; 97 | r_din <= #DLY PT1; 98 | r_state <= #DLY 2'd2; 99 | end 100 | end 101 | 2'd2: begin 102 | r_din_en <= #DLY 1'b0; 103 | if(s_dout_en) begin 104 | if(s_dout!=CT1) 105 | r_err <= #DLY r_err + 1'b1; 106 | r_din_en <= #DLY 1'b1; 107 | r_din <= #DLY CT1; 108 | r_flag <= #DLY 1'b0; 109 | r_state <= #DLY 2'd3; 110 | end 111 | end 112 | 2'd3: begin 113 | r_din_en <= #DLY 1'b0; 114 | if(s_dout_en) begin 115 | if(s_dout!=PT1) 116 | r_err <= #DLY r_err + 1'b1; 117 | r_count <= #DLY r_count + 1'b1; 118 | if(r_count == 'd7) 119 | r_state <= #DLY 2'd0; 120 | else 121 | r_state <= #DLY 2'd1; 122 | end 123 | end 124 | endcase 125 | end 126 | 127 | end 128 | 129 | initial begin 130 | r_rst = 1'b1; 131 | r_test = 1'b0; 132 | repeat(50) @(negedge r_clk); 133 | r_rst = 1'b0; 134 | repeat(10) @(negedge r_clk); 135 | r_test = 1'b1; 136 | repeat(5000) @(negedge r_clk); 137 | $stop; 138 | end 139 | 140 | endmodule 141 | -------------------------------------------------------------------------------- /hashes/sha2/tb_sha224_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sha224_core.v 19 | // Function : SHA256-224 Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sha224_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [511:0] r_data; 35 | reg [255:0] r_vin; 36 | wire [255:0] s_vout; 37 | wire s_done; 38 | 39 | reg [255:0] INIT = { 40 | 32'hc1059ed8,32'h367cd507,32'h3070dd17,32'hf70e5939, 41 | 32'hffc00b31,32'h68581511,32'h64f98fa7,32'hbefa4fa4}; 42 | //SHA224("abc")= 43 | // { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 44 | // 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 45 | // 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 46 | // 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 } 47 | reg [511:0] DATA1 = {32'h61626380,416'h0,32'h0,32'h00000018}; 48 | 49 | //SHA224("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 50 | // "b5,0a,ec,be,4e,9b,b0,b5, 51 | // 7b,c5,f3,ae,76,0a,8e,01, 52 | // db,24,f2,03,fb,3c,dc,d1, 53 | // 31,48,04,6e" 54 | reg [511:0] DATA2_1 = {80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 55 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 56 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 57 | 80'h3132_3334_3536_3738_3930,32'h3132_3334}; 58 | reg [511:0] DATA2_2 = {48'h3536_3738_3930,80'h3132_3334_3536_3738_3930, 59 | 48'h8000_0000_0000,272'h0,64'h0000_0000_0000_0280}; 60 | 61 | sha256_core uut( 62 | .i_clk (r_clk), 63 | .i_rst (r_rst), 64 | .i_start (r_start), 65 | .i_data (r_data), 66 | .i_vin (r_vin), 67 | .o_vout (s_vout), //hash value = s_vout[255:32]; 68 | .o_done (s_done)); 69 | 70 | initial begin 71 | r_clk = 0; 72 | forever #5 r_clk = ~r_clk; 73 | end 74 | 75 | initial begin 76 | r_rst = 1'b1; 77 | r_start = 1'b0; 78 | r_vin = 256'b0; 79 | r_data = 512'b0; 80 | repeat(50) @(posedge r_clk); 81 | r_rst = 1'b0; 82 | 83 | ////test data 1 84 | repeat(50) @(posedge r_clk); 85 | r_start = 1'b1; 86 | r_vin = INIT; //init 87 | r_data = DATA1; 88 | $display("vin=0x%x",r_vin); 89 | $display("data=0x%x",r_data); 90 | @(posedge r_clk); 91 | r_start = 1'b0; 92 | wait(s_done); 93 | $display("vout=0x%x",s_vout); 94 | 95 | /////test data 2 96 | repeat(50) @(posedge r_clk); 97 | r_start = 1'b1; 98 | r_vin = INIT; //init 99 | r_data = DATA2_1; 100 | //$display("vin=0x%x",r_vin); 101 | //$display("data=0x%x",r_data); 102 | @(posedge r_clk); 103 | r_start = 1'b0; 104 | wait(s_done); 105 | $display("vout=0x%x",s_vout); 106 | r_vin = s_vout; 107 | @(posedge r_clk); 108 | r_start = 1'b1; 109 | r_data= DATA2_2; 110 | @(posedge r_clk); 111 | r_start = 1'b0; 112 | wait(s_done); 113 | $display("vout=0x%x",s_vout); 114 | 115 | /////stop 116 | repeat(50) @(posedge r_clk); 117 | $stop; 118 | end 119 | 120 | endmodule 121 | -------------------------------------------------------------------------------- /ciphers/rc6/rc6_keyex.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : rc6_keyex.v 19 | // Function : RC6 Cryptographic Algorithm Core Cacate Round KEY(KeyLen = 16) 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module rc6_keyex( 28 | input i_clk, 29 | input i_rst, 30 | input [127:0] i_key, //key 31 | input i_key_en, //key init flag 32 | output [32*44-1:0] o_exkey, //round key 33 | output o_key_ok //key init ok 34 | ); 35 | 36 | localparam DLY = 1; 37 | localparam [32*44-1:0] STAB = { 38 | 32'hb7e15163, 32'h5618cb1c, 32'hf45044d5, 32'h9287be8e, 39 | 32'h30bf3847, 32'hcef6b200, 32'h6d2e2bb9, 32'h0b65a572, 40 | 32'ha99d1f2b, 32'h47d498e4, 32'he60c129d, 32'h84438c56, 41 | 32'h227b060f, 32'hc0b27fc8, 32'h5ee9f981, 32'hfd21733a, 42 | 32'h9b58ecf3, 32'h399066ac, 32'hd7c7e065, 32'h75ff5a1e, 43 | 32'h1436d3d7, 32'hb26e4d90, 32'h50a5c749, 32'heedd4102, 44 | 32'h8d14babb, 32'h2b4c3474, 32'hc983ae2d, 32'h67bb27e6, 45 | 32'h05f2a19f, 32'ha42a1b58, 32'h42619511, 32'he0990eca, 46 | 32'h7ed08883, 32'h1d08023c, 32'hbb3f7bf5, 32'h5976f5ae, 47 | 32'hf7ae6f67, 32'h95e5e920, 32'h341d62d9, 32'hd254dc92, 48 | 32'h708c564b, 32'h0ec3d004, 32'hacfb49bd, 32'h4b32c376}; 49 | 50 | wire [127:0] s_ikey; 51 | wire [31:0] s_sk; 52 | wire [31:0] s_lk; 53 | wire [31:0] s_a,s_ax; 54 | wire [31:0] s_b,s_bx; 55 | reg [127:0] r_key; 56 | reg [32*44-1:0] r_exkey; 57 | reg [7:0] r_count; 58 | reg r_key_ok; 59 | wire s_busy; 60 | wire [31:0] s_tmp; 61 | 62 | function [31:0] SWAP; 63 | input [31:0] D; 64 | begin 65 | SWAP = {D[7:0],D[15:8],D[23:16],D[31:24]}; 66 | end 67 | endfunction 68 | 69 | assign s_ikey = {SWAP(i_key[127:96]),SWAP(i_key[95:64]),SWAP(i_key[63:32]),SWAP(i_key[31:0])}; 70 | 71 | always@(posedge i_clk or posedge i_rst) begin 72 | if(i_rst) 73 | r_key <= #DLY 128'b0; 74 | else if(i_key_en) 75 | r_key <= #DLY {s_ikey[95:0],s_bx}; 76 | else if(s_busy) 77 | r_key <= #DLY {r_key[95:0],s_bx}; 78 | end 79 | 80 | always@(posedge i_clk or posedge i_rst) begin 81 | if(i_rst) 82 | r_exkey <= #DLY 1408'b0; 83 | else if(i_key_en) 84 | r_exkey <= #DLY {STAB[32*43-1:0],s_ax}; 85 | else if(s_busy)begin 86 | r_exkey <= #DLY {r_exkey[32*43-1:0],s_ax}; 87 | end 88 | end 89 | 90 | assign s_a = i_key_en ? 32'b0 : r_exkey[31:0]; 91 | assign s_b = i_key_en ? 32'b0 : r_key[31:0]; 92 | assign s_lk = i_key_en ? s_ikey[127:96] : r_key[127:96]; 93 | assign s_sk = i_key_en ? STAB[32*44-1:32*43]: r_exkey[32*44-1:32*43]; 94 | assign s_tmp = s_ax+s_b; 95 | 96 | rc6_rol u_rol1(.round(5'd3),.din((s_sk + s_a + s_b)),.dout(s_ax)); //S 97 | rc6_rol u_rol2(.round(s_tmp[4:0]),.din((s_lk + s_ax + s_b)),.dout(s_bx)); //L 98 | 99 | always@(posedge i_clk or posedge i_rst) begin 100 | if(i_rst) 101 | r_count <= #DLY 8'd0; 102 | else if(i_key_en) 103 | r_count <= #DLY 8'd1; 104 | else if(r_count==8'd131) 105 | r_count <= #DLY 7'd0; 106 | else if(r_count!=8'd0) 107 | r_count <= #DLY r_count + 8'd1; 108 | end 109 | 110 | assign o_exkey = r_exkey; 111 | 112 | assign s_busy = ((r_count!=8'd0)||(i_key_en==1'b1)) ? 1'b1 : 1'b0; 113 | 114 | always@(posedge i_clk or posedge i_rst) begin 115 | if(i_rst) 116 | r_key_ok <= #DLY 1'b0; 117 | else if(r_count==8'd131) 118 | r_key_ok <= #DLY 1'b1; 119 | else if(i_key_en==1'b1) 120 | r_key_ok <= #DLY 1'b0; 121 | end 122 | 123 | assign o_key_ok = r_key_ok&(~i_key_en); 124 | 125 | endmodule 126 | 127 | -------------------------------------------------------------------------------- /ciphers/aes/aes256_keyex.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : aes256_keyex.v 19 | // Function : AES-256 Cryptographic Algorithm Core Caculate Round KEY 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-4-20 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module aes256_keyex( 30 | input i_clk, 31 | input i_rst, 32 | input [255:0] i_key, //key 33 | input i_key_en, //key init flag 34 | output [128*15-1:0] o_exkey, //round key(60 words) 35 | output o_key_ok, //key init ok 36 | output o_sbox_use, 37 | output [63:0] o_sbox_din, 38 | input [63:0] i_sbox_dout 39 | ); 40 | 41 | localparam DLY = 1; 42 | 43 | wire [255:0] s_key; 44 | reg [255:0] r_key; 45 | reg [32*56-1:0] r_exkey; //56 words 46 | reg [3:0] r_count; 47 | reg r_key_ok; 48 | wire s_busy; 49 | wire [255:0] s_exk; 50 | reg [31:0] r_rcon; 51 | 52 | //round left shift 8bit 53 | function [31:0] ROL; 54 | input [31:0] D; 55 | begin 56 | ROL = {D[23:0],D[31:24]}; 57 | end 58 | endfunction 59 | 60 | //round left shift 8bit 61 | function [31:0] ROR; 62 | input [31:0] D; 63 | begin 64 | ROR = {D[7:0],D[31:8]}; 65 | end 66 | endfunction 67 | 68 | always@(*) begin 69 | case(r_count) 70 | 4'd0: r_rcon = 32'h01000000; 71 | 4'd1: r_rcon = 32'h02000000; 72 | 4'd2: r_rcon = 32'h04000000; 73 | 4'd3: r_rcon = 32'h08000000; 74 | 4'd4: r_rcon = 32'h10000000; 75 | 4'd5: r_rcon = 32'h20000000; 76 | 4'd6: r_rcon = 32'h40000000; 77 | 4'd7: r_rcon = 32'h80000000; 78 | 4'd8: r_rcon = 32'h1B000000; 79 | 4'd9: r_rcon = 32'h36000000; 80 | default: r_rcon = 32'b0; 81 | endcase 82 | end 83 | 84 | assign s_key = i_key_en ? i_key : r_key; 85 | //left shift 1|2 bits 86 | assign o_sbox_use = s_busy; 87 | assign o_sbox_din[31:0] = ROL(s_key[31:0]); 88 | assign o_sbox_din[63:32] = ROL(ROR(s_exk[159:128])); 89 | // 90 | assign s_exk[255:224] = s_key[255:224]^i_sbox_dout[31:0]^r_rcon; 91 | assign s_exk[223:192] = s_key[223:192]^s_exk[255:224]; 92 | assign s_exk[191:160] = s_key[191:160]^s_exk[223:192]; 93 | assign s_exk[159:128] = s_key[159:128]^s_exk[191:160]; 94 | 95 | assign s_exk[127:96] = s_key[127:96]^i_sbox_dout[63:32]; 96 | assign s_exk[95:64] = s_key[95:64]^s_exk[127:96]; 97 | assign s_exk[63:32] = s_key[63:32]^s_exk[95:64]; 98 | assign s_exk[31:0] = s_key[31:0]^s_exk[63:32]; 99 | 100 | 101 | always@(posedge i_clk or posedge i_rst) begin 102 | if(i_rst) 103 | r_key <= #DLY 256'b0; 104 | else if(s_busy) 105 | r_key <= #DLY s_exk; 106 | end 107 | 108 | always@(posedge i_clk or posedge i_rst) begin 109 | if(i_rst) begin 110 | r_exkey <= #DLY 1536'b0; 111 | end else if(s_busy)begin 112 | r_exkey <= #DLY {r_exkey[256*6-1:0],s_exk}; 113 | end 114 | end 115 | 116 | always@(posedge i_clk or posedge i_rst) begin 117 | if(i_rst) 118 | r_count <= #DLY 4'd0; 119 | else if(i_key_en) 120 | r_count <= #DLY 4'd1; 121 | else if(r_count ==4'd6) 122 | r_count <= #DLY 4'd0; 123 | else if(r_count!=4'd0) 124 | r_count <= #DLY r_count + 4'd1; 125 | end 126 | 127 | assign s_busy = ((r_count!=5'd0)||(i_key_en==1'b1)) ? 1'b1 : 1'b0; 128 | 129 | always@(posedge i_clk or posedge i_rst) begin 130 | if(i_rst) 131 | r_key_ok <= #DLY 1'b0; 132 | else if(r_count==4'd6) 133 | r_key_ok <= #DLY 1'b1; 134 | else if(i_key_en==1'b1) 135 | r_key_ok <= #DLY 1'b0; 136 | end 137 | 138 | assign o_key_ok = r_key_ok&(~i_key_en); 139 | assign o_exkey = {i_key,r_exkey[32*56-1:128]}; 140 | 141 | endmodule 142 | -------------------------------------------------------------------------------- /ciphers/rc5/rc5_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : rc5_dpc.v 19 | // Function : RC5 Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module rc5_dpc( 28 | input i_clk, 29 | input i_rst, 30 | input i_flag, 31 | input [32*26-1:0] i_keyex, 32 | input [63:0] i_din, 33 | input i_din_en, 34 | output [63:0] o_dout, 35 | output o_dout_en 36 | ); 37 | 38 | localparam DLY = 1; 39 | 40 | reg [3:0] r_count; 41 | wire [31:0] s_a,s_ax,s_ay,s_ay_e,s_ay_d; 42 | wire [31:0] s_b,s_bx,s_by,s_by_e,s_by_d; 43 | wire [63:0] s_din; 44 | reg [63:0] r_din; 45 | reg [32*22-1:0] r_keyex; 46 | wire [63:0] s_pkey; 47 | wire [63:0] s_ikey; 48 | wire [63:0] s_rkey; 49 | wire [63:0] s_pdin; 50 | wire [4:0] s_rr_x,s_rr_y; 51 | wire [31:0] s_rdin_x,s_rdin_y; 52 | wire [31:0] s_rdout_x,s_rdout_y; 53 | 54 | function [31:0] SWAP; 55 | input [31:0] D; 56 | begin 57 | SWAP = {D[7:0],D[15:8],D[23:16],D[31:24]}; 58 | end 59 | endfunction 60 | 61 | always@(posedge i_clk or posedge i_rst) begin 62 | if(i_rst) 63 | r_count <= #DLY 4'b0; 64 | else if(i_din_en) 65 | r_count <= #DLY 4'd1; 66 | else if(r_count==4'd11) 67 | r_count <= #DLY 4'b0; 68 | else if(r_count!=4'd0) 69 | r_count <= #DLY r_count + 4'd1; 70 | end 71 | 72 | always@(posedge i_clk or posedge i_rst) begin 73 | if(i_rst) 74 | r_keyex <= #DLY 'b0; 75 | else if(i_din_en) begin 76 | if(i_flag) 77 | r_keyex <= #DLY i_keyex[32*22-1:0]; 78 | else 79 | r_keyex <= #DLY i_keyex[32*24-1:32*2]; 80 | end else if(r_count!=5'd0)begin 81 | if(i_flag) 82 | r_keyex <= #DLY {r_keyex[32*20-1:0],64'b0}; 83 | else 84 | r_keyex <= #DLY {64'b0,r_keyex[32*22-1:64]}; 85 | end 86 | end 87 | 88 | assign s_pkey = i_keyex[32*26-1:32*24]; 89 | assign s_ikey = i_flag ? i_keyex[32*24-1:32*22] : i_keyex[32*2-1:0]; 90 | assign s_rkey = i_flag ? r_keyex[32*22-1:32*20] : r_keyex[32*2-1:0]; 91 | 92 | assign s_pdin = i_flag ? {(SWAP(i_din[63:32])+s_pkey[63:32]),(SWAP(i_din[31:0])+s_pkey[31:0])} : {SWAP(i_din[63:32]),SWAP(i_din[31:0])}; 93 | assign s_din = i_din_en ? s_pdin : r_din; 94 | 95 | assign s_a = s_din[63:32]; 96 | assign s_b = s_din[31:0]; 97 | // A = ROL(A ^ B, B) + K[0]; //encrypt 98 | // B = ROL(B ^ A, A) + K[1]; 99 | // B = ROR(B - K[1], A) ^ A; //decrypt 100 | // A = ROR(A - K[0], B) ^ B; 101 | assign s_rr_x = i_flag ? s_b[4:0] : (32-s_a[4:0]); 102 | assign s_rr_y = i_flag ? s_ay[4:0] : (32-s_by[4:0]); 103 | assign s_rdin_x = i_flag ? s_a^s_b : (i_din_en ? (s_b-s_ikey[31:0]):(s_b-s_rkey[31:0])); 104 | assign s_rdin_y = i_flag ? s_ay^s_b : (i_din_en ? (s_a-s_ikey[63:32]):(s_a-s_rkey[63:32])); 105 | 106 | rc5_rol u_rol1(.round(s_rr_x),.din(s_rdin_x),.dout(s_rdout_x)); 107 | rc5_rol u_rol2(.round(s_rr_y),.din(s_rdin_y),.dout(s_rdout_y)); 108 | 109 | assign s_ax = i_flag ? s_rdout_x : s_rdout_y^s_by; 110 | assign s_bx = i_flag ? s_rdout_y : s_rdout_x^s_a; 111 | 112 | assign s_ay_e = i_din_en ? (s_ax + s_ikey[63:32]):(s_ax + s_rkey[63:32]); 113 | assign s_by_e = i_din_en ? (s_bx + s_ikey[31:0]):(s_bx + s_rkey[31:0]); 114 | 115 | assign s_by_d = s_bx; 116 | assign s_ay_d = s_ax; 117 | 118 | assign s_ay = i_flag ? s_ay_e : s_ay_d; 119 | assign s_by = i_flag ? s_by_e : s_by_d; 120 | 121 | always@(posedge i_clk or posedge i_rst) begin 122 | if(i_rst) 123 | r_din <= #DLY 64'b0; 124 | else 125 | r_din <= #DLY {s_ay,s_by}; 126 | end 127 | 128 | assign o_dout = i_flag ? {SWAP(s_ay),SWAP(s_by)} : {SWAP(s_ay-s_pkey[63:32]),SWAP(s_by-s_pkey[31:0])}; 129 | assign o_dout_en = (r_count==4'd11) ? 1'b1:1'b0; 130 | 131 | endmodule 132 | -------------------------------------------------------------------------------- /ciphers/xtea/tb_xtea_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_xtea_core.v 19 | // Function : XTEA Cryptographic Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_xtea_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_flag; 34 | reg r_key_en; 35 | reg [127:0] r_key; 36 | reg r_din_en; 37 | reg [63:0] r_din; 38 | reg [31:0] r_err; 39 | reg [2:0] r_count; 40 | reg r_test; 41 | wire s_dout_en; 42 | wire [63:0] s_dout; 43 | wire s_key_ok; 44 | reg [1:0] r_state; 45 | 46 | localparam DLY = 1; 47 | 48 | // { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 50 | // { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 51 | // { 0xde, 0xe9, 0xd4, 0xd8, 0xf7, 0x13, 0x1e, 0xd9 } 52 | reg [127:0] KEY1 = {32'b0,32'b0,32'b0,32'b0}; 53 | reg [63:0] PT1 = {32'b0,32'b0}; 54 | reg [63:0] CT1 = {8'hde, 8'he9, 8'hd4, 8'hd8, 8'hf7, 8'h13, 8'h1e, 8'hd9}; 55 | // { 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 56 | // 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 }, 57 | // { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 }, 58 | // { 0x70, 0x4b, 0x31, 0x34, 0x47, 0x44, 0xdf, 0xab } 59 | reg [127:0] KEY2 = {8'h78, 8'h69, 8'h5a, 8'h4b, 8'h3c, 8'h2d, 8'h1e, 8'h0f, 8'hf0, 8'he1, 8'hd2, 8'hc3, 8'hb4, 8'ha5, 8'h96, 8'h87}; 60 | reg [63:0] PT2 = {8'hf0, 8'he1, 8'hd2, 8'hc3, 8'hb4, 8'ha5, 8'h96, 8'h87}; 61 | reg [63:0] CT2 = {8'h70, 8'h4b, 8'h31, 8'h34, 8'h47, 8'h44, 8'hdf, 8'hab}; 62 | 63 | 64 | xtea_core uut( 65 | .i_clk (r_clk ), 66 | .i_rst (r_rst ), 67 | .i_flag (r_flag ), //1-encrypt,0-decrypt 68 | .i_key (r_key ), 69 | .i_key_en (r_key_en ), 70 | .i_din (r_din ), 71 | .i_din_en (r_din_en ), 72 | .o_dout (s_dout ), 73 | .o_dout_en (s_dout_en ), 74 | .o_key_ok (s_key_ok ) 75 | ); 76 | 77 | initial begin 78 | r_clk = 0; 79 | forever #5 r_clk = ~r_clk; 80 | end 81 | 82 | always@(posedge r_clk or posedge r_rst) begin 83 | if(r_rst) begin 84 | r_count <= #DLY 3'd0; 85 | r_flag <= #DLY 1'b0; 86 | r_din_en <= #DLY 1'b0; 87 | r_din <= #DLY 'b0; 88 | r_key_en <= #DLY 1'b0; 89 | r_key <= #DLY 'b0; 90 | r_err <= #DLY 'b0; 91 | r_state <= #DLY 2'b0; 92 | end else begin 93 | case(r_state) 94 | 2'd0: begin 95 | if(r_test) begin 96 | r_key_en <= #DLY 1'b1; 97 | r_key <= #DLY KEY2; 98 | r_state <= #DLY 2'd1; 99 | end 100 | end 101 | 2'd1: begin 102 | r_key_en <= #DLY 1'b0; 103 | if(s_key_ok) begin 104 | r_din_en <= #DLY 1'b1; 105 | r_flag <= #DLY 1'b1; 106 | r_din <= #DLY PT2; 107 | r_state <= #DLY 2'd2; 108 | end 109 | end 110 | 2'd2: begin 111 | r_din_en <= #DLY 1'b0; 112 | if(s_dout_en) begin 113 | if(s_dout!=CT2) 114 | r_err <= #DLY r_err + 1'b1; 115 | r_din_en <= #DLY 1'b1; 116 | r_din <= #DLY CT2; 117 | r_flag <= #DLY 1'b0; 118 | r_state <= #DLY 2'd3; 119 | end 120 | end 121 | 2'd3: begin 122 | r_din_en <= #DLY 1'b0; 123 | if(s_dout_en) begin 124 | if(s_dout!=PT2) 125 | r_err <= #DLY r_err + 1'b1; 126 | r_count <= #DLY r_count + 1'b1; 127 | if(r_count == 'd7) 128 | r_state <= #DLY 2'd0; 129 | else 130 | r_state <= #DLY 2'd1; 131 | end 132 | end 133 | endcase 134 | end 135 | 136 | end 137 | 138 | initial begin 139 | r_rst = 1'b1; 140 | r_test = 1'b0; 141 | repeat(50) @(negedge r_clk); 142 | r_rst = 1'b0; 143 | repeat(10) @(negedge r_clk); 144 | r_test = 1'b1; 145 | repeat(5000) @(negedge r_clk); 146 | end 147 | 148 | 149 | endmodule 150 | -------------------------------------------------------------------------------- /hashes/sha2/tb_sha512_224_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sha512_224_core.v 19 | // Function : SHA512-224 Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sha512_224_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [1023:0] r_data; 35 | reg [511:0] r_vin; 36 | wire [511:0] s_vout; 37 | wire s_done; 38 | 39 | reg [511:0] INIT = { 40 | 64'h8C3D37C819544DA2,64'h73E1996689DCD4D6, 41 | 64'h1DFAB7AE32FF9C82,64'h679DD514582F9FCF, 42 | 64'h0F6D2B697BD44DA8,64'h77E36F7304C48942, 43 | 64'h3F9D85A86A1D36C8,64'h1112E6AD91D692A1}; 44 | 45 | // sha512_224("abc") 46 | //{ 0x46, 0x34, 0x27, 0x0F, 0x70, 0x7B, 0x6A, 0x54, 47 | // 0xDA, 0xAE, 0x75, 0x30, 0x46, 0x08, 0x42, 0xE2, 48 | // 0x0E, 0x37, 0xED, 0x26, 0x5C, 0xEE, 0xE9, 0xA4, 49 | // 0x3E, 0x89, 0x24, 0xAA } 50 | reg [1023:0] DATA1 = {32'h61626380,960'h0,32'h00000018}; 51 | 52 | // sha512_224 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890 53 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 54 | // 1a7dd4c3,e52b0587,92188abf,37076bc5,1685a3bf,a5558dad,19227274 55 | reg [1023:0] DATA2_1 = { 56 | 64'h3132333435363738,64'h3930313233343536, 57 | 64'h3738393031323334,64'h3536373839303132, 58 | 64'h3334353637383930,64'h3132333435363738, 59 | 64'h3930313233343536,64'h3738393031323334, 60 | 64'h3536373839303132,64'h3334353637383930, 61 | 64'h3132333435363738,64'h3930313233343536, 62 | 64'h3738393031323334,64'h3536373839303132, 63 | 64'h3334353637383930,64'h3132333435363738}; 64 | 65 | reg [1023:0] DATA2_2 = { 66 | 64'h3930313233343536,64'h3738393031323334, 67 | 64'h3536373839303132,64'h3334353637383930, 68 | 64'h8000000000000000,64'h0000000000000000, 69 | 64'h0000000000000000,64'h0000000000000000, 70 | 64'h0000000000000000,64'h0000000000000000, 71 | 64'h0000000000000000,64'h0000000000000000, 72 | 64'h0000000000000000,64'h0000000000000000, 73 | 64'h0000000000000000,64'h0000000000000500}; 74 | 75 | sha512_core uut( 76 | .i_clk (r_clk), 77 | .i_rst (r_rst), 78 | .i_start (r_start), 79 | .i_data (r_data), 80 | .i_vin (r_vin), 81 | .o_vout (s_vout), //hash value = s_vout[511:288] 82 | .o_done (s_done)); 83 | 84 | initial begin 85 | r_clk = 0; 86 | forever #5 r_clk = ~r_clk; 87 | end 88 | 89 | initial begin 90 | r_rst = 1'b1; 91 | r_start = 1'b0; 92 | r_vin = 256'b0; 93 | r_data = 512'b0; 94 | repeat(50) @(posedge r_clk); 95 | r_rst = 1'b0; 96 | 97 | ////test data 1 98 | repeat(50) @(posedge r_clk); 99 | r_start = 1'b1; 100 | r_vin = INIT; //init 101 | r_data = DATA1; 102 | $display("vin=0x%x",r_vin); 103 | $display("data=0x%x",r_data); 104 | @(posedge r_clk); 105 | r_start = 1'b0; 106 | wait(s_done); 107 | $display("vout=0x%x",s_vout); 108 | 109 | /////test data 2 110 | repeat(50) @(posedge r_clk); 111 | r_start = 1'b1; 112 | r_vin = INIT; //init 113 | r_data = DATA2_1; 114 | //$display("vin=0x%x",r_vin); 115 | //$display("data=0x%x",r_data); 116 | @(posedge r_clk); 117 | r_start = 1'b0; 118 | wait(s_done); 119 | $display("vout=0x%x",s_vout); 120 | r_vin = s_vout; 121 | @(posedge r_clk); 122 | r_start = 1'b1; 123 | r_data= DATA2_2; 124 | @(posedge r_clk); 125 | r_start = 1'b0; 126 | wait(s_done); 127 | $display("vout=0x%x",s_vout); 128 | 129 | /////stop 130 | repeat(50) @(posedge r_clk); 131 | $stop; 132 | end 133 | 134 | endmodule 135 | -------------------------------------------------------------------------------- /hashes/sha2/tb_sha512_256_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sha512_256_core.v 19 | // Function : SHA512-256 Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sha512_256_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [1023:0] r_data; 35 | reg [511:0] r_vin; 36 | wire [511:0] s_vout; 37 | wire s_done; 38 | 39 | reg [511:0] INIT = { 40 | 64'h22312194FC2BF72C,64'h9F555FA3C84C64C2, 41 | 64'h2393B86B6F53B151,64'h963877195940EABD, 42 | 64'h96283EE2A88EFFE3,64'hBE5E1E2553863992, 43 | 64'h2B0199FC2C85B8AA,64'h0EB72DDC81C52CA2}; 44 | 45 | // sha512_256("abc") 46 | // { 0x53, 0x04, 0x8E, 0x26, 0x81, 0x94, 0x1E, 0xF9, 47 | // 0x9B, 0x2E, 0x29, 0xB7, 0x6B, 0x4C, 0x7D, 0xAB, 48 | // 0xE4, 0xC2, 0xD0, 0xC6, 0x34, 0xFC, 0x6D, 0x46, 49 | // 0xE0, 0xE2, 0xF1, 0x31, 0x07, 0xE7, 0xAF, 0x23 } 50 | 51 | reg [1023:0] DATA1 = {32'h61626380,960'h0,32'h00000018}; 52 | 53 | // sha512_256 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890 54 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 55 | // 62308929,65ac0a68,744d2a16,db636f5f,f108bf54,eb253b27,a3e9c521,85a09a5c 56 | reg [1023:0] DATA2_1 = { 57 | 64'h3132333435363738,64'h3930313233343536, 58 | 64'h3738393031323334,64'h3536373839303132, 59 | 64'h3334353637383930,64'h3132333435363738, 60 | 64'h3930313233343536,64'h3738393031323334, 61 | 64'h3536373839303132,64'h3334353637383930, 62 | 64'h3132333435363738,64'h3930313233343536, 63 | 64'h3738393031323334,64'h3536373839303132, 64 | 64'h3334353637383930,64'h3132333435363738}; 65 | 66 | reg [1023:0] DATA2_2 = { 67 | 64'h3930313233343536,64'h3738393031323334, 68 | 64'h3536373839303132,64'h3334353637383930, 69 | 64'h8000000000000000,64'h0000000000000000, 70 | 64'h0000000000000000,64'h0000000000000000, 71 | 64'h0000000000000000,64'h0000000000000000, 72 | 64'h0000000000000000,64'h0000000000000000, 73 | 64'h0000000000000000,64'h0000000000000000, 74 | 64'h0000000000000000,64'h0000000000000500}; 75 | 76 | sha512_core uut( 77 | .i_clk (r_clk), 78 | .i_rst (r_rst), 79 | .i_start (r_start), 80 | .i_data (r_data), 81 | .i_vin (r_vin), 82 | .o_vout (s_vout), //hash value = s_vout[511:256] 83 | .o_done (s_done)); 84 | 85 | initial begin 86 | r_clk = 0; 87 | forever #5 r_clk = ~r_clk; 88 | end 89 | 90 | initial begin 91 | r_rst = 1'b1; 92 | r_start = 1'b0; 93 | r_vin = 256'b0; 94 | r_data = 512'b0; 95 | repeat(50) @(posedge r_clk); 96 | r_rst = 1'b0; 97 | 98 | ////test data 1 99 | repeat(50) @(posedge r_clk); 100 | r_start = 1'b1; 101 | r_vin = INIT; //init 102 | r_data = DATA1; 103 | $display("vin=0x%x",r_vin); 104 | $display("data=0x%x",r_data); 105 | @(posedge r_clk); 106 | r_start = 1'b0; 107 | wait(s_done); 108 | $display("vout=0x%x",s_vout); 109 | 110 | /////test data 2 111 | repeat(50) @(posedge r_clk); 112 | r_start = 1'b1; 113 | r_vin = INIT; //init 114 | r_data = DATA2_1; 115 | //$display("vin=0x%x",r_vin); 116 | //$display("data=0x%x",r_data); 117 | @(posedge r_clk); 118 | r_start = 1'b0; 119 | wait(s_done); 120 | $display("vout=0x%x",s_vout); 121 | r_vin = s_vout; 122 | @(posedge r_clk); 123 | r_start = 1'b1; 124 | r_data= DATA2_2; 125 | @(posedge r_clk); 126 | r_start = 1'b0; 127 | wait(s_done); 128 | $display("vout=0x%x",s_vout); 129 | 130 | /////stop 131 | repeat(50) @(posedge r_clk); 132 | $stop; 133 | end 134 | 135 | endmodule 136 | -------------------------------------------------------------------------------- /hashes/sha2/tb_sha384_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sha384_core.v 19 | // Function : SHA512-384 Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sha384_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [1023:0] r_data; 35 | reg [511:0] r_vin; 36 | wire [511:0] s_vout; 37 | wire s_done; 38 | 39 | reg [511:0] INIT = {64'hcbbb9d5dc1059ed8,64'h629a292a367cd507, 40 | 64'h9159015a3070dd17,64'h152fecd8f70e5939, 41 | 64'h67332667ffc00b31,64'h8eb44a8768581511, 42 | 64'hdb0c2e0d64f98fa7,64'h47b5481dbefa4fa4}; 43 | // SHA384("abc") 44 | // { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 45 | // 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07, 46 | // 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 47 | // 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed, 48 | // 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 49 | // 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 } 50 | reg [1023:0] DATA1 = {32'h61626380,960'h0,32'h00000018}; 51 | // SHA384 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890 52 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 53 | // 8d,eb,7d,cc,ae,68,ef,ba, 54 | // fe,c8,2d,5a,3d,9d,85,1a, 55 | // ef,45,8b,c2,7f,fc,c0,fc, 56 | // c8,cc,0e,43,b7,62,0e,b1, 57 | // 48,65,b0,d6,75,db,bf,92, 58 | // 30,54,85,28,b4,7f,fe,7b, 59 | reg [1023:0] DATA2_1 = { 60 | 64'h3132333435363738,64'h3930313233343536, 61 | 64'h3738393031323334,64'h3536373839303132, 62 | 64'h3334353637383930,64'h3132333435363738, 63 | 64'h3930313233343536,64'h3738393031323334, 64 | 64'h3536373839303132,64'h3334353637383930, 65 | 64'h3132333435363738,64'h3930313233343536, 66 | 64'h3738393031323334,64'h3536373839303132, 67 | 64'h3334353637383930,64'h3132333435363738}; 68 | 69 | 70 | reg [1023:0] DATA2_2 = { 71 | 64'h3930313233343536,64'h3738393031323334, 72 | 64'h3536373839303132,64'h3334353637383930, 73 | 64'h8000000000000000,64'h0000000000000000, 74 | 64'h0000000000000000,64'h0000000000000000, 75 | 64'h0000000000000000,64'h0000000000000000, 76 | 64'h0000000000000000,64'h0000000000000000, 77 | 64'h0000000000000000,64'h0000000000000000, 78 | 64'h0000000000000000,64'h0000000000000500}; 79 | 80 | sha512_core uut( 81 | .i_clk (r_clk), 82 | .i_rst (r_rst), 83 | .i_start (r_start), 84 | .i_data (r_data), 85 | .i_vin (r_vin), 86 | .o_vout (s_vout), //hash value = s_vout[511:128] 87 | .o_done (s_done)); 88 | 89 | initial begin 90 | r_clk = 0; 91 | forever #5 r_clk = ~r_clk; 92 | end 93 | 94 | initial begin 95 | r_rst = 1'b1; 96 | r_start = 1'b0; 97 | r_vin = 256'b0; 98 | r_data = 512'b0; 99 | repeat(50) @(posedge r_clk); 100 | r_rst = 1'b0; 101 | 102 | ////test data 1 103 | repeat(50) @(posedge r_clk); 104 | r_start = 1'b1; 105 | r_vin = INIT; //init 106 | r_data = DATA1; 107 | $display("vin=0x%x",r_vin); 108 | $display("data=0x%x",r_data); 109 | @(posedge r_clk); 110 | r_start = 1'b0; 111 | wait(s_done); 112 | $display("vout=0x%x",s_vout); 113 | 114 | /////test data 2 115 | repeat(50) @(posedge r_clk); 116 | r_start = 1'b1; 117 | r_vin = INIT; //init 118 | r_data = DATA2_1; 119 | //$display("vin=0x%x",r_vin); 120 | //$display("data=0x%x",r_data); 121 | @(posedge r_clk); 122 | r_start = 1'b0; 123 | wait(s_done); 124 | $display("vout=0x%x",s_vout); 125 | r_vin = s_vout; 126 | @(posedge r_clk); 127 | r_start = 1'b1; 128 | r_data= DATA2_2; 129 | @(posedge r_clk); 130 | r_start = 1'b0; 131 | wait(s_done); 132 | $display("vout=0x%x",s_vout); 133 | 134 | /////stop 135 | repeat(50) @(posedge r_clk); 136 | $stop; 137 | end 138 | 139 | endmodule 140 | -------------------------------------------------------------------------------- /hashes/tiger/tb_tiger_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_tiger_core.v 19 | // Function : Tiger Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-23 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_tiger_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [511:0] r_data; 35 | reg [191:0] r_vin; 36 | wire [191:0] s_vout; 37 | wire s_done; 38 | 39 | reg [191:0] INIT = {64'hEFCDAB8967452301,64'h1032547698BADCFE,64'h87E1B2C3B4A596F0}; 40 | 41 | //Tiger("abc") = { "abc", 42 | // { 0x2a, 0xab, 0x14, 0x84, 0xe8, 0xc1, 0x58, 0xf2, 43 | // 0xbf, 0xb8, 0xc5, 0xff, 0x41, 0xb5, 0x7a, 0x52, 44 | // 0x51, 0x29, 0x13, 0x1c, 0x95, 0x7b, 0x5f, 0x93 } 45 | reg [511:0] DATA1 = {64'h61626301_00000000,64'h00000000_00000000,64'h00000000_00000000,64'h00000000_00000000, 46 | 64'h00000000_00000000,64'h00000000_00000000,64'h00000000_00000000,64'h18000000_00000000}; 47 | 48 | //SHA256("123456789012345678901234567890123456789012345678901234567890123456 49 | //78901234567890") = 50 | // 0x1c, 0x14, 0x79, 0x55, 0x29, 0xfd, 0x9f, 0x20, 51 | // 0x7a, 0x95, 0x8f, 0x84, 0xc5, 0x2f, 0x11, 0xe8, 52 | // 0x87, 0xfa, 0x0c, 0xab, 0xdf, 0xd9, 0x1b, 0xfd 53 | reg [511:0] DATA2_1 = {80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 54 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 55 | 80'h3132_3334_3536_3738_3930,80'h3132_3334_3536_3738_3930, 56 | 80'h3132_3334_3536_3738_3930,32'h3132_3334}; 57 | reg [511:0] DATA2_2 = {48'h3536_3738_3930,80'h3132_3334_3536_3738_3930, 58 | 48'h0100_0000_0000,272'h0,64'h8002_0000_0000_0000}; 59 | 60 | //Tiger("") = { "", 61 | // { 0x32, 0x93, 0xac, 0x63, 0x0c, 0x13, 0xf0, 0x24, 62 | // 0x5f, 0x92, 0xbb, 0xb1, 0x76, 0x6e, 0x16, 0x16, 63 | // 0x7a, 0x4e, 0x58, 0x49, 0x2d, 0xde, 0x73, 0xf3 } 64 | reg [511:0] DATA3 = {64'h01000000_00000000,64'h00000000_00000000,64'h00000000_00000000,64'h00000000_00000000, 65 | 64'h00000000_00000000,64'h00000000_00000000,64'h00000000_00000000,64'h00000000_00000000}; 66 | 67 | tiger_core uut( 68 | .i_clk (r_clk), 69 | .i_rst (r_rst), 70 | .i_start (r_start), 71 | .i_data (r_data), 72 | .i_vin (r_vin), 73 | .o_vout (s_vout), 74 | .o_done (s_done)); 75 | 76 | initial begin 77 | r_clk = 0; 78 | forever #5 r_clk = ~r_clk; 79 | end 80 | 81 | initial begin 82 | r_rst = 1'b1; 83 | r_start = 1'b0; 84 | r_vin = 256'b0; 85 | r_data = 512'b0; 86 | repeat(50) @(negedge r_clk); 87 | r_rst = 1'b0; 88 | 89 | ////test data 1 90 | repeat(50) @(negedge r_clk); 91 | r_start = 1'b1; 92 | r_vin = INIT; //init 93 | r_data = DATA1; 94 | $display("vin=0x%x",r_vin); 95 | $display("data=0x%x",r_data); 96 | @(negedge r_clk); 97 | r_start = 1'b0; 98 | wait(s_done); 99 | @(negedge r_clk); 100 | $display("vout=0x%x",s_vout); 101 | 102 | /////test data 2 103 | repeat(50) @(negedge r_clk); 104 | r_start = 1'b1; 105 | r_vin = INIT; //init 106 | r_data = DATA2_1; 107 | //$display("vin=0x%x",r_vin); 108 | //$display("data=0x%x",r_data); 109 | @(negedge r_clk); 110 | r_start = 1'b0; 111 | wait(s_done); 112 | @(negedge r_clk); 113 | $display("vout=0x%x",s_vout); 114 | r_vin = s_vout; 115 | @(negedge r_clk); 116 | r_start = 1'b1; 117 | r_data= DATA2_2; 118 | @(negedge r_clk); 119 | r_start = 1'b0; 120 | wait(s_done); 121 | @(negedge r_clk); 122 | $display("vout=0x%x",s_vout); 123 | 124 | ////test data 3 125 | repeat(50) @(negedge r_clk); 126 | r_start = 1'b1; 127 | r_vin = INIT; //init 128 | r_data = DATA3; 129 | $display("vin=0x%x",r_vin); 130 | $display("data=0x%x",r_data); 131 | @(negedge r_clk); 132 | r_start = 1'b0; 133 | wait(s_done); 134 | @(negedge r_clk); 135 | $display("vout=0x%x",s_vout); 136 | 137 | /////stop 138 | repeat(50) @(negedge r_clk); 139 | $stop; 140 | end 141 | 142 | endmodule 143 | -------------------------------------------------------------------------------- /hashes/sha2/tb_sha512_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : tb_sha512_core.v 19 | // Function : SHA512 Hash Algorithm Core Simulate File 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module tb_sha512_core(); 30 | 31 | reg r_clk; 32 | reg r_rst; 33 | reg r_start; 34 | reg [1023:0] r_data; 35 | reg [511:0] r_vin; 36 | wire [511:0] s_vout; 37 | wire s_done; 38 | 39 | reg [511:0] INIT = {64'h6a09e667f3bcc908,64'hbb67ae8584caa73b, 40 | 64'h3c6ef372fe94f82b,64'ha54ff53a5f1d36f1, 41 | 64'h510e527fade682d1,64'h9b05688c2b3e6c1f, 42 | 64'h1f83d9abfb41bd6b,64'h5be0cd19137e2179}; 43 | // SHA512("abc") 44 | // {0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 45 | // 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, 46 | // 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 47 | // 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 48 | // 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 49 | // 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, 50 | // 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 51 | // 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f } 52 | reg [1023:0] DATA1 = {32'h61626380,960'h0,32'h00000018}; 53 | // SHA512 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890 54 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 55 | // "72,bf,79,45,67,40,d5,5c, 56 | // 96,ad,93,01,a3,53,d6,f8, 57 | // 21,91,0a,e3,b2,e9,b2,f4, 58 | // 02,20,63,0d,4f,c6,1c,2c, 59 | // 2d,8c,e3,fa,42,a2,fb,74, 60 | // 4b,39,d5,9f,08,ba,5f,36, 61 | // 78,97,2b,20,a1,c7,ae,50, 62 | // 61,d4,91,9f,1b,1b,02,34" 63 | reg [1023:0] DATA2_1 = { 64 | 64'h3132333435363738,64'h3930313233343536, 65 | 64'h3738393031323334,64'h3536373839303132, 66 | 64'h3334353637383930,64'h3132333435363738, 67 | 64'h3930313233343536,64'h3738393031323334, 68 | 64'h3536373839303132,64'h3334353637383930, 69 | 64'h3132333435363738,64'h3930313233343536, 70 | 64'h3738393031323334,64'h3536373839303132, 71 | 64'h3334353637383930,64'h3132333435363738}; 72 | 73 | 74 | reg [1023:0] DATA2_2 = { 75 | 64'h3930313233343536,64'h3738393031323334, 76 | 64'h3536373839303132,64'h3334353637383930, 77 | 64'h8000000000000000,64'h0000000000000000, 78 | 64'h0000000000000000,64'h0000000000000000, 79 | 64'h0000000000000000,64'h0000000000000000, 80 | 64'h0000000000000000,64'h0000000000000000, 81 | 64'h0000000000000000,64'h0000000000000000, 82 | 64'h0000000000000000,64'h0000000000000500}; 83 | 84 | sha512_core uut( 85 | .i_clk (r_clk), 86 | .i_rst (r_rst), 87 | .i_start (r_start), 88 | .i_data (r_data), 89 | .i_vin (r_vin), 90 | .o_vout (s_vout), 91 | .o_done (s_done)); 92 | 93 | initial begin 94 | r_clk = 0; 95 | forever #5 r_clk = ~r_clk; 96 | end 97 | 98 | initial begin 99 | r_rst = 1'b1; 100 | r_start = 1'b0; 101 | r_vin = 256'b0; 102 | r_data = 512'b0; 103 | repeat(50) @(posedge r_clk); 104 | r_rst = 1'b0; 105 | 106 | ////test data 1 107 | repeat(50) @(posedge r_clk); 108 | r_start = 1'b1; 109 | r_vin = INIT; //init 110 | r_data = DATA1; 111 | $display("vin=0x%x",r_vin); 112 | $display("data=0x%x",r_data); 113 | @(posedge r_clk); 114 | r_start = 1'b0; 115 | wait(s_done); 116 | $display("vout=0x%x",s_vout); 117 | 118 | /////test data 2 119 | repeat(50) @(posedge r_clk); 120 | r_start = 1'b1; 121 | r_vin = INIT; //init 122 | r_data = DATA2_1; 123 | //$display("vin=0x%x",r_vin); 124 | //$display("data=0x%x",r_data); 125 | @(posedge r_clk); 126 | r_start = 1'b0; 127 | wait(s_done); 128 | $display("vout=0x%x",s_vout); 129 | r_vin = s_vout; 130 | @(posedge r_clk); 131 | r_start = 1'b1; 132 | r_data= DATA2_2; 133 | @(posedge r_clk); 134 | r_start = 1'b0; 135 | wait(s_done); 136 | $display("vout=0x%x",s_vout); 137 | 138 | /////stop 139 | repeat(50) @(posedge r_clk); 140 | $stop; 141 | end 142 | 143 | endmodule 144 | -------------------------------------------------------------------------------- /ciphers/cast5/cast5_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : cast5_dpc.v 19 | // Function : CAST5 Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-3 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module cast5_dpc( 28 | input i_clk, 29 | input i_rst, 30 | input i_flag, 31 | input [32*32-1:0] i_keyex, 32 | input [63:0] i_din, 33 | input i_din_en, 34 | output [63:0] o_dout, 35 | output o_dout_en, 36 | output [31:0] o_sbox_din, 37 | input [127:0] i_sbox_dout 38 | ); 39 | 40 | localparam DLY = 1; 41 | 42 | reg [3:0] r_count; 43 | wire [63:0] s_din; 44 | reg [63:0] r_din; 45 | wire [63:0] s_ikey; 46 | wire [63:0] s_rkey; 47 | wire [4:0] s_rr_x; 48 | wire [31:0] s_rdin_x; 49 | wire [31:0] s_rdout_x; 50 | reg [32*15-1:0] r_keyex_h; 51 | reg [32*15-1:0] r_keyex_l; 52 | wire [1:0] s_op_a,s_op_b; 53 | reg [5:0] r_op; 54 | wire [31:0] s_l,s_r; 55 | wire s_busy; 56 | reg r_dout_en; 57 | 58 | function [31:0] FIA; 59 | input [31:0] D; 60 | input [31:0] K; 61 | input [1:0] S; 62 | begin 63 | FIA = (S==2'd1) ? (K + D) : 64 | ((S==2'd2) ? (K ^ D) : 65 | ((S==2'd3) ? (K - D) : 32'b0)); 66 | end 67 | endfunction 68 | 69 | function [31:0] FIB; 70 | input [127:0] D; 71 | input [1:0] S; 72 | begin 73 | FIB = (S==2'd1) ? (((D[127:96]^D[95:64])-D[63:32])+D[31:0]) : 74 | ((S==2'd2) ? (((D[127:96]-D[95:64])+D[63:32])^D[31:0]) : 75 | ((S==2'd3) ? (((D[127:96]+D[95:64])^D[63:32])-D[31:0]) : 32'b0)); 76 | end 77 | endfunction 78 | 79 | always@(posedge i_clk or posedge i_rst) begin 80 | if(i_rst) 81 | r_op <= #DLY 6'b0; 82 | else if(i_din_en) 83 | r_op <= #DLY (i_flag ? 6'b101101:6'b111001); 84 | else if(s_busy) 85 | r_op <= #DLY {r_op[3:0],r_op[5:4]}; 86 | end 87 | 88 | assign s_op_a = i_din_en ? 2'b01 : r_op[5:4]; 89 | assign s_op_b = r_op[1:0]; 90 | 91 | always@(posedge i_clk or posedge i_rst) begin 92 | if(i_rst) 93 | r_count <= #DLY 4'b0; 94 | else if(i_din_en) 95 | r_count <= #DLY 4'd1; 96 | else if(r_count!=4'd0) 97 | r_count <= #DLY r_count + 4'd1; 98 | end 99 | 100 | always@(posedge i_clk or posedge i_rst) begin 101 | if(i_rst) 102 | r_keyex_h <= #DLY 'b0; 103 | else if(i_din_en) begin 104 | if(i_flag) 105 | r_keyex_h <= #DLY i_keyex[32*31-1:32*16]; 106 | else 107 | r_keyex_h <= #DLY i_keyex[32*32-1:32*17]; 108 | end else if(r_count!=5'd0)begin 109 | if(i_flag) 110 | r_keyex_h <= #DLY {r_keyex_h[32*14-1:0],32'b0}; 111 | else 112 | r_keyex_h <= #DLY {32'b0,r_keyex_h[32*15-1:32]}; 113 | end 114 | end 115 | 116 | always@(posedge i_clk or posedge i_rst) begin 117 | if(i_rst) 118 | r_keyex_l <= #DLY 'b0; 119 | else if(i_din_en) begin 120 | if(i_flag) 121 | r_keyex_l <= #DLY i_keyex[32*15-1:0]; 122 | else 123 | r_keyex_l <= #DLY i_keyex[32*16-1:32]; 124 | end else if(r_count!=5'd0)begin 125 | if(i_flag) 126 | r_keyex_l <= #DLY {r_keyex_l[32*14-1:0],32'b0}; 127 | else 128 | r_keyex_l <= #DLY {32'b0,r_keyex_l[32*15-1:32]}; 129 | end 130 | end 131 | 132 | assign s_ikey = i_flag ? {i_keyex[32*32-1:32*31],i_keyex[32*16-1:32*15]} : {i_keyex[32*17-1:32*16],i_keyex[32*1-1:0]}; 133 | assign s_rkey = i_flag ? {r_keyex_h[32*15-1:32*14],r_keyex_l[32*15-1:32*14]} : {r_keyex_h[32*1-1:0],r_keyex_l[32*1-1:0]}; 134 | 135 | assign s_din = i_din_en ? i_din : {r_din[31:0],r_din[63:32]^FIB(i_sbox_dout,s_op_b)}; 136 | 137 | assign s_l = s_din[63:32]; 138 | assign s_r = s_din[31:0]; 139 | 140 | assign s_rr_x = i_din_en ? s_ikey[4:0] : s_rkey[4:0]; 141 | assign s_rdin_x = i_din_en ? FIA(s_r,s_ikey[63:32],s_op_a) :FIA(s_r,s_rkey[63:32],s_op_a); 142 | 143 | cast5_rol u_rol(.round(s_rr_x),.din(s_rdin_x),.dout(s_rdout_x)); 144 | 145 | assign o_sbox_din = s_rdout_x; 146 | assign s_busy = (i_din_en|(r_count!='d0)) ? 1'b1 : 1'b0; 147 | 148 | always@(posedge i_clk or posedge i_rst) begin 149 | if(i_rst) 150 | r_din <= #DLY 64'b0; 151 | else if(i_din_en) 152 | r_din <= #DLY i_din; 153 | else 154 | r_din <= #DLY {r_din[31:0],r_din[63:32]^FIB(i_sbox_dout,s_op_b)}; 155 | end 156 | 157 | always@(posedge i_clk or posedge i_rst) begin 158 | if(i_rst) 159 | r_dout_en <= #DLY 1'b0; 160 | else if(r_count ==4'd15) 161 | r_dout_en <= #DLY 1'b1; 162 | else 163 | r_dout_en <= #DLY 1'b0; 164 | end 165 | 166 | assign o_dout_en = r_dout_en; 167 | assign o_dout = {r_din[63:32]^FIB(i_sbox_dout,s_op_b),r_din[31:0]}; 168 | 169 | endmodule 170 | -------------------------------------------------------------------------------- /hashes/sha1/sha1_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : sha1_core.v 19 | // Function : SHA1 Hash Algorithm Core 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module sha1_core( 28 | input i_clk, //clock 29 | input i_rst, //reset high valid 30 | input i_start, //high valid(only one clock) 31 | input [511:0] i_data, //hash data input 32 | input [159:0] i_vin, //hash init value input(not change before o_done valid) 33 | output [159:0] o_vout, //hash value output 34 | output o_done); //high valid(only one clock) 35 | 36 | localparam K0 = 32'h5A827999; 37 | localparam K1 = 32'h6ED9EBA1; 38 | localparam K2 = 32'h8F1BBCDC; 39 | localparam K3 = 32'hCA62C1D6; 40 | 41 | reg r_done; 42 | reg [6:0] r_count; 43 | reg [31:0] r_a,r_b,r_c,r_d,r_e; 44 | reg [511:0] r_w; 45 | reg [2:0] r_state; 46 | 47 | function [31:0] FF0; 48 | input [31:0] A,B,C,D,E,W,K; 49 | begin 50 | FF0 = {A[26:0],A[31:27]} + ((B&C)|((~B)&D)) + E + W + K; 51 | end 52 | endfunction 53 | 54 | function [31:0] FF1; 55 | input [31:0] A,B,C,D,E,W,K; 56 | begin 57 | FF1 = {A[26:0],A[31:27]} + (B^C^D) + E + W + K; 58 | end 59 | endfunction 60 | 61 | function [31:0] FF2; 62 | input [31:0] A,B,C,D,E,W,K; 63 | begin 64 | FF2 = {A[26:0],A[31:27]} + ((B&C)|(B&D)|(C&D)) + E + W + K; 65 | end 66 | endfunction 67 | 68 | //SHA1CircularShift(1,W) 69 | function [31:0] ROL0; 70 | input [31:0] W; 71 | begin 72 | ROL0 = {W[30:0],W[31]}; 73 | end 74 | endfunction 75 | 76 | always@(posedge i_clk or posedge i_rst) begin 77 | if(i_rst) begin 78 | r_done <= 1'b0; 79 | r_count <= 7'b0; 80 | r_a <= 32'b0; 81 | r_b <= 32'b0; 82 | r_c <= 32'b0; 83 | r_d <= 32'b0; 84 | r_e <= 32'b0; 85 | r_w <= 512'b0; 86 | r_state <= 3'd0; 87 | end else begin 88 | case(r_state) 89 | 3'd0: begin 90 | r_done <= 1'b0; 91 | r_count <= 7'b0; 92 | if(i_start) begin 93 | r_a <= i_vin[159:128]; 94 | r_b <= i_vin[127:96]; 95 | r_c <= i_vin[95:64]; 96 | r_d <= i_vin[63:32]; 97 | r_e <= i_vin[31:0]; 98 | r_w <= i_data; 99 | r_state <= 3'd1; 100 | end 101 | end 102 | 3'd1: begin 103 | r_count <= r_count + 7'b1; 104 | r_a <= FF0(r_a,r_b,r_c,r_d,r_e,r_w[511:480],K0); 105 | r_b <= r_a; 106 | r_c <= {r_b[1:0],r_b[31:2]}; 107 | r_d <= r_c; 108 | r_e <= r_d; 109 | r_w <= {r_w[479:0],ROL0(r_w[32*3-1:32*2]^r_w[32*8-1:32*7]^r_w[32*14-1:32*13]^r_w[32*16-1:32*15])}; 110 | if(r_count==7'd19) begin 111 | r_state <= 3'd2; 112 | end 113 | end 114 | 3'd2: begin 115 | r_count <= r_count + 7'b1; 116 | r_a <= FF1(r_a,r_b,r_c,r_d,r_e,r_w[511:480],K1); 117 | r_b <= r_a; 118 | r_c <= {r_b[1:0],r_b[31:2]}; 119 | r_d <= r_c; 120 | r_e <= r_d; 121 | r_w <= {r_w[479:0],ROL0(r_w[32*3-1:32*2]^r_w[32*8-1:32*7]^r_w[32*14-1:32*13]^r_w[32*16-1:32*15])}; 122 | if(r_count==7'd39) begin 123 | r_state <= 3'd3; 124 | end 125 | end 126 | 3'd3: begin 127 | r_count <= r_count + 7'b1; 128 | r_a <= FF2(r_a,r_b,r_c,r_d,r_e,r_w[511:480],K2); 129 | r_b <= r_a; 130 | r_c <= {r_b[1:0],r_b[31:2]}; 131 | r_d <= r_c; 132 | r_e <= r_d; 133 | r_w <= {r_w[479:0],ROL0(r_w[32*3-1:32*2]^r_w[32*8-1:32*7]^r_w[32*14-1:32*13]^r_w[32*16-1:32*15])}; 134 | if(r_count==7'd59) begin 135 | r_state <= 3'd4; 136 | end 137 | end 138 | 3'd4: begin 139 | r_count <= r_count + 7'b1; 140 | r_a <= FF1(r_a,r_b,r_c,r_d,r_e,r_w[511:480],K3); 141 | r_b <= r_a; 142 | r_c <= {r_b[1:0],r_b[31:2]}; 143 | r_d <= r_c; 144 | r_e <= r_d; 145 | r_w <= {r_w[479:0],ROL0(r_w[32*3-1:32*2]^r_w[32*8-1:32*7]^r_w[32*14-1:32*13]^r_w[32*16-1:32*15])}; 146 | if(r_count==7'd79) begin 147 | r_state <= 3'd5; 148 | end 149 | end 150 | 3'd5: begin 151 | r_a <= r_a + i_vin[159:128]; 152 | r_b <= r_b + i_vin[127:96]; 153 | r_c <= r_c + i_vin[95:64]; 154 | r_d <= r_d + i_vin[63:32]; 155 | r_e <= r_e + i_vin[31:0]; 156 | r_done <= 1'b1; 157 | r_state <= 3'd0; 158 | end 159 | default: begin 160 | r_done <= 1'b0; 161 | r_count <= 7'b0; 162 | r_a <= 32'b0; 163 | r_b <= 32'b0; 164 | r_c <= 32'b0; 165 | r_d <= 32'b0; 166 | r_e <= 32'b0; 167 | r_w <= 512'b0; 168 | r_state <= 3'd0; 169 | end 170 | endcase 171 | end 172 | end 173 | 174 | assign o_vout = {r_a,r_b,r_c,r_d,r_e}; 175 | assign o_done = r_done; 176 | 177 | endmodule 178 | -------------------------------------------------------------------------------- /ciphers/aes/aes128_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : aes128_dpc.v 19 | // Function : AES-128 Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module aes128_dpc( 28 | input i_clk, 29 | input i_rst, 30 | input i_flag, 31 | input [128*11-1:0] i_keyex, 32 | input [127:0] i_din, 33 | input i_din_en, 34 | output [127:0] o_dout, 35 | output o_dout_en, 36 | output [127:0] o_sbox_din, 37 | input [127:0] i_sbox_dout 38 | ); 39 | 40 | localparam DLY = 1; 41 | 42 | reg [3:0] r_count; 43 | reg [127:0] r_ka; 44 | reg [127:0] r_din; 45 | wire [127:0] s_din; 46 | wire [31:0] s_dina,s_dinb,s_dinc,s_dind; 47 | wire [127:0] s_mixc_doutx; 48 | wire [127:0] s_mixc_douty; 49 | wire [127:0] s_mixc_din; 50 | wire [127:0] s_ikey; 51 | 52 | //byte select 53 | function [7:0] BS; 54 | input [31:0] D; 55 | input [1:0] S; 56 | begin 57 | BS = (S==2'd3) ? D[31:24]: 58 | ((S==2'd2) ? D[23:16]: 59 | ((S==2'd1) ? D[15:8] : 60 | ((S==2'd0) ? D[7:0] :8'b0))); 61 | end 62 | endfunction 63 | 64 | always@(posedge i_clk or posedge i_rst) begin 65 | if(i_rst) 66 | r_count <= #DLY 4'b0; 67 | else if(i_din_en) 68 | r_count <= #DLY 4'd1; 69 | else if(r_count==4'd9) 70 | r_count <= #DLY 4'b0; 71 | else if(r_count!=4'd0) 72 | r_count <= #DLY r_count + 4'd1; 73 | end 74 | 75 | always@(*) begin 76 | if(i_flag) begin //encrypt 77 | case(r_count) 78 | 4'd0: r_ka = i_keyex[128*10-1:128* 9]; 79 | 4'd1: r_ka = i_keyex[128* 9-1:128* 8]; 80 | 4'd2: r_ka = i_keyex[128* 8-1:128* 7]; 81 | 4'd3: r_ka = i_keyex[128* 7-1:128* 6]; 82 | 4'd4: r_ka = i_keyex[128* 6-1:128* 5]; 83 | 4'd5: r_ka = i_keyex[128* 5-1:128* 4]; 84 | 4'd6: r_ka = i_keyex[128* 4-1:128* 3]; 85 | 4'd7: r_ka = i_keyex[128* 3-1:128* 2]; 86 | 4'd8: r_ka = i_keyex[128* 2-1:128* 1]; 87 | 4'd9: r_ka = i_keyex[128* 1-1:128* 0]; 88 | endcase 89 | end else begin //decrypt 90 | case(r_count) 91 | 4'd9: r_ka = i_keyex[128*11-1:128*10]; 92 | 4'd8: r_ka = i_keyex[128*10-1:128* 9]; 93 | 4'd7: r_ka = i_keyex[128* 9-1:128* 8]; 94 | 4'd6: r_ka = i_keyex[128* 8-1:128* 7]; 95 | 4'd5: r_ka = i_keyex[128* 7-1:128* 6]; 96 | 4'd4: r_ka = i_keyex[128* 6-1:128* 5]; 97 | 4'd3: r_ka = i_keyex[128* 5-1:128* 4]; 98 | 4'd2: r_ka = i_keyex[128* 4-1:128* 3]; 99 | 4'd1: r_ka = i_keyex[128* 3-1:128* 2]; 100 | 4'd0: r_ka = i_keyex[128* 2-1:128* 1]; 101 | endcase 102 | end 103 | end 104 | 105 | assign s_ikey = i_flag ? i_keyex[128*11-1:128*10] : i_keyex[128* 1-1:128* 0]; 106 | assign s_din = i_din_en ? i_din^s_ikey : r_din; 107 | assign s_dina = s_din[127:96]; //col-1 108 | assign s_dinb = s_din[95:64]; //col-2 109 | assign s_dinc = s_din[63:32]; //col-3 110 | assign s_dind = s_din[31:0]; //col-4 111 | 112 | //ShiftRows & SubBytes 113 | assign o_sbox_din[127:96] = i_flag ? {BS(s_dina,3),BS(s_dinb,2),BS(s_dinc,1),BS(s_dind,0)} : {BS(s_dina,3),BS(s_dind,2),BS(s_dinc,1),BS(s_dinb,0)}; 114 | assign o_sbox_din[95:64] = i_flag ? {BS(s_dinb,3),BS(s_dinc,2),BS(s_dind,1),BS(s_dina,0)} : {BS(s_dinb,3),BS(s_dina,2),BS(s_dind,1),BS(s_dinc,0)}; 115 | assign o_sbox_din[63:32] = i_flag ? {BS(s_dinc,3),BS(s_dind,2),BS(s_dina,1),BS(s_dinb,0)} : {BS(s_dinc,3),BS(s_dinb,2),BS(s_dina,1),BS(s_dind,0)}; 116 | assign o_sbox_din[31:0] = i_flag ? {BS(s_dind,3),BS(s_dina,2),BS(s_dinb,1),BS(s_dinc,0)} : {BS(s_dind,3),BS(s_dinc,2),BS(s_dinb,1),BS(s_dina,0)}; 117 | // 118 | assign s_mixc_din = i_flag ? i_sbox_dout : i_sbox_dout^r_ka; 119 | //MixColumns 120 | aes_mixcol_w u_mixcol_1( 121 | .i_din (s_mixc_din[127:96] ), 122 | .o_dout_x (s_mixc_doutx[127:96] ), 123 | .o_dout_y (s_mixc_douty[127:96] )); 124 | 125 | aes_mixcol_w u_mixcol_2( 126 | .i_din (s_mixc_din[95:64] ), 127 | .o_dout_x (s_mixc_doutx[95:64] ), 128 | .o_dout_y (s_mixc_douty[95:64] )); 129 | 130 | aes_mixcol_w u_mixcol_3( 131 | .i_din (s_mixc_din[63:32] ), 132 | .o_dout_x (s_mixc_doutx[63:32] ), 133 | .o_dout_y (s_mixc_douty[63:32] )); 134 | 135 | aes_mixcol_w u_mixcol_4( 136 | .i_din (s_mixc_din[31:0] ), 137 | .o_dout_x (s_mixc_doutx[31:0] ), 138 | .o_dout_y (s_mixc_douty[31:0] )); 139 | 140 | always@(posedge i_clk or posedge i_rst) begin 141 | if(i_rst) 142 | r_din <= #DLY 128'b0; 143 | else if(i_flag) 144 | r_din <= #DLY s_mixc_doutx^r_ka; 145 | else 146 | r_din <= #DLY s_mixc_douty; 147 | end 148 | 149 | assign o_dout = i_sbox_dout^r_ka; 150 | 151 | assign o_dout_en = (r_count==4'd9) ? 1'b1:1'b0; 152 | 153 | endmodule 154 | -------------------------------------------------------------------------------- /ciphers/aes/aes192_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : aes192_dpc.v 19 | // Function : AES-192 Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-4-19 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module aes192_dpc( 30 | input i_clk, 31 | input i_rst, 32 | input i_flag, 33 | input [128*13-1:0] i_keyex, 34 | input [127:0] i_din, 35 | input i_din_en, 36 | output [127:0] o_dout, 37 | output o_dout_en, 38 | output [127:0] o_sbox_din, 39 | input [127:0] i_sbox_dout 40 | ); 41 | 42 | localparam DLY = 1; 43 | 44 | reg [3:0] r_count; 45 | reg [127:0] r_ka; 46 | reg [127:0] r_din; 47 | wire [127:0] s_din; 48 | wire [31:0] s_dina,s_dinb,s_dinc,s_dind; 49 | wire [127:0] s_mixc_doutx; 50 | wire [127:0] s_mixc_douty; 51 | wire [127:0] s_mixc_din; 52 | wire [127:0] s_ikey; 53 | 54 | //byte select 55 | function [7:0] BS; 56 | input [31:0] D; 57 | input [1:0] S; 58 | begin 59 | BS = (S==2'd3) ? D[31:24]: 60 | ((S==2'd2) ? D[23:16]: 61 | ((S==2'd1) ? D[15:8] : 62 | ((S==2'd0) ? D[7:0] :8'b0))); 63 | end 64 | endfunction 65 | 66 | always@(posedge i_clk or posedge i_rst) begin 67 | if(i_rst) 68 | r_count <= #DLY 4'b0; 69 | else if(i_din_en) 70 | r_count <= #DLY 4'd1; 71 | else if(r_count==4'd11) 72 | r_count <= #DLY 4'b0; 73 | else if(r_count!=4'd0) 74 | r_count <= #DLY r_count + 4'd1; 75 | end 76 | 77 | always@(*) begin 78 | if(i_flag) begin //encrypt 79 | case(r_count) 80 | 4'd0 : r_ka = i_keyex[128*12-1:128*11]; 81 | 4'd1 : r_ka = i_keyex[128*11-1:128*10]; 82 | 4'd2 : r_ka = i_keyex[128*10-1:128* 9]; 83 | 4'd3 : r_ka = i_keyex[128* 9-1:128* 8]; 84 | 4'd4 : r_ka = i_keyex[128* 8-1:128* 7]; 85 | 4'd5 : r_ka = i_keyex[128* 7-1:128* 6]; 86 | 4'd6 : r_ka = i_keyex[128* 6-1:128* 5]; 87 | 4'd7 : r_ka = i_keyex[128* 5-1:128* 4]; 88 | 4'd8 : r_ka = i_keyex[128* 4-1:128* 3]; 89 | 4'd9 : r_ka = i_keyex[128* 3-1:128* 2]; 90 | 4'd10: r_ka = i_keyex[128* 2-1:128* 1]; 91 | 4'd11: r_ka = i_keyex[128* 1-1:128* 0]; 92 | endcase 93 | end else begin //decrypt 94 | case(r_count) 95 | 4'd11: r_ka = i_keyex[128*13-1:128*12]; 96 | 4'd10: r_ka = i_keyex[128*12-1:128*11]; 97 | 4'd9 : r_ka = i_keyex[128*11-1:128*10]; 98 | 4'd8 : r_ka = i_keyex[128*10-1:128* 9]; 99 | 4'd7 : r_ka = i_keyex[128* 9-1:128* 8]; 100 | 4'd6 : r_ka = i_keyex[128* 8-1:128* 7]; 101 | 4'd5 : r_ka = i_keyex[128* 7-1:128* 6]; 102 | 4'd4 : r_ka = i_keyex[128* 6-1:128* 5]; 103 | 4'd3 : r_ka = i_keyex[128* 5-1:128* 4]; 104 | 4'd2 : r_ka = i_keyex[128* 4-1:128* 3]; 105 | 4'd1 : r_ka = i_keyex[128* 3-1:128* 2]; 106 | 4'd0 : r_ka = i_keyex[128* 2-1:128* 1]; 107 | endcase 108 | end 109 | end 110 | 111 | assign s_ikey = i_flag ? i_keyex[128*13-1:128*12] : i_keyex[128* 1-1:128* 0]; 112 | assign s_din = i_din_en ? i_din^s_ikey : r_din; 113 | assign s_dina = s_din[127:96]; //col-1 114 | assign s_dinb = s_din[95:64]; //col-2 115 | assign s_dinc = s_din[63:32]; //col-3 116 | assign s_dind = s_din[31:0]; //col-4 117 | 118 | //ShiftRows & SubBytes 119 | assign o_sbox_din[127:96] = i_flag ? {BS(s_dina,3),BS(s_dinb,2),BS(s_dinc,1),BS(s_dind,0)} : {BS(s_dina,3),BS(s_dind,2),BS(s_dinc,1),BS(s_dinb,0)}; 120 | assign o_sbox_din[95:64] = i_flag ? {BS(s_dinb,3),BS(s_dinc,2),BS(s_dind,1),BS(s_dina,0)} : {BS(s_dinb,3),BS(s_dina,2),BS(s_dind,1),BS(s_dinc,0)}; 121 | assign o_sbox_din[63:32] = i_flag ? {BS(s_dinc,3),BS(s_dind,2),BS(s_dina,1),BS(s_dinb,0)} : {BS(s_dinc,3),BS(s_dinb,2),BS(s_dina,1),BS(s_dind,0)}; 122 | assign o_sbox_din[31:0] = i_flag ? {BS(s_dind,3),BS(s_dina,2),BS(s_dinb,1),BS(s_dinc,0)} : {BS(s_dind,3),BS(s_dinc,2),BS(s_dinb,1),BS(s_dina,0)}; 123 | // 124 | assign s_mixc_din = i_flag ? i_sbox_dout : i_sbox_dout^r_ka; 125 | //MixColumns 126 | aes_mixcol_w u_mixcol_1( 127 | .i_din (s_mixc_din[127:96] ), 128 | .o_dout_x (s_mixc_doutx[127:96] ), 129 | .o_dout_y (s_mixc_douty[127:96] )); 130 | 131 | aes_mixcol_w u_mixcol_2( 132 | .i_din (s_mixc_din[95:64] ), 133 | .o_dout_x (s_mixc_doutx[95:64] ), 134 | .o_dout_y (s_mixc_douty[95:64] )); 135 | 136 | aes_mixcol_w u_mixcol_3( 137 | .i_din (s_mixc_din[63:32] ), 138 | .o_dout_x (s_mixc_doutx[63:32] ), 139 | .o_dout_y (s_mixc_douty[63:32] )); 140 | 141 | aes_mixcol_w u_mixcol_4( 142 | .i_din (s_mixc_din[31:0] ), 143 | .o_dout_x (s_mixc_doutx[31:0] ), 144 | .o_dout_y (s_mixc_douty[31:0] )); 145 | 146 | always@(posedge i_clk or posedge i_rst) begin 147 | if(i_rst) 148 | r_din <= #DLY 128'b0; 149 | else if(i_flag) 150 | r_din <= #DLY s_mixc_doutx^r_ka; 151 | else 152 | r_din <= #DLY s_mixc_douty; 153 | end 154 | 155 | assign o_dout = i_sbox_dout^r_ka; 156 | 157 | assign o_dout_en = (r_count==4'd11) ? 1'b1:1'b0; 158 | 159 | endmodule 160 | -------------------------------------------------------------------------------- /ciphers/aes/aes256_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : aes256_dpc.v 19 | // Function : AES-256 Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-4-20 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | `timescale 1ns / 1ps 28 | 29 | module aes256_dpc( 30 | input i_clk, 31 | input i_rst, 32 | input i_flag, 33 | input [128*15-1:0] i_keyex, 34 | input [127:0] i_din, 35 | input i_din_en, 36 | output [127:0] o_dout, 37 | output o_dout_en, 38 | output [127:0] o_sbox_din, 39 | input [127:0] i_sbox_dout 40 | ); 41 | 42 | localparam DLY = 1; 43 | 44 | reg [3:0] r_count; 45 | reg [127:0] r_ka; 46 | reg [127:0] r_din; 47 | wire [127:0] s_din; 48 | wire [31:0] s_dina,s_dinb,s_dinc,s_dind; 49 | wire [127:0] s_mixc_doutx; 50 | wire [127:0] s_mixc_douty; 51 | wire [127:0] s_mixc_din; 52 | wire [127:0] s_ikey; 53 | 54 | //byte select 55 | function [7:0] BS; 56 | input [31:0] D; 57 | input [1:0] S; 58 | begin 59 | BS = (S==2'd3) ? D[31:24]: 60 | ((S==2'd2) ? D[23:16]: 61 | ((S==2'd1) ? D[15:8] : 62 | ((S==2'd0) ? D[7:0] :8'b0))); 63 | end 64 | endfunction 65 | 66 | always@(posedge i_clk or posedge i_rst) begin 67 | if(i_rst) 68 | r_count <= #DLY 4'b0; 69 | else if(i_din_en) 70 | r_count <= #DLY 4'd1; 71 | else if(r_count==4'd13) 72 | r_count <= #DLY 4'b0; 73 | else if(r_count!=4'd0) 74 | r_count <= #DLY r_count + 4'd1; 75 | end 76 | 77 | always@(*) begin 78 | if(i_flag) begin //encrypt 79 | case(r_count) 80 | 4'd0 : r_ka = i_keyex[128*14-1:128*13]; 81 | 4'd1 : r_ka = i_keyex[128*13-1:128*12]; 82 | 4'd2 : r_ka = i_keyex[128*12-1:128*11]; 83 | 4'd3 : r_ka = i_keyex[128*11-1:128*10]; 84 | 4'd4 : r_ka = i_keyex[128*10-1:128* 9]; 85 | 4'd5 : r_ka = i_keyex[128* 9-1:128* 8]; 86 | 4'd6 : r_ka = i_keyex[128* 8-1:128* 7]; 87 | 4'd7 : r_ka = i_keyex[128* 7-1:128* 6]; 88 | 4'd8 : r_ka = i_keyex[128* 6-1:128* 5]; 89 | 4'd9 : r_ka = i_keyex[128* 5-1:128* 4]; 90 | 4'd10: r_ka = i_keyex[128* 4-1:128* 3]; 91 | 4'd11: r_ka = i_keyex[128* 3-1:128* 2]; 92 | 4'd12: r_ka = i_keyex[128* 2-1:128* 1]; 93 | 4'd13: r_ka = i_keyex[128* 1-1:128* 0]; 94 | endcase 95 | end else begin //decrypt 96 | case(r_count) 97 | 4'd13: r_ka = i_keyex[128*15-1:128*14]; 98 | 4'd12: r_ka = i_keyex[128*14-1:128*13]; 99 | 4'd11: r_ka = i_keyex[128*13-1:128*12]; 100 | 4'd10: r_ka = i_keyex[128*12-1:128*11]; 101 | 4'd9 : r_ka = i_keyex[128*11-1:128*10]; 102 | 4'd8 : r_ka = i_keyex[128*10-1:128* 9]; 103 | 4'd7 : r_ka = i_keyex[128* 9-1:128* 8]; 104 | 4'd6 : r_ka = i_keyex[128* 8-1:128* 7]; 105 | 4'd5 : r_ka = i_keyex[128* 7-1:128* 6]; 106 | 4'd4 : r_ka = i_keyex[128* 6-1:128* 5]; 107 | 4'd3 : r_ka = i_keyex[128* 5-1:128* 4]; 108 | 4'd2 : r_ka = i_keyex[128* 4-1:128* 3]; 109 | 4'd1 : r_ka = i_keyex[128* 3-1:128* 2]; 110 | 4'd0 : r_ka = i_keyex[128* 2-1:128* 1]; 111 | endcase 112 | end 113 | end 114 | 115 | assign s_ikey = i_flag ? i_keyex[128*15-1:128*14] : i_keyex[128* 1-1:128* 0]; 116 | assign s_din = i_din_en ? i_din^s_ikey : r_din; 117 | assign s_dina = s_din[127:96]; //col-1 118 | assign s_dinb = s_din[95:64]; //col-2 119 | assign s_dinc = s_din[63:32]; //col-3 120 | assign s_dind = s_din[31:0]; //col-4 121 | 122 | //ShiftRows & SubBytes 123 | assign o_sbox_din[127:96] = i_flag ? {BS(s_dina,3),BS(s_dinb,2),BS(s_dinc,1),BS(s_dind,0)} : {BS(s_dina,3),BS(s_dind,2),BS(s_dinc,1),BS(s_dinb,0)}; 124 | assign o_sbox_din[95:64] = i_flag ? {BS(s_dinb,3),BS(s_dinc,2),BS(s_dind,1),BS(s_dina,0)} : {BS(s_dinb,3),BS(s_dina,2),BS(s_dind,1),BS(s_dinc,0)}; 125 | assign o_sbox_din[63:32] = i_flag ? {BS(s_dinc,3),BS(s_dind,2),BS(s_dina,1),BS(s_dinb,0)} : {BS(s_dinc,3),BS(s_dinb,2),BS(s_dina,1),BS(s_dind,0)}; 126 | assign o_sbox_din[31:0] = i_flag ? {BS(s_dind,3),BS(s_dina,2),BS(s_dinb,1),BS(s_dinc,0)} : {BS(s_dind,3),BS(s_dinc,2),BS(s_dinb,1),BS(s_dina,0)}; 127 | // 128 | assign s_mixc_din = i_flag ? i_sbox_dout : i_sbox_dout^r_ka; 129 | //MixColumns 130 | aes_mixcol_w u_mixcol_1( 131 | .i_din (s_mixc_din[127:96] ), 132 | .o_dout_x (s_mixc_doutx[127:96] ), 133 | .o_dout_y (s_mixc_douty[127:96] )); 134 | 135 | aes_mixcol_w u_mixcol_2( 136 | .i_din (s_mixc_din[95:64] ), 137 | .o_dout_x (s_mixc_doutx[95:64] ), 138 | .o_dout_y (s_mixc_douty[95:64] )); 139 | 140 | aes_mixcol_w u_mixcol_3( 141 | .i_din (s_mixc_din[63:32] ), 142 | .o_dout_x (s_mixc_doutx[63:32] ), 143 | .o_dout_y (s_mixc_douty[63:32] )); 144 | 145 | aes_mixcol_w u_mixcol_4( 146 | .i_din (s_mixc_din[31:0] ), 147 | .o_dout_x (s_mixc_doutx[31:0] ), 148 | .o_dout_y (s_mixc_douty[31:0] )); 149 | 150 | always@(posedge i_clk or posedge i_rst) begin 151 | if(i_rst) 152 | r_din <= #DLY 128'b0; 153 | else if(i_flag) 154 | r_din <= #DLY s_mixc_doutx^r_ka; 155 | else 156 | r_din <= #DLY s_mixc_douty; 157 | end 158 | 159 | assign o_dout = i_sbox_dout^r_ka; 160 | 161 | assign o_dout_en = (r_count==4'd13) ? 1'b1:1'b0; 162 | 163 | endmodule 164 | -------------------------------------------------------------------------------- /ciphers/xtea/xtea_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : xtea_dpc.v 19 | // Function : XTEA Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-23 24 | // Email : xcrypt@126.com 25 | // copyright : XCrypt Studio 26 | // ------------------------------------------------------------------------------ 27 | 28 | module xtea_dpc( 29 | input i_clk, 30 | input i_rst, 31 | input i_flag, 32 | input [1023:0] i_keyex_a, 33 | input [1023:0] i_keyex_b, 34 | input [63:0] i_din, 35 | input i_din_en, 36 | output [63:0] o_dout, 37 | output o_dout_en 38 | ); 39 | 40 | localparam DLY = 1; 41 | 42 | reg [2:0] r_count; 43 | reg [127:0] r_ka,r_kb; 44 | wire [31:0] s_y,s_ya,s_yb,s_yc,s_yd; 45 | wire [31:0] s_z,s_za,s_zb,s_zc,s_zd; 46 | reg [31:0] r_y,r_z; 47 | wire [1023:0] s_keyex_a,s_keyex_b; 48 | wire [63:0] s_din; 49 | 50 | function [31:0] LS4; 51 | input [31:0] D; 52 | begin 53 | LS4 = {D[27:0],4'b0}; 54 | end 55 | endfunction 56 | 57 | function [31:0] RS5; 58 | input [31:0] D; 59 | begin 60 | RS5 = {5'b0,D[31:5]}; 61 | end 62 | endfunction 63 | 64 | function [127:0] SWAP128; 65 | input [127:0] D; 66 | begin 67 | SWAP128 = {D[31:0],D[63:32],D[95:64],D[127:96]}; 68 | end 69 | endfunction 70 | 71 | always@(posedge i_clk or posedge i_rst) begin 72 | if(i_rst) 73 | r_count <= #DLY 3'b0; 74 | else if(i_din_en) 75 | r_count <= #DLY 3'd1; 76 | else if(r_count!=3'd0) 77 | r_count <= #DLY r_count + 3'd1; 78 | end 79 | 80 | always@(*) begin 81 | if(i_flag) begin //encrypt 82 | case(r_count) 83 | 3'd0: r_ka = s_keyex_a[128*8-1:128*7]; 84 | 3'd1: r_ka = s_keyex_a[128*7-1:128*6]; 85 | 3'd2: r_ka = s_keyex_a[128*6-1:128*5]; 86 | 3'd3: r_ka = s_keyex_a[128*5-1:128*4]; 87 | 3'd4: r_ka = s_keyex_a[128*4-1:128*3]; 88 | 3'd5: r_ka = s_keyex_a[128*3-1:128*2]; 89 | 3'd6: r_ka = s_keyex_a[128*2-1:128*1]; 90 | 3'd7: r_ka = s_keyex_a[128*1-1:128*0]; 91 | endcase 92 | end else begin //decrypt 93 | case(r_count) 94 | 3'd7: r_ka = SWAP128(s_keyex_a[128*8-1:128*7]); 95 | 3'd6: r_ka = SWAP128(s_keyex_a[128*7-1:128*6]); 96 | 3'd5: r_ka = SWAP128(s_keyex_a[128*6-1:128*5]); 97 | 3'd4: r_ka = SWAP128(s_keyex_a[128*5-1:128*4]); 98 | 3'd3: r_ka = SWAP128(s_keyex_a[128*4-1:128*3]); 99 | 3'd2: r_ka = SWAP128(s_keyex_a[128*3-1:128*2]); 100 | 3'd1: r_ka = SWAP128(s_keyex_a[128*2-1:128*1]); 101 | 3'd0: r_ka = SWAP128(s_keyex_a[128*1-1:128*0]); 102 | endcase 103 | end 104 | end 105 | 106 | always@(*) begin 107 | if(i_flag) begin //encrypt 108 | case(r_count) 109 | 3'd0: r_kb = s_keyex_b[128*8-1:128*7]; 110 | 3'd1: r_kb = s_keyex_b[128*7-1:128*6]; 111 | 3'd2: r_kb = s_keyex_b[128*6-1:128*5]; 112 | 3'd3: r_kb = s_keyex_b[128*5-1:128*4]; 113 | 3'd4: r_kb = s_keyex_b[128*4-1:128*3]; 114 | 3'd5: r_kb = s_keyex_b[128*3-1:128*2]; 115 | 3'd6: r_kb = s_keyex_b[128*2-1:128*1]; 116 | 3'd7: r_kb = s_keyex_b[128*1-1:128*0]; 117 | endcase 118 | end else begin //decrypt 119 | case(r_count) 120 | 3'd7: r_kb = SWAP128(s_keyex_b[128*8-1:128*7]); 121 | 3'd6: r_kb = SWAP128(s_keyex_b[128*7-1:128*6]); 122 | 3'd5: r_kb = SWAP128(s_keyex_b[128*6-1:128*5]); 123 | 3'd4: r_kb = SWAP128(s_keyex_b[128*5-1:128*4]); 124 | 3'd3: r_kb = SWAP128(s_keyex_b[128*4-1:128*3]); 125 | 3'd2: r_kb = SWAP128(s_keyex_b[128*3-1:128*2]); 126 | 3'd1: r_kb = SWAP128(s_keyex_b[128*2-1:128*1]); 127 | 3'd0: r_kb = SWAP128(s_keyex_b[128*1-1:128*0]); 128 | endcase 129 | end 130 | end 131 | 132 | assign s_din = i_flag ? i_din : {i_din[31:0],i_din[63:32]}; 133 | assign s_keyex_a = i_flag ? i_keyex_a : i_keyex_b; 134 | assign s_keyex_b = i_flag ? i_keyex_b : i_keyex_a; 135 | 136 | assign s_y = (r_count==3'd0) ? s_din[63:32]:r_y; 137 | assign s_z = (r_count==3'd0) ? s_din[31:0]:r_z ; 138 | 139 | assign s_ya = i_flag ? (s_y + (((LS4(s_z)^RS5(s_z)) + s_z)^r_ka[127:96])) : (s_y - (((LS4(s_z)^RS5(s_z)) + s_z)^r_ka[127:96])); 140 | assign s_za = i_flag ? (s_z + (((LS4(s_ya)^RS5(s_ya)) + s_ya)^r_kb[127:96])): (s_z - (((LS4(s_ya)^RS5(s_ya)) + s_ya)^r_kb[127:96])); 141 | assign s_yb = i_flag ? (s_ya + (((LS4(s_za)^RS5(s_za)) + s_za)^r_ka[95:64])) : (s_ya - (((LS4(s_za)^RS5(s_za)) + s_za)^r_ka[95:64])); 142 | assign s_zb = i_flag ? (s_za + (((LS4(s_yb)^RS5(s_yb)) + s_yb)^r_kb[95:64])) : (s_za - (((LS4(s_yb)^RS5(s_yb)) + s_yb)^r_kb[95:64])); 143 | assign s_yc = i_flag ? (s_yb + (((LS4(s_zb)^RS5(s_zb)) + s_zb)^r_ka[63:32])) : (s_yb - (((LS4(s_zb)^RS5(s_zb)) + s_zb)^r_ka[63:32])); 144 | assign s_zc = i_flag ? (s_zb + (((LS4(s_yc)^RS5(s_yc)) + s_yc)^r_kb[63:32])) : (s_zb - (((LS4(s_yc)^RS5(s_yc)) + s_yc)^r_kb[63:32])); 145 | assign s_yd = i_flag ? (s_yc + (((LS4(s_zc)^RS5(s_zc)) + s_zc)^r_ka[31:0])) : (s_yc - (((LS4(s_zc)^RS5(s_zc)) + s_zc)^r_ka[31:0])); 146 | assign s_zd = i_flag ? (s_zc + (((LS4(s_yd)^RS5(s_yd)) + s_yd)^r_kb[31:0])) : (s_zc - (((LS4(s_yd)^RS5(s_yd)) + s_yd)^r_kb[31:0])); 147 | 148 | always@(posedge i_clk) begin 149 | r_y <= #DLY s_yd; 150 | r_z <= #DLY s_zd; 151 | end 152 | 153 | assign o_dout = i_flag ? {s_yd,s_zd} :{s_zd,s_yd}; 154 | assign o_dout_en = (r_count==3'd7) ? 1'b1:1'b0; 155 | 156 | endmodule 157 | -------------------------------------------------------------------------------- /hashes/sha2/sha256_core.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : sha256_core.v 19 | // Function : SHA256 Hash Algorithm Core 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-1-24 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module sha256_core( 28 | input i_clk, //clock 29 | input i_rst, //reset high valid 30 | input i_start, //high valid(only one clock) 31 | input [511:0] i_data, //hash data input 32 | input [255:0] i_vin, //hash init value input(not change before o_done valid) 33 | output [255:0] o_vout, //hash value output 34 | output o_done); //high valid(only one clock) 35 | 36 | reg r_done; 37 | reg [6:0] r_count; 38 | reg [31:0] r_a,r_b,r_c,r_d,r_e,r_f,r_g,r_h; 39 | reg [2047:0] r_k; 40 | reg [511:0] r_w; 41 | reg [2:0] r_state; 42 | 43 | parameter IK = { 44 | 32'h428a2f98, 32'h71374491, 32'hb5c0fbcf, 32'he9b5dba5, 32'h3956c25b, 45 | 32'h59f111f1, 32'h923f82a4, 32'hab1c5ed5, 32'hd807aa98, 32'h12835b01, 46 | 32'h243185be, 32'h550c7dc3, 32'h72be5d74, 32'h80deb1fe, 32'h9bdc06a7, 47 | 32'hc19bf174, 32'he49b69c1, 32'hefbe4786, 32'h0fc19dc6, 32'h240ca1cc, 48 | 32'h2de92c6f, 32'h4a7484aa, 32'h5cb0a9dc, 32'h76f988da, 32'h983e5152, 49 | 32'ha831c66d, 32'hb00327c8, 32'hbf597fc7, 32'hc6e00bf3, 32'hd5a79147, 50 | 32'h06ca6351, 32'h14292967, 32'h27b70a85, 32'h2e1b2138, 32'h4d2c6dfc, 51 | 32'h53380d13, 32'h650a7354, 32'h766a0abb, 32'h81c2c92e, 32'h92722c85, 52 | 32'ha2bfe8a1, 32'ha81a664b, 32'hc24b8b70, 32'hc76c51a3, 32'hd192e819, 53 | 32'hd6990624, 32'hf40e3585, 32'h106aa070, 32'h19a4c116, 32'h1e376c08, 54 | 32'h2748774c, 32'h34b0bcb5, 32'h391c0cb3, 32'h4ed8aa4a, 32'h5b9cca4f, 55 | 32'h682e6ff3, 32'h748f82ee, 32'h78a5636f, 32'h84c87814, 32'h8cc70208, 56 | 32'h90befffa, 32'ha4506ceb, 32'hbef9a3f7, 32'hc67178f2 57 | }; 58 | 59 | //W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; 60 | //#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) 61 | //#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) 62 | function [31:0] WG; 63 | input [31:0] a,b,c,d; 64 | begin 65 | WG = ({a[16:0],a[31:17]}^{a[18:0],a[31:19]}^{10'b0,a[31:10]}) + b 66 | + ({c[6:0],c[31:7]}^{c[17:0],c[31:18]}^{3'b0,c[31:3]}) + d; 67 | end 68 | endfunction 69 | 70 | function [31:0] T0; 71 | input [31:0] a,b,c,d,e,f,g,h,k,w; 72 | begin 73 | T0 = h + ({e[5:0],e[31:6]}^{e[10:0],e[31:11]}^{e[24:0],e[31:25]}) 74 | + (g^(e&(f^g))) + k + w; 75 | end 76 | endfunction 77 | 78 | function [31:0] T1; 79 | input [31:0] a,b,c; 80 | begin 81 | T1 = ({a[1:0],a[31:2]}^{a[12:0],a[31:13]}^{a[21:0],a[31:22]}) 82 | + (((a | b) & c) | (a & b)); 83 | end 84 | endfunction 85 | 86 | always@(posedge i_clk or posedge i_rst) begin 87 | if(i_rst) begin 88 | r_done <= 1'b0; 89 | r_count <= 7'b0; 90 | r_a <= 32'b0; 91 | r_b <= 32'b0; 92 | r_c <= 32'b0; 93 | r_d <= 32'b0; 94 | r_e <= 32'b0; 95 | r_f <= 32'b0; 96 | r_g <= 32'b0; 97 | r_h <= 32'b0; 98 | r_k <= 2048'b0; 99 | r_w <= 512'b0; 100 | r_state <= 2'd0; 101 | end else begin 102 | case(r_state) 103 | 2'd0: begin 104 | r_done <= 1'b0; 105 | r_count <= 7'b0; 106 | if(i_start) begin 107 | r_a <= i_vin[255:224]; 108 | r_b <= i_vin[223:192]; 109 | r_c <= i_vin[191:160]; 110 | r_d <= i_vin[159:128]; 111 | r_e <= i_vin[127:96]; 112 | r_f <= i_vin[95:64]; 113 | r_g <= i_vin[63:32]; 114 | r_h <= i_vin[31:0]; 115 | r_k <= IK; 116 | r_w <= i_data; 117 | r_state <= 2'd1; 118 | end 119 | end 120 | 2'd1: begin 121 | r_count <= r_count + 7'b1; 122 | r_a <= T0(r_a,r_b,r_c,r_d,r_e,r_f,r_g,r_h,r_k[2047:2016],r_w[511:480])+T1(r_a,r_b,r_c); 123 | r_b <= r_a; 124 | r_c <= r_b; 125 | r_d <= r_c; 126 | r_e <= r_d + T0(r_a,r_b,r_c,r_d,r_e,r_f,r_g,r_h,r_k[2047:2016],r_w[511:480]); 127 | r_f <= r_e; 128 | r_g <= r_f; 129 | r_h <= r_g; 130 | r_k <= {r_k[2015:0],32'b0}; 131 | //r_w <= {r_w[511:480],WG(r_w[32*15-1:32*14],r_w[32*10-1:32*9],r_w[32*2-1:32*1],r_w[31:0])}; 132 | r_w <= {r_w[479:0],WG(r_w[32*2-1:32],r_w[32*7-1:32*6],r_w[32*15-1:32*14],r_w[32*16-1:32*15])}; 133 | if(r_count==7'd63) begin 134 | r_state <= 2'd2; 135 | end 136 | end 137 | 2'd2: begin 138 | r_a <= r_a + i_vin[255:224]; 139 | r_b <= r_b + i_vin[223:192]; 140 | r_c <= r_c + i_vin[191:160]; 141 | r_d <= r_d + i_vin[159:128]; 142 | r_e <= r_e + i_vin[127:96]; 143 | r_f <= r_f + i_vin[95:64]; 144 | r_g <= r_g + i_vin[63:32]; 145 | r_h <= r_h + i_vin[31:0]; 146 | r_done <= 1'b1; 147 | r_state <= 2'd0; 148 | end 149 | default: begin 150 | r_done <= 1'b0; 151 | r_count <= 7'b0; 152 | r_a <= 32'b0; 153 | r_b <= 32'b0; 154 | r_c <= 32'b0; 155 | r_d <= 32'b0; 156 | r_e <= 32'b0; 157 | r_f <= 32'b0; 158 | r_g <= 32'b0; 159 | r_h <= 32'b0; 160 | r_k <= 256'b0; 161 | r_w <= 512'b0; 162 | r_state <= 2'd0; 163 | end 164 | endcase 165 | end 166 | end 167 | 168 | assign o_done = r_done; 169 | assign o_vout = {r_a,r_b,r_c,r_d,r_e,r_f,r_g,r_h}; 170 | 171 | 172 | endmodule 173 | -------------------------------------------------------------------------------- /ciphers/rc6/rc6_dpc.v: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 XCrypt Studio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ------------------------------------------------------------------------------ 18 | // File name : rc6_dpc.v 19 | // Function : RC6 Cryptographic Algorithm Core Data Encrypt&Decrypt 20 | // ------------------------------------------------------------------------------ 21 | // Author : Xie 22 | // Version : v-1.0 23 | // Date : 2019-2-1 24 | // Email : xcrypt@126.com 25 | // ------------------------------------------------------------------------------ 26 | 27 | module rc6_dpc( 28 | input i_clk, 29 | input i_rst, 30 | input i_flag, 31 | input [32*44-1:0] i_keyex, 32 | input [127:0] i_din, 33 | input i_din_en, 34 | output [127:0] o_dout, 35 | output o_dout_en 36 | ); 37 | 38 | localparam DLY = 1; 39 | 40 | reg [4:0] r_count; 41 | wire [31:0] s_a,s_ax,s_ay,s_ay_e,s_ay_d; 42 | wire [31:0] s_b; 43 | wire [31:0] s_c,s_cx,s_cy,s_cy_e,s_cy_d; 44 | wire [31:0] s_d; 45 | wire [127:0] s_din; 46 | reg [127:0] r_din; 47 | reg [32*38-1:0] r_keyex; 48 | wire [63:0] s_fkey; 49 | wire [63:0] s_lkey; 50 | wire [63:0] s_ikey; 51 | wire [63:0] s_rkey; 52 | wire [127:0] s_pdin; 53 | wire [4:0] s_rr_x,s_rr_y; 54 | wire [31:0] s_rdin_x,s_rdin_y; 55 | wire [31:0] s_rdout_x,s_rdout_y; 56 | wire [31:0] s_t,s_u; 57 | 58 | function [31:0] SWAP; 59 | input [31:0] D; 60 | begin 61 | SWAP = {D[7:0],D[15:8],D[23:16],D[31:24]}; 62 | end 63 | endfunction 64 | 65 | function [31:0] ROL5; 66 | input [31:0] D; 67 | begin 68 | ROL5 = {D[26:0],D[31:27]}; 69 | end 70 | endfunction 71 | 72 | always@(posedge i_clk or posedge i_rst) begin 73 | if(i_rst) 74 | r_count <= #DLY 5'b0; 75 | else if(i_din_en) 76 | r_count <= #DLY 5'd1; 77 | else if(r_count==5'd19) 78 | r_count <= #DLY 5'b0; 79 | else if(r_count!=5'd0) 80 | r_count <= #DLY r_count + 5'd1; 81 | end 82 | 83 | always@(posedge i_clk or posedge i_rst) begin 84 | if(i_rst) 85 | r_keyex <= #DLY 'b0; 86 | else if(i_din_en) begin 87 | if(i_flag) 88 | r_keyex <= #DLY i_keyex[32*40-1:32*2]; 89 | else 90 | r_keyex <= #DLY i_keyex[32*42-1:32*4]; 91 | end else if(r_count!=5'd0)begin 92 | if(i_flag) 93 | r_keyex <= #DLY {r_keyex[32*36-1:0],64'b0}; 94 | else 95 | r_keyex <= #DLY {64'b0,r_keyex[32*38-1:64]}; 96 | end 97 | end 98 | 99 | assign s_fkey = i_flag ? i_keyex[32*44-1:32*42]:i_keyex[32*2-1:0]; //first 100 | assign s_lkey = i_flag ? i_keyex[32*2-1:0]:i_keyex[32*44-1:32*42]; //last 101 | assign s_ikey = i_flag ? i_keyex[32*42-1:32*40] : i_keyex[32*4-1:32*2]; 102 | assign s_rkey = i_flag ? r_keyex[32*38-1:32*36] : r_keyex[32*2-1:0]; 103 | 104 | assign s_pdin = i_flag ? {SWAP(i_din[127:96]),(SWAP(i_din[95:64])+s_fkey[63:32]),SWAP(i_din[63:32]),(SWAP(i_din[31:0])+s_fkey[31:0])} 105 | : {SWAP(i_din[127:96])-s_fkey[63:32],SWAP(i_din[95:64]),SWAP(i_din[63:32])-s_fkey[31:0],SWAP(i_din[31:0])}; 106 | 107 | assign s_din = i_din_en ? s_pdin : r_din; 108 | 109 | assign s_a = i_flag ? s_din[127:96] : s_din[31:0]; 110 | assign s_b = i_flag ? s_din[95:64] : s_din[127:96]; 111 | assign s_c = i_flag ? s_din[63:32] : s_din[95:64]; 112 | assign s_d = i_flag ? s_din[31:0] : s_din[63:32]; 113 | 114 | //---ENCRYPT--- 115 | // for (r = 0; r < 20; r += 4) { 116 | // RND(a,b,c,d); 117 | // RND(b,c,d,a); 118 | // RND(c,d,a,b); 119 | // RND(d,a,b,c); 120 | // } 121 | // t = (b * (b + b + 1)); t = ROLc(t, 5); \ encrypt 122 | // u = (d * (d + d + 1)); u = ROLc(u, 5); \ 123 | // a = ROL(a^t,u) + K[0]; \ 124 | // c = ROL(c^u,t) + K[1]; K += 2; 125 | //---DECRYPT--- 126 | // for (r = 0; r < 20; r += 4) { 127 | // RND(d,a,b,c); 128 | // RND(c,d,a,b); 129 | // RND(b,c,d,a); 130 | // RND(a,b,c,d); 131 | // } 132 | // t = (b * (b + b + 1)); t = ROLc(t, 5); \ decrypt 133 | // u = (d * (d + d + 1)); u = ROLc(u, 5); \ 134 | // c = ROR(c - K[1], t) ^ u; \ 135 | // a = ROR(a - K[0], u) ^ t; K -= 2; 136 | 137 | assign s_t = ROL5(s_b*(s_b + s_b + 1)); 138 | assign s_u = ROL5(s_d*(s_d + s_d + 1)); 139 | assign s_rr_x = i_flag ? s_u[4:0] : (32-s_t[4:0]); 140 | assign s_rr_y = i_flag ? s_t[4:0] : (32-s_u[4:0]); 141 | assign s_rdin_x = i_flag ? s_a^s_t : (i_din_en ? (s_c-s_ikey[31:0]):(s_c-s_rkey[31:0])); 142 | assign s_rdin_y = i_flag ? s_c^s_u : (i_din_en ? (s_a-s_ikey[63:32]):(s_a-s_rkey[63:32])); 143 | 144 | rc6_rol u_rol1(.round(s_rr_x),.din(s_rdin_x),.dout(s_rdout_x)); 145 | rc6_rol u_rol2(.round(s_rr_y),.din(s_rdin_y),.dout(s_rdout_y)); 146 | 147 | assign s_ax = i_flag ? s_rdout_x : s_rdout_y^s_t; 148 | assign s_cx = i_flag ? s_rdout_y : s_rdout_x^s_u; 149 | 150 | assign s_ay_e = i_din_en ? (s_ax + s_ikey[63:32]):(s_ax + s_rkey[63:32]); 151 | assign s_cy_e = i_din_en ? (s_cx + s_ikey[31:0]):(s_cx + s_rkey[31:0]); 152 | 153 | assign s_cy_d = s_cx; 154 | assign s_ay_d = s_ax; 155 | 156 | assign s_ay = i_flag ? s_ay_e : s_ay_d; 157 | assign s_cy = i_flag ? s_cy_e : s_cy_d; 158 | 159 | always@(posedge i_clk or posedge i_rst) begin 160 | if(i_rst) 161 | r_din <= #DLY 64'b0; 162 | else if(i_flag) 163 | r_din <= #DLY {s_b,s_cy,s_d,s_ay}; 164 | else 165 | r_din <= #DLY {s_ay,s_b,s_cy,s_d}; 166 | end 167 | 168 | assign o_dout = i_flag ? {SWAP(s_b+s_lkey[63:32]),SWAP(s_cy),SWAP(s_d+s_lkey[31:0]),SWAP(s_ay)} 169 | : {SWAP(s_ay),SWAP(s_b-s_lkey[63:32]),SWAP(s_cy),SWAP(s_d-s_lkey[31:0])}; 170 | 171 | assign o_dout_en = (r_count==5'd19) ? 1'b1:1'b0; 172 | 173 | endmodule 174 | --------------------------------------------------------------------------------