From cfc79313f59b18a0e6d11002185b503b1590f9d3 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 18 Apr 2014 23:56:01 -0700 Subject: [PATCH] Update for ~[T] changes --- crypto/hash.rs | 9 +++--- crypto/hmac.rs | 5 ++-- crypto/pkcs5.rs | 29 ++++++++++---------- crypto/pkey.rs | 73 ++++++++++++++++++++++++------------------------- crypto/rand.rs | 5 ++-- crypto/symm.rs | 45 +++++++++++++++--------------- 6 files changed, 80 insertions(+), 86 deletions(-) diff --git a/crypto/hash.rs b/crypto/hash.rs index ed617be6..fdf7e625 100644 --- a/crypto/hash.rs +++ b/crypto/hash.rs @@ -1,7 +1,6 @@ use libc; use libc::c_uint; use std::ptr; -use std::slice; pub enum HashType { MD5, @@ -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 { 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 } @@ -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 { let h = Hasher::new(t); h.update(data); h.final() @@ -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); diff --git a/crypto/hmac.rs b/crypto/hmac.rs index b66180fc..a30ce4aa 100644 --- a/crypto/hmac.rs +++ b/crypto/hmac.rs @@ -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)] @@ -70,9 +69,9 @@ impl HMAC { } } - pub fn final(&mut self) -> ~[u8] { + pub fn final(&mut self) -> Vec { 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) diff --git a/crypto/pkcs5.rs b/crypto/pkcs5.rs index 2d1bfd34..06fc1bb9 100644 --- a/crypto/pkcs5.rs +++ b/crypto/pkcs5.rs @@ -1,5 +1,4 @@ use libc::c_int; -use std::slice; #[link(name = "crypto")] extern { @@ -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 { 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, @@ -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!( @@ -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!( @@ -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!( @@ -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!( @@ -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!( @@ -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 - ] + ) ); } } diff --git a/crypto/pkey.rs b/crypto/pkey.rs index 0058e6d0..a65e9131 100644 --- a/crypto/pkey.rs +++ b/crypto/pkey.rs @@ -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)] @@ -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 { 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()); @@ -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 { self._tostr(i2d_PublicKey) } @@ -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 { self._tostr(i2d_PrivateKey) } /** @@ -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 { 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, @@ -229,7 +228,7 @@ impl PKey { openssl_padding_code(padding)); if rv < 0 as c_int { - ~[] + vec!() } else { r.truncate(rv as uint); r @@ -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 { 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, @@ -254,7 +253,7 @@ impl PKey { openssl_padding_code(padding)); if rv < 0 as c_int { - ~[] + vec!() } else { r.truncate(rv as uint); r @@ -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 { 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 { 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 { self.sign_with_hash(s, SHA256) } /** * Verifies a signature s (using OpenSSL's default scheme and sha256) on a @@ -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 { 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), @@ -300,7 +299,7 @@ impl PKey { rsa); if rv < 0 as c_int { - ~[] + vec!() } else { r.truncate(len as uint); r @@ -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)); @@ -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)); @@ -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); } @@ -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); } @@ -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); } @@ -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)); } } diff --git a/crypto/rand.rs b/crypto/rand.rs index 2ae001ef..9db87fcd 100644 --- a/crypto/rand.rs +++ b/crypto/rand.rs @@ -1,14 +1,13 @@ 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 { 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!() } diff --git a/crypto/symm.rs b/crypto/symm.rs index d648d7c4..a12e8b41 100644 --- a/crypto/symm.rs +++ b/crypto/symm.rs @@ -1,6 +1,5 @@ use libc::{c_int, c_uint}; use libc; -use std::slice; #[allow(non_camel_case_types)] pub type EVP_CIPHER_CTX = *libc::c_void; @@ -124,9 +123,9 @@ impl Crypter { * Update this crypter with more data to encrypt or decrypt. Returns * encrypted or decrypted bytes. */ - pub fn update(&self, data: &[u8]) -> ~[u8] { + pub fn update(&self, data: &[u8]) -> Vec { unsafe { - let mut res = slice::from_elem(data.len() + self.blocksize, 0u8); + let mut res = Vec::from_elem(data.len() + self.blocksize, 0u8); let mut reslen = (data.len() + self.blocksize) as u32; EVP_CipherUpdate( @@ -145,9 +144,9 @@ impl Crypter { /** * Finish crypting. Returns the remaining partial block of output, if any. */ - pub fn final(&self) -> ~[u8] { + pub fn final(&self) -> Vec { unsafe { - let mut res = slice::from_elem(self.blocksize, 0u8); + let mut res = Vec::from_elem(self.blocksize, 0u8); let mut reslen = self.blocksize as c_int; EVP_CipherFinal(self.ctx, @@ -172,24 +171,24 @@ impl Drop for Crypter { * Encrypts data, using the specified crypter type in encrypt mode with the * specified key and iv; returns the resulting (encrypted) data. */ -pub fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] { +pub fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> Vec { let c = Crypter::new(t); c.init(Encrypt, key, iv); let r = c.update(data); let rest = c.final(); - r + rest + r.append(rest.as_slice()) } /** * Decrypts data, using the specified crypter type in decrypt mode with the * specified key and iv; returns the resulting (decrypted) data. */ -pub fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] { +pub fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> Vec { let c = Crypter::new(t); c.init(Decrypt, key, iv); let r = c.update(data); let rest = c.final(); - r + rest + r.append(rest.as_slice()) } #[cfg(test)] @@ -201,24 +200,24 @@ mod tests { #[test] fn test_aes_256_ecb() { let k0 = - ~[ 0x00u8, 0x01u8, 0x02u8, 0x03u8, 0x04u8, 0x05u8, 0x06u8, 0x07u8, + vec!(0x00u8, 0x01u8, 0x02u8, 0x03u8, 0x04u8, 0x05u8, 0x06u8, 0x07u8, 0x08u8, 0x09u8, 0x0au8, 0x0bu8, 0x0cu8, 0x0du8, 0x0eu8, 0x0fu8, 0x10u8, 0x11u8, 0x12u8, 0x13u8, 0x14u8, 0x15u8, 0x16u8, 0x17u8, - 0x18u8, 0x19u8, 0x1au8, 0x1bu8, 0x1cu8, 0x1du8, 0x1eu8, 0x1fu8 ]; + 0x18u8, 0x19u8, 0x1au8, 0x1bu8, 0x1cu8, 0x1du8, 0x1eu8, 0x1fu8); let p0 = - ~[ 0x00u8, 0x11u8, 0x22u8, 0x33u8, 0x44u8, 0x55u8, 0x66u8, 0x77u8, - 0x88u8, 0x99u8, 0xaau8, 0xbbu8, 0xccu8, 0xddu8, 0xeeu8, 0xffu8 ]; + vec!(0x00u8, 0x11u8, 0x22u8, 0x33u8, 0x44u8, 0x55u8, 0x66u8, 0x77u8, + 0x88u8, 0x99u8, 0xaau8, 0xbbu8, 0xccu8, 0xddu8, 0xeeu8, 0xffu8); let c0 = - ~[ 0x8eu8, 0xa2u8, 0xb7u8, 0xcau8, 0x51u8, 0x67u8, 0x45u8, 0xbfu8, - 0xeau8, 0xfcu8, 0x49u8, 0x90u8, 0x4bu8, 0x49u8, 0x60u8, 0x89u8 ]; + vec!(0x8eu8, 0xa2u8, 0xb7u8, 0xcau8, 0x51u8, 0x67u8, 0x45u8, 0xbfu8, + 0xeau8, 0xfcu8, 0x49u8, 0x90u8, 0x4bu8, 0x49u8, 0x60u8, 0x89u8); let c = super::Crypter::new(super::AES_256_ECB); - c.init(super::Encrypt, k0, []); + c.init(super::Encrypt, k0.as_slice(), []); c.pad(false); - let r0 = c.update(p0) + c.final(); + let r0 = c.update(p0.as_slice()).append(c.final().as_slice()); assert!(r0 == c0); - c.init(super::Decrypt, k0, []); + c.init(super::Decrypt, k0.as_slice(), []); c.pad(false); - let p1 = c.update(r0) + c.final(); + let p1 = c.update(r0.as_slice()).append(c.final().as_slice()); assert!(p1 == p0); } @@ -228,12 +227,12 @@ mod tests { let cipher = super::Crypter::new(ciphertype); cipher.init(super::Encrypt, key.from_hex().unwrap(), iv.from_hex().unwrap()); - let expected = ct.from_hex().unwrap(); - let computed = cipher.update(pt.from_hex().unwrap()) + cipher.final(); + let expected = Vec::from_slice(ct.from_hex().unwrap()); + let computed = cipher.update(pt.from_hex().unwrap()).append(cipher.final().as_slice()); if computed != expected { - println!("Computed: {}", computed.to_hex()); - println!("Expected: {}", expected.to_hex()); + println!("Computed: {}", computed.as_slice().to_hex()); + println!("Expected: {}", expected.as_slice().to_hex()); if computed.len() != expected.len() { println!("Lengths differ: {} in computed vs {} expected", computed.len(), expected.len());