| |
| /pliant/util/crypto/rc4.pli |
| |
| 1 |
abstract | |
| 2 |
[This is RC4 Pliant implementation.] ; eol | |
| 3 |
[See ] ; link "CipherSaber Home Page" "http://ciphersaber.gurus.com/" ; [ for more details about the algorithm.] ; eol | |
| 4 |
| |
| 5 |
constant rc4_bits 8 | |
| 6 |
| |
| 7 |
| |
| 8 |
type RC4Ctx | |
| 9 |
field Int x y | |
| 10 |
field (Array Int 2^rc4_bits) perm | |
| 11 |
| |
| 12 |
function rc4_init ctx key laps | |
| 13 |
arg_w RC4Ctx ctx ; arg Str key ; arg Int laps | |
| 14 |
implicit ctx | |
| 15 |
for (var Int i) 0 2^rc4_bits-1 | |
| 16 |
perm i := i | |
| 17 |
var Int j := 0 | |
| 18 |
for (var Int l) 1 laps | |
| 19 |
for (var Int i) 0 2^rc4_bits-1 | |
| 20 |
j := j+perm:i+(key i%key:len):number .and. 2^rc4_bits-1 | |
| 21 |
swap perm:i perm:j | |
| 22 |
x := 0 ; y := 0 | |
| 23 |
| |
| 24 |
function rc4_byte ctx -> result | |
| 25 |
arg_rw RC4Ctx ctx ; arg Int result | |
| 26 |
implicit ctx | |
| 27 |
x := x+1 .and. 2^rc4_bits-1 | |
| 28 |
y := y+perm:x .and. 2^rc4_bits-1 | |
| 29 |
var Int i := perm:x+perm:y .and. 255 | |
| 30 |
swap perm:x perm:y | |
| 31 |
result := perm i | |
| 32 |
| |
| 33 |
| |
| 34 |
doc | |
| 35 |
[And a simple added routine for ciphering a bloc of memory.] | |
| 36 |
| |
| 37 |
module "/pliant/language/unsafe.pli" | |
| 38 |
| |
| 39 |
function rc4_cipher ctx src dest size | |
| 40 |
arg_rw RC4Ctx ctx ; arg Address src dest ; arg Int size | |
| 41 |
for (var Int i) 0 size-1 | |
| 42 |
(dest translate Byte i) map uInt8 := ((src translate Byte i) map uInt8) .xor. (rc4_byte:ctx .and. 255) | |
| 43 |
| |
| 44 |
export RC4Ctx '. perm' rc4_bits | |
| 45 |
export rc4_init rc4_byte rc4_cipher | |
| |