From 7d36600ec8f00bd4ee2b1e9b28ea1032f25e33f5 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 3 Jul 2019 13:44:31 -1000 Subject: [PATCH] Use `alloc` instead of `std` when possible. --- STYLE.md | 2 +- src/aead/chacha.rs | 2 +- src/arithmetic.rs | 3 +++ src/arithmetic/bigint.rs | 4 ++-- src/digest.rs | 2 +- src/ec/suite_b/ops.rs | 3 ++- src/error.rs | 2 +- src/io/der_writer.rs | 2 +- src/io/writer.rs | 2 +- src/lib.rs | 3 +++ src/rsa/padding.rs | 2 +- src/rsa/signing.rs | 2 +- src/test.rs | 6 +++--- tests/ecdsa_tests.rs | 6 ++---- tests/pbkdf2_tests.rs | 2 +- tests/rsa_tests.rs | 5 +---- 16 files changed, 25 insertions(+), 23 deletions(-) diff --git a/STYLE.md b/STYLE.md index 601e8e908..882b7f5d8 100644 --- a/STYLE.md +++ b/STYLE.md @@ -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 diff --git a/src/aead/chacha.rs b/src/aead/chacha.rs index 56db5d4f2..23dc81bc3 100644 --- a/src/aead/chacha.rs +++ b/src/aead/chacha.rs @@ -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, diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 0bf0edbdb..c2be27ab0 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -14,5 +14,8 @@ #[macro_use] pub mod constant; + +#[cfg(feature = "use_heap")] pub mod bigint; + pub mod montgomery; diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index 1ab5ab46a..cfef6c722 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -43,11 +43,11 @@ use crate::{ 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 {} @@ -1292,7 +1292,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. diff --git a/src/digest.rs b/src/digest.rs index 2e6f20903..e27c34853 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -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 ) => { diff --git a/src/ec/suite_b/ops.rs b/src/ec/suite_b/ops.rs index a9ac8c592..3e21637aa 100644 --- a/src/ec/suite_b/ops.rs +++ b/src/ec/suite_b/ops.rs @@ -440,7 +440,8 @@ extern "C" { mod tests { use super::*; use crate::test; - use std::{format, print, vec, vec::Vec}; + use alloc::{format, vec, vec::Vec}; + use std::print; use untrusted; const ZERO_SCALAR: Scalar = Scalar { diff --git a/src/error.rs b/src/error.rs index e6416cf85..81719a5e4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -196,7 +196,7 @@ impl KeyRejected { } } -#[cfg(feature = "use_heap")] +#[cfg(feature = "use_std")] impl std::error::Error for KeyRejected { fn cause(&self) -> Option<&dyn std::error::Error> { None diff --git a/src/io/der_writer.rs b/src/io/der_writer.rs index 4de247f38..4c166eb9e 100644 --- a/src/io/der_writer.rs +++ b/src/io/der_writer.rs @@ -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(); diff --git a/src/io/writer.rs b/src/io/writer.rs index ff4bbb146..24436bd53 100644 --- a/src/io/writer.rs +++ b/src/io/writer.rs @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 6565bcf1f..974a20f47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,9 @@ #![no_std] #![cfg_attr(feature = "internal_benches", allow(unstable_features), feature(test))] +#[cfg(any(test, feature = "use_heap"))] +extern crate alloc; + #[cfg(any(test, feature = "use_heap"))] extern crate std; diff --git a/src/rsa/padding.rs b/src/rsa/padding.rs index bc84db017..59ca00e56 100644 --- a/src/rsa/padding.rs +++ b/src/rsa/padding.rs @@ -521,7 +521,7 @@ rsa_pss_padding!( mod test { use super::*; use crate::{digest, error, test}; - use std::vec; + use alloc::vec; use untrusted; #[test] diff --git a/src/rsa/signing.rs b/src/rsa/signing.rs index 3b8ec31f7..50842eb7a 100644 --- a/src/rsa/signing.rs +++ b/src/rsa/signing.rs @@ -24,7 +24,7 @@ use crate::{ io::{self, der, der_writer}, pkcs8, rand, signature, }; -use std::boxed::Box; +use alloc::boxed::Box; use untrusted; /// An RSA key pair, used for signing. diff --git a/src/test.rs b/src/test.rs index 75fe703d7..6d7e9f564 100644 --- a/src/test.rs +++ b/src/test.rs @@ -122,8 +122,8 @@ use crate::bits; use crate::{digest, error}; -use std::{format, string::String, vec::Vec}; -use std::{panic, println}; +use alloc::{format, string::String, vec::Vec}; +use std::println; /// `compile_time_assert_clone::();` fails to compile if `T` doesn't /// implement `Clone`. @@ -311,7 +311,7 @@ where #[allow(box_pointers)] while let Some(mut test_case) = parse_test_case(&mut current_section, lines) { - let result = panic::catch_unwind(panic::AssertUnwindSafe(|| { + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { f(¤t_section, &mut test_case) })); let result = match result { diff --git a/tests/ecdsa_tests.rs b/tests/ecdsa_tests.rs index b6a1ea864..cebb073eb 100644 --- a/tests/ecdsa_tests.rs +++ b/tests/ecdsa_tests.rs @@ -45,8 +45,6 @@ fn ecdsa_from_pkcs8_test() { test::run( test_file!("ecdsa_from_pkcs8_tests.txt"), |section, test_case| { - use std::error::Error; - assert_eq!(section, ""); let curve_name = test_case.consume_string("Curve"); @@ -85,7 +83,7 @@ fn ecdsa_from_pkcs8_test() { (Ok(_), None) => (), (Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e), (Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e), - (Err(actual), Some(expected)) => assert_eq!(actual.description(), expected), + (Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected), }; match ( @@ -95,7 +93,7 @@ fn ecdsa_from_pkcs8_test() { (Ok(_), None) => (), (Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e), (Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e), - (Err(actual), Some(expected)) => assert_eq!(actual.description(), expected), + (Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected), }; assert!(signature::EcdsaKeyPair::from_pkcs8(other_fixed, &input).is_err()); diff --git a/tests/pbkdf2_tests.rs b/tests/pbkdf2_tests.rs index 17c3ad0fa..07c77263d 100644 --- a/tests/pbkdf2_tests.rs +++ b/tests/pbkdf2_tests.rs @@ -32,7 +32,7 @@ )] use ring::{digest, error, pbkdf2, test, test_file}; -use std::num::NonZeroU32; +use core::num::NonZeroU32; #[test] pub fn pbkdf2_tests() { diff --git a/tests/rsa_tests.rs b/tests/rsa_tests.rs index 3c4f58ffc..30cc42027 100644 --- a/tests/rsa_tests.rs +++ b/tests/rsa_tests.rs @@ -46,8 +46,6 @@ fn rsa_from_pkcs8_test() { test::run( test_file!("rsa_from_pkcs8_tests.txt"), |section, test_case| { - use std::error::Error; - assert_eq!(section, ""); let input = test_case.consume_bytes("Input"); @@ -57,7 +55,7 @@ fn rsa_from_pkcs8_test() { (Ok(_), None) => (), (Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e), (Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e), - (Err(actual), Some(expected)) => assert_eq!(actual.description(), expected), + (Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected), }; Ok(()) @@ -93,7 +91,6 @@ fn test_signature_rsa_pkcs1_sign() { return Ok(()); } let key_pair = key_pair.unwrap(); - let key_pair = std::sync::Arc::new(key_pair); // XXX: This test is too slow on Android ARM Travis CI builds. // TODO: re-enable these tests on Android ARM.