const_ciphers/
rc4.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use const_for::const_for;

pub struct Rc4Const {}

impl Rc4Const {
    pub const fn encrypt<const N: usize, const K: usize>(
        plaintext: &[u8; N],
        key: &[u8; K],
    ) -> [u8; N] {
        if K == 0 {
            panic!("Key cannot be empty");
        }

        let mut s = [0u8; 256];
        const_for!(i in 0..256 => {
            s[i] = i as u8;
        });

        let mut j = 0usize;
        const_for!(i in 0..256 => {
            j = (j + s[i] as usize + key[i % K] as usize) % 256;

            // Manual swap
            let tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;
        });

        let mut i = 0usize;
        j = 0usize;
        let mut ciphertext = [0u8; N];

        const_for!(n in 0..N => {
            i = (i + 1) % 256;
            j = (j + s[i] as usize) % 256;

            // Manual swap
            let tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;

            let k = s[(s[i] as usize + s[j] as usize) % 256];
            ciphertext[n] = plaintext[n] ^ k;
        });

        ciphertext
    }

    pub const fn decrypt<const N: usize, const K: usize>(
        ciphertext: &[u8; N],
        key: &[u8; K],
    ) -> [u8; N] {
        Self::encrypt(ciphertext, key)
    }
}

#[cfg(test)]
mod tests {
    use crate::rc4::Rc4Const;

    #[test]
    fn test_block_mode() {
        let plaintext = [0u8; 16];
        let key = [0xFF; 16];

        let encrypted = Rc4Const::encrypt(&plaintext, &key);
        let decrypted = Rc4Const::decrypt(&encrypted, &key);

        assert_eq!(
            decrypted, plaintext,
            "Decrypted text does not match original plaintext"
        );
    }
}