Skip to content

Commit

Permalink
wasm update
Browse files Browse the repository at this point in the history
  • Loading branch information
eschorn1 committed Mar 22, 2024
1 parent cee9dcd commit b52a86c
Showing 5 changed files with 26 additions and 22 deletions.
9 changes: 5 additions & 4 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ version = "0.1.3"
authors = ["Eric Schorn <eschorn@integritychain.com>"]
description = "Sample web page utilizing FIPS 203 code"
repository = ""
license = "MIT OR Apache-2.0"
publish = false
edition = "2021"

@@ -17,13 +18,13 @@ default = ["console_error_panic_hook"]


[dependencies]
wasm-bindgen = "0.2.84"
fips203 = { path = "../../fips203", default-features = false, features = ["ml-kem-512"] }
rand_chacha = "0.3.1"
console_error_panic_hook = { version = "0.1.7", optional = true }
rand = "0.8.5"
fips203 = { path = "../../fips203", default-features = false, features = ["ml-kem-512"] }
getrandom = { version = "0.2", features = ["js"] }
hex = "0.4.3"
rand_chacha = "0.3.1"
rand = "0.8.5"
wasm-bindgen = "0.2.84"


[dev-dependencies]
9 changes: 5 additions & 4 deletions wasm/README.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ This is a simple WASM demo for the FIPS 203 code.

~~~
$ cargo install wasm-pack
$ <install Node.js if not already installed>
$ sudo apt install npm
~~~

@@ -15,9 +16,9 @@ This is a simple WASM demo for the FIPS 203 code.
$ cd www
$ npm install
$ npm run start
browse http://localhost:8080/
~~~

If the final step fails, try preceding it with: `$ export NODE_OPTIONS=--openssl-legacy-provider`.

While this simple demo will run as-is, it likely has security vulnerabilities within the npm
dependencies that requires `npm audit fix --force` to resolve.
If the final step fails on newer Node.js versions, try preceding it
with: `$ export NODE_OPTIONS=--openssl-legacy-provider`.
17 changes: 10 additions & 7 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -12,40 +12,43 @@ pub fn run(seed: &str) -> String {

let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);

// Alice runs `key_gen()` and then serializes the encaps key `ek` for Bob (to bytes).
// Alice runs `key_gen()` and then serializes the encaps key `ek` for Bob via `into_bytes().`
let (alice_ek, alice_dk) = ml_kem_512::KG::try_keygen_with_rng_vt(&mut rng).expect("keygen failed");
let alice_ek_bytes = alice_ek.into_bytes();

// Alice sends the encaps key `ek_bytes` to Bob.
let bob_ek_bytes = alice_ek_bytes;

// Bob deserializes the encaps `ek_bytes` and then runs `encaps() to get the shared
// secret `ssk` and ciphertext `ct`. He serializes the ciphertext `ct` for Alice (to bytes).
// Bob deserializes the encaps `ek_bytes` and then runs `encaps() to get the shared secret
// `ssk` and ciphertext `ct`. He serializes the ciphertext `ct` for Alice via `into_bytes()`.
let bob_ek = ml_kem_512::EncapsKey::try_from_bytes(bob_ek_bytes).expect("ek deser failed");
let (bob_ssk, bob_ct) = bob_ek.try_encaps_with_rng_vt(&mut rng).expect("encaps failed");
let bob_ct_bytes = bob_ct.into_bytes();

// Bob sends the ciphertext `ct_bytes` to Alice
// Bob sends the ciphertext `ct_bytes` to Alice.
let alice_ct_bytes = bob_ct_bytes;

// Alice deserializes the ciphertext `ct` and runs `decaps()` with her decaps key
// Alice deserializes the ciphertext `ct` and runs `decaps()` with her decaps key to get her `ssk`.
let alice_ct = ml_kem_512::CipherText::try_from_bytes(alice_ct_bytes).expect("ct deser failed");
let alice_ssk = alice_dk.try_decaps_vt(&alice_ct).expect("decaps failed");

// Alice and Bob will now have the same secret key
// Alice and Bob will now have the same secret key; deserialize to check the underlying byte array.
assert_eq!(bob_ssk.into_bytes(), alice_ssk.clone().into_bytes(), "shared secret not identical");

// Now we encode the relevant values into hex strings
let ek_hex = hex::encode(&bob_ek_bytes);
let ct_hex = hex::encode(&bob_ct_bytes);
let dk_hex = hex::encode(alice_dk.into_bytes());
let ssk_hex = hex::encode(alice_ssk.into_bytes());

// Build the output as a series of strings
let s0 = format!("The seed used to generate the keys is: {}\n\n", seed);
let s1 = format!("The generated encaps key is: {}\n", ek_hex);
let s2 = format!("The generated decaps key is: {}\n\n", dk_hex);
let s3 = format!("The generated ciphertext is: {}\n\n", ct_hex);
let s4 = format!("The shared secret is: {}\n", ssk_hex);
let s5 = "Alice and Bob have an identical shared secret."; // because the above assert! passed
let s5 = "Alice and Bob have an identical shared secret."; // because the above assert_eq! passed

// Return the concatenated strings as the output
(s0 + &s1 + &s2 + &s3 + &s4 + &s5).into()
}
8 changes: 4 additions & 4 deletions wasm/www/package.json
Original file line number Diff line number Diff line change
@@ -30,10 +30,10 @@
"wasm": "file:../pkg"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"hello-wasm-pack": "^0.1.0",
"webpack": "^4.29.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"copy-webpack-plugin": "^5.0.0"
"webpack": "^5.88.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
5 changes: 2 additions & 3 deletions wasm/www/webpack.config.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ module.exports = {
filename: "bootstrap.js",
},
mode: "development",
plugins: [
new CopyWebpackPlugin(['index.html'])
],
plugins: [new CopyWebpackPlugin({patterns: ["index.html"]})],
experiments: {asyncWebAssembly: true},
};

0 comments on commit b52a86c

Please sign in to comment.