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

wasm #73

Merged
merged 18 commits into from
Sep 18, 2023
Merged

wasm #73

Show file tree
Hide file tree
Changes from 14 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
104 changes: 84 additions & 20 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,110 @@ name: Build & Test

on:
push:
branches: [ "main" ]
branches: ["main", "dev"]
pull_request:
branches: [ "main" ]
branches: ["main", "dev"]
workflow_dispatch:

env:
CARGO_TERM_COLOR: always

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
strategy:
fail-fast: false
matrix:
bits: [32, 64]
os:
- macos-latest
- ubuntu-latest
- windows-latest
exclude:
- bits: 32
os: "macos-latest"
# FIXME: Linking isn't working here yet for hacl #42
- bits: 32
os: "windows-latest"

runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4

- run: echo "RUST_TARGET_FLAG=" > $GITHUB_ENV
if: ${{ matrix.bits == 64 }}

- name: Setup Ubuntu x86
if: ${{ matrix.bits == 32 && matrix.os == 'ubuntu-latest' }}
run: |
rustup target add i686-unknown-linux-gnu
sudo apt-get update
sudo apt-get install -y gcc-multilib g++-multilib

# Set up 32 bit systems

- name: Config Windows x86
run: echo "RUST_TARGET_FLAG=--target=i686-pc-windows-msvc" > $GITHUB_ENV
if: ${{ matrix.bits == 32 && matrix.os == 'windows-latest' }}

- name: Config Linux x86
run: |
echo "RUST_TARGET_FLAG=--target=i686-unknown-linux-gnu" > $GITHUB_ENV
if: ${{ matrix.bits == 32 && matrix.os == 'ubuntu-latest' }}

# Set up windows

- name: Setup Windows x86
if: ${{ matrix.bits == 32 && matrix.os == 'windows-latest' }}
shell: pwsh
run: |
echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append
vcpkg install openssl:x86-windows-static-md

- name: Setup Ubuntu
if: matrix.os == 'ubuntu-latest'
run: sudo apt-get install ninja-build
- name: Setup MacOS
if: matrix.os == 'macos-latest'
run: brew install ninja
- name: Setup Windows
if: matrix.os == 'windows-latest'
run: |
echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append
vcpkg install openssl:x64-windows-static-md
- name: Setup Windows x64
if: ${{ matrix.bits == 64 && matrix.os == 'windows-latest' }}
shell: pwsh
run: |
echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append
vcpkg install openssl:x64-windows-static-md

- uses: actions/checkout@v3
# Build ...

- name: Build
run: cargo build --verbose
- name: Build
run: cargo build --verbose $RUST_TARGET_FLAG

- name: Build Release
run: cargo build --verbose --release $RUST_TARGET_FLAG

# Test ...

- name: Test
run: cargo test --verbose $RUST_TARGET_FLAG

- name: Test Release
run: cargo test --verbose --release $RUST_TARGET_FLAG

- name: Build sys/hacl
working-directory: sys/hacl
run: cargo build --verbose $RUST_TARGET_FLAG

wasm:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: mymindstorm/setup-emsdk@v11

- name: Run tests
run: cargo test --verbose
- name: Setup
run: |
rustup target add wasm32-unknown-unknown
cargo install wasm-pack

- name: Build sys/hacl
run: cargo build --target-dir sys/hacl --verbose
- run: CC=emcc AR=emar wasm-pack test --node --features wasm
20 changes: 15 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ libcrux_platform = { version = "=0.0.1", path = "sys/platform" }
libcrux-hacl = { version = "=0.0.1", path = "sys/hacl" }
libcrux_platform = { version = "=0.0.1", path = "sys/platform" }
rand = { version = "0.8" }
getrandom = { version = "0.2", features = ["js"] }
log = "0.4"
# WASM API
wasm-bindgen = { version = "0.2.87", optional = true }

[target.'cfg(all(not(target_os = "windows"), any(target_arch = "x86_64", target_arch = "x86")))'.dependencies]
[target.'cfg(all(not(target_os = "windows"), target_arch = "x86_64"))'.dependencies]
libjade-sys = { version = "0.0.1", path = "sys/libjade" }

