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

Add support for ESP32 and RISC-V #1459

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]
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 = []

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

Expand Down
13 changes: 7 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ 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/curve25519/curve25519.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"),
(&[], "crypto/curve25519/curve25519.c"),
(&[], "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,
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
#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")]
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