├── README.md ├── inv_key_gen.v ├── inv_sbox.v ├── inv_shift_row.v ├── inv_sub_byte.v ├── main.v ├── mix_col.v ├── sbox.v ├── sub_byte.v └── testkeygen.v /README.md: -------------------------------------------------------------------------------- 1 | # 128-Bit-AES-Encryption-and-Decryption 2 | This is a project meant to be run on an FPGA that was Implemented in the Verilog HDL using Xilinx ISE design suite. 3 | 4 | The project is split into Five separate modules that make up the AES. 5 | Each module has been written in a Verilog file (.v) and has been instantiated in the 'main' module. 6 | The input data and key (each 128-bit long) need to be given through a test bench. 7 | This project was made with the Xilinx Spartan 3E FPGA in mind. 8 | -------------------------------------------------------------------------------- /inv_key_gen.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 18:56:43 07/05/2017 7 | // Design Name: 8 | // Module Name: inv_key_gen 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module inv_key_gen(temp_key,mx_key,rcon,ko,r_key); 22 | 23 | input [0:127] temp_key,mx_key; 24 | input [0:31] rcon; 25 | output [0:127] ko,r_key; 26 | wire [0:127] ko; 27 | 28 | //shifting operation 29 | 30 | wire [0:127] key,key2; 31 | assign key[0:95] = temp_key[0:95]; 32 | assign key[96:103] = temp_key[104:111]; 33 | assign key[104:111] = temp_key[112:119]; 34 | assign key[112:119] = temp_key[120:127]; 35 | assign key[120:127] = temp_key[96:103]; 36 | 37 | //sub_bytes operation 38 | 39 | inv_sbox s1(key[96:99],key[100:103],key2[96:103]); 40 | inv_sbox s2(key[104:107],key[108:111],key2[104:111]); 41 | inv_sbox s3(key[112:115],key[116:119],key2[112:119]); 42 | inv_sbox s4(key[120:123],key[124:127],key2[120:127]); 43 | 44 | //multiplication with rcon values 45 | //xor with different columns 46 | //ko is also numbered column wise 47 | 48 | assign ko[0:31] = key2[96:127]^key[0:31]^rcon[0:31]; 49 | assign ko[32:63] = key2[96:127]^key[0:31]^key[32:63]^rcon[0:31]; 50 | assign ko[64:95] = key2[96:127]^key[0:31]^key[32:63]^key[64:95]^rcon[0:31]; 51 | assign ko[96:127] = key2[96:127]^key[0:31]^key[32:63]^key[64:95]^temp_key[96:127]^rcon[0:31]; 52 | 53 | //add round operation 54 | 55 | assign r_key[0:127] = mx_key[0:127]^ko[0:127]; 56 | 57 | 58 | endmodule 59 | -------------------------------------------------------------------------------- /inv_sbox.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 18:11:31 07/05/2017 7 | // Design Name: 8 | // Module Name: inv_sbox 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module inv_sbox(x,y,isbout); 22 | input [0:3] x,y; 23 | output reg [0:3] sbout; 24 | always@(x,y) 25 | begin 26 | case({x,y}) 27 | 8'h00:sbout =8'h52; 28 | 8'h01:sbout =8'h09; 29 | 8'h02:sbout =8'h6a; 30 | 8'h03:sbout =8'hd5; 31 | 8'h04:sbout =8'h30; 32 | 8'h05:sbout =8'h36; 33 | 8'h06:sbout =8'ha5; 34 | 8'h07:sbout =8'h38; 35 | 8'h08:sbout =8'hbf; 36 | 8'h09:sbout =8'h40; 37 | 8'h0a:sbout =8'ha3; 38 | 8'h0b:sbout =8'h9e; 39 | 8'h0c:sbout =8'h81; 40 | 8'h0d:sbout =8'hf3; 41 | 8'h0e:sbout =8'hd7; 42 | 8'h0f:sbout =8'hfb; 43 | 8'h10:sbout =8'h7c; 44 | 8'h11:sbout =8'he3; 45 | 8'h12:sbout =8'h39; 46 | 8'h13:sbout =8'h82; 47 | 8'h14:sbout =8'h9b; 48 | 8'h15:sbout =8'h2f; 49 | 8'h16:sbout =8'hff; 50 | 8'h17:sbout =8'h87; 51 | 8'h18:sbout =8'h34; 52 | 8'h19:sbout =8'h8e; 53 | 8'h1a:sbout =8'h43; 54 | 8'h1b:sbout =8'h44; 55 | 8'h1c:sbout =8'hc4; 56 | 8'h1d:sbout =8'hde; 57 | 8'h1e:sbout =8'he9; 58 | 8'h1f:sbout =8'hcb; 59 | 8'h20:sbout =8'h54; 60 | 8'h21:sbout =8'h7b; 61 | 8'h22:sbout =8'h94; 62 | 8'h23:sbout =8'h32; 63 | 8'h24:sbout =8'ha6; 64 | 8'h25:sbout =8'hc2; 65 | 8'h26:sbout =8'h23; 66 | 8'h27:sbout =8'h3d; 67 | 8'h28:sbout =8'hee; 68 | 8'h29:sbout =8'h4c; 69 | 8'h2a:sbout =8'h95; 70 | 8'h2b:sbout =8'h0b; 71 | 8'h2c:sbout =8'h42; 72 | 8'h2d:sbout =8'hfa; 73 | 8'h2e:sbout =8'hc3; 74 | 8'h2f:sbout =8'h4e; 75 | 8'h30:sbout =8'h08; 76 | 8'h31:sbout =8'h2e; 77 | 8'h32:sbout =8'ha1; 78 | 8'h33:sbout =8'h66; 79 | 8'h34:sbout =8'h28; 80 | 8'h35:sbout =8'hd9; 81 | 8'h36:sbout =8'h24; 82 | 8'h37:sbout =8'hb2; 83 | 8'h38:sbout =8'h76; 84 | 8'h39:sbout =8'h5b; 85 | 8'h3a:sbout =8'ha2; 86 | 8'h3b:sbout =8'h49; 87 | 8'h3c:sbout =8'h6d; 88 | 8'h3d:sbout =8'h8b; 89 | 8'h3e:sbout =8'hd1; 90 | 8'h3f:sbout =8'h25; 91 | 8'h40:sbout =8'h72; 92 | 8'h41:sbout =8'hf8; 93 | 8'h42:sbout =8'hf6; 94 | 8'h43:sbout =8'h64; 95 | 8'h44:sbout =8'h86; 96 | 8'h45:sbout =8'h68; 97 | 8'h46:sbout =8'h98; 98 | 8'h47:sbout =8'h16; 99 | 8'h48:sbout =8'hd4; 100 | 8'h49:sbout =8'ha4; 101 | 8'h4a:sbout =8'h5c; 102 | 8'h4b:sbout =8'hcc; 103 | 8'h4c:sbout =8'h5d; 104 | 8'h4d:sbout =8'h65; 105 | 8'h4e:sbout =8'hb6; 106 | 8'h4f:sbout =8'h92; 107 | 8'h50:sbout =8'h6c; 108 | 8'h51:sbout =8'h70; 109 | 8'h52:sbout =8'h48; 110 | 8'h53:sbout =8'h50; 111 | 8'h54:sbout =8'hfd; 112 | 8'h55:sbout =8'hed; 113 | 8'h56:sbout =8'hb9; 114 | 8'h57:sbout =8'hda; 115 | 8'h58:sbout =8'h5e; 116 | 8'h59:sbout =8'h15; 117 | 8'h5a:sbout =8'h46; 118 | 8'h5b:sbout =8'h57; 119 | 8'h5c:sbout =8'ha7; 120 | 8'h5d:sbout =8'h8d; 121 | 8'h5e:sbout =8'h9d; 122 | 8'h5f:sbout =8'h84; 123 | 8'h60:sbout =8'h90; 124 | 8'h61:sbout =8'hd8; 125 | 8'h62:sbout =8'hab; 126 | 8'h63:sbout =8'h00; 127 | 8'h64:sbout =8'h8c; 128 | 8'h65:sbout =8'hbc; 129 | 8'h66:sbout =8'hd3; 130 | 8'h67:sbout =8'h0a; 131 | 8'h68:sbout =8'hf7; 132 | 8'h69:sbout =8'he4; 133 | 8'h6a:sbout =8'h58; 134 | 8'h6b:sbout =8'h05; 135 | 8'h6c:sbout =8'hb8; 136 | 8'h6d:sbout =8'hb3; 137 | 8'h6e:sbout =8'h45; 138 | 8'h6f:sbout =8'h06; 139 | 8'h70:sbout =8'hd0; 140 | 8'h71:sbout =8'h2c; 141 | 8'h72:sbout =8'h1e; 142 | 8'h73:sbout =8'h8f; 143 | 8'h74:sbout =8'hca; 144 | 8'h75:sbout =8'h3f; 145 | 8'h76:sbout =8'h0f; 146 | 8'h77:sbout =8'h02; 147 | 8'h78:sbout =8'hc1; 148 | 8'h79:sbout =8'haf; 149 | 8'h7a:sbout =8'hbd; 150 | 8'h7b:sbout =8'h03; 151 | 8'h7c:sbout =8'h01; 152 | 8'h7d:sbout =8'h13; 153 | 8'h7e:sbout =8'h8a; 154 | 8'h7f:sbout =8'h6b; 155 | 8'h80:sbout =8'h3a; 156 | 8'h81:sbout =8'h91; 157 | 8'h82:sbout =8'h11; 158 | 8'h83:sbout =8'h41; 159 | 8'h84:sbout =8'h4f; 160 | 8'h85:sbout =8'h67; 161 | 8'h86:sbout =8'hdc; 162 | 8'h87:sbout =8'hea; 163 | 8'h88:sbout =8'h97; 164 | 8'h89:sbout =8'hf2; 165 | 8'h8a:sbout =8'hcf; 166 | 8'h8b:sbout =8'hce; 167 | 8'h8c:sbout =8'hf0; 168 | 8'h8d:sbout =8'hb4; 169 | 8'h8e:sbout =8'he6; 170 | 8'h8f:sbout =8'h73; 171 | 8'h90:sbout =8'h96; 172 | 8'h91:sbout =8'hac; 173 | 8'h92:sbout =8'h74; 174 | 8'h93:sbout =8'h22; 175 | 8'h94:sbout =8'he7; 176 | 8'h95:sbout =8'had; 177 | 8'h96:sbout =8'h35; 178 | 8'h97:sbout =8'h85; 179 | 8'h98:sbout =8'he2; 180 | 8'h99:sbout =8'hf9; 181 | 8'h9a:sbout =8'h37; 182 | 8'h9b:sbout =8'he8; 183 | 8'h9c:sbout =8'h1c; 184 | 8'h9d:sbout =8'h75; 185 | 8'h9e:sbout =8'hdf; 186 | 8'h9f:sbout =8'h6e; 187 | 8'ha0:sbout =8'h47; 188 | 8'ha1:sbout =8'hf1; 189 | 8'ha2:sbout =8'h1a; 190 | 8'ha3:sbout =8'h71; 191 | 8'ha4:sbout =8'h1d; 192 | 8'ha5:sbout =8'h29; 193 | 8'ha6:sbout =8'hc5; 194 | 8'ha7:sbout =8'h89; 195 | 8'ha8:sbout =8'h6f; 196 | 8'ha9:sbout =8'hb7; 197 | 8'haa:sbout =8'h62; 198 | 8'hab:sbout =8'h0e; 199 | 8'hac:sbout =8'haa; 200 | 8'had:sbout =8'h18; 201 | 8'hae:sbout =8'hbe; 202 | 8'haf:sbout =8'h1b; 203 | 8'hb0:sbout =8'hfc; 204 | 8'hb1:sbout =8'h56; 205 | 8'hb2:sbout =8'h3e; 206 | 8'hb3:sbout =8'h4b; 207 | 8'hb4:sbout =8'hc6; 208 | 8'hb5:sbout =8'hd2; 209 | 8'hb6:sbout =8'h79; 210 | 8'hb7:sbout =8'h20; 211 | 8'hb8:sbout =8'h9a; 212 | 8'hb9:sbout =8'hdb; 213 | 8'hba:sbout =8'hc0; 214 | 8'hbb:sbout =8'hfe; 215 | 8'hbc:sbout =8'h78; 216 | 8'hbd:sbout =8'hcd; 217 | 8'hbe:sbout =8'h5a; 218 | 8'hbf:sbout =8'hf4; 219 | 8'hc0:sbout =8'h1f; 220 | 8'hc1:sbout =8'hdd; 221 | 8'hc2:sbout =8'ha8; 222 | 8'hc3:sbout =8'h33; 223 | 8'hc4:sbout =8'h88; 224 | 8'hc5:sbout =8'h07; 225 | 8'hc6:sbout =8'hc7; 226 | 8'hc7:sbout =8'h31; 227 | 8'hc8:sbout =8'hb1; 228 | 8'hc9:sbout =8'h12; 229 | 8'hca:sbout =8'h10; 230 | 8'hcb:sbout =8'h59; 231 | 8'hcc:sbout =8'h27; 232 | 8'hcd:sbout =8'h80; 233 | 8'hce:sbout =8'hec; 234 | 8'hcf:sbout =8'h5f; 235 | 8'hd0:sbout =8'h60; 236 | 8'hd1:sbout =8'h51; 237 | 8'hd2:sbout =8'h7f; 238 | 8'hd3:sbout =8'ha9; 239 | 8'hd4:sbout =8'h19; 240 | 8'hd5:sbout =8'hb5; 241 | 8'hd6:sbout =8'h4a; 242 | 8'hd7:sbout =8'h0d; 243 | 8'hd8:sbout =8'h2d; 244 | 8'hd9:sbout =8'he5; 245 | 8'hda:sbout =8'h7a; 246 | 8'hdb:sbout =8'h9f; 247 | 8'hdc:sbout =8'h93; 248 | 8'hdd:sbout =8'hc9; 249 | 8'hde:sbout =8'h9c; 250 | 8'hdf:sbout =8'hef; 251 | 8'he0:sbout =8'ha0; 252 | 8'he1:sbout =8'he0; 253 | 8'he2:sbout =8'h3b; 254 | 8'he3:sbout =8'h4d; 255 | 8'he4:sbout =8'hae; 256 | 8'he5:sbout =8'h2a; 257 | 8'he6:sbout =8'hf5; 258 | 8'he7:sbout =8'hb0; 259 | 8'he8:sbout =8'hc8; 260 | 8'he9:sbout =8'heb; 261 | 8'hea:sbout =8'hbb; 262 | 8'heb:sbout =8'h3c; 263 | 8'hec:sbout =8'h83; 264 | 8'hed:sbout =8'h53; 265 | 8'hee:sbout =8'h99; 266 | 8'hef:sbout =8'h61; 267 | 8'hf0:sbout =8'h17; 268 | 8'hf1:sbout =8'h2b; 269 | 8'hf2:sbout =8'h04; 270 | 8'hf3:sbout =8'h7e; 271 | 8'hf4:sbout =8'hba; 272 | 8'hf5:sbout =8'h77; 273 | 8'hf6:sbout =8'hd6; 274 | 8'hf7:sbout =8'h26; 275 | 8'hf8:sbout =8'he1; 276 | 8'hf9:sbout =8'h69; 277 | 8'hfa:sbout =8'h14; 278 | 8'hfb:sbout =8'h63; 279 | 8'hfc:sbout =8'h55; 280 | 8'hfd:sbout =8'h21; 281 | 8'hfe:sbout =8'h0c; 282 | 8'hff:sbout =8'h7d; 283 | endcase 284 | end 285 | endmodule 286 | -------------------------------------------------------------------------------- /inv_shift_row.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 18:12:42 07/05/2017 7 | // Design Name: 8 | // Module Name: inv_shift_row 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module inv_shift_row(s_state,shifted_state); 22 | input [0:127] s_state; 23 | inout [0:127] shifted_state; 24 | assign shifted_state[0:7] = s_state[0:7];//0 25 | assign shifted_state[8:15] = s_state[104:111];//1 26 | assign shifted_state[16:23] = s_state[80:87];//2 27 | assign shifted_state[24:31] = s_state[56:63];//3 28 | 29 | assign shifted_state[32:39] = s_state[32:39];//4 30 | assign shifted_state[40:47] = s_state[0:7];//5 31 | assign shifted_state[48:55] = s_state[48:55];//6 32 | assign shifted_state[56:63] = s_state[88:95];//7 33 | 34 | assign shifted_state[64:71] = s_state[64:71];//8 35 | assign shifted_state[72:79] = s_state[40:47];//9 36 | assign shifted_state[80:87] = s_state[16:23];//10 37 | assign shifted_state[88:95] = s_state[120:127];//11 38 | 39 | assign shifted_state[96:103] = s_state[96:103];//12 40 | assign shifted_state[104:111] = s_state[72:79];//13s 41 | assign shifted_state[112:119] = s_state[48:55];//14 42 | assign shifted_state[120:127] = s_state[56:63];//15 43 | endmodule 44 | -------------------------------------------------------------------------------- /inv_sub_byte.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 19:01:39 07/05/2017 7 | // Design Name: 8 | // Module Name: inv_sub_byte 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module inv_sub_byte(in_key,out_key); 22 | input [0:127] in_key; 23 | output [0:127] out_key; 24 | 25 | inv_sbox sb5(in_key[0:3],in_key[4:7],out_key[0:7]); 26 | inv_sbox sb6(in_key[8:11],in_key[12:15],out_key[8:15]); 27 | inv_sbox sb7(in_key[16:19],in_key[20:23],out_key[16:23]); 28 | inv_sbox sb8(in_key[24:27],in_key[28:31],out_key[24:31]); 29 | inv_sbox sb9(in_key[32:35],in_key[36:39],out_key[32:39]); 30 | inv_sbox sb10(in_key[40:43],in_key[44:47],out_key[40:47]); 31 | inv_sbox sb11(in_key[48:51],in_key[52:55],out_key[48:55]); 32 | inv_sbox sb12(in_key[56:59],in_key[60:63],out_key[56:63]); 33 | inv_sbox sb13(in_key[64:67],in_key[68:71],out_key[64:71]); 34 | inv_sbox sb14(in_key[72:75],in_key[76:79],out_key[72:79]); 35 | inv_sbox sb15(in_key[80:83],in_key[84:87],out_key[80:87]); 36 | inv_sbox sb16(in_key[88:91],in_key[92:95],out_key[88:95]); 37 | inv_sbox sb17(in_key[96:99],in_key[100:103],out_key[96:103]); 38 | inv_sbox sb18(in_key[104:107],in_key[108:111],out_key[104:111]); 39 | inv_sbox sb19(in_key[112:115],in_key[116:119],out_key[112:119]); 40 | inv_sbox sb20(in_key[120:123],in_key[124:127],out_key[120:127]); 41 | 42 | endmodule 43 | -------------------------------------------------------------------------------- /main.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 12:11:38 06/10/2017 7 | // Design Name: 8 | // Module Name: main 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module main(data,key,en_key);// 22 | input [0:127] data,key; 23 | output [0:127] en_key; 24 | wire [0:127] en_key0,en_key1,en_key2,en_key3,en_key4,en_key5,en_key6,en_key7,en_key8; 25 | wire [0:127] s_key0,s_key1,s_key2,s_key3,s_key4,s_key5,s_key6,s_key7,s_key8,s_key9; 26 | wire [0:127] sh_key0,sh_key1,sh_key2,sh_key3,sh_key4,sh_key5,sh_key6,sh_key7,sh_key8,sh_key9; 27 | wire [0:127] mx_key0,mx_key1,mx_key2,mx_key3,mx_key4,mx_key5,mx_key6,mx_key7,mx_key8; 28 | wire [0:127] gen_key0,gen_key1,gen_key2,gen_key3,gen_key4,gen_key5,gen_key6,gen_key7,gen_key8,gen_key9; 29 | wire [0:127] pr_key; 30 | //pre round operation 31 | 32 | assign pr_key = data^key; 33 | 34 | //rounds begin 35 | 36 | //*****************************round1***************************************** 37 | 38 | //sub bytes operation 39 | 40 | sub_byte s0(pr_key,s_key0); 41 | 42 | //mix column 43 | 44 | mix_col m0(s_key0,mx_key0); 45 | 46 | //add round key operation 47 | 48 | key_gen k0(key,mx_key0,32'h01000000,gen_key0,en_key0); 49 | 50 | //****************************************ROUND2******************************** 51 | 52 | //sub bytes operation 53 | 54 | sub_byte s1(en_key0,s_key1); 55 | 56 | //mix column 57 | 58 | mix_col m1(s_key1,mx_key1); 59 | 60 | //add round key operation 61 | 62 | key_gen k1(gen_key0,mx_key1,32'h02000000,gen_key1,en_key1); 63 | 64 | //******************************ROUND3************************************** 65 | 66 | //sub bytes operation 67 | 68 | sub_byte s2(en_key1,s_key2); 69 | 70 | //mix column 71 | 72 | mix_col m2(s_key2,mx_key2); 73 | 74 | //add round key operation 75 | 76 | key_gen k2(gen_key1,mx_key2,32'h04000000,gen_key2,en_key2); 77 | 78 | //*******************************ROUND4************************************ 79 | 80 | //sub bytes operation 81 | 82 | sub_byte s3(en_key2,s_key3); 83 | 84 | //mix column 85 | 86 | mix_col m3(s_key3,mx_key3); 87 | 88 | //add round key operation 89 | 90 | key_gen k3(gen_key2,mx_key3,32'h08000000,gen_key3,en_key3); 91 | 92 | //*********************************ROUND5********************************* 93 | 94 | //sub bytes operation 95 | 96 | sub_byte s4(en_key3,s_key4); 97 | 98 | //mix column 99 | 100 | mix_col m4(s_key4,mx_key4); 101 | 102 | //add round key operation 103 | 104 | key_gen k4(gen_key3,mx_key4,32'h10000000,gen_key4,en_key4); 105 | 106 | //**********************************ROUND6********************************** 107 | 108 | //sub bytes operation 109 | 110 | sub_byte s5(en_key4,s_key5); 111 | 112 | //mix column 113 | 114 | mix_col m5(s_key5,mx_key5); 115 | 116 | //add round key operation 117 | 118 | key_gen k5(gen_key4,mx_key5,32'h20000000,gen_key5,en_key5); 119 | 120 | //*******************************ROUND7************************************* 121 | 122 | //sub bytes operation 123 | 124 | sub_byte s6(en_key5,s_key6); 125 | 126 | //mix column 127 | 128 | mix_col m6(s_key6,mx_key6); 129 | 130 | //add round key operation 131 | 132 | key_gen k6(gen_key5,mx_key6,32'h40000000,gen_key6,en_key6); 133 | 134 | //**************************ROUND8******************************************** 135 | 136 | //sub bytes operation 137 | 138 | sub_byte s7(en_key6,s_key7); 139 | 140 | //mix column 141 | 142 | mix_col m7(s_key7,mx_key7); 143 | 144 | //add round key operation 145 | 146 | key_gen k7(gen_key6,mx_key7,32'h80000000,gen_key7,en_key7); 147 | 148 | //****************************ROUND9***************************************** 149 | 150 | //sub bytes operation 151 | 152 | sub_byte s8(en_key7,s_key8); 153 | 154 | //mix column 155 | 156 | mix_col m8(s_key8,mx_key8); 157 | 158 | //add round key operation 159 | 160 | key_gen k8(gen_key7,mx_key8,32'h1b000000,gen_key8,en_key8); 161 | 162 | 163 | 164 | //********************************FINAL ROUND********************************** 165 | 166 | //sub bytes operation 167 | 168 | sub_byte s9(en_key8,s_key9); 169 | 170 | //shift row 171 | 172 | shift_row sh9(s_key9,sh_key9); 173 | 174 | //no mix column 175 | 176 | //add round key operation 177 | 178 | key_gen k9(gen_key8,sh_key9,32'h36000000,gen_key9,en_key); 179 | 180 | endmodule 181 | -------------------------------------------------------------------------------- /mix_col.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 15:15:05 06/09/2017 7 | // Design Name: 8 | // Module Name: mix_col 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module mix_col(i_shift,i_mix 22 | ); 23 | input [0:127]i_shift; 24 | output [0:127]i_mix; 25 | 26 | assign i_mix[0:7] = xtime(i_shift[0:7])^xtime(i_shift[8:15])^i_shift[8:15]^i_shift[16:23]^i_shift[24:31]; 27 | assign i_mix[8:15] = i_shift[0:7]^xtime(i_shift[8:15])^xtime(i_shift[16:23])^i_shift[16:23]^i_shift[24:31]; 28 | assign i_mix[16:23] = i_shift[0:7]^i_shift[8:15]^xtime(i_shift[16:23])^xtime(i_shift[24:31])^i_shift[24:31]; 29 | assign i_mix[24:31] = xtime(i_shift[0:7])^i_shift[0:7]^i_shift[8:15]^i_shift[16:23]^xtime(i_shift[24:31]); 30 | 31 | assign i_mix[32:39] = xtime(i_shift[32:39])^xtime(i_shift[40:47])^i_shift[40:47]^i_shift[48:55]^i_shift[56:63]; 32 | assign i_mix[40:47] = i_shift[32:39]^xtime(i_shift[40:47])^xtime(i_shift[48:55])^i_shift[48:55]^i_shift[56:63]; 33 | assign i_mix[48:55] = i_shift[32:39]^i_shift[40:47]^xtime(i_shift[48:55])^xtime(i_shift[56:63])^i_shift[56:63]; 34 | assign i_mix[56:63] = xtime(i_shift[32:39])^i_shift[32:39]^i_shift[40:47]^i_shift[48:55]^xtime(i_shift[56:63]); 35 | 36 | assign i_mix[64:71] = xtime(i_shift[64:71])^xtime(i_shift[72:79])^i_shift[72:79]^i_shift[80:87]^i_shift[88:95]; 37 | assign i_mix[72:79] = i_shift[64:71]^xtime(i_shift[72:79])^xtime(i_shift[80:87])^i_shift[80:87]^i_shift[88:95]; 38 | assign i_mix[80:87] = i_shift[64:71]^i_shift[72:79]^xtime(i_shift[80:87])^xtime(i_shift[88:95])^i_shift[88:95]; 39 | assign i_mix[88:95] = xtime(i_shift[64:71])^i_shift[64:71]^i_shift[72:79]^i_shift[80:87]^xtime(i_shift[88:95]); 40 | 41 | assign i_mix[96:103] = xtime(i_shift[96:103])^xtime(i_shift[104:111])^i_shift[104:111]^i_shift[112:119]^i_shift[120:127]; 42 | assign i_mix[104:111] = i_shift[96:103]^xtime(i_shift[104:111])^xtime(i_shift[112:119])^i_shift[112:119]^i_shift[120:127]; 43 | assign i_mix[112:119] = i_shift[96:103]^i_shift[104:111]^xtime(i_shift[112:119])^xtime(i_shift[120:127])^i_shift[120:127]; 44 | assign i_mix[120:127] = xtime(i_shift[96:103])^i_shift[96:103]^i_shift[104:111]^i_shift[112:119]^xtime(i_shift[120:127]); 45 | 46 | function [0:7]xtime; 47 | input [0:7]i; 48 | begin 49 | if(i[0] ==0) 50 | xtime = {i[1:7],1'b0}; 51 | else 52 | xtime = {i[1:7],1'b0}^8'h1b; 53 | end 54 | endfunction 55 | endmodule 56 | -------------------------------------------------------------------------------- /sbox.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 12:09:17 06/06/2017 7 | // Design Name: 8 | // Module Name: sbox 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module sbox(x,y,sbout); 22 | input [0:3] x,y; 23 | output reg [0:7] sbout; 24 | reg [0:7] c; 25 | always @(x,y) 26 | begin 27 | c = {x,y}; 28 | case(c) 29 | 8'h00:sbout =8'h63; 30 | 8'h01:sbout =8'h7c; 31 | 8'h02:sbout =8'h77; 32 | 8'h03:sbout =8'h7b; 33 | 8'h04:sbout =8'hf2; 34 | 8'h05:sbout =8'h6b; 35 | 8'h06:sbout =8'h6f; 36 | 8'h07:sbout =8'hc5; 37 | 8'h08:sbout =8'h30; 38 | 8'h09:sbout =8'h01; 39 | 8'h0a:sbout =8'h67; 40 | 8'h0b:sbout =8'h2b; 41 | 8'h0c:sbout =8'hfe; 42 | 8'h0d:sbout =8'hd7; 43 | 8'h0e:sbout =8'hab; 44 | 8'h0f:sbout =8'h76; 45 | 8'h10:sbout =8'hca; 46 | 8'h11:sbout =8'h82; 47 | 8'h12:sbout =8'hc9; 48 | 8'h13:sbout =8'h7d; 49 | 8'h14:sbout =8'hfa; 50 | 8'h15:sbout =8'h59; 51 | 8'h16:sbout =8'h47; 52 | 8'h17:sbout =8'hf0; 53 | 8'h18:sbout =8'had; 54 | 8'h19:sbout =8'hd4; 55 | 8'h1a:sbout =8'ha2; 56 | 8'h1b:sbout =8'haf; 57 | 8'h1c:sbout =8'h9c; 58 | 8'h1d:sbout =8'ha4; 59 | 8'h1e:sbout =8'h72; 60 | 8'h1f:sbout =8'hc0; 61 | 8'h20:sbout =8'hb7; 62 | 8'h21:sbout =8'hfd; 63 | 8'h22:sbout =8'h93; 64 | 8'h23:sbout =8'h26; 65 | 8'h24:sbout =8'h36; 66 | 8'h25:sbout =8'h3f; 67 | 8'h26:sbout =8'hf7; 68 | 8'h27:sbout =8'hcc; 69 | 8'h28:sbout =8'h34; 70 | 8'h29:sbout =8'ha5; 71 | 8'h2a:sbout =8'he5; 72 | 8'h2b:sbout =8'hf1; 73 | 8'h2c:sbout =8'h71; 74 | 8'h2d:sbout =8'hd8; 75 | 8'h2e:sbout =8'h31; 76 | 8'h2f:sbout =8'h15; 77 | 8'h30:sbout =8'h04; 78 | 8'h31:sbout =8'hc7; 79 | 8'h32:sbout =8'h23; 80 | 8'h33:sbout =8'hc3; 81 | 8'h34:sbout =8'h18; 82 | 8'h35:sbout =8'h96; 83 | 8'h36:sbout =8'h05; 84 | 8'h37:sbout =8'h9a; 85 | 8'h38:sbout =8'h07; 86 | 8'h39:sbout =8'h12; 87 | 8'h3a:sbout =8'h80; 88 | 8'h3b:sbout =8'he2; 89 | 8'h3c:sbout =8'heb; 90 | 8'h3d:sbout =8'h27; 91 | 8'h3e:sbout =8'hb2; 92 | 8'h3f:sbout =8'h75; 93 | 8'h40:sbout =8'h09; 94 | 8'h41:sbout =8'h83; 95 | 8'h42:sbout =8'h2c; 96 | 8'h43:sbout =8'h1a; 97 | 8'h44:sbout =8'h1b; 98 | 8'h45:sbout =8'h6e; 99 | 8'h46:sbout =8'h5a; 100 | 8'h47:sbout =8'ha0; 101 | 8'h48:sbout =8'h52; 102 | 8'h49:sbout =8'h3b; 103 | 8'h4a:sbout =8'hd6; 104 | 8'h4b:sbout =8'hb3; 105 | 8'h4c:sbout =8'h29; 106 | 8'h4d:sbout =8'he3; 107 | 8'h4e:sbout =8'h2f; 108 | 8'h4f:sbout =8'h84; 109 | 8'h50:sbout =8'h53; 110 | 8'h51:sbout =8'hd1; 111 | 8'h52:sbout =8'h00; 112 | 8'h53:sbout =8'hed; 113 | 8'h54:sbout =8'h20; 114 | 8'h55:sbout =8'hfc; 115 | 8'h56:sbout =8'hb1; 116 | 8'h57:sbout =8'h5b; 117 | 8'h58:sbout =8'h6a; 118 | 8'h59:sbout =8'hcb; 119 | 8'h5a:sbout =8'hbe; 120 | 8'h5b:sbout =8'h39; 121 | 8'h5c:sbout =8'h4a; 122 | 8'h5d:sbout =8'h4c; 123 | 8'h5e:sbout =8'h58; 124 | 8'h5f:sbout =8'hcf; 125 | 8'h60:sbout =8'hd0; 126 | 8'h61:sbout =8'hef; 127 | 8'h62:sbout =8'haa; 128 | 8'h63:sbout =8'hfb; 129 | 8'h64:sbout =8'h43; 130 | 8'h65:sbout =8'h4d; 131 | 8'h66:sbout =8'h33; 132 | 8'h67:sbout =8'h85; 133 | 8'h68:sbout =8'h45; 134 | 8'h69:sbout =8'hf9; 135 | 8'h6a:sbout =8'h02; 136 | 8'h6b:sbout =8'h7f; 137 | 8'h6c:sbout =8'h50; 138 | 8'h6d:sbout =8'h3c; 139 | 8'h6e:sbout =8'h9f; 140 | 8'h6f:sbout =8'ha8; 141 | 8'h70:sbout =8'h51; 142 | 8'h71:sbout =8'ha3; 143 | 8'h72:sbout =8'h40; 144 | 8'h73:sbout =8'h8f; 145 | 8'h74:sbout =8'h92; 146 | 8'h75:sbout =8'h9d; 147 | 8'h76:sbout =8'h38; 148 | 8'h77:sbout =8'hf5; 149 | 8'h78:sbout =8'hbc; 150 | 8'h79:sbout =8'hb6; 151 | 8'h7a:sbout =8'hda; 152 | 8'h7b:sbout =8'h21; 153 | 8'h7c:sbout =8'h10; 154 | 8'h7d:sbout =8'hff; 155 | 8'h7e:sbout =8'hf3; 156 | 8'h7f:sbout =8'hd2; 157 | 8'h80:sbout =8'hcd; 158 | 8'h81:sbout =8'h0c; 159 | 8'h82:sbout =8'h13; 160 | 8'h83:sbout =8'hec; 161 | 8'h84:sbout =8'h5f; 162 | 8'h85:sbout =8'h97; 163 | 8'h86:sbout =8'h44; 164 | 8'h87:sbout =8'h17; 165 | 8'h88:sbout =8'hc4; 166 | 8'h89:sbout =8'ha7; 167 | 8'h8a:sbout =8'h7e; 168 | 8'h8b:sbout =8'h3d; 169 | 8'h8c:sbout =8'h64; 170 | 8'h8d:sbout =8'h5d; 171 | 8'h8e:sbout =8'h19; 172 | 8'h8f:sbout =8'h73; 173 | 8'h90:sbout =8'h60; 174 | 8'h91:sbout =8'h81; 175 | 8'h92:sbout =8'h4f; 176 | 8'h93:sbout =8'hdc; 177 | 8'h94:sbout =8'h22; 178 | 8'h95:sbout =8'h2a; 179 | 8'h96:sbout =8'h90; 180 | 8'h97:sbout =8'h88; 181 | 8'h98:sbout =8'h46; 182 | 8'h99:sbout =8'hee; 183 | 8'h9a:sbout =8'hb8; 184 | 8'h9b:sbout =8'h14; 185 | 8'h9c:sbout =8'hde; 186 | 8'h9d:sbout =8'h5e; 187 | 8'h9e:sbout =8'h0b; 188 | 8'h9f:sbout =8'hdb; 189 | 8'ha0:sbout =8'he0; 190 | 8'ha1:sbout =8'h32; 191 | 8'ha2:sbout =8'h3a; 192 | 8'ha3:sbout =8'h0a; 193 | 8'ha4:sbout =8'h49; 194 | 8'ha5:sbout =8'h06; 195 | 8'ha6:sbout =8'h24; 196 | 8'ha7:sbout =8'h5c; 197 | 8'ha8:sbout =8'hc2; 198 | 8'ha9:sbout =8'hd3; 199 | 8'haa:sbout =8'hac; 200 | 8'hab:sbout =8'h62; 201 | 8'hac:sbout =8'h91; 202 | 8'had:sbout =8'h95; 203 | 8'hae:sbout =8'he4; 204 | 8'haf:sbout =8'h79; 205 | 8'hb0:sbout =8'he7; 206 | 8'hb1:sbout =8'hc8; 207 | 8'hb2:sbout =8'h37; 208 | 8'hb3:sbout =8'h6d; 209 | 8'hb4:sbout =8'h8d; 210 | 8'hb5:sbout =8'hd5; 211 | 8'hb6:sbout =8'h4e; 212 | 8'hb7:sbout =8'ha9; 213 | 8'hb8:sbout =8'h6c; 214 | 8'hb9:sbout =8'h56; 215 | 8'hba:sbout =8'hf4; 216 | 8'hbb:sbout =8'hea; 217 | 8'hbc:sbout =8'h65; 218 | 8'hbd:sbout =8'h7a; 219 | 8'hbe:sbout =8'hae; 220 | 8'hbf:sbout =8'h08; 221 | 8'hc0:sbout =8'hba; 222 | 8'hc1:sbout =8'h78; 223 | 8'hc2:sbout =8'h25; 224 | 8'hc3:sbout =8'h2e; 225 | 8'hc4:sbout =8'h1c; 226 | 8'hc5:sbout =8'ha6; 227 | 8'hc6:sbout =8'hb4; 228 | 8'hc7:sbout =8'hc6; 229 | 8'hc8:sbout =8'he8; 230 | 8'hc9:sbout =8'hdd; 231 | 8'hca:sbout =8'h74; 232 | 8'hcb:sbout =8'h1f; 233 | 8'hcc:sbout =8'h4b; 234 | 8'hcd:sbout =8'hbd; 235 | 8'hce:sbout =8'h8b; 236 | 8'hcf:sbout =8'h8a; 237 | 8'hd0:sbout =8'h70; 238 | 8'hd1:sbout =8'h3e; 239 | 8'hd2:sbout =8'hb5; 240 | 8'hd3:sbout =8'h66; 241 | 8'hd4:sbout =8'h48; 242 | 8'hd5:sbout =8'h03; 243 | 8'hd6:sbout =8'hf6; 244 | 8'hd7:sbout =8'h0e; 245 | 8'hd8:sbout =8'h61; 246 | 8'hd9:sbout =8'h35; 247 | 8'hda:sbout =8'h57; 248 | 8'hdb:sbout =8'hb9; 249 | 8'hdc:sbout =8'h86; 250 | 8'hdd:sbout =8'hc1; 251 | 8'hde:sbout =8'h1d; 252 | 8'hdf:sbout =8'h9e; 253 | 8'he0:sbout =8'he1; 254 | 8'he1:sbout =8'hf8; 255 | 8'he2:sbout =8'h98; 256 | 8'he3:sbout =8'h11; 257 | 8'he4:sbout =8'h69; 258 | 8'he5:sbout =8'hd9; 259 | 8'he6:sbout =8'h8e; 260 | 8'he7:sbout =8'h94; 261 | 8'he8:sbout =8'h9b; 262 | 8'he9:sbout =8'h1e; 263 | 8'hea:sbout =8'h87; 264 | 8'heb:sbout =8'he9; 265 | 8'hec:sbout =8'hce; 266 | 8'hed:sbout =8'h55; 267 | 8'hee:sbout =8'h28; 268 | 8'hef:sbout =8'hdf; 269 | 8'hf0:sbout =8'h8c; 270 | 8'hf1:sbout =8'ha1; 271 | 8'hf2:sbout =8'h89; 272 | 8'hf3:sbout =8'h0d; 273 | 8'hf4:sbout =8'hbf; 274 | 8'hf5:sbout =8'he6; 275 | 8'hf6:sbout =8'h42; 276 | 8'hf7:sbout =8'h68; 277 | 8'hf8:sbout =8'h41; 278 | 8'hf9:sbout =8'h99; 279 | 8'hfa:sbout =8'h2d; 280 | 8'hfb:sbout =8'h0f; 281 | 8'hfc:sbout =8'hb0; 282 | 8'hfd:sbout =8'h54; 283 | 8'hfe:sbout =8'hbb; 284 | 8'hff:sbout =8'h16; 285 | endcase 286 | end 287 | 288 | endmodule 289 | -------------------------------------------------------------------------------- /sub_byte.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 12:39:40 06/10/2017 7 | // Design Name: 8 | // Module Name: sub_byte 9 | // Project Name: 10 | // Target Devices: 11 | // Tool versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | module sub_byte(in_key,out_key); 22 | input [0:127] in_key; 23 | output [0:127] out_key; 24 | 25 | sbox sb5(in_key[0:3],in_key[4:7],out_key[0:7]); 26 | sbox sb6(in_key[40:43],in_key[44:47],out_key[40:47]); 27 | sbox sb7(in_key[80:83],in_key[84:87],out_key[80:87]); 28 | sbox sb8(in_key[120:123],in_key[124:127],out_key[120:127]); 29 | 30 | sbox sb9(in_key[32:35],in_key[36:39],out_key[32:39]); 31 | sbox sb10(in_key[72:75],in_key[76:79],out_key[72:79]); 32 | sbox sb11(in_key[112:115],in_key[116:119],out_key[112:119]); 33 | sbox sb12(in_key[24:27],in_key[28:31],out_key[24:31]); 34 | 35 | sbox sb13(in_key[64:67],in_key[68:71],out_key[64:71]); 36 | sbox sb14(in_key[104:107],in_key[108:111],out_key[104:111]); 37 | sbox sb15(in_key[16:19],in_key[20:23],out_key[16:23]); 38 | sbox sb16(in_key[56:59],in_key[60:63],out_key[56:63]); 39 | 40 | sbox sb17(in_key[96:99],in_key[100:103],out_key[96:103]); 41 | sbox sb18(in_key[8:11],in_key[12:15],out_key[8:15]); 42 | sbox sb19(in_key[48:51],in_key[52:55],out_key[48:55]); 43 | sbox sb20(in_key[88:91],in_key[92:95],out_key[88:95]); 44 | 45 | 46 | 47 | 48 | endmodule 49 | -------------------------------------------------------------------------------- /testkeygen.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | 3 | module key_gen(temp_key,mx_key,rcon,ko,r_key); 4 | 5 | input [0:127] temp_key,mx_key; 6 | input [0:31] rcon; 7 | output [0:127] ko,r_key; 8 | wire [0:127] ko; 9 | 10 | //shifting operation 11 | 12 | wire [0:127] key,key2; 13 | assign key[0:95] = temp_key[0:95]; 14 | assign key[96:103] = temp_key[104:111]; 15 | assign key[104:111] = temp_key[112:119]; 16 | assign key[112:119] = temp_key[120:127]; 17 | assign key[120:127] = temp_key[96:103]; 18 | 19 | //sub_bytes operation 20 | 21 | sbox s1(key[96:99],key[100:103],key2[96:103]); 22 | sbox s2(key[104:107],key[108:111],key2[104:111]); 23 | sbox s3(key[112:115],key[116:119],key2[112:119]); 24 | sbox s4(key[120:123],key[124:127],key2[120:127]); 25 | 26 | //multiplication with rcon values 27 | //xor with different columns 28 | //ko is also numbered column wise 29 | 30 | assign ko[0:31] = key2[96:127]^key[0:31]^rcon[0:31]; 31 | assign ko[32:63] = key2[96:127]^key[0:31]^key[32:63]^rcon[0:31]; 32 | assign ko[64:95] = key2[96:127]^key[0:31]^key[32:63]^key[64:95]^rcon[0:31]; 33 | assign ko[96:127] = key2[96:127]^key[0:31]^key[32:63]^key[64:95]^temp_key[96:127]^rcon[0:31]; 34 | 35 | //add round operation 36 | 37 | assign r_key[0:127] = mx_key[0:127]^ko[0:127]; 38 | 39 | 40 | endmodule 41 | --------------------------------------------------------------------------------