Skip to content

Commit 0170702

Browse files
committed
feat: use wasi-crypto as an optionl wasi feature
Signed-off-by: Richard Zak <[email protected]>
1 parent 5fb350a commit 0170702

File tree

9 files changed

+268
-48
lines changed

9 files changed

+268
-48
lines changed

.cargo/config

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
[env]
2+
RUST_BACKTRACE = "1"
3+
WASMTIME_BACKTRACE_DETAILS = "1"
4+
5+
[build]
6+
target = "wasm32-wasi"
7+
18
[target.wasm32-wasi]
29
rustflags = ["--cfg", "tokio_unstable"]
3-
runner = ["wasmtime", "run", "--env", "FD_COUNT=4", "--tcplisten", "0.0.0.0:3000", "--"]
10+
# runner = ["wasmtime", "run", "--env", "FD_COUNT=4", "--tcplisten", "0.0.0.0:3000", "--"]
411
# Put this back when Enarx is able to receive TCP configuation from the command line.
512
# runner = ["enarx", "run", "--wasmcfgfile", "Enarx.toml"]
13+
runner = ["/home/rjzak/bin/wasmtime-wasi-crypto", "--wasi-modules", "experimental-wasi-crypto", "--"]

Cargo.lock

Lines changed: 86 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ confargs = { version = "^0.1.3", default-features = false }
3434
[target.'cfg(not(target_os = "wasi"))'.dependencies]
3535
tokio = { version = "^1.21.2", features = ["rt-multi-thread", "macros"], default-features = false }
3636

37+
[target.'cfg(target_os = "wasi")'.dependencies]
38+
wasi-crypto-guest = { git = "https://github.com/WebAssembly/wasi-crypto", branch = "main", optional = true }
39+
3740
[dev-dependencies]
3841
tower = { version = "^0.4.11", features = ["util"], default-features = false }
3942
axum = { version = "^0.5.17", default-features = false }
@@ -42,6 +45,10 @@ memoffset = { version = "0.7.1", default-features = false }
4245
rstest = { version = "0.15", default-features = false }
4346
testaso = { version = "0.1", default-features = false }
4447

48+
[features]
49+
default = []
50+
wasi-crypto = ["dep:wasi-crypto-guest"]
51+
4552
[profile.release]
4653
incremental = false
4754
codegen-units = 1
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-FileCopyrightText: 2022 Profian Inc. <[email protected]>
2+
// SPDX-License-Identifier: AGPL-3.0-only
3+
4+
use anyhow::Result;
5+
6+
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
7+
use anyhow::anyhow;
8+
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
9+
use wasi_crypto_guest::prelude::Hash;
10+
11+
#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))]
12+
use sha2::{Digest, Sha256, Sha384};
13+
14+
#[inline]
15+
pub fn sha256(data: impl AsRef<[u8]>) -> Result<Vec<u8>> {
16+
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
17+
return Ok(Hash::hash("SHA-256", data, 32, None).or_else(|_| Err(anyhow!("hash error")))?);
18+
19+
#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))]
20+
Ok(Sha256::digest(data).as_slice().to_vec())
21+
}
22+
23+
#[inline]
24+
pub fn sha384(data: impl AsRef<[u8]>) -> Result<Vec<u8>> {
25+
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
26+
return Ok(Hash::hash("SHA-384", data, 48, None).or_else(|_| Err(anyhow!("hash error")))?);
27+
28+
#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))]
29+
Ok(Sha384::digest(data).as_slice().to_vec())
30+
}
31+
32+
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))]
33+
#[cfg(test)]
34+
mod wasi_crypto {
35+
use crate::{sha256, sha384};
36+
use sha2::Digest;
37+
38+
const DATA: &[u8] = b"SOME_TEST_DATA";
39+
40+
#[test]
41+
fn test_sha256() {
42+
let hash = sha256(DATA).unwrap();
43+
assert_eq!(hash, sha2::Sha256::digest(DATA).as_slice());
44+
}
45+
46+
#[test]
47+
fn test_sha384() {
48+
let hash = sha384(DATA).unwrap();
49+
assert_eq!(hash, sha2::Sha384::digest(DATA).as_slice());
50+
}
51+
}

crates/cryptography/src/ext/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
mod cert;
55
mod certreq;
6+
mod hashing;
67
mod pki;
78
mod spki;
89

910
pub use self::cert::TbsCertificateExt;
1011
pub use self::certreq::{CertReqExt, CertReqInfoExt};
12+
pub use self::hashing::{sha256, sha384};
1113
pub use self::pki::PrivateKeyInfoExt;
1214
pub use self::spki::SubjectPublicKeyInfoExt;

0 commit comments

Comments
 (0)