diff --git a/crypto.rs b/crypto.rs index 1686a715..7ed4bfd9 100644 --- a/crypto.rs +++ b/crypto.rs @@ -16,10 +16,13 @@ */ #[link(name = "crypto", + package_id = "crypto", vers = "0.3", uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")]; #[crate_type = "lib"]; +#[feature(globs)]; + pub mod hash; pub mod hex; pub mod hmac; diff --git a/hash.rs b/hash.rs index 2a43cbd2..339aafeb 100644 --- a/hash.rs +++ b/hash.rs @@ -22,21 +22,21 @@ mod libcrypto { use super::*; use std::libc::c_uint; - #[link_args = "-lcrypto"] + #[link(name = "crypto")] extern { - fn EVP_MD_CTX_create() -> EVP_MD_CTX; - fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); + 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; - 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 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); } } @@ -72,11 +72,11 @@ impl Hasher { /// Update this hasher with more input bytes pub fn update(&self, data: &[u8]) { - do data.as_imm_buf |pdata, len| { + data.as_imm_buf(|pdata, len| { unsafe { libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) } - } + }); } /** @@ -85,17 +85,17 @@ impl Hasher { */ pub fn final(&self) -> ~[u8] { let mut res = vec::from_elem(self.len, 0u8); - do res.as_mut_buf |pres, _len| { + res.as_mut_buf(|pres, _len| { unsafe { libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); } - } + }); res } } impl Drop for Hasher { - fn drop(&self) { + fn drop(&mut self) { unsafe { libcrypto::EVP_MD_CTX_destroy(self.ctx); } @@ -134,7 +134,7 @@ mod tests { let calced = calced_raw.to_hex(); if calced != hashtest.expected_output { - println(fmt!("Test failed - %s != %s", calced, hashtest.expected_output)); + println!("Test failed - {} != {}", calced, hashtest.expected_output); } assert!(calced == hashtest.expected_output); diff --git a/hex.rs b/hex.rs index b479ea18..f55dc1a9 100644 --- a/hex.rs +++ b/hex.rs @@ -20,10 +20,10 @@ pub trait ToHex { fn to_hex(&self) -> ~str; } -impl<'self> ToHex for &'self [u8] { +impl<'a> ToHex for &'a [u8] { fn to_hex(&self) -> ~str { - let chars = "0123456789ABCDEF".iter().collect::<~[char]>(); + let chars = "0123456789ABCDEF".chars().collect::<~[char]>(); let mut s = ~""; @@ -46,11 +46,11 @@ pub trait FromHex { fn from_hex(&self) -> ~[u8]; } -impl<'self> FromHex for &'self str { +impl<'a> FromHex for &'a str { fn from_hex(&self) -> ~[u8] { let mut vec = vec::with_capacity(self.len() / 2); - for (i,c) in self.iter().enumerate() { + for (i,c) in self.chars().enumerate() { let nibble = if c >= '0' && c <= '9' { (c as u8) - 0x30 } else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) } diff --git a/hmac.rs b/hmac.rs index 1e71ed1b..c7cad6a2 100644 --- a/hmac.rs +++ b/hmac.rs @@ -27,8 +27,7 @@ pub struct HMAC_CTX { key: [libc::c_uchar, ..128] } -#[link_args = "-lcrypto"] -#[abi = "cdecl"] +#[link(name = "crypto")] extern { fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD); @@ -68,9 +67,9 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { impl HMAC { pub fn update(&mut self, data: &[u8]) { unsafe { - do data.as_imm_buf |pdata, len| { + data.as_imm_buf(|pdata, len| { HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) - } + }); } } @@ -78,19 +77,11 @@ impl HMAC { unsafe { let mut res = vec::from_elem(self.len, 0u8); let mut outlen: libc::c_uint = 0; - do res.as_mut_buf |pres, _len| { + res.as_mut_buf(|pres, _len| { HMAC_Final(&mut self.ctx, pres, &mut outlen); assert!(self.len == outlen as uint) - } + }); res } } } - -fn main() { - let mut h = HMAC(SHA512, ~[00u8]); - - h.update([00u8]); - - println(fmt!("%?", h.final())) -} diff --git a/pkcs5.rs b/pkcs5.rs index 400d3207..b7c83a3f 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -4,28 +4,26 @@ use std::vec; mod libcrypto { use std::libc::c_int; - #[link_args = "-lcrypto"] + #[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; + 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; } } -#[doc = " -Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm. -"] +/// 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); - do pass.as_imm_buf |pass_buf, pass_len| { - do salt.as_imm_buf |salt_buf, salt_len| { + pass.as_imm_buf(|pass_buf, pass_len| { + salt.as_imm_buf(|salt_buf, salt_len| { let mut out = vec::with_capacity(keylen); - do out.as_mut_buf |out_buf, _out_len| { + out.as_mut_buf(|out_buf, _out_len| { let r = unsafe { libcrypto::PKCS5_PBKDF2_HMAC_SHA1( pass_buf, pass_len as c_int, @@ -35,13 +33,13 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, }; if r != 1 as c_int { fail!(); } - } + }); unsafe { vec::raw::set_len(&mut out, keylen); } out - } - } + }) + }) } #[cfg(test)] diff --git a/pkey.rs b/pkey.rs index 9b97721b..7b5245b2 100644 --- a/pkey.rs +++ b/pkey.rs @@ -15,29 +15,29 @@ mod libcrypto { use super::*; use std::libc::{c_char, c_int, c_uint}; - #[link_args = "-lcrypto"] + #[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; + 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; - 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; + 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; } } @@ -47,7 +47,7 @@ enum Parts { Both } -#[doc = "Represents a role an asymmetric key might be appropriate for."] +/// Represents a role an asymmetric key might be appropriate for. pub enum Role { Encrypt, Decrypt, @@ -55,7 +55,7 @@ pub enum Role { Verify } -#[doc = "Type of encryption padding to use."] +/// Type of encryption padding to use. pub enum EncryptionPadding { OAEP, PKCS1v15 @@ -84,7 +84,7 @@ pub struct PKey { priv parts: Parts, } -///Represents a public key, optionally with a private key attached. +/// Represents a public key, optionally with a private key attached. impl PKey { pub fn new() -> PKey { PKey { @@ -99,9 +99,9 @@ impl PKey { if len < 0 as c_int { return ~[]; } let mut s = vec::from_elem(len as uint, 0u8); - let r = do s.as_mut_buf |buf, _| { + let r = s.as_mut_buf(|buf, _| { f(self.evp, &buf) - }; + }); s.truncate(r as uint); s @@ -109,13 +109,13 @@ impl PKey { } fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) { - do s.as_imm_buf |ps, len| { + s.as_imm_buf(|ps, len| { let evp = ptr::null(); unsafe { f(6 as c_int, &evp, &ps, len as c_uint); } self.evp = evp; - } + }); } pub fn gen(&mut self, keysz: uint) { @@ -228,8 +228,8 @@ impl PKey { let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = do r.as_mut_buf |pr, _len| { - do s.as_imm_buf |ps, s_len| { + 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, @@ -237,8 +237,8 @@ impl PKey { rsa, openssl_padding_code(padding) ) - } - }; + }) + }); if rv < 0 as c_int { ~[] } else { @@ -257,8 +257,8 @@ impl PKey { let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = do r.as_mut_buf |pr, _len| { - do s.as_imm_buf |ps, s_len| { + 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, @@ -266,8 +266,8 @@ impl PKey { rsa, openssl_padding_code(padding) ) - } - }; + }) + }); if rv < 0 as c_int { ~[] @@ -307,8 +307,8 @@ impl PKey { let mut len = libcrypto::RSA_size(rsa); let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = do r.as_mut_buf |pr, _len| { - do s.as_imm_buf |ps, s_len| { + let rv = r.as_mut_buf(|pr, _len| { + s.as_imm_buf(|ps, s_len| { libcrypto::RSA_sign( openssl_hash_nid(hash), ps, @@ -316,8 +316,8 @@ impl PKey { pr, &mut len, rsa) - } - }; + }) + }); if rv < 0 as c_int { ~[] @@ -332,8 +332,8 @@ impl PKey { unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); - do m.as_imm_buf |pm, m_len| { - do s.as_imm_buf |ps, s_len| { + m.as_imm_buf(|pm, m_len| { + s.as_imm_buf(|ps, s_len| { let rv = libcrypto::RSA_verify( openssl_hash_nid(hash), pm, @@ -344,14 +344,14 @@ impl PKey { ); rv == 1 as c_int - } - } + }) + }) } } } impl Drop for PKey { - fn drop(&self) { + fn drop(&mut self) { unsafe { libcrypto::EVP_PKEY_free(self.evp); } diff --git a/rand.rs b/rand.rs index eedac4ab..8e78c51c 100644 --- a/rand.rs +++ b/rand.rs @@ -4,19 +4,19 @@ use std::vec; mod libcrypto { use std::libc::c_int; - #[link_args = "-lcrypto"] + #[link(name = "crypto")] extern { - fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; + pub 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); - do out.as_mut_buf |out_buf, len| { + out.as_mut_buf(|out_buf, len| { let r = unsafe { libcrypto::RAND_bytes(out_buf, len as c_int) }; if r != 1 as c_int { fail!() } - } + }); unsafe { vec::raw::set_len(&mut out, len); } @@ -30,6 +30,6 @@ mod tests { #[test] fn test_rand_bytes() { let bytes = rand_bytes(32u); - println(fmt!("%?", bytes)); + println!("{:?}", bytes); } } diff --git a/symm.rs b/symm.rs index bc28624a..3d31bde0 100644 --- a/symm.rs +++ b/symm.rs @@ -14,27 +14,27 @@ mod libcrypto { extern { #[link_args = "-lcrypto"] - 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); + 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; - fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER, + pub 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, + pub 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 fn EVP_CipherFinal(ctx: EVP_CIPHER_CTX, res: *mut u8, len: &mut c_int); } } @@ -115,8 +115,8 @@ impl Crypter { }; assert_eq!(key.len(), self.keylen); - do key.as_imm_buf |pkey, _len| { - do iv.as_imm_buf |piv, _len| { + key.as_imm_buf(|pkey, _len| { + iv.as_imm_buf(|piv, _len| { libcrypto::EVP_CipherInit( self.ctx, self.evp, @@ -124,8 +124,8 @@ impl Crypter { piv, mode ) - } - } + }); + }); } } @@ -135,10 +135,10 @@ impl Crypter { */ pub fn update(&self, data: &[u8]) -> ~[u8] { unsafe { - do data.as_imm_buf |pdata, len| { + data.as_imm_buf(|pdata, len| { let mut res = vec::from_elem(len + self.blocksize, 0u8); - let reslen = do res.as_mut_buf |pres, _len| { + let reslen = res.as_mut_buf(|pres, _len| { let mut reslen = (len + self.blocksize) as u32; libcrypto::EVP_CipherUpdate( @@ -150,11 +150,11 @@ impl Crypter { ); reslen - }; + }); res.truncate(reslen as uint); res - } + }) } } @@ -165,11 +165,11 @@ impl Crypter { unsafe { let mut res = vec::from_elem(self.blocksize, 0u8); - let reslen = do res.as_mut_buf |pres, _len| { + 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 - }; + }); res.truncate(reslen as uint); res @@ -178,7 +178,7 @@ impl Crypter { } impl Drop for Crypter { - fn drop(&self) { + fn drop(&mut self) { unsafe { libcrypto::EVP_CIPHER_CTX_free(self.ctx); } @@ -251,13 +251,13 @@ mod tests { let computed = cipher.update(pt.from_hex()) + cipher.final(); if computed != expected { - println(fmt!("Computed: %s", computed.to_hex())); - println(fmt!("Expected: %s", expected.to_hex())); + println!("Computed: {}", computed.to_hex()); + println!("Expected: {}", expected.to_hex()); if computed.len() != expected.len() { - println(fmt!("Lengths differ: %u in computed vs %u expected", - computed.len(), expected.len())); + println!("Lengths differ: {} in computed vs {} expected", + computed.len(), expected.len()); } - fail!(~"test failure"); + fail!("test failure"); } }