From 9027378487f9b270455b1e105566522f36151cd2 Mon Sep 17 00:00:00 2001
From: Trojan295 <trojan295@gmail.com>
Date: Wed, 19 Apr 2017 19:21:53 +0200
Subject: [PATCH 1/6] Started RC2 cipher. Is able to encrypt data

---
 Cargo.toml        |   1 +
 rc2/.gitignore    |   2 +
 rc2/Cargo.toml    |   7 +++
 rc2/README.md     |  13 +++++
 rc2/src/consts.rs |  19 +++++++
 rc2/src/lib.rs    | 130 ++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 172 insertions(+)
 create mode 100644 rc2/.gitignore
 create mode 100644 rc2/Cargo.toml
 create mode 100644 rc2/README.md
 create mode 100644 rc2/src/consts.rs
 create mode 100644 rc2/src/lib.rs

diff --git a/Cargo.toml b/Cargo.toml
index 4f345ddf..01132331 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,4 +3,5 @@ members = [
     "blowfish",
     "kuznyechik",
     "magma",
+    "rc2",
 ]
diff --git a/rc2/.gitignore b/rc2/.gitignore
new file mode 100644
index 00000000..a9d37c56
--- /dev/null
+++ b/rc2/.gitignore
@@ -0,0 +1,2 @@
+target
+Cargo.lock
diff --git a/rc2/Cargo.toml b/rc2/Cargo.toml
new file mode 100644
index 00000000..29ac25e7
--- /dev/null
+++ b/rc2/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "rc2"
+version = "0.1.0"
+authors = ["Trojan295 <trojan295@gmail.com>"]
+
+[dependencies]
+block-cipher-trait = "0.2"
diff --git a/rc2/README.md b/rc2/README.md
new file mode 100644
index 00000000..15e01a27
--- /dev/null
+++ b/rc2/README.md
@@ -0,0 +1,13 @@
+Name: RC2
+Type: Block cipher
+
+Input: 64 bits
+Output 64 bits
+
+Key length: variable (1-128 bytes)
+
+
+Algorythms:
+1. Key expansion.
+2. Encryption
+3. Decryption
diff --git a/rc2/src/consts.rs b/rc2/src/consts.rs
new file mode 100644
index 00000000..ef250f48
--- /dev/null
+++ b/rc2/src/consts.rs
@@ -0,0 +1,19 @@
+
+pub static PITABLE: [u8; 256] = [
+217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
+198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
+ 23,154, 89,245,135,179, 79, 19, 97, 69,109,141,  9,129,125, 50,
+189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
+ 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
+ 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
+111,191, 14,218, 70,105,  7, 87, 39,242, 29,155,188,148, 67,  3,
+248, 17,199,246,144,239, 62,231,  6,195,213, 47,200,102, 30,215,
+  8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
+150, 26,210,113, 90, 21, 73,116, 75,159,208, 94,  4, 24,164,236,
+194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
+153,124, 58,133, 35,184,180,122,252,  2, 54, 91, 37, 85,151, 49,
+ 45, 93,250,152,227,138,146,174,  5,223, 41, 16,103,108,186,201,
+211,  0,230,207,225,158,168, 44, 99, 22,  1, 63, 88,226,137,169,
+ 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
+197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
+];
diff --git a/rc2/src/lib.rs b/rc2/src/lib.rs
new file mode 100644
index 00000000..e6c5d6c5
--- /dev/null
+++ b/rc2/src/lib.rs
@@ -0,0 +1,130 @@
+extern crate block_cipher_trait;
+
+
+use block_cipher_trait::{Block, BlockCipher, BlockCipherVarKey};
+
+mod consts;
+use consts::PITABLE;
+
+
+#[derive(Clone,Copy)]
+pub struct RC2 {
+    s: [[u32; 256]; 4],
+    p: [u32; 18]
+}
+
+fn expand_key(key: Vec<u8>) -> [u16; 64] {
+    let key_len = key.len() as usize;
+
+    let t1: usize = key_len<<3;
+    let t8: usize = (t1+7)>>3;
+
+    let tm: usize = (255 % ((2 as u32).pow((8+t1-8*t8) as u32))) as usize;
+
+    let mut key_buffer: [u8; 128] = [0; 128];
+    for i in 0..key_len {
+        key_buffer[i] = key[i];
+    }
+
+    for i in key_len..128 {
+        let pos: u32 = (key_buffer[i-1] as u32 + key_buffer[i-key_len] as u32) & 0xff;
+        key_buffer[i] = PITABLE[pos as usize];
+    }
+
+    key_buffer[128-t8] = PITABLE[(key_buffer[128-t8] & tm as u8) as usize];
+
+    for i in (0..128-t8).rev() {
+        let pos: usize = (key_buffer[i+1] ^ key_buffer[i+t8]) as usize;
+        key_buffer[i] = PITABLE[pos];
+    }
+
+    let mut result: [u16; 64] = [0; 64];
+    for i in 0..64 {
+        result[i] = ((key_buffer[2*i+1] as u16) << 8) + (key_buffer[2*i] as u16)
+    }
+    result
+}
+
+fn mix(r: &mut [u16; 4], j: &mut usize, k: &[u16; 64]) {
+    r[0] = r[0] + k[*j] + (r[3] & r[2]) + (!r[3] & r[1]);
+    *j += 1;
+    r[0] = (r[0] << 1) | (r[0] >> 15);
+
+    r[1] = r[1] + k[*j] + (r[0] & r[3]) + (!r[0] & r[2]);
+    *j += 1;
+    r[1] = (r[1] << 2) | (r[1] >> 14);
+
+    r[2] = r[2] + k[*j] + (r[1] & r[0]) + (!r[1] & r[3]);
+    *j += 1;
+    r[2] = (r[2] << 3) | (r[2] >> 13);
+
+    r[3] = r[3] + k[*j] + (r[2] & r[1]) + (!r[2] & r[0]);
+    *j += 1;
+    r[3] = (r[3] << 5) | (r[3] >> 11);
+}
+
+fn mash(r: &mut [u16; 4], k: &[u16; 64]) {
+    r[0] = r[0] + k[(r[3] & 63) as usize];
+    r[1] = r[1] + k[(r[0] & 63) as usize];
+    r[2] = r[2] + k[(r[1] & 63) as usize];
+    r[3] = r[3] + k[(r[2] & 63) as usize];
+}
+
+fn rc2_encrypt(block: [u8; 8], k: [u16; 64]) -> [u8; 8] {
+    let mut r: [u16; 4] = [0; 4];
+
+    r[0] = ((block[1] as u16) << 8) + (block[0] as u16);
+    r[1] = ((block[3] as u16) << 8) + (block[2] as u16);
+    r[2] = ((block[5] as u16) << 8) + (block[4] as u16);
+    r[3] = ((block[7] as u16) << 8) + (block[6] as u16);
+
+    let mut j = 0;
+
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+
+    mash(&mut r, &k);
+
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+
+    mash(&mut r, &k);
+
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+    mix(&mut r, &mut j, &k);
+
+    let mut ct: [u8; 8] = [0; 8];
+    ct[0] = r[0] as u8;
+    ct[1] = (r[0] >> 8) as u8;
+    ct[2] = r[1] as u8;
+    ct[3] = (r[1] >> 8) as u8;
+    ct[4] = r[2] as u8;
+    ct[5] = (r[2] >> 8) as u8;
+    ct[6] = r[3] as u8;
+    ct[7] = (r[3] >> 8) as u8;
+
+    ct
+}
+
+#[test]
+fn test_rc2_encrypt() {
+    let key = expand_key(vec![255,255,255,255,255,255,255,255]);
+    let pt = [255,255,255,255,255,255,255,255];
+    let ct = rc2_encrypt(pt, key);
+    assert_eq!(ct, [0x27,0x8b,0x27,0xe4,0x2e,0x2f,0x0d,0x49]);
+
+    let key = expand_key(vec![0x30,0,0,0,0,0,0,0]);
+    let pt = [0x10,0,0,0,0,0,0,0x01];
+    let ct = rc2_encrypt(pt, key);
+    assert_eq!(ct, [0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2]);
+}

