Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make std an optional dependency. Require alloc instead. #869

Merged
merged 3 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ e.g. export `CFLAGS=-D__ANDROID_API__=21`.

Additional Features that are Useful for Development
---------------------------------------------------

The `use_heap` feature enables functionality that uses the heap. This is on by
default. Disabling it is useful for code running in kernel space and some
embedded applications. For now some RSA, ECDH, and ECDSA signing functionality
still uses the heap. This feature will go away once RSA signing is the only
feature that uses the heap.

The `internal_benches` feature enable benchmarks of internal functions. These
benchmarks are only useful for people hacking on the implementation of *ring*.
(The benchmarks for the *ring* API are in the
Expand Down
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,13 @@ cc = { version = "1.0.37", default-features = false }

[features]
# These features are documented in the top-level module's documentation.
default = ["use_heap", "dev_urandom_fallback"]
dev_urandom_fallback = ["use_heap", "lazy_static"]
default = ["alloc", "dev_urandom_fallback", "std"]
alloc = []
dev_urandom_fallback = ["std", "lazy_static"]
internal_benches = []
slow_tests = []
test_logging = []
use_heap = []
std = ["alloc"]
test_logging = ["std"]

# XXX: debug = false because of https://github.com/rust-lang/rust/issues/34122

Expand Down
2 changes: 1 addition & 1 deletion STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ The C code generally uses the C `int` type as a return value, where 1 indicates
success and 0 indicates failure. The module [ring::bssl](src/bssl.rs) contains
a [transparent] `Result` type which should be used as the return type when
declaring foreign functions which follow this convention. A
`ring::bssl::Result` should be converted to a `std::result::Result` using the
`ring::bssl::Result` should be converted to a `core::result::Result` using the
pattern in the following example (note the placement of `unsafe`):

[transparent]: https://doc.rust-lang.org/nightly/reference/type-layout.html#the-transparent-representation
Expand Down
2 changes: 1 addition & 1 deletion src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ pub const KEY_LEN: usize = KEY_BLOCKS * BLOCK_LEN;
mod tests {
use super::*;
use crate::test;
use alloc::vec;
use core::convert::TryInto;
use std::vec;

// This verifies the encryption functionality provided by ChaCha20_ctr32
// is successful when either computed on disjoint input/output buffers,
Expand Down
3 changes: 3 additions & 0 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@

#[macro_use]
pub mod constant;

#[cfg(feature = "alloc")]
pub mod bigint;

pub mod montgomery;
6 changes: 2 additions & 4 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@
//! [Static checking of units in Servo]:
//! https://blog.mozilla.org/research/2014/06/23/static-checking-of-units-in-servo/

#![allow(box_pointers)]

use crate::{
arithmetic::montgomery::*,
bits, bssl, c, error,
limb::{self, Limb, LimbMask, LIMB_BITS, LIMB_BYTES},
};
use alloc::{borrow::ToOwned as _, boxed::Box, vec, vec::Vec};
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
use std::{borrow::ToOwned as _, boxed::Box, vec, vec::Vec};
use untrusted;

pub unsafe trait Prime {}
Expand Down Expand Up @@ -1292,7 +1290,7 @@ extern "C" {
mod tests {
use super::*;
use crate::test;
use std::format;
use alloc::format;
use untrusted;

// Type-level representation of an arbitrary modulus.
Expand Down
6 changes: 3 additions & 3 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl BitLength {
Ok(Self::from_usize_bits(bits))
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
#[inline]
pub fn half_rounded_up(&self) -> Self {
let round_up = self.0 & 1;
Expand All @@ -43,7 +43,7 @@ impl BitLength {
self.0
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
#[inline]
pub fn as_usize_bytes_rounded_up(&self) -> usize {
// Equivalent to (self.0 + 7) / 8, except with no potential for
Expand All @@ -55,7 +55,7 @@ impl BitLength {
(self.0 / 8) + round_up
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
#[inline]
pub fn try_sub_1(self) -> Result<BitLength, error::Unspecified> {
let sum = self.0.checked_sub(1).ok_or(error::Unspecified)?;
Expand Down
4 changes: 2 additions & 2 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Context {
/// # Examples:
///
/// ```
/// # #[cfg(feature = "use_heap")]
/// # #[cfg(feature = "alloc")]
/// # {
/// use ring::{digest, test};
/// let expected_hex = "09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b";
Expand Down Expand Up @@ -489,7 +489,7 @@ mod tests {
mod max_input {
use super::super::super::digest;
use crate::polyfill;
use std::vec;
use alloc::vec;

macro_rules! max_input_tests {
( $algorithm_name:ident ) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ec/suite_b/ecdsa/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub static ECDSA_P384_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificatio
mod tests {
use super::*;
use crate::test;
use std::vec::Vec;
use alloc::vec::Vec;

#[test]
fn test_digest_based_test_vectors() {
Expand Down
5 changes: 2 additions & 3 deletions src/ec/suite_b/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ extern "C" {
mod tests {
use super::*;
use crate::test;
use std::{format, print, vec, vec::Vec};
use alloc::{format, vec, vec::Vec};
use untrusted;

const ZERO_SCALAR: Scalar = Scalar {
Expand Down Expand Up @@ -1115,12 +1115,11 @@ mod tests {
) {
for i in 0..ops.num_limbs {
if actual[i] != expected[i] {
let mut s = std::string::String::new();
let mut s = alloc::string::String::new();
for j in 0..ops.num_limbs {
let formatted = format!("{:016x}", actual[ops.num_limbs - j - 1]);
s.push_str(&formatted);
}
print!("\n");
panic!("Actual != Expected,\nActual = {}", s);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use untrusted;
/// enum Error {
/// CryptoError,
///
/// # #[cfg(feature = "use_heap")]
/// # #[cfg(feature = "alloc")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feature = "std"

/// IOError(std::io::Error),
/// // [...]
/// }
Expand Down Expand Up @@ -88,7 +88,7 @@ impl core::fmt::Display for Unspecified {
}
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "std")]
impl std::error::Error for Unspecified {
#[inline]
fn cause(&self) -> Option<&dyn std::error::Error> {
Expand Down Expand Up @@ -168,12 +168,12 @@ impl KeyRejected {
KeyRejected("PublicKeyIsMissing")
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub(crate) fn too_small() -> Self {
KeyRejected("TooSmall")
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub(crate) fn too_large() -> Self {
KeyRejected("TooLarge")
}
Expand All @@ -186,7 +186,7 @@ impl KeyRejected {
KeyRejected("WrongAlgorithm")
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub(crate) fn private_modulus_len_not_multiple_of_512_bits() -> Self {
KeyRejected("PrivateModulusLenNotMultipleOf512Bits")
}
Expand All @@ -196,7 +196,7 @@ impl KeyRejected {
}
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "std")]
impl std::error::Error for KeyRejected {
fn cause(&self) -> Option<&dyn std::error::Error> {
None
Expand Down
4 changes: 2 additions & 2 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#[doc(hidden)]
pub mod der;

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
mod writer;

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub(crate) mod der_writer;

pub(crate) mod positive;
Expand Down
2 changes: 1 addition & 1 deletion src/io/der_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{der::*, writer::*, *};
use std::boxed::Box;
use alloc::boxed::Box;

pub(crate) fn write_positive_integer(output: &mut dyn Accumulator, value: &Positive) {
let first_byte = value.first_byte();
Expand Down
2 changes: 1 addition & 1 deletion src/io/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 std::{boxed::Box, vec::Vec};
use alloc::{boxed::Box, vec::Vec};

pub trait Accumulator {
fn write_byte(&mut self, value: u8);
Expand Down
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
//! <table>
//! <tr><th>Feature
//! <th>Description
//! <tr><td><code>alloc (default)</code>
//! <td>Enable features that require use of the heap, RSA in particular.
//! <tr><td><code>dev_urandom_fallback (default)</code>
//! <td>This is only applicable to Linux. On Linux, by default,
//! <code>ring::rand::SystemRandom</code> will fall back to reading
Expand All @@ -30,8 +32,9 @@
//! <code>dev_urandom_fallback</code> feature is disabled, such
//! fallbacks will not occur. See the documentation for
//! <code>rand::SystemRandom</code> for more details.
//! <tr><td><code>use_heap (default)</code>
//! <td>Enable features that require use of the heap, RSA in particular.
//! <tr><td><code>std (default)</code>
//! <td>Enable features that use libstd, in particular `std::error::Error`
//! integration.
//! </table>

#![doc(html_root_url = "https://briansmith.org/rustdoc/")]
Expand Down Expand Up @@ -62,13 +65,15 @@
#![no_std]
#![cfg_attr(feature = "internal_benches", allow(unstable_features), feature(test))]

#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

#[macro_use]
mod debug;

#[cfg(any(test, feature = "use_heap"))]
#[macro_use]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs a #[cfg(test)] in order for cargo build --no-default-features to work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, #[cfg(feature = "alloc")] is what you want here, so that the integration tests keep working.

pub mod test;

Expand Down Expand Up @@ -103,7 +108,7 @@ pub mod pbkdf2;
pub mod pkcs8;
pub mod rand;

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
mod rsa;

pub mod signature;
Expand Down
26 changes: 13 additions & 13 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
use crate::{c, error};
use untrusted;

#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
use crate::bits;

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
use core::num::Wrapping;

// XXX: Not correct for x32 ABIs.
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn limbs_less_than_limbs_vartime(a: &[Limb], b: &[Limb]) -> bool {
}

#[inline]
#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub fn limbs_less_than_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask {
unsafe { LIMBS_less_than_limb(a.as_ptr(), b, a.len()) }
}
Expand All @@ -87,13 +87,13 @@ pub fn limbs_are_zero_constant_time(limbs: &[Limb]) -> LimbMask {
unsafe { LIMBS_are_zero(limbs.as_ptr(), limbs.len()) }
}

#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
#[inline]
pub fn limbs_are_even_constant_time(limbs: &[Limb]) -> LimbMask {
unsafe { LIMBS_are_even(limbs.as_ptr(), limbs.len()) }
}

#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
#[inline]
pub fn limbs_equal_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask {
unsafe { LIMBS_equal_limb(a.as_ptr(), b, a.len()) }
Expand All @@ -106,7 +106,7 @@ pub fn limbs_equal_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask {
// with respect to `a.len()` or the value of the result or the value of the
// most significant bit (It's 1, unless the input is zero, in which case it's
// zero.)
#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
pub fn limbs_minimal_bits(a: &[Limb]) -> bits::BitLength {
for num_limbs in (1..=a.len()).rev() {
let high_limb = a[num_limbs - 1];
Expand Down Expand Up @@ -252,7 +252,7 @@ pub fn big_endian_from_limbs(limbs: &[Limb], out: &mut [u8]) {
}
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub type Window = Limb;

/// Processes `limbs` as a sequence of 5-bit windows, folding the windows from
Expand All @@ -267,7 +267,7 @@ pub type Window = Limb;
/// channels as long as `init` and `fold` are side-channel free.
///
/// Panics if `limbs` is empty.
#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
pub fn fold_5_bit_windows<R, I: FnOnce(Window) -> R, F: Fn(R, Window) -> R>(
limbs: &[Limb],
init: I,
Expand Down Expand Up @@ -333,16 +333,16 @@ pub fn fold_5_bit_windows<R, I: FnOnce(Window) -> R, F: Fn(R, Window) -> R>(
}

extern "C" {
#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
fn LIMB_shr(a: Limb, shift: c::size_t) -> Limb;

#[cfg(any(test, feature = "use_heap"))]
#[cfg(feature = "alloc")]
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"))]
#[cfg(feature = "alloc")]
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")]
#[cfg(feature = "alloc")]
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);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ mod tests {
}

#[test]
#[cfg(feature = "use_heap")]
#[cfg(feature = "alloc")]
fn test_limbs_less_than_limb_constant_time() {
static LESSER: &[(&[Limb], Limb)] = &[
(&[0], 1),
Expand Down
Loading