[dev-dependencies]
libcrux = { path = ".", features = ["rand"] }
pretty_env_logger = "0.5"
rand = { version = "0.8" }
rand_core = { version = "0.6" }
criterion = "0.5"
quickcheck = "1"
quickcheck_macros = "1"
serde_json = { version = "1.0" }
Expand All @@ -46,9 +48,16 @@ sha3 = "0.10"
# Benchmarking "Ring"
ring = "0.16"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = "0.5"

# Benchmarking "OpenSSL"
# XXX: We don't do this for Windows right now.
[target.'cfg(not(windows))'.dev-dependencies]
# XXX: We don't do this for Windows or wasm right now.
# x86 is usally a cross compilation where this won't work either.
[target.'cfg(all(not(windows), not(target_arch = "wasm32"), not(target_arch = "x86")))'.dev-dependencies]
openssl = "0.10"

[[bench]]
Expand Down Expand Up @@ -76,8 +85,9 @@ name = "drbg"
harness = false

[features]
hacspec = [] # TODO: #7 Use specs instead of efficient implementations
hacspec = [] # TODO: #7 Use specs instead of efficient implementations
rand = []
wasm = ["wasm-bindgen"]

# [patch.crates-io]
# hacl = { git = "https://github.com/cryspen/hacl-packages" }
Expand Down
11 changes: 7 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::env;

fn main() {
if libcrux_platform::simd128_support() {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();

if libcrux_platform::simd128_support() && target_arch == "x86_64" {
println!("cargo:rustc-cfg=simd128");
}
if libcrux_platform::simd256_support() {
if libcrux_platform::simd256_support() && target_arch == "x86_64" {
println!("cargo:rustc-cfg=simd256");
}
if libcrux_platform::bmi2_adx_support() {
println!("cargo:rustc-cfg=bmi2");
println!("cargo:rustc-cfg=adx");
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
franziskuskiefer marked this conversation as resolved.
Show resolved Hide resolved
if libcrux_platform::aes_ni_support() {
if libcrux_platform::aes_ni_support() && target_arch == "x86_64" {
println!("cargo:rustc-cfg=aes_ni");
}
}
2 changes: 2 additions & 0 deletions specs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Cargo.lock
10 changes: 2 additions & 8 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,13 @@ pub type Sha3_512Digest = [u8; digest_size(Algorithm::Sha3_512)];

macro_rules! sha3_impl {
($fun_name:ident, $output:ty, $jasmin_fun:expr, $hacl_fun:expr) => {
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
any(target_os = "linux", target_os = "macos")
))]
#[cfg(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
pub fn $fun_name(payload: &[u8]) -> $output {
// On x64 we use Jasmin for AVX2 and fallback.
$jasmin_fun(payload)
}

#[cfg(not(all(
any(target_arch = "x86", target_arch = "x86_64"),
any(target_os = "linux", target_os = "macos")
)))]
#[cfg(not(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos"))))]
pub fn $fun_name(payload: &[u8]) -> $output {
// On all other platforms we use HACL
$hacl_fun(payload)
Expand Down
1 change: 1 addition & 0 deletions src/hacl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) mod aesgcm;
pub(crate) mod blake2;
pub(crate) mod chacha20_poly1305;
pub(crate) mod curve25519;
#[cfg(not(target_arch ="wasm32"))]
pub(crate) mod drbg;
pub(crate) mod ed25519;
pub(crate) mod hkdf;
Expand Down
91 changes: 0 additions & 91 deletions src/hpke/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,94 +981,3 @@ pub fn ReceiveExport(
}?;
Context_Export(config, &ctx, exporter_context, L)
}

// // === WASM API - NOT HACSPEC === //
// use wasm_bindgen::prelude::*;

// /// ## WASM key gen API.
// ///
// /// This function exposes a simplified API to be called from WASM and panics on
// /// any error.
// ///
// /// It generates x25519 keys sk||pk.
// #[cfg(feature = "wasm")]
// #[wasm_bindgen]
// pub fn hpke_key_gen(randomness: &[u8]) -> Vec<u8> {
// let (sk, pk) = GenerateKeyPair(
// KEM::DHKEM_X25519_HKDF_SHA256,
// Bytes::from_public_slice(&randomness),
// )
// .unwrap();
// let mut out = sk.into_native();
// out.append(&mut pk.into_native());
// out
// }

