Skip to content
This repository has been archived by the owner on Sep 4, 2022. It is now read-only.

Commit

Permalink
Revert "Initialize libsodium with global constructor instead of expos…
Browse files Browse the repository at this point in the history
…ing initialization function."

This reverts commit d3d8aa0.
  • Loading branch information
kpp committed May 11, 2019
1 parent 1b32a3b commit 8833b62
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -27,7 +27,6 @@ coveralls = { repository = "sodiumoxide/sodiumoxide" }
libc = { version = "^0.2.41" , default-features = false }
libsodium-sys = { version = "0.2.1", path = "libsodium-sys" }
serde = { version = "^1.0.59", default-features = false, optional = true }
ctor = "0.1.7"

[dev-dependencies]
serde = "^1.0.59"
Expand Down
8 changes: 8 additions & 0 deletions src/crypto/aead/aead_macros.rs
Expand Up @@ -40,13 +40,21 @@ new_type! {
}

/// `gen_key()` randomly generates a secret key
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_key() -> Key {
let mut k = Key([0u8; KEYBYTES]);
randombytes_into(&mut k.0);
k
}

/// `gen_nonce()` randomly generates a nonce
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_nonce() -> Nonce {
let mut n = Nonce([0u8; NONCEBYTES]);
randombytes_into(&mut n.0);
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/auth/auth_macros.rs
Expand Up @@ -29,6 +29,10 @@ new_type! {
}

/// `gen_key()` randomly generates a key for authentication
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_key() -> Key {
let mut k = [0; KEYBYTES];
randombytes_into(&mut k);
Expand Down
12 changes: 12 additions & 0 deletions src/crypto/box_/curve25519xsalsa20poly1305.rs
Expand Up @@ -81,6 +81,10 @@ new_type! {
}

/// `gen_keypair()` randomly generates a secret key and a corresponding public key.
///
/// THREAD SAFETY: `gen_keypair()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_keypair() -> (PublicKey, SecretKey) {
unsafe {
let mut pk = PublicKey([0u8; PUBLICKEYBYTES]);
Expand All @@ -91,6 +95,10 @@ pub fn gen_keypair() -> (PublicKey, SecretKey) {
}

/// `key_pair_from_seed()` deterministically derives a key pair from a single key seed (crypto_box_SEEDBYTES bytes).
///
/// THREAD SAFETY: `key_pair_from_seed()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn keypair_from_seed(seed: &Seed) -> (PublicKey, SecretKey) {
unsafe {
let mut pk = PublicKey([0u8; PUBLICKEYBYTES]);
Expand All @@ -105,6 +113,10 @@ pub fn keypair_from_seed(seed: &Seed) -> (PublicKey, SecretKey) {
}

/// `gen_nonce()` randomly generates a nonce
///
/// THREAD SAFETY: `gen_nonce()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_nonce() -> Nonce {
let mut n = [0; NONCEBYTES];
randombytes_into(&mut n);
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/kx/x25519blake2b.rs
Expand Up @@ -44,6 +44,10 @@ new_type! {

/// `gen_keypair()` randomly generates a secret key and a corresponding public
/// key.
///
/// THREAD SAFETY: `gen_keypair()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_keypair() -> (PublicKey, SecretKey) {
unsafe {
let mut pk = PublicKey([0u8; PUBLICKEYBYTES]);
Expand Down
3 changes: 3 additions & 0 deletions src/crypto/pwhash/argon2_macros.rs
Expand Up @@ -76,6 +76,9 @@ new_type! {
}

/// `gen_salt()` randomly generates a new `Salt` for key derivation
///
/// THREAD SAFETY: `gen_salt()` is thread-safe provided that you have called
/// `sodiumoxide::init()` once before using any other function from sodiumoxide.
pub fn gen_salt() -> Salt {
let mut salt = Salt([0; SALTBYTES]);
randombytes_into(&mut salt.0);
Expand Down
3 changes: 3 additions & 0 deletions src/crypto/pwhash/scryptsalsa208sha256.rs
Expand Up @@ -61,6 +61,9 @@ new_type! {
}

/// `gen_salt()` randombly generates a new `Salt` for key derivation
///
/// THREAD SAFETY: `gen_salt()` is thread-safe provided that you have called
/// `sodiumoxide::init()` once before using any other function from sodiumoxide.
pub fn gen_salt() -> Salt {
let mut salt = Salt([0; SALTBYTES]);
randombytes_into(&mut salt.0);
Expand Down
8 changes: 8 additions & 0 deletions src/crypto/secretbox/xsalsa20poly1305.rs
Expand Up @@ -42,13 +42,21 @@ new_type! {
pub const MACBYTES: usize = ffi::crypto_secretbox_xsalsa20poly1305_MACBYTES as usize;

/// `gen_key()` randomly generates a secret key
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_key() -> Key {
let mut key = [0; KEYBYTES];
randombytes_into(&mut key);
Key(key)
}

/// `gen_nonce()` randomly generates a nonce
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_nonce() -> Nonce {
let mut nonce = [0; NONCEBYTES];
randombytes_into(&mut nonce);
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/secretstream/secretstream_macros.rs
Expand Up @@ -113,6 +113,10 @@ new_type! {
}

/// `gen_key()` randomly generates a secret key
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_key() -> Key {
let mut key = [0; KEYBYTES];
randombytes_into(&mut key);
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/shorthash/siphash24.rs
Expand Up @@ -23,6 +23,10 @@ new_type! {
}

/// `gen_key()` randomly generates a key for shorthash
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_key() -> Key {
let mut k = [0; KEYBYTES];
randombytes_into(&mut k);
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/sign/ed25519.rs
Expand Up @@ -53,6 +53,10 @@ new_type! {

/// `gen_keypair()` randomly generates a secret key and a corresponding public
/// key.
///
/// THREAD SAFETY: `gen_keypair()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_keypair() -> (PublicKey, SecretKey) {
let mut pk = PublicKey([0u8; PUBLICKEYBYTES]);
let mut sk = SecretKey([0u8; SECRETKEYBYTES]);
Expand Down
8 changes: 8 additions & 0 deletions src/crypto/stream/stream_macros.rs
Expand Up @@ -28,6 +28,10 @@ new_type! {
}

/// `gen_key()` randomly generates a key for symmetric encryption
///
/// THREAD SAFETY: `gen_key()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn gen_key() -> Key {
let mut key = [0; KEYBYTES];
randombytes_into(&mut key);
Expand All @@ -36,6 +40,10 @@ pub fn gen_key() -> Key {

/// `gen_nonce()` randomly generates a nonce for symmetric encryption
///
/// THREAD SAFETY: `gen_nonce()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
///
/// NOTE: When using primitives with short nonces (e.g. salsa20, salsa208, salsa2012)
/// do not use random nonces since the probability of nonce-collision is not negligible
pub fn gen_nonce() -> Nonce {
Expand Down
13 changes: 0 additions & 13 deletions src/lib.rs
Expand Up @@ -61,8 +61,6 @@

extern crate libsodium_sys as ffi;

#[macro_use]
extern crate ctor;
extern crate libc;
#[cfg(test)]
extern crate rustc_serialize;
Expand Down Expand Up @@ -91,10 +89,6 @@ mod prelude {
/// thread-safe
///
/// `init()` returns `Ok` if initialization succeeded and `Err` if it failed.
#[deprecated(
since = "0.2.2",
note = "libsodium is automatically initialized by sodiumoxide now."
)]
pub fn init() -> Result<(), ()> {
if unsafe { ffi::sodium_init() } >= 0 {
Ok(())
Expand All @@ -103,13 +97,6 @@ pub fn init() -> Result<(), ()> {
}
}

#[ctor]
fn init_real() {
unsafe {
ffi::sodium_init();
}
}

#[macro_use]
mod newtype_macros;
pub mod randombytes;
Expand Down
18 changes: 18 additions & 0 deletions src/randombytes.rs
Expand Up @@ -5,6 +5,10 @@ use ffi;
use prelude::*;

/// `randombytes()` randomly generates size bytes of data.
///
/// THREAD SAFETY: `randombytes()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn randombytes(size: usize) -> Vec<u8> {
unsafe {
let mut buf = vec![0u8; size];
Expand All @@ -14,6 +18,10 @@ pub fn randombytes(size: usize) -> Vec<u8> {
}

/// `randombytes_into()` fills a buffer `buf` with random data.
///
/// THREAD SAFETY: `randombytes_into()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn randombytes_into(buf: &mut [u8]) {
unsafe {
ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, buf.len());
Expand All @@ -25,6 +33,10 @@ pub fn randombytes_into(buf: &mut [u8]) {
/// possible output values even when `upper_bound` is not a power of 2. Note
/// that an `upper_bound` < 2 leaves only a single element to be chosen, namely
/// 0.
///
/// THREAD SAFETY: `randombytes()` is thread-safe provided that you have
/// called `sodiumoxide::init()` once before using any other function
/// from sodiumoxide.
pub fn randombytes_uniform(upper_bound: u32) -> u32 {
unsafe { ffi::randombytes_uniform(upper_bound) }
}
Expand All @@ -35,16 +47,22 @@ mod test {

#[test]
fn test_randombytes_uniform_0() {
::init().unwrap();

assert_eq!(randombytes_uniform(0), 0);
}

#[test]
fn test_randombytes_uniform_1() {
::init().unwrap();

assert_eq!(randombytes_uniform(1), 0);
}

#[test]
fn test_randombytes_uniform_7() {
::init().unwrap();

assert!(randombytes_uniform(7) < 7);
}
}

0 comments on commit 8833b62

Please sign in to comment.