-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
arithmetic internals: Clarify memory safety of calls to
bn_mul_mont
.
Replace `debug_asesrt!`-based checking with proper error checking. The error cases will never be reached because the callers already ensured that the slices are the correct lengths, but this is more clearly correct. The previous step defining the `InOut` type didn't work out so well, so replace `InOut` with `AliasingSlices` that does the same thing. The cost is more monomorphization, but that will become moot soon, and it already isn't too bad since there are only three cases to consider. It does help reduce the number of length checks that end up getting generated.
- Loading branch information
1 parent
4955dd7
commit b794f56
Showing
5 changed files
with
210 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2024-2025 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. | ||
|
||
use super::{inout::AliasingSlices, n0::N0, LimbSliceError, MAX_LIMBS, MIN_LIMBS}; | ||
use crate::{c, limb::Limb, polyfill::usize_from_u32}; | ||
use core::mem::size_of; | ||
|
||
const _MAX_LIMBS_ADDRESSES_MEMORY_SAFETY_ISSUES: () = { | ||
// BoringSSL's limit: 8 kiloBYTES. | ||
const BN_MONTGOMERY_MAX_WORDS: usize = (8 * 1092) / size_of::<Limb>(); | ||
assert!(MAX_LIMBS <= BN_MONTGOMERY_MAX_WORDS); | ||
|
||
// Some 64-bit assembly implementations were written to take `len` as a | ||
// `c_int`, so they zero out the undefined top half of `len` to convert it | ||
// to a `usize`. But, others don't. | ||
assert!(MAX_LIMBS <= usize_from_u32(u32::MAX)); | ||
}; | ||
|
||
macro_rules! bn_mul_mont_ffi { | ||
( $in_out:expr, $n:expr, $n0:expr, $cpu:expr, | ||
unsafe { ($MIN_LEN:expr, $Cpu:ty) => $f:ident }) => {{ | ||
use crate::{c, limb::Limb}; | ||
prefixed_extern! { | ||
// `r` and/or 'a' and/or 'b' may alias. | ||
// XXX: BoringSSL declares these functions to return `int`. | ||
fn $f( | ||
r: *mut Limb, | ||
a: *const Limb, | ||
b: *const Limb, | ||
n: *const Limb, | ||
n0: &N0, | ||
len: c::size_t, | ||
); | ||
} | ||
unsafe { | ||
crate::arithmetic::ffi::bn_mul_mont_ffi::<$Cpu, { $MIN_LEN }>( | ||
$in_out, $n, $n0, $cpu, $f, | ||
) | ||
} | ||
}}; | ||
} | ||
|
||
#[inline] | ||
pub(super) unsafe fn bn_mul_mont_ffi<Cpu, const MIN_LEN: usize>( | ||
mut in_out: impl AliasingSlices<Limb>, | ||
n: &[Limb], | ||
n0: &N0, | ||
cpu: Cpu, | ||
f: unsafe extern "C" fn( | ||
r: *mut Limb, | ||
a: *const Limb, | ||
b: *const Limb, | ||
n: *const Limb, | ||
n0: &N0, | ||
len: c::size_t, | ||
), | ||
) -> Result<(), LimbSliceError> { | ||
/// The x86 implementation of `bn_mul_mont`, at least, requires at least 4 | ||
/// limbs. For a long time we have required 4 limbs for all targets, though | ||
/// this may be unnecessary. | ||
const _MIN_LIMBS_AT_LEAST_4: () = assert!(MIN_LIMBS >= 4); | ||
// We haven't tested shorter lengths. | ||
assert!(MIN_LEN >= MIN_LIMBS); | ||
if n.len() < MIN_LEN { | ||
return Err(LimbSliceError::too_short(n.len())); | ||
} | ||
|
||
// Avoid stack overflow from the alloca inside. | ||
if n.len() > MAX_LIMBS { | ||
return Err(LimbSliceError::too_long(n.len())); | ||
} | ||
|
||
in_out | ||
.with_pointers(n.len(), |r, a, b| { | ||
let len = n.len(); | ||
let n = n.as_ptr(); | ||
let _: Cpu = cpu; | ||
unsafe { f(r, a, b, n, n0, len) }; | ||
}) | ||
.map_err(LimbSliceError::len_mismatch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.