Bring back the ring::c
internal C types module.
This commit is contained in:
parent
8237fac8be
commit
a804615ed2
@ -16,8 +16,7 @@ use super::{
|
||||
nonce::{self, Iv},
|
||||
shift, Block, Direction, BLOCK_LEN,
|
||||
};
|
||||
use crate::{bits::BitLength, cpu, endian::*, error, polyfill};
|
||||
use libc::size_t;
|
||||
use crate::{bits::BitLength, c, cpu, endian::*, error, polyfill};
|
||||
|
||||
pub(crate) struct Key {
|
||||
inner: AES_KEY,
|
||||
@ -49,14 +48,14 @@ impl Key {
|
||||
extern "C" {
|
||||
fn GFp_aes_hw_set_encrypt_key(
|
||||
user_key: *const u8,
|
||||
bits: libc::c_uint,
|
||||
bits: c::uint,
|
||||
key: &mut AES_KEY,
|
||||
) -> ZeroMeansSuccess;
|
||||
}
|
||||
Result::from(unsafe {
|
||||
GFp_aes_hw_set_encrypt_key(
|
||||
bytes.as_ptr(),
|
||||
key_bits.as_usize_bits() as libc::c_uint,
|
||||
key_bits.as_usize_bits() as c::uint,
|
||||
&mut key,
|
||||
)
|
||||
})?;
|
||||
@ -67,14 +66,14 @@ impl Key {
|
||||
extern "C" {
|
||||
fn GFp_vpaes_set_encrypt_key(
|
||||
user_key: *const u8,
|
||||
bits: libc::c_uint,
|
||||
bits: c::uint,
|
||||
key: &mut AES_KEY,
|
||||
) -> ZeroMeansSuccess;
|
||||
}
|
||||
Result::from(unsafe {
|
||||
GFp_vpaes_set_encrypt_key(
|
||||
bytes.as_ptr(),
|
||||
key_bits.as_usize_bits() as libc::c_uint,
|
||||
key_bits.as_usize_bits() as c::uint,
|
||||
&mut key,
|
||||
)
|
||||
})?;
|
||||
@ -84,14 +83,14 @@ impl Key {
|
||||
extern "C" {
|
||||
fn GFp_aes_nohw_set_encrypt_key(
|
||||
user_key: *const u8,
|
||||
bits: libc::c_uint,
|
||||
bits: c::uint,
|
||||
key: &mut AES_KEY,
|
||||
) -> ZeroMeansSuccess;
|
||||
}
|
||||
Result::from(unsafe {
|
||||
GFp_aes_nohw_set_encrypt_key(
|
||||
bytes.as_ptr(),
|
||||
key_bits.as_usize_bits() as libc::c_uint,
|
||||
key_bits.as_usize_bits() as c::uint,
|
||||
&mut key,
|
||||
)
|
||||
})?;
|
||||
@ -176,7 +175,7 @@ impl Key {
|
||||
fn GFp_aes_hw_ctr32_encrypt_blocks(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
blocks: size_t,
|
||||
blocks: c::size_t,
|
||||
key: &AES_KEY,
|
||||
ivec: &Counter,
|
||||
);
|
||||
@ -193,7 +192,7 @@ impl Key {
|
||||
fn GFp_bsaes_ctr32_encrypt_blocks(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
blocks: size_t,
|
||||
blocks: c::size_t,
|
||||
key: &AES_KEY,
|
||||
ivec: &Counter,
|
||||
);
|
||||
@ -241,7 +240,7 @@ impl Key {
|
||||
#[repr(C)]
|
||||
pub(super) struct AES_KEY {
|
||||
pub rd_key: [u32; 4 * (MAX_ROUNDS + 1)],
|
||||
pub rounds: libc::c_uint,
|
||||
pub rounds: c::uint,
|
||||
}
|
||||
|
||||
// Keep this in sync with `AES_MAXNR` in aes.h.
|
||||
@ -292,7 +291,7 @@ fn detect_implementation(cpu_features: cpu::Features) -> Implementation {
|
||||
|
||||
#[must_use]
|
||||
#[repr(transparent)]
|
||||
pub struct ZeroMeansSuccess(libc::c_int);
|
||||
pub struct ZeroMeansSuccess(c::int);
|
||||
|
||||
impl From<ZeroMeansSuccess> for Result<(), error::Unspecified> {
|
||||
fn from(ZeroMeansSuccess(value): ZeroMeansSuccess) -> Self {
|
||||
|
@ -205,7 +205,7 @@ fn integrated_aes_gcm<'a>(
|
||||
direction: Direction,
|
||||
cpu_features: cpu::Features,
|
||||
) -> &'a mut [u8] {
|
||||
use libc::size_t;
|
||||
use crate::c;
|
||||
|
||||
if !aes_key.is_aes_hw() || !gcm_ctx.is_avx2(cpu_features) {
|
||||
return in_out;
|
||||
@ -217,11 +217,11 @@ fn integrated_aes_gcm<'a>(
|
||||
fn GFp_aesni_gcm_decrypt(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
key: &aes::AES_KEY,
|
||||
ivec: &mut Counter,
|
||||
gcm: &mut gcm::Context,
|
||||
) -> size_t;
|
||||
) -> c::size_t;
|
||||
}
|
||||
unsafe {
|
||||
GFp_aesni_gcm_decrypt(
|
||||
@ -239,11 +239,11 @@ fn integrated_aes_gcm<'a>(
|
||||
fn GFp_aesni_gcm_encrypt(
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
key: &aes::AES_KEY,
|
||||
ivec: &mut Counter,
|
||||
gcm: &mut gcm::Context,
|
||||
) -> size_t;
|
||||
) -> c::size_t;
|
||||
}
|
||||
unsafe {
|
||||
GFp_aesni_gcm_encrypt(
|
||||
|
@ -17,8 +17,7 @@ use super::{
|
||||
nonce::{self, Iv},
|
||||
Block, BLOCK_LEN,
|
||||
};
|
||||
use crate::{endian::*, polyfill::convert::*};
|
||||
use libc::size_t;
|
||||
use crate::{c, endian::*, polyfill::convert::*};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Key([Block; KEY_BLOCKS]);
|
||||
@ -120,7 +119,7 @@ impl Key {
|
||||
fn GFp_ChaCha20_ctr32(
|
||||
out: *mut u8,
|
||||
in_: *const u8,
|
||||
in_len: size_t,
|
||||
in_len: c::size_t,
|
||||
key: &Key,
|
||||
first_iv: &Iv,
|
||||
);
|
||||
|
@ -13,8 +13,7 @@
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{Aad, Block, BLOCK_LEN};
|
||||
use crate::cpu;
|
||||
use libc::size_t;
|
||||
use crate::{c, cpu};
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct Key(GCM128_KEY);
|
||||
@ -111,7 +110,7 @@ impl Context {
|
||||
ctx: &mut Context,
|
||||
h_table: *const GCM128_KEY,
|
||||
inp: *const u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
@ -125,7 +124,7 @@ impl Context {
|
||||
ctx: &mut Context,
|
||||
h_table: *const GCM128_KEY,
|
||||
inp: *const u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
@ -140,7 +139,7 @@ impl Context {
|
||||
ctx: &mut Context,
|
||||
h_table: *const GCM128_KEY,
|
||||
inp: *const u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
@ -154,7 +153,7 @@ impl Context {
|
||||
ctx: &mut Context,
|
||||
h_table: *const GCM128_KEY,
|
||||
inp: *const u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
|
@ -19,8 +19,7 @@ use super::{
|
||||
block::{Block, BLOCK_LEN},
|
||||
Tag,
|
||||
};
|
||||
use crate::{bssl, error};
|
||||
use libc::size_t;
|
||||
use crate::{bssl, c, error};
|
||||
|
||||
/// A Poly1305 key.
|
||||
pub struct Key([Block; KEY_BLOCKS]);
|
||||
@ -51,7 +50,7 @@ impl Context {
|
||||
fn GFp_poly1305_blocks(
|
||||
state: &mut Opaque,
|
||||
input: *const u8,
|
||||
len: size_t,
|
||||
len: c::size_t,
|
||||
should_pad: Pad,
|
||||
);
|
||||
fn GFp_poly1305_emit(state: &mut Opaque, tag: &mut Tag, nonce: &Nonce);
|
||||
@ -123,7 +122,7 @@ struct Nonce(Block);
|
||||
#[repr(C)]
|
||||
struct Funcs {
|
||||
blocks_fn:
|
||||
unsafe extern "C" fn(&mut Opaque, input: *const u8, input_len: size_t, should_pad: Pad),
|
||||
unsafe extern "C" fn(&mut Opaque, input: *const u8, input_len: c::size_t, should_pad: Pad),
|
||||
emit_fn: unsafe extern "C" fn(&mut Opaque, &mut Tag, nonce: &Nonce),
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::error;
|
||||
use crate::{c, error};
|
||||
|
||||
/// An `int` returned from a foreign function containing **1** if the function
|
||||
/// was successful or **0** if an error occurred. This is the convention used by
|
||||
@ -20,7 +20,7 @@ use crate::error;
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[must_use]
|
||||
#[repr(transparent)]
|
||||
pub struct Result(libc::c_int);
|
||||
pub struct Result(c::int);
|
||||
|
||||
impl From<Result> for core::result::Result<(), error::Unspecified> {
|
||||
fn from(ret: Result) -> Self {
|
||||
@ -37,12 +37,12 @@ impl From<Result> for core::result::Result<(), error::Unspecified> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
mod result {
|
||||
use crate::bssl;
|
||||
use crate::{bssl, c};
|
||||
use core::mem;
|
||||
|
||||
#[test]
|
||||
fn size_and_alignment() {
|
||||
type Underlying = libc::c_int;
|
||||
type Underlying = c::int;
|
||||
assert_eq!(mem::size_of::<bssl::Result>(), mem::size_of::<Underlying>());
|
||||
assert_eq!(
|
||||
mem::align_of::<bssl::Result>(),
|
||||
|
33
src/c.rs
Normal file
33
src/c.rs
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright 2016-2019 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! C types.
|
||||
//!
|
||||
//! The libc crate provide the C types for most, but not all, targets that
|
||||
//! *ring* supports.
|
||||
|
||||
use libc;
|
||||
|
||||
pub(crate) type size_t = libc::size_t;
|
||||
pub(crate) type int = libc::c_int;
|
||||
pub(crate) type uint = libc::c_uint;
|
||||
|
||||
#[cfg(all(
|
||||
any(target_os = "android", target_os = "linux"),
|
||||
any(target_arch = "aarch64", target_arch = "arm")
|
||||
))]
|
||||
pub(crate) type ulong = libc::c_ulong;
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
pub(crate) type long = libc::c_long;
|
@ -14,8 +14,7 @@
|
||||
|
||||
//! Constant-time operations.
|
||||
|
||||
use crate::error;
|
||||
use libc::size_t;
|
||||
use crate::{c, error};
|
||||
|
||||
/// Returns `Ok(())` if `a == b` and `Err(error::Unspecified)` otherwise.
|
||||
/// The comparison of `a` and `b` is done in constant time with respect to the
|
||||
@ -33,7 +32,7 @@ pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), error::Unspecif
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn GFp_memcmp(a: *const u8, b: *const u8, len: size_t) -> libc::c_int;
|
||||
fn GFp_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> c::int;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
22
src/cpu.rs
22
src/cpu.rs
@ -64,19 +64,21 @@ pub(crate) mod arm {
|
||||
any(target_arch = "aarch64", target_arch = "arm")
|
||||
))]
|
||||
pub fn linux_setup() {
|
||||
use crate::c;
|
||||
|
||||
// XXX: The `libc` crate doesn't provide `libc::getauxval` consistently
|
||||
// across all Android/Linux targets, e.g. musl.
|
||||
extern "C" {
|
||||
fn getauxval(type_: libc::c_ulong) -> libc::c_ulong;
|
||||
fn getauxval(type_: c::ulong) -> c::ulong;
|
||||
}
|
||||
|
||||
const AT_HWCAP: libc::c_ulong = 16;
|
||||
const AT_HWCAP: c::ulong = 16;
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
const HWCAP_NEON: libc::c_ulong = 1 << 1;
|
||||
const HWCAP_NEON: c::ulong = 1 << 1;
|
||||
|
||||
#[cfg(target_arch = "arm")]
|
||||
const HWCAP_NEON: libc::c_ulong = 1 << 12;
|
||||
const HWCAP_NEON: c::ulong = 1 << 12;
|
||||
|
||||
let caps = unsafe { getauxval(AT_HWCAP) };
|
||||
|
||||
@ -86,20 +88,20 @@ pub(crate) mod arm {
|
||||
let mut features = NEON.mask;
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
const OFFSET: libc::c_ulong = 3;
|
||||
const OFFSET: c::ulong = 3;
|
||||
|
||||
#[cfg(target_arch = "arm")]
|
||||
const OFFSET: libc::c_ulong = 0;
|
||||
const OFFSET: c::ulong = 0;
|
||||
|
||||
#[cfg(target_arch = "arm")]
|
||||
let caps = {
|
||||
const AT_HWCAP2: libc::c_ulong = 26;
|
||||
const AT_HWCAP2: c::ulong = 26;
|
||||
unsafe { getauxval(AT_HWCAP2) }
|
||||
};
|
||||
|
||||
const HWCAP_AES: libc::c_ulong = 1 << 0 + OFFSET;
|
||||
const HWCAP_PMULL: libc::c_ulong = 1 << 1 + OFFSET;
|
||||
const HWCAP_SHA2: libc::c_ulong = 1 << 3 + OFFSET;
|
||||
const HWCAP_AES: c::ulong = 1 << 0 + OFFSET;
|
||||
const HWCAP_PMULL: c::ulong = 1 << 1 + OFFSET;
|
||||
const HWCAP_SHA2: c::ulong = 1 << 3 + OFFSET;
|
||||
|
||||
if caps & HWCAP_AES == HWCAP_AES {
|
||||
features |= AES.mask;
|
||||
|
@ -24,9 +24,8 @@
|
||||
// The goal for this implementation is to drive the overhead as close to zero
|
||||
// as possible.
|
||||
|
||||
use crate::{cpu, debug, endian::*, polyfill};
|
||||
use crate::{c, cpu, debug, endian::*, polyfill};
|
||||
use core::num::Wrapping;
|
||||
use libc::size_t;
|
||||
|
||||
mod sha1;
|
||||
|
||||
@ -248,7 +247,7 @@ pub struct Algorithm {
|
||||
/// The length of the length in the padding.
|
||||
len_len: usize,
|
||||
|
||||
block_data_order: unsafe extern "C" fn(state: &mut State, data: *const u8, num: size_t),
|
||||
block_data_order: unsafe extern "C" fn(state: &mut State, data: *const u8, num: c::size_t),
|
||||
format_output: fn(input: State) -> Output,
|
||||
|
||||
initial_state: State,
|
||||
@ -484,8 +483,8 @@ const SHA512_BLOCK_LEN: usize = 1024 / 8;
|
||||
const SHA512_LEN_LEN: usize = 128 / 8;
|
||||
|
||||
extern "C" {
|
||||
fn GFp_sha256_block_data_order(state: &mut State, data: *const u8, num: size_t);
|
||||
fn GFp_sha512_block_data_order(state: &mut State, data: *const u8, num: size_t);
|
||||
fn GFp_sha256_block_data_order(state: &mut State, data: *const u8, num: c::size_t);
|
||||
fn GFp_sha512_block_data_order(state: &mut State, data: *const u8, num: c::size_t);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -13,9 +13,8 @@
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::polyfill;
|
||||
use crate::{c, polyfill};
|
||||
use core::num::Wrapping;
|
||||
use libc::size_t;
|
||||
|
||||
pub const BLOCK_LEN: usize = 512 / 8;
|
||||
pub const CHAINING_LEN: usize = 160 / 8;
|
||||
@ -47,7 +46,7 @@ fn maj(x: W32, y: W32, z: W32) -> W32 {
|
||||
pub(super) unsafe extern "C" fn block_data_order(
|
||||
state: &mut super::State,
|
||||
data: *const u8,
|
||||
num: size_t,
|
||||
num: c::size_t,
|
||||
) {
|
||||
let data = data as *const [[u8; 4]; 16];
|
||||
let blocks = core::slice::from_raw_parts(data, num);
|
||||
|
@ -12,9 +12,8 @@
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::{arithmetic::montgomery::*, error, limb::*};
|
||||
use crate::{arithmetic::montgomery::*, c, error, limb::*};
|
||||
use core::marker::PhantomData;
|
||||
use libc::size_t;
|
||||
use untrusted;
|
||||
|
||||
pub use self::elem::*;
|
||||
@ -461,7 +460,7 @@ extern "C" {
|
||||
a: *const Limb,
|
||||
b: *const Limb,
|
||||
m: *const Limb,
|
||||
num_limbs: size_t,
|
||||
num_limbs: c::size_t,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -85,6 +85,7 @@ pub mod agreement;
|
||||
|
||||
mod bits;
|
||||
|
||||
pub(crate) mod c;
|
||||
pub mod constant_time;
|
||||
|
||||
pub mod io;
|
||||
|
23
src/limb.rs
23
src/limb.rs
@ -18,8 +18,7 @@
|
||||
//! Limbs ordered least-significant-limb to most-significant-limb. The bits
|
||||
//! limbs use the native endianness.
|
||||
|
||||
use crate::error;
|
||||
use libc::size_t;
|
||||
use crate::{c, error};
|
||||
use untrusted;
|
||||
|
||||
#[cfg(any(test, feature = "use_heap"))]
|
||||
@ -59,7 +58,7 @@ pub const LIMB_BYTES: usize = (LIMB_BITS + 7) / 8;
|
||||
#[inline]
|
||||
pub fn limbs_equal_limbs_consttime(a: &[Limb], b: &[Limb]) -> LimbMask {
|
||||
extern "C" {
|
||||
fn LIMBS_equal(a: *const Limb, b: *const Limb, num_limbs: size_t) -> LimbMask;
|
||||
fn LIMBS_equal(a: *const Limb, b: *const Limb, num_limbs: c::size_t) -> LimbMask;
|
||||
}
|
||||
|
||||
assert_eq!(a.len(), b.len());
|
||||
@ -276,9 +275,9 @@ pub fn fold_5_bit_windows<R, I: FnOnce(Window) -> R, F: Fn(R, Window) -> R>(
|
||||
) -> R {
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(transparent)]
|
||||
struct BitIndex(Wrapping<size_t>);
|
||||
struct BitIndex(Wrapping<c::size_t>);
|
||||
|
||||
const WINDOW_BITS: Wrapping<size_t> = Wrapping(5);
|
||||
const WINDOW_BITS: Wrapping<c::size_t> = Wrapping(5);
|
||||
|
||||
extern "C" {
|
||||
fn LIMBS_window5_split_window(
|
||||
@ -335,17 +334,17 @@ pub fn fold_5_bit_windows<R, I: FnOnce(Window) -> R, F: Fn(R, Window) -> R>(
|
||||
|
||||
extern "C" {
|
||||
#[cfg(any(test, feature = "use_heap"))]
|
||||
fn LIMB_shr(a: Limb, shift: size_t) -> Limb;
|
||||
fn LIMB_shr(a: Limb, shift: c::size_t) -> Limb;
|
||||
|
||||
#[cfg(any(test, feature = "use_heap"))]
|
||||
fn LIMBS_are_even(a: *const Limb, num_limbs: size_t) -> LimbMask;
|
||||
fn LIMBS_are_zero(a: *const Limb, num_limbs: size_t) -> LimbMask;
|
||||
fn LIMBS_are_even(a: *const Limb, num_limbs: c::size_t) -> LimbMask;
|
||||
fn LIMBS_are_zero(a: *const Limb, num_limbs: c::size_t) -> LimbMask;
|
||||
#[cfg(any(test, feature = "use_heap"))]
|
||||
fn LIMBS_equal_limb(a: *const Limb, b: Limb, num_limbs: size_t) -> LimbMask;
|
||||
fn LIMBS_less_than(a: *const Limb, b: *const Limb, num_limbs: size_t) -> LimbMask;
|
||||
fn LIMBS_equal_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask;
|
||||
fn LIMBS_less_than(a: *const Limb, b: *const Limb, num_limbs: c::size_t) -> LimbMask;
|
||||
#[cfg(feature = "use_heap")]
|
||||
fn LIMBS_less_than_limb(a: *const Limb, b: Limb, num_limbs: size_t) -> LimbMask;
|
||||
fn LIMBS_reduce_once(r: *mut Limb, m: *const Limb, num_limbs: size_t);
|
||||
fn LIMBS_less_than_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask;
|
||||
fn LIMBS_reduce_once(r: *mut Limb, m: *const Limb, num_limbs: c::size_t);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
19
src/rand.rs
19
src/rand.rs
@ -115,26 +115,25 @@ use crate::sealed;
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
mod sysrand_chunk {
|
||||
use crate::error;
|
||||
use libc::{self, size_t};
|
||||
use crate::{c, error};
|
||||
|
||||
#[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;
|
||||
const SYS_GETRANDOM: c::long = 278;
|
||||
|
||||
#[cfg(target_arch = "arm")]
|
||||
const SYS_GETRANDOM: libc::c_long = 384;
|
||||
const SYS_GETRANDOM: c::long = 384;
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
const SYS_GETRANDOM: libc::c_long = 355;
|
||||
const SYS_GETRANDOM: c::long = 355;
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
const SYS_GETRANDOM: libc::c_long = 318;
|
||||
const SYS_GETRANDOM: c::long = 318;
|
||||
|
||||
let chunk_len: size_t = dest.len();
|
||||
let chunk_len: c::size_t = dest.len();
|
||||
let r = unsafe { libc::syscall(SYS_GETRANDOM, dest.as_mut_ptr(), chunk_len, 0) };
|
||||
if r < 0 {
|
||||
let errno;
|
||||
@ -261,7 +260,7 @@ mod sysrand_or_urandom {
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
||||
mod darwin {
|
||||
use crate::error;
|
||||
use crate::{c, error};
|
||||
|
||||
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
|
||||
let r = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
|
||||
@ -286,9 +285,9 @@ mod darwin {
|
||||
#[must_use]
|
||||
fn SecRandomCopyBytes(
|
||||
rnd: &'static SecRandomRef,
|
||||
count: libc::size_t,
|
||||
count: c::size_t,
|
||||
bytes: *mut u8,
|
||||
) -> libc::c_int;
|
||||
) -> c::int;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,14 +42,13 @@
|
||||
|
||||
use crate::{
|
||||
arithmetic::montgomery::*,
|
||||
bits, bssl, error,
|
||||
bits, bssl, c, error,
|
||||
limb::{self, Limb, LimbMask, LIMB_BITS, LIMB_BYTES},
|
||||
};
|
||||
use core::{
|
||||
marker::PhantomData,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
use libc::size_t;
|
||||
use std::{borrow::ToOwned as _, boxed::Box, vec, vec::Vec};
|
||||
use untrusted;
|
||||
|
||||
@ -486,7 +485,7 @@ where
|
||||
|
||||
fn elem_mul_by_2<M, AF>(a: &mut Elem<M, AF>, m: &PartialModulus<M>) {
|
||||
extern "C" {
|
||||
fn LIMBS_shl_mod(r: *mut Limb, a: *const Limb, m: *const Limb, num_limbs: size_t);
|
||||
fn LIMBS_shl_mod(r: *mut Limb, a: *const Limb, m: *const Limb, num_limbs: c::size_t);
|
||||
}
|
||||
unsafe {
|
||||
LIMBS_shl_mod(
|
||||
@ -522,11 +521,11 @@ pub fn elem_reduced<Larger, Smaller: NotMuchSmallerModulus<Larger>>(
|
||||
extern "C" {
|
||||
fn GFp_bn_from_montgomery_in_place(
|
||||
r: *mut Limb,
|
||||
num_r: size_t,
|
||||
num_r: c::size_t,
|
||||
a: *mut Limb,
|
||||
num_a: size_t,
|
||||
num_a: c::size_t,
|
||||
n: *const Limb,
|
||||
num_n: size_t,
|
||||
num_n: c::size_t,
|
||||
n0: &N0,
|
||||
) -> bssl::Result;
|
||||
}
|
||||
@ -582,7 +581,7 @@ pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Modulus<M>) -> Elem<
|
||||
a: *const Limb,
|
||||
b: *const Limb,
|
||||
m: *const Limb,
|
||||
num_limbs: size_t,
|
||||
num_limbs: c::size_t,
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
@ -606,7 +605,7 @@ pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Modulus<M>) -> Elem
|
||||
a: *const Limb,
|
||||
b: *const Limb,
|
||||
m: *const Limb,
|
||||
num_limbs: size_t,
|
||||
num_limbs: c::size_t,
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
@ -852,7 +851,7 @@ pub fn elem_exp_consttime<M>(
|
||||
fn LIMBS_select_512_32(
|
||||
r: *mut Limb,
|
||||
table: *const Limb,
|
||||
num_limbs: size_t,
|
||||
num_limbs: c::size_t,
|
||||
i: Window,
|
||||
) -> bssl::Result;
|
||||
}
|
||||
@ -976,7 +975,7 @@ pub fn elem_exp_consttime<M>(
|
||||
|
||||
fn scatter(table: &mut [Limb], state: &[Limb], i: Window, num_limbs: usize) {
|
||||
extern "C" {
|
||||
fn GFp_bn_scatter5(a: *const Limb, a_len: size_t, table: *mut Limb, i: Window);
|
||||
fn GFp_bn_scatter5(a: *const Limb, a_len: c::size_t, table: *mut Limb, i: Window);
|
||||
}
|
||||
unsafe {
|
||||
GFp_bn_scatter5(
|
||||
@ -990,7 +989,7 @@ pub fn elem_exp_consttime<M>(
|
||||
|
||||
fn gather(table: &[Limb], state: &mut [Limb], i: Window, num_limbs: usize) {
|
||||
extern "C" {
|
||||
fn GFp_bn_gather5(r: *mut Limb, a_len: size_t, table: *const Limb, i: Window);
|
||||
fn GFp_bn_gather5(r: *mut Limb, a_len: c::size_t, table: *const Limb, i: Window);
|
||||
}
|
||||
unsafe {
|
||||
GFp_bn_gather5(
|
||||
@ -1018,7 +1017,7 @@ pub fn elem_exp_consttime<M>(
|
||||
table: *const Limb,
|
||||
np: *const Limb,
|
||||
n0: &N0,
|
||||
num: size_t,
|
||||
num: c::size_t,
|
||||
power: Window,
|
||||
);
|
||||
}
|
||||
@ -1043,7 +1042,7 @@ pub fn elem_exp_consttime<M>(
|
||||
table: *const Limb,
|
||||
n: *const Limb,
|
||||
n0: &N0,
|
||||
num: size_t,
|
||||
num: c::size_t,
|
||||
i: Window,
|
||||
);
|
||||
}
|
||||
@ -1101,7 +1100,7 @@ pub fn elem_exp_consttime<M>(
|
||||
not_used: *const Limb,
|
||||
n: *const Limb,
|
||||
n0: &N0,
|
||||
num: size_t,
|
||||
num: c::size_t,
|
||||
) -> bssl::Result;
|
||||
}
|
||||
Result::from(unsafe {
|
||||
@ -1285,7 +1284,7 @@ extern "C" {
|
||||
b: *const Limb,
|
||||
n: *const Limb,
|
||||
n0: &N0,
|
||||
num_limbs: size_t,
|
||||
num_limbs: c::size_t,
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user