Loading .gitignore +1 −1 Original line number Diff line number Diff line Loading @@ -6,4 +6,4 @@ /bin/ /build/ /lib/ /src/crypto/lib /src/crypto/crypto Makefile +1 −1 Original line number Diff line number Diff line Loading @@ -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 src/crypto/hash.rs +37 −47 Original line number Diff line number Diff line Loading @@ -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 { pub fn EVP_MD_CTX_create() -> EVP_MD_CTX; pub fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); fn EVP_MD_CTX_create() -> EVP_MD_CTX; fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); 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; 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_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), } } } Loading @@ -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 } Loading @@ -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) EVP_DigestUpdate(self.ctx, data.as_ptr(), data.len() as c_uint) } }); } /** Loading @@ -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()); } }); 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); } } } Loading @@ -114,7 +105,6 @@ pub fn hash(t: HashType, data: &[u8]) -> ~[u8] { #[cfg(test)] mod tests { use super::*; use hex::FromHex; use hex::ToHex; Loading @@ -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(); Loading Loading @@ -159,7 +149,7 @@ mod tests { HashTest(~"AAED18DBE8938C19ED734A8D", ~"6F80FB775F27E0A4CE5C2F42FC72C5F1")]; for test in tests.iter() { hash_test(MD5, test); hash_test(super::MD5, test); } } Loading @@ -170,7 +160,7 @@ mod tests { ]; for test in tests.iter() { hash_test(SHA1, test); hash_test(super::SHA1, test); } } Loading @@ -181,7 +171,7 @@ mod tests { ]; for test in tests.iter() { hash_test(SHA256, test); hash_test(super::SHA256, test); } } } src/crypto/hex.rs +0 −5 Original line number Diff line number Diff line Loading @@ -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"); } } src/crypto/hmac.rs +20 −25 Original line number Diff line number Diff line Loading @@ -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 { Loading @@ -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(), Loading @@ -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 } Loading @@ -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); let mut outlen = 0; HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen); assert!(self.len == outlen as uint) }); res } } Loading Loading
.gitignore +1 −1 Original line number Diff line number Diff line Loading @@ -6,4 +6,4 @@ /bin/ /build/ /lib/ /src/crypto/lib /src/crypto/crypto
Makefile +1 −1 Original line number Diff line number Diff line Loading @@ -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
src/crypto/hash.rs +37 −47 Original line number Diff line number Diff line Loading @@ -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 { pub fn EVP_MD_CTX_create() -> EVP_MD_CTX; pub fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); fn EVP_MD_CTX_create() -> EVP_MD_CTX; fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); 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; 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_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), } } } Loading @@ -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 } Loading @@ -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) EVP_DigestUpdate(self.ctx, data.as_ptr(), data.len() as c_uint) } }); } /** Loading @@ -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()); } }); 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); } } } Loading @@ -114,7 +105,6 @@ pub fn hash(t: HashType, data: &[u8]) -> ~[u8] { #[cfg(test)] mod tests { use super::*; use hex::FromHex; use hex::ToHex; Loading @@ -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(); Loading Loading @@ -159,7 +149,7 @@ mod tests { HashTest(~"AAED18DBE8938C19ED734A8D", ~"6F80FB775F27E0A4CE5C2F42FC72C5F1")]; for test in tests.iter() { hash_test(MD5, test); hash_test(super::MD5, test); } } Loading @@ -170,7 +160,7 @@ mod tests { ]; for test in tests.iter() { hash_test(SHA1, test); hash_test(super::SHA1, test); } } Loading @@ -181,7 +171,7 @@ mod tests { ]; for test in tests.iter() { hash_test(SHA256, test); hash_test(super::SHA256, test); } } }
src/crypto/hex.rs +0 −5 Original line number Diff line number Diff line Loading @@ -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"); } }
src/crypto/hmac.rs +20 −25 Original line number Diff line number Diff line Loading @@ -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 { Loading @@ -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(), Loading @@ -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 } Loading @@ -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); let mut outlen = 0; HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen); assert!(self.len == outlen as uint) }); res } } Loading