Line 1... |
Line 1... |
/*
|
/*
|
RC4 PRGA module implementation
|
RC4 PRGA module implementation
|
Copyright 2012 - Alfredo Ortega
|
Copyright 2012 - Alfredo Ortega
|
aortega@alu.itba.edu.ar
|
aortega@alu.itba.edu.ar
|
|
aortega@groundworkstech.com
|
|
|
This library is free software: you can redistribute it and/or
|
This library is free software: you can redistribute it and/or
|
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
License as published by the Free Software Foundation, either
|
License as published by the Free Software Foundation, either
|
version 3 of the License, or (at your option) any later version.
|
version 3 of the License, or (at your option) any later version.
|
Line 16... |
Line 17... |
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
*/
|
*/
|
|
|
|
|
`include "rc4.inc"
|
`include "/home/guest/docto/FPGADesign/rc4-prbs/trunk/rc4.inc"
|
|
|
module rc4(clk,rst,output_ready,password_input,K);
|
module rc4(clk,rst,output_ready,password_input,K);
|
|
|
input clk; // Clock
|
input clk; // Clock
|
input rst; // Reset
|
input rst; // Reset
|
Line 46... |
Line 47... |
`define KSS_KEYREAD 4'h0
|
`define KSS_KEYREAD 4'h0
|
`define KSS_KEYSCHED1 4'h1
|
`define KSS_KEYSCHED1 4'h1
|
`define KSS_KEYSCHED2 4'h2
|
`define KSS_KEYSCHED2 4'h2
|
`define KSS_KEYSCHED3 4'h3
|
`define KSS_KEYSCHED3 4'h3
|
`define KSS_CRYPTO 4'h4
|
`define KSS_CRYPTO 4'h4
|
`define KSS_CRYPTO2 4'h5
|
|
// Variable names from http://en.wikipedia.org/wiki/RC4
|
// Variable names from http://en.wikipedia.org/wiki/RC4
|
reg [3:0] KSState;
|
reg [3:0] KSState;
|
reg [7:0] i; // Counter
|
reg [7:0] i; // Counter
|
reg [7:0] j;
|
reg [7:0] j;
|
reg [7:0] K;
|
reg [7:0] K;
|
Line 73... |
Line 73... |
i<=8'h00;
|
i<=8'h00;
|
end
|
end
|
else begin
|
else begin
|
i <= i+1;
|
i <= i+1;
|
key[i] <= password_input;
|
key[i] <= password_input;
|
$display ("key[%d] = %08X",i,password_input);
|
$display ("rc4: key[%d] = %08X",i,password_input);
|
end
|
end
|
end
|
end
|
/*
|
/*
|
for i from 0 to 255
|
for i from 0 to 255
|
S[i] := i
|
S[i] := i
|
Line 108... |
Line 108... |
S[j]<=S[i];
|
S[j]<=S[i];
|
if (i == 8'hFF)
|
if (i == 8'hFF)
|
begin
|
begin
|
KSState <= `KSS_CRYPTO;
|
KSState <= `KSS_CRYPTO;
|
i <= 8'h01;
|
i <= 8'h01;
|
j <= 8'h00;
|
j <= S[1];
|
discardCount <= 10'h0;
|
discardCount <= 10'h0;
|
|
output_ready <= 0; // K not valid yet
|
end
|
end
|
else begin
|
else begin
|
i <= i + 1;
|
i <= i + 1;
|
KSState <= `KSS_KEYSCHED2;
|
KSState <= `KSS_KEYSCHED2;
|
end
|
end
|
Line 127... |
Line 128... |
swap values of S[i] and S[j]
|
swap values of S[i] and S[j]
|
K := S[(S[i] + S[j]) mod 256]
|
K := S[(S[i] + S[j]) mod 256]
|
output K
|
output K
|
endwhile
|
endwhile
|
*/
|
*/
|
`KSS_CRYPTO: begin //KSS_CRYPTO: Output crypto stream
|
`KSS_CRYPTO: begin
|
j <= (j + S[i]);
|
|
KSState <= `KSS_CRYPTO2;
|
|
output_ready <= 0; // K not valid yet
|
|
end
|
|
`KSS_CRYPTO2: begin
|
|
S[i] <= S[j];
|
S[i] <= S[j];
|
S[j] <= S[i]; // We can do this because of verilog.
|
S[j] <= S[i]; // We can do this because of verilog.
|
K <= S[ S[i]+S[j] ];
|
K <= S[ S[i]+S[j] ];
|
if (discardCount<1000)
|
if (discardCount<10'h3E8) // discard first 1000 values
|
discardCount<=discardCount+1;
|
discardCount<=discardCount+1;
|
else output_ready <= 1; // Valid K at output
|
else output_ready <= 1; // Valid K at output
|
i <= i+1;
|
i <= i+1;
|
KSState <= `KSS_CRYPTO;
|
// Here is the secret of 1-clock: we develop all possible values of j in the future
|
|
if (j==i+1)
|
|
j <= (j + S[i]);
|
|
else
|
|
if (i==255) j <= (j + S[0]);
|
|
else j <= (j + S[i+1]);
|
|
$display ("rc4: output = %08X",K);
|
end
|
end
|
default: begin
|
default: begin
|
end
|
end
|
endcase
|
endcase
|
end
|
end
|