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

feat: wasi-crypto as an optional wasi feature #70

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[env]
RUST_BACKTRACE = "1"
WASMTIME_BACKTRACE_DETAILS = "1"

[build]
target = "wasm32-wasi"

[target.wasm32-wasi]
rustflags = ["--cfg", "tokio_unstable"]
runner = ["wasmtime", "run", "--env", "FD_COUNT=4", "--tcplisten", "0.0.0.0:3000", "--"]
# runner = ["wasmtime", "run", "--env", "FD_COUNT=4", "--tcplisten", "0.0.0.0:3000", "--"]
# Put this back when Enarx is able to receive TCP configuation from the command line.
# runner = ["enarx", "run", "--wasmcfgfile", "Enarx.toml"]
runner = ["/home/rjzak/bin/wasmtime-wasi-crypto", "--wasi-modules", "experimental-wasi-crypto", "--"]
90 changes: 60 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ tracing = { workspace = true }
[target.'cfg(not(target_os = "wasi"))'.dependencies]
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

[target.'cfg(target_os = "wasi")'.dependencies]
wasi-crypto-guest = { git = "https://github.com/WebAssembly/wasi-crypto", branch = "main", optional = true }

[features]
default = []
wasi-crypto = ["dep:wasi-crypto-guest"]

[profile.release]
incremental = false
codegen-units = 1
Expand Down
51 changes: 51 additions & 0 deletions crates/attestation/src/crypto/hashing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2022 Profian Inc. <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-only

use anyhow::Result;

#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
use anyhow::anyhow;
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
use wasi_crypto_guest::prelude::Hash;

#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))]
use sha2::{Digest, Sha256, Sha384};

#[inline]
pub fn sha256(data: impl AsRef<[u8]>) -> Result<Vec<u8>> {
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
return Ok(Hash::hash("SHA-256", data, 32, None).or_else(|_| Err(anyhow!("hash error")))?);

#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))]
Ok(Sha256::digest(data).as_slice().to_vec())
}

#[inline]
pub fn sha384(data: impl AsRef<[u8]>) -> Result<Vec<u8>> {
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
return Ok(Hash::hash("SHA-384", data, 48, None).or_else(|_| Err(anyhow!("hash error")))?);

#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))]
Ok(Sha384::digest(data).as_slice().to_vec())
}

#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
#[cfg(test)]
mod wasi_crypto {
use crate::{sha256, sha384};
use sha2::Digest;

const DATA: &[u8] = b"SOME_TEST_DATA";

#[test]
fn test_sha256() {
let hash = sha256(DATA).unwrap();
assert_eq!(hash, sha2::Sha256::digest(DATA).as_slice());
}

#[test]
fn test_sha384() {
let hash = sha384(DATA).unwrap();
assert_eq!(hash, sha2::Sha384::digest(DATA).as_slice());
}
}
2 changes: 2 additions & 0 deletions crates/attestation/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

mod cert;
mod certreq;
mod hashing;
mod pki;
mod spki;

pub use self::cert::TbsCertificateExt;
pub use self::certreq::{CertReqExt, CertReqInfoExt};
pub use self::hashing::{sha256, sha384};
pub use self::pki::PrivateKeyInfoExt;
pub use self::spki::SubjectPublicKeyInfoExt;
Loading