zpeterson44
Newbie level 1
- Joined
- Apr 6, 2015
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 48
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 `timescale 1ns / 1ns module RC4Test ( input wire clk, input wire rst, input wire password_ready, input wire [55:0] inkey, output reg output_ready, output reg [7:0] K ); typedef enum bit [3:0] {IDLE,KEYREAD,KEYSCHED1,KEYSCHED2,KEYSCHED3,KEYSCHED4,CRYPTO1,CRYPTO2,CRYPTOPREP} stateType; stateType state; stateType nxt_state; //`define `KEYSIZE 7 // Key //reg [7:0] key[0:6]; reg [7:0] key[0:6]; // S array reg [7:0] S[0:256]; reg [9:0] discardCount; reg [7:0] i; reg [7:0] j; reg [7:0] nxt_j; reg [7:0] tmp; always_ff @ (posedge clk, negedge rst) begin /*$display("i :%d",i); $display("State :%s",state); $display("password_input :%08b",password_input);*/ if(1'b0 == rst) begin state <= IDLE; j <= 0; end else begin state <= nxt_state; j <= nxt_j; end end always_comb begin nxt_state = state; case(state) IDLE: begin if(password_ready == 1'b1) begin nxt_state = KEYREAD; end else begin nxt_state = IDLE; end end KEYREAD: begin if(i == 8'b00000001) begin nxt_state = KEYSCHED1; end else begin nxt_state = KEYREAD; end end KEYSCHED1: begin if(i == 8'b00000000) begin nxt_state = KEYSCHED3; end else begin nxt_state = KEYSCHED1; end end KEYSCHED2: begin nxt_state = CRYPTOPREP; end KEYSCHED3: begin if(i == 8'b11111111) begin nxt_state = KEYSCHED2; end else begin if (j != nxt_j) begin nxt_state = KEYSCHED3; end else begin nxt_state = KEYSCHED4; end end end KEYSCHED4: begin if(i == 8'b11111111) begin nxt_state = KEYSCHED2; end else begin if (j != nxt_j) begin nxt_state = KEYSCHED4; end else begin nxt_state = KEYSCHED3; end end end CRYPTOPREP: begin nxt_state = CRYPTO1; end CRYPTO1: begin if (j != nxt_j) begin nxt_state = CRYPTO1; end else begin nxt_state = CRYPTO2; end end CRYPTO2: begin if (j != nxt_j) begin nxt_state = CRYPTO2; end else begin nxt_state = CRYPTO1; end end default: begin end endcase end always_comb begin output_ready = 0; K = 8'b00000000; //$display ("ALWAYS COMB 2"); case(state) IDLE: begin output_ready = 0; nxt_j = 0; i = 8'b00000000; end KEYREAD: begin key[0] = inkey[7:0]; key[1] = inkey[15:8]; key[2] = inkey[23:16]; key[3] = inkey[31:24]; key[4] = inkey[39:32]; key[5] = inkey[47:40]; key[6] = inkey[55:48]; /*while (i < 8'b00000111) begin //$display("%b",key[i]); key[i] = inkey[(i*8)+7:(i*8)]; i = i+1; end*/ i = 8'b00000001; /*while (i < 8'b00000111) begin key[i] = password_input; $display("%b",key[i]); i = i+1; end key[0] = 8'b01011010; key[1] = 8'b01100001; key[2] = 8'b01100011; key[3] = 8'b01101000; key[4] = 8'b01100001; key[5] = 8'b01110010; key[6] = 8'b01111001;*/ //key[i] = password_input; //$display ("rc4: key[%d] = %08X",i,password_input); end KEYSCHED1: begin while (i < 8'b11111111) begin S[i] = i; i = i+1; end S[i] = i; S[0] = 0; i = 8'b00000000; nxt_j = (0 + S[i] + key[i % 8'b00000111])%8'b11111111; //$display ("nxt_j: %b",nxt_j); end KEYSCHED2: begin //j = (j + S[i] + key[i % 8'b00000111])%8'b11111111; ///nxt_j = (nxt_j + S[i] + key[i % 8'b00000111])%8'b11111111; S[i] = S[j]; S[j] = S[i]; i = 8'b00000001; nxt_j = S[1]; discardCount = 10'b0000000000; output_ready = 0; end KEYSCHED3: begin if (i == 8'b11111111) begin nxt_j = (nxt_j + S[i] + key[i % 8'b00000111])%8'b11111111; S[i] = S[j]; S[j] = S[i]; end else begin //while (i < 8'b11111111) begin S[i] = S[j]; S[j] = S[i]; nxt_j = (nxt_j + S[i+1] + key[(i+1) % 8'b00000111])%8'b11111111; i = i + 1; //$display ("j: %b",j); //$display ("nxt_j: %b",nxt_j); //end end end KEYSCHED4: begin if (i == 8'b11111111) begin nxt_j = (nxt_j + S[i] + key[i % 8'b00000111])%8'b11111111; S[i] = S[j]; S[j] = S[i]; end else begin //while (i < 8'b11111111) begin S[i] = S[j]; S[j] = S[i]; nxt_j = (nxt_j + S[i+1] + key[(i+1) % 8'b00000111])%8'b11111111; i = i + 1; //$display ("j: %b",j); //$display ("nxt_j: %b",nxt_j); //end end end CRYPTOPREP: begin /*i = 8'b00000000; while (i < 8'b11111111) begin $display ("S[%d]: %b",i,S[i]); i = i+1; end $display ("S[%d]: %b",i,S[i]); i = 8'b00000001;*/ end CRYPTO1: begin S[i] = S[j]; S[j] = S[i]; tmp = S[i]+S[j]; output_ready = 0; K = S[tmp%8'b11111111];//S[ S[i]+S[j] ]; if (discardCount<10'b1100000000) begin // discard first 1536 values - SSH RC4 compliant discardCount = discardCount + 1; end else begin output_ready = 1; // Valid K at output end i = (i+1)%8'b11111111; // Here is the secret of 1-clock: we develop all possible values of j in the future if (nxt_j==i+1) begin nxt_j = (nxt_j + S[i])%8'b11111111; end else begin if (i==255) begin nxt_j = (nxt_j + S[0])%8'b11111111; end else begin nxt_j = (nxt_j + S[i+1])%8'b11111111; end end /*if(output_ready == 1) begin $display ("K[%d]: %b",i,K); end*/ end CRYPTO2: begin S[i] = S[j]; S[j] = S[i]; tmp = S[i]+S[j]; output_ready = 0; K = S[tmp%8'b11111111];//S[ S[i]+S[j] ]; if (discardCount<10'b1100000000) begin // discard first 1536 values - SSH RC4 compliant discardCount = discardCount + 1; end else begin output_ready = 1; // Valid K at output end i = (i+1)%8'b11111111; // Here is the secret of 1-clock: we develop all possible values of j in the future if (nxt_j==i+1) begin nxt_j = (nxt_j + S[i])%8'b11111111; end else begin if (i==255) begin nxt_j = (nxt_j + S[0])%8'b11111111; end else begin nxt_j = (nxt_j + S[i+1])%8'b11111111; end end /*if(output_ready == 1) begin $display ("K[%d]: %b",i,K); end*/ end default: begin end endcase end endmodule
Last edited by a moderator: