Loading crypto/hash.rs +4 −5 Original line number Diff line number Diff line use libc; use libc::c_uint; use std::ptr; use std::slice; pub enum HashType { MD5, Loading Loading @@ -76,9 +75,9 @@ impl Hasher { * Return the digest of all bytes added to this hasher since its last * initialization */ pub fn final(&self) -> ~[u8] { pub fn final(&self) -> Vec<u8> { unsafe { let mut res = slice::from_elem(self.len, 0u8); let mut res = Vec::from_elem(self.len, 0u8); EVP_DigestFinal(self.ctx, res.as_mut_ptr(), ptr::null()); res } Loading @@ -97,7 +96,7 @@ impl Drop for Hasher { * Hashes the supplied input data using hash t, returning the resulting hash * value */ pub fn hash(t: HashType, data: &[u8]) -> ~[u8] { pub fn hash(t: HashType, data: &[u8]) -> Vec<u8> { let h = Hasher::new(t); h.update(data); h.final() Loading @@ -120,7 +119,7 @@ mod tests { fn hash_test(hashtype: super::HashType, hashtest: &HashTest) { let calced_raw = super::hash(hashtype, hashtest.input); let calced = calced_raw.to_hex(); let calced = calced_raw.as_slice().to_hex(); if calced != hashtest.expected_output { println!("Test failed - {} != {}", calced, hashtest.expected_output); Loading crypto/hmac.rs +2 −3 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ use libc::{c_uchar, c_int, c_uint}; use std::ptr; use std::slice; use crypto::hash; #[allow(non_camel_case_types)] Loading Loading @@ -70,9 +69,9 @@ impl HMAC { } } pub fn final(&mut self) -> ~[u8] { pub fn final(&mut self) -> Vec<u8> { unsafe { let mut res = slice::from_elem(self.len, 0u8); let mut res = Vec::from_elem(self.len, 0u8); let mut outlen = 0; HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen); assert!(self.len == outlen as uint) Loading crypto/pkcs5.rs +14 −15 Original line number Diff line number Diff line use libc::c_int; use std::slice; #[link(name = "crypto")] extern { Loading @@ -10,12 +9,12 @@ extern { } /// 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] { pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> Vec<u8> { unsafe { assert!(iter >= 1); assert!(keylen >= 1); let mut out = slice::with_capacity(keylen); let mut out = Vec::with_capacity(keylen); let r = PKCS5_PBKDF2_HMAC_SHA1( pass.as_ptr(), pass.len() as c_int, Loading Loading @@ -44,11 +43,11 @@ mod tests { 1u, 20u ), ~[ vec!( 0x0c_u8, 0x60_u8, 0xc8_u8, 0x0f_u8, 0x96_u8, 0x1f_u8, 0x0e_u8, 0x71_u8, 0xf3_u8, 0xa9_u8, 0xb5_u8, 0x24_u8, 0xaf_u8, 0x60_u8, 0x12_u8, 0x06_u8, 0x2f_u8, 0xe0_u8, 0x37_u8, 0xa6_u8 ] ) ); assert_eq!( Loading @@ -58,11 +57,11 @@ mod tests { 2u, 20u ), ~[ vec!( 0xea_u8, 0x6c_u8, 0x01_u8, 0x4d_u8, 0xc7_u8, 0x2d_u8, 0x6f_u8, 0x8c_u8, 0xcd_u8, 0x1e_u8, 0xd9_u8, 0x2a_u8, 0xce_u8, 0x1d_u8, 0x41_u8, 0xf0_u8, 0xd8_u8, 0xde_u8, 0x89_u8, 0x57_u8 ] ) ); assert_eq!( Loading @@ -72,11 +71,11 @@ mod tests { 4096u, 20u ), ~[ vec!( 0x4b_u8, 0x00_u8, 0x79_u8, 0x01_u8, 0xb7_u8, 0x65_u8, 0x48_u8, 0x9a_u8, 0xbe_u8, 0xad_u8, 0x49_u8, 0xd9_u8, 0x26_u8, 0xf7_u8, 0x21_u8, 0xd0_u8, 0x65_u8, 0xa4_u8, 0x29_u8, 0xc1_u8 ] ) ); assert_eq!( Loading @@ -86,11 +85,11 @@ mod tests { 16777216u, 20u ), ~[ vec!( 0xee_u8, 0xfe_u8, 0x3d_u8, 0x61_u8, 0xcd_u8, 0x4d_u8, 0xa4_u8, 0xe4_u8, 0xe9_u8, 0x94_u8, 0x5b_u8, 0x3d_u8, 0x6b_u8, 0xa2_u8, 0x15_u8, 0x8c_u8, 0x26_u8, 0x34_u8, 0xe9_u8, 0x84_u8 ] ) ); assert_eq!( Loading @@ -100,12 +99,12 @@ mod tests { 4096u, 25u ), ~[ vec!( 0x3d_u8, 0x2e_u8, 0xec_u8, 0x4f_u8, 0xe4_u8, 0x1c_u8, 0x84_u8, 0x9b_u8, 0x80_u8, 0xc8_u8, 0xd8_u8, 0x36_u8, 0x62_u8, 0xc0_u8, 0xe4_u8, 0x4a_u8, 0x8b_u8, 0x29_u8, 0x1a_u8, 0x96_u8, 0x4c_u8, 0xf2_u8, 0xf0_u8, 0x70_u8, 0x38_u8 ] ) ); assert_eq!( Loading @@ -115,11 +114,11 @@ mod tests { 4096u, 16u ), ~[ vec!( 0x56_u8, 0xfa_u8, 0x6a_u8, 0xa7_u8, 0x55_u8, 0x48_u8, 0x09_u8, 0x9d_u8, 0xcc_u8, 0x37_u8, 0xd7_u8, 0xf0_u8, 0x34_u8, 0x25_u8, 0xe0_u8, 0xc3_u8 ] ) ); } } crypto/pkey.rs +36 −37 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ use std::cast; use libc::{c_char, c_int, c_uint}; use libc; use std::ptr; use std::slice; use crypto::hash::{HashType, MD5, SHA1, SHA224, SHA256, SHA384, SHA512}; #[allow(non_camel_case_types)] Loading Loading @@ -90,11 +89,11 @@ impl PKey { } } fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] { fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> Vec<u8> { unsafe { let len = f(self.evp, ptr::null()); if len < 0 as c_int { return ~[]; } let mut s = slice::from_elem(len as uint, 0u8); if len < 0 as c_int { return vec!(); } let mut s = Vec::from_elem(len as uint, 0u8); let r = f(self.evp, &s.as_mut_ptr()); Loading Loading @@ -133,7 +132,7 @@ impl PKey { /** * Returns a serialized form of the public key, suitable for load_pub(). */ pub fn save_pub(&self) -> ~[u8] { pub fn save_pub(&self) -> Vec<u8> { self._tostr(i2d_PublicKey) } Loading @@ -149,7 +148,7 @@ impl PKey { * Returns a serialized form of the public and private keys, suitable for * load_priv(). */ pub fn save_priv(&self) -> ~[u8] { pub fn save_priv(&self) -> Vec<u8> { self._tostr(i2d_PrivateKey) } /** Loading Loading @@ -212,14 +211,14 @@ impl PKey { } } pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> { unsafe { let rsa = EVP_PKEY_get1_RSA(self.evp); let len = RSA_size(rsa); assert!(s.len() < self.max_data()); let mut r = slice::from_elem(len as uint + 1u, 0u8); let mut r = Vec::from_elem(len as uint + 1u, 0u8); let rv = RSA_public_encrypt( s.len() as c_uint, Loading @@ -229,7 +228,7 @@ impl PKey { openssl_padding_code(padding)); if rv < 0 as c_int { ~[] vec!() } else { r.truncate(rv as uint); r Loading @@ -237,14 +236,14 @@ impl PKey { } } pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> { unsafe { let rsa = EVP_PKEY_get1_RSA(self.evp); let len = RSA_size(rsa); assert_eq!(s.len() as c_uint, RSA_size(rsa)); let mut r = slice::from_elem(len as uint + 1u, 0u8); let mut r = Vec::from_elem(len as uint + 1u, 0u8); let rv = RSA_private_decrypt( s.len() as c_uint, Loading @@ -254,7 +253,7 @@ impl PKey { openssl_padding_code(padding)); if rv < 0 as c_int { ~[] vec!() } else { r.truncate(rv as uint); r Loading @@ -266,18 +265,18 @@ impl PKey { * Encrypts data using OAEP padding, returning the encrypted data. The * supplied data must not be larger than max_data(). */ pub fn encrypt(&self, s: &[u8]) -> ~[u8] { self.encrypt_with_padding(s, OAEP) } pub fn encrypt(&self, s: &[u8]) -> Vec<u8> { self.encrypt_with_padding(s, OAEP) } /** * Decrypts data, expecting OAEP padding, returning the decrypted data. */ pub fn decrypt(&self, s: &[u8]) -> ~[u8] { self.decrypt_with_padding(s, OAEP) } pub fn decrypt(&self, s: &[u8]) -> Vec<u8> { self.decrypt_with_padding(s, OAEP) } /** * Signs data, using OpenSSL's default scheme and sha256. Unlike encrypt(), * can process an arbitrary amount of data; returns the signature. */ pub fn sign(&self, s: &[u8]) -> ~[u8] { self.sign_with_hash(s, SHA256) } pub fn sign(&self, s: &[u8]) -> Vec<u8> { self.sign_with_hash(s, SHA256) } /** * Verifies a signature s (using OpenSSL's default scheme and sha256) on a Loading @@ -285,11 +284,11 @@ impl PKey { */ pub fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, SHA256) } pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] { pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> Vec<u8> { unsafe { let rsa = EVP_PKEY_get1_RSA(self.evp); let mut len = RSA_size(rsa); let mut r = slice::from_elem(len as uint + 1u, 0u8); let mut r = Vec::from_elem(len as uint + 1u, 0u8); let rv = RSA_sign( openssl_hash_nid(hash), Loading @@ -300,7 +299,7 @@ impl PKey { rsa); if rv < 0 as c_int { ~[] vec!() } else { r.truncate(len as uint); r Loading Loading @@ -343,7 +342,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); k0.gen(512u); k1.load_pub(k0.save_pub()); k1.load_pub(k0.save_pub().as_slice()); assert_eq!(k0.save_pub(), k1.save_pub()); assert_eq!(k0.size(), k1.size()); assert!(k0.can(super::Encrypt)); Loading @@ -361,7 +360,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); k0.gen(512u); k1.load_priv(k0.save_priv()); k1.load_priv(k0.save_priv().as_slice()); assert_eq!(k0.save_priv(), k1.save_priv()); assert_eq!(k0.size(), k1.size()); assert!(k0.can(super::Encrypt)); Loading @@ -378,11 +377,11 @@ mod tests { fn test_encrypt() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); let emsg = k1.encrypt(msg); let dmsg = k0.decrypt(emsg); k1.load_pub(k0.save_pub().as_slice()); let emsg = k1.encrypt(msg.as_slice()); let dmsg = k0.decrypt(emsg.as_slice()); assert!(msg == dmsg); } Loading @@ -390,11 +389,11 @@ mod tests { fn test_encrypt_pkcs() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); let emsg = k1.encrypt_with_padding(msg, super::PKCS1v15); let dmsg = k0.decrypt_with_padding(emsg, super::PKCS1v15); k1.load_pub(k0.save_pub().as_slice()); let emsg = k1.encrypt_with_padding(msg.as_slice(), super::PKCS1v15); let dmsg = k0.decrypt_with_padding(emsg.as_slice(), super::PKCS1v15); assert!(msg == dmsg); } Loading @@ -402,11 +401,11 @@ mod tests { fn test_sign() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); let sig = k0.sign(msg); let rv = k1.verify(msg, sig); k1.load_pub(k0.save_pub().as_slice()); let sig = k0.sign(msg.as_slice()); let rv = k1.verify(msg.as_slice(), sig.as_slice()); assert!(rv == true); } Loading @@ -414,13 +413,13 @@ mod tests { fn test_sign_hashes() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); k1.load_pub(k0.save_pub().as_slice()); let sig = k0.sign_with_hash(msg, MD5); let sig = k0.sign_with_hash(msg.as_slice(), MD5); assert!(k1.verify_with_hash(msg, sig, MD5)); assert!(!k1.verify_with_hash(msg, sig, SHA1)); assert!(k1.verify_with_hash(msg.as_slice(), sig.as_slice(), MD5)); assert!(!k1.verify_with_hash(msg.as_slice(), sig.as_slice(), SHA1)); } } crypto/rand.rs +2 −3 Original line number Diff line number Diff line use libc::c_int; use std::slice; #[link(name = "crypto")] extern { fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; } pub fn rand_bytes(len: uint) -> ~[u8] { pub fn rand_bytes(len: uint) -> Vec<u8> { unsafe { let mut out = slice::with_capacity(len); let mut out = Vec::with_capacity(len); let r = RAND_bytes(out.as_mut_ptr(), len as c_int); if r != 1 as c_int { fail!() } Loading Loading
crypto/hash.rs +4 −5 Original line number Diff line number Diff line use libc; use libc::c_uint; use std::ptr; use std::slice; pub enum HashType { MD5, Loading Loading @@ -76,9 +75,9 @@ impl Hasher { * Return the digest of all bytes added to this hasher since its last * initialization */ pub fn final(&self) -> ~[u8] { pub fn final(&self) -> Vec<u8> { unsafe { let mut res = slice::from_elem(self.len, 0u8); let mut res = Vec::from_elem(self.len, 0u8); EVP_DigestFinal(self.ctx, res.as_mut_ptr(), ptr::null()); res } Loading @@ -97,7 +96,7 @@ impl Drop for Hasher { * Hashes the supplied input data using hash t, returning the resulting hash * value */ pub fn hash(t: HashType, data: &[u8]) -> ~[u8] { pub fn hash(t: HashType, data: &[u8]) -> Vec<u8> { let h = Hasher::new(t); h.update(data); h.final() Loading @@ -120,7 +119,7 @@ mod tests { fn hash_test(hashtype: super::HashType, hashtest: &HashTest) { let calced_raw = super::hash(hashtype, hashtest.input); let calced = calced_raw.to_hex(); let calced = calced_raw.as_slice().to_hex(); if calced != hashtest.expected_output { println!("Test failed - {} != {}", calced, hashtest.expected_output); Loading
crypto/hmac.rs +2 −3 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ use libc::{c_uchar, c_int, c_uint}; use std::ptr; use std::slice; use crypto::hash; #[allow(non_camel_case_types)] Loading Loading @@ -70,9 +69,9 @@ impl HMAC { } } pub fn final(&mut self) -> ~[u8] { pub fn final(&mut self) -> Vec<u8> { unsafe { let mut res = slice::from_elem(self.len, 0u8); let mut res = Vec::from_elem(self.len, 0u8); let mut outlen = 0; HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen); assert!(self.len == outlen as uint) Loading
crypto/pkcs5.rs +14 −15 Original line number Diff line number Diff line use libc::c_int; use std::slice; #[link(name = "crypto")] extern { Loading @@ -10,12 +9,12 @@ extern { } /// 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] { pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> Vec<u8> { unsafe { assert!(iter >= 1); assert!(keylen >= 1); let mut out = slice::with_capacity(keylen); let mut out = Vec::with_capacity(keylen); let r = PKCS5_PBKDF2_HMAC_SHA1( pass.as_ptr(), pass.len() as c_int, Loading Loading @@ -44,11 +43,11 @@ mod tests { 1u, 20u ), ~[ vec!( 0x0c_u8, 0x60_u8, 0xc8_u8, 0x0f_u8, 0x96_u8, 0x1f_u8, 0x0e_u8, 0x71_u8, 0xf3_u8, 0xa9_u8, 0xb5_u8, 0x24_u8, 0xaf_u8, 0x60_u8, 0x12_u8, 0x06_u8, 0x2f_u8, 0xe0_u8, 0x37_u8, 0xa6_u8 ] ) ); assert_eq!( Loading @@ -58,11 +57,11 @@ mod tests { 2u, 20u ), ~[ vec!( 0xea_u8, 0x6c_u8, 0x01_u8, 0x4d_u8, 0xc7_u8, 0x2d_u8, 0x6f_u8, 0x8c_u8, 0xcd_u8, 0x1e_u8, 0xd9_u8, 0x2a_u8, 0xce_u8, 0x1d_u8, 0x41_u8, 0xf0_u8, 0xd8_u8, 0xde_u8, 0x89_u8, 0x57_u8 ] ) ); assert_eq!( Loading @@ -72,11 +71,11 @@ mod tests { 4096u, 20u ), ~[ vec!( 0x4b_u8, 0x00_u8, 0x79_u8, 0x01_u8, 0xb7_u8, 0x65_u8, 0x48_u8, 0x9a_u8, 0xbe_u8, 0xad_u8, 0x49_u8, 0xd9_u8, 0x26_u8, 0xf7_u8, 0x21_u8, 0xd0_u8, 0x65_u8, 0xa4_u8, 0x29_u8, 0xc1_u8 ] ) ); assert_eq!( Loading @@ -86,11 +85,11 @@ mod tests { 16777216u, 20u ), ~[ vec!( 0xee_u8, 0xfe_u8, 0x3d_u8, 0x61_u8, 0xcd_u8, 0x4d_u8, 0xa4_u8, 0xe4_u8, 0xe9_u8, 0x94_u8, 0x5b_u8, 0x3d_u8, 0x6b_u8, 0xa2_u8, 0x15_u8, 0x8c_u8, 0x26_u8, 0x34_u8, 0xe9_u8, 0x84_u8 ] ) ); assert_eq!( Loading @@ -100,12 +99,12 @@ mod tests { 4096u, 25u ), ~[ vec!( 0x3d_u8, 0x2e_u8, 0xec_u8, 0x4f_u8, 0xe4_u8, 0x1c_u8, 0x84_u8, 0x9b_u8, 0x80_u8, 0xc8_u8, 0xd8_u8, 0x36_u8, 0x62_u8, 0xc0_u8, 0xe4_u8, 0x4a_u8, 0x8b_u8, 0x29_u8, 0x1a_u8, 0x96_u8, 0x4c_u8, 0xf2_u8, 0xf0_u8, 0x70_u8, 0x38_u8 ] ) ); assert_eq!( Loading @@ -115,11 +114,11 @@ mod tests { 4096u, 16u ), ~[ vec!( 0x56_u8, 0xfa_u8, 0x6a_u8, 0xa7_u8, 0x55_u8, 0x48_u8, 0x09_u8, 0x9d_u8, 0xcc_u8, 0x37_u8, 0xd7_u8, 0xf0_u8, 0x34_u8, 0x25_u8, 0xe0_u8, 0xc3_u8 ] ) ); } }
crypto/pkey.rs +36 −37 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ use std::cast; use libc::{c_char, c_int, c_uint}; use libc; use std::ptr; use std::slice; use crypto::hash::{HashType, MD5, SHA1, SHA224, SHA256, SHA384, SHA512}; #[allow(non_camel_case_types)] Loading Loading @@ -90,11 +89,11 @@ impl PKey { } } fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] { fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> Vec<u8> { unsafe { let len = f(self.evp, ptr::null()); if len < 0 as c_int { return ~[]; } let mut s = slice::from_elem(len as uint, 0u8); if len < 0 as c_int { return vec!(); } let mut s = Vec::from_elem(len as uint, 0u8); let r = f(self.evp, &s.as_mut_ptr()); Loading Loading @@ -133,7 +132,7 @@ impl PKey { /** * Returns a serialized form of the public key, suitable for load_pub(). */ pub fn save_pub(&self) -> ~[u8] { pub fn save_pub(&self) -> Vec<u8> { self._tostr(i2d_PublicKey) } Loading @@ -149,7 +148,7 @@ impl PKey { * Returns a serialized form of the public and private keys, suitable for * load_priv(). */ pub fn save_priv(&self) -> ~[u8] { pub fn save_priv(&self) -> Vec<u8> { self._tostr(i2d_PrivateKey) } /** Loading Loading @@ -212,14 +211,14 @@ impl PKey { } } pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> { unsafe { let rsa = EVP_PKEY_get1_RSA(self.evp); let len = RSA_size(rsa); assert!(s.len() < self.max_data()); let mut r = slice::from_elem(len as uint + 1u, 0u8); let mut r = Vec::from_elem(len as uint + 1u, 0u8); let rv = RSA_public_encrypt( s.len() as c_uint, Loading @@ -229,7 +228,7 @@ impl PKey { openssl_padding_code(padding)); if rv < 0 as c_int { ~[] vec!() } else { r.truncate(rv as uint); r Loading @@ -237,14 +236,14 @@ impl PKey { } } pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> { unsafe { let rsa = EVP_PKEY_get1_RSA(self.evp); let len = RSA_size(rsa); assert_eq!(s.len() as c_uint, RSA_size(rsa)); let mut r = slice::from_elem(len as uint + 1u, 0u8); let mut r = Vec::from_elem(len as uint + 1u, 0u8); let rv = RSA_private_decrypt( s.len() as c_uint, Loading @@ -254,7 +253,7 @@ impl PKey { openssl_padding_code(padding)); if rv < 0 as c_int { ~[] vec!() } else { r.truncate(rv as uint); r Loading @@ -266,18 +265,18 @@ impl PKey { * Encrypts data using OAEP padding, returning the encrypted data. The * supplied data must not be larger than max_data(). */ pub fn encrypt(&self, s: &[u8]) -> ~[u8] { self.encrypt_with_padding(s, OAEP) } pub fn encrypt(&self, s: &[u8]) -> Vec<u8> { self.encrypt_with_padding(s, OAEP) } /** * Decrypts data, expecting OAEP padding, returning the decrypted data. */ pub fn decrypt(&self, s: &[u8]) -> ~[u8] { self.decrypt_with_padding(s, OAEP) } pub fn decrypt(&self, s: &[u8]) -> Vec<u8> { self.decrypt_with_padding(s, OAEP) } /** * Signs data, using OpenSSL's default scheme and sha256. Unlike encrypt(), * can process an arbitrary amount of data; returns the signature. */ pub fn sign(&self, s: &[u8]) -> ~[u8] { self.sign_with_hash(s, SHA256) } pub fn sign(&self, s: &[u8]) -> Vec<u8> { self.sign_with_hash(s, SHA256) } /** * Verifies a signature s (using OpenSSL's default scheme and sha256) on a Loading @@ -285,11 +284,11 @@ impl PKey { */ pub fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, SHA256) } pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] { pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> Vec<u8> { unsafe { let rsa = EVP_PKEY_get1_RSA(self.evp); let mut len = RSA_size(rsa); let mut r = slice::from_elem(len as uint + 1u, 0u8); let mut r = Vec::from_elem(len as uint + 1u, 0u8); let rv = RSA_sign( openssl_hash_nid(hash), Loading @@ -300,7 +299,7 @@ impl PKey { rsa); if rv < 0 as c_int { ~[] vec!() } else { r.truncate(len as uint); r Loading Loading @@ -343,7 +342,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); k0.gen(512u); k1.load_pub(k0.save_pub()); k1.load_pub(k0.save_pub().as_slice()); assert_eq!(k0.save_pub(), k1.save_pub()); assert_eq!(k0.size(), k1.size()); assert!(k0.can(super::Encrypt)); Loading @@ -361,7 +360,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); k0.gen(512u); k1.load_priv(k0.save_priv()); k1.load_priv(k0.save_priv().as_slice()); assert_eq!(k0.save_priv(), k1.save_priv()); assert_eq!(k0.size(), k1.size()); assert!(k0.can(super::Encrypt)); Loading @@ -378,11 +377,11 @@ mod tests { fn test_encrypt() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); let emsg = k1.encrypt(msg); let dmsg = k0.decrypt(emsg); k1.load_pub(k0.save_pub().as_slice()); let emsg = k1.encrypt(msg.as_slice()); let dmsg = k0.decrypt(emsg.as_slice()); assert!(msg == dmsg); } Loading @@ -390,11 +389,11 @@ mod tests { fn test_encrypt_pkcs() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); let emsg = k1.encrypt_with_padding(msg, super::PKCS1v15); let dmsg = k0.decrypt_with_padding(emsg, super::PKCS1v15); k1.load_pub(k0.save_pub().as_slice()); let emsg = k1.encrypt_with_padding(msg.as_slice(), super::PKCS1v15); let dmsg = k0.decrypt_with_padding(emsg.as_slice(), super::PKCS1v15); assert!(msg == dmsg); } Loading @@ -402,11 +401,11 @@ mod tests { fn test_sign() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); let sig = k0.sign(msg); let rv = k1.verify(msg, sig); k1.load_pub(k0.save_pub().as_slice()); let sig = k0.sign(msg.as_slice()); let rv = k1.verify(msg.as_slice(), sig.as_slice()); assert!(rv == true); } Loading @@ -414,13 +413,13 @@ mod tests { fn test_sign_hashes() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); k0.gen(512u); k1.load_pub(k0.save_pub()); k1.load_pub(k0.save_pub().as_slice()); let sig = k0.sign_with_hash(msg, MD5); let sig = k0.sign_with_hash(msg.as_slice(), MD5); assert!(k1.verify_with_hash(msg, sig, MD5)); assert!(!k1.verify_with_hash(msg, sig, SHA1)); assert!(k1.verify_with_hash(msg.as_slice(), sig.as_slice(), MD5)); assert!(!k1.verify_with_hash(msg.as_slice(), sig.as_slice(), SHA1)); } }
crypto/rand.rs +2 −3 Original line number Diff line number Diff line use libc::c_int; use std::slice; #[link(name = "crypto")] extern { fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; } pub fn rand_bytes(len: uint) -> ~[u8] { pub fn rand_bytes(len: uint) -> Vec<u8> { unsafe { let mut out = slice::with_capacity(len); let mut out = Vec::with_capacity(len); let r = RAND_bytes(out.as_mut_ptr(), len as c_int); if r != 1 as c_int { fail!() } Loading