Skip to content

Commit

Permalink
Update to rust master
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jan 9, 2015
1 parent 8b67adf commit 9dfeea6
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 98 deletions.
2 changes: 2 additions & 0 deletions openssl-sys/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unstable)]

extern crate "pkg-config" as pkg_config;

use std::os;
Expand Down
12 changes: 6 additions & 6 deletions openssl-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
#![allow(dead_code)]
#![allow(dead_code, unstable)]

extern crate libc;

Expand Down Expand Up @@ -197,12 +197,12 @@ static mut GUARDS: *mut Vec<Option<MutexGuard<'static, ()>>> = 0 as *mut Vec<Opt
extern fn locking_function(mode: c_int, n: c_int, _file: *const c_char,
_line: c_int) {
unsafe {
let mutex = &(*MUTEXES)[n as uint];
let mutex = &(*MUTEXES)[n as usize];

if mode & CRYPTO_LOCK != 0 {
(*GUARDS)[n as uint] = Some(mutex.lock().unwrap());
(*GUARDS)[n as usize] = Some(mutex.lock().unwrap());
} else {
&(*GUARDS)[n as uint].take();
&(*GUARDS)[n as usize].take();
}
}
}
Expand All @@ -216,10 +216,10 @@ pub fn init() {
SSL_load_error_strings();

let num_locks = CRYPTO_num_locks();
let mutexes = box range(0, num_locks).map(|_| MUTEX_INIT).collect::<Vec<_>>();
let mutexes = Box::new(range(0, num_locks).map(|_| MUTEX_INIT).collect::<Vec<_>>());
MUTEXES = mem::transmute(mutexes);
let guards: Box<Vec<Option<MutexGuard<()>>>> =
box range(0, num_locks).map(|_| None).collect();
Box::new(range(0, num_locks).map(|_| None).collect());
GUARDS = mem::transmute(guards);

CRYPTO_set_locking_callback(locking_function);
Expand Down
2 changes: 1 addition & 1 deletion src/asn1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Asn1Time {
}

/// Creates a new time on specified interval in days from now
pub fn days_from_now(days: uint) -> Result<Asn1Time, SslError> {
pub fn days_from_now(days: u32) -> Result<Asn1Time, SslError> {
Asn1Time::new_with_period(days as u64 * 60 * 60 * 24)
}

Expand Down
6 changes: 3 additions & 3 deletions src/bio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl MemBio {
}

impl Reader for MemBio {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
let ret = unsafe {
ffi::BIO_read(self.bio, buf.as_ptr() as *mut c_void,
buf.len() as c_int)
Expand All @@ -81,7 +81,7 @@ impl Reader for MemBio {
};
Err(err)
} else {
Ok(ret as uint)
Ok(ret as usize)
}
}
}
Expand All @@ -92,7 +92,7 @@ impl Writer for MemBio {
ffi::BIO_write(self.bio, buf.as_ptr() as *const c_void,
buf.len() as c_int)
};
if buf.len() != ret as uint {
if buf.len() != ret as usize {
Err(IoError {
kind: OtherIoError,
desc: "MemBio write error",
Expand Down
2 changes: 1 addition & 1 deletion src/bn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl BigNum {
}

pub fn to_vec(&self) -> Vec<u8> {
let size = self.num_bytes() as uint;
let size = self.num_bytes() as usize;
let mut v = Vec::with_capacity(size);
unsafe {
ffi::BN_bn2bin(self.raw(), v.as_mut_ptr());
Expand Down
20 changes: 10 additions & 10 deletions src/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
};
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ffi;

pub struct HMAC {
ctx: ffi::HMAC_CTX,
len: uint,
len: u32,
}

#[allow(non_snake_case)]
Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/crypto/pkcs5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
40 changes: 20 additions & 20 deletions src/crypto/pkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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()
);
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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,
Expand All @@ -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
}
}
Expand All @@ -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,
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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),
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 9dfeea6

Please sign in to comment.