Skip to content

Commit

Permalink
Move SYS_GETRANDOM stuff from C to Rust.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Feb 6, 2019
1 parent 52e46fe commit dd5f7fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 36 deletions.
30 changes: 0 additions & 30 deletions crypto/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#if defined(__linux__)
#define _GNU_SOURCE
#endif

#include <GFp/cpu.h>
#include "internal.h"

Expand Down Expand Up @@ -46,29 +42,3 @@ HIDDEN uint32_t GFp_ia32cap_P[4] = {0};
HIDDEN uint32_t GFp_armcap_P = 0;

#endif

#if defined(__linux__)

// The getrandom syscall was added in Linux 3.17. For some important platforms,
// we also support building against older kernels' headers. For other
// platforms, the newer kernel's headers are required. */
#if !defined(SYS_getrandom)
#if defined(OPENSSL_AARCH64)
#define SYS_getrandom 278
#elif defined(OPENSSL_ARM)
#define SYS_getrandom 384
#elif defined(OPENSSL_X86)
#define SYS_getrandom 355
#elif defined(OPENSSL_X86_64)
#define SYS_getrandom 318
#else
#include <sys/syscall.h>
#if !defined(SYS_getrandom)
#error "Error: Kernel headers are too old; SYS_getrandom not defined."
#endif
#endif

#endif

const long GFp_SYS_GETRANDOM = SYS_getrandom;
#endif
21 changes: 15 additions & 6 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,24 @@ mod sysrand_chunk {
use crate::error;
use libc::{self, size_t};

extern "C" {
static GFp_SYS_GETRANDOM: libc::c_long;
}

#[inline]
pub fn chunk(dest: &mut [u8]) -> Result<usize, error::Unspecified> {
// See `SYS_getrandom` in #include <sys/syscall.h>.

#[cfg(target_arch = "aarch64")]
const SYS_GETRANDOM: libc::c_long = 278;

#[cfg(target_arch = "arm")]
const SYS_GETRANDOM: libc::c_long = 384;

#[cfg(target_arch = "x86")]
const SYS_GETRANDOM: libc::c_long = 355;

#[cfg(target_arch = "x86_64")]
const SYS_GETRANDOM: libc::c_long = 318;

let chunk_len: size_t = dest.len();
let flags: libc::c_uint = 0;
let r = unsafe { libc::syscall(GFp_SYS_GETRANDOM, dest.as_mut_ptr(), chunk_len, flags) };
let r = unsafe { libc::syscall(SYS_GETRANDOM, dest.as_mut_ptr(), chunk_len, 0) };
if r < 0 {
if unsafe { *libc::__errno_location() } == libc::EINTR {
// If an interrupt occurs while getrandom() is blocking to wait
Expand Down

0 comments on commit dd5f7fe

Please sign in to comment.