Skip to content

Commit fbd505f

Browse files
authored
Merge pull request #1661 from briansmith/revert-1657-unsafe-cleanup
Revert "Document or remove some uses of `unsafe`"
2 parents 238ff8b + de138ee commit fbd505f

File tree

5 files changed

+18
-77
lines changed

5 files changed

+18
-77
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ name = "ring"
168168
[dependencies]
169169
getrandom = { version = "0.2.8" }
170170
untrusted = { version = "0.9" }
171-
zerocopy = { version = "0.7.5", features = ["derive"] }
172171

173172
[target.'cfg(any(target_arch = "x86",target_arch = "x86_64", all(any(target_arch = "aarch64", target_arch = "arm"), any(target_os = "android", target_os = "fuchsia", target_os = "linux", target_os = "windows"))))'.dependencies]
174173
spin = { version = "0.9.2", default-features = false, features = ["once"] }

src/aead/gcm.rs

-6
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,6 @@ impl Context {
126126
debug_assert!(input_bytes > 0);
127127

128128
let input = input.as_ptr() as *const [u8; BLOCK_LEN];
129-
// SAFETY:
130-
// - `[[u8; BLOCK_LEN]]` has the same bit validity as `[u8]`.
131-
// - `[[u8; BLOCK_LEN]]` has the same alignment requirement as `[u8]`.
132-
// - `input_bytes / BLOCK_LEN` ensures that the total length in bytes of
133-
// the new `[[u8; BLOCK_LEN]]` will not be longer than the original
134-
// `[u8]`.
135129
let input = unsafe { core::slice::from_raw_parts(input, input_bytes / BLOCK_LEN) };
136130

137131
// Although these functions take `Xi` and `h_table` as separate

src/digest.rs

+11-39
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
// The goal for this implementation is to drive the overhead as close to zero
2525
// as possible.
2626

27-
use crate::{c, cpu, debug, endian::BigEndian, polyfill};
27+
use crate::{
28+
c, cpu, debug,
29+
endian::{ArrayEncoding, BigEndian},
30+
polyfill,
31+
};
2832
use core::num::Wrapping;
29-
use zerocopy::{AsBytes, FromBytes};
3033

3134
mod sha1;
3235
mod sha2;
@@ -111,9 +114,7 @@ impl BlockContext {
111114

112115
Digest {
113116
algorithm: self.algorithm,
114-
// SAFETY: Invariant on this field promises that this is the correct
115-
// format function for this algorithm's block size.
116-
value: unsafe { (self.algorithm.format_output)(self.state) },
117+
value: (self.algorithm.format_output)(self.state),
117118
}
118119
}
119120
}
@@ -247,11 +248,8 @@ impl Digest {
247248
impl AsRef<[u8]> for Digest {
248249
#[inline(always)]
249250
fn as_ref(&self) -> &[u8] {
250-
let data = (&self.value as *const Output).cast::<u8>();
251-
// SAFETY: So long as `self.algorithm` is the correct algorithm, all
252-
// code initializes all bytes of `self.value` in the range `[0,
253-
// self.algorithm.output_len)`.
254-
unsafe { core::slice::from_raw_parts(data, self.algorithm.output_len) }
251+
let as64 = unsafe { &self.value.as64 };
252+
&as64.as_byte_array()[..self.algorithm.output_len]
255253
}
256254
}
257255

@@ -272,9 +270,7 @@ pub struct Algorithm {
272270
len_len: usize,
273271

274272
block_data_order: unsafe extern "C" fn(state: &mut State, data: *const u8, num: c::size_t),
275-
// INVARIANT: This is always set to the correct output for the algorithm's
276-
// block size.
277-
format_output: unsafe fn(input: State) -> Output,
273+
format_output: fn(input: State) -> Output,
278274

279275
initial_state: State,
280276

@@ -478,38 +474,14 @@ pub const MAX_OUTPUT_LEN: usize = 512 / 8;
478474
/// algorithms in this module.
479475
pub const MAX_CHAINING_LEN: usize = MAX_OUTPUT_LEN;
480476

481-
fn sha256_format_output(input: State) -> Output
482-
where
483-
[BigEndian<u32>; 256 / 8 / core::mem::size_of::<BigEndian<u32>>()]: FromBytes,
484-
[BigEndian<u64>; 512 / 8 / core::mem::size_of::<BigEndian<u64>>()]: AsBytes,
485-
{
486-
// SAFETY: There are two cases:
487-
// - The union is initialized as `as32`, in which case this is trivially
488-
// sound.
489-
// - The union is initialized as `as64`. In this case, the `as64` variant is
490-
// longer than the `as32` variant, so all bytes of `as32` are initialized
491-
// as they are in the prefix of `as64`. Since `as64`'s type is `AsBytes`
492-
// (see the where bound on this function), all of its bytes are
493-
// initialized (ie, none are padding). Since `as32`'s type is `FromBytes`,
494-
// any initialized sequence of bytes constitutes a valid instance of the
495-
// type, so this is sound.
477+
fn sha256_format_output(input: State) -> Output {
496478
let input = unsafe { &input.as32 };
497479
Output {
498480
as32: input.map(BigEndian::from),
499481
}
500482
}
501483

502-
/// # Safety
503-
///
504-
/// The caller must ensure that all bytes of `State` have been initialized.
505-
unsafe fn sha512_format_output(input: State) -> Output
506-
where
507-
[BigEndian<u64>; 512 / 8 / core::mem::size_of::<BigEndian<u64>>()]: FromBytes,
508-
{
509-
// SAFETY: Caller has promised that all bytes are initialized. Since
510-
// `input.as64` is `FromBytes`, we know that this is sufficient to guarantee
511-
// that the input has been initialized to a valid instance of the type of
512-
// `input.as64`.
484+
fn sha512_format_output(input: State) -> Output {
513485
let input = unsafe { &input.as64 };
514486
Output {
515487
as64: input.map(BigEndian::from),

src/endian.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use core::{mem, num::Wrapping};
2-
3-
use zerocopy::{AsBytes, FromBytes, FromZeroes};
1+
use core::num::Wrapping;
42

53
/// An `Encoding` of a type `T` can be converted to/from its byte
64
/// representation without any byte swapping or other computation.
75
///
86
/// The `Self: Copy` constraint addresses `clippy::declare_interior_mutable_const`.
9-
pub trait Encoding<T>: From<T> + Into<T> + FromBytes + AsBytes
7+
pub trait Encoding<T>: From<T> + Into<T>
108
where
119
Self: Copy,
1210
{
@@ -27,7 +25,7 @@ pub trait FromByteArray<T> {
2725

2826
macro_rules! define_endian {
2927
($endian:ident) => {
30-
#[derive(Clone, Copy, FromZeroes, FromBytes, AsBytes)]
28+
#[derive(Clone, Copy)]
3129
#[repr(transparent)]
3230
pub struct $endian<T>(T);
3331

@@ -50,7 +48,7 @@ macro_rules! impl_from_byte_array {
5048
{
5149
#[inline]
5250
fn from_byte_array(a: &[u8; $elems * core::mem::size_of::<$base>()]) -> Self {
53-
zerocopy::transmute!(*a)
51+
unsafe { core::mem::transmute_copy(a) }
5452
}
5553
}
5654
};
@@ -60,24 +58,11 @@ macro_rules! impl_array_encoding {
6058
($endian:ident, $base:ident, $elems:expr) => {
6159
impl ArrayEncoding<[u8; $elems * core::mem::size_of::<$base>()]>
6260
for [$endian<$base>; $elems]
63-
where
64-
Self: AsBytes,
6561
{
6662
#[inline]
6763
fn as_byte_array(&self) -> &[u8; $elems * core::mem::size_of::<$base>()] {
68-
const _: () = assert!(
69-
mem::size_of::<[$endian<$base>; $elems]>()
70-
== $elems * core::mem::size_of::<$base>()
71-
);
7264
let as_bytes_ptr =
7365
self.as_ptr() as *const [u8; $elems * core::mem::size_of::<$base>()];
74-
// SAFETY:
75-
// - `Self: AsBytes`, so it's sound to observe the bytes of
76-
// `self` and to have a reference to those bytes alive at the
77-
// same time as `&self`.
78-
// - As confirmed by the preceding assertion, the sizes of
79-
// `Self` and the return type are equal.
80-
// - `[u8; N]` has no alignment requirement.
8166
unsafe { &*as_bytes_ptr }
8267
}
8368
}

src/rsa/public_exponent.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@ impl PublicExponent {
1212

1313
// TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is
1414
// stable.
15-
pub(super) const _3: Self = Self(match NonZeroU64::new(3) {
16-
Some(nz) => nz,
17-
None => unreachable!(),
18-
});
19-
pub(super) const _65537: Self = Self(match NonZeroU64::new(65537) {
20-
Some(nz) => nz,
21-
None => unreachable!(),
22-
});
15+
pub(super) const _3: Self = Self(unsafe { NonZeroU64::new_unchecked(3) });
16+
pub(super) const _65537: Self = Self(unsafe { NonZeroU64::new_unchecked(65537) });
2317

2418
// This limit was chosen to bound the performance of the simple
2519
// exponentiation-by-squaring implementation in `elem_exp_vartime`. In
@@ -35,10 +29,7 @@ impl PublicExponent {
3529
//
3630
// TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is
3731
// stable.
38-
const MAX: Self = Self(match NonZeroU64::new((1u64 << 33) - 1) {
39-
Some(nz) => nz,
40-
None => unreachable!(),
41-
});
32+
const MAX: Self = Self(unsafe { NonZeroU64::new_unchecked((1u64 << 33) - 1) });
4233

4334
pub(super) fn from_be_bytes(
4435
input: untrusted::Input,

0 commit comments

Comments
 (0)