Skip to content

Commit

Permalink
MSRV(1.64): Use core::ffi::{c_int,c_uint}.
Browse files Browse the repository at this point in the history
We can use these after the recent MSRV bump.
  • Loading branch information
briansmith committed Nov 27, 2023
1 parent 6c29bf6 commit 65c8fe3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
error,
polyfill::{self, ArraySplitMap},
};
use core::ops::RangeFrom;
use core::{ffi, ops::RangeFrom};

#[derive(Clone)]
pub(super) struct Key {
Expand All @@ -35,22 +35,22 @@ pub(super) struct Key {
macro_rules! set_encrypt_key {
( $name:ident, $bytes:expr, $key_bits:expr, $key:expr ) => {{
prefixed_extern! {
fn $name(user_key: *const u8, bits: c::uint, key: &mut AES_KEY) -> c::int;
fn $name(user_key: *const u8, bits: ffi::c_uint, key: &mut AES_KEY) -> ffi::c_int;
}
set_encrypt_key($name, $bytes, $key_bits, $key)
}};
}

#[inline]
fn set_encrypt_key(
f: unsafe extern "C" fn(*const u8, c::uint, &mut AES_KEY) -> c::int,
f: unsafe extern "C" fn(*const u8, ffi::c_uint, &mut AES_KEY) -> ffi::c_int,
bytes: &[u8],
key_bits: BitLength,
key: &mut AES_KEY,
) -> Result<(), error::Unspecified> {
// Unusually, in this case zero means success and non-zero means failure.
#[allow(clippy::cast_possible_truncation)]
if 0 == unsafe { f(bytes.as_ptr(), key_bits.as_usize_bits() as c::uint, key) } {
if 0 == unsafe { f(bytes.as_ptr(), key_bits.as_usize_bits() as ffi::c_uint, key) } {
Ok(())
} else {
Err(error::Unspecified)
Expand Down Expand Up @@ -312,7 +312,7 @@ impl Key {
#[derive(Clone)]
pub(super) struct AES_KEY {
pub rd_key: [u32; 4 * (MAX_ROUNDS + 1)],
pub rounds: c::uint,
pub rounds: ffi::c_uint,
}

// Keep this in sync with `AES_MAXNR` in aes.h.
Expand Down
11 changes: 6 additions & 5 deletions src/bssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use crate::{c, error};
use crate::error;
use core::ffi;

/// 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
/// C code in `ring`.
#[derive(Clone, Copy, Debug)]
#[must_use]
#[repr(transparent)]
pub struct Result(c::int);
pub struct Result(ffi::c_int);

impl From<Result> for core::result::Result<(), error::Unspecified> {
fn from(ret: Result) -> Self {
Expand All @@ -37,12 +38,12 @@ impl From<Result> for core::result::Result<(), error::Unspecified> {
#[cfg(test)]
mod tests {
mod result {
use crate::{bssl, c};
use core::mem;
use crate::bssl;
use core::{ffi, mem};

#[test]
fn size_and_alignment() {
type Underlying = c::int;
type Underlying = ffi::c_int;
assert_eq!(mem::size_of::<bssl::Result>(), mem::size_of::<Underlying>());
assert_eq!(
mem::align_of::<bssl::Result>(),
Expand Down
13 changes: 0 additions & 13 deletions src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,13 @@
// Keep in sync with the checks in base.h that verify these assumptions.

pub(crate) type int = i32;
pub(crate) type uint = u32;
pub(crate) type size_t = usize;

#[cfg(all(test, any(unix, windows)))]
mod tests {
use crate::c;

#[test]
fn test_libc_compatible() {
{
let x: c::int = 1;
let _x: libc::c_int = x;
}

{
let x: c::uint = 1;
let _x: libc::c_uint = x;
}

{
let x: c::size_t = 1;
let _x: libc::size_t = x;
Expand Down
3 changes: 2 additions & 1 deletion src/constant_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Constant-time operations.
use crate::{c, error};
use core::ffi;

/// 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
Expand All @@ -32,7 +33,7 @@ pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), error::Unspecif
}

prefixed_extern! {
fn CRYPTO_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> c::int;
fn CRYPTO_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> ffi::c_int;
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions src/ec/curve25519/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
pub use super::scalar::{MaskedScalar, Scalar, SCALAR_LEN};
use crate::{
bssl, c, cpu, error,
bssl, cpu, error,
limb::{Limb, LIMB_BITS},
};
use core::marker::PhantomData;
use core::{ffi, marker::PhantomData};

// Elem<T>` is `fe` in curve25519/internal.h.
// Elem<L> is `fe_loose` in curve25519/internal.h.
Expand Down Expand Up @@ -82,7 +82,7 @@ impl ExtPoint {
t: Elem::zero(),
};
prefixed_extern! {
fn x25519_ge_scalarmult_base(h: &mut ExtPoint, a: &Scalar, has_fe25519_adx: c::int);
fn x25519_ge_scalarmult_base(h: &mut ExtPoint, a: &Scalar, has_fe25519_adx: ffi::c_int);
}
unsafe {
x25519_ge_scalarmult_base(&mut r, scalar, has_fe25519_adx(cpu).into());
Expand Down
5 changes: 3 additions & 2 deletions src/ec/curve25519/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
//! X25519 Key agreement.
use super::{ops, scalar::SCALAR_LEN};
use crate::{agreement, c, constant_time, cpu, ec, error, rand};
use crate::{agreement, constant_time, cpu, ec, error, rand};
use core::ffi;

static CURVE25519: ec::Curve = ec::Curve {
public_key_len: PUBLIC_KEY_LEN,
Expand Down Expand Up @@ -79,7 +80,7 @@ fn x25519_public_from_private(
fn x25519_public_from_private_generic_masked(
public_key_out: &mut PublicKey,
private_key: &PrivateKey,
use_adx: c::int,
use_adx: ffi::c_int,
);
}
unsafe {
Expand Down

0 comments on commit 65c8fe3

Please sign in to comment.