From 85e6d1db12e63a24e9afc1d61c604597dc6c91d5 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 27 Dec 2013 22:02:38 -0500 Subject: [PATCH] update to rust 0.9-pre (a5fa1d9) --- .gitignore | 2 +- Makefile | 2 +- src/crypto/hash.rs | 80 +++++++------- src/crypto/hex.rs | 5 - src/crypto/hmac.rs | 45 ++++---- src/crypto/lib.rs | 8 +- src/crypto/pkcs5.rs | 65 +++++------- src/crypto/pkey.rs | 247 ++++++++++++++++++++------------------------ src/crypto/rand.rs | 24 ++--- src/crypto/symm.rs | 145 +++++++++++--------------- 10 files changed, 268 insertions(+), 355 deletions(-) diff --git a/.gitignore b/.gitignore index d67afc9b..f8f57469 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ /bin/ /build/ /lib/ -/src/crypto/lib +/src/crypto/crypto diff --git a/Makefile b/Makefile index 5e385052..51070f9d 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ all: test: $(RUSTC) $(RUST_FLAGS) --test src/crypto/lib.rs - ./src/crypto/lib + ./src/crypto/crypto clean: rm -rf bin/ lib/ build/ src/crypto/lib diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs index 339aafeb..bb75d0ef 100644 --- a/src/crypto/hash.rs +++ b/src/crypto/hash.rs @@ -18,37 +18,32 @@ pub type EVP_MD_CTX = *libc::c_void; #[allow(non_camel_case_types)] pub type EVP_MD = *libc::c_void; -mod libcrypto { - use super::*; - use std::libc::c_uint; +#[link(name = "crypto")] +extern { + fn EVP_MD_CTX_create() -> EVP_MD_CTX; + fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); - #[link(name = "crypto")] - extern { - pub fn EVP_MD_CTX_create() -> EVP_MD_CTX; - pub fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); + fn EVP_md5() -> EVP_MD; + fn EVP_sha1() -> EVP_MD; + fn EVP_sha224() -> EVP_MD; + fn EVP_sha256() -> EVP_MD; + fn EVP_sha384() -> EVP_MD; + fn EVP_sha512() -> EVP_MD; - pub fn EVP_md5() -> EVP_MD; - pub fn EVP_sha1() -> EVP_MD; - pub fn EVP_sha224() -> EVP_MD; - pub fn EVP_sha256() -> EVP_MD; - pub fn EVP_sha384() -> EVP_MD; - pub fn EVP_sha512() -> EVP_MD; - - pub fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD); - pub fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); - pub fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32); - } + fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD); + fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); + fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32); } pub fn evpmd(t: HashType) -> (EVP_MD, uint) { unsafe { match t { - MD5 => (libcrypto::EVP_md5(), 16u), - SHA1 => (libcrypto::EVP_sha1(), 20u), - SHA224 => (libcrypto::EVP_sha224(), 28u), - SHA256 => (libcrypto::EVP_sha256(), 32u), - SHA384 => (libcrypto::EVP_sha384(), 48u), - SHA512 => (libcrypto::EVP_sha512(), 64u), + MD5 => (EVP_md5(), 16u), + SHA1 => (EVP_sha1(), 20u), + SHA224 => (EVP_sha224(), 28u), + SHA256 => (EVP_sha256(), 32u), + SHA384 => (EVP_sha384(), 48u), + SHA512 => (EVP_sha512(), 64u), } } } @@ -61,10 +56,10 @@ pub struct Hasher { impl Hasher { pub fn new(ht: HashType) -> Hasher { - let ctx = unsafe { libcrypto::EVP_MD_CTX_create() }; + let ctx = unsafe { EVP_MD_CTX_create() }; let (evp, mdlen) = evpmd(ht); unsafe { - libcrypto::EVP_DigestInit(ctx, evp); + EVP_DigestInit(ctx, evp); } Hasher { evp: evp, ctx: ctx, len: mdlen } @@ -72,11 +67,9 @@ impl Hasher { /// Update this hasher with more input bytes pub fn update(&self, data: &[u8]) { - data.as_imm_buf(|pdata, len| { - unsafe { - libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) - } - }); + unsafe { + EVP_DigestUpdate(self.ctx, data.as_ptr(), data.len() as c_uint) + } } /** @@ -84,20 +77,18 @@ impl Hasher { * initialization */ pub fn final(&self) -> ~[u8] { - let mut res = vec::from_elem(self.len, 0u8); - res.as_mut_buf(|pres, _len| { - unsafe { - libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); - } - }); - res + unsafe { + let mut res = vec::from_elem(self.len, 0u8); + EVP_DigestFinal(self.ctx, res.as_mut_ptr(), ptr::null()); + res + } } } impl Drop for Hasher { fn drop(&mut self) { unsafe { - libcrypto::EVP_MD_CTX_destroy(self.ctx); + EVP_MD_CTX_destroy(self.ctx); } } } @@ -114,7 +105,6 @@ pub fn hash(t: HashType, data: &[u8]) -> ~[u8] { #[cfg(test)] mod tests { - use super::*; use hex::FromHex; use hex::ToHex; @@ -128,8 +118,8 @@ mod tests { expected_output: output } } - fn hash_test(hashtype: HashType, hashtest: &HashTest) { - let calced_raw = hash(hashtype, hashtest.input); + fn hash_test(hashtype: super::HashType, hashtest: &HashTest) { + let calced_raw = super::hash(hashtype, hashtest.input); let calced = calced_raw.to_hex(); @@ -159,7 +149,7 @@ mod tests { HashTest(~"AAED18DBE8938C19ED734A8D", ~"6F80FB775F27E0A4CE5C2F42FC72C5F1")]; for test in tests.iter() { - hash_test(MD5, test); + hash_test(super::MD5, test); } } @@ -170,7 +160,7 @@ mod tests { ]; for test in tests.iter() { - hash_test(SHA1, test); + hash_test(super::SHA1, test); } } @@ -181,7 +171,7 @@ mod tests { ]; for test in tests.iter() { - hash_test(SHA256, test); + hash_test(super::SHA256, test); } } } diff --git a/src/crypto/hex.rs b/src/crypto/hex.rs index f55dc1a9..1a7ed75b 100644 --- a/src/crypto/hex.rs +++ b/src/crypto/hex.rs @@ -71,17 +71,12 @@ impl<'a> FromHex for &'a str { #[cfg(test)] mod tests { - use super::*; - #[test] pub fn test() { - assert!([05u8, 0xffu8, 0x00u8, 0x59u8].to_hex() == ~"05FF0059"); assert!("00FFA9D1F5".from_hex() == ~[0, 0xff, 0xa9, 0xd1, 0xf5]); assert!("00FFA9D1F5".from_hex().to_hex() == ~"00FFA9D1F5"); } - - } diff --git a/src/crypto/hmac.rs b/src/crypto/hmac.rs index 5bc8b359..5cac7f50 100644 --- a/src/crypto/hmac.rs +++ b/src/crypto/hmac.rs @@ -14,26 +14,26 @@ * limitations under the License. */ -use hash::*; -use std::{libc,ptr,vec}; +use std::libc::{c_uchar, c_int, c_uint}; +use std::ptr; +use std::vec; +use hash; #[allow(non_camel_case_types)] pub struct HMAC_CTX { - md: EVP_MD, - md_ctx: EVP_MD_CTX, - i_ctx: EVP_MD_CTX, - o_ctx: EVP_MD_CTX, - key_length: libc::c_uint, - key: [libc::c_uchar, ..128] + md: hash::EVP_MD, + md_ctx: hash::EVP_MD_CTX, + i_ctx: hash::EVP_MD_CTX, + o_ctx: hash::EVP_MD_CTX, + key_length: c_uint, + key: [c_uchar, ..128] } #[link(name = "crypto")] extern { - fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD); - - fn HMAC_Update(ctx: *mut HMAC_CTX, input: *u8, len: libc::c_uint); - - fn HMAC_Final(ctx: *mut HMAC_CTX, output: *mut u8, len: *mut libc::c_uint); + fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: c_int, md: hash::EVP_MD); + fn HMAC_Update(ctx: *mut HMAC_CTX, input: *u8, len: c_uint); + fn HMAC_Final(ctx: *mut HMAC_CTX, output: *mut u8, len: *mut c_uint); } pub struct HMAC { @@ -41,10 +41,9 @@ pub struct HMAC { priv len: uint, } -pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { +pub fn HMAC(ht: hash::HashType, key: ~[u8]) -> HMAC { unsafe { - - let (evp, mdlen) = evpmd(ht); + let (evp, mdlen) = hash::evpmd(ht); let mut ctx : HMAC_CTX = HMAC_CTX { md: ptr::null(), @@ -57,7 +56,7 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { HMAC_CTX_init(&mut ctx, key.as_ptr(), - key.len() as libc::c_int, + key.len() as c_int, evp); HMAC { ctx: ctx, len: mdlen } @@ -67,20 +66,16 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { impl HMAC { pub fn update(&mut self, data: &[u8]) { unsafe { - data.as_imm_buf(|pdata, len| { - HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) - }); + HMAC_Update(&mut self.ctx, data.as_ptr(), data.len() as c_uint) } } pub fn final(&mut self) -> ~[u8] { unsafe { let mut res = vec::from_elem(self.len, 0u8); - let mut outlen: libc::c_uint = 0; - res.as_mut_buf(|pres, _len| { - HMAC_Final(&mut self.ctx, pres, &mut outlen); - assert!(self.len == outlen as uint) - }); + let mut outlen = 0; + HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen); + assert!(self.len == outlen as uint) res } } diff --git a/src/crypto/lib.rs b/src/crypto/lib.rs index 7ed4bfd9..f1038450 100644 --- a/src/crypto/lib.rs +++ b/src/crypto/lib.rs @@ -15,13 +15,7 @@ * limitations under the License. */ -#[link(name = "crypto", - package_id = "crypto", - vers = "0.3", - uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")]; -#[crate_type = "lib"]; - -#[feature(globs)]; +#[crate_id = "crypto#0.3"]; pub mod hash; pub mod hex; diff --git a/src/crypto/pkcs5.rs b/src/crypto/pkcs5.rs index 9dfc026c..4dec03f6 100644 --- a/src/crypto/pkcs5.rs +++ b/src/crypto/pkcs5.rs @@ -1,57 +1,44 @@ use std::libc::c_int; use std::vec; -mod libcrypto { - use std::libc::c_int; - - #[link(name = "crypto")] - extern { - pub fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int, - salt: *u8, saltlen: c_int, - iter: c_int, keylen: c_int, - out: *mut u8) -> c_int; - } +#[link(name = "crypto")] +extern { + fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int, + salt: *u8, saltlen: c_int, + iter: c_int, keylen: c_int, + out: *mut u8) -> c_int; } /// Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm. -pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, - keylen: uint) -> ~[u8] { - assert!(iter >= 1u); - assert!(keylen >= 1u); +pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> ~[u8] { + unsafe { + assert!(iter >= 1); + assert!(keylen >= 1); - pass.as_imm_buf(|pass_buf, pass_len| { - salt.as_imm_buf(|salt_buf, salt_len| { - let mut out = vec::with_capacity(keylen); + let mut out = vec::with_capacity(keylen); - out.as_mut_buf(|out_buf, _out_len| { - let r = unsafe { - libcrypto::PKCS5_PBKDF2_HMAC_SHA1( - pass_buf, pass_len as c_int, - salt_buf, salt_len as c_int, - iter as c_int, keylen as c_int, - out_buf) - }; + let r = PKCS5_PBKDF2_HMAC_SHA1( + pass.as_ptr(), pass.len() as c_int, + salt.as_ptr(), salt.len() as c_int, + iter as c_int, keylen as c_int, + out.as_mut_ptr()); - if r != 1 as c_int { fail!(); } - }); + if r != 1 { fail!(); } - unsafe { out.set_len(keylen); } + out.set_len(keylen); - out - }) - }) + out + } } #[cfg(test)] mod tests { - use super::*; - // Test vectors from // http://tools.ietf.org/html/draft-josefsson-pbkdf2-test-vectors-06 #[test] fn test_pbkdf2_hmac_sha1() { assert_eq!( - pbkdf2_hmac_sha1( + super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), 1u, @@ -65,7 +52,7 @@ mod tests { ); assert_eq!( - pbkdf2_hmac_sha1( + super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), 2u, @@ -79,7 +66,7 @@ mod tests { ); assert_eq!( - pbkdf2_hmac_sha1( + super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), 4096u, @@ -93,7 +80,7 @@ mod tests { ); assert_eq!( - pbkdf2_hmac_sha1( + super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), 16777216u, @@ -107,7 +94,7 @@ mod tests { ); assert_eq!( - pbkdf2_hmac_sha1( + super::pbkdf2_hmac_sha1( "passwordPASSWORDpassword", "saltSALTsaltSALTsaltSALTsaltSALTsalt".as_bytes(), 4096u, @@ -122,7 +109,7 @@ mod tests { ); assert_eq!( - pbkdf2_hmac_sha1( + super::pbkdf2_hmac_sha1( "pass\x00word", "sa\x00lt".as_bytes(), 4096u, diff --git a/src/crypto/pkey.rs b/src/crypto/pkey.rs index 7b5245b2..b8206d4e 100644 --- a/src/crypto/pkey.rs +++ b/src/crypto/pkey.rs @@ -1,5 +1,5 @@ use std::cast; -use std::libc::{c_int, c_uint}; +use std::libc::{c_char, c_int, c_uint}; use std::libc; use std::ptr; use std::vec; @@ -11,34 +11,29 @@ pub type EVP_PKEY = *libc::c_void; #[allow(non_camel_case_types)] pub type RSA = *libc::c_void; -mod libcrypto { - use super::*; - use std::libc::{c_char, c_int, c_uint}; +#[link(name = "crypto")] +extern { + fn EVP_PKEY_new() -> *EVP_PKEY; + fn EVP_PKEY_free(k: *EVP_PKEY); + fn EVP_PKEY_assign(pkey: *EVP_PKEY, typ: c_int, key: *c_char) -> c_int; + fn EVP_PKEY_get1_RSA(k: *EVP_PKEY) -> *RSA; - #[link(name = "crypto")] - extern { - pub fn EVP_PKEY_new() -> *EVP_PKEY; - pub fn EVP_PKEY_free(k: *EVP_PKEY); - pub fn EVP_PKEY_assign(pkey: *EVP_PKEY, typ: c_int, key: *c_char) -> c_int; - pub fn EVP_PKEY_get1_RSA(k: *EVP_PKEY) -> *RSA; + fn i2d_PublicKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; + fn d2i_PublicKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; + fn i2d_PrivateKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; + fn d2i_PrivateKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; - pub fn i2d_PublicKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; - pub fn d2i_PublicKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; - pub fn i2d_PrivateKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; - pub fn d2i_PrivateKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; + fn RSA_generate_key(modsz: c_uint, e: c_uint, cb: *u8, cbarg: *u8) -> *RSA; + fn RSA_size(k: *RSA) -> c_uint; - pub fn RSA_generate_key(modsz: c_uint, e: c_uint, cb: *u8, cbarg: *u8) -> *RSA; - pub fn RSA_size(k: *RSA) -> c_uint; - - pub fn RSA_public_encrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, - pad: c_int) -> c_int; - pub fn RSA_private_decrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, - pad: c_int) -> c_int; - pub fn RSA_sign(t: c_int, m: *u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint, - k: *RSA) -> c_int; - pub fn RSA_verify(t: c_int, m: *u8, mlen: c_uint, sig: *u8, siglen: c_uint, - k: *RSA) -> c_int; - } + fn RSA_public_encrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, + pad: c_int) -> c_int; + fn RSA_private_decrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, + pad: c_int) -> c_int; + fn RSA_sign(t: c_int, m: *u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint, + k: *RSA) -> c_int; + fn RSA_verify(t: c_int, m: *u8, mlen: c_uint, sig: *u8, siglen: c_uint, + k: *RSA) -> c_int; } enum Parts { @@ -87,9 +82,11 @@ pub struct PKey { /// Represents a public key, optionally with a private key attached. impl PKey { pub fn new() -> PKey { - PKey { - evp: unsafe { libcrypto::EVP_PKEY_new() }, - parts: Neither, + unsafe { + PKey { + evp: EVP_PKEY_new(), + parts: Neither, + } } } @@ -99,9 +96,7 @@ impl PKey { if len < 0 as c_int { return ~[]; } let mut s = vec::from_elem(len as uint, 0u8); - let r = s.as_mut_buf(|buf, _| { - f(self.evp, &buf) - }); + let r = f(self.evp, &s.as_mut_ptr()); s.truncate(r as uint); s @@ -109,18 +104,16 @@ impl PKey { } fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) { - s.as_imm_buf(|ps, len| { + unsafe { let evp = ptr::null(); - unsafe { - f(6 as c_int, &evp, &ps, len as c_uint); - } + f(6 as c_int, &evp, &s.as_ptr(), s.len() as c_uint); self.evp = evp; - }); + } } pub fn gen(&mut self, keysz: uint) { unsafe { - let rsa = libcrypto::RSA_generate_key( + let rsa = RSA_generate_key( keysz as c_uint, 65537u as c_uint, ptr::null(), @@ -128,7 +121,7 @@ impl PKey { ); // XXX: 6 == NID_rsaEncryption - libcrypto::EVP_PKEY_assign( + EVP_PKEY_assign( self.evp, 6 as c_int, cast::transmute(rsa)); @@ -141,14 +134,14 @@ impl PKey { * Returns a serialized form of the public key, suitable for load_pub(). */ pub fn save_pub(&self) -> ~[u8] { - self._tostr(libcrypto::i2d_PublicKey) + self._tostr(i2d_PublicKey) } /** * Loads a serialized form of the public key, as produced by save_pub(). */ pub fn load_pub(&mut self, s: &[u8]) { - self._fromstr(s, libcrypto::d2i_PublicKey); + self._fromstr(s, d2i_PublicKey); self.parts = Public; } @@ -157,14 +150,14 @@ impl PKey { * load_priv(). */ pub fn save_priv(&self) -> ~[u8] { - self._tostr(libcrypto::i2d_PrivateKey) + self._tostr(i2d_PrivateKey) } /** * Loads a serialized form of the public and private keys, as produced by * save_priv(). */ pub fn load_priv(&mut self, s: &[u8]) { - self._fromstr(s, libcrypto::d2i_PrivateKey); + self._fromstr(s, d2i_PrivateKey); self.parts = Both; } @@ -173,7 +166,7 @@ impl PKey { */ pub fn size(&self) -> uint { unsafe { - libcrypto::RSA_size(libcrypto::EVP_PKEY_get1_RSA(self.evp)) as uint + RSA_size(EVP_PKEY_get1_RSA(self.evp)) as uint } } @@ -211,8 +204,8 @@ impl PKey { */ pub fn max_data(&self) -> uint { unsafe { - let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); - let len = libcrypto::RSA_size(rsa); + let rsa = EVP_PKEY_get1_RSA(self.evp); + let len = RSA_size(rsa); // 41 comes from RSA_public_encrypt(3) for OAEP len as uint - 41u @@ -221,24 +214,20 @@ impl PKey { pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { unsafe { - let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); - let len = libcrypto::RSA_size(rsa); + let rsa = EVP_PKEY_get1_RSA(self.evp); + let len = RSA_size(rsa); assert!(s.len() < self.max_data()); let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = r.as_mut_buf(|pr, _len| { - s.as_imm_buf(|ps, s_len| { - libcrypto::RSA_public_encrypt( - s_len as c_uint, - ps, - pr, - rsa, - openssl_padding_code(padding) - ) - }) - }); + let rv = RSA_public_encrypt( + s.len() as c_uint, + s.as_ptr(), + r.as_mut_ptr(), + rsa, + openssl_padding_code(padding)); + if rv < 0 as c_int { ~[] } else { @@ -250,24 +239,19 @@ impl PKey { pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { unsafe { - let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); - let len = libcrypto::RSA_size(rsa); + let rsa = EVP_PKEY_get1_RSA(self.evp); + let len = RSA_size(rsa); - assert_eq!(s.len() as c_uint, libcrypto::RSA_size(rsa)); + assert_eq!(s.len() as c_uint, RSA_size(rsa)); let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = r.as_mut_buf(|pr, _len| { - s.as_imm_buf(|ps, s_len| { - libcrypto::RSA_private_decrypt( - s_len as c_uint, - ps, - pr, - rsa, - openssl_padding_code(padding) - ) - }) - }); + let rv = RSA_private_decrypt( + s.len() as c_uint, + s.as_ptr(), + r.as_mut_ptr(), + rsa, + openssl_padding_code(padding)); if rv < 0 as c_int { ~[] @@ -303,21 +287,17 @@ impl PKey { pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] { unsafe { - let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); - let mut len = libcrypto::RSA_size(rsa); + let rsa = EVP_PKEY_get1_RSA(self.evp); + let mut len = RSA_size(rsa); let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = r.as_mut_buf(|pr, _len| { - s.as_imm_buf(|ps, s_len| { - libcrypto::RSA_sign( - openssl_hash_nid(hash), - ps, - s_len as c_uint, - pr, - &mut len, - rsa) - }) - }); + let rv = RSA_sign( + openssl_hash_nid(hash), + s.as_ptr(), + s.len() as c_uint, + r.as_mut_ptr(), + &mut len, + rsa); if rv < 0 as c_int { ~[] @@ -330,22 +310,18 @@ impl PKey { pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool { unsafe { - let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); + let rsa = EVP_PKEY_get1_RSA(self.evp); - m.as_imm_buf(|pm, m_len| { - s.as_imm_buf(|ps, s_len| { - let rv = libcrypto::RSA_verify( - openssl_hash_nid(hash), - pm, - m_len as c_uint, - ps, - s_len as c_uint, - rsa - ); + let rv = RSA_verify( + openssl_hash_nid(hash), + m.as_ptr(), + m.len() as c_uint, + s.as_ptr(), + s.len() as c_uint, + rsa + ); - rv == 1 as c_int - }) - }) + rv == 1 as c_int } } } @@ -353,56 +329,55 @@ impl PKey { impl Drop for PKey { fn drop(&mut self) { unsafe { - libcrypto::EVP_PKEY_free(self.evp); + EVP_PKEY_free(self.evp); } } } #[cfg(test)] mod tests { - use super::*; use hash::{MD5, SHA1}; #[test] fn test_gen_pub() { - let mut k0 = PKey::new(); - let mut k1 = PKey::new(); + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); k0.gen(512u); k1.load_pub(k0.save_pub()); - assert!(k0.save_pub() == k1.save_pub()); - assert!(k0.size() == k1.size()); - assert!(k0.can(Encrypt)); - assert!(k0.can(Decrypt)); - assert!(k0.can(Verify)); - assert!(k0.can(Sign)); - assert!(k1.can(Encrypt)); - assert!(!k1.can(Decrypt)); - assert!(k1.can(Verify)); - assert!(!k1.can(Sign)); + assert_eq!(k0.save_pub(), k1.save_pub()); + assert_eq!(k0.size(), k1.size()); + assert!(k0.can(super::Encrypt)); + assert!(k0.can(super::Decrypt)); + assert!(k0.can(super::Verify)); + assert!(k0.can(super::Sign)); + assert!(k1.can(super::Encrypt)); + assert!(!k1.can(super::Decrypt)); + assert!(k1.can(super::Verify)); + assert!(!k1.can(super::Sign)); } #[test] fn test_gen_priv() { - let mut k0 = PKey::new(); - let mut k1 = PKey::new(); + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); k0.gen(512u); k1.load_priv(k0.save_priv()); - assert!(k0.save_priv() == k1.save_priv()); - assert!(k0.size() == k1.size()); - assert!(k0.can(Encrypt)); - assert!(k0.can(Decrypt)); - assert!(k0.can(Verify)); - assert!(k0.can(Sign)); - assert!(k1.can(Encrypt)); - assert!(k1.can(Decrypt)); - assert!(k1.can(Verify)); - assert!(k1.can(Sign)); + assert_eq!(k0.save_priv(), k1.save_priv()); + assert_eq!(k0.size(), k1.size()); + assert!(k0.can(super::Encrypt)); + assert!(k0.can(super::Decrypt)); + assert!(k0.can(super::Verify)); + assert!(k0.can(super::Sign)); + assert!(k1.can(super::Encrypt)); + assert!(k1.can(super::Decrypt)); + assert!(k1.can(super::Verify)); + assert!(k1.can(super::Sign)); } #[test] fn test_encrypt() { - let mut k0 = PKey::new(); - let mut k1 = PKey::new(); + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; k0.gen(512u); k1.load_pub(k0.save_pub()); @@ -413,20 +388,20 @@ mod tests { #[test] fn test_encrypt_pkcs() { - let mut k0 = PKey::new(); - let mut k1 = PKey::new(); + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; k0.gen(512u); k1.load_pub(k0.save_pub()); - let emsg = k1.encrypt_with_padding(msg, PKCS1v15); - let dmsg = k0.decrypt_with_padding(emsg, PKCS1v15); + let emsg = k1.encrypt_with_padding(msg, super::PKCS1v15); + let dmsg = k0.decrypt_with_padding(emsg, super::PKCS1v15); assert!(msg == dmsg); } #[test] fn test_sign() { - let mut k0 = PKey::new(); - let mut k1 = PKey::new(); + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; k0.gen(512u); k1.load_pub(k0.save_pub()); @@ -437,8 +412,8 @@ mod tests { #[test] fn test_sign_hashes() { - let mut k0 = PKey::new(); - let mut k1 = PKey::new(); + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; k0.gen(512u); k1.load_pub(k0.save_pub()); diff --git a/src/crypto/rand.rs b/src/crypto/rand.rs index 6510b9f4..4e95046b 100644 --- a/src/crypto/rand.rs +++ b/src/crypto/rand.rs @@ -1,31 +1,27 @@ use std::libc::c_int; use std::vec; -mod libcrypto { - use std::libc::c_int; - - #[link(name = "crypto")] - extern { - pub fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; - } +#[link(name = "crypto")] +extern { + fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; } pub fn rand_bytes(len: uint) -> ~[u8] { - let mut out = vec::with_capacity(len); + unsafe { + let mut out = vec::with_capacity(len); - out.as_mut_buf(|out_buf, len| { - let r = unsafe { libcrypto::RAND_bytes(out_buf, len as c_int) }; + let r = RAND_bytes(out.as_mut_ptr(), len as c_int); if r != 1 as c_int { fail!() } - }); - unsafe { out.set_len(len); } + out.set_len(len); - out + out + } } #[cfg(test)] mod tests { - use super::*; + use super::rand_bytes; #[test] fn test_rand_bytes() { diff --git a/src/crypto/symm.rs b/src/crypto/symm.rs index 3d31bde0..d3c54d27 100644 --- a/src/crypto/symm.rs +++ b/src/crypto/symm.rs @@ -1,4 +1,4 @@ -use std::libc::c_int; +use std::libc::{c_int, c_uint}; use std::libc; use std::vec; @@ -8,34 +8,29 @@ pub type EVP_CIPHER_CTX = *libc::c_void; #[allow(non_camel_case_types)] pub type EVP_CIPHER = *libc::c_void; -mod libcrypto { - use super::*; - use std::libc::{c_int, c_uint}; +#[link(name = "crypto")] +extern { + fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX; + fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int); + fn EVP_CIPHER_CTX_free(ctx: EVP_CIPHER_CTX); - extern { - #[link_args = "-lcrypto"] - pub fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX; - pub fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int); - pub fn EVP_CIPHER_CTX_free(ctx: EVP_CIPHER_CTX); + fn EVP_aes_128_ecb() -> EVP_CIPHER; + fn EVP_aes_128_cbc() -> EVP_CIPHER; + // fn EVP_aes_128_ctr() -> EVP_CIPHER; + // fn EVP_aes_128_gcm() -> EVP_CIPHER; - pub fn EVP_aes_128_ecb() -> EVP_CIPHER; - pub fn EVP_aes_128_cbc() -> EVP_CIPHER; - // pub fn EVP_aes_128_ctr() -> EVP_CIPHER; - // pub fn EVP_aes_128_gcm() -> EVP_CIPHER; + fn EVP_aes_256_ecb() -> EVP_CIPHER; + fn EVP_aes_256_cbc() -> EVP_CIPHER; + // fn EVP_aes_256_ctr() -> EVP_CIPHER; + // fn EVP_aes_256_gcm() -> EVP_CIPHER; - pub fn EVP_aes_256_ecb() -> EVP_CIPHER; - pub fn EVP_aes_256_cbc() -> EVP_CIPHER; - // pub fn EVP_aes_256_ctr() -> EVP_CIPHER; - // pub fn EVP_aes_256_gcm() -> EVP_CIPHER; + fn EVP_rc4() -> EVP_CIPHER; - pub fn EVP_rc4() -> EVP_CIPHER; - - pub fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER, - key: *u8, iv: *u8, mode: c_int); - pub fn EVP_CipherUpdate(ctx: EVP_CIPHER_CTX, outbuf: *mut u8, - outlen: &mut c_uint, inbuf: *u8, inlen: c_int); - pub fn EVP_CipherFinal(ctx: EVP_CIPHER_CTX, res: *mut u8, len: &mut c_int); - } + fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER, + key: *u8, iv: *u8, mode: c_int); + fn EVP_CipherUpdate(ctx: EVP_CIPHER_CTX, outbuf: *mut u8, + outlen: &mut c_uint, inbuf: *u8, inlen: c_int); + fn EVP_CipherFinal(ctx: EVP_CIPHER_CTX, res: *mut u8, len: &mut c_int); } pub enum Mode { @@ -61,17 +56,17 @@ pub enum Type { fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) { unsafe { match t { - AES_128_ECB => (libcrypto::EVP_aes_128_ecb(), 16u, 16u), - AES_128_CBC => (libcrypto::EVP_aes_128_cbc(), 16u, 16u), - // AES_128_CTR => (libcrypto::EVP_aes_128_ctr(), 16u, 0u), - //AES_128_GCM => (libcrypto::EVP_aes_128_gcm(), 16u, 16u), + AES_128_ECB => (EVP_aes_128_ecb(), 16u, 16u), + AES_128_CBC => (EVP_aes_128_cbc(), 16u, 16u), + // AES_128_CTR => (EVP_aes_128_ctr(), 16u, 0u), + //AES_128_GCM => (EVP_aes_128_gcm(), 16u, 16u), - AES_256_ECB => (libcrypto::EVP_aes_256_ecb(), 32u, 16u), - AES_256_CBC => (libcrypto::EVP_aes_256_cbc(), 32u, 16u), - // AES_256_CTR => (libcrypto::EVP_aes_256_ctr(), 32u, 0u), - //AES_256_GCM => (libcrypto::EVP_aes_256_gcm(), 32u, 16u), + AES_256_ECB => (EVP_aes_256_ecb(), 32u, 16u), + AES_256_CBC => (EVP_aes_256_cbc(), 32u, 16u), + // AES_256_CTR => (EVP_aes_256_ctr(), 32u, 0u), + //AES_256_GCM => (EVP_aes_256_gcm(), 32u, 16u), - RC4_128 => (libcrypto::EVP_rc4(), 16u, 0u), + RC4_128 => (EVP_rc4(), 16u, 0u), } } } @@ -86,7 +81,7 @@ pub struct Crypter { impl Crypter { pub fn new(t: Type) -> Crypter { - let ctx = unsafe { libcrypto::EVP_CIPHER_CTX_new() }; + let ctx = unsafe { EVP_CIPHER_CTX_new() }; let (evp, keylen, blocksz) = evpc(t); Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz } } @@ -99,7 +94,7 @@ impl Crypter { if self.blocksize > 0 { unsafe { let v = if padding { 1 } else { 0 } as c_int; - libcrypto::EVP_CIPHER_CTX_set_padding(self.ctx, v); + EVP_CIPHER_CTX_set_padding(self.ctx, v); } } } @@ -115,17 +110,13 @@ impl Crypter { }; assert_eq!(key.len(), self.keylen); - key.as_imm_buf(|pkey, _len| { - iv.as_imm_buf(|piv, _len| { - libcrypto::EVP_CipherInit( - self.ctx, - self.evp, - pkey, - piv, - mode - ) - }); - }); + EVP_CipherInit( + self.ctx, + self.evp, + key.as_ptr(), + iv.as_ptr(), + mode + ) } } @@ -135,26 +126,19 @@ impl Crypter { */ pub fn update(&self, data: &[u8]) -> ~[u8] { unsafe { - data.as_imm_buf(|pdata, len| { - let mut res = vec::from_elem(len + self.blocksize, 0u8); + let mut res = vec::from_elem(data.len() + self.blocksize, 0u8); + let mut reslen = (data.len() + self.blocksize) as u32; - let reslen = res.as_mut_buf(|pres, _len| { - let mut reslen = (len + self.blocksize) as u32; + EVP_CipherUpdate( + self.ctx, + res.as_mut_ptr(), + &mut reslen, + data.as_ptr(), + data.len() as c_int + ); - libcrypto::EVP_CipherUpdate( - self.ctx, - pres, - &mut reslen, - pdata, - len as c_int - ); - - reslen - }); - - res.truncate(reslen as uint); - res - }) + res.truncate(reslen as uint); + res } } @@ -164,12 +148,11 @@ impl Crypter { pub fn final(&self) -> ~[u8] { unsafe { let mut res = vec::from_elem(self.blocksize, 0u8); + let mut reslen = self.blocksize as c_int; - let reslen = res.as_mut_buf(|pres, _len| { - let mut reslen = self.blocksize as c_int; - libcrypto::EVP_CipherFinal(self.ctx, pres, &mut reslen); - reslen - }); + EVP_CipherFinal(self.ctx, + res.as_mut_ptr(), + &mut reslen); res.truncate(reslen as uint); res @@ -180,7 +163,7 @@ impl Crypter { impl Drop for Crypter { fn drop(&mut self) { unsafe { - libcrypto::EVP_CIPHER_CTX_free(self.ctx); + EVP_CIPHER_CTX_free(self.ctx); } } } @@ -211,8 +194,6 @@ pub fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] { #[cfg(test)] mod tests { - use super::*; - use hex::FromHex; // Test vectors from FIPS-197: @@ -230,22 +211,22 @@ mod tests { let c0 = ~[ 0x8eu8, 0xa2u8, 0xb7u8, 0xcau8, 0x51u8, 0x67u8, 0x45u8, 0xbfu8, 0xeau8, 0xfcu8, 0x49u8, 0x90u8, 0x4bu8, 0x49u8, 0x60u8, 0x89u8 ]; - let c = Crypter::new(AES_256_ECB); - c.init(Encrypt, k0, []); + let c = super::Crypter::new(super::AES_256_ECB); + c.init(super::Encrypt, k0, []); c.pad(false); let r0 = c.update(p0) + c.final(); assert!(r0 == c0); - c.init(Decrypt, k0, []); + c.init(super::Decrypt, k0, []); c.pad(false); let p1 = c.update(r0) + c.final(); assert!(p1 == p0); } - fn cipher_test(ciphertype: Type, pt: ~str, ct: ~str, key: ~str, iv: ~str) { + fn cipher_test(ciphertype: super::Type, pt: ~str, ct: ~str, key: ~str, iv: ~str) { use hex::ToHex; - let cipher = Crypter::new(ciphertype); - cipher.init(Encrypt, key.from_hex(), iv.from_hex()); + let cipher = super::Crypter::new(ciphertype); + cipher.init(super::Encrypt, key.from_hex(), iv.from_hex()); let expected = ct.from_hex(); let computed = cipher.update(pt.from_hex()) + cipher.final(); @@ -269,7 +250,7 @@ mod tests { let key = ~"97CD440324DA5FD1F7955C1C13B6B466"; let iv = ~""; - cipher_test(RC4_128, pt, ct, key, iv); + cipher_test(super::RC4_128, pt, ct, key, iv); } /*#[test] @@ -280,7 +261,7 @@ mod tests { let key = ~"2B7E151628AED2A6ABF7158809CF4F3C"; let iv = ~"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"; - cipher_test(AES_128_CTR, pt, ct, key, iv); + cipher_test(super::AES_128_CTR, pt, ct, key, iv); }*/ /*#[test] @@ -291,6 +272,6 @@ mod tests { let key = ~"feffe9928665731c6d6a8f9467308308"; let iv = ~"cafebabefacedbaddecaf888"; - cipher_test(AES_128_GCM, pt, ct, key, iv); + cipher_test(super::AES_128_GCM, pt, ct, key, iv); }*/ }