Skip to content

Commit

Permalink
more cleanup; mainly trying to get the hosted version to build
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnie committed Feb 10, 2024
1 parent 64a7a36 commit d85160b
Show file tree
Hide file tree
Showing 22 changed files with 103 additions and 68 deletions.
47 changes: 29 additions & 18 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const X86: &str = "x86";
const X86_64: &str = "x86_64";
const AARCH64: &str = "aarch64";
const ARM: &str = "arm";
const WASM32: &str = "wasm32";

#[rustfmt::skip]
const RING_SRCS: &[(&[&str], &str)] = &[
Expand Down Expand Up @@ -112,6 +111,7 @@ fn cpp_flags(compiler: &cc::Tool) -> &'static [&'static str] {
static NON_MSVC_FLAGS: &[&str] = &[
"-fvisibility=hidden",
"-std=c1x", // GCC 4.6 requires "c1x" instead of "c11"
"-pedantic",
"-Wall",
"-Wextra",
"-Wbad-function-cast",
Expand All @@ -121,7 +121,6 @@ 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 Expand Up @@ -163,63 +162,63 @@ fn cpp_flags(compiler: &cc::Tool) -> &'static [&'static str] {
const ASM_TARGETS: &[AsmTarget] = &[
AsmTarget {
oss: LINUX_ABI,
arch: AARCH64,
arch: "aarch64",
perlasm_format: "linux64",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: LINUX_ABI,
arch: ARM,
arch: "arm",
perlasm_format: "linux32",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: LINUX_ABI,
arch: X86,
arch: "x86",
perlasm_format: "elf",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: LINUX_ABI,
arch: X86_64,
arch: "x86_64",
perlasm_format: "elf",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: MACOS_ABI,
arch: AARCH64,
arch: "aarch64",
perlasm_format: "ios64",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: MACOS_ABI,
arch: X86_64,
arch: "x86_64",
perlasm_format: "macosx",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: &[WINDOWS],
arch: X86,
arch: "x86",
perlasm_format: "win32n",
asm_extension: "asm",
preassemble: true,
},
AsmTarget {
oss: &[WINDOWS],
arch: X86_64,
arch: "x86_64",
perlasm_format: "nasm",
asm_extension: "asm",
preassemble: true,
},
AsmTarget {
oss: &[WINDOWS],
arch: AARCH64,
arch: "aarch64",
perlasm_format: "win64",
asm_extension: "S",
preassemble: false,
Expand Down Expand Up @@ -265,9 +264,8 @@ const LINUX_ABI: &[&str] = &[

/// Operating systems that have the same ABI as macOS on every architecture
/// mentioned in `ASM_TARGETS`.
const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"];
const MACOS_ABI: &[&str] = &["ios", "macos", "tvos"];

const MACOS: &str = "macos";
const WINDOWS: &str = "windows";

/// Read an environment variable and tell Cargo that we depend on it.
Expand Down Expand Up @@ -420,6 +418,11 @@ fn build_c_code(
ring_core_prefix: &str,
use_pregenerated: bool,
) {
// Xous uses a pure Rust transpiled version of the code base
//if &target.os == "xous" && &target.arch != "x86_64" {
// return;
//}

println!("cargo:rustc-env=RING_CORE_PREFIX={}", ring_core_prefix);

let asm_target = ASM_TARGETS.iter().find(|asm_target| {
Expand Down Expand Up @@ -593,7 +596,7 @@ fn configure_cc(c: &mut cc::Build, target: &Target, include_dir: &Path) {
let _ = c.flag(f);
}

if target.os.as_str() == MACOS {
if target.os.as_str() == "macos" {
// ``-gfull`` is required for Darwin's |-dead_strip|.
let _ = c.flag("-gfull");
} else if !compiler.is_like_msvc() {
Expand All @@ -607,7 +610,8 @@ fn configure_cc(c: &mut cc::Build, target: &Target, include_dir: &Path) {
// Allow cross-compiling without a target sysroot for these targets.
//
// poly1305_vec.c requires <emmintrin.h> which requires <stdlib.h>.
if (target.arch == WASM32) || (target.os == "linux" && target.is_musl && target.arch != X86_64)
if (target.arch == "wasm32")
|| (target.os == "linux" && target.is_musl && target.arch != "x86_64")
{
if let Ok(compiler) = c.try_get_compiler() {
// TODO: Expand this to non-clang compilers in 0.17.0 if practical.
Expand Down Expand Up @@ -638,8 +642,8 @@ fn cc_asm(b: &cc::Build, file: &Path, out_file: &Path) -> Command {

fn nasm(file: &Path, arch: &str, include_dir: &Path, out_file: &Path) -> Command {
let oformat = match arch {
x if x == X86_64 => "win64",
x if x == X86 => "win32",
"x86_64" => "win64",
"x86" => "win32",
_ => panic!("unsupported arch: {}", arch),
};

Expand Down Expand Up @@ -746,7 +750,7 @@ fn perlasm(src_dst: &[(PathBuf, PathBuf)], asm_target: &AsmTarget) {
src.to_string_lossy().into_owned(),
asm_target.perlasm_format.to_owned(),
];
if asm_target.arch == X86 {
if asm_target.arch == "x86" {
args.push("-fPIC".into());
args.push("-DOPENSSL_IA32_SSE2".into());
}
Expand Down Expand Up @@ -878,6 +882,10 @@ fn generate_prefix_symbols_header(
fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String {
// Rename some nistz256 assembly functions to match the names of their
// polyfills.
#[cfg(any(target_arch="wasm32", target_os="xous"))]
static SYMBOLS_TO_RENAME: &[(&str, &str)] = &[
];
#[cfg(not(any(target_arch="wasm32", target_os="xous")))]
static SYMBOLS_TO_RENAME: &[(&str, &str)] = &[
("ecp_nistz256_point_double", "p256_point_double"),
("ecp_nistz256_point_add", "p256_point_add"),
Expand All @@ -888,6 +896,9 @@ fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String {
("ecp_nistz256_sqr_mont", "p256_sqr_mont"),
];

#[cfg(any(target_arch="wasm32", target_os="xous"))]
static SYMBOLS_TO_PREFIX: &[&str] = &[];
#[cfg(not(any(target_arch="wasm32", target_os="xous")))]
static SYMBOLS_TO_PREFIX: &[&str] = &[
"CRYPTO_memcmp",
"CRYPTO_poly1305_finish",
Expand Down
Binary file modified pregenerated/aesni-gcm-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/aesni-x86-win32n.o
Binary file not shown.
Binary file modified pregenerated/aesni-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/chacha-x86-win32n.o
Binary file not shown.
Binary file modified pregenerated/chacha-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/chacha20_poly1305_x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/ghash-x86-win32n.o
Binary file not shown.
Binary file modified pregenerated/ghash-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/p256-x86_64-asm-nasm.o
Binary file not shown.
Binary file modified pregenerated/sha256-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/sha512-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/vpaes-x86-win32n.o
Binary file not shown.
Binary file modified pregenerated/vpaes-x86_64-nasm.o
Binary file not shown.
Binary file modified pregenerated/x86-mont-win32n.o
Binary file not shown.
Binary file modified pregenerated/x86_64-mont-nasm.o
Binary file not shown.
Binary file modified pregenerated/x86_64-mont5-nasm.o
Binary file not shown.
15 changes: 7 additions & 8 deletions ring-transpile-c2rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@
import re

RING_C_FILES = [
"crypto/curve25519/curve25519.c",
"crypto/fipsmodule/aes/aes_nohw.c",
"crypto/fipsmodule/bn/montgomery.c",
"crypto/fipsmodule/bn/montgomery_inv.c",
"crypto/fipsmodule/ec/ecp_nistz.c",
"crypto/fipsmodule/ec/gfp_p256.c",
"crypto/fipsmodule/ec/gfp_p384.c",
"crypto/fipsmodule/ec/p256.c",
"crypto/limbs/limbs.c",
"crypto/mem.c",
"crypto/poly1305/poly1305.c",
# Other libraries
"crypto/crypto.c",
"crypto/curve25519/curve25519.c",
"crypto/fipsmodule/ec/ecp_nistz.c",
# "crypto/fipsmodule/ec/ecp_nistz256.c",
"crypto/fipsmodule/ec/gfp_p256.c",
"crypto/fipsmodule/ec/gfp_p384.c",
"crypto/fipsmodule/ec/p256.c",
]


Expand Down Expand Up @@ -225,12 +224,12 @@ def run():

print("Add this to the end of `src/lib.rs`:")

print("mod c2rust {")
print("pub mod c2rust {")
for file in RING_C_FILES:
mod_name = file.split("/")[-1].split(".")[0]
rs_file = file.replace(".c", ".rs")
# print(f" #[path = \"../{rs_file}\"]")
print(f" mod {mod_name};")
print(f" pub mod {mod_name};")
with open(rs_file, "r") as src_file:
with open(f"src/c2rust/{mod_name}.rs", "w") as dest_file:
print("#![allow(non_camel_case_types)]", file=dest_file)
Expand Down
10 changes: 5 additions & 5 deletions src/c2rust/gfp_p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
#![allow(non_upper_case_globals)]

extern "C" {
fn LIMBS_shl_mod(r: *mut Limb, a: *const Limb, m: *const Limb, num_limbs: size_t);
fn __assert_fail(
__assertion: *const core::ffi::c_char,
__file: *const core::ffi::c_char,
__line: core::ffi::c_uint,
__function: *const core::ffi::c_char,
) -> !;
fn LIMBS_are_zero(a: *const Limb, num_limbs: size_t) -> Limb;
fn LIMBS_equal(a: *const Limb, b: *const Limb, num_limbs: size_t) -> Limb;
fn LIMBS_add_mod(
fn LIMBS_sub_mod(
r: *mut Limb,
a: *const Limb,
b: *const Limb,
m: *const Limb,
num_limbs: size_t,
);
fn LIMBS_sub_mod(
fn LIMBS_add_mod(
r: *mut Limb,
a: *const Limb,
b: *const Limb,
m: *const Limb,
num_limbs: size_t,
);
fn LIMBS_shl_mod(r: *mut Limb, a: *const Limb, m: *const Limb, num_limbs: size_t);
fn LIMBS_equal(a: *const Limb, b: *const Limb, num_limbs: size_t) -> Limb;
fn LIMBS_are_zero(a: *const Limb, num_limbs: size_t) -> Limb;
fn bn_mul_mont(
rp: *mut BN_ULONG,
ap: *const BN_ULONG,
Expand Down
40 changes: 20 additions & 20 deletions src/c2rust/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,9 @@ pub type fiat_p256_int1 = core::ffi::c_schar;
pub type fiat_p256_limb_t = uint32_t;
pub type fiat_p256_felem = [uint32_t; 8];
#[inline]
unsafe extern "C" fn OPENSSL_memset(
mut dst: *mut core::ffi::c_void,
mut c: core::ffi::c_int,
mut n: size_t,
) -> *mut core::ffi::c_void {
if n == 0 as core::ffi::c_int as core::ffi::c_uint {
return dst;
}
return memset(dst, c, n);
}
#[inline]
unsafe extern "C" fn constant_time_declassify_w(mut v: crypto_word_t) -> crypto_word_t {
return value_barrier_w(v);
}
#[inline]
unsafe extern "C" fn constant_time_is_zero_w(mut a: crypto_word_t) -> crypto_word_t {
return constant_time_msb_w(!a & a.wrapping_sub(1 as core::ffi::c_int as core::ffi::c_uint));
unsafe extern "C" fn value_barrier_w(mut a: crypto_word_t) -> crypto_word_t {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
return a;
}
#[inline]
unsafe extern "C" fn constant_time_msb_w(mut a: crypto_word_t) -> crypto_word_t {
Expand All @@ -61,9 +47,12 @@ unsafe extern "C" fn constant_time_msb_w(mut a: crypto_word_t) -> crypto_word_t
);
}
#[inline]
unsafe extern "C" fn value_barrier_w(mut a: crypto_word_t) -> crypto_word_t {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
return a;
unsafe extern "C" fn constant_time_is_zero_w(mut a: crypto_word_t) -> crypto_word_t {
return constant_time_msb_w(!a & a.wrapping_sub(1 as core::ffi::c_int as core::ffi::c_uint));
}
#[inline]
unsafe extern "C" fn constant_time_declassify_w(mut v: crypto_word_t) -> crypto_word_t {
return value_barrier_w(v);
}
#[inline]
unsafe extern "C" fn OPENSSL_memcpy(
Expand All @@ -77,6 +66,17 @@ unsafe extern "C" fn OPENSSL_memcpy(
return memcpy(dst, src, n);
}
#[inline]
unsafe extern "C" fn OPENSSL_memset(
mut dst: *mut core::ffi::c_void,
mut c: core::ffi::c_int,
mut n: size_t,
) -> *mut core::ffi::c_void {
if n == 0 as core::ffi::c_int as core::ffi::c_uint {
return dst;
}
return memset(dst, c, n);
}
#[inline]
unsafe extern "C" fn recode_scalar_bits(
mut sign: *mut crypto_word_t,
mut digit: *mut crypto_word_t,
Expand Down
30 changes: 13 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@
// #![deny(missing_docs, unused_qualifications, variant_size_differences)]
// turning this off because c2rust has a lot of unused results
// #![forbid(unused_results)]
//#![deny(variant_size_differences)]
#![deny(variant_size_differences)]
#![forbid(
// unused_results,
unused_results,
invalid_reference_casting,
clippy::char_lit_as_u8,
clippy::fn_to_numeric_cast,
clippy::fn_to_numeric_cast_with_truncation,
Expand All @@ -88,10 +89,6 @@ extern crate alloc;
#[macro_use]
mod debug;

#[cfg(any(target_arch="wasm32", target_os="xous"))]
#[macro_use]
mod prefixed;

#[macro_use]
mod prefixed;

Expand Down Expand Up @@ -152,20 +149,19 @@ mod sealed {
}

#[cfg(any(target_arch="wasm32", target_os="xous"))]
mod c2rust {
pub mod c2rust {
pub mod aes_nohw;
pub mod montgomery;
mod montgomery_inv;
pub mod montgomery_inv;
pub mod limbs;
mod mem;
mod poly1305;
mod crypto;
mod curve25519;
mod ecp_nistz;
// mod ecp_nistz256;
mod gfp_p256;
mod gfp_p384;
mod p256;
pub mod mem;
pub mod poly1305;
pub mod crypto;
pub mod curve25519;
pub mod ecp_nistz;
pub mod gfp_p256;
pub mod gfp_p384;
pub mod p256;
}

#[cfg(target_os="xous")]
Expand Down
Loading

0 comments on commit d85160b

Please sign in to comment.