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

Subversion Repositories rc4-prbs

[/] [rc4-prbs/] [trunk/] [rc4.v] - Blame information for rev 18

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 ortegaalfr
/*
2
        RC4 PRGA module implementation
3
        Copyright 2012 - Alfredo Ortega
4
        aortega@alu.itba.edu.ar
5 14 ortegaalfr
        aortega@groundworkstech.com
6 3 ortegaalfr
 
7
 This library is free software: you can redistribute it and/or
8
 modify it under the terms of the GNU Lesser General Public
9
 License as published by the Free Software Foundation, either
10
 version 3 of the License, or (at your option) any later version.
11
 
12
 This library is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 Lesser General Public License for more details.
16
 
17
 You should have received a copy of the GNU Lesser General Public
18
 License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
 
21
 
22 17 ortegaalfr
`include "rc4.inc"
23 3 ortegaalfr
 
24
module rc4(clk,rst,output_ready,password_input,K);
25
 
26
input clk; // Clock
27
input rst; // Reset
28
input [7:0] password_input; // Password input
29
output output_ready; // Output valid
30
output [7:0] K; // Output port
31
 
32
 
33
wire clk, rst; // clock, reset
34
reg output_ready;
35
wire [7:0] password_input;
36
 
37
 
38
/* RC4 PRGA */
39
 
40
// Key
41
reg [7:0] key[0:`KEY_SIZE-1];
42
// S array
43
reg [7:0] S[0:256];
44 18 ortegaalfr
reg [10:0] discardCount;
45 3 ortegaalfr
 
46
// Key-scheduling state
47
`define KSS_KEYREAD 4'h0
48
`define KSS_KEYSCHED1 4'h1
49
`define KSS_KEYSCHED2 4'h2
50
`define KSS_KEYSCHED3 4'h3
51 7 ortegaalfr
`define KSS_CRYPTO       4'h4
52 3 ortegaalfr
// Variable names from http://en.wikipedia.org/wiki/RC4
53
reg [3:0] KSState;
54
reg [7:0] i; // Counter
55
reg [7:0] j;
56
reg [7:0] K;
57
 
58
always @ (posedge clk or posedge rst)
59
        begin
60
        if (rst)
61
                begin
62
                i <= 8'h0;
63
                KSState <= `KSS_KEYREAD;
64
                output_ready <= 0;
65
                j <= 0;
66
                end
67 7 ortegaalfr
        else
68 3 ortegaalfr
        case (KSState)
69
                `KSS_KEYREAD:   begin // KSS_KEYREAD state: Read key from input
70
                                if (i == `KEY_SIZE)
71
                                        begin
72
                                        KSState <= `KSS_KEYSCHED1;
73
                                        i<=8'h00;
74
                                        end
75
                                else    begin
76
                                        i <= i+1;
77
                                        key[i] <= password_input;
78 14 ortegaalfr
                                        $display ("rc4: key[%d] = %08X",i,password_input);
79 3 ortegaalfr
                                        end
80
                                end
81 7 ortegaalfr
/*
82
for i from 0 to 255
83
    S[i] := i
84
endfor
85
*/
86 3 ortegaalfr
                `KSS_KEYSCHED1: begin // KSS_KEYSCHED1: Increment counter for S initialization
87
                                S[i] <= i;
88
                                if (i == 8'hFF)
89
                                        begin
90
                                        KSState <= `KSS_KEYSCHED2;
91
                                        i <= 8'h00;
92
                                        end
93
                                else    i <= i +1;
94
                                end
95 7 ortegaalfr
/*
96
j := 0
97
for i from 0 to 255
98
    j := (j + S[i] + key[i mod keylength]) mod 256
99
    swap values of S[i] and S[j]
100
endfor
101
*/
102 3 ortegaalfr
                `KSS_KEYSCHED2: begin // KSS_KEYSCHED2: Initialize S array
103
                                j <= (j + S[i] + key[i % `KEY_SIZE]);
104
                                KSState <= `KSS_KEYSCHED3;
105
                                end
106 5 ortegaalfr
                `KSS_KEYSCHED3: begin // KSS_KEYSCHED3: S array permutation
107 3 ortegaalfr
                                S[i]<=S[j];
108
                                S[j]<=S[i];
109
                                if (i == 8'hFF)
110
                                        begin
111
                                        KSState <= `KSS_CRYPTO;
112 7 ortegaalfr
                                        i <= 8'h01;
113 14 ortegaalfr
                                        j <= S[1];
114 18 ortegaalfr
                                        discardCount <= 11'h0;
115 14 ortegaalfr
                                        output_ready <= 0; // K not valid yet
116 3 ortegaalfr
                                        end
117
                                else    begin
118
                                        i <= i + 1;
119
                                        KSState <= `KSS_KEYSCHED2;
120
                                        end
121
                                end
122 7 ortegaalfr
/*
123
i := 0
124
j := 0
125
while GeneratingOutput:
126
    i := (i + 1) mod 256
127
    j := (j + S[i]) mod 256
128
    swap values of S[i] and S[j]
129
    K := S[(S[i] + S[j]) mod 256]
130
    output K
131
endwhile
132
*/
133 14 ortegaalfr
                `KSS_CRYPTO: begin
134 7 ortegaalfr
                                S[i] <= S[j];
135
                                S[j] <= S[i]; // We can do this because of verilog.
136
                                K <= S[ S[i]+S[j] ];
137 18 ortegaalfr
                                if (discardCount<11'h600) // discard first 1536 values / RFC 4345
138 10 ortegaalfr
                                        discardCount<=discardCount+1;
139
                                else    output_ready <= 1; // Valid K at output
140 7 ortegaalfr
                                i <= i+1;
141 14 ortegaalfr
                                // Here is the secret of 1-clock: we develop all possible values of j in the future
142
                                if (j==i+1)
143
                                     j <= (j + S[i]);
144
                                else
145
                                        if (i==255) j <= (j + S[0]);
146
                                                else j <= (j + S[i+1]);
147 18 ortegaalfr
                                //$display ("rc4: output = %08X",K);
148 7 ortegaalfr
                                end
149 3 ortegaalfr
                default:        begin
150
                                end
151
        endcase
152
        end
153
 
154
endmodule

powered by: WebSVN 2.1.0

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