diff options
| author | Alex Crichton <[email protected]> | 2015-01-09 08:07:39 -0800 |
|---|---|---|
| committer | Alex Crichton <[email protected]> | 2015-01-09 08:12:39 -0800 |
| commit | 9dfeea6ca9f8527e8be56c208e21a1a48f22f6cf (patch) | |
| tree | e26eea29b731b0c6df36847923e3a51b5bb048a4 /src/crypto | |
| parent | Merge pull request #139 from vhbit/up-master (diff) | |
| download | rust-openssl-9dfeea6ca9f8527e8be56c208e21a1a48f22f6cf.tar.xz rust-openssl-9dfeea6ca9f8527e8be56c208e21a1a48f22f6cf.zip | |
Update to rust master
Diffstat (limited to 'src/crypto')
| -rw-r--r-- | src/crypto/hash.rs | 20 | ||||
| -rw-r--r-- | src/crypto/hmac.rs | 6 | ||||
| -rw-r--r-- | src/crypto/pkcs5.rs | 26 | ||||
| -rw-r--r-- | src/crypto/pkey.rs | 40 | ||||
| -rw-r--r-- | src/crypto/rand.rs | 6 | ||||
| -rw-r--r-- | src/crypto/symm.rs | 41 |
6 files changed, 70 insertions, 69 deletions
diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs index 1f8a3936..602a7d08 100644 --- a/src/crypto/hash.rs +++ b/src/crypto/hash.rs @@ -16,16 +16,16 @@ pub enum HashType { RIPEMD160 } -pub fn evpmd(t: HashType) -> (*const ffi::EVP_MD, uint) { +pub fn evpmd(t: HashType) -> (*const ffi::EVP_MD, u32) { unsafe { match t { - HashType::MD5 => (ffi::EVP_md5(), 16u), - HashType::SHA1 => (ffi::EVP_sha1(), 20u), - HashType::SHA224 => (ffi::EVP_sha224(), 28u), - HashType::SHA256 => (ffi::EVP_sha256(), 32u), - HashType::SHA384 => (ffi::EVP_sha384(), 48u), - HashType::SHA512 => (ffi::EVP_sha512(), 64u), - HashType::RIPEMD160 => (ffi::EVP_ripemd160(), 20u), + HashType::MD5 => (ffi::EVP_md5(), 16), + HashType::SHA1 => (ffi::EVP_sha1(), 20), + HashType::SHA224 => (ffi::EVP_sha224(), 28), + HashType::SHA256 => (ffi::EVP_sha256(), 32), + HashType::SHA384 => (ffi::EVP_sha384(), 48), + HashType::SHA512 => (ffi::EVP_sha512(), 64), + HashType::RIPEMD160 => (ffi::EVP_ripemd160(), 20), } } } @@ -56,7 +56,7 @@ impl Drop for HasherContext { pub struct Hasher { evp: *const ffi::EVP_MD, ctx: HasherContext, - len: uint, + len: u32, } impl io::Writer for Hasher { @@ -102,7 +102,7 @@ impl Hasher { * initialization and its context for reuse */ pub fn finalize_reuse(self) -> (Vec<u8>, HasherContext) { - let mut res = repeat(0u8).take(self.len).collect::<Vec<_>>(); + let mut res = repeat(0u8).take(self.len as usize).collect::<Vec<_>>(); unsafe { ffi::EVP_DigestFinal_ex(self.ctx.ptr, res.as_mut_ptr(), ptr::null_mut()) }; diff --git a/src/crypto/hmac.rs b/src/crypto/hmac.rs index 6de0b1e7..4a0c3e77 100644 --- a/src/crypto/hmac.rs +++ b/src/crypto/hmac.rs @@ -22,7 +22,7 @@ use ffi; pub struct HMAC { ctx: ffi::HMAC_CTX, - len: uint, + len: u32, } #[allow(non_snake_case)] @@ -53,10 +53,10 @@ impl HMAC { pub fn finalize(&mut self) -> Vec<u8> { unsafe { - let mut res: Vec<u8> = repeat(0).take(self.len).collect(); + let mut res: Vec<u8> = repeat(0).take(self.len as usize).collect(); let mut outlen = 0; ffi::HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen); - assert!(self.len == outlen as uint); + assert!(self.len == outlen as u32); res } } diff --git a/src/crypto/pkcs5.rs b/src/crypto/pkcs5.rs index 6efd23ff..b101c3ed 100644 --- a/src/crypto/pkcs5.rs +++ b/src/crypto/pkcs5.rs @@ -2,7 +2,7 @@ use libc::c_int; use ffi; /// 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) -> Vec<u8> { +pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: usize, keylen: usize) -> Vec<u8> { unsafe { assert!(iter >= 1); assert!(keylen >= 1); @@ -35,8 +35,8 @@ mod tests { super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), - 1u, - 20u + 1, + 20 ), vec!( 0x0c_u8, 0x60_u8, 0xc8_u8, 0x0f_u8, 0x96_u8, 0x1f_u8, 0x0e_u8, @@ -49,8 +49,8 @@ mod tests { super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), - 2u, - 20u + 2, + 20 ), vec!( 0xea_u8, 0x6c_u8, 0x01_u8, 0x4d_u8, 0xc7_u8, 0x2d_u8, 0x6f_u8, @@ -63,8 +63,8 @@ mod tests { super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), - 4096u, - 20u + 4096, + 20 ), vec!( 0x4b_u8, 0x00_u8, 0x79_u8, 0x01_u8, 0xb7_u8, 0x65_u8, 0x48_u8, @@ -77,8 +77,8 @@ mod tests { super::pbkdf2_hmac_sha1( "password", "salt".as_bytes(), - 16777216u, - 20u + 16777216, + 20 ), vec!( 0xee_u8, 0xfe_u8, 0x3d_u8, 0x61_u8, 0xcd_u8, 0x4d_u8, 0xa4_u8, @@ -91,8 +91,8 @@ mod tests { super::pbkdf2_hmac_sha1( "passwordPASSWORDpassword", "saltSALTsaltSALTsaltSALTsaltSALTsalt".as_bytes(), - 4096u, - 25u + 4096, + 25 ), vec!( 0x3d_u8, 0x2e_u8, 0xec_u8, 0x4f_u8, 0xe4_u8, 0x1c_u8, 0x84_u8, @@ -106,8 +106,8 @@ mod tests { super::pbkdf2_hmac_sha1( "pass\x00word", "sa\x00lt".as_bytes(), - 4096u, - 16u + 4096, + 16 ), vec!( 0x56_u8, 0xfa_u8, 0x6a_u8, 0xa7_u8, 0x55_u8, 0x48_u8, 0x09_u8, diff --git a/src/crypto/pkey.rs b/src/crypto/pkey.rs index 2264e192..14144dc0 100644 --- a/src/crypto/pkey.rs +++ b/src/crypto/pkey.rs @@ -72,11 +72,11 @@ impl PKey { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); let len = f(rsa, ptr::null()); if len < 0 as c_int { return vec!(); } - let mut s = repeat(0u8).take(len as uint).collect::<Vec<_>>(); + let mut s = repeat(0u8).take(len as usize).collect::<Vec<_>>(); let r = f(rsa, &s.as_mut_ptr()); - s.truncate(r as uint); + s.truncate(r as usize); s } } @@ -89,11 +89,11 @@ impl PKey { } } - pub fn gen(&mut self, keysz: uint) { + pub fn gen(&mut self, keysz: usize) { unsafe { let rsa = ffi::RSA_generate_key( keysz as c_uint, - 65537u as c_uint, + 65537 as c_uint, ptr::null(), ptr::null() ); @@ -155,9 +155,9 @@ impl PKey { /** * Returns the size of the public key modulus. */ - pub fn size(&self) -> uint { + pub fn size(&self) -> usize { unsafe { - ffi::RSA_size(ffi::EVP_PKEY_get1_RSA(self.evp)) as uint + ffi::RSA_size(ffi::EVP_PKEY_get1_RSA(self.evp)) as usize } } @@ -193,13 +193,13 @@ impl PKey { * Returns the maximum amount of data that can be encrypted by an encrypt() * call. */ - pub fn max_data(&self) -> uint { + pub fn max_data(&self) -> usize { unsafe { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); let len = ffi::RSA_size(rsa); // 41 comes from RSA_public_encrypt(3) for OAEP - len as uint - 41u + len as usize - 41 } } @@ -210,7 +210,7 @@ impl PKey { assert!(s.len() < self.max_data()); - let mut r = repeat(0u8).take(len as uint + 1).collect::<Vec<_>>(); + let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>(); let rv = ffi::RSA_public_encrypt( s.len() as c_uint, @@ -222,7 +222,7 @@ impl PKey { if rv < 0 as c_int { vec!() } else { - r.truncate(rv as uint); + r.truncate(rv as usize); r } } @@ -235,7 +235,7 @@ impl PKey { assert_eq!(s.len() as c_uint, ffi::RSA_size(rsa)); - let mut r = repeat(0u8).take(len as uint + 1).collect::<Vec<_>>(); + let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>(); let rv = ffi::RSA_private_decrypt( s.len() as c_uint, @@ -247,7 +247,7 @@ impl PKey { if rv < 0 as c_int { vec!() } else { - r.truncate(rv as uint); + r.truncate(rv as usize); r } } @@ -280,7 +280,7 @@ impl PKey { unsafe { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); let mut len = ffi::RSA_size(rsa); - let mut r = repeat(0u8).take(len as uint + 1).collect::<Vec<_>>(); + let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>(); let rv = ffi::RSA_sign( openssl_hash_nid(hash), @@ -293,7 +293,7 @@ impl PKey { if rv < 0 as c_int { vec!() } else { - r.truncate(len as uint); + r.truncate(len as usize); r } } @@ -337,7 +337,7 @@ mod tests { fn test_gen_pub() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); - k0.gen(512u); + k0.gen(512); k1.load_pub(k0.save_pub().as_slice()); assert_eq!(k0.save_pub(), k1.save_pub()); assert_eq!(k0.size(), k1.size()); @@ -355,7 +355,7 @@ mod tests { fn test_gen_priv() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); - k0.gen(512u); + k0.gen(512); k1.load_priv(k0.save_priv().as_slice()); assert_eq!(k0.save_priv(), k1.save_priv()); assert_eq!(k0.size(), k1.size()); @@ -374,7 +374,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); - k0.gen(512u); + k0.gen(512); k1.load_pub(k0.save_pub().as_slice()); let emsg = k1.encrypt(msg.as_slice()); let dmsg = k0.decrypt(emsg.as_slice()); @@ -386,7 +386,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); - k0.gen(512u); + k0.gen(512); k1.load_pub(k0.save_pub().as_slice()); let emsg = k1.encrypt_with_padding(msg.as_slice(), super::EncryptionPadding::PKCS1v15); let dmsg = k0.decrypt_with_padding(emsg.as_slice(), super::EncryptionPadding::PKCS1v15); @@ -398,7 +398,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); - k0.gen(512u); + k0.gen(512); 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()); @@ -410,7 +410,7 @@ mod tests { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); let msg = vec!(0xdeu8, 0xadu8, 0xd0u8, 0x0du8); - k0.gen(512u); + k0.gen(512); k1.load_pub(k0.save_pub().as_slice()); let sig = k0.sign_with_hash(msg.as_slice(), MD5); diff --git a/src/crypto/rand.rs b/src/crypto/rand.rs index 92d38f2c..dc338a3d 100644 --- a/src/crypto/rand.rs +++ b/src/crypto/rand.rs @@ -1,7 +1,7 @@ use libc::c_int; use ffi; -pub fn rand_bytes(len: uint) -> Vec<u8> { +pub fn rand_bytes(len: usize) -> Vec<u8> { unsafe { let mut out = Vec::with_capacity(len); @@ -21,7 +21,7 @@ mod tests { #[test] fn test_rand_bytes() { - let bytes = rand_bytes(32u); - println!("{}", bytes); + let bytes = rand_bytes(32); + println!("{:?}", bytes); } } diff --git a/src/crypto/symm.rs b/src/crypto/symm.rs index 9478bc4a..ccff62bb 100644 --- a/src/crypto/symm.rs +++ b/src/crypto/symm.rs @@ -31,24 +31,24 @@ pub enum Type { RC4_128, } -fn evpc(t: Type) -> (*const ffi::EVP_CIPHER, uint, uint) { +fn evpc(t: Type) -> (*const ffi::EVP_CIPHER, u32, u32) { unsafe { match t { - Type::AES_128_ECB => (ffi::EVP_aes_128_ecb(), 16u, 16u), - Type::AES_128_CBC => (ffi::EVP_aes_128_cbc(), 16u, 16u), + Type::AES_128_ECB => (ffi::EVP_aes_128_ecb(), 16, 16), + Type::AES_128_CBC => (ffi::EVP_aes_128_cbc(), 16, 16), #[cfg(feature = "aes_xts")] - Type::AES_128_XTS => (ffi::EVP_aes_128_xts(), 32u, 16u), - // AES_128_CTR => (EVP_aes_128_ctr(), 16u, 0u), - //AES_128_GCM => (EVP_aes_128_gcm(), 16u, 16u), + Type::AES_128_XTS => (ffi::EVP_aes_128_xts(), 32, 16), + // AES_128_CTR => (EVP_aes_128_ctr(), 16, 0), + //AES_128_GCM => (EVP_aes_128_gcm(), 16, 16), - Type::AES_256_ECB => (ffi::EVP_aes_256_ecb(), 32u, 16u), - Type::AES_256_CBC => (ffi::EVP_aes_256_cbc(), 32u, 16u), + Type::AES_256_ECB => (ffi::EVP_aes_256_ecb(), 32, 16), + Type::AES_256_CBC => (ffi::EVP_aes_256_cbc(), 32, 16), #[cfg(feature = "aes_xts")] - Type::AES_256_XTS => (ffi::EVP_aes_256_xts(), 64u, 16u), - // AES_256_CTR => (EVP_aes_256_ctr(), 32u, 0u), - //AES_256_GCM => (EVP_aes_256_gcm(), 32u, 16u), + Type::AES_256_XTS => (ffi::EVP_aes_256_xts(), 64, 16), + // AES_256_CTR => (EVP_aes_256_ctr(), 32, 0), + //AES_256_GCM => (EVP_aes_256_gcm(), 32, 16), - Type::RC4_128 => (ffi::EVP_rc4(), 16u, 0u), + Type::RC4_128 => (ffi::EVP_rc4(), 16, 0), } } } @@ -57,8 +57,8 @@ fn evpc(t: Type) -> (*const ffi::EVP_CIPHER, uint, uint) { pub struct Crypter { evp: *const ffi::EVP_CIPHER, ctx: *mut ffi::EVP_CIPHER_CTX, - keylen: uint, - blocksize: uint + keylen: u32, + blocksize: u32, } impl Crypter { @@ -92,7 +92,7 @@ impl Crypter { Mode::Encrypt => 1 as c_int, Mode::Decrypt => 0 as c_int, }; - assert_eq!(key.len(), self.keylen); + assert_eq!(key.len(), self.keylen as usize); ffi::EVP_CipherInit( self.ctx, @@ -110,8 +110,9 @@ impl Crypter { */ pub fn update(&self, data: &[u8]) -> Vec<u8> { unsafe { - let mut res = repeat(0u8).take(data.len() + self.blocksize).collect::<Vec<_>>(); - let mut reslen = (data.len() + self.blocksize) as u32; + let sum = data.len() + (self.blocksize as usize); + let mut res = repeat(0u8).take(sum).collect::<Vec<_>>(); + let mut reslen = sum as u32; ffi::EVP_CipherUpdate( self.ctx, @@ -121,7 +122,7 @@ impl Crypter { data.len() as c_int ); - res.truncate(reslen as uint); + res.truncate(reslen as usize); res } } @@ -131,14 +132,14 @@ impl Crypter { */ pub fn finalize(&self) -> Vec<u8> { unsafe { - let mut res = repeat(0u8).take(self.blocksize).collect::<Vec<_>>(); + let mut res = repeat(0u8).take(self.blocksize as usize).collect::<Vec<_>>(); let mut reslen = self.blocksize as c_int; ffi::EVP_CipherFinal(self.ctx, res.as_mut_ptr(), &mut reslen); - res.truncate(reslen as uint); + res.truncate(reslen as usize); res } } |