OpenCores
URL https://opencores.org/ocsvn/rc4-prbs/rc4-prbs/trunk

Subversion Repositories rc4-prbs

[/] [rc4-prbs/] [trunk/] [rc4.v] - Diff between revs 5 and 7

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 5 Rev 7
Line 16... Line 16...
 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/alfred/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 45... Line 45...
`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] temp;
 
reg [7:0] K;
reg [7:0] K;
 
 
always @ (posedge clk or posedge rst)
always @ (posedge clk or posedge rst)
        begin
        begin
        if (rst)
        if (rst)
Line 62... Line 61...
                i <= 8'h0;
                i <= 8'h0;
                KSState <= `KSS_KEYREAD;
                KSState <= `KSS_KEYREAD;
                output_ready <= 0;
                output_ready <= 0;
                j <= 0;
                j <= 0;
                end
                end
 
        else
        case (KSState)
        case (KSState)
                `KSS_KEYREAD:   begin // KSS_KEYREAD state: Read key from input
                `KSS_KEYREAD:   begin // KSS_KEYREAD state: Read key from input
                                if (i == `KEY_SIZE)
                                if (i == `KEY_SIZE)
                                        begin
                                        begin
                                        KSState <= `KSS_KEYSCHED1;
                                        KSState <= `KSS_KEYSCHED1;
Line 75... Line 75...
                                        i <= i+1;
                                        i <= i+1;
                                        key[i] <= password_input;
                                        key[i] <= password_input;
                                        $display ("key[%d] = %08X",i,password_input);
                                        $display ("key[%d] = %08X",i,password_input);
                                        end
                                        end
                                end
                                end
 
/*
 
for i from 0 to 255
 
    S[i] := i
 
endfor
 
*/
                `KSS_KEYSCHED1: begin // KSS_KEYSCHED1: Increment counter for S initialization
                `KSS_KEYSCHED1: begin // KSS_KEYSCHED1: Increment counter for S initialization
                                S[i] <= i;
                                S[i] <= i;
                                if (i == 8'hFF)
                                if (i == 8'hFF)
                                        begin
                                        begin
                                        KSState <= `KSS_KEYSCHED2;
                                        KSState <= `KSS_KEYSCHED2;
                                        i <= 8'h00;
                                        i <= 8'h00;
                                        end
                                        end
                                else    i <= i +1;
                                else    i <= i +1;
                                end
                                end
 
/*
 
j := 0
 
for i from 0 to 255
 
    j := (j + S[i] + key[i mod keylength]) mod 256
 
    swap values of S[i] and S[j]
 
endfor
 
*/
                `KSS_KEYSCHED2: begin // KSS_KEYSCHED2: Initialize S array
                `KSS_KEYSCHED2: begin // KSS_KEYSCHED2: Initialize S array
                                j <= (j + S[i] + key[i % `KEY_SIZE]);
                                j <= (j + S[i] + key[i % `KEY_SIZE]);
                                KSState <= `KSS_KEYSCHED3;
                                KSState <= `KSS_KEYSCHED3;
                                end
                                end
                `KSS_KEYSCHED3: begin // KSS_KEYSCHED3: S array permutation
                `KSS_KEYSCHED3: begin // KSS_KEYSCHED3: S array permutation
                                S[i]<=S[j];
                                S[i]<=S[j];
                                S[j]<=S[i];
                                S[j]<=S[i];
                                if (i == 8'hFF)
                                if (i == 8'hFF)
                                        begin
                                        begin
                                        KSState <= `KSS_CRYPTO;
                                        KSState <= `KSS_CRYPTO;
                                        output_ready <= 1; // Flag keysched finished
                                        i <= 8'h01;
                                        i <= 8'h00;
                                        j <= 8'h00;
                                        end
                                        end
                                else    begin
                                else    begin
                                        i <= i + 1;
                                        i <= i + 1;
                                        KSState <= `KSS_KEYSCHED2;
                                        KSState <= `KSS_KEYSCHED2;
                                        end
                                        end
                                end
                                end
 
/*
 
i := 0
 
j := 0
 
while GeneratingOutput:
 
    i := (i + 1) mod 256
 
    j := (j + S[i]) mod 256
 
    swap values of S[i] and S[j]
 
    K := S[(S[i] + S[j]) mod 256]
 
    output K
 
endwhile
 
*/
                `KSS_CRYPTO:    begin // KSS_CRYPTO: Output crypto stream
                `KSS_CRYPTO:    begin // KSS_CRYPTO: Output crypto stream
                                // It was all nicely pipelined until this point where I don't care anymore
                                j <= (j + S[i]);
                                i = i + 1;
                                KSState <= `KSS_CRYPTO2;
                                j = (j + S[i]);
                                output_ready <= 0; // K not valid yet
                                temp = S[j];
                                end
                                S[j]=S[i];
                `KSS_CRYPTO2: begin
                                S[i]=temp;
                                S[i] <= S[j];
                                K = S[ S[i]+S[j] ];
                                S[j] <= S[i]; // We can do this because of verilog.
 
                                K <= S[ S[i]+S[j] ];
 
                                output_ready <= 1; // Valid K at output
 
                                i <= i+1;
 
                                KSState <= `KSS_CRYPTO;
                                end
                                end
                default:        begin
                default:        begin
                                end
                                end
        endcase
        endcase
        end
        end

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.