import byucc.jhdl.base.*; import byucc.jhdl.Xilinx.*; import byucc.jhdl.Xilinx.XC4000.*; public class DESRoundC extends DESLogic{ // arrays for key compression permutation, data expansion permutation, // and pbox permutation static int[] c_permute = {14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31, 37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32}; static int[] e_permute = {32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20, 21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1}; static int[] p_permute = {16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30, 6,22,11,4,25}; // Cell interface public static CellInterface[] cell_interface = { in("encode",1), in("data_in", 64), in("key_in", 56), out("data_out", 64), out("key_out", 56) }; // circuit constructor public DESRoundC(Node parent, //parent of this circuit int round_num, //Number of the current round Wire encode, //Encode/decode control (1=encode) Wire data_in, //input to be ciphered Wire key_in, //input key Wire data_out, //output data Wire key_out){ //output key // call superclass constructor super(parent); //connect Wires to cell ports connect("encode",encode); connect("data_in",data_in); connect("key_in",key_in); connect("data_out",data_out); connect("key_out",key_out); // integers for amounts to shift left/right the key int shift_amtl; int shift_amtr; //assign values to shift amounts if (round_num == 1 || round_num == 2 || round_num == 9 || round_num == 16){ shift_amtl = 1; shift_amtr = 1; } else{ shift_amtl = 2; shift_amtr = 2; } if (round_num == 1) shift_amtr = 0; //split the data into right and left halves Wire datal = data_in.range(63,32); Wire datar = data_in.range(31,0); //perform left key schedule shift by calls to rotl in DESLogic Wire keyl_shl = rotl(shift_amtl,key_in.range(55,28),"keyl_shl"); Wire keyr_shl = rotl(shift_amtl,key_in.range(27,0),"keyr_shl"); //perform right key schedule shift by calls to rotr in DESLogic Wire keyl_shr = rotr(shift_amtr,key_in.range(55,28),"keyl_shr"); Wire keyr_shr = rotr(shift_amtr,key_in.range(27,0),"keyr_shr"); //select left or right shifted key using encode signal (right shifted for //decode) Wire key_sh = mux(concat(keyl_shr,keyr_shr,"key_shr"), concat(keyl_shl,keyr_shl,"key_shl"), encode,"key_sh"); //place new key on the key output buf_o(key_sh,key_out); //perform compression permutation on key (56 bits to 48 bits) Wire key_cp = permute(c_permute,key_sh,"key_cp"); //perform expansion permutation on lower half of data (32 bits to 48 bits) Wire datar_ep = permute(e_permute,datar,"datar_ep"); //xor key and low data together Wire keyxordatar = xor(key_cp,datar_ep,"keyxordatar"); //create a wire for the output of the sbox permutation Wire datar_sp = wire(32,"datar_sp"); //instantiate sbox permutation hardware sBoxes(keyxordatar,datar_sp); //perform pbox permutation on output of sboxes Wire datar_pp = permute(p_permute,datar_sp,"datar_pp"); //xor high order half of input data and modified right half together Wire datarxordatal = xor(datar_pp,datal,"drxordl"); //place new data word on the output buf_o(concat(datar,datarxordatal),data_out); } }