@stenvault/pqc-wasm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/package.json +48 -0
- package/src/core.d.ts +11 -0
- package/src/core.js +73 -0
- package/src/index.browser.d.ts +9 -0
- package/src/index.browser.js +44 -0
- package/src/index.node.d.ts +9 -0
- package/src/index.node.js +41 -0
- package/src/types.d.ts +21 -0
- package/src/types.js +6 -0
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# @stenvault/pqc-wasm
|
|
2
|
+
|
|
3
|
+
Post-quantum cryptography WASM wrapper for StenVault — ML-KEM-768 (FIPS 203) and ML-DSA-65 (FIPS 204) compiled from RustCrypto crates to WebAssembly.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Replaces `@openforge-sh/liboqs` (unofficial C wrapper, single anonymous maintainer, 0 stars) with a self-maintained Rust→WASM build using the RustCrypto ecosystem (`ml-kem` + `ml-dsa` crates).
|
|
8
|
+
|
|
9
|
+
WASM preserves constant-time properties from the Rust `subtle` crate — pure JS alternatives (noble-post-quantum, mlkem) cannot guarantee this due to V8 JIT and GC interference.
|
|
10
|
+
|
|
11
|
+
## Build
|
|
12
|
+
|
|
13
|
+
Requires Rust toolchain with `wasm32-unknown-unknown` target and `wasm-pack`:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
rustup target add wasm32-unknown-unknown
|
|
17
|
+
cargo install wasm-pack
|
|
18
|
+
|
|
19
|
+
# Build both targets
|
|
20
|
+
wasm-pack build --target bundler --release --out-dir pkg-bundler
|
|
21
|
+
wasm-pack build --target nodejs --release --out-dir pkg-nodejs
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import {
|
|
28
|
+
generateKemKeyPair,
|
|
29
|
+
encapsulate,
|
|
30
|
+
decapsulate,
|
|
31
|
+
generateSignatureKeyPair,
|
|
32
|
+
sign,
|
|
33
|
+
verify,
|
|
34
|
+
} from '@stenvault/pqc-wasm'
|
|
35
|
+
|
|
36
|
+
// ML-KEM-768
|
|
37
|
+
const kp = await generateKemKeyPair()
|
|
38
|
+
// kp.publicKey: Uint8Array (1,184 bytes)
|
|
39
|
+
// kp.secretKey: Uint8Array (2,400 bytes)
|
|
40
|
+
|
|
41
|
+
const { ciphertext, sharedSecret } = await encapsulate(kp.publicKey)
|
|
42
|
+
const decrypted = await decapsulate(ciphertext, kp.secretKey)
|
|
43
|
+
// sharedSecret === decrypted (32 bytes)
|
|
44
|
+
|
|
45
|
+
// ML-DSA-65
|
|
46
|
+
const sigKp = await generateSignatureKeyPair()
|
|
47
|
+
// sigKp.publicKey: Uint8Array (1,952 bytes)
|
|
48
|
+
// sigKp.secretKey: Uint8Array (4,032 bytes)
|
|
49
|
+
|
|
50
|
+
const signature = await sign(message, sigKp.secretKey)
|
|
51
|
+
// signature: Uint8Array (3,309 bytes)
|
|
52
|
+
|
|
53
|
+
const valid = await verify(message, signature, sigKp.publicKey)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Architecture
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
Consumer (StenVault)
|
|
60
|
+
↓ imports from "@stenvault/pqc-wasm"
|
|
61
|
+
index.browser.ts / index.node.ts ← ensurePqcInit() + re-export
|
|
62
|
+
↓
|
|
63
|
+
core.ts ← free() in try/finally, param order normalization
|
|
64
|
+
↓
|
|
65
|
+
pkg-bundler/ or pkg-nodejs/ ← wasm-bindgen generated bindings
|
|
66
|
+
↓
|
|
67
|
+
src/lib.rs ← 7 Rust functions, zeroize on Drop
|
|
68
|
+
↓
|
|
69
|
+
ml-kem + ml-dsa (RustCrypto) ← FIPS 203/204 implementations
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The consumer never calls `free()`, never calls `init()`, and never imports from `pkg-*` directly. The `exports` field in `package.json` routes `browser` to `pkg-bundler` and `node`/`default` to `pkg-nodejs` automatically.
|
|
73
|
+
|
|
74
|
+
## Key sizes (FIPS)
|
|
75
|
+
|
|
76
|
+
| Algorithm | Public Key | Secret Key | Ciphertext | Signature | Shared Secret |
|
|
77
|
+
|-----------|-----------|-----------|-----------|----------|--------------|
|
|
78
|
+
| ML-KEM-768 | 1,184 B | 2,400 B | 1,088 B | — | 32 B |
|
|
79
|
+
| ML-DSA-65 | 1,952 B | 4,032 B | — | 3,309 B | — |
|
|
80
|
+
|
|
81
|
+
## Security
|
|
82
|
+
|
|
83
|
+
- **Constant-time**: `subtle` crate in Rust, preserved through WASM compilation
|
|
84
|
+
- **Memory zeroing**: `zeroize` crate with `#[derive(ZeroizeOnDrop)]` on all secret-holding structs
|
|
85
|
+
- **No audit**: Neither RustCrypto nor this wrapper have been independently audited. RustCrypto is honest about this. See `PLAN_stenvault_pqc_wasm.md` section 10 for full risk analysis.
|
|
86
|
+
- **CVE-2026-22705**: Timing side-channel in `ml-dsa` Decompose function, patched in `>= 0.1.0-rc.3` (Barrett reduction). Cargo.toml pins `>= 0.1.0-rc.3, < 0.2`.
|
|
87
|
+
- **Supply chain**: `Cargo.lock` committed, CI runs `cargo audit` before every build.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stenvault/pqc-wasm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "ML-KEM-768 + ML-DSA-65 WASM wrapper — RustCrypto compiled to WebAssembly",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/StenVault/pqc-wasm"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./src/index.node.js",
|
|
12
|
+
"types": "./src/types.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"browser": {
|
|
16
|
+
"types": "./src/index.browser.d.ts",
|
|
17
|
+
"default": "./src/index.browser.js"
|
|
18
|
+
},
|
|
19
|
+
"node": {
|
|
20
|
+
"types": "./src/index.node.d.ts",
|
|
21
|
+
"default": "./src/index.node.js"
|
|
22
|
+
},
|
|
23
|
+
"default": {
|
|
24
|
+
"types": "./src/index.node.d.ts",
|
|
25
|
+
"default": "./src/index.node.js"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src/*.js",
|
|
31
|
+
"src/*.d.ts",
|
|
32
|
+
"pkg-bundler/",
|
|
33
|
+
"pkg-nodejs/"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build:bundler": "wasm-pack build --target bundler --release --out-dir pkg-bundler",
|
|
37
|
+
"build:nodejs": "wasm-pack build --target nodejs --release --out-dir pkg-nodejs",
|
|
38
|
+
"build:wasm": "npm run build:bundler && npm run build:nodejs",
|
|
39
|
+
"build:ts": "tsc",
|
|
40
|
+
"build": "npm run build:wasm && npm run build:ts",
|
|
41
|
+
"test": "npx tsx tests/interop.ts"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@openforge-sh/liboqs": "^0.14.3",
|
|
45
|
+
"tsx": "^4.21.0",
|
|
46
|
+
"typescript": "^5.7.0"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/core.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { KemKeyPair, EncapsulateResult, SignatureKeyPair } from './types.js';
|
|
2
|
+
type WasmBindings = typeof import('../pkg-bundler/stenvault_pqc.js');
|
|
3
|
+
export declare function setWasmBindings(bindings: WasmBindings): void;
|
|
4
|
+
export declare function generateKemKeyPairCore(): KemKeyPair;
|
|
5
|
+
export declare function encapsulateCore(publicKey: Uint8Array): EncapsulateResult;
|
|
6
|
+
export declare function decapsulateCore(ciphertext: Uint8Array, secretKey: Uint8Array): Uint8Array;
|
|
7
|
+
export declare function generateSignatureKeyPairCore(): SignatureKeyPair;
|
|
8
|
+
export declare function signCore(message: Uint8Array, secretKey: Uint8Array): Uint8Array;
|
|
9
|
+
export declare function verifyCore(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=core.d.ts.map
|
package/src/core.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Core implementation — wraps the Rust WASM API.
|
|
2
|
+
// All free() calls happen here in try/finally — never exposed to the consumer.
|
|
3
|
+
// Parameter order is normalized here: StenVault convention (ct, sk), (msg, sk)
|
|
4
|
+
// maps to Rust convention (dk, ct), (sk, msg) internally.
|
|
5
|
+
//
|
|
6
|
+
// This file is imported by index.browser.ts and index.node.ts, which each
|
|
7
|
+
// provide their own ensurePqcInit() before delegating here.
|
|
8
|
+
let wasm = null;
|
|
9
|
+
export function setWasmBindings(bindings) {
|
|
10
|
+
wasm = bindings;
|
|
11
|
+
}
|
|
12
|
+
function getWasm() {
|
|
13
|
+
if (!wasm)
|
|
14
|
+
throw new Error('@stenvault/pqc-wasm: WASM not initialized. This is a bug.');
|
|
15
|
+
return wasm;
|
|
16
|
+
}
|
|
17
|
+
// ── ML-KEM-768 ──────────────────────────────────────────────────────────────
|
|
18
|
+
export function generateKemKeyPairCore() {
|
|
19
|
+
const w = getWasm();
|
|
20
|
+
const kp = w.ml_kem_768_generate();
|
|
21
|
+
try {
|
|
22
|
+
return {
|
|
23
|
+
publicKey: new Uint8Array(kp.encapsulation_key),
|
|
24
|
+
secretKey: new Uint8Array(kp.decapsulation_key),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
kp.free();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function encapsulateCore(publicKey) {
|
|
32
|
+
const w = getWasm();
|
|
33
|
+
const result = w.ml_kem_768_encapsulate(publicKey);
|
|
34
|
+
try {
|
|
35
|
+
return {
|
|
36
|
+
ciphertext: new Uint8Array(result.ciphertext),
|
|
37
|
+
sharedSecret: new Uint8Array(result.shared_secret),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
result.free();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function decapsulateCore(ciphertext, secretKey) {
|
|
45
|
+
const w = getWasm();
|
|
46
|
+
// Rust API: (dk, ct) — we receive (ct, sk) from StenVault convention
|
|
47
|
+
return new Uint8Array(w.ml_kem_768_decapsulate(secretKey, ciphertext));
|
|
48
|
+
}
|
|
49
|
+
// ── ML-DSA-65 ───────────────────────────────────────────────────────────────
|
|
50
|
+
export function generateSignatureKeyPairCore() {
|
|
51
|
+
const w = getWasm();
|
|
52
|
+
const kp = w.ml_dsa_65_generate();
|
|
53
|
+
try {
|
|
54
|
+
return {
|
|
55
|
+
publicKey: new Uint8Array(kp.verifying_key),
|
|
56
|
+
secretKey: new Uint8Array(kp.signing_key),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
kp.free();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function signCore(message, secretKey) {
|
|
64
|
+
const w = getWasm();
|
|
65
|
+
// Rust API: (sk, msg) — we receive (msg, sk) from StenVault convention
|
|
66
|
+
return new Uint8Array(w.ml_dsa_65_sign(secretKey, message));
|
|
67
|
+
}
|
|
68
|
+
export function verifyCore(message, signature, publicKey) {
|
|
69
|
+
const w = getWasm();
|
|
70
|
+
// Rust API: (vk, msg, sig) — we receive (msg, sig, pk) from StenVault convention
|
|
71
|
+
return w.ml_dsa_65_verify(publicKey, message, signature);
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=core.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { KemKeyPair, EncapsulateResult, SignatureKeyPair } from './types.js';
|
|
2
|
+
export type { KemKeyPair, EncapsulateResult, SignatureKeyPair, PqcApi } from './types.js';
|
|
3
|
+
export declare function generateKemKeyPair(): Promise<KemKeyPair>;
|
|
4
|
+
export declare function encapsulate(publicKey: Uint8Array): Promise<EncapsulateResult>;
|
|
5
|
+
export declare function decapsulate(ciphertext: Uint8Array, secretKey: Uint8Array): Promise<Uint8Array>;
|
|
6
|
+
export declare function generateSignatureKeyPair(): Promise<SignatureKeyPair>;
|
|
7
|
+
export declare function sign(message: Uint8Array, secretKey: Uint8Array): Promise<Uint8Array>;
|
|
8
|
+
export declare function verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
|
|
9
|
+
//# sourceMappingURL=index.browser.d.ts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Browser entry point — uses pkg-bundler (Vite/webpack ESM)
|
|
2
|
+
// ensurePqcInit() is internal, never exposed to the consumer.
|
|
3
|
+
//
|
|
4
|
+
// wasm-bindgen bundler target auto-initializes via:
|
|
5
|
+
// import * as wasm from "./stenvault_pqc_bg.wasm"
|
|
6
|
+
// The bundler (Vite/webpack) resolves the .wasm import at build time.
|
|
7
|
+
// No explicit init() call is needed — bindings are ready after import.
|
|
8
|
+
import * as wasmBindings from '../pkg-bundler/stenvault_pqc.js';
|
|
9
|
+
import { setWasmBindings, generateKemKeyPairCore, encapsulateCore, decapsulateCore, generateSignatureKeyPairCore, signCore, verifyCore } from './core.js';
|
|
10
|
+
// ── WASM init — invisible to consumer ───────────────────────────────────────
|
|
11
|
+
let initialized = false;
|
|
12
|
+
function ensurePqcInit() {
|
|
13
|
+
if (!initialized) {
|
|
14
|
+
setWasmBindings(wasmBindings);
|
|
15
|
+
initialized = true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// ── ML-KEM-768 — public stable API ─────────────────────────────────────────
|
|
19
|
+
export async function generateKemKeyPair() {
|
|
20
|
+
await ensurePqcInit();
|
|
21
|
+
return generateKemKeyPairCore();
|
|
22
|
+
}
|
|
23
|
+
export async function encapsulate(publicKey) {
|
|
24
|
+
await ensurePqcInit();
|
|
25
|
+
return encapsulateCore(publicKey);
|
|
26
|
+
}
|
|
27
|
+
export async function decapsulate(ciphertext, secretKey) {
|
|
28
|
+
await ensurePqcInit();
|
|
29
|
+
return decapsulateCore(ciphertext, secretKey);
|
|
30
|
+
}
|
|
31
|
+
// ── ML-DSA-65 — public stable API ──────────────────────────────────────────
|
|
32
|
+
export async function generateSignatureKeyPair() {
|
|
33
|
+
await ensurePqcInit();
|
|
34
|
+
return generateSignatureKeyPairCore();
|
|
35
|
+
}
|
|
36
|
+
export async function sign(message, secretKey) {
|
|
37
|
+
await ensurePqcInit();
|
|
38
|
+
return signCore(message, secretKey);
|
|
39
|
+
}
|
|
40
|
+
export async function verify(message, signature, publicKey) {
|
|
41
|
+
await ensurePqcInit();
|
|
42
|
+
return verifyCore(message, signature, publicKey);
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { KemKeyPair, EncapsulateResult, SignatureKeyPair } from './types.js';
|
|
2
|
+
export type { KemKeyPair, EncapsulateResult, SignatureKeyPair, PqcApi } from './types.js';
|
|
3
|
+
export declare function generateKemKeyPair(): Promise<KemKeyPair>;
|
|
4
|
+
export declare function encapsulate(publicKey: Uint8Array): Promise<EncapsulateResult>;
|
|
5
|
+
export declare function decapsulate(ciphertext: Uint8Array, secretKey: Uint8Array): Promise<Uint8Array>;
|
|
6
|
+
export declare function generateSignatureKeyPair(): Promise<SignatureKeyPair>;
|
|
7
|
+
export declare function sign(message: Uint8Array, secretKey: Uint8Array): Promise<Uint8Array>;
|
|
8
|
+
export declare function verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
|
|
9
|
+
//# sourceMappingURL=index.node.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Node.js entry point — uses pkg-nodejs (CommonJS + fs WASM loading)
|
|
2
|
+
// ensurePqcInit() is internal, never exposed to the consumer.
|
|
3
|
+
import * as wasmBindings from '../pkg-nodejs/stenvault_pqc.js';
|
|
4
|
+
import { setWasmBindings, generateKemKeyPairCore, encapsulateCore, decapsulateCore, generateSignatureKeyPairCore, signCore, verifyCore } from './core.js';
|
|
5
|
+
// ── WASM init — invisible to consumer ───────────────────────────────────────
|
|
6
|
+
let initialized = false;
|
|
7
|
+
function ensurePqcInit() {
|
|
8
|
+
if (!initialized) {
|
|
9
|
+
// pkg-nodejs target loads WASM synchronously via fs.readFileSync
|
|
10
|
+
// No async init() needed — bindings are ready after require/import
|
|
11
|
+
setWasmBindings(wasmBindings);
|
|
12
|
+
initialized = true;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// ── ML-KEM-768 — public stable API ─────────────────────────────────────────
|
|
16
|
+
export async function generateKemKeyPair() {
|
|
17
|
+
ensurePqcInit();
|
|
18
|
+
return generateKemKeyPairCore();
|
|
19
|
+
}
|
|
20
|
+
export async function encapsulate(publicKey) {
|
|
21
|
+
ensurePqcInit();
|
|
22
|
+
return encapsulateCore(publicKey);
|
|
23
|
+
}
|
|
24
|
+
export async function decapsulate(ciphertext, secretKey) {
|
|
25
|
+
ensurePqcInit();
|
|
26
|
+
return decapsulateCore(ciphertext, secretKey);
|
|
27
|
+
}
|
|
28
|
+
// ── ML-DSA-65 — public stable API ──────────────────────────────────────────
|
|
29
|
+
export async function generateSignatureKeyPair() {
|
|
30
|
+
ensurePqcInit();
|
|
31
|
+
return generateSignatureKeyPairCore();
|
|
32
|
+
}
|
|
33
|
+
export async function sign(message, secretKey) {
|
|
34
|
+
ensurePqcInit();
|
|
35
|
+
return signCore(message, secretKey);
|
|
36
|
+
}
|
|
37
|
+
export async function verify(message, signature, publicKey) {
|
|
38
|
+
ensurePqcInit();
|
|
39
|
+
return verifyCore(message, signature, publicKey);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=index.node.js.map
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface KemKeyPair {
|
|
2
|
+
publicKey: Uint8Array;
|
|
3
|
+
secretKey: Uint8Array;
|
|
4
|
+
}
|
|
5
|
+
export interface EncapsulateResult {
|
|
6
|
+
ciphertext: Uint8Array;
|
|
7
|
+
sharedSecret: Uint8Array;
|
|
8
|
+
}
|
|
9
|
+
export interface SignatureKeyPair {
|
|
10
|
+
publicKey: Uint8Array;
|
|
11
|
+
secretKey: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
export interface PqcApi {
|
|
14
|
+
generateKemKeyPair(): Promise<KemKeyPair>;
|
|
15
|
+
encapsulate(publicKey: Uint8Array): Promise<EncapsulateResult>;
|
|
16
|
+
decapsulate(ciphertext: Uint8Array, secretKey: Uint8Array): Promise<Uint8Array>;
|
|
17
|
+
generateSignatureKeyPair(): Promise<SignatureKeyPair>;
|
|
18
|
+
sign(message: Uint8Array, secretKey: Uint8Array): Promise<Uint8Array>;
|
|
19
|
+
verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=types.d.ts.map
|
package/src/types.js
ADDED