From dd46d1922e4706cf4c15c8cd915c1254363def8c Mon Sep 17 00:00:00 2001 From: Valerii Hiora Date: Mon, 13 Oct 2014 17:41:03 +0300 Subject: Correct init mutexes and locking function `libcrypto` uses locks quite intensively even without SSL. So they should be initialized before everything else to function properly in multi-threaded apps in which SSL operations are absent or delayed. Finishes #79 --- src/crypto/hash.rs | 2 ++ src/crypto/hmac.rs | 2 ++ src/crypto/pkcs5.rs | 2 ++ src/crypto/pkey.rs | 2 ++ src/crypto/rand.rs | 1 + src/crypto/symm.rs | 2 ++ 6 files changed, 11 insertions(+) (limited to 'src/crypto') diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs index 61221cb5..a72b8d9f 100644 --- a/src/crypto/hash.rs +++ b/src/crypto/hash.rs @@ -36,6 +36,8 @@ pub struct Hasher { impl Hasher { pub fn new(ht: HashType) -> Hasher { + ffi::init(); + let ctx = unsafe { ffi::EVP_MD_CTX_create() }; let (evp, mdlen) = evpmd(ht); unsafe { diff --git a/src/crypto/hmac.rs b/src/crypto/hmac.rs index 4c8617ca..ef2a0414 100644 --- a/src/crypto/hmac.rs +++ b/src/crypto/hmac.rs @@ -27,6 +27,8 @@ pub struct HMAC { #[allow(non_snake_case)] pub fn HMAC(ht: hash::HashType, key: &[u8]) -> HMAC { unsafe { + ffi::init(); + let (evp, mdlen) = hash::evpmd(ht); let mut ctx : ffi::HMAC_CTX = ::std::mem::uninitialized(); diff --git a/src/crypto/pkcs5.rs b/src/crypto/pkcs5.rs index ec6e0cef..feaff9c8 100644 --- a/src/crypto/pkcs5.rs +++ b/src/crypto/pkcs5.rs @@ -9,6 +9,8 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> Ve let mut out = Vec::with_capacity(keylen); + ffi::init(); + let r = ffi::PKCS5_PBKDF2_HMAC_SHA1( pass.as_ptr(), pass.len() as c_int, salt.as_ptr(), salt.len() as c_int, diff --git a/src/crypto/pkey.rs b/src/crypto/pkey.rs index d95e7738..ac16f478 100644 --- a/src/crypto/pkey.rs +++ b/src/crypto/pkey.rs @@ -55,6 +55,8 @@ pub struct PKey { impl PKey { pub fn new() -> PKey { unsafe { + ffi::init(); + PKey { evp: ffi::EVP_PKEY_new(), parts: Neither, diff --git a/src/crypto/rand.rs b/src/crypto/rand.rs index dffddee7..5f94c93c 100644 --- a/src/crypto/rand.rs +++ b/src/crypto/rand.rs @@ -5,6 +5,7 @@ pub fn rand_bytes(len: uint) -> Vec { unsafe { let mut out = Vec::with_capacity(len); + ffi::init(); let r = ffi::RAND_bytes(out.as_mut_ptr(), len as c_int); if r != 1 as c_int { fail!() } diff --git a/src/crypto/symm.rs b/src/crypto/symm.rs index 9953ac7b..171c1b05 100644 --- a/src/crypto/symm.rs +++ b/src/crypto/symm.rs @@ -50,6 +50,8 @@ pub struct Crypter { impl Crypter { pub fn new(t: Type) -> Crypter { + ffi::init(); + let ctx = unsafe { ffi::EVP_CIPHER_CTX_new() }; let (evp, keylen, blocksz) = evpc(t); Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz } -- cgit v1.2.3