URL
https://opencores.org/ocsvn/rc4-prbs/rc4-prbs/trunk
Subversion Repositories rc4-prbs
Compare Revisions
- This comparison shows the changes necessary to convert path
/rc4-prbs/trunk
- from Rev 2 to Rev 3
- ↔ Reverse comparison
Rev 2 → Rev 3
/rc4.inc
0,0 → 1,21
/* |
RC4 PRGA constants |
Copyright 2012 - Alfredo Ortega |
aortega@alu.itba.edu.ar |
|
This library is free software: you can redistribute it and/or |
modify it under the terms of the GNU Lesser General Public |
License as published by the Free Software Foundation, either |
version 3 of the License, or (at your option) any later version. |
|
This library is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
Lesser General Public License for more details. |
|
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/>. |
*/ |
|
|
`define KEY_SIZE 8 |
/rc4_tb.v
1,5 → 1,22
/* RC4 PRGA Testbench */ |
/* |
RC4 PRGA Testbench |
Copyright 2012 - Alfredo Ortega |
aortega@alu.itba.edu.ar |
|
This library is free software: you can redistribute it and/or |
modify it under the terms of the GNU Lesser General Public |
License as published by the Free Software Foundation, either |
version 3 of the License, or (at your option) any later version. |
|
This library is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
Lesser General Public License for more details. |
|
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/>. |
*/ |
|
`define RC4 |
|
|
7,116 → 24,61
`define TEST_CYCLES 2000 |
`endif |
|
`define KEY_SIZE 8 |
`include "rc4.inc" |
|
module rc4; |
endmodule |
|
module rc4_tb; |
|
reg [7:0] password[0:`KEY_SIZE-1]; |
|
parameter tck = 10, program_cycles = `TEST_CYCLES; |
|
|
reg clk, rst; // clock, reset |
wire output_ready; // output ready (valid) |
|
wire [7:0] K; // output |
reg [7:0] password_input; //input |
//wire [7:0] Kreg; // output |
|
|
//assign Kreg=K; |
/* Clocking device */ |
always #(tck/2) |
clk = ~clk; |
|
integer clkcount=0; |
integer clkcount; |
always @ (posedge clk) |
begin |
clkcount=clkcount+1; |
$display ("--- clk %d ---",clkcount); |
end |
|
/* RC4 PRGA */ |
|
// S array |
reg [7:0] S[0:256]; |
// Key |
reg [7:0] key[0:`KEY_SIZE-1]; |
|
// Key-scheduling state |
`define KSS_KEYSCHED1 4'h1 |
`define KSS_KEYSCHED2 4'h2 |
`define KSS_KEYSCHED3 4'h3 |
`define KSS_CRYPTO 4'h4 |
|
// Variable names from http://en.wikipedia.org/wiki/RC4 |
reg [3:0] KSState; |
reg [7:0] i; // Counter |
reg [7:0] j; |
reg [7:0] temp; |
reg [7:0] K; |
reg KS_Finished; |
always @ (posedge clk or posedge rst) |
begin |
if (rst) |
clkcount<=clkcount+1; |
if (clkcount < `KEY_SIZE) |
begin |
i <= 8'h0; |
KSState <= `KSS_KEYSCHED1; |
KS_Finished <= 0; |
j <= 0; |
password_input<=password[clkcount]; |
$display ("--- clk %d --- key[%x] = %08X",clkcount,clkcount,password[clkcount]); |
end |
case (KSState) |
`KSS_KEYSCHED1: begin |
S[i] <= i; |
if (i == 8'hFF) |
begin |
KSState <= `KSS_KEYSCHED2; |
i <= 8'h00; |
end |
else i <= i +1; |
end |
`KSS_KEYSCHED2: begin |
j <= (j + S[i] + key[i % `KEY_SIZE]); |
KSState <= `KSS_KEYSCHED3; |
end |
`KSS_KEYSCHED3: begin |
S[i]<=S[j]; |
S[j]<=S[i]; |
if (i == 8'hFF) |
begin |
KSState <= `KSS_CRYPTO; |
KS_Finished <= 1; // Flag keysched finished |
i <= 8'h00; |
end |
else begin |
i <= i + 1; |
KSState <= `KSS_KEYSCHED2; |
end |
end |
|
`KSS_CRYPTO: begin // It was all nicely pipelined until this point where I don't care anymore |
i = i + 1; |
j = (j + S[i]); |
temp = S[j]; |
S[j]=S[i]; |
S[i]=temp; |
K = S[ S[i]+S[j] ]; |
$display ("KSS_CRYPTO: K: %d",K); |
end |
default: begin |
end |
endcase |
else $display ("--- clk %d --- K %08X",clkcount,K); |
end |
|
|
/* rc4 module implementation */ |
rc4 rc4mod( |
.clk(clk), |
.rst(rst), |
.password_input(password_input), |
.output_ready(output_ready), |
.K(K) |
); |
|
|
/* Simulation */ |
integer q; |
initial begin |
for (q=0; q<`KEY_SIZE; q=q+1) key[q] = 8'h42; // initialize Key |
for (q=0; q<`KEY_SIZE; q=q+1) password[q] = 8'h42; // initialize Key |
$display ("Start..."); |
clk <= 0; |
KSState <= 8'h0; // Init key-schedule state |
rst <= 1; |
clk = 0; |
rst = 1; |
clkcount =0; |
password_input=password[clkcount]; |
#(1*tck) |
rst <= 0; |
rst = 0; |
#(program_cycles*tck+100) |
$display ("Finish."); |
$finish; |
/LICENSE
0,0 → 1,166
GNU LESSER GENERAL PUBLIC LICENSE |
Version 3, 29 June 2007 |
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> |
Everyone is permitted to copy and distribute verbatim copies |
of this license document, but changing it is not allowed. |
|
|
This version of the GNU Lesser General Public License incorporates |
the terms and conditions of version 3 of the GNU General Public |
License, supplemented by the additional permissions listed below. |
|
0. Additional Definitions. |
|
As used herein, "this License" refers to version 3 of the GNU Lesser |
General Public License, and the "GNU GPL" refers to version 3 of the GNU |
General Public License. |
|
"The Library" refers to a covered work governed by this License, |
other than an Application or a Combined Work as defined below. |
|
An "Application" is any work that makes use of an interface provided |
by the Library, but which is not otherwise based on the Library. |
Defining a subclass of a class defined by the Library is deemed a mode |
of using an interface provided by the Library. |
|
A "Combined Work" is a work produced by combining or linking an |
Application with the Library. The particular version of the Library |
with which the Combined Work was made is also called the "Linked |
Version". |
|
The "Minimal Corresponding Source" for a Combined Work means the |
Corresponding Source for the Combined Work, excluding any source code |
for portions of the Combined Work that, considered in isolation, are |
based on the Application, and not on the Linked Version. |
|
The "Corresponding Application Code" for a Combined Work means the |
object code and/or source code for the Application, including any data |
and utility programs needed for reproducing the Combined Work from the |
Application, but excluding the System Libraries of the Combined Work. |
|
1. Exception to Section 3 of the GNU GPL. |
|
You may convey a covered work under sections 3 and 4 of this License |
without being bound by section 3 of the GNU GPL. |
|
2. Conveying Modified Versions. |
|
If you modify a copy of the Library, and, in your modifications, a |
facility refers to a function or data to be supplied by an Application |
that uses the facility (other than as an argument passed when the |
facility is invoked), then you may convey a copy of the modified |
version: |
|
a) under this License, provided that you make a good faith effort to |
ensure that, in the event an Application does not supply the |
function or data, the facility still operates, and performs |
whatever part of its purpose remains meaningful, or |
|
b) under the GNU GPL, with none of the additional permissions of |
this License applicable to that copy. |
|
3. Object Code Incorporating Material from Library Header Files. |
|
The object code form of an Application may incorporate material from |
a header file that is part of the Library. You may convey such object |
code under terms of your choice, provided that, if the incorporated |
material is not limited to numerical parameters, data structure |
layouts and accessors, or small macros, inline functions and templates |
(ten or fewer lines in length), you do both of the following: |
|
a) Give prominent notice with each copy of the object code that the |
Library is used in it and that the Library and its use are |
covered by this License. |
|
b) Accompany the object code with a copy of the GNU GPL and this license |
document. |
|
4. Combined Works. |
|
You may convey a Combined Work under terms of your choice that, |
taken together, effectively do not restrict modification of the |
portions of the Library contained in the Combined Work and reverse |
engineering for debugging such modifications, if you also do each of |
the following: |
|
a) Give prominent notice with each copy of the Combined Work that |
the Library is used in it and that the Library and its use are |
covered by this License. |
|
b) Accompany the Combined Work with a copy of the GNU GPL and this license |
document. |
|
c) For a Combined Work that displays copyright notices during |
execution, include the copyright notice for the Library among |
these notices, as well as a reference directing the user to the |
copies of the GNU GPL and this license document. |
|
d) Do one of the following: |
|
0) Convey the Minimal Corresponding Source under the terms of this |
License, and the Corresponding Application Code in a form |
suitable for, and under terms that permit, the user to |
recombine or relink the Application with a modified version of |
the Linked Version to produce a modified Combined Work, in the |
manner specified by section 6 of the GNU GPL for conveying |
Corresponding Source. |
|
1) Use a suitable shared library mechanism for linking with the |
Library. A suitable mechanism is one that (a) uses at run time |
a copy of the Library already present on the user's computer |
system, and (b) will operate properly with a modified version |
of the Library that is interface-compatible with the Linked |
Version. |
|
e) Provide Installation Information, but only if you would otherwise |
be required to provide such information under section 6 of the |
GNU GPL, and only to the extent that such information is |
necessary to install and execute a modified version of the |
Combined Work produced by recombining or relinking the |
Application with a modified version of the Linked Version. (If |
you use option 4d0, the Installation Information must accompany |
the Minimal Corresponding Source and Corresponding Application |
Code. If you use option 4d1, you must provide the Installation |
Information in the manner specified by section 6 of the GNU GPL |
for conveying Corresponding Source.) |
|
5. Combined Libraries. |
|
You may place library facilities that are a work based on the |
Library side by side in a single library together with other library |
facilities that are not Applications and are not covered by this |
License, and convey such a combined library under terms of your |
choice, if you do both of the following: |
|
a) Accompany the combined library with a copy of the same work based |
on the Library, uncombined with any other library facilities, |
conveyed under the terms of this License. |
|
b) Give prominent notice with the combined library that part of it |
is a work based on the Library, and explaining where to find the |
accompanying uncombined form of the same work. |
|
6. Revised Versions of the GNU Lesser General Public License. |
|
The Free Software Foundation may publish revised and/or new versions |
of the GNU Lesser General Public License from time to time. Such new |
versions will be similar in spirit to the present version, but may |
differ in detail to address new problems or concerns. |
|
Each version is given a distinguishing version number. If the |
Library as you received it specifies that a certain numbered version |
of the GNU Lesser General Public License "or any later version" |
applies to it, you have the option of following the terms and |
conditions either of that published version or of any later version |
published by the Free Software Foundation. If the Library as you |
received it does not specify a version number of the GNU Lesser |
General Public License, you may choose any version of the GNU Lesser |
General Public License ever published by the Free Software Foundation. |
|
If the Library as you received it specifies that a proxy can decide |
whether future versions of the GNU Lesser General Public License shall |
apply, that proxy's public statement of acceptance of any version is |
permanent authorization for you to choose that version for the |
Library. |
|
/README.txt
0,0 → 1,19
Hi, |
|
I was looking for a quick implementation of RC4 and I couldn't find one, so I wrote one based on the wikipedia example. |
|
It's quite easy to use: |
|
1) First, issue rst |
2) Load the password byte-by-byte into the password_input port. The lenght of the password is KEY_SIZE |
3) Issue 768 clocks to perform key expansion |
4) Now you should start receiving the pseudo-random stream via the output bus, one byte per clock. To encrypt or decrypt using RC4 you simply xor your data with the output stream. |
Also you shouldn't use the first kb of stream because of a known RC4 vulnerability, so please discard those bytes. |
|
The testbench and makefile work using icarus verilog and you can peer into rc4_tb.v to see an example implementation. |
|
Any question or suggestion send an email to aortega@alu.itba.edu.ar |
|
Cheers, |
|
Alfredo |
/rc4.v
0,0 → 1,121
/* |
RC4 PRGA module implementation |
Copyright 2012 - Alfredo Ortega |
aortega@alu.itba.edu.ar |
|
This library is free software: you can redistribute it and/or |
modify it under the terms of the GNU Lesser General Public |
License as published by the Free Software Foundation, either |
version 3 of the License, or (at your option) any later version. |
|
This library is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
Lesser General Public License for more details. |
|
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/>. |
*/ |
|
|
`include "rc4.inc" |
|
module rc4(clk,rst,output_ready,password_input,K); |
|
input clk; // Clock |
input rst; // Reset |
input [7:0] password_input; // Password input |
output output_ready; // Output valid |
output [7:0] K; // Output port |
|
|
wire clk, rst; // clock, reset |
reg output_ready; |
wire [7:0] password_input; |
|
|
/* RC4 PRGA */ |
|
// Key |
reg [7:0] key[0:`KEY_SIZE-1]; |
// S array |
reg [7:0] S[0:256]; |
|
// Key-scheduling state |
`define KSS_KEYREAD 4'h0 |
`define KSS_KEYSCHED1 4'h1 |
`define KSS_KEYSCHED2 4'h2 |
`define KSS_KEYSCHED3 4'h3 |
`define KSS_CRYPTO 4'h4 |
|
// Variable names from http://en.wikipedia.org/wiki/RC4 |
reg [3:0] KSState; |
reg [7:0] i; // Counter |
reg [7:0] j; |
reg [7:0] temp; |
reg [7:0] K; |
|
always @ (posedge clk or posedge rst) |
begin |
if (rst) |
begin |
i <= 8'h0; |
KSState <= `KSS_KEYREAD; |
output_ready <= 0; |
j <= 0; |
end |
case (KSState) |
`KSS_KEYREAD: begin // KSS_KEYREAD state: Read key from input |
if (i == `KEY_SIZE) |
begin |
KSState <= `KSS_KEYSCHED1; |
i<=8'h00; |
end |
else begin |
i <= i+1; |
key[i] <= password_input; |
$display ("key[%d] = %08X",i,password_input); |
end |
end |
`KSS_KEYSCHED1: begin // KSS_KEYSCHED1: Increment counter for S initialization |
S[i] <= i; |
if (i == 8'hFF) |
begin |
KSState <= `KSS_KEYSCHED2; |
i <= 8'h00; |
end |
else i <= i +1; |
end |
`KSS_KEYSCHED2: begin // KSS_KEYSCHED2: Initialize S array |
j <= (j + S[i] + key[i % `KEY_SIZE]); |
KSState <= `KSS_KEYSCHED3; |
end |
`KSS_KEYSCHED3: begin |
S[i]<=S[j]; |
S[j]<=S[i]; |
if (i == 8'hFF) |
begin |
KSState <= `KSS_CRYPTO; |
output_ready <= 1; // Flag keysched finished |
i <= 8'h00; |
end |
else begin |
i <= i + 1; |
KSState <= `KSS_KEYSCHED2; |
end |
end |
|
`KSS_CRYPTO: begin // It was all nicely pipelined until this point where I don't care anymore |
i = i + 1; |
j = (j + S[i]); |
temp = S[j]; |
S[j]=S[i]; |
S[i]=temp; |
K = S[ S[i]+S[j] ]; |
end |
default: begin |
end |
endcase |
end |
|
endmodule |