Make bssl-crypto no_std compatible

Bug: 649
Change-Id: Ib47e843496e58a5cdb3cd04b3929e0a08ba09744
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63145
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/rust/bssl-crypto/Cargo.toml b/rust/bssl-crypto/Cargo.toml
index c60e9ca..315c35b 100644
--- a/rust/bssl-crypto/Cargo.toml
+++ b/rust/bssl-crypto/Cargo.toml
@@ -7,3 +7,7 @@
 
 [dependencies]
 bssl-sys = {path = "../bssl-sys"}
+
+[features]
+default = []
+std = []
diff --git a/rust/bssl-crypto/src/aead.rs b/rust/bssl-crypto/src/aead.rs
index 4cab452..a387e30 100644
--- a/rust/bssl-crypto/src/aead.rs
+++ b/rust/bssl-crypto/src/aead.rs
@@ -12,7 +12,9 @@
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+
 use crate::{CSlice, CSliceMut};
+use alloc::vec::Vec;
 use bssl_sys::{EVP_AEAD, EVP_AEAD_CTX};
 
 /// Error returned in the event of an unsuccessful AEAD operation.
diff --git a/rust/bssl-crypto/src/cipher/mod.rs b/rust/bssl-crypto/src/cipher/mod.rs
index 1215469..2ff6b3a 100644
--- a/rust/bssl-crypto/src/cipher/mod.rs
+++ b/rust/bssl-crypto/src/cipher/mod.rs
@@ -15,8 +15,8 @@
 
 use crate::{CSlice, CSliceMut};
 use bssl_sys::EVP_CIPHER;
-use std::ffi::c_int;
-use std::marker::PhantomData;
+use core::ffi::c_int;
+use core::marker::PhantomData;
 
 /// AES-CTR stream cipher operations.
 pub mod aes_ctr;
@@ -98,7 +98,7 @@
             bssl_sys::EVP_EncryptInit_ex(
                 ctx,
                 C::evp_cipher(),
-                std::ptr::null_mut(),
+                core::ptr::null_mut(),
                 key_cslice.as_ptr(),
                 iv_cslice.as_ptr(),
             )
diff --git a/rust/bssl-crypto/src/ec.rs b/rust/bssl-crypto/src/ec.rs
index 06f74d3..55fe4e9 100644
--- a/rust/bssl-crypto/src/ec.rs
+++ b/rust/bssl-crypto/src/ec.rs
@@ -17,8 +17,11 @@
 //! intended for internal use within this crate only, to create higher-level abstractions suitable
 //! to be exposed externally.
 
+use alloc::borrow::ToOwned;
+use alloc::vec;
+use alloc::vec::Vec;
 use core::panic;
-use std::{borrow::Borrow, fmt::Debug, ops::Deref};
+use core::{borrow::Borrow, fmt::Debug, ops::Deref};
 
 use crate::{bn::BigNum, CSlice, CSliceMut, ForeignType, ForeignTypeRef};
 
diff --git a/rust/bssl-crypto/src/ecdh.rs b/rust/bssl-crypto/src/ecdh.rs
index 231b362..aca711b 100644
--- a/rust/bssl-crypto/src/ecdh.rs
+++ b/rust/bssl-crypto/src/ecdh.rs
@@ -13,7 +13,8 @@
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-use std::marker::PhantomData;
+use alloc::vec::Vec;
+use core::marker::PhantomData;
 
 use crate::{
     ec::{Curve, EcKey},
diff --git a/rust/bssl-crypto/src/hkdf.rs b/rust/bssl-crypto/src/hkdf.rs
index 8eadcd0..e4e9c01 100644
--- a/rust/bssl-crypto/src/hkdf.rs
+++ b/rust/bssl-crypto/src/hkdf.rs
@@ -15,6 +15,7 @@
 use crate::digest::Md;
 use crate::digest::{Sha256, Sha512};
 use crate::{CSlice, CSliceMut, ForeignTypeRef};
+use alloc::vec::Vec;
 use core::marker::PhantomData;
 
 /// Implementation of HKDF-SHA-256
diff --git a/rust/bssl-crypto/src/lib.rs b/rust/bssl-crypto/src/lib.rs
index cd40b81..e53469d 100644
--- a/rust/bssl-crypto/src/lib.rs
+++ b/rust/bssl-crypto/src/lib.rs
@@ -21,8 +21,10 @@
     clippy::panic,
     clippy::expect_used
 )]
