This is an automated email from the ASF dual-hosted git repository.
rduan pushed a commit to branch emm-dev
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave-sgx-sdk.git
The following commit(s) were added to refs/heads/emm-dev by this push:
new 72b13eff Reimplement sgx_read_rand for ucrypto
72b13eff is described below
commit 72b13eff25fd2e960ad44ed683081db2c69b85c3
Author: volcano <[email protected]>
AuthorDate: Tue Mar 26 22:38:53 2024 +0800
Reimplement sgx_read_rand for ucrypto
---
sgx_crypto/sgx_crypto_sys/Cargo.toml | 4 +-
sgx_crypto/sgx_crypto_sys/src/lib.rs | 29 ++++++++++++++
.../tcrypto/ipp/sgx_tcrypto_common.cpp | 45 ----------------------
3 files changed, 32 insertions(+), 46 deletions(-)
diff --git a/sgx_crypto/sgx_crypto_sys/Cargo.toml
b/sgx_crypto/sgx_crypto_sys/Cargo.toml
index 3a935918..32312c8f 100644
--- a/sgx_crypto/sgx_crypto_sys/Cargo.toml
+++ b/sgx_crypto/sgx_crypto_sys/Cargo.toml
@@ -31,10 +31,12 @@ crate-type = ["rlib"]
[features]
default = []
-ucrypto = []
+ucrypto = ["rdrand", "rand_core"]
[dependencies]
sgx_types = { path = "../../sgx_types" }
+rdrand = { version = "0.8", optional = true }
+rand_core = { version = "0.6", optional = true }
[build-dependencies]
sgx_build_helper = { path = "../../sgx_build_helper/build_helper" }
diff --git a/sgx_crypto/sgx_crypto_sys/src/lib.rs
b/sgx_crypto/sgx_crypto_sys/src/lib.rs
index c190880e..eff0b118 100644
--- a/sgx_crypto/sgx_crypto_sys/src/lib.rs
+++ b/sgx_crypto/sgx_crypto_sys/src/lib.rs
@@ -642,3 +642,32 @@ mod bindings {
) -> SgxStatus;
}
}
+
+#[cfg(feature = "ucrypto")]
+mod rand {
+ use core::slice;
+ use rand_core::RngCore;
+ use rdrand::RdRand;
+ use sgx_types::error::{SgxResult, SgxStatus};
+
+ /// # Safety
+ #[no_mangle]
+ pub unsafe extern "C" fn sgx_read_rand(p: *mut u8, len: usize) -> u32 {
+ if p.is_null() || len == 0 {
+ return SgxStatus::InvalidParameter.into();
+ }
+
+ let buf = slice::from_raw_parts_mut(p, len);
+ match rand(buf) {
+ Ok(_) => SgxStatus::Success.into(),
+ Err(e) => e.into(),
+ }
+ }
+
+ #[inline]
+ fn rand(nonce: &mut [u8]) -> SgxResult {
+ let mut rd = RdRand::new().map_err(|_| SgxStatus::Unexpected)?;
+ rd.fill_bytes(nonce);
+ Ok(())
+ }
+}
diff --git a/sgx_crypto/sgx_crypto_sys/tcrypto/ipp/sgx_tcrypto_common.cpp
b/sgx_crypto/sgx_crypto_sys/tcrypto/ipp/sgx_tcrypto_common.cpp
index a87e5d9f..d322309a 100644
--- a/sgx_crypto/sgx_crypto_sys/tcrypto/ipp/sgx_tcrypto_common.cpp
+++ b/sgx_crypto/sgx_crypto_sys/tcrypto/ipp/sgx_tcrypto_common.cpp
@@ -210,49 +210,4 @@ int consttime_memequal(const void *b1, const void *b2,
size_t len)
return (1 & ((res - 1) >> 8));
}
-sgx_status_t sgx_read_rand(unsigned char *rand, size_t length_in_bytes)
-{
- // check parameters
- if (!rand || !length_in_bytes) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
-
- int ctxSize = 0;
- int length_in_bits = length_in_bytes * 8;
- IppsPRNGState* pPRNG = NULL;
- IppStatus ipp_ret = ippStsNoErr;
-
- do {
- ipp_ret = ippsPRNGGetSize(&ctxSize);
- ERROR_BREAK(ipp_ret);
-
- pPRNG = (IppsPRNGState*)(malloc(ctxSize));
- if (!pPRNG) {
- ipp_ret = ippStsNoMemErr;
- break;
- }
-
- ipp_ret = ippsPRNGInit(length_in_bits, pPRNG);
- ERROR_BREAK(ipp_ret);
-
- ipp_ret = ippsPRNGen((Ipp32u *)rand, length_in_bits, pPRNG);
- ERROR_BREAK(ipp_ret);
- } while (0);
-
- CLEAR_FREE_MEM(pPRNG, ctxSize);
-
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNoMemErr:
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr:
- case ippStsOutOfRangeErr:
- case ippStsSizeErr:
- case ippStsBadArgErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
-}
-
#endif
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]