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

Subversion Repositories rc4-prbs

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

Go to most recent revision | 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
 
6
 This library is free software: you can redistribute it and/or
7
 modify it under the terms of the GNU Lesser General Public
8
 License as published by the Free Software Foundation, either
9
 version 3 of the License, or (at your option) any later version.
10
 
11
 This library is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 Lesser General Public License for more details.
15
 
16
 You should have received a copy of the GNU Lesser General Public
17
 License along with this library.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19
 
20
 
21 11 ortegaalfr
`include "rc4.inc"
22 3 ortegaalfr
 
23
module rc4(clk,rst,output_ready,password_input,K);
24
 
25
input clk; // Clock
26
input rst; // Reset
27
input [7:0] password_input; // Password input
28
output output_ready; // Output valid
29
output [7:0] K; // Output port
30
 
31
 
32
wire clk, rst; // clock, reset
33
reg output_ready;
34
wire [7:0] password_input;
35
 
36
 
37
/* RC4 PRGA */
38
 
39
// Key
40
reg [7:0] key[0:`KEY_SIZE-1];
41
// S array
42
reg [7:0] S[0:256];
43 10 ortegaalfr
reg [9:0] discardCount;
44 3 ortegaalfr
 
45
// Key-scheduling state
46
`define KSS_KEYREAD 4'h0
47
`define KSS_KEYSCHED1 4'h1
48
`define KSS_KEYSCHED2 4'h2
49
`define KSS_KEYSCHED3 4'h3
50 7 ortegaalfr
`define KSS_CRYPTO       4'h4
51
`define KSS_CRYPTO2      4'h5
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
                                        $display ("key[%d] = %08X",i,password_input);
79
                                        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
                                        j <= 8'h00;
114 10 ortegaalfr
                                        discardCount <= 10'h0;
115 3 ortegaalfr
                                        end
116
                                else    begin
117
                                        i <= i + 1;
118
                                        KSState <= `KSS_KEYSCHED2;
119
                                        end
120
                                end
121 7 ortegaalfr
/*
122
i := 0
123
j := 0
124
while GeneratingOutput:
125
    i := (i + 1) mod 256
126
    j := (j + S[i]) mod 256
127
    swap values of S[i] and S[j]
128
    K := S[(S[i] + S[j]) mod 256]
129
    output K
130
endwhile
131
*/
132
                `KSS_CRYPTO: begin      //KSS_CRYPTO: Output crypto stream
133
                                j <= (j + S[i]);
134
                                KSState <= `KSS_CRYPTO2;
135
                                output_ready <= 0; // K not valid yet
136 3 ortegaalfr
                                end
137 7 ortegaalfr
                `KSS_CRYPTO2: begin
138
                                S[i] <= S[j];
139
                                S[j] <= S[i]; // We can do this because of verilog.
140
                                K <= S[ S[i]+S[j] ];
141 10 ortegaalfr
                                if (discardCount<1000)
142
                                        discardCount<=discardCount+1;
143
                                else    output_ready <= 1; // Valid K at output
144 7 ortegaalfr
                                i <= i+1;
145
                                KSState <= `KSS_CRYPTO;
146
                                end
147 3 ortegaalfr
                default:        begin
148
                                end
149
        endcase
150
        end
151
 
152
endmodule

powered by: WebSVN 2.1.0

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