Merge pull request #530 from 0xa/blowfish

Add Blowfish support
This commit is contained in:
Steven Fackler 2016-12-09 14:24:56 -08:00 committed by GitHub
commit 33ca7b6320
2 changed files with 94 additions and 0 deletions

View File

@ -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_cfb64() -> *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;

View File

@ -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_cfb64() -> Cipher {
unsafe { Cipher(ffi::EVP_bf_cfb64()) }
}
pub fn bf_ofb() -> Cipher {
unsafe { Cipher(ffi::EVP_bf_ofb()) }
}
pub fn des_cbc() -> Cipher {
unsafe { Cipher(ffi::EVP_des_cbc()) }
}
@ -509,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() {
@ -615,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() {