From ec7474c895f967ed67a33fd69708af68745fe078 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 22 Aug 2013 20:31:02 -0700 Subject: [PATCH 1/8] Update to latest rust master (0.8-pre 063a005) --- hash.rs | 5 +++++ hmac.rs | 3 +++ pkcs5.rs | 6 +++--- pkey.rs | 17 ++++++++++++++--- rand.rs | 1 + symm.rs | 7 +++++++ 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/hash.rs b/hash.rs index 2a43cbd2..61f9c80f 100644 --- a/hash.rs +++ b/hash.rs @@ -41,6 +41,7 @@ mod libcrypto { } pub fn evpmd(t: HashType) -> (EVP_MD, uint) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { match t { MD5 => (libcrypto::EVP_md5(), 16u), @@ -61,6 +62,7 @@ pub struct Hasher { impl Hasher { pub fn new(ht: HashType) -> Hasher { + #[fixed_stack_segment]; #[inline(never)]; let ctx = unsafe { libcrypto::EVP_MD_CTX_create() }; let (evp, mdlen) = evpmd(ht); unsafe { @@ -72,6 +74,7 @@ impl Hasher { /// Update this hasher with more input bytes pub fn update(&self, data: &[u8]) { + #[fixed_stack_segment]; #[inline(never)]; do data.as_imm_buf |pdata, len| { unsafe { libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) @@ -84,6 +87,7 @@ impl Hasher { * initialization */ pub fn final(&self) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; let mut res = vec::from_elem(self.len, 0u8); do res.as_mut_buf |pres, _len| { unsafe { @@ -96,6 +100,7 @@ impl Hasher { impl Drop for Hasher { fn drop(&self) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_MD_CTX_destroy(self.ctx); } diff --git a/hmac.rs b/hmac.rs index 1e71ed1b..638e35c9 100644 --- a/hmac.rs +++ b/hmac.rs @@ -43,6 +43,7 @@ pub struct HMAC { } pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let (evp, mdlen) = evpmd(ht); @@ -67,6 +68,7 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { impl HMAC { pub fn update(&mut self, data: &[u8]) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { do data.as_imm_buf |pdata, len| { HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) @@ -75,6 +77,7 @@ impl HMAC { } pub fn final(&mut self) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let mut res = vec::from_elem(self.len, 0u8); let mut outlen: libc::c_uint = 0; diff --git a/pkcs5.rs b/pkcs5.rs index 400d3207..f9e2a48d 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -13,9 +13,9 @@ mod libcrypto { } } -#[doc = " -Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm. -"] +/// Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm. +#[fixed_stack_segment] +#[inline(never)] pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> ~[u8] { assert!(iter >= 1u); diff --git a/pkey.rs b/pkey.rs index 9b97721b..c6aac6d4 100644 --- a/pkey.rs +++ b/pkey.rs @@ -47,7 +47,7 @@ enum Parts { Both } -#[doc = "Represents a role an asymmetric key might be appropriate for."] +/// Represents a role an asymmetric key might be appropriate for. pub enum Role { Encrypt, Decrypt, @@ -55,7 +55,7 @@ pub enum Role { Verify } -#[doc = "Type of encryption padding to use."] +/// Type of encryption padding to use. pub enum EncryptionPadding { OAEP, PKCS1v15 @@ -84,9 +84,10 @@ pub struct PKey { priv parts: Parts, } -///Represents a public key, optionally with a private key attached. +/// Represents a public key, optionally with a private key attached. impl PKey { pub fn new() -> PKey { + #[fixed_stack_segment]; #[inline(never)]; PKey { evp: unsafe { libcrypto::EVP_PKEY_new() }, parts: Neither, @@ -94,6 +95,7 @@ impl PKey { } fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let len = f(self.evp, ptr::null()); if len < 0 as c_int { return ~[]; } @@ -109,6 +111,7 @@ impl PKey { } fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) { + #[fixed_stack_segment]; #[inline(never)]; do s.as_imm_buf |ps, len| { let evp = ptr::null(); unsafe { @@ -119,6 +122,7 @@ impl PKey { } pub fn gen(&mut self, keysz: uint) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::RSA_generate_key( keysz as c_uint, @@ -172,6 +176,7 @@ impl PKey { * Returns the size of the public key modulus. */ pub fn size(&self) -> uint { + #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::RSA_size(libcrypto::EVP_PKEY_get1_RSA(self.evp)) as uint } @@ -210,6 +215,7 @@ impl PKey { * call. */ pub fn max_data(&self) -> uint { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let len = libcrypto::RSA_size(rsa); @@ -220,6 +226,7 @@ impl PKey { } pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let len = libcrypto::RSA_size(rsa); @@ -249,6 +256,7 @@ impl PKey { } pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let len = libcrypto::RSA_size(rsa); @@ -302,6 +310,7 @@ 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] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let mut len = libcrypto::RSA_size(rsa); @@ -329,6 +338,7 @@ impl PKey { } pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); @@ -352,6 +362,7 @@ impl PKey { impl Drop for PKey { fn drop(&self) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_PKEY_free(self.evp); } diff --git a/rand.rs b/rand.rs index eedac4ab..dd6ee8a3 100644 --- a/rand.rs +++ b/rand.rs @@ -11,6 +11,7 @@ mod libcrypto { } pub fn rand_bytes(len: uint) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; let mut out = vec::with_capacity(len); do out.as_mut_buf |out_buf, len| { diff --git a/symm.rs b/symm.rs index bc28624a..b9353fcc 100644 --- a/symm.rs +++ b/symm.rs @@ -59,6 +59,7 @@ pub enum Type { } fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { match t { AES_128_ECB => (libcrypto::EVP_aes_128_ecb(), 16u, 16u), @@ -86,6 +87,7 @@ pub struct Crypter { impl Crypter { pub fn new(t: Type) -> Crypter { + #[fixed_stack_segment]; #[inline(never)]; let ctx = unsafe { libcrypto::EVP_CIPHER_CTX_new() }; let (evp, keylen, blocksz) = evpc(t); Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz } @@ -96,6 +98,7 @@ impl Crypter { * data encrypted must be a multiple of block size. */ pub fn pad(&self, padding: bool) { + #[fixed_stack_segment]; #[inline(never)]; if self.blocksize > 0 { unsafe { let v = if padding { 1 } else { 0 } as c_int; @@ -108,6 +111,7 @@ impl Crypter { * Initializes this crypter. */ pub fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let mode = match mode { Encrypt => 1 as c_int, @@ -134,6 +138,7 @@ impl Crypter { * encrypted or decrypted bytes. */ pub fn update(&self, data: &[u8]) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { do data.as_imm_buf |pdata, len| { let mut res = vec::from_elem(len + self.blocksize, 0u8); @@ -162,6 +167,7 @@ impl Crypter { * Finish crypting. Returns the remaining partial block of output, if any. */ pub fn final(&self) -> ~[u8] { + #[fixed_stack_segment]; #[inline(never)]; unsafe { let mut res = vec::from_elem(self.blocksize, 0u8); @@ -179,6 +185,7 @@ impl Crypter { impl Drop for Crypter { fn drop(&self) { + #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_CIPHER_CTX_free(self.ctx); } From 76a3b83d27a06ff79ca8bd8a652b3476ae3b2c9e Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Wed, 18 Sep 2013 13:51:05 -0700 Subject: [PATCH 2/8] Update to latest rust master (0.8-pre d2b0b11) --- hash.rs | 2 +- pkey.rs | 2 +- symm.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hash.rs b/hash.rs index 61f9c80f..3ace9751 100644 --- a/hash.rs +++ b/hash.rs @@ -99,7 +99,7 @@ impl Hasher { } impl Drop for Hasher { - fn drop(&self) { + fn drop(&mut self) { #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_MD_CTX_destroy(self.ctx); diff --git a/pkey.rs b/pkey.rs index c6aac6d4..ce8bed91 100644 --- a/pkey.rs +++ b/pkey.rs @@ -361,7 +361,7 @@ impl PKey { } impl Drop for PKey { - fn drop(&self) { + fn drop(&mut self) { #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_PKEY_free(self.evp); diff --git a/symm.rs b/symm.rs index b9353fcc..5f91909f 100644 --- a/symm.rs +++ b/symm.rs @@ -184,7 +184,7 @@ impl Crypter { } impl Drop for Crypter { - fn drop(&self) { + fn drop(&mut self) { #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_CIPHER_CTX_free(self.ctx); From 25e18fab13879348ee440aaf525b4f277b3c06a5 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sat, 19 Oct 2013 17:47:49 -0700 Subject: [PATCH 3/8] Update to latest rust master (0.9-pre 69e46f3) --- crypto.rs | 2 ++ hash.rs | 24 ++++++++++++------------ hmac.rs | 2 +- pkcs5.rs | 8 ++++---- pkey.rs | 36 ++++++++++++++++++------------------ rand.rs | 4 ++-- symm.rs | 40 ++++++++++++++++++++-------------------- 7 files changed, 59 insertions(+), 57 deletions(-) diff --git a/crypto.rs b/crypto.rs index 1686a715..615e8b43 100644 --- a/crypto.rs +++ b/crypto.rs @@ -20,6 +20,8 @@ uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")]; #[crate_type = "lib"]; +#[feature(globs)]; + pub mod hash; pub mod hex; pub mod hmac; diff --git a/hash.rs b/hash.rs index 3ace9751..2f4e8a05 100644 --- a/hash.rs +++ b/hash.rs @@ -24,19 +24,19 @@ mod libcrypto { #[link_args = "-lcrypto"] extern { - fn EVP_MD_CTX_create() -> EVP_MD_CTX; - fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); + pub fn EVP_MD_CTX_create() -> EVP_MD_CTX; + pub fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); - fn EVP_md5() -> EVP_MD; - fn EVP_sha1() -> EVP_MD; - fn EVP_sha224() -> EVP_MD; - fn EVP_sha256() -> EVP_MD; - fn EVP_sha384() -> EVP_MD; - fn EVP_sha512() -> EVP_MD; + pub fn EVP_md5() -> EVP_MD; + pub fn EVP_sha1() -> EVP_MD; + pub fn EVP_sha224() -> EVP_MD; + pub fn EVP_sha256() -> EVP_MD; + pub fn EVP_sha384() -> EVP_MD; + pub fn EVP_sha512() -> EVP_MD; - fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD); - fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); - fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32); + pub fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD); + pub fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); + pub fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32); } } @@ -139,7 +139,7 @@ mod tests { let calced = calced_raw.to_hex(); if calced != hashtest.expected_output { - println(fmt!("Test failed - %s != %s", calced, hashtest.expected_output)); + println!("Test failed - {} != {}", calced, hashtest.expected_output); } assert!(calced == hashtest.expected_output); diff --git a/hmac.rs b/hmac.rs index 638e35c9..de93b850 100644 --- a/hmac.rs +++ b/hmac.rs @@ -95,5 +95,5 @@ fn main() { h.update([00u8]); - println(fmt!("%?", h.final())) + println!("{:?}", h.final()) } diff --git a/pkcs5.rs b/pkcs5.rs index f9e2a48d..1be74af9 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -6,10 +6,10 @@ mod libcrypto { #[link_args = "-lcrypto"] 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; + pub 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; } } diff --git a/pkey.rs b/pkey.rs index ce8bed91..6dab8f05 100644 --- a/pkey.rs +++ b/pkey.rs @@ -17,27 +17,27 @@ mod libcrypto { #[link_args = "-lcrypto"] extern { - fn EVP_PKEY_new() -> *EVP_PKEY; - fn EVP_PKEY_free(k: *EVP_PKEY); - fn EVP_PKEY_assign(pkey: *EVP_PKEY, typ: c_int, key: *c_char) -> c_int; - fn EVP_PKEY_get1_RSA(k: *EVP_PKEY) -> *RSA; + pub fn EVP_PKEY_new() -> *EVP_PKEY; + pub fn EVP_PKEY_free(k: *EVP_PKEY); + pub fn EVP_PKEY_assign(pkey: *EVP_PKEY, typ: c_int, key: *c_char) -> c_int; + pub fn EVP_PKEY_get1_RSA(k: *EVP_PKEY) -> *RSA; - fn i2d_PublicKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; - fn d2i_PublicKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; - fn i2d_PrivateKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; - fn d2i_PrivateKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; + pub fn i2d_PublicKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; + pub fn d2i_PublicKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; + pub fn i2d_PrivateKey(k: *EVP_PKEY, buf: **mut u8) -> c_int; + pub fn d2i_PrivateKey(t: c_int, k: **EVP_PKEY, buf: **u8, len: c_uint) -> *EVP_PKEY; - fn RSA_generate_key(modsz: c_uint, e: c_uint, cb: *u8, cbarg: *u8) -> *RSA; - fn RSA_size(k: *RSA) -> c_uint; + pub fn RSA_generate_key(modsz: c_uint, e: c_uint, cb: *u8, cbarg: *u8) -> *RSA; + pub 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; - fn RSA_private_decrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, - pad: c_int) -> c_int; - fn RSA_sign(t: c_int, m: *u8, mlen: c_uint, sig: *mut u8, siglen: *mut 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; + pub fn RSA_public_encrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, + pad: c_int) -> c_int; + pub fn RSA_private_decrypt(flen: c_uint, from: *u8, to: *mut u8, k: *RSA, + pad: c_int) -> c_int; + pub fn RSA_sign(t: c_int, m: *u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint, + k: *RSA) -> c_int; + pub fn RSA_verify(t: c_int, m: *u8, mlen: c_uint, sig: *u8, siglen: c_uint, + k: *RSA) -> c_int; } } diff --git a/rand.rs b/rand.rs index dd6ee8a3..96473ea4 100644 --- a/rand.rs +++ b/rand.rs @@ -6,7 +6,7 @@ mod libcrypto { #[link_args = "-lcrypto"] extern { - fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; + pub fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; } } @@ -31,6 +31,6 @@ mod tests { #[test] fn test_rand_bytes() { let bytes = rand_bytes(32u); - println(fmt!("%?", bytes)); + println!("{:?}", bytes); } } diff --git a/symm.rs b/symm.rs index 5f91909f..3649dea8 100644 --- a/symm.rs +++ b/symm.rs @@ -14,27 +14,27 @@ mod libcrypto { extern { #[link_args = "-lcrypto"] - fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX; - fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int); - fn EVP_CIPHER_CTX_free(ctx: EVP_CIPHER_CTX); + pub fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX; + pub fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int); + pub fn EVP_CIPHER_CTX_free(ctx: EVP_CIPHER_CTX); - fn EVP_aes_128_ecb() -> EVP_CIPHER; - fn EVP_aes_128_cbc() -> EVP_CIPHER; - // fn EVP_aes_128_ctr() -> EVP_CIPHER; - // fn EVP_aes_128_gcm() -> EVP_CIPHER; + pub fn EVP_aes_128_ecb() -> EVP_CIPHER; + pub fn EVP_aes_128_cbc() -> EVP_CIPHER; + // pub fn EVP_aes_128_ctr() -> EVP_CIPHER; + // pub fn EVP_aes_128_gcm() -> EVP_CIPHER; - fn EVP_aes_256_ecb() -> EVP_CIPHER; - fn EVP_aes_256_cbc() -> EVP_CIPHER; - // fn EVP_aes_256_ctr() -> EVP_CIPHER; - // fn EVP_aes_256_gcm() -> EVP_CIPHER; + pub fn EVP_aes_256_ecb() -> EVP_CIPHER; + pub fn EVP_aes_256_cbc() -> EVP_CIPHER; + // pub fn EVP_aes_256_ctr() -> EVP_CIPHER; + // pub fn EVP_aes_256_gcm() -> EVP_CIPHER; - fn EVP_rc4() -> EVP_CIPHER; + pub fn EVP_rc4() -> EVP_CIPHER; - fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER, + pub fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER, key: *u8, iv: *u8, mode: c_int); - fn EVP_CipherUpdate(ctx: EVP_CIPHER_CTX, outbuf: *mut u8, + pub 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); + pub fn EVP_CipherFinal(ctx: EVP_CIPHER_CTX, res: *mut u8, len: &mut c_int); } } @@ -258,13 +258,13 @@ mod tests { let computed = cipher.update(pt.from_hex()) + cipher.final(); if computed != expected { - println(fmt!("Computed: %s", computed.to_hex())); - println(fmt!("Expected: %s", expected.to_hex())); + println!("Computed: {}", computed.to_hex()); + println!("Expected: {}", expected.to_hex()); if computed.len() != expected.len() { - println(fmt!("Lengths differ: %u in computed vs %u expected", - computed.len(), expected.len())); + println!("Lengths differ: {} in computed vs {} expected", + computed.len(), expected.len()); } - fail!(~"test failure"); + fail!("test failure"); } } From 9ea9c195e2c8f01616363a24385894e23cd8e813 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Tue, 26 Nov 2013 01:14:38 -0800 Subject: [PATCH 4/8] Update for latest master (0.9-pre b42c438) --- crypto.rs | 1 + hash.rs | 5 ----- hmac.rs | 4 ---- pkcs5.rs | 2 -- pkey.rs | 11 ----------- rand.rs | 1 - symm.rs | 7 ------- 7 files changed, 1 insertion(+), 30 deletions(-) diff --git a/crypto.rs b/crypto.rs index 615e8b43..7ed4bfd9 100644 --- a/crypto.rs +++ b/crypto.rs @@ -16,6 +16,7 @@ */ #[link(name = "crypto", + package_id = "crypto", vers = "0.3", uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")]; #[crate_type = "lib"]; diff --git a/hash.rs b/hash.rs index 2f4e8a05..139ae4ca 100644 --- a/hash.rs +++ b/hash.rs @@ -41,7 +41,6 @@ mod libcrypto { } pub fn evpmd(t: HashType) -> (EVP_MD, uint) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { match t { MD5 => (libcrypto::EVP_md5(), 16u), @@ -62,7 +61,6 @@ pub struct Hasher { impl Hasher { pub fn new(ht: HashType) -> Hasher { - #[fixed_stack_segment]; #[inline(never)]; let ctx = unsafe { libcrypto::EVP_MD_CTX_create() }; let (evp, mdlen) = evpmd(ht); unsafe { @@ -74,7 +72,6 @@ impl Hasher { /// Update this hasher with more input bytes pub fn update(&self, data: &[u8]) { - #[fixed_stack_segment]; #[inline(never)]; do data.as_imm_buf |pdata, len| { unsafe { libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) @@ -87,7 +84,6 @@ impl Hasher { * initialization */ pub fn final(&self) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; let mut res = vec::from_elem(self.len, 0u8); do res.as_mut_buf |pres, _len| { unsafe { @@ -100,7 +96,6 @@ impl Hasher { impl Drop for Hasher { fn drop(&mut self) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_MD_CTX_destroy(self.ctx); } diff --git a/hmac.rs b/hmac.rs index de93b850..510c8b2d 100644 --- a/hmac.rs +++ b/hmac.rs @@ -28,7 +28,6 @@ pub struct HMAC_CTX { } #[link_args = "-lcrypto"] -#[abi = "cdecl"] extern { fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD); @@ -43,7 +42,6 @@ pub struct HMAC { } pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let (evp, mdlen) = evpmd(ht); @@ -68,7 +66,6 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { impl HMAC { pub fn update(&mut self, data: &[u8]) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { do data.as_imm_buf |pdata, len| { HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) @@ -77,7 +74,6 @@ impl HMAC { } pub fn final(&mut self) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let mut res = vec::from_elem(self.len, 0u8); let mut outlen: libc::c_uint = 0; diff --git a/pkcs5.rs b/pkcs5.rs index 1be74af9..3c5ba23b 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -14,8 +14,6 @@ mod libcrypto { } /// Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm. -#[fixed_stack_segment] -#[inline(never)] pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> ~[u8] { assert!(iter >= 1u); diff --git a/pkey.rs b/pkey.rs index 6dab8f05..d59e05f1 100644 --- a/pkey.rs +++ b/pkey.rs @@ -87,7 +87,6 @@ pub struct PKey { /// Represents a public key, optionally with a private key attached. impl PKey { pub fn new() -> PKey { - #[fixed_stack_segment]; #[inline(never)]; PKey { evp: unsafe { libcrypto::EVP_PKEY_new() }, parts: Neither, @@ -95,7 +94,6 @@ impl PKey { } fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let len = f(self.evp, ptr::null()); if len < 0 as c_int { return ~[]; } @@ -111,7 +109,6 @@ impl PKey { } fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) { - #[fixed_stack_segment]; #[inline(never)]; do s.as_imm_buf |ps, len| { let evp = ptr::null(); unsafe { @@ -122,7 +119,6 @@ impl PKey { } pub fn gen(&mut self, keysz: uint) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::RSA_generate_key( keysz as c_uint, @@ -176,7 +172,6 @@ impl PKey { * Returns the size of the public key modulus. */ pub fn size(&self) -> uint { - #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::RSA_size(libcrypto::EVP_PKEY_get1_RSA(self.evp)) as uint } @@ -215,7 +210,6 @@ impl PKey { * call. */ pub fn max_data(&self) -> uint { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let len = libcrypto::RSA_size(rsa); @@ -226,7 +220,6 @@ impl PKey { } pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let len = libcrypto::RSA_size(rsa); @@ -256,7 +249,6 @@ impl PKey { } pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let len = libcrypto::RSA_size(rsa); @@ -310,7 +302,6 @@ 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] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let mut len = libcrypto::RSA_size(rsa); @@ -338,7 +329,6 @@ impl PKey { } pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); @@ -362,7 +352,6 @@ impl PKey { impl Drop for PKey { fn drop(&mut self) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_PKEY_free(self.evp); } diff --git a/rand.rs b/rand.rs index 96473ea4..07a39184 100644 --- a/rand.rs +++ b/rand.rs @@ -11,7 +11,6 @@ mod libcrypto { } pub fn rand_bytes(len: uint) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; let mut out = vec::with_capacity(len); do out.as_mut_buf |out_buf, len| { diff --git a/symm.rs b/symm.rs index 3649dea8..edc408cb 100644 --- a/symm.rs +++ b/symm.rs @@ -59,7 +59,6 @@ pub enum Type { } fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { match t { AES_128_ECB => (libcrypto::EVP_aes_128_ecb(), 16u, 16u), @@ -87,7 +86,6 @@ pub struct Crypter { impl Crypter { pub fn new(t: Type) -> Crypter { - #[fixed_stack_segment]; #[inline(never)]; let ctx = unsafe { libcrypto::EVP_CIPHER_CTX_new() }; let (evp, keylen, blocksz) = evpc(t); Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz } @@ -98,7 +96,6 @@ impl Crypter { * data encrypted must be a multiple of block size. */ pub fn pad(&self, padding: bool) { - #[fixed_stack_segment]; #[inline(never)]; if self.blocksize > 0 { unsafe { let v = if padding { 1 } else { 0 } as c_int; @@ -111,7 +108,6 @@ impl Crypter { * Initializes this crypter. */ pub fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let mode = match mode { Encrypt => 1 as c_int, @@ -138,7 +134,6 @@ impl Crypter { * encrypted or decrypted bytes. */ pub fn update(&self, data: &[u8]) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { do data.as_imm_buf |pdata, len| { let mut res = vec::from_elem(len + self.blocksize, 0u8); @@ -167,7 +162,6 @@ impl Crypter { * Finish crypting. Returns the remaining partial block of output, if any. */ pub fn final(&self) -> ~[u8] { - #[fixed_stack_segment]; #[inline(never)]; unsafe { let mut res = vec::from_elem(self.blocksize, 0u8); @@ -185,7 +179,6 @@ impl Crypter { impl Drop for Crypter { fn drop(&mut self) { - #[fixed_stack_segment]; #[inline(never)]; unsafe { libcrypto::EVP_CIPHER_CTX_free(self.ctx); } From 7c05f58ac7aba88fca01d2091f7c05d59f2a6dd9 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 28 Nov 2013 18:35:04 -0800 Subject: [PATCH 5/8] Update for latest master (0.9-pre 90d06ec) --- hash.rs | 8 ++++---- hex.rs | 4 ++-- hmac.rs | 8 ++++---- pkcs5.rs | 12 ++++++------ pkey.rs | 40 ++++++++++++++++++++-------------------- rand.rs | 4 ++-- symm.rs | 20 ++++++++++---------- 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/hash.rs b/hash.rs index 139ae4ca..d7b99264 100644 --- a/hash.rs +++ b/hash.rs @@ -72,11 +72,11 @@ impl Hasher { /// Update this hasher with more input bytes pub fn update(&self, data: &[u8]) { - do data.as_imm_buf |pdata, len| { + data.as_imm_buf(|pdata, len| { unsafe { libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) } - } + }); } /** @@ -85,11 +85,11 @@ impl Hasher { */ pub fn final(&self) -> ~[u8] { let mut res = vec::from_elem(self.len, 0u8); - do res.as_mut_buf |pres, _len| { + res.as_mut_buf(|pres, _len| { unsafe { libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); } - } + }); res } } diff --git a/hex.rs b/hex.rs index b479ea18..8a91c071 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 = "0123456789ABCDEF".iter().collect::<~[char]>(); + let chars = "0123456789ABCDEF".chars().collect::<~[char]>(); let mut s = ~""; @@ -50,7 +50,7 @@ impl<'self> FromHex for &'self str { fn from_hex(&self) -> ~[u8] { let mut vec = vec::with_capacity(self.len() / 2); - for (i,c) in self.iter().enumerate() { + for (i,c) in self.chars().enumerate() { let nibble = if c >= '0' && c <= '9' { (c as u8) - 0x30 } else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) } diff --git a/hmac.rs b/hmac.rs index 510c8b2d..8e533f53 100644 --- a/hmac.rs +++ b/hmac.rs @@ -67,9 +67,9 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC { impl HMAC { pub fn update(&mut self, data: &[u8]) { unsafe { - do data.as_imm_buf |pdata, len| { + data.as_imm_buf(|pdata, len| { HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) - } + }); } } @@ -77,10 +77,10 @@ impl HMAC { unsafe { let mut res = vec::from_elem(self.len, 0u8); let mut outlen: libc::c_uint = 0; - do res.as_mut_buf |pres, _len| { + res.as_mut_buf(|pres, _len| { HMAC_Final(&mut self.ctx, pres, &mut outlen); assert!(self.len == outlen as uint) - } + }); res } } diff --git a/pkcs5.rs b/pkcs5.rs index 3c5ba23b..71c7e590 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -19,11 +19,11 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, assert!(iter >= 1u); assert!(keylen >= 1u); - do pass.as_imm_buf |pass_buf, pass_len| { - do salt.as_imm_buf |salt_buf, salt_len| { + pass.as_imm_buf(|pass_buf, pass_len| { + salt.as_imm_buf(|salt_buf, salt_len| { let mut out = vec::with_capacity(keylen); - do out.as_mut_buf |out_buf, _out_len| { + out.as_mut_buf(|out_buf, _out_len| { let r = unsafe { libcrypto::PKCS5_PBKDF2_HMAC_SHA1( pass_buf, pass_len as c_int, @@ -33,13 +33,13 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, }; if r != 1 as c_int { fail!(); } - } + }); unsafe { vec::raw::set_len(&mut out, keylen); } out - } - } + }) + }) } #[cfg(test)] diff --git a/pkey.rs b/pkey.rs index d59e05f1..41996c65 100644 --- a/pkey.rs +++ b/pkey.rs @@ -99,9 +99,9 @@ impl PKey { if len < 0 as c_int { return ~[]; } let mut s = vec::from_elem(len as uint, 0u8); - let r = do s.as_mut_buf |buf, _| { + let r = s.as_mut_buf(|buf, _| { f(self.evp, &buf) - }; + }); s.truncate(r as uint); s @@ -109,13 +109,13 @@ impl PKey { } fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) { - do s.as_imm_buf |ps, len| { + s.as_imm_buf(|ps, len| { let evp = ptr::null(); unsafe { f(6 as c_int, &evp, &ps, len as c_uint); } self.evp = evp; - } + }); } pub fn gen(&mut self, keysz: uint) { @@ -228,8 +228,8 @@ impl PKey { let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = do r.as_mut_buf |pr, _len| { - do s.as_imm_buf |ps, s_len| { + let rv = r.as_mut_buf(|pr, _len| { + s.as_imm_buf(|ps, s_len| { libcrypto::RSA_public_encrypt( s_len as c_uint, ps, @@ -237,8 +237,8 @@ impl PKey { rsa, openssl_padding_code(padding) ) - } - }; + }) + }); if rv < 0 as c_int { ~[] } else { @@ -257,8 +257,8 @@ impl PKey { let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = do r.as_mut_buf |pr, _len| { - do s.as_imm_buf |ps, s_len| { + let rv = r.as_mut_buf(|pr, _len| { + s.as_imm_buf(|ps, s_len| { libcrypto::RSA_private_decrypt( s_len as c_uint, ps, @@ -266,8 +266,8 @@ impl PKey { rsa, openssl_padding_code(padding) ) - } - }; + }) + }); if rv < 0 as c_int { ~[] @@ -307,8 +307,8 @@ impl PKey { let mut len = libcrypto::RSA_size(rsa); let mut r = vec::from_elem(len as uint + 1u, 0u8); - let rv = do r.as_mut_buf |pr, _len| { - do s.as_imm_buf |ps, s_len| { + let rv = r.as_mut_buf(|pr, _len| { + s.as_imm_buf(|ps, s_len| { libcrypto::RSA_sign( openssl_hash_nid(hash), ps, @@ -316,8 +316,8 @@ impl PKey { pr, &mut len, rsa) - } - }; + }) + }); if rv < 0 as c_int { ~[] @@ -332,8 +332,8 @@ impl PKey { unsafe { let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); - do m.as_imm_buf |pm, m_len| { - do s.as_imm_buf |ps, s_len| { + m.as_imm_buf(|pm, m_len| { + s.as_imm_buf(|ps, s_len| { let rv = libcrypto::RSA_verify( openssl_hash_nid(hash), pm, @@ -344,8 +344,8 @@ impl PKey { ); rv == 1 as c_int - } - } + }) + }) } } } diff --git a/rand.rs b/rand.rs index 07a39184..f8dd25b4 100644 --- a/rand.rs +++ b/rand.rs @@ -13,10 +13,10 @@ mod libcrypto { pub fn rand_bytes(len: uint) -> ~[u8] { let mut out = vec::with_capacity(len); - do out.as_mut_buf |out_buf, len| { + out.as_mut_buf(|out_buf, len| { let r = unsafe { libcrypto::RAND_bytes(out_buf, len as c_int) }; if r != 1 as c_int { fail!() } - } + }); unsafe { vec::raw::set_len(&mut out, len); } diff --git a/symm.rs b/symm.rs index edc408cb..3d31bde0 100644 --- a/symm.rs +++ b/symm.rs @@ -115,8 +115,8 @@ impl Crypter { }; assert_eq!(key.len(), self.keylen); - do key.as_imm_buf |pkey, _len| { - do iv.as_imm_buf |piv, _len| { + key.as_imm_buf(|pkey, _len| { + iv.as_imm_buf(|piv, _len| { libcrypto::EVP_CipherInit( self.ctx, self.evp, @@ -124,8 +124,8 @@ impl Crypter { piv, mode ) - } - } + }); + }); } } @@ -135,10 +135,10 @@ impl Crypter { */ pub fn update(&self, data: &[u8]) -> ~[u8] { unsafe { - do data.as_imm_buf |pdata, len| { + data.as_imm_buf(|pdata, len| { let mut res = vec::from_elem(len + self.blocksize, 0u8); - let reslen = do res.as_mut_buf |pres, _len| { + let reslen = res.as_mut_buf(|pres, _len| { let mut reslen = (len + self.blocksize) as u32; libcrypto::EVP_CipherUpdate( @@ -150,11 +150,11 @@ impl Crypter { ); reslen - }; + }); res.truncate(reslen as uint); res - } + }) } } @@ -165,11 +165,11 @@ impl Crypter { unsafe { let mut res = vec::from_elem(self.blocksize, 0u8); - let reslen = do res.as_mut_buf |pres, _len| { + let reslen = res.as_mut_buf(|pres, _len| { let mut reslen = self.blocksize as c_int; libcrypto::EVP_CipherFinal(self.ctx, pres, &mut reslen); reslen - }; + }); res.truncate(reslen as uint); res From ee4d0b4d2b16ba62f848e017ef8647a1cf19deb2 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 5 Dec 2013 01:43:37 -0800 Subject: [PATCH 6/8] Update for latest master (0.9-pre b5bab85) --- hash.rs | 2 +- hmac.rs | 2 +- pkcs5.rs | 2 +- pkey.rs | 2 +- rand.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hash.rs b/hash.rs index d7b99264..339aafeb 100644 --- a/hash.rs +++ b/hash.rs @@ -22,7 +22,7 @@ mod libcrypto { use super::*; use std::libc::c_uint; - #[link_args = "-lcrypto"] + #[link(name = "crypto")] extern { pub fn EVP_MD_CTX_create() -> EVP_MD_CTX; pub fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX); diff --git a/hmac.rs b/hmac.rs index 8e533f53..fc48b33d 100644 --- a/hmac.rs +++ b/hmac.rs @@ -27,7 +27,7 @@ pub struct HMAC_CTX { key: [libc::c_uchar, ..128] } -#[link_args = "-lcrypto"] +#[link(name = "crypto")] extern { fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD); diff --git a/pkcs5.rs b/pkcs5.rs index 71c7e590..b7c83a3f 100644 --- a/pkcs5.rs +++ b/pkcs5.rs @@ -4,7 +4,7 @@ use std::vec; mod libcrypto { use std::libc::c_int; - #[link_args = "-lcrypto"] + #[link(name = "crypto")] extern { pub fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int, salt: *u8, saltlen: c_int, diff --git a/pkey.rs b/pkey.rs index 41996c65..7b5245b2 100644 --- a/pkey.rs +++ b/pkey.rs @@ -15,7 +15,7 @@ mod libcrypto { use super::*; use std::libc::{c_char, c_int, c_uint}; - #[link_args = "-lcrypto"] + #[link(name = "crypto")] extern { pub fn EVP_PKEY_new() -> *EVP_PKEY; pub fn EVP_PKEY_free(k: *EVP_PKEY); diff --git a/rand.rs b/rand.rs index f8dd25b4..8e78c51c 100644 --- a/rand.rs +++ b/rand.rs @@ -4,7 +4,7 @@ use std::vec; mod libcrypto { use std::libc::c_int; - #[link_args = "-lcrypto"] + #[link(name = "crypto")] extern { pub fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; } From c9793a907fdc5fc2dff5f7c986ca04259cf06575 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sun, 8 Dec 2013 23:45:52 -0800 Subject: [PATCH 7/8] Delete unused main() function The new dead_code lint warns on this. --- hmac.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/hmac.rs b/hmac.rs index fc48b33d..c7cad6a2 100644 --- a/hmac.rs +++ b/hmac.rs @@ -85,11 +85,3 @@ impl HMAC { } } } - -fn main() { - let mut h = HMAC(SHA512, ~[00u8]); - - h.update([00u8]); - - println!("{:?}", h.final()) -} From bcdc23c359835fd8a3a872e8b66f47bb71046d91 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sat, 14 Dec 2013 18:51:43 -0800 Subject: [PATCH 8/8] Update for latest rustc (0.9-pre ca54ad8) --- hex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hex.rs b/hex.rs index 8a91c071..f55dc1a9 100644 --- a/hex.rs +++ b/hex.rs @@ -20,7 +20,7 @@ pub trait ToHex { fn to_hex(&self) -> ~str; } -impl<'self> ToHex for &'self [u8] { +impl<'a> ToHex for &'a [u8] { fn to_hex(&self) -> ~str { let chars = "0123456789ABCDEF".chars().collect::<~[char]>(); @@ -46,7 +46,7 @@ pub trait FromHex { fn from_hex(&self) -> ~[u8]; } -impl<'self> FromHex for &'self str { +impl<'a> FromHex for &'a str { fn from_hex(&self) -> ~[u8] { let mut vec = vec::with_capacity(self.len() / 2);