@virgilsecurity/virgil-crypto-core 0.18.0-dev.1
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 +193 -0
- package/dist/foundation/browser.cjs.js +1 -0
- package/dist/foundation/browser.es.js +1 -0
- package/dist/foundation/libfoundation.browser.wasm +0 -0
- package/dist/foundation/libfoundation.wasm +0 -0
- package/dist/foundation/libfoundation.worker.wasm +0 -0
- package/dist/foundation/node.cjs.js +1 -0
- package/dist/foundation/node.es.mjs +1 -0
- package/dist/foundation/worker.cjs.js +1 -0
- package/dist/foundation/worker.es.js +1 -0
- package/dist/phe/browser.cjs.js +1 -0
- package/dist/phe/browser.es.js +1 -0
- package/dist/phe/libphe.browser.wasm +0 -0
- package/dist/phe/libphe.wasm +0 -0
- package/dist/phe/libphe.worker.wasm +0 -0
- package/dist/phe/node.cjs.js +1 -0
- package/dist/phe/node.es.mjs +1 -0
- package/dist/phe/worker.cjs.js +1 -0
- package/dist/phe/worker.es.js +1 -0
- package/dist/ratchet/browser.cjs.js +1 -0
- package/dist/ratchet/browser.es.js +1 -0
- package/dist/ratchet/libratchet.browser.wasm +0 -0
- package/dist/ratchet/libratchet.wasm +0 -0
- package/dist/ratchet/libratchet.worker.wasm +0 -0
- package/dist/ratchet/node.cjs.js +1 -0
- package/dist/ratchet/node.es.mjs +1 -0
- package/dist/ratchet/worker.cjs.js +1 -0
- package/dist/ratchet/worker.es.js +1 -0
- package/package.json +99 -0
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @virgilsecurity/virgil-crypto-core
|
|
2
|
+
|
|
3
|
+
WebAssembly wrapper for [Virgil Security Crypto C](https://github.com/VirgilSecurity/virgil-crypto-c).
|
|
4
|
+
|
|
5
|
+
Provides three modules:
|
|
6
|
+
|
|
7
|
+
| Module | Import path | Contents |
|
|
8
|
+
|--------|------------|---------|
|
|
9
|
+
| **Foundation** | `@virgilsecurity/virgil-crypto-core/foundation` | Symmetric/asymmetric encryption, signing, hashing, key management, group sessions |
|
|
10
|
+
| **PHE** | `@virgilsecurity/virgil-crypto-core/phe` | Password-hardened encryption (PHE) and UOKMS |
|
|
11
|
+
| **Ratchet** | `@virgilsecurity/virgil-crypto-core/ratchet` | Double-ratchet end-to-end encrypted sessions |
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @virgilsecurity/virgil-crypto-core
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Each module is initialized asynchronously (WASM loads once, then all classes are available synchronously).
|
|
22
|
+
|
|
23
|
+
### Hashing
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const initFoundation = require('@virgilsecurity/virgil-crypto-core/foundation');
|
|
27
|
+
|
|
28
|
+
const foundation = await initFoundation();
|
|
29
|
+
const sha256 = new foundation.Sha256();
|
|
30
|
+
|
|
31
|
+
const data = Buffer.from('hello world');
|
|
32
|
+
const digest = sha256.hash(data);
|
|
33
|
+
|
|
34
|
+
sha256.delete();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Key generation and encryption
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const initFoundation = require('@virgilsecurity/virgil-crypto-core/foundation');
|
|
41
|
+
|
|
42
|
+
const foundation = await initFoundation();
|
|
43
|
+
|
|
44
|
+
// Generate Ed25519 key pair
|
|
45
|
+
const ed25519 = new foundation.Ed25519();
|
|
46
|
+
ed25519.setupDefaults();
|
|
47
|
+
const privateKey = ed25519.generateKey();
|
|
48
|
+
const publicKey = privateKey.getPublicKey();
|
|
49
|
+
|
|
50
|
+
// Encrypt
|
|
51
|
+
const recipientId = Buffer.from('alice');
|
|
52
|
+
const cipher = new foundation.RecipientCipher();
|
|
53
|
+
cipher.addKeyRecipient(recipientId, publicKey);
|
|
54
|
+
cipher.startEncryption();
|
|
55
|
+
const messageInfo = cipher.packMessageInfo();
|
|
56
|
+
const ciphertext = Buffer.concat([
|
|
57
|
+
messageInfo,
|
|
58
|
+
cipher.processEncryption(Buffer.from('secret message')),
|
|
59
|
+
cipher.finishEncryption(),
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
// Decrypt
|
|
63
|
+
cipher.startDecryptionWithKey(recipientId, privateKey, new Uint8Array());
|
|
64
|
+
const plaintext = Buffer.concat([
|
|
65
|
+
cipher.processDecryption(ciphertext),
|
|
66
|
+
cipher.finishDecryption(),
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
// Free WASM memory explicitly
|
|
70
|
+
cipher.delete();
|
|
71
|
+
ed25519.delete();
|
|
72
|
+
privateKey.delete();
|
|
73
|
+
publicKey.delete();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Signing and verification
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
const initFoundation = require('@virgilsecurity/virgil-crypto-core/foundation');
|
|
80
|
+
|
|
81
|
+
const foundation = await initFoundation();
|
|
82
|
+
|
|
83
|
+
const ed25519 = new foundation.Ed25519();
|
|
84
|
+
ed25519.setupDefaults();
|
|
85
|
+
const privateKey = ed25519.generateKey();
|
|
86
|
+
const publicKey = privateKey.getPublicKey();
|
|
87
|
+
|
|
88
|
+
const signer = new foundation.Signer();
|
|
89
|
+
signer.reset();
|
|
90
|
+
signer.appendData(Buffer.from('message'));
|
|
91
|
+
const signature = signer.sign(privateKey);
|
|
92
|
+
|
|
93
|
+
const verifier = new foundation.Verifier();
|
|
94
|
+
verifier.reset(signature);
|
|
95
|
+
verifier.appendData(Buffer.from('message'));
|
|
96
|
+
const valid = verifier.verify(publicKey); // true
|
|
97
|
+
|
|
98
|
+
signer.delete();
|
|
99
|
+
verifier.delete();
|
|
100
|
+
privateKey.delete();
|
|
101
|
+
publicKey.delete();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### PHE (Password-Hardened Encryption)
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
const initPhe = require('@virgilsecurity/virgil-crypto-core/phe');
|
|
108
|
+
|
|
109
|
+
const phe = await initPhe();
|
|
110
|
+
|
|
111
|
+
const pheServer = new phe.PheServer();
|
|
112
|
+
const pheClient = new phe.PheClient();
|
|
113
|
+
pheServer.setupDefaults();
|
|
114
|
+
pheClient.setupDefaults();
|
|
115
|
+
|
|
116
|
+
const serverKeyPair = pheServer.generateServerKeyPair();
|
|
117
|
+
pheClient.setKeys(clientKeyPair.clientPrivateKey, serverKeyPair.serverPublicKey);
|
|
118
|
+
|
|
119
|
+
const enrollment = pheServer.getEnrollment(
|
|
120
|
+
serverKeyPair.serverPrivateKey,
|
|
121
|
+
serverKeyPair.serverPublicKey,
|
|
122
|
+
);
|
|
123
|
+
const { enrollmentRecord, accountKey } = pheClient.enrollAccount(enrollment, Buffer.from('password'));
|
|
124
|
+
|
|
125
|
+
pheServer.delete();
|
|
126
|
+
pheClient.delete();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Ratchet (double-ratchet E2EE session)
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
const initRatchet = require('@virgilsecurity/virgil-crypto-core/ratchet');
|
|
133
|
+
|
|
134
|
+
const ratchet = await initRatchet();
|
|
135
|
+
|
|
136
|
+
const aliceSession = new ratchet.RatchetSession();
|
|
137
|
+
const bobSession = new ratchet.RatchetSession();
|
|
138
|
+
|
|
139
|
+
// ... initialize sessions with key exchange, then:
|
|
140
|
+
const message = aliceSession.encrypt(Buffer.from('hello bob'));
|
|
141
|
+
const decrypted = bobSession.decryptMessage(message);
|
|
142
|
+
|
|
143
|
+
aliceSession.delete();
|
|
144
|
+
bobSession.delete();
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Platform variants
|
|
148
|
+
|
|
149
|
+
Each module ships three environment builds. Pick the right import path for your target:
|
|
150
|
+
|
|
151
|
+
| Environment | Import path suffix | Format |
|
|
152
|
+
|------------|-------------------|--------|
|
|
153
|
+
| Node.js (default) | `@virgilsecurity/virgil-crypto-core/foundation` | CJS / ESM |
|
|
154
|
+
| Browser | `@virgilsecurity/virgil-crypto-core/foundation/browser` | ESM |
|
|
155
|
+
| Web Worker | `@virgilsecurity/virgil-crypto-core/foundation/worker` | ESM |
|
|
156
|
+
|
|
157
|
+
Replace `foundation` with `phe` or `ratchet` for the other modules.
|
|
158
|
+
|
|
159
|
+
## Memory management
|
|
160
|
+
|
|
161
|
+
WASM objects are not garbage collected. Call `.delete()` on every object when done to release
|
|
162
|
+
WASM heap memory. Failing to do so causes memory leaks in long-lived processes.
|
|
163
|
+
|
|
164
|
+
## Build from source
|
|
165
|
+
|
|
166
|
+
**Prerequisites:** [Emscripten](https://emscripten.org/docs/getting_started/downloads.html) ≥ 3.1 and CMake ≥ 3.16.
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
# 1. Configure and build WASM libraries via CMake (from repo root)
|
|
170
|
+
emcmake cmake -DCMAKE_BUILD_TYPE=Release -Bbuild-wasm -S. \
|
|
171
|
+
-DVIRGIL_LIB_PYTHIA=OFF
|
|
172
|
+
cmake --build build-wasm -j$(nproc)
|
|
173
|
+
|
|
174
|
+
# 2. Install npm dependencies
|
|
175
|
+
cd wrappers/wasm
|
|
176
|
+
npm install
|
|
177
|
+
|
|
178
|
+
# 3. Bundle with Rollup (builds all three modules into dist/)
|
|
179
|
+
npm run prepare
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Testing
|
|
183
|
+
|
|
184
|
+
Tests require the built `dist/` bundles (run `npm run prepare` first).
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
cd wrappers/wasm
|
|
188
|
+
npm test
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
BSD 3-Clause — see [LICENSE](../../LICENSE).
|