From 0f592abef50b1bbe8a59d15883b08712bbf19db9 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Wed, 12 Jun 2019 12:20:17 -0700 Subject: [PATCH] Simplify core vs std usage --- src/aead/chacha.rs | 2 +- src/digest.rs | 3 ++- src/digest/sha1.rs | 2 +- src/ec/curve25519/ed25519/signing.rs | 1 - src/ec/curve25519/ed25519/verification.rs | 1 - src/ec/suite_b/ecdh.rs | 1 - src/ec/suite_b/ecdsa/signing.rs | 1 - src/ec/suite_b/ecdsa/verification.rs | 1 + src/ec/suite_b/ops.rs | 2 +- src/error.rs | 7 +------ src/io/der_writer.rs | 1 + src/io/writer.rs | 2 ++ src/lib.rs | 17 ++++------------- src/pkcs8.rs | 1 - src/polyfill.rs | 2 -- src/rand.rs | 3 +-- src/rsa/bigint.rs | 9 +++++++-- src/rsa/padding.rs | 1 + src/rsa/signing.rs | 2 ++ src/signature.rs | 1 - src/test.rs | 7 +++---- 21 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/aead/chacha.rs b/src/aead/chacha.rs index 5bbdaa541a..9f465bb362 100644 --- a/src/aead/chacha.rs +++ b/src/aead/chacha.rs @@ -18,7 +18,6 @@ use super::{ Block, BLOCK_LEN, }; use crate::{endian::*, polyfill::convert::*}; -use core; use libc::size_t; #[repr(C)] @@ -145,6 +144,7 @@ pub const KEY_LEN: usize = KEY_BLOCKS * BLOCK_LEN; mod tests { use super::*; use crate::test; + 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/digest.rs b/src/digest.rs index a5d8b2405c..5088eb1f40 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -25,7 +25,7 @@ // as possible. use crate::{cpu, debug, endian::*, polyfill}; -use core::{self, num::Wrapping}; +use core::num::Wrapping; use libc::size_t; mod sha1; @@ -507,6 +507,7 @@ mod tests { mod max_input { use super::super::super::digest; use crate::polyfill; + use std::vec; macro_rules! max_input_tests { ( $algorithm_name:ident ) => { diff --git a/src/digest/sha1.rs b/src/digest/sha1.rs index e5dadb8683..56596748e7 100644 --- a/src/digest/sha1.rs +++ b/src/digest/sha1.rs @@ -14,7 +14,7 @@ // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. use crate::polyfill; -use core::{self, num::Wrapping}; +use core::num::Wrapping; use libc::size_t; pub const BLOCK_LEN: usize = 512 / 8; diff --git a/src/ec/curve25519/ed25519/signing.rs b/src/ec/curve25519/ed25519/signing.rs index 0889c23641..a3072be4f3 100644 --- a/src/ec/curve25519/ed25519/signing.rs +++ b/src/ec/curve25519/ed25519/signing.rs @@ -23,7 +23,6 @@ use crate::{ rand, signature::{self, KeyPair as SigningKeyPair}, }; -use core; use untrusted; use super::digest::*; diff --git a/src/ec/curve25519/ed25519/verification.rs b/src/ec/curve25519/ed25519/verification.rs index 01ea921b81..9aceeaeee3 100644 --- a/src/ec/curve25519/ed25519/verification.rs +++ b/src/ec/curve25519/ed25519/verification.rs @@ -16,7 +16,6 @@ use super::super::ops::*; use crate::{error, polyfill::convert::*, sealed, signature}; -use core; use untrusted; use super::digest::*; diff --git a/src/ec/suite_b/ecdh.rs b/src/ec/suite_b/ecdh.rs index ca6451ccdb..f8680ccad6 100644 --- a/src/ec/suite_b/ecdh.rs +++ b/src/ec/suite_b/ecdh.rs @@ -143,7 +143,6 @@ fn ecdh( mod tests { use super::super::ops; use crate::{agreement, ec, limb, test}; - use core; static SUPPORTED_SUITE_B_ALGS: [(&str, &agreement::Algorithm, &ec::Curve, &ops::CommonOps); 2] = [ ( diff --git a/src/ec/suite_b/ecdsa/signing.rs b/src/ec/suite_b/ecdsa/signing.rs index b1c5360378..8651351551 100644 --- a/src/ec/suite_b/ecdsa/signing.rs +++ b/src/ec/suite_b/ecdsa/signing.rs @@ -26,7 +26,6 @@ use crate::{ io::der, limb, pkcs8, rand, sealed, signature, }; -use core; use untrusted; /// An ECDSA signing algorithm. diff --git a/src/ec/suite_b/ecdsa/verification.rs b/src/ec/suite_b/ecdsa/verification.rs index fa1adbe7fe..654a90f1ae 100644 --- a/src/ec/suite_b/ecdsa/verification.rs +++ b/src/ec/suite_b/ecdsa/verification.rs @@ -289,6 +289,7 @@ pub static ECDSA_P384_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificatio mod tests { use super::*; use crate::test; + use std::vec::Vec; #[test] fn test_digest_based_test_vectors() { diff --git a/src/ec/suite_b/ops.rs b/src/ec/suite_b/ops.rs index 7da176c1c6..159a4c66f9 100644 --- a/src/ec/suite_b/ops.rs +++ b/src/ec/suite_b/ops.rs @@ -469,7 +469,7 @@ extern "C" { mod tests { use super::*; use crate::test; - use std; + use std::{format, print, vec, vec::Vec}; use untrusted; const ZERO_SCALAR: Scalar = Scalar { diff --git a/src/error.rs b/src/error.rs index 35cdee4502..b71d75b33a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,12 +15,8 @@ //! Error reporting. use crate::polyfill::convert::*; -use core; use untrusted; -#[cfg(feature = "use_heap")] -use std; - /// An error with absolutely no details. /// /// *ring* uses this unit type as the error type in most of its results @@ -212,8 +208,7 @@ impl std::error::Error for KeyRejected { } } -#[cfg(feature = "use_heap")] -impl std::fmt::Display for KeyRejected { +impl core::fmt::Display for KeyRejected { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { f.write_str(self.description_()) } diff --git a/src/io/der_writer.rs b/src/io/der_writer.rs index 10e817a886..c370331ac3 100644 --- a/src/io/der_writer.rs +++ b/src/io/der_writer.rs @@ -13,6 +13,7 @@ // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. use super::{der::*, writer::*, *}; +use std::boxed::Box; pub(crate) fn write_positive_integer(output: &mut Accumulator, value: &Positive) { let first_byte = value.first_byte(); diff --git a/src/io/writer.rs b/src/io/writer.rs index e8f9bcb78d..681fd6679a 100644 --- a/src/io/writer.rs +++ b/src/io/writer.rs @@ -12,6 +12,8 @@ // 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}; + pub trait Accumulator { fn write_byte(&mut self, value: u8); fn write_bytes(&mut self, value: &[u8]); diff --git a/src/lib.rs b/src/lib.rs index ae759cd75a..a289dde3e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,21 +59,12 @@ unused_results, warnings )] -#![cfg_attr( - any( - target_os = "redox", - all( - not(test), - not(feature = "use_heap"), - unix, - not(any(target_os = "macos", target_os = "ios")), - any(not(target_os = "linux"), feature = "dev_urandom_fallback") - ) - ), - no_std -)] +#![no_std] #![cfg_attr(feature = "internal_benches", allow(unstable_features), feature(test))] +#[cfg(any(test, feature = "use_heap"))] +extern crate std; + #[macro_use] mod debug; diff --git a/src/pkcs8.rs b/src/pkcs8.rs index 63588c8db5..53bec5ae81 100644 --- a/src/pkcs8.rs +++ b/src/pkcs8.rs @@ -17,7 +17,6 @@ //! [RFC 5958]: https://tools.ietf.org/html/rfc5958. use crate::{ec, error, io::der}; -use core; use untrusted; pub(crate) enum Version { diff --git a/src/polyfill.rs b/src/polyfill.rs index 39db5d4858..42835390df 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -15,8 +15,6 @@ //! Polyfills for functionality that will (hopefully) be added to Rust's //! standard library soon. -use core; - #[macro_use] pub mod convert; diff --git a/src/rand.rs b/src/rand.rs index 2f73ce381c..1860a11a05 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -165,7 +165,6 @@ mod sysrand_chunk { #[cfg(windows)] mod sysrand_chunk { use crate::{error, polyfill}; - use core; #[inline] pub fn chunk(dest: &mut [u8]) -> Result { @@ -212,7 +211,6 @@ mod sysrand { ))] mod urandom { use crate::error; - use std; pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> { use lazy_static::lazy_static; @@ -321,6 +319,7 @@ mod fuchsia { #[cfg(test)] mod tests { use crate::rand::{self, SecureRandom}; + use std::vec; #[test] fn test_system_random_lengths() { diff --git a/src/rsa/bigint.rs b/src/rsa/bigint.rs index 45cbef09fe..6605c249d1 100644 --- a/src/rsa/bigint.rs +++ b/src/rsa/bigint.rs @@ -46,12 +46,16 @@ use crate::{ limb::{self, Limb, LimbMask, LIMB_BITS, LIMB_BYTES}, }; use core::{ - self, marker::PhantomData, ops::{Deref, DerefMut}, }; use libc::size_t; -use std::borrow::ToOwned as _; // TODO: Remove; Redundant as of Rust 1.36. +use std::{ + borrow::ToOwned as _, // TODO: Remove; Redundant as of Rust 1.36. + boxed::Box, + vec, + vec::Vec, +}; use untrusted; pub unsafe trait Prime {} @@ -1294,6 +1298,7 @@ extern "C" { mod tests { use super::*; use crate::test; + use std::format; use untrusted; // Type-level representation of an arbitrary modulus. diff --git a/src/rsa/padding.rs b/src/rsa/padding.rs index f733cbbd8c..de45a1d511 100644 --- a/src/rsa/padding.rs +++ b/src/rsa/padding.rs @@ -521,6 +521,7 @@ rsa_pss_padding!( mod test { use super::*; use crate::{digest, error, test}; + use std::vec; use untrusted; #[test] diff --git a/src/rsa/signing.rs b/src/rsa/signing.rs index c7fdc3005a..3083592601 100644 --- a/src/rsa/signing.rs +++ b/src/rsa/signing.rs @@ -24,6 +24,7 @@ use crate::{ io::{self, der, der_writer}, pkcs8, rand, signature, }; +use std::boxed::Box; use untrusted; /// An RSA key pair, used for signing. @@ -609,6 +610,7 @@ mod tests { // We intentionally avoid `use super::*` so that we are sure to use only // the public API; this ensures that enough of the API is public. use crate::{rand, signature}; + use std::vec; // `KeyPair::sign` requires that the output buffer is the same length as // the public key modulus. Test what happens when it isn't the same length. diff --git a/src/signature.rs b/src/signature.rs index 358ac8c361..42f1b01f66 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -257,7 +257,6 @@ //! ``` use crate::{cpu, ec, error, sealed}; -use core; use untrusted; pub use crate::ec::{ diff --git a/src/test.rs b/src/test.rs index 24e4675b62..e4e685af39 100644 --- a/src/test.rs +++ b/src/test.rs @@ -121,8 +121,8 @@ use crate::bits; use crate::{digest, error}; -use core; -use std::{self, string::String, vec::Vec}; +use std::{format, string::String, vec::Vec}; +use std::{panic, println}; /// `compile_time_assert_clone::();` fails to compile if `T` doesn't /// implement `Clone`. @@ -310,7 +310,7 @@ where #[allow(box_pointers)] while let Some(mut test_case) = parse_test_case(&mut current_section, lines) { - let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let result = panic::catch_unwind(panic::AssertUnwindSafe(|| { f(¤t_section, &mut test_case) })); let result = match result { @@ -455,7 +455,6 @@ fn parse_test_case( #[allow(missing_docs)] pub mod rand { use crate::{error, polyfill, rand, sealed}; - use core; /// An implementation of `SecureRandom` that always fills the output slice /// with the given byte.