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

Support Xtensa, RISC-V and ESP-IDF #1506

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ untrusted = { version = "0.9" }
[target.'cfg(any(target_arch = "x86",target_arch = "x86_64", all(any(target_arch = "aarch64", target_arch = "arm"), any(target_os = "android", target_os = "fuchsia", target_os = "linux", target_os = "windows"))))'.dependencies]
spin = { version = "0.9.2", default-features = false, features = ["once"] }

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
[target.'cfg(any(target_os = "android", target_os = "linux", target_os = "espidf"))'.dependencies]
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this change will be needed once you update the rand.rs changes.

libc = { version = "0.2.100", default-features = false }
once_cell = { version = "1.8.0", default-features = false, features=["std"], optional = true }

Expand Down Expand Up @@ -201,6 +201,7 @@ slow_tests = []
std = ["alloc"]
test_logging = []
wasm32_unknown_unknown_js = ["web-sys"]
size_optimized = []
Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather not control this with a feature flag. Perhaps you can work around this with CFLAGS in your build environment for now?


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

Expand Down
11 changes: 6 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ const RING_SRCS: &[(&[&str], &str)] = &[
(&[], "crypto/mem.c"),
(&[], "crypto/poly1305/poly1305.c"),

(&[AARCH64, ARM, X86_64, X86], "crypto/crypto.c"),
(&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/ecp_nistz.c"),
(&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/gfp_p256.c"),
(&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/gfp_p384.c"),
(&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/p256.c"),
(&[], "crypto/crypto.c"),
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think a change for crypto.c is needed.

(&[], "crypto/fipsmodule/ec/ecp_nistz.c"),
(&[], "crypto/fipsmodule/ec/gfp_p256.c"),
(&[], "crypto/fipsmodule/ec/gfp_p384.c"),
(&[], "crypto/fipsmodule/ec/p256.c"),

(&[X86_64, X86], "crypto/cpu-intel.c"),

Expand Down Expand Up @@ -126,6 +126,7 @@ fn cpp_flags(compiler: &cc::Tool) -> &'static [&'static str] {
"-Wenum-compare",
"-Wfloat-equal",
"-Wformat=2",
#[cfg(not(feature = "size_optimized"))]
"-Winline",
"-Winvalid-pch",
"-Wmissing-field-initializers",
Expand Down
14 changes: 14 additions & 0 deletions crypto/fipsmodule/bn/montgomery.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,17 @@ int bn_from_montgomery_in_place(BN_ULONG r[], size_t num_r, BN_ULONG a[],
}
return 1;
}

#if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64) && \
!defined(OPENSSL_ARM) && !defined(OPENSSL_AARCH64)
void bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
Copy link
Owner

Choose a reason for hiding this comment

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

Let's solve this in a PR that isn't specific to any OS/architecture. I will contact the original author of this code to get it submitted so there's no IPR concerns.

const BN_ULONG *np, const BN_ULONG *n0, size_t num) {
Limb tmp[2 * num];
for (size_t i = 0; i < num; i++)
tmp[i] = 0;
for (size_t i = 0; i < num; i++)
tmp[num + i] = limbs_mul_add_limb(tmp + i, ap, bp[i], num);

bn_from_montgomery_in_place(rp, num, tmp, 2 * num, np, num, n0);
}
#endif
6 changes: 6 additions & 0 deletions include/ring-core/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
#define OPENSSL_MIPS64
#elif defined(__wasm__)
#define OPENSSL_32_BIT
#elif defined(__xtensa__)
#define OPENSSL_32_BIT
#elif defined(__riscv) && __riscv_xlen == 64
#define OPENSSL_64_BIT
#elif defined(__riscv) && __riscv_xlen == 32
#define OPENSSL_32_BIT
Copy link
Owner

Choose a reason for hiding this comment

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

Please reconsider this in light of the approach I suggest in #1455.

#else
// Note BoringSSL only supports standard 32-bit and 64-bit two's-complement,
// little-endian architectures. Functions will not produce the correct answer
Expand Down
17 changes: 17 additions & 0 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl crate::sealed::Sealed for SystemRandom {}
not(feature = "dev_urandom_fallback")
),
target_arch = "wasm32",
target_os = "espidf",
windows
))]
use self::sysrand::fill as fill_impl;
Expand Down Expand Up @@ -229,6 +230,21 @@ mod sysrand_chunk {
}
}

#[cfg(target_os = "espidf")]
Copy link
Owner

Choose a reason for hiding this comment

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

This part should be updated to just add target_os = "espidf" to the allowlist in the new version of rand.rs, after reviewing that the getrandom crate implements this functionality for this operating system correctly. Please do this change in a separate PR.

mod sysrand_chunk {
use crate::{c, error};

#[inline]
pub fn chunk(dest: &mut [u8]) -> Result<usize, error::Unspecified> {
let chunk_len: c::size_t = dest.len();
let r = unsafe { libc::getrandom(dest.as_mut_ptr() as *mut libc::c_void, chunk_len, 0) };
if r < 0 {
return Err(error::Unspecified);
}
Ok(r as usize)
}
}

#[cfg(all(
feature = "wasm32_unknown_unknown_js",
target_arch = "wasm32",
Expand Down Expand Up @@ -286,6 +302,7 @@ mod sysrand_chunk {
target_os = "android",
target_os = "linux",
target_arch = "wasm32",
target_os = "espidf",
windows
))]
mod sysrand {
Expand Down