// /// ## WASM single-shot HPKE seal.
// ///
// /// This function exposes a simplified API to be called from WASM and panics on
// /// any error.
// ///
// /// It uses x25519 as KEM, SHA256 as hash function and Chacha20Poly1305 as AEAD.
// #[cfg(feature = "wasm")]
// #[wasm_bindgen]
// pub fn hpke_seal_base(
// pkR: &[u8],
// info: &[u8],
// aad: &[u8],
// pt: &[u8],
// randomness: &[u8],
// ) -> Vec<u8> {
// let HPKECiphertext(enc, ct) = HpkeSeal(
// HPKEConfig(
// Mode::mode_base,
// KEM::DHKEM_X25519_HKDF_SHA256,
// KDF::HKDF_SHA256,
// AEAD::ChaCha20Poly1305,
// ),
// &Bytes::from_public_slice(pkR),
// &Bytes::from_public_slice(info),
// &Bytes::from_public_slice(aad),
// &Bytes::from_public_slice(pt),
// None,
// None,
// None,
// Bytes::from_public_slice(&randomness),
// )
// .unwrap();
// let mut out = enc.into_native();
// out.append(&mut ct.into_native());
// out
// }

// /// ## WASM single-shot HPKE open.
// ///
// /// This function exposes a simplified API to be called from WASM and panics on
// /// any error.
// ///
// /// It uses x25519 as KEM, SHA256 as hash function and Chacha20Poly1305 as AEAD.
// #[cfg(feature = "wasm")]
// #[wasm_bindgen]
// pub fn hpke_open_base(ctxt: &[u8], enc: &[u8], skR: &[u8], info: &[u8], aad: &[u8]) -> Vec<u8> {
// let ct = HPKECiphertext(
// Bytes::from_public_slice(enc),
// Bytes::from_public_slice(ctxt),
// );
// let pt = HpkeOpen(
// HPKEConfig(
// Mode::mode_base,
// KEM::DHKEM_X25519_HKDF_SHA256,
// KDF::HKDF_SHA256,
// AEAD::ChaCha20Poly1305,
// ),
// &ct,
// &Bytes::from_public_slice(skR),
// &Bytes::from_public_slice(info),
// &Bytes::from_public_slice(aad),
// None,
// None,
// None,
// )
// .unwrap();
// pt.into_native()
// }
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
pub use libcrux_platform::aes_ni_support;

// Jasmin
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
any(target_os = "linux", target_os = "macos")
))]
#[cfg(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
pub(crate) mod jasmin;

// HACL
pub(crate) mod hacl;

// libcrux
pub mod aead;
// The BLS code requires a 64 bit system.
#[cfg(all(not(target_arch = "wasm32"), not(target_arch = "x86")))]
pub mod bls12;
pub mod digest;
// XXX: Looks like the bindings are broken for drbg for some reason.
#[cfg(not(target_arch = "wasm32"))]
pub mod drbg;
pub mod ecdh;
pub mod hkdf;
pub mod hmac;
pub mod hpke;
pub mod kem;
pub mod signature;

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
pub mod wasm;
4 changes: 4 additions & 0 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub fn sign(
)?
}
Algorithm::Ed25519 => {
log::debug!("Signing with ed25519");
log::trace!(" payload: {payload:x?}");
log::trace!(" private_key: {private_key:x?}");
let signature = ed25519::sign(
payload,
private_key.try_into().map_err(|_| Error::SigningError)?,
Expand Down Expand Up @@ -262,6 +265,7 @@ pub fn key_gen(
ecdh::key_gen(ecdh::Algorithm::P256, rng).map_err(|_| Error::KeyGenError)
}
Algorithm::Ed25519 => {
log::debug!("Generating ed25519 key");
const LIMIT: usize = 100;
let mut sk = [0u8; 32];
for _ in 0..LIMIT {
Expand Down
Loading
Loading