2020-11-11 17:19:03 +01:00
|
|
|
module rc4
|
|
|
|
|
2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-07-26 16:48:49 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's
|
|
|
|
// Applied Cryptography.
|
|
|
|
//
|
|
|
|
// RC4 is cryptographically broken and should not be used for secure
|
|
|
|
// applications.
|
2019-08-02 06:37:19 +02:00
|
|
|
// Based off: https://github.com/golang/go/blob/master/src/crypto/rc4
|
|
|
|
// Last commit: https://github.com/golang/go/commit/b35dacaac57b039205d9b07ea24098e2c3fcb12e
|
2019-07-26 16:48:49 +02:00
|
|
|
import crypto.internal.subtle
|
|
|
|
|
|
|
|
// A Cipher is an instance of RC4 using a particular key.
|
|
|
|
struct Cipher {
|
|
|
|
mut:
|
|
|
|
s []u32
|
2019-09-02 14:02:25 +02:00
|
|
|
i byte
|
|
|
|
j byte
|
2019-07-26 16:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// new_cipher creates and returns a new Cipher. The key argument should be the
|
|
|
|
// RC4 key, at least 1 byte and at most 256 bytes.
|
|
|
|
pub fn new_cipher(key []byte) ?Cipher {
|
|
|
|
if key.len < 1 || key.len > 256 {
|
|
|
|
return error('crypto.rc4: invalid key size ' + key.len.str())
|
|
|
|
}
|
|
|
|
mut c := Cipher{
|
2020-10-21 11:23:03 +02:00
|
|
|
s: []u32{len: (256)}
|
2019-07-26 16:48:49 +02:00
|
|
|
}
|
2020-10-21 11:23:03 +02:00
|
|
|
for i in 0 .. 256 {
|
2019-07-26 16:48:49 +02:00
|
|
|
c.s[i] = u32(i)
|
|
|
|
}
|
2022-04-15 13:58:56 +02:00
|
|
|
mut j := u8(0)
|
2020-10-21 11:23:03 +02:00
|
|
|
for i in 0 .. 256 {
|
2022-04-15 13:58:56 +02:00
|
|
|
j += u8(c.s[i]) + key[i % key.len]
|
2019-07-26 16:48:49 +02:00
|
|
|
tmp := c.s[i]
|
|
|
|
c.s[i] = c.s[j]
|
|
|
|
c.s[j] = tmp
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:37:07 +02:00
|
|
|
// reset zeros the key data and makes the Cipher unusable.good to com
|
2019-07-26 16:48:49 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Reset can't guarantee that the key will be entirely removed from
|
|
|
|
// the process's memory.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut c Cipher) reset() {
|
2019-07-26 16:48:49 +02:00
|
|
|
for i in c.s {
|
2019-09-26 13:57:31 +02:00
|
|
|
c.s[i] = 0
|
2019-07-26 16:48:49 +02:00
|
|
|
}
|
2019-09-26 13:57:31 +02:00
|
|
|
c.i = 0
|
|
|
|
c.j = 0
|
2019-07-26 16:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// xor_key_stream sets dst to the result of XORing src with the key stream.
|
|
|
|
// Dst and src must overlap entirely or not at all.
|
2020-10-21 11:23:03 +02:00
|
|
|
pub fn (mut c Cipher) xor_key_stream(mut dst []byte, mut src []byte) {
|
2019-07-26 16:48:49 +02:00
|
|
|
if src.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-04-23 11:19:04 +02:00
|
|
|
if subtle.inexact_overlap(dst, src) {
|
2019-07-26 16:48:49 +02:00
|
|
|
panic('crypto.rc4: invalid buffer overlap')
|
|
|
|
}
|
|
|
|
mut i := c.i
|
|
|
|
mut j := c.j
|
|
|
|
for k, v in src {
|
2022-04-15 13:58:56 +02:00
|
|
|
i += u8(1)
|
2019-07-26 16:48:49 +02:00
|
|
|
x := c.s[i]
|
2022-04-15 13:58:56 +02:00
|
|
|
j += u8(x)
|
2019-07-26 16:48:49 +02:00
|
|
|
y := c.s[j]
|
|
|
|
c.s[i] = y
|
|
|
|
c.s[j] = x
|
2022-04-15 13:58:56 +02:00
|
|
|
dst[k] = v ^ u8(c.s[u8(x + y)])
|
2019-07-26 16:48:49 +02:00
|
|
|
}
|
|
|
|
c.i = i
|
|
|
|
c.j = j
|
|
|
|
}
|