From 7bb041d6c92bdbcaa281e273a4593408c1b6051b Mon Sep 17 00:00:00 2001
From: Damian Czaja <damian@damian-pc.home>
Date: Sun, 23 Apr 2017 17:59:22 +0200
Subject: [PATCH 2/6] Added decryption

---
 rc2/.gitignore              |   2 -
 rc2/Cargo.toml              |  12 +-
 rc2/LICENSE-APACHE          |  13 ++
 rc2/LICENSE-MIT             |  25 ++++
 rc2/README.md               |  13 --
 rc2/benches/lib.rs          |   7 +
 rc2/src/lib.rs              | 261 ++++++++++++++++++++++--------------
 rc2/tests/data/1.input.bin  |   1 +
 rc2/tests/data/1.key.bin    |   1 +
 rc2/tests/data/1.output.bin |   1 +
 rc2/tests/data/2.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/2.key.bin    |   1 +
 rc2/tests/data/2.output.bin |   1 +
 rc2/tests/lib.rs            |  12 ++
 14 files changed, 235 insertions(+), 115 deletions(-)
 delete mode 100644 rc2/.gitignore
 create mode 100644 rc2/LICENSE-APACHE
 create mode 100644 rc2/LICENSE-MIT
 delete mode 100644 rc2/README.md
 create mode 100644 rc2/benches/lib.rs
 create mode 100644 rc2/tests/data/1.input.bin
 create mode 100644 rc2/tests/data/1.key.bin
 create mode 100644 rc2/tests/data/1.output.bin
 create mode 100644 rc2/tests/data/2.input.bin
 create mode 100644 rc2/tests/data/2.key.bin
 create mode 100644 rc2/tests/data/2.output.bin
 create mode 100644 rc2/tests/lib.rs

diff --git a/rc2/.gitignore b/rc2/.gitignore
deleted file mode 100644
index a9d37c56..00000000
--- a/rc2/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-target
-Cargo.lock
diff --git a/rc2/Cargo.toml b/rc2/Cargo.toml
index 29ac25e7..762fdab0 100644
--- a/rc2/Cargo.toml
+++ b/rc2/Cargo.toml
@@ -1,7 +1,17 @@
 [package]
 name = "rc2"
 version = "0.1.0"
-authors = ["Trojan295 <trojan295@gmail.com>"]
+authors = ["The Rust-Crypto Project Developers"]
+license = "MIT/Apache-2.0"
+description = "RC2 block cipher"
+documentation = "https://docs.rs/rc2"
+repository = "https://github.com/RustCrypto/block-ciphers"
+keywords = ["crypto", "rc2", "block-cipher"]
 
 [dependencies]
+generic-array = "0.5"
 block-cipher-trait = "0.2"
+byte-tools = "0.1"
+
+[dev-dependencies]
+crypto-tests = "^0.2.4"
diff --git a/rc2/LICENSE-APACHE b/rc2/LICENSE-APACHE
new file mode 100644
index 00000000..652489d7
--- /dev/null
+++ b/rc2/LICENSE-APACHE
@@ -0,0 +1,13 @@
+Copyright 2017 Damian Czaja
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/rc2/LICENSE-MIT b/rc2/LICENSE-MIT
new file mode 100644
index 00000000..069bb21e
--- /dev/null
+++ b/rc2/LICENSE-MIT
@@ -0,0 +1,25 @@
+Copyright (c) 2017 Damian Czaja
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/rc2/README.md b/rc2/README.md
deleted file mode 100644
index 15e01a27..00000000
--- a/rc2/README.md
+++ /dev/null
@@ -1,13 +0,0 @@
-Name: RC2
-Type: Block cipher
-
-Input: 64 bits
-Output 64 bits
-
-Key length: variable (1-128 bytes)
-
-
-Algorythms:
-1. Key expansion.
-2. Encryption
-3. Decryption
diff --git a/rc2/benches/lib.rs b/rc2/benches/lib.rs
new file mode 100644
index 00000000..f29719ae
--- /dev/null
+++ b/rc2/benches/lib.rs
@@ -0,0 +1,7 @@
+#![no_std]
+#![feature(test)]
+#[macro_use]
+extern crate crypto_tests;
+extern crate rc2;
+
+bench_block_cipher!(rc2::RC2, &[0u8; 16], &[1u8; 8]);
diff --git a/rc2/src/lib.rs b/rc2/src/lib.rs
index e6c5d6c5..fc48c0f3 100644
--- a/rc2/src/lib.rs
+++ b/rc2/src/lib.rs
@@ -1,130 +1,193 @@
+#![no_std]
 extern crate block_cipher_trait;
-
+extern crate generic_array;
+extern crate byte_tools;
 
 use block_cipher_trait::{Block, BlockCipher, BlockCipherVarKey};
+use generic_array::typenum::U8;
 
 mod consts;
 use consts::PITABLE;
 
 
-#[derive(Clone,Copy)]
 pub struct RC2 {
-    s: [[u32; 256]; 4],
-    p: [u32; 18]
+    exp_key: [u16; 64],
 }
 
