From 00816653399ceb3a06a35c3b7c3fb161891f5763 Mon Sep 17 00:00:00 2001 From: 0xa Date: Fri, 9 Dec 2016 17:06:15 +0000 Subject: [PATCH 1/3] Add Blowfish support --- openssl-sys/src/lib.rs | 4 ++++ openssl/src/symm.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 8e427c7a..142ec09b 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -1445,6 +1445,10 @@ extern { pub fn EVP_aes_256_cfb1() -> *const EVP_CIPHER; pub fn EVP_aes_256_cfb128() -> *const EVP_CIPHER; pub fn EVP_aes_256_cfb8() -> *const EVP_CIPHER; + pub fn EVP_bf_cbc() -> *const EVP_CIPHER; + pub fn EVP_bf_ecb() -> *const EVP_CIPHER; + pub fn EVP_bf_cfb() -> *const EVP_CIPHER; + pub fn EVP_bf_ofb() -> *const EVP_CIPHER; pub fn EVP_rc4() -> *const EVP_CIPHER; pub fn EVP_des_cbc() -> *const EVP_CIPHER; diff --git a/openssl/src/symm.rs b/openssl/src/symm.rs index f94a8d70..2704bae7 100644 --- a/openssl/src/symm.rs +++ b/openssl/src/symm.rs @@ -80,6 +80,22 @@ impl Cipher { unsafe { Cipher(ffi::EVP_aes_256_gcm()) } } + pub fn bf_cbc() -> Cipher { + unsafe { Cipher(ffi::EVP_bf_cbc()) } + } + + pub fn bf_ecb() -> Cipher { + unsafe { Cipher(ffi::EVP_bf_ecb()) } + } + + pub fn bf_cfb() -> Cipher { + unsafe { Cipher(ffi::EVP_bf_cfb()) } + } + + pub fn bf_ofb() -> Cipher { + unsafe { Cipher(ffi::EVP_bf_ofb()) } + } + pub fn des_cbc() -> Cipher { unsafe { Cipher(ffi::EVP_des_cbc()) } } From 0850f605b1fcb686bae341a4763d072af4c67e50 Mon Sep 17 00:00:00 2001 From: 0xa Date: Fri, 9 Dec 2016 18:42:10 +0000 Subject: [PATCH 2/3] Use EVP_bf_cfb64 instead of EVP_bf_cfb --- openssl-sys/src/lib.rs | 2 +- openssl/src/symm.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 142ec09b..3ac3c2c2 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -1447,7 +1447,7 @@ extern { pub fn EVP_aes_256_cfb8() -> *const EVP_CIPHER; pub fn EVP_bf_cbc() -> *const EVP_CIPHER; pub fn EVP_bf_ecb() -> *const EVP_CIPHER; - pub fn EVP_bf_cfb() -> *const EVP_CIPHER; + pub fn EVP_bf_cfb64() -> *const EVP_CIPHER; pub fn EVP_bf_ofb() -> *const EVP_CIPHER; pub fn EVP_rc4() -> *const EVP_CIPHER; diff --git a/openssl/src/symm.rs b/openssl/src/symm.rs index 2704bae7..1a46411d 100644 --- a/openssl/src/symm.rs +++ b/openssl/src/symm.rs @@ -88,8 +88,8 @@ impl Cipher { unsafe { Cipher(ffi::EVP_bf_ecb()) } } - pub fn bf_cfb() -> Cipher { - unsafe { Cipher(ffi::EVP_bf_cfb()) } + pub fn bf_cfb64() -> Cipher { + unsafe { Cipher(ffi::EVP_bf_cfb64()) } } pub fn bf_ofb() -> Cipher { From 5340895249164e0761996b5adbdb4587122880dc Mon Sep 17 00:00:00 2001 From: 0xa Date: Fri, 9 Dec 2016 21:26:58 +0000 Subject: [PATCH 3/3] Add Blowfish tests --- openssl/src/symm.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/openssl/src/symm.rs b/openssl/src/symm.rs index 1a46411d..d2cb0cc8 100644 --- a/openssl/src/symm.rs +++ b/openssl/src/symm.rs @@ -525,6 +525,35 @@ mod tests { } } + fn cipher_test_nopad(ciphertype: super::Cipher, pt: &str, ct: &str, key: &str, iv: &str) { + let pt = Vec::from_hex(pt).unwrap(); + let ct = Vec::from_hex(ct).unwrap(); + let key = Vec::from_hex(key).unwrap(); + let iv = Vec::from_hex(iv).unwrap(); + + let computed = { + let mut c = Crypter::new(ciphertype, Mode::Decrypt, &key, Some(&iv)).unwrap(); + c.pad(false); + let mut out = vec![0; ct.len() + ciphertype.block_size()]; + let count = c.update(&ct, &mut out).unwrap(); + let rest = c.finalize(&mut out[count..]).unwrap(); + out.truncate(count + rest); + out + }; + let expected = pt; + + if computed != expected { + println!("Computed: {}", computed.to_hex()); + println!("Expected: {}", expected.to_hex()); + if computed.len() != expected.len() { + println!("Lengths differ: {} in computed vs {} expected", + computed.len(), + expected.len()); + } + panic!("test failure"); + } + } + #[test] fn test_rc4() { @@ -631,6 +660,51 @@ mod tests { cipher_test(super::Cipher::aes_256_cfb8(), pt, ct, key, iv); } + #[test] + fn test_bf_cbc() { + // https://www.schneier.com/code/vectors.txt + + let pt = "37363534333231204E6F77206973207468652074696D6520666F722000000000"; + let ct = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC"; + let key = "0123456789ABCDEFF0E1D2C3B4A59687"; + let iv = "FEDCBA9876543210"; + + cipher_test_nopad(super::Cipher::bf_cbc(), pt, ct, key, iv); + } + + #[test] + fn test_bf_ecb() { + + let pt = "5CD54CA83DEF57DA"; + let ct = "B1B8CC0B250F09A0"; + let key = "0131D9619DC1376E"; + let iv = "0000000000000000"; + + cipher_test_nopad(super::Cipher::bf_ecb(), pt, ct, key, iv); + } + + #[test] + fn test_bf_cfb64() { + + let pt = "37363534333231204E6F77206973207468652074696D6520666F722000"; + let ct = "E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"; + let key = "0123456789ABCDEFF0E1D2C3B4A59687"; + let iv = "FEDCBA9876543210"; + + cipher_test_nopad(super::Cipher::bf_cfb64(), pt, ct, key, iv); + } + + #[test] + fn test_bf_ofb() { + + let pt = "37363534333231204E6F77206973207468652074696D6520666F722000"; + let ct = "E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA"; + let key = "0123456789ABCDEFF0E1D2C3B4A59687"; + let iv = "FEDCBA9876543210"; + + cipher_test_nopad(super::Cipher::bf_ofb(), pt, ct, key, iv); + } + #[test] fn test_des_cbc() {