diff --git a/crypto.rc b/crypto.rc index eeccd14f..22ba4385 100644 --- a/crypto.rc +++ b/crypto.rc @@ -20,8 +20,6 @@ uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")]; #[crate_type = "lib"]; -extern mod std; // FIXME https://github.com/mozilla/rust/issues/1127 - pub mod hex; pub mod hash; pub mod hmac; diff --git a/hash.rs b/hash.rs index 78dfd08e..bd19f333 100644 --- a/hash.rs +++ b/hash.rs @@ -1,4 +1,4 @@ -use core::libc::c_uint; +use std::libc::c_uint; pub enum HashType { MD5, @@ -15,9 +15,9 @@ pub type EVP_MD_CTX = *libc::c_void; #[allow(non_camel_case_types)] pub type EVP_MD = *libc::c_void; -#[link_name = "crypto"] #[abi = "cdecl"] -extern mod libcrypto { +#[link_args = "-lcrypto"] +extern { fn EVP_MD_CTX_create() -> EVP_MD_CTX; fn EVP_md5() -> EVP_MD; @@ -35,12 +35,12 @@ extern mod libcrypto { 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), } } } @@ -53,7 +53,7 @@ pub struct Hasher { pub fn Hasher(ht: HashType) -> Hasher { unsafe { - let ctx = libcrypto::EVP_MD_CTX_create(); + let ctx = EVP_MD_CTX_create(); let (evp, mdlen) = evpmd(ht); let h = Hasher { evp: evp, ctx: ctx, len: mdlen }; h.init(); @@ -65,7 +65,7 @@ pub impl Hasher { /// Initializes this hasher fn init(&self) { unsafe { - libcrypto::EVP_DigestInit(self.ctx, self.evp); + EVP_DigestInit(self.ctx, self.evp); } } @@ -73,7 +73,7 @@ pub impl Hasher { fn update(&self, data: &[u8]) { unsafe { do vec::as_imm_buf(data) |pdata, len| { - libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) + EVP_DigestUpdate(self.ctx, pdata, len as c_uint) } } } @@ -86,7 +86,7 @@ pub impl Hasher { unsafe { let mut res = vec::from_elem(self.len, 0u8); do vec::as_mut_buf(res) |pres, _len| { - libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); + EVP_DigestFinal(self.ctx, pres, ptr::null()); } res } diff --git a/hex.rs b/hex.rs index abd62e8a..380369f4 100644 --- a/hex.rs +++ b/hex.rs @@ -23,7 +23,7 @@ pub trait ToHex { impl<'self> ToHex for &'self [u8] { fn to_hex(&self) -> ~str { - let chars = str::to_chars(~"0123456789ABCDEF"); + let chars = str::to_chars("0123456789ABCDEF"); let mut s = ~""; diff --git a/hmac.rs b/hmac.rs index dd35010f..1c9e145d 100644 --- a/hmac.rs +++ b/hmac.rs @@ -26,10 +26,9 @@ pub struct HMAC_CTX { key: [libc::c_uchar, ..128] } -#[link_name = "crypto"] +#[link_args = "-lcrypto"] #[abi = "cdecl"] -extern mod libcrypto { - +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); @@ -56,7 +55,7 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { mut key: [0u8, .. 128] }; - libcrypto::HMAC_CTX_init(&mut ctx, + HMAC_CTX_init(&mut ctx, vec::raw::to_ptr(key), key.len() as libc::c_int, evp); @@ -69,7 +68,7 @@ pub impl HMAC { fn update(&mut self, data: &[u8]) { unsafe { do vec::as_imm_buf(data) |pdata, len| { - libcrypto::HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) + HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) } } } @@ -79,7 +78,7 @@ pub impl HMAC { let mut res = vec::from_elem(self.len, 0u8); let mut outlen: libc::c_uint = 0; do vec::as_mut_buf(res) |pres, _len| { - libcrypto::HMAC_Final(&mut self.ctx, pres, &mut outlen); + HMAC_Final(&mut self.ctx, pres, &mut outlen); assert!(self.len == outlen as uint) } res @@ -90,7 +89,7 @@ pub impl HMAC { fn main() { let mut h = HMAC(SHA512, ~[00u8]); - h.update(~[00u8]); + h.update([00u8]); io::println(fmt!("%?", h.final())) } diff --git a/pkcs5.rs b/pkcs5.rs index ec81c333..c43999ee 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -1,12 +1,12 @@ -use core::libc::c_int; +use std::libc::c_int; -#[link_name = "crypto"] +#[link_args = "-lcrypto"] #[abi = "cdecl"] -extern mod libcrypto { +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; + salt: *u8, saltlen: c_int, + iter: c_int, keylen: c_int, + out: *mut u8) -> c_int; } #[doc = " @@ -23,7 +23,7 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, do vec::as_mut_buf(out) |out_buf, _out_len| { unsafe { - let r = libcrypto::PKCS5_PBKDF2_HMAC_SHA1( + let r = 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, diff --git a/pkey.rs b/pkey.rs index bdd18ada..3d8ba5f9 100644 --- a/pkey.rs +++ b/pkey.rs @@ -1,4 +1,4 @@ -use core::libc::{c_int, c_uint}; +use std::libc::{c_int, c_uint}; use hash::{HashType, MD5, SHA1, SHA224, SHA256, SHA384, SHA512}; #[allow(non_camel_case_types)] @@ -10,9 +10,9 @@ type ANYKEY = *libc::c_void; #[allow(non_camel_case_types)] type RSA = *libc::c_void; -#[link_name = "crypto"] +#[link_args = "-lcrypto"] #[abi = "cdecl"] -extern mod libcrypto { +extern { fn EVP_PKEY_new() -> *EVP_PKEY; fn EVP_PKEY_free(k: *EVP_PKEY); fn EVP_PKEY_assign(k: *EVP_PKEY, t: c_int, inner: *ANYKEY); @@ -27,13 +27,13 @@ extern mod libcrypto { 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; + pad: c_int) -> c_int; fn RSA_private_decrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, - pad: c_int) -> c_int; + pad: c_int) -> c_int; fn RSA_sign(t: c_int, m: *u8, mlen: c_uint, sig: *mut u8, siglen: *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; + k: *RSA) -> c_int; } enum Parts { @@ -93,13 +93,13 @@ pub struct PKey { pub fn PKey() -> PKey { unsafe { - PKey { evp: libcrypto::EVP_PKEY_new(), parts: Neither } + PKey { evp: EVP_PKEY_new(), parts: Neither } } } ///Represents a public key, optionally with a private key attached. priv impl PKey { - priv fn _tostr(&self, f: @fn(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] { + priv fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] { let buf = ptr::mut_null(); let len = f(self.evp, &buf); if len < 0 as c_int { return ~[]; } @@ -115,7 +115,7 @@ priv impl PKey { priv fn _fromstr( &mut self, s: &[u8], - f: @fn(c_int, &*EVP_PKEY, &*u8, c_uint) -> *EVP_PKEY + f: extern "C" unsafe fn(c_int, &*EVP_PKEY, &*u8, c_uint) -> *EVP_PKEY ) { do vec::as_imm_buf(s) |ps, len| { let evp = ptr::null(); @@ -128,7 +128,7 @@ priv impl PKey { pub impl PKey { 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(), @@ -137,7 +137,7 @@ pub impl PKey { let rsa_ = rsa_to_any(rsa); // XXX: 6 == NID_rsaEncryption - libcrypto::EVP_PKEY_assign(self.evp, 6 as c_int, rsa_); + EVP_PKEY_assign(self.evp, 6 as c_int, rsa_); self.parts = Both; } } @@ -147,7 +147,7 @@ pub impl PKey { */ fn save_pub(&self) -> ~[u8] { unsafe { - self._tostr(libcrypto::i2d_PublicKey) + self._tostr(i2d_PublicKey) } } @@ -156,7 +156,7 @@ pub impl PKey { */ fn load_pub(&mut self, s: &[u8]) { unsafe { - self._fromstr(s, libcrypto::d2i_PublicKey); + self._fromstr(s, d2i_PublicKey); self.parts = Public; } } @@ -167,7 +167,7 @@ pub impl PKey { */ fn save_priv(&self, ) -> ~[u8] { unsafe { - self._tostr(libcrypto::i2d_PrivateKey) + self._tostr(i2d_PrivateKey) } } /** @@ -176,7 +176,7 @@ pub impl PKey { */ fn load_priv(&mut self, s: &[u8]) { unsafe { - self._fromstr(s, libcrypto::d2i_PrivateKey); + self._fromstr(s, d2i_PrivateKey); self.parts = Both; } } @@ -186,7 +186,7 @@ pub impl PKey { */ 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 } } @@ -224,8 +224,8 @@ pub impl PKey { */ 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 @@ -234,58 +234,57 @@ pub impl PKey { 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); - do vec::as_mut_buf(r) |pr, _len| { - do vec::as_imm_buf(s) |ps, s_len| { - let rv = libcrypto::RSA_public_encrypt( - s_len as c_uint, - ps, - pr, - rsa, - openssl_padding_code(padding) - ); - - if rv < 0 as c_int { - ~[] - } else { - vec::const_slice(r, 0u, rv as uint).to_owned() - } - } + let rv = do vec::as_mut_buf(r) |pr, _len| { + do vec::as_imm_buf(s) |ps, s_len| { + RSA_public_encrypt( + s_len as c_uint, + ps, + pr, + rsa, + openssl_padding_code(padding) + ) + } + }; + if rv < 0 as c_int { + ~[] + } else { + vec::slice(r, 0u, rv as uint).to_owned() } } } 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!(s.len() as c_uint == libcrypto::RSA_size(rsa)); + assert!(s.len() as c_uint == RSA_size(rsa)); let mut r = vec::from_elem(len as uint + 1u, 0u8); - do vec::as_mut_buf(r) |pr, _len| { - do vec::as_imm_buf(s) |ps, s_len| { - let rv = libcrypto::RSA_private_decrypt( - s_len as c_uint, - ps, - pr, - rsa, - openssl_padding_code(padding) - ); + let rv = do vec::as_mut_buf(r) |pr, _len| { + do vec::as_imm_buf(s) |ps, s_len| { + RSA_private_decrypt( + s_len as c_uint, + ps, + pr, + rsa, + openssl_padding_code(padding) + ) + } + }; - if rv < 0 as c_int { - ~[] - } else { - vec::const_slice(r, 0u, rv as uint).to_owned() - } - } + if rv < 0 as c_int { + ~[] + } else { + vec::slice(r, 0u, rv as uint).to_owned() } } } @@ -315,37 +314,37 @@ pub impl PKey { fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[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); let mut r = vec::from_elem(len as uint + 1u, 0u8); - do vec::as_mut_buf(r) |pr, _len| { - do vec::as_imm_buf(s) |ps, s_len| { - let rv = libcrypto::RSA_sign( - openssl_hash_nid(hash), - ps, - s_len as c_uint, - pr, - &len, - rsa); + let rv = do vec::as_mut_buf(r) |pr, _len| { + do vec::as_imm_buf(s) |ps, s_len| { + RSA_sign( + openssl_hash_nid(hash), + ps, + s_len as c_uint, + pr, + &len, + rsa) + } + }; - if rv < 0 as c_int { - ~[] - } else { - vec::const_slice(r, 0u, len as uint).to_owned() - } - } + if rv < 0 as c_int { + ~[] + } else { + vec::slice(r, 0u, len as uint).to_owned() } } } 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); do vec::as_imm_buf(m) |pm, m_len| { do vec::as_imm_buf(s) |ps, s_len| { - let rv = libcrypto::RSA_verify( + let rv = RSA_verify( openssl_hash_nid(hash), pm, m_len as c_uint, diff --git a/rand.rs b/rand.rs index 96565084..e4cf2d1a 100644 --- a/rand.rs +++ b/rand.rs @@ -1,8 +1,8 @@ -use core::libc::c_int; +use std::libc::c_int; -#[link_name = "crypto"] +#[link_args = "-lcrypto"] #[abi = "cdecl"] -extern mod libcrypto { +extern { fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; } @@ -11,7 +11,7 @@ pub fn rand_bytes(len: uint) -> ~[u8] { do vec::as_mut_buf(out) |out_buf, len| { unsafe { - let r = libcrypto::RAND_bytes(out_buf, len as c_int); + let r = RAND_bytes(out_buf, len as c_int); if r != 1 as c_int { fail!() } } } diff --git a/symm.rs b/symm.rs index f1ba5235..1cb8ed44 100644 --- a/symm.rs +++ b/symm.rs @@ -1,4 +1,4 @@ -use core::libc::{c_int, c_uint}; +use std::libc::{c_int, c_uint}; #[allow(non_camel_case_types)] type EVP_CIPHER_CTX = *libc::c_void; @@ -6,9 +6,9 @@ type EVP_CIPHER_CTX = *libc::c_void; #[allow(non_camel_case_types)] type EVP_CIPHER = *libc::c_void; -#[link_name = "crypto"] +#[link_args = "-lcrypto"] #[abi = "cdecl"] -extern mod libcrypto { +extern { fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX; fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int); @@ -25,7 +25,7 @@ extern mod libcrypto { fn EVP_rc4() -> EVP_CIPHER; fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER, - key: *u8, iv: *u8, mode: c_int); + 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); @@ -54,17 +54,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), } } } @@ -79,7 +79,7 @@ pub struct Crypter { pub fn Crypter(t: Type) -> Crypter { unsafe { - let ctx = libcrypto::EVP_CIPHER_CTX_new(); + let ctx = EVP_CIPHER_CTX_new(); let (evp, keylen, blocksz) = evpc(t); Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz } } @@ -94,7 +94,7 @@ pub 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); } } } @@ -112,7 +112,7 @@ pub impl Crypter { do vec::as_imm_buf(key) |pkey, _len| { do vec::as_imm_buf(iv) |piv, _len| { - libcrypto::EVP_CipherInit( + EVP_CipherInit( self.ctx, self.evp, pkey, @@ -136,7 +136,7 @@ pub impl Crypter { let reslen = do vec::as_mut_buf(res) |pres, _len| { let mut reslen = (len + self.blocksize) as u32; - libcrypto::EVP_CipherUpdate( + EVP_CipherUpdate( self.ctx, pres, &mut reslen, @@ -161,7 +161,7 @@ pub impl Crypter { let reslen = do vec::as_mut_buf(res) |pres, _len| { let mut reslen = self.blocksize as c_int; - libcrypto::EVP_CipherFinal(self.ctx, pres, &mut reslen); + EVP_CipherFinal(self.ctx, pres, &mut reslen); reslen }; @@ -216,11 +216,11 @@ mod tests { ~[ 0x8eu8, 0xa2u8, 0xb7u8, 0xcau8, 0x51u8, 0x67u8, 0x45u8, 0xbfu8, 0xeau8, 0xfcu8, 0x49u8, 0x90u8, 0x4bu8, 0x49u8, 0x60u8, 0x89u8 ]; let c = Crypter(AES_256_ECB); - c.init(Encrypt, k0, ~[]); + c.init(Encrypt, k0, []); c.pad(false); let r0 = c.update(p0) + c.final(); assert!(r0 == c0); - c.init(Decrypt, k0, ~[]); + c.init(Decrypt, k0, []); c.pad(false); let p1 = c.update(r0) + c.final(); assert!(p1 == p0);