-fn expand_key(key: Vec<u8>) -> [u16; 64] {
-    let key_len = key.len() as usize;
+impl RC2 {
+    fn expand_key(key: &[u8]) -> [u16; 64] {
+        let key_len = key.len() as usize;
 
-    let t1: usize = key_len<<3;
-    let t8: usize = (t1+7)>>3;
+        let t1: usize = key_len<<3;
+        let t8: usize = (t1+7)>>3;
 
-    let tm: usize = (255 % ((2 as u32).pow((8+t1-8*t8) as u32))) as usize;
+        let tm: usize = (255 % ((2 as u32).pow((8+t1-8*t8) as u32))) as usize;
 
-    let mut key_buffer: [u8; 128] = [0; 128];
-    for i in 0..key_len {
-        key_buffer[i] = key[i];
-    }
+        let mut key_buffer: [u8; 128] = [0; 128];
+        for i in 0..key_len {
+            key_buffer[i] = key[i];
+        }
+
+        for i in key_len..128 {
+            let pos: u32 = (key_buffer[i-1] as u32 + key_buffer[i-key_len] as u32) & 0xff;
+            key_buffer[i] = PITABLE[pos as usize];
+        }
+
+        key_buffer[128-t8] = PITABLE[(key_buffer[128-t8] & tm as u8) as usize];
+
+        for i in (0..128-t8).rev() {
+            let pos: usize = (key_buffer[i+1] ^ key_buffer[i+t8]) as usize;
+            key_buffer[i] = PITABLE[pos];
+        }
 
-    for i in key_len..128 {
-        let pos: u32 = (key_buffer[i-1] as u32 + key_buffer[i-key_len] as u32) & 0xff;
-        key_buffer[i] = PITABLE[pos as usize];
+        let mut result: [u16; 64] = [0; 64];
+        for i in 0..64 {
+            result[i] = ((key_buffer[2*i+1] as u16) << 8) + (key_buffer[2*i] as u16)
+        }
+        result
     }
 
-    key_buffer[128-t8] = PITABLE[(key_buffer[128-t8] & tm as u8) as usize];
+    fn mix(&self, r: &mut [u16; 4], j: &mut usize) {
+        r[0] = r[0].wrapping_add(self.exp_key[*j]).wrapping_add(r[3] & r[2]).wrapping_add(!r[3] & r[1]);
+        *j += 1;
+        r[0] = (r[0] << 1) | (r[0] >> 15);
 
-    for i in (0..128-t8).rev() {
-        let pos: usize = (key_buffer[i+1] ^ key_buffer[i+t8]) as usize;
-        key_buffer[i] = PITABLE[pos];
+        r[1] = r[1].wrapping_add(self.exp_key[*j]).wrapping_add(r[0] & r[3]).wrapping_add(!r[0] & r[2]);
+        *j += 1;
+        r[1] = (r[1] << 2) | (r[1] >> 14);
+
+        r[2] = r[2].wrapping_add(self.exp_key[*j]).wrapping_add(r[1] & r[0]).wrapping_add(!r[1] & r[3]);
+        *j += 1;
+        r[2] = (r[2] << 3) | (r[2] >> 13);
+
+        r[3] = r[3].wrapping_add(self.exp_key[*j]).wrapping_add(r[2] & r[1]).wrapping_add(!r[2] & r[0]);
+        *j += 1;
+        r[3] = (r[3] << 5) | (r[3] >> 11);
     }
 
-    let mut result: [u16; 64] = [0; 64];
-    for i in 0..64 {
-        result[i] = ((key_buffer[2*i+1] as u16) << 8) + (key_buffer[2*i] as u16)
+    fn mash(&self, r: &mut [u16; 4]) {
+        r[0] = r[0].wrapping_add(self.exp_key[(r[3] & 63) as usize]);
+        r[1] = r[1].wrapping_add(self.exp_key[(r[0] & 63) as usize]);
+        r[2] = r[2].wrapping_add(self.exp_key[(r[1] & 63) as usize]);
+        r[3] = r[3].wrapping_add(self.exp_key[(r[2] & 63) as usize]);
     }
-    result
-}
 
-fn mix(r: &mut [u16; 4], j: &mut usize, k: &[u16; 64]) {
-    r[0] = r[0] + k[*j] + (r[3] & r[2]) + (!r[3] & r[1]);
-    *j += 1;
-    r[0] = (r[0] << 1) | (r[0] >> 15);
+    fn reverse_mix(&self, r: &mut [u16; 4], j: &mut usize) {
+        r[3] = (r[3] << 11) | (r[3] >> 5);
+        r[3] = r[3].wrapping_sub(self.exp_key[*j]).wrapping_sub(r[2] & r[1]).wrapping_sub(!r[2] & r[0]);
+        *j -= 1;
 
-    r[1] = r[1] + k[*j] + (r[0] & r[3]) + (!r[0] & r[2]);
-    *j += 1;
-    r[1] = (r[1] << 2) | (r[1] >> 14);
+        r[2] = (r[2] << 13) | (r[2] >> 3);
+        r[2] = r[2].wrapping_sub(self.exp_key[*j]).wrapping_sub(r[1] & r[0]).wrapping_sub(!r[1] & r[3]);
+        *j -= 1;
 
-    r[2] = r[2] + k[*j] + (r[1] & r[0]) + (!r[1] & r[3]);
-    *j += 1;
-    r[2] = (r[2] << 3) | (r[2] >> 13);
+        r[1] = (r[1] << 14) | (r[1] >> 2);
+        r[1] = r[1].wrapping_sub(self.exp_key[*j]).wrapping_sub(r[0] & r[3]).wrapping_sub(!r[0] & r[2]);
+        *j -= 1;
 
-    r[3] = r[3] + k[*j] + (r[2] & r[1]) + (!r[2] & r[0]);
-    *j += 1;
-    r[3] = (r[3] << 5) | (r[3] >> 11);
-}
+        r[0] = (r[0] << 15) | (r[0] >> 1);
+        r[0] = r[0].wrapping_sub(self.exp_key[*j]).wrapping_sub(r[3] & r[2]).wrapping_sub(!r[3] & r[1]);
+        *j = j.wrapping_sub(1);
+    }
 
-fn mash(r: &mut [u16; 4], k: &[u16; 64]) {
-    r[0] = r[0] + k[(r[3] & 63) as usize];
-    r[1] = r[1] + k[(r[0] & 63) as usize];
-    r[2] = r[2] + k[(r[1] & 63) as usize];
-    r[3] = r[3] + k[(r[2] & 63) as usize];
+    fn reverse_mash(&self, r: &mut [u16; 4]) {
+        r[3] = r[3].wrapping_sub(self.exp_key[(r[2] & 63) as usize]);
+        r[2] = r[2].wrapping_sub(self.exp_key[(r[1] & 63) as usize]);
+        r[1] = r[1].wrapping_sub(self.exp_key[(r[0] & 63) as usize]);
+        r[0] = r[0].wrapping_sub(self.exp_key[(r[3] & 63) as usize]);
+    }
+
+    fn encrypt(&self, block: [u8; 8]) -> [u8; 8] {
+        let mut r: [u16; 4] = [0; 4];
+
+        r[0] = ((block[1] as u16) << 8) + (block[0] as u16);
+        r[1] = ((block[3] as u16) << 8) + (block[2] as u16);
+        r[2] = ((block[5] as u16) << 8) + (block[4] as u16);
+        r[3] = ((block[7] as u16) << 8) + (block[6] as u16);
+
+        let mut j = 0;
+
+        for i in 0..16 {
+            self.mix(&mut r, &mut j);
+            if i == 4 || i == 10 {
+                self.mash(&mut r);
+            }
+        }
+
+        let mut ct: [u8; 8] = [0; 8];
+        ct[0] = r[0] as u8;
+        ct[1] = (r[0] >> 8) as u8;
+        ct[2] = r[1] as u8;
+        ct[3] = (r[1] >> 8) as u8;
+        ct[4] = r[2] as u8;
+        ct[5] = (r[2] >> 8) as u8;
+        ct[6] = r[3] as u8;
+        ct[7] = (r[3] >> 8) as u8;
+
+        ct
+    }
+
+    fn decrypt(&self, block: [u8; 8]) -> [u8; 8] {
+        let mut r: [u16; 4] = [0; 4];
+
+        r[0] = ((block[1] as u16) << 8) + (block[0] as u16);
+        r[1] = ((block[3] as u16) << 8) + (block[2] as u16);
+        r[2] = ((block[5] as u16) << 8) + (block[4] as u16);
+        r[3] = ((block[7] as u16) << 8) + (block[6] as u16);
+
+        let mut j = 63;
+
+        for i in 0..16 {
+            self.reverse_mix(&mut r, &mut j);
+            if i == 4 || i == 10 {
+                self.reverse_mash(&mut r);
+            }
+        }
+
+        let mut ct: [u8; 8] = [0; 8];
+        ct[0] = r[0] as u8;
+        ct[1] = (r[0] >> 8) as u8;
+        ct[2] = r[1] as u8;
+        ct[3] = (r[1] >> 8) as u8;
+        ct[4] = r[2] as u8;
+        ct[5] = (r[2] >> 8) as u8;
+        ct[6] = r[3] as u8;
+        ct[7] = (r[3] >> 8) as u8;
+
+        ct
+    }
 }
 
-fn rc2_encrypt(block: [u8; 8], k: [u16; 64]) -> [u8; 8] {
-    let mut r: [u16; 4] = [0; 4];
-
-    r[0] = ((block[1] as u16) << 8) + (block[0] as u16);
-    r[1] = ((block[3] as u16) << 8) + (block[2] as u16);
-    r[2] = ((block[5] as u16) << 8) + (block[4] as u16);
-    r[3] = ((block[7] as u16) << 8) + (block[6] as u16);
-
-    let mut j = 0;
-
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-
-    mash(&mut r, &k);
-
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-
-    mash(&mut r, &k);
-
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-    mix(&mut r, &mut j, &k);
-
-    let mut ct: [u8; 8] = [0; 8];
-    ct[0] = r[0] as u8;
-    ct[1] = (r[0] >> 8) as u8;
-    ct[2] = r[1] as u8;
-    ct[3] = (r[1] >> 8) as u8;
-    ct[4] = r[2] as u8;
-    ct[5] = (r[2] >> 8) as u8;
-    ct[6] = r[3] as u8;
-    ct[7] = (r[3] >> 8) as u8;
-
-    ct
+impl BlockCipherVarKey for RC2 {
+    fn new(key: &[u8]) -> RC2 {
+        RC2 {
+            exp_key: RC2::expand_key(key)
+        }
+    }
 }
 
-#[test]
-fn test_rc2_encrypt() {
-    let key = expand_key(vec![255,255,255,255,255,255,255,255]);
-    let pt = [255,255,255,255,255,255,255,255];
-    let ct = rc2_encrypt(pt, key);
-    assert_eq!(ct, [0x27,0x8b,0x27,0xe4,0x2e,0x2f,0x0d,0x49]);
-
-    let key = expand_key(vec![0x30,0,0,0,0,0,0,0]);
-    let pt = [0x10,0,0,0,0,0,0,0x01];
-    let ct = rc2_encrypt(pt, key);
-    assert_eq!(ct, [0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2]);
+impl BlockCipher for RC2 {
+    type BlockSize = U8;
+
+    fn encrypt_block(&self, input: &Block<U8>, output: &mut Block<U8>) {
+        let mut iblock: [u8; 8] = [0; 8];
+        for i in 0..8 {
+            iblock[i] = input[i];
+        }
+        let oblock = self.encrypt(iblock);
+        for i in 0..8 {
+            output[i] = oblock[i];
+        }
+    }
+
+    fn decrypt_block(&self, input: &Block<U8>, output: &mut Block<U8>) {
+        let mut iblock: [u8; 8] = [0; 8];
+        for i in 0..8 {
+            iblock[i] = input[i];
+        }
+        let oblock = self.decrypt(iblock);
+        for i in 0..8 {
+            output[i] = oblock[i];
+        }
+    }
 }
diff --git a/rc2/tests/data/1.input.bin b/rc2/tests/data/1.input.bin
new file mode 100644
index 00000000..8663f7de
--- /dev/null
+++ b/rc2/tests/data/1.input.bin
@@ -0,0 +1 @@
+ÿÿÿÿÿÿÿÿ
\ No newline at end of file
diff --git a/rc2/tests/data/1.key.bin b/rc2/tests/data/1.key.bin
new file mode 100644
index 00000000..8663f7de
--- /dev/null
+++ b/rc2/tests/data/1.key.bin
@@ -0,0 +1 @@
+ÿÿÿÿÿÿÿÿ
\ No newline at end of file
diff --git a/rc2/tests/data/1.output.bin b/rc2/tests/data/1.output.bin
new file mode 100644
index 00000000..d6ac753c
--- /dev/null
+++ b/rc2/tests/data/1.output.bin
@@ -0,0 +1 @@
+'‹'ä./
I
\ No newline at end of file
diff --git a/rc2/tests/data/2.input.bin b/rc2/tests/data/2.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e
GIT binary patch
diff --git a/rc2/tests/data/2.key.bin b/rc2/tests/data/2.key.bin
new file mode 100644
index 00000000..42b75e5f
--- /dev/null
+++ b/rc2/tests/data/2.key.bin
@@ -0,0 +1 @@
+ˆ¼©�‡ZyÃ„b{¯²
\ No newline at end of file
diff --git a/rc2/tests/data/2.output.bin b/rc2/tests/data/2.output.bin
new file mode 100644
index 00000000..d7c6ab03
--- /dev/null
+++ b/rc2/tests/data/2.output.bin
@@ -0,0 +1 @@
+"iU*°ø\¦
\ No newline at end of file
diff --git a/rc2/tests/lib.rs b/rc2/tests/lib.rs
new file mode 100644
index 00000000..ef957637
--- /dev/null
+++ b/rc2/tests/lib.rs
@@ -0,0 +1,12 @@
+#![no_std]
+#[macro_use]
+extern crate crypto_tests;
+extern crate rc2;
+
+use crypto_tests::block_cipher::{BlockCipherTest, encrypt_decrypt};
+
+#[test]
+fn rc2() {
+    let tests = new_block_cipher_tests!("1", "2");
+    encrypt_decrypt::<rc2::RC2>(&tests);
+}

From 6f74e0801b208046ed91e1bcca883b19fc866930 Mon Sep 17 00:00:00 2001
From: Damian Czaja <trojan295@gmail.com>
Date: Tue, 25 Apr 2017 22:56:28 +0200
Subject: [PATCH 3/6] Added option to create RC2 cipher with different
 effective key length

---
 rc2/src/lib.rs              |  12 +++++++++---
 rc2/tests/data/1.input.bin  | Bin 8 -> 8 bytes
 rc2/tests/data/1.key.bin    | Bin 8 -> 8 bytes
 rc2/tests/data/1.output.bin |   2 +-
 rc2/tests/data/2.input.bin  | Bin 8 -> 8 bytes
 rc2/tests/data/2.key.bin    |   2 +-
 rc2/tests/data/2.output.bin |   2 +-
 rc2/tests/data/3.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/3.key.bin    | Bin 0 -> 8 bytes
 rc2/tests/data/3.output.bin |   1 +
 rc2/tests/data/4.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/4.key.bin    |   1 +
 rc2/tests/data/4.output.bin |   1 +
 rc2/tests/lib.rs            |  25 +++++++++++++++++++++++-
 rc2/tests/test_vectors.rs   |  38 ++++++++++++++++++++++++++++++++++++
 15 files changed, 77 insertions(+), 7 deletions(-)
 create mode 100644 rc2/tests/data/3.input.bin
 create mode 100644 rc2/tests/data/3.key.bin
 create mode 100644 rc2/tests/data/3.output.bin
 create mode 100644 rc2/tests/data/4.input.bin
 create mode 100644 rc2/tests/data/4.key.bin
 create mode 100644 rc2/tests/data/4.output.bin
 create mode 100644 rc2/tests/test_vectors.rs

diff --git a/rc2/src/lib.rs b/rc2/src/lib.rs
index fc48c0f3..c083e206 100644
--- a/rc2/src/lib.rs
+++ b/rc2/src/lib.rs
@@ -15,10 +15,16 @@ pub struct RC2 {
 }
 
 impl RC2 {
-    fn expand_key(key: &[u8]) -> [u16; 64] {
+    pub fn new_with_effective_key_length(key: &[u8], eff_key_len: usize) -> RC2 {
+        RC2 {
+            exp_key: RC2::expand_key(key, eff_key_len)
+        }
+    }
+
+    fn expand_key(key: &[u8], t1: usize) -> [u16; 64] {
         let key_len = key.len() as usize;
 
-        let t1: usize = key_len<<3;
+        //let t1: usize = key_len<<3;
         let t8: usize = (t1+7)>>3;
 
         let tm: usize = (255 % ((2 as u32).pow((8+t1-8*t8) as u32))) as usize;
@@ -161,7 +167,7 @@ impl RC2 {
 impl BlockCipherVarKey for RC2 {
     fn new(key: &[u8]) -> RC2 {
         RC2 {
-            exp_key: RC2::expand_key(key)
+            exp_key: RC2::expand_key(key, key.len()*8)
         }
     }
 }
diff --git a/rc2/tests/data/1.input.bin b/rc2/tests/data/1.input.bin
index 8663f7de5dd7a79b04f7fcc4fea0046e2659a1a1..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e 100644
GIT binary patch
diff --git a/rc2/tests/data/1.key.bin b/rc2/tests/data/1.key.bin
index 8663f7de5dd7a79b04f7fcc4fea0046e2659a1a1..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e 100644
GIT binary patch
diff --git a/rc2/tests/data/1.output.bin b/rc2/tests/data/1.output.bin
index d6ac753c..2ef3c18d 100644
--- a/rc2/tests/data/1.output.bin
+++ b/rc2/tests/data/1.output.bin
@@ -1 +1 @@
-'‹'ä./
I
\ No newline at end of file
+ë·sù“'Žÿ
\ No newline at end of file
diff --git a/rc2/tests/data/2.input.bin b/rc2/tests/data/2.input.bin
index 1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e..8663f7de5dd7a79b04f7fcc4fea0046e2659a1a1 100644
GIT binary patch
diff --git a/rc2/tests/data/2.key.bin b/rc2/tests/data/2.key.bin
index 42b75e5f..8663f7de 100644
--- a/rc2/tests/data/2.key.bin
+++ b/rc2/tests/data/2.key.bin
@@ -1 +1 @@
-ˆ¼©�‡ZyÃ„b{¯²
\ No newline at end of file
+ÿÿÿÿÿÿÿÿ
\ No newline at end of file
diff --git a/rc2/tests/data/2.output.bin b/rc2/tests/data/2.output.bin
index d7c6ab03..d6ac753c 100644
--- a/rc2/tests/data/2.output.bin
+++ b/rc2/tests/data/2.output.bin
@@ -1 +1 @@
-"iU*°ø\¦
\ No newline at end of file
+'‹'ä./
I
\ No newline at end of file
diff --git a/rc2/tests/data/3.input.bin b/rc2/tests/data/3.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..cae03540b01e423dbdbaeb525476275bb45e7c9d
GIT binary patch
diff --git a/rc2/tests/data/3.key.bin b/rc2/tests/data/3.key.bin
new file mode 100644
index 0000000000000000000000000000000000000000..8f19f54362bc273c54ac58d1bd8ac7a298c430c5
GIT binary patch
diff --git a/rc2/tests/data/3.output.bin b/rc2/tests/data/3.output.bin
new file mode 100644
index 00000000..a3013ac9
--- /dev/null
+++ b/rc2/tests/data/3.output.bin
@@ -0,0 +1 @@
+0džß›çÒÂ
\ No newline at end of file
diff --git a/rc2/tests/data/4.input.bin b/rc2/tests/data/4.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e
GIT binary patch
diff --git a/rc2/tests/data/4.key.bin b/rc2/tests/data/4.key.bin
new file mode 100644
index 00000000..42b75e5f
--- /dev/null
+++ b/rc2/tests/data/4.key.bin
@@ -0,0 +1 @@
+ˆ¼©�‡ZyÃ„b{¯²
\ No newline at end of file
diff --git a/rc2/tests/data/4.output.bin b/rc2/tests/data/4.output.bin
new file mode 100644
index 00000000..d7c6ab03
--- /dev/null
+++ b/rc2/tests/data/4.output.bin
@@ -0,0 +1 @@
+"iU*°ø\¦
\ No newline at end of file
diff --git a/rc2/tests/lib.rs b/rc2/tests/lib.rs
index ef957637..52c1e678 100644
--- a/rc2/tests/lib.rs
+++ b/rc2/tests/lib.rs
@@ -3,10 +3,33 @@
 extern crate crypto_tests;
 extern crate rc2;
 
+mod test_vectors;
+
 use crypto_tests::block_cipher::{BlockCipherTest, encrypt_decrypt};
 
+extern crate block_cipher_trait;
+extern crate generic_array;
+
+use block_cipher_trait::from_slice;
+use generic_array::GenericArray;
+use block_cipher_trait::BlockCipher;
+
 #[test]
 fn rc2() {
-    let tests = new_block_cipher_tests!("1", "2");
+    let tests = new_block_cipher_tests!("1", "2", "3", "4");
     encrypt_decrypt::<rc2::RC2>(&tests);
 }
+
+#[test]
+fn rc2_with_different_effective_key_length() {
+    let mut buf = GenericArray::new();
+
+    for test in test_vectors::RC2_EFF_KEY_LEN_TESTS {
+        let cipher = rc2::RC2::new_with_effective_key_length(test.key, test.eff_key_length);
+
+        cipher.encrypt_block(&from_slice(test.input), &mut buf);
+        assert_eq!(test.output, &buf[..]);
+        cipher.decrypt_block(&from_slice(test.output), &mut buf);
+        assert_eq!(test.input, &buf[..]);
+    }
+}
diff --git a/rc2/tests/test_vectors.rs b/rc2/tests/test_vectors.rs
new file mode 100644
index 00000000..33306c74
--- /dev/null
+++ b/rc2/tests/test_vectors.rs
@@ -0,0 +1,38 @@
+
+pub struct RC2CipherTest {
+    pub key: &'static [u8],
+    pub eff_key_length: usize,
+    pub input: &'static [u8],
+    pub output: &'static [u8],
+}
+
+pub static RC2_EFF_KEY_LEN_TESTS: &[RC2CipherTest] = &[
+    RC2CipherTest {
+        key: &[0x88],
+        eff_key_length: 64,
+        input: &[0;8],
+        output: &[0x61, 0xa8, 0xa2, 0x44, 0xad, 0xac, 0xcc, 0xf0],
+    },
+    RC2CipherTest {
+        key: &[0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a],
+        eff_key_length: 64,
+        input: &[0;8],
+        output: &[0x6c, 0xcf, 0x43, 0x08, 0x97, 0x4c, 0x26, 0x7f],
+    },
+    RC2CipherTest {
+        key: &[0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f, 0x0f,
+            0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2],
+        eff_key_length: 64,
+        input: &[0;8],
+        output: &[ 0x1a, 0x80, 0x7d, 0x27, 0x2b, 0xbe, 0x5d, 0xb1],
+    },
+    RC2CipherTest {
+        key: &[0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f, 0x0f,
+            0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2, 0x16, 0xf8, 0x0a,
+            0x6f, 0x85, 0x92, 0x05, 0x84, 0xc4, 0x2f, 0xce, 0xb0, 0xbe,
+            0x25, 0x5d, 0xaf, 0x1e],
+        eff_key_length: 129,
+        input: &[0;8],
+        output: &[0x5b, 0x78, 0xd3, 0xa4, 0x3d, 0xff, 0xf1, 0xf1],
+    }
+];

From 5c14465b79a3776ff0e95fb766b1d8d00879514d Mon Sep 17 00:00:00 2001
From: Damian Czaja <trojan295@gmail.com>
Date: Tue, 25 Apr 2017 23:01:52 +0200
Subject: [PATCH 4/6] Fixed problem with build on Rust stable

---
 rc2/tests/test_vectors.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rc2/tests/test_vectors.rs b/rc2/tests/test_vectors.rs
index 33306c74..a45709ac 100644
--- a/rc2/tests/test_vectors.rs
+++ b/rc2/tests/test_vectors.rs
@@ -6,7 +6,7 @@ pub struct RC2CipherTest {
     pub output: &'static [u8],
 }
 
-pub static RC2_EFF_KEY_LEN_TESTS: &[RC2CipherTest] = &[
+pub static RC2_EFF_KEY_LEN_TESTS: &'static [RC2CipherTest] = &[
     RC2CipherTest {
         key: &[0x88],
         eff_key_length: 64,

From 1f93138959db69a4dc39f048c0f9d6d9407a48d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?=
 =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= <newpavlov@gmail.com>
Date: Thu, 27 Apr 2017 01:31:36 +0300
Subject: [PATCH 5/6] Minor modifications

---
 rc2/src/consts.rs           |   2 +-
 rc2/src/lib.rs              | 107 +++++++++++++++---------------------
 rc2/tests/data/4.key.bin    |   2 +-
 rc2/tests/data/4.output.bin |   2 +-
 rc2/tests/data/5.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/5.key.bin    |   1 +
 rc2/tests/data/5.output.bin |   1 +
 rc2/tests/data/6.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/6.key.bin    |   1 +
 rc2/tests/data/6.output.bin |   1 +
 rc2/tests/data/7.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/7.key.bin    |   1 +
 rc2/tests/data/7.output.bin |   1 +
 rc2/tests/data/8.input.bin  | Bin 0 -> 8 bytes
 rc2/tests/data/8.key.bin    |   2 +
 rc2/tests/data/8.output.bin |   1 +
 rc2/tests/lib.rs            |  28 +++++++---
 rc2/tests/test_vectors.rs   |  38 -------------
 18 files changed, 76 insertions(+), 112 deletions(-)
 create mode 100644 rc2/tests/data/5.input.bin
 create mode 100644 rc2/tests/data/5.key.bin
 create mode 100644 rc2/tests/data/5.output.bin
 create mode 100644 rc2/tests/data/6.input.bin
 create mode 100644 rc2/tests/data/6.key.bin
 create mode 100644 rc2/tests/data/6.output.bin
 create mode 100644 rc2/tests/data/7.input.bin
 create mode 100644 rc2/tests/data/7.key.bin
 create mode 100644 rc2/tests/data/7.output.bin
 create mode 100644 rc2/tests/data/8.input.bin
 create mode 100644 rc2/tests/data/8.key.bin
 create mode 100644 rc2/tests/data/8.output.bin
 delete mode 100644 rc2/tests/test_vectors.rs

diff --git a/rc2/src/consts.rs b/rc2/src/consts.rs
index ef250f48..5574c3f2 100644
--- a/rc2/src/consts.rs
+++ b/rc2/src/consts.rs
@@ -1,5 +1,5 @@
 
-pub static PITABLE: [u8; 256] = [
+pub static PI_TABLE: [u8; 256] = [
 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
  23,154, 89,245,135,179, 79, 19, 97, 69,109,141,  9,129,125, 50,
diff --git a/rc2/src/lib.rs b/rc2/src/lib.rs
index c083e206..1536cec5 100644
--- a/rc2/src/lib.rs
+++ b/rc2/src/lib.rs
@@ -1,4 +1,7 @@
-#![no_std]
+//! An implementation of the [RC2][1] block cipher.
+//!
+//! [1]: https://en.wikipedia.org/wiki/RC2
+ #![no_std]
 extern crate block_cipher_trait;
 extern crate generic_array;
 extern crate byte_tools;
@@ -7,15 +10,16 @@ use block_cipher_trait::{Block, BlockCipher, BlockCipherVarKey};
 use generic_array::typenum::U8;
 
 mod consts;
-use consts::PITABLE;
-
+use consts::PI_TABLE;
 
+/// A structure that represents the block cipher initialized with a key
 pub struct RC2 {
     exp_key: [u16; 64],
 }
 
 impl RC2 {
-    pub fn new_with_effective_key_length(key: &[u8], eff_key_len: usize) -> RC2 {
+    /// Create a cipher with the specified effective key length
+    pub fn new_with_eff_key_len(key: &[u8], eff_key_len: usize) -> RC2 {
         RC2 {
             exp_key: RC2::expand_key(key, eff_key_len)
         }
@@ -24,7 +28,6 @@ impl RC2 {
     fn expand_key(key: &[u8], t1: usize) -> [u16; 64] {
         let key_len = key.len() as usize;
 
-        //let t1: usize = key_len<<3;
         let t8: usize = (t1+7)>>3;
 
         let tm: usize = (255 % ((2 as u32).pow((8+t1-8*t8) as u32))) as usize;
@@ -36,14 +39,14 @@ impl RC2 {
 
         for i in key_len..128 {
             let pos: u32 = (key_buffer[i-1] as u32 + key_buffer[i-key_len] as u32) & 0xff;
-            key_buffer[i] = PITABLE[pos as usize];
+            key_buffer[i] = PI_TABLE[pos as usize];
         }
 
-        key_buffer[128-t8] = PITABLE[(key_buffer[128-t8] & tm as u8) as usize];
+        key_buffer[128-t8] = PI_TABLE[(key_buffer[128-t8] & tm as u8) as usize];
 
         for i in (0..128-t8).rev() {
             let pos: usize = (key_buffer[i+1] ^ key_buffer[i+t8]) as usize;
-            key_buffer[i] = PITABLE[pos];
+            key_buffer[i] = PI_TABLE[pos];
         }
 
         let mut result: [u16; 64] = [0; 64];
@@ -103,13 +106,13 @@ impl RC2 {
         r[0] = r[0].wrapping_sub(self.exp_key[(r[3] & 63) as usize]);
     }
 
-    fn encrypt(&self, block: [u8; 8]) -> [u8; 8] {
-        let mut r: [u16; 4] = [0; 4];
-
-        r[0] = ((block[1] as u16) << 8) + (block[0] as u16);
-        r[1] = ((block[3] as u16) << 8) + (block[2] as u16);
-        r[2] = ((block[5] as u16) << 8) + (block[4] as u16);
-        r[3] = ((block[7] as u16) << 8) + (block[6] as u16);
+    fn encrypt(&self, block: &Block<U8>, output: &mut Block<U8>) {
+        let mut r: [u16; 4] = [
+            ((block[1] as u16) << 8) + (block[0] as u16),
+            ((block[3] as u16) << 8) + (block[2] as u16),
+            ((block[5] as u16) << 8) + (block[4] as u16),
+            ((block[7] as u16) << 8) + (block[6] as u16),
+        ];
 
         let mut j = 0;
 
@@ -120,26 +123,23 @@ impl RC2 {
             }
         }
 
-        let mut ct: [u8; 8] = [0; 8];
-        ct[0] = r[0] as u8;
-        ct[1] = (r[0] >> 8) as u8;
-        ct[2] = r[1] as u8;
-        ct[3] = (r[1] >> 8) as u8;
-        ct[4] = r[2] as u8;
-        ct[5] = (r[2] >> 8) as u8;
-        ct[6] = r[3] as u8;
-        ct[7] = (r[3] >> 8) as u8;
-
-        ct
+        output[0] = (r[0] & 0xff) as u8;
+        output[1] = (r[0] >> 8) as u8;
+        output[2] = (r[1] & 0xff) as u8;
+        output[3] = (r[1] >> 8) as u8;
+        output[4] = (r[2] & 0xff) as u8;
+        output[5] = (r[2] >> 8) as u8;
+        output[6] = (r[3] & 0xff) as u8;
+        output[7] = (r[3] >> 8) as u8;
     }
 
-    fn decrypt(&self, block: [u8; 8]) -> [u8; 8] {
-        let mut r: [u16; 4] = [0; 4];
-
-        r[0] = ((block[1] as u16) << 8) + (block[0] as u16);
-        r[1] = ((block[3] as u16) << 8) + (block[2] as u16);
-        r[2] = ((block[5] as u16) << 8) + (block[4] as u16);
-        r[3] = ((block[7] as u16) << 8) + (block[6] as u16);
+    fn decrypt(&self, block: &Block<U8>, output: &mut Block<U8>) {
+        let mut r: [u16; 4] = [
+            ((block[1] as u16) << 8) + (block[0] as u16),
+            ((block[3] as u16) << 8) + (block[2] as u16),
+            ((block[5] as u16) << 8) + (block[4] as u16),
+            ((block[7] as u16) << 8) + (block[6] as u16),
+        ];
 
         let mut j = 63;
 
@@ -150,25 +150,20 @@ impl RC2 {
             }
         }
 
-        let mut ct: [u8; 8] = [0; 8];
-        ct[0] = r[0] as u8;
-        ct[1] = (r[0] >> 8) as u8;
-        ct[2] = r[1] as u8;
-        ct[3] = (r[1] >> 8) as u8;
-        ct[4] = r[2] as u8;
-        ct[5] = (r[2] >> 8) as u8;
-        ct[6] = r[3] as u8;
-        ct[7] = (r[3] >> 8) as u8;
-
-        ct
+        output[0] = r[0] as u8;
+        output[1] = (r[0] >> 8) as u8;
+        output[2] = r[1] as u8;
+        output[3] = (r[1] >> 8) as u8;
+        output[4] = r[2] as u8;
+        output[5] = (r[2] >> 8) as u8;
+        output[6] = r[3] as u8;
+        output[7] = (r[3] >> 8) as u8;
     }
 }
 
 impl BlockCipherVarKey for RC2 {
     fn new(key: &[u8]) -> RC2 {
-        RC2 {
-            exp_key: RC2::expand_key(key, key.len()*8)
-        }
+        RC2::new_with_eff_key_len(key, key.len()*8)
     }
 }
 
@@ -176,24 +171,10 @@ impl BlockCipher for RC2 {
     type BlockSize = U8;
 
     fn encrypt_block(&self, input: &Block<U8>, output: &mut Block<U8>) {
-        let mut iblock: [u8; 8] = [0; 8];
-        for i in 0..8 {
-            iblock[i] = input[i];
-        }
-        let oblock = self.encrypt(iblock);
-        for i in 0..8 {
-            output[i] = oblock[i];
-        }
+        self.encrypt(input, output);
     }
 
     fn decrypt_block(&self, input: &Block<U8>, output: &mut Block<U8>) {
-        let mut iblock: [u8; 8] = [0; 8];
-        for i in 0..8 {
-            iblock[i] = input[i];
-        }
-        let oblock = self.decrypt(iblock);
-        for i in 0..8 {
-            output[i] = oblock[i];
-        }
+        self.decrypt(input, output);
     }
 }
diff --git a/rc2/tests/data/4.key.bin b/rc2/tests/data/4.key.bin
index 42b75e5f..ae9780bc 100644
--- a/rc2/tests/data/4.key.bin
+++ b/rc2/tests/data/4.key.bin
@@ -1 +1 @@
-ˆ¼©�‡ZyÃ„b{¯²
\ No newline at end of file
+ˆ
\ No newline at end of file
diff --git a/rc2/tests/data/4.output.bin b/rc2/tests/data/4.output.bin
index d7c6ab03..9aa3e7b1 100644
--- a/rc2/tests/data/4.output.bin
+++ b/rc2/tests/data/4.output.bin
@@ -1 +1 @@
-"iU*°ø\¦
\ No newline at end of file
+a¨¢D­¬Ìð
\ No newline at end of file
diff --git a/rc2/tests/data/5.input.bin b/rc2/tests/data/5.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e
GIT binary patch
diff --git a/rc2/tests/data/5.key.bin b/rc2/tests/data/5.key.bin
new file mode 100644
index 00000000..432eb3ef
--- /dev/null
+++ b/rc2/tests/data/5.key.bin
@@ -0,0 +1 @@
+ˆ¼©�‡Z
\ No newline at end of file
diff --git a/rc2/tests/data/5.output.bin b/rc2/tests/data/5.output.bin
new file mode 100644
index 00000000..396ec68a
--- /dev/null
+++ b/rc2/tests/data/5.output.bin
@@ -0,0 +1 @@
+lÏC—L&
\ No newline at end of file
diff --git a/rc2/tests/data/6.input.bin b/rc2/tests/data/6.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e
GIT binary patch
diff --git a/rc2/tests/data/6.key.bin b/rc2/tests/data/6.key.bin
new file mode 100644
index 00000000..42b75e5f
--- /dev/null
+++ b/rc2/tests/data/6.key.bin
@@ -0,0 +1 @@
+ˆ¼©�‡ZyÃ„b{¯²
\ No newline at end of file
diff --git a/rc2/tests/data/6.output.bin b/rc2/tests/data/6.output.bin
new file mode 100644
index 00000000..ff0c5565
--- /dev/null
+++ b/rc2/tests/data/6.output.bin
@@ -0,0 +1 @@
+€}'+¾]±
\ No newline at end of file
diff --git a/rc2/tests/data/7.input.bin b/rc2/tests/data/7.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e
GIT binary patch
diff --git a/rc2/tests/data/7.key.bin b/rc2/tests/data/7.key.bin
new file mode 100644
index 00000000..42b75e5f
--- /dev/null
+++ b/rc2/tests/data/7.key.bin
@@ -0,0 +1 @@
+ˆ¼©�‡ZyÃ„b{¯²
\ No newline at end of file
diff --git a/rc2/tests/data/7.output.bin b/rc2/tests/data/7.output.bin
new file mode 100644
index 00000000..d7c6ab03
--- /dev/null
+++ b/rc2/tests/data/7.output.bin
@@ -0,0 +1 @@
+"iU*°ø\¦
\ No newline at end of file
diff --git a/rc2/tests/data/8.input.bin b/rc2/tests/data/8.input.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e
GIT binary patch
diff --git a/rc2/tests/data/8.key.bin b/rc2/tests/data/8.key.bin
new file mode 100644
index 00000000..5e2685de
--- /dev/null
+++ b/rc2/tests/data/8.key.bin
@@ -0,0 +1,2 @@
+ˆ¼©�‡ZyÃ„b{¯²ø
+o…’„Ä/Î°¾%]¯
\ No newline at end of file
diff --git a/rc2/tests/data/8.output.bin b/rc2/tests/data/8.output.bin
new file mode 100644
index 00000000..8170ac08
--- /dev/null
+++ b/rc2/tests/data/8.output.bin
@@ -0,0 +1 @@
+[xÓ¤=ÿññ
\ No newline at end of file
diff --git a/rc2/tests/lib.rs b/rc2/tests/lib.rs
index 52c1e678..eae4d733 100644
--- a/rc2/tests/lib.rs
+++ b/rc2/tests/lib.rs
@@ -3,8 +3,6 @@
 extern crate crypto_tests;
 extern crate rc2;
 
-mod test_vectors;
-
 use crypto_tests::block_cipher::{BlockCipherTest, encrypt_decrypt};
 
 extern crate block_cipher_trait;
@@ -16,20 +14,34 @@ use block_cipher_trait::BlockCipher;
 
 #[test]
 fn rc2() {
-    let tests = new_block_cipher_tests!("1", "2", "3", "4");
+    let tests = new_block_cipher_tests!("1", "2", "3", "7");
     encrypt_decrypt::<rc2::RC2>(&tests);
 }
 
 #[test]
-fn rc2_with_different_effective_key_length() {
+fn rc2_effective_key_64() {
     let mut buf = GenericArray::new();
+    let tests = new_block_cipher_tests!("4", "5", "6");
+    for test in &tests {
+        let cipher = rc2::RC2::new_with_eff_key_len(test.key, 64);
+
+        cipher.encrypt_block(from_slice(test.input), &mut buf);
+        assert_eq!(test.output, &buf[..]);
+        cipher.decrypt_block(from_slice(test.output), &mut buf);
+        assert_eq!(test.input, &buf[..]);
+    }
+}
 
-    for test in test_vectors::RC2_EFF_KEY_LEN_TESTS {
-        let cipher = rc2::RC2::new_with_effective_key_length(test.key, test.eff_key_length);
+#[test]
+fn rc2_effective_key_129() {
+    let mut buf = GenericArray::new();
+    let tests = new_block_cipher_tests!("8");
+    for test in &tests {
+        let cipher = rc2::RC2::new_with_eff_key_len(test.key, 129);
 
-        cipher.encrypt_block(&from_slice(test.input), &mut buf);
+        cipher.encrypt_block(from_slice(test.input), &mut buf);
         assert_eq!(test.output, &buf[..]);
-        cipher.decrypt_block(&from_slice(test.output), &mut buf);
+        cipher.decrypt_block(from_slice(test.output), &mut buf);
         assert_eq!(test.input, &buf[..]);
     }
 }
diff --git a/rc2/tests/test_vectors.rs b/rc2/tests/test_vectors.rs
deleted file mode 100644
index a45709ac..00000000
--- a/rc2/tests/test_vectors.rs
+++ /dev/null
@@ -1,38 +0,0 @@
-
-pub struct RC2CipherTest {
-    pub key: &'static [u8],
-    pub eff_key_length: usize,
-    pub input: &'static [u8],
-    pub output: &'static [u8],
-}
-
-pub static RC2_EFF_KEY_LEN_TESTS: &'static [RC2CipherTest] = &[
-    RC2CipherTest {
-        key: &[0x88],
-        eff_key_length: 64,
-        input: &[0;8],
-        output: &[0x61, 0xa8, 0xa2, 0x44, 0xad, 0xac, 0xcc, 0xf0],
-    },
-    RC2CipherTest {
-        key: &[0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a],
-        eff_key_length: 64,
-        input: &[0;8],
-        output: &[0x6c, 0xcf, 0x43, 0x08, 0x97, 0x4c, 0x26, 0x7f],
-    },
-    RC2CipherTest {
-        key: &[0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f, 0x0f,
-            0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2],
-        eff_key_length: 64,
-        input: &[0;8],
-        output: &[ 0x1a, 0x80, 0x7d, 0x27, 0x2b, 0xbe, 0x5d, 0xb1],
-    },
-    RC2CipherTest {
-        key: &[0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f, 0x0f,
-            0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2, 0x16, 0xf8, 0x0a,
-            0x6f, 0x85, 0x92, 0x05, 0x84, 0xc4, 0x2f, 0xce, 0xb0, 0xbe,
-            0x25, 0x5d, 0xaf, 0x1e],
-        eff_key_length: 129,
-        input: &[0;8],
-        output: &[0x5b, 0x78, 0xd3, 0xa4, 0x3d, 0xff, 0xf1, 0xf1],
-    }
-];

From 3693a320704d588b98daa25a2f33ccc506b8414e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?=
 =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= <newpavlov@gmail.com>
Date: Thu, 27 Apr 2017 01:36:08 +0300
Subject: [PATCH 6/6] Use copy_from_slice instead of the loop

---
 rc2/src/lib.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/rc2/src/lib.rs b/rc2/src/lib.rs
index 1536cec5..f06c2b6e 100644
--- a/rc2/src/lib.rs
+++ b/rc2/src/lib.rs
@@ -33,9 +33,7 @@ impl RC2 {
         let tm: usize = (255 % ((2 as u32).pow((8+t1-8*t8) as u32))) as usize;
 
         let mut key_buffer: [u8; 128] = [0; 128];
-        for i in 0..key_len {
-            key_buffer[i] = key[i];
-        }
+        key_buffer[..key_len].copy_from_slice(&key[..key_len]);
 
         for i in key_len..128 {
             let pos: u32 = (key_buffer[i-1] as u32 + key_buffer[i-key_len] as u32) & 0xff;