+#![cfg_attr(not(any(feature = "std", test)), no_std)]
 
 //! Rust BoringSSL bindings
+extern crate alloc;
 
 extern crate core;
 
@@ -79,7 +81,7 @@
     /// Returns a raw pointer to the value, which is safe to pass over FFI.
     pub fn as_ptr<T>(&self) -> *const T {
         if self.0.is_empty() {
-            std::ptr::null()
+            core::ptr::null()
         } else {
             self.0.as_ptr() as *const T
         }
@@ -97,7 +99,7 @@
     /// Returns a raw pointer to the value, which is safe to pass over FFI.
     pub fn as_mut_ptr<T>(&mut self) -> *mut T {
         if self.0.is_empty() {
-            std::ptr::null_mut()
+            core::ptr::null_mut()
         } else {
             self.0.as_mut_ptr() as *mut T
         }
diff --git a/rust/bssl-crypto/src/pkey.rs b/rust/bssl-crypto/src/pkey.rs
index f72c13c..3d4a62b 100644
--- a/rust/bssl-crypto/src/pkey.rs
+++ b/rust/bssl-crypto/src/pkey.rs
@@ -18,6 +18,8 @@
 //! externally.
 
 use crate::{ec::EcKey, CSliceMut, ForeignType};
+use alloc::borrow::ToOwned;
+use alloc::string::String;
 
 pub(crate) struct Pkey {
     ptr: *mut bssl_sys::EVP_PKEY,
@@ -45,8 +47,7 @@
         // - pkey is just allocated and is null-checked
         // - EcKey ensures eckey.ptr is valid during its lifetime
         // - EVP_PKEY_set1_EC_KEY doesn't take ownership
-        let result =
-            unsafe { bssl_sys::EVP_PKEY_set1_EC_KEY(pkey, eckey.as_ptr()) };
+        let result = unsafe { bssl_sys::EVP_PKEY_set1_EC_KEY(pkey, eckey.as_ptr()) };
         assert_eq!(result, 1, "bssl_sys::EVP_PKEY_set1_EC_KEY failed");
         Self { ptr: pkey }
     }
diff --git a/rust/bssl-crypto/src/test_helpers.rs b/rust/bssl-crypto/src/test_helpers.rs
index e4b4afb..9834805 100644
--- a/rust/bssl-crypto/src/test_helpers.rs
+++ b/rust/bssl-crypto/src/test_helpers.rs
@@ -12,6 +12,7 @@
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+use alloc::vec::Vec;
 
 #[allow(clippy::expect_used, clippy::unwrap_used, clippy::indexing_slicing)]
 pub(crate) fn decode_hex<const N: usize>(s: &str) -> [u8; N] {
diff --git a/rust/bssl-crypto/src/x25519.rs b/rust/bssl-crypto/src/x25519.rs
index 6e1a574..9ee449b 100644
--- a/rust/bssl-crypto/src/x25519.rs
+++ b/rust/bssl-crypto/src/x25519.rs
@@ -17,6 +17,8 @@
 //! “curve25519”, but “X25519” is a more precise name. See http://cr.yp.to/ecdh.html and
 //! https://tools.ietf.org/html/rfc7748.
 
+use alloc::borrow::ToOwned;
+
 /// Number of bytes in a private key in X25519
 pub const PRIVATE_KEY_LEN: usize = bssl_sys::X25519_PRIVATE_KEY_LEN as usize;
 /// Number of bytes in a public key in X25519