From f8db53d48652834ac55ba5647aabce8a6a2cb31e Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Mon, 27 Nov 2023 12:43:21 -0800 Subject: [PATCH] NFC: Use `core::ffi::{c_int,c_uint}`. We can use these after the recent MSRV bump. --- src/aead/aes.rs | 10 +++++----- src/bssl.rs | 11 ++++++----- src/c.rs | 13 ------------- src/constant_time.rs | 3 ++- src/ec/curve25519/ops.rs | 6 +++--- src/ec/curve25519/x25519.rs | 5 +++-- 6 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/aead/aes.rs b/src/aead/aes.rs index f87427b584..f534dd39b9 100644 --- a/src/aead/aes.rs +++ b/src/aead/aes.rs @@ -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 { @@ -35,7 +35,7 @@ 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) }}; @@ -43,14 +43,14 @@ macro_rules! set_encrypt_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) @@ -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. diff --git a/src/bssl.rs b/src/bssl.rs index 8ce769da21..8f0bf20171 100644 --- a/src/bssl.rs +++ b/src/bssl.rs @@ -12,7 +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::{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 @@ -20,7 +21,7 @@ use crate::{c, error}; #[derive(Clone, Copy, Debug)] #[must_use] #[repr(transparent)] -pub struct Result(c::int); +pub struct Result(ffi::c_int); impl From for core::result::Result<(), error::Unspecified> { fn from(ret: Result) -> Self { @@ -37,12 +38,12 @@ impl From 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::(), mem::size_of::()); assert_eq!( mem::align_of::(), diff --git a/src/c.rs b/src/c.rs index facbd7a5d3..ce010f2770 100644 --- a/src/c.rs +++ b/src/c.rs @@ -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; diff --git a/src/constant_time.rs b/src/constant_time.rs index 3ac6ad4c21..d889075b3e 100644 --- a/src/constant_time.rs +++ b/src/constant_time.rs @@ -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 @@ -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)] diff --git a/src/ec/curve25519/ops.rs b/src/ec/curve25519/ops.rs index 3536ebc8c2..4939898af4 100644 --- a/src/ec/curve25519/ops.rs +++ b/src/ec/curve25519/ops.rs @@ -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` is `fe` in curve25519/internal.h. // Elem is `fe_loose` in curve25519/internal.h. @@ -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()); diff --git a/src/ec/curve25519/x25519.rs b/src/ec/curve25519/x25519.rs index fdfaa4c555..e00cacbc8d 100644 --- a/src/ec/curve25519/x25519.rs +++ b/src/ec/curve25519/x25519.rs @@ -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, @@ -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 {