Skip to content

Commit 77e85be

Browse files
author
Nicola Dardanis
committed
feat(node): add a web crypto provider for node
1 parent 2ad33d1 commit 77e85be

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ members = [
1212
"mls-rs-crypto-rustcrypto",
1313
"mls-rs-crypto-awslc",
1414
"mls-rs-crypto-webcrypto",
15+
"mls-rs-crypto-webnodecrypto",
1516
"mls-rs-crypto-hpke",
1617
"mls-rs-provider-sqlite",
1718
"mls-rs-codec",
@@ -28,6 +29,7 @@ default-members = [
2829
"mls-rs-crypto-hpke",
2930
"mls-rs-crypto-openssl",
3031
"mls-rs-crypto-rustcrypto",
32+
"mls-rs-crypto-webnodecrypto",
3133
"mls-rs-crypto-awslc",
3234
"mls-rs-crypto-webcrypto",
3335
"mls-rs-provider-sqlite",

mls-rs-crypto-webcrypto/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ repository = "https://github.com/awslabs/mls-rs"
88
keywords = ["mls", "mls-rs"]
99
license = "Apache-2.0 OR MIT"
1010

11+
[features]
12+
web = []
13+
node = []
14+
default = ["web"]
15+
1116
[dependencies]
17+
cfg-if = "1.0.0"
1218
mls-rs-core = { path = "../mls-rs-core", default-features = false, features = ["std"], version = "0.19.0" }
1319
mls-rs-crypto-hpke = { path = "../mls-rs-crypto-hpke", default-features = false, features = ["std"], version = "0.10.0" }
1420
mls-rs-crypto-traits = { path = "../mls-rs-crypto-traits", default-features = false, features = ["std"], version = "0.11.0" }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const crypto = require("node:crypto");
2+
3+
module.exports.node_crypto = function() {
4+
return crypto;
5+
};

mls-rs-crypto-webcrypto/src/lib.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ use mls_rs_crypto_hpke::{
2626
use mls_rs_crypto_traits::{AeadType, KdfType, KemId};
2727

2828
use wasm_bindgen::JsValue;
29+
use wasm_bindgen::prelude::*;
2930
use web_sys::SubtleCrypto;
3031
use zeroize::Zeroizing;
32+
use cfg_if::cfg_if;
3133

3234
use crate::{
3335
aead::Aead,
@@ -61,14 +63,33 @@ impl From<JsValue> for CryptoError {
6163
}
6264
}
6365

64-
#[inline]
65-
pub(crate) fn get_crypto() -> Result<SubtleCrypto, CryptoError> {
66-
Ok(web_sys::window()
67-
.ok_or(CryptoError::WindowNotFound)?
68-
.crypto()?
69-
.subtle())
66+
cfg_if! {
67+
if #[cfg(feature = "web")] {
68+
#[inline]
69+
pub(crate) fn get_crypto() -> Result<SubtleCrypto, CryptoError> {
70+
Ok(web_sys::window()
71+
.ok_or(CryptoError::WindowNotFound)?
72+
.crypto()?
73+
.subtle())
74+
}
75+
} else {
76+
use web_sys::Crypto;
77+
78+
#[wasm_bindgen(module = "/js/node-crypto.js")]
79+
extern "C" {
80+
#[wasm_bindgen]
81+
fn node_crypto() -> Crypto;
82+
}
83+
84+
#[inline]
85+
pub(crate) fn get_crypto() -> Result<SubtleCrypto, CryptoError> {
86+
Ok(node_crypto()
87+
.subtle())
88+
}
89+
}
7090
}
7191

92+
7293
#[derive(Clone, Default, Debug)]
7394
pub struct WebCryptoProvider;
7495

0 commit comments

Comments
 (0)