@nexwage/crypto 0.1.0 → 0.1.2
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 +354 -96
- package/dist/crypto/group.d.ts +102 -0
- package/dist/crypto/group.d.ts.map +1 -0
- package/dist/crypto/group.js +228 -0
- package/dist/crypto/group.js.map +1 -0
- package/dist/crypto/key-exchange.d.ts +61 -0
- package/dist/crypto/key-exchange.d.ts.map +1 -0
- package/dist/crypto/key-exchange.js +89 -0
- package/dist/crypto/key-exchange.js.map +1 -0
- package/dist/crypto/signature.d.ts +55 -0
- package/dist/crypto/signature.d.ts.map +1 -0
- package/dist/crypto/signature.js +95 -0
- package/dist/crypto/signature.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
# @nexwage/crypto
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Library kripto minimal berbasis libsodium untuk alur master key, recovery key, dan enkripsi file. Menggunakan XChaCha20-Poly1305 untuk AEAD dan Argon2 untuk KDF password.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Instalasi
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
8
|
npm i @nexwage/crypto
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Kompatibilitas
|
|
12
|
+
|
|
13
|
+
- ESM (type: module)
|
|
14
|
+
- Node.js 18+ (direkomendasikan)
|
|
15
|
+
|
|
16
|
+
## Mulai Cepat
|
|
12
17
|
|
|
13
18
|
```ts
|
|
14
19
|
import {
|
|
@@ -18,12 +23,14 @@ import {
|
|
|
18
23
|
wrapMasterKeyWithRecoveryKey,
|
|
19
24
|
generateFileKey,
|
|
20
25
|
wrapFileKeyWithMasterKey,
|
|
26
|
+
unwrapFileKeyWithMasterKey,
|
|
21
27
|
encryptFileData,
|
|
22
28
|
decryptFileData,
|
|
29
|
+
fromString,
|
|
23
30
|
} from "@nexwage/crypto";
|
|
24
31
|
|
|
25
32
|
const password = "strong-password";
|
|
26
|
-
const aad =
|
|
33
|
+
const aad = fromString("uid:user_123|app:v1|slot:password");
|
|
27
34
|
|
|
28
35
|
const { masterKey, wrapped } = await generateMasterKeyWithPassword(
|
|
29
36
|
password,
|
|
@@ -41,7 +48,7 @@ const wrappedByRecovery = await wrapMasterKeyWithRecoveryKey(
|
|
|
41
48
|
const fileKey = await generateFileKey();
|
|
42
49
|
const wrappedFileKey = await wrapFileKeyWithMasterKey(fileKey, masterKey, aad);
|
|
43
50
|
|
|
44
|
-
const fileBytes =
|
|
51
|
+
const fileBytes = fromString("file rahasia");
|
|
45
52
|
const encryptedFile = await encryptFileData(fileKey, fileBytes, aad);
|
|
46
53
|
|
|
47
54
|
const masterKey2 = await unwrapMasterKeyWithPassword(password, wrapped, aad);
|
|
@@ -53,36 +60,357 @@ const fileKey2 = await unwrapFileKeyWithMasterKey(
|
|
|
53
60
|
const decryptedFile = await decryptFileData(fileKey2, encryptedFile, aad);
|
|
54
61
|
```
|
|
55
62
|
|
|
56
|
-
##
|
|
63
|
+
## Konsep Utama
|
|
64
|
+
|
|
65
|
+
- Master Key: kunci utama untuk membuka kunci lain.
|
|
66
|
+
- Recovery Key: kunci cadangan jika password hilang.
|
|
67
|
+
- File Key: kunci khusus per file.
|
|
68
|
+
- Wrap/Unwrap: membungkus dan membuka kunci memakai AEAD.
|
|
69
|
+
- AAD: data konteks yang diikat ke ciphertext (tidak dienkripsi).
|
|
70
|
+
- Versioning: field `v` pada payload untuk kompatibilitas format.
|
|
71
|
+
|
|
72
|
+
## Alur Dasar
|
|
73
|
+
|
|
74
|
+
Alur 1: Setup awal + upload
|
|
75
|
+
1) Generate master key lalu wrap dengan password.
|
|
76
|
+
2) Generate recovery key.
|
|
77
|
+
3) Wrap master key dengan recovery key (recovery).
|
|
78
|
+
4) Wrap recovery key dengan master key (backup recovery key).
|
|
79
|
+
5) Generate file key.
|
|
80
|
+
6) Wrap file key dengan master key.
|
|
81
|
+
7) Enkripsi file pakai file key.
|
|
82
|
+
|
|
83
|
+
Alur 2: Buka file (login normal)
|
|
84
|
+
1) Ambil wrapped master key.
|
|
85
|
+
2) Unwrap master key pakai password.
|
|
86
|
+
3) Unwrap file key pakai master key.
|
|
87
|
+
4) Dekripsi file.
|
|
88
|
+
|
|
89
|
+
Alur 3: Recovery + re-encrypt
|
|
90
|
+
1) Ambil wrapped master key.
|
|
91
|
+
2) Unwrap master key pakai recovery key.
|
|
92
|
+
3) Re-wrap master key dengan password baru.
|
|
93
|
+
|
|
94
|
+
## Referensi API
|
|
95
|
+
|
|
96
|
+
Semua fungsi diekspor dari `@nexwage/crypto`.
|
|
97
|
+
|
|
98
|
+
### Peta Penggunaan di Kasus Industri (Per Domain)
|
|
99
|
+
|
|
100
|
+
#### Simetris (Password + File + Recovery)
|
|
101
|
+
|
|
102
|
+
| Metode/Fitur | Kasus Industri Nyata | Kenapa Dipakai | Contoh Implementasi |
|
|
103
|
+
| --- | --- | --- | --- |
|
|
104
|
+
| Master Key + Password KDF | Zero‑knowledge SaaS (password vault, secure notes) | Password tidak pernah dikirim ke server; master key tetap terenkripsi | `generateMasterKeyWithPassword` saat signup, simpan `wrapped` di server |
|
|
105
|
+
| Recovery Key | Recovery akun tanpa reset password | Memberi opsi recovery tanpa membuka password | Simpan `wrapMasterKeyWithRecoveryKey` di server, recovery key dipegang user |
|
|
106
|
+
| File Key + Wrap | Enkripsi per file untuk performa dan isolasi | Setiap file punya key sendiri; kompromi satu file tidak bocor semua | `generateFileKey` + `wrapFileKeyWithMasterKey` per file |
|
|
107
|
+
| AEAD XChaCha20-Poly1305 | Enkripsi data at rest/in transit | Authenticated encryption, tahan tampering | `encryptFileData` / `decryptFileData` |
|
|
108
|
+
| AAD | Bind data ke konteks (userId, fileId, slot) | Mencegah swap/replay antar konteks | AAD berisi `userId|fileId|slot` |
|
|
109
|
+
| Versioning (`v`) | Migrasi format/enkripsi | Menjaga kompatibilitas payload lama | Simpan `v` di payload, upgrade saat decrypt |
|
|
110
|
+
|
|
111
|
+
#### Asimetris (Key Exchange + Signature)
|
|
112
|
+
|
|
113
|
+
| Metode/Fitur | Kasus Industri Nyata | Kenapa Dipakai | Contoh Implementasi |
|
|
114
|
+
| --- | --- | --- | --- |
|
|
115
|
+
| Key Exchange (X25519) | Secure session key (chat, realtime sync) | Negosiasi kunci simetris tanpa berbagi rahasia | `deriveClientSessionKeys` + `deriveServerSessionKeys` |
|
|
116
|
+
| Sealed Box | Share ke penerima tanpa keypair pengirim | Enkripsi langsung ke public key penerima | `encryptSealedBox` untuk file sharing 1:1 |
|
|
117
|
+
| Signature (Ed25519) | Audit log, proof of authorship | Verifikasi integritas dan sumber data | Tanda tangan metadata/commit dengan `signMessage` |
|
|
118
|
+
| Signature Anti‑Replay | API request sensitif (approve, payment, revoke) | Mencegah replay dengan nonce+timestamp | `signMessageWithNonce` + simpan nonce di server |
|
|
119
|
+
|
|
120
|
+
#### Group (MLS-like)
|
|
121
|
+
|
|
122
|
+
| Metode/Fitur | Kasus Industri Nyata | Kenapa Dipakai | Contoh Implementasi |
|
|
123
|
+
| --- | --- | --- | --- |
|
|
124
|
+
| Group Commit + Epoch | Team sharing (drive tim, workspace) | Rotasi kunci saat member berubah | `createGroupCommit` + `applyGroupCommit` setiap perubahan anggota |
|
|
125
|
+
| Group Key Derivation | Enkripsi grup skala besar | Satu group key untuk banyak file | `deriveGroupKey` untuk wrap file key |
|
|
126
|
+
| Welcome Payload | Onboarding member baru | Distribusi secret awal dengan aman | `createGroupState` + `openGroupWelcome` |
|
|
127
|
+
|
|
128
|
+
#### Wrapper libsodium
|
|
129
|
+
|
|
130
|
+
| Metode/Fitur | Kasus Industri Nyata | Kenapa Dipakai | Contoh Implementasi |
|
|
131
|
+
| --- | --- | --- | --- |
|
|
132
|
+
| libsodium Wrapper | Akses primitive low-level | Integrasi fleksibel ke arsitektur existing | `cryptoPwhash`, `aeadXChaCha20Poly1305IetfEncrypt` |
|
|
133
|
+
|
|
134
|
+
### Tipe Export
|
|
135
|
+
|
|
136
|
+
- `AeadEnvelopeV1`
|
|
137
|
+
- `PasswordKdfParamsV1`
|
|
138
|
+
- `PasswordWrappedKeyV1`
|
|
139
|
+
- `PasswordKdfOptions`
|
|
140
|
+
- `KeyExchangeKeyPair`
|
|
141
|
+
- `SessionKeys`
|
|
142
|
+
- `SigningKeyPair`
|
|
143
|
+
- `SignedMessageV1`
|
|
144
|
+
- `GroupMember`
|
|
145
|
+
- `GroupMemberWire`
|
|
146
|
+
- `GroupState`
|
|
147
|
+
- `GroupWelcome`
|
|
148
|
+
- `GroupCommit`
|
|
149
|
+
- `GroupCommitAction`
|
|
150
|
+
|
|
151
|
+
### Master Key
|
|
152
|
+
|
|
153
|
+
- `generateMasterKey(): Promise<Uint8Array>`
|
|
154
|
+
- `generateMasterKeyWithPassword(password, opts?, aad?): Promise<{ masterKey; wrapped }>`
|
|
155
|
+
- `wrapMasterKeyWithPassword(masterKey, password, opts?, aad?): Promise<PasswordWrappedKeyV1>`
|
|
156
|
+
- `unwrapMasterKeyWithPassword(password, wrapped, aad?): Promise<Uint8Array>`
|
|
157
|
+
|
|
158
|
+
### Recovery Key
|
|
159
|
+
|
|
160
|
+
- `generateRecoveryKey(): Promise<Uint8Array>`
|
|
161
|
+
- `wrapMasterKeyWithRecoveryKey(masterKey, recoveryKey, aad?): Promise<AeadEnvelopeV1>`
|
|
162
|
+
- `unwrapMasterKeyWithRecoveryKey(recoveryKey, wrapped, aad?): Promise<Uint8Array>`
|
|
163
|
+
- `wrapRecoveryKeyWithMasterKey(recoveryKey, masterKey, aad?): Promise<AeadEnvelopeV1>`
|
|
164
|
+
- `unwrapRecoveryKeyWithMasterKey(masterKey, wrapped, aad?): Promise<Uint8Array>`
|
|
165
|
+
|
|
166
|
+
### File Encryption
|
|
167
|
+
|
|
168
|
+
- `generateFileKey(): Promise<Uint8Array>`
|
|
169
|
+
- `wrapFileKeyWithMasterKey(fileKey, masterKey, aad?): Promise<AeadEnvelopeV1>`
|
|
170
|
+
- `unwrapFileKeyWithMasterKey(masterKey, wrapped, aad?): Promise<Uint8Array>`
|
|
171
|
+
- `encryptFileData(fileKey, plaintext, aad?): Promise<AeadEnvelopeV1>`
|
|
172
|
+
- `decryptFileData(fileKey, wrapped, aad?): Promise<Uint8Array>`
|
|
173
|
+
|
|
174
|
+
### Password KDF
|
|
175
|
+
|
|
176
|
+
- `deriveKeyFromPassword(password, opts?): Promise<{ key; kdf }>`
|
|
177
|
+
- `wrapKeyWithPassword(key, password, opts?, aad?): Promise<PasswordWrappedKeyV1>`
|
|
178
|
+
- `unwrapKeyWithPassword(password, wrapped, aad?): Promise<Uint8Array>`
|
|
179
|
+
|
|
180
|
+
Opsi KDF (`PasswordKdfOptions`):
|
|
181
|
+
- `opslimit` (number)
|
|
182
|
+
- `memlimit` (number)
|
|
183
|
+
- `salt` (Uint8Array)
|
|
184
|
+
|
|
185
|
+
### Utilitas Encoding
|
|
186
|
+
|
|
187
|
+
- `encodeBase64(u8): string`
|
|
188
|
+
- `decodeBase64(s): Uint8Array`
|
|
189
|
+
- `fromString(s): Uint8Array`
|
|
190
|
+
- `toString(u8): string`
|
|
191
|
+
|
|
192
|
+
### Wrapper libsodium
|
|
193
|
+
|
|
194
|
+
Wrapper disediakan agar fungsi libsodium bisa dipanggil dari library ini:
|
|
195
|
+
|
|
196
|
+
- `sodiumReady(): Promise<void>`: wajib dipanggil jika ingin akses konstanta libsodium secara aman.
|
|
197
|
+
- `randombytesBuf(size): Uint8Array`
|
|
198
|
+
- `cryptoPwhash(outLen, password, salt, opslimit, memlimit, alg): Uint8Array`
|
|
199
|
+
- `cryptoPwhashSaltBytes`
|
|
200
|
+
- `cryptoPwhashOpslimitModerate`
|
|
201
|
+
- `cryptoPwhashMemlimitModerate`
|
|
202
|
+
- `aeadXChaCha20Poly1305IetfEncrypt(message, aad, secretNonce, publicNonce, key)`
|
|
203
|
+
- `aeadXChaCha20Poly1305IetfDecrypt(secretNonce, ciphertext, aad, publicNonce, key)`
|
|
204
|
+
- `aeadXChaCha20Poly1305IetfNpubBytes`
|
|
205
|
+
- `toBase64(u8): string`
|
|
206
|
+
- `fromBase64(s): Uint8Array`
|
|
207
|
+
- `base64VariantOriginal`
|
|
208
|
+
- `memcmp(a, b): boolean`
|
|
209
|
+
- `sodium` (akses penuh ke instance libsodium)
|
|
210
|
+
|
|
211
|
+
Catatan: konstanta seperti `cryptoPwhashSaltBytes` diisi setelah `sodiumReady()` dipanggil.
|
|
212
|
+
|
|
213
|
+
### Asimetris: Key Exchange & Signature
|
|
214
|
+
|
|
215
|
+
Library ini menyediakan dua keypair terpisah:
|
|
216
|
+
1) X25519 untuk key exchange/shared key.
|
|
217
|
+
2) Ed25519 untuk tanda tangan digital.
|
|
218
|
+
|
|
219
|
+
Key exchange (X25519):
|
|
220
|
+
- `generateKeyExchangeKeyPair(): Promise<{ publicKey; privateKey }>`
|
|
221
|
+
- `deriveClientSessionKeys(clientKeyPair, serverPublicKey): Promise<{ rx; tx }>`
|
|
222
|
+
- `deriveServerSessionKeys(serverKeyPair, clientPublicKey): Promise<{ rx; tx }>`
|
|
223
|
+
- `encryptSealedBox(message, recipientPublicKey): Promise<Uint8Array>`
|
|
224
|
+
- `decryptSealedBox(ciphertext, recipientKeyPair): Promise<Uint8Array>`
|
|
225
|
+
|
|
226
|
+
Signature (Ed25519):
|
|
227
|
+
- `generateSigningKeyPair(): Promise<{ publicKey; privateKey }>`
|
|
228
|
+
- `signMessage(message, privateKey): Promise<Uint8Array>`
|
|
229
|
+
- `verifySignature(message, signature, publicKey): Promise<boolean>`
|
|
230
|
+
- `signMessageWithNonce(message, privateKey, opts?): Promise<SignedMessageV1>`
|
|
231
|
+
- `verifySignedMessage(payload, publicKey): Promise<boolean>`
|
|
232
|
+
|
|
233
|
+
Anti‑replay:
|
|
234
|
+
- `signMessageWithNonce` menambahkan `nonce` + `timestamp` ke payload yang ditandatangani.
|
|
235
|
+
- Simpan nonce yang sudah dipakai (mis. di cache/DB) untuk mencegah replay.
|
|
236
|
+
|
|
237
|
+
### Group (MLS-like, tahap awal)
|
|
238
|
+
|
|
239
|
+
Implementasi awal untuk group key management dengan commit + epoch. Saat ini masih O(n) karena secret baru dibagikan via sealed box ke setiap member. Struktur API disiapkan agar dapat ditingkatkan ke tree-based MLS.
|
|
240
|
+
|
|
241
|
+
- `createGroupState(groupId, members): Promise<{ state; welcome }>`
|
|
242
|
+
- `openGroupWelcome(welcome, recipientId, recipientKeyPair): Promise<GroupState>`
|
|
243
|
+
- `deriveGroupKey(state): Promise<Uint8Array>`
|
|
244
|
+
- `createGroupCommit(state, senderId, senderSignPrivateKey, action, updates?): Promise<{ commit; state }>`
|
|
245
|
+
- `verifyGroupCommitSignature(commit, senderSignPublicKey): Promise<boolean>`
|
|
246
|
+
- `applyGroupCommit(state, commit, recipientId, recipientKeyPair, senderSignPublicKey): Promise<GroupState>`
|
|
247
|
+
|
|
248
|
+
Catatan:
|
|
249
|
+
- `GroupMember.signPublicKey` bersifat opsional dan hanya diperlukan jika kamu ingin menyimpan public key signature di metadata group.
|
|
250
|
+
- Semua payload group disarankan disimpan sebagai JSON di server (zero-knowledge friendly).
|
|
251
|
+
|
|
252
|
+
## Format Payload
|
|
253
|
+
|
|
254
|
+
AEAD envelope:
|
|
255
|
+
```json
|
|
256
|
+
{
|
|
257
|
+
"v": 1,
|
|
258
|
+
"nonce": "base64",
|
|
259
|
+
"ct": "base64"
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Password-wrapped key:
|
|
264
|
+
```json
|
|
265
|
+
{
|
|
266
|
+
"v": 1,
|
|
267
|
+
"kdf": { "v": 1, "salt": "...", "opslimit": 3, "memlimit": 268435456 },
|
|
268
|
+
"nonce": "....",
|
|
269
|
+
"ct": "...."
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## AAD (Additional Authenticated Data)
|
|
57
274
|
|
|
58
|
-
|
|
275
|
+
AAD digunakan untuk mengikat ciphertext ke konteks (mis. userId, slot, fileId). Jika AAD salah, proses decrypt akan gagal.
|
|
276
|
+
|
|
277
|
+
Contoh AAD:
|
|
278
|
+
```ts
|
|
279
|
+
import { fromString } from "@nexwage/crypto";
|
|
280
|
+
const aad = fromString("uid:user_123|app:v1|slot:password");
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Penanganan Error
|
|
284
|
+
|
|
285
|
+
- Jika ukuran key/salt/nonce salah, fungsi akan melempar error validasi.
|
|
286
|
+
- Jika password/AAD salah, decrypt akan gagal dan melempar error.
|
|
287
|
+
|
|
288
|
+
## Struktur Proyek
|
|
289
|
+
|
|
290
|
+
| Path | Tujuan |
|
|
59
291
|
| --- | --- |
|
|
60
292
|
| `src/index.ts` | Public exports. |
|
|
61
|
-
| `src/types.ts` |
|
|
62
|
-
| `src/crypto/password-kdf.ts` |
|
|
63
|
-
| `src/crypto/master-key.ts` |
|
|
293
|
+
| `src/types.ts` | Tipe payload dan versioning. |
|
|
294
|
+
| `src/crypto/password-kdf.ts` | KDF password + wrap/unwrap key. |
|
|
295
|
+
| `src/crypto/master-key.ts` | Lifecycle master key. |
|
|
64
296
|
| `src/crypto/recovery-key.ts` | Recovery key wrapping. |
|
|
65
|
-
| `src/crypto/file-encryption.ts` | File key + file
|
|
66
|
-
| `src/
|
|
67
|
-
| `src/
|
|
68
|
-
| `src/
|
|
69
|
-
| `
|
|
70
|
-
| `
|
|
297
|
+
| `src/crypto/file-encryption.ts` | File key + enkripsi/dekripsi file. |
|
|
298
|
+
| `src/crypto/key-exchange.ts` | Key exchange (X25519) + sealed box. |
|
|
299
|
+
| `src/crypto/signature.ts` | Tanda tangan digital (Ed25519). |
|
|
300
|
+
| `src/crypto/group.ts` | Group key management (MLS-like tahap awal). |
|
|
301
|
+
| `src/utils/encoding.ts` | Helper base64. |
|
|
302
|
+
| `src/utils/bytes.ts` | Validasi panjang bytes. |
|
|
303
|
+
| `src/sodium.ts` | Wrapper libsodium. |
|
|
304
|
+
| `examples/basic-flow.ts` | Contoh alur end-to-end. |
|
|
305
|
+
| `examples/master-key-example.ts` | Contoh master key + KDF. |
|
|
306
|
+
| `examples/asymmetric-key-exchange.ts` | Contoh key exchange + sealed box. |
|
|
307
|
+
| `examples/asymmetric-signature.ts` | Contoh signature. |
|
|
308
|
+
| `examples/group-basic.ts` | Contoh group basic. |
|
|
309
|
+
| `SIMULATION.md` | Simulasi ringkas simetris & asimetris. |
|
|
310
|
+
|
|
311
|
+
## Pengujian
|
|
312
|
+
|
|
313
|
+
```sh
|
|
314
|
+
npm test
|
|
315
|
+
```
|
|
71
316
|
|
|
72
|
-
##
|
|
317
|
+
## Build
|
|
73
318
|
|
|
74
|
-
|
|
75
|
-
|
|
319
|
+
```sh
|
|
320
|
+
npm run build
|
|
321
|
+
```
|
|
76
322
|
|
|
77
|
-
##
|
|
323
|
+
## Examples
|
|
78
324
|
|
|
79
|
-
|
|
325
|
+
Jalankan contoh:
|
|
80
326
|
|
|
81
|
-
|
|
327
|
+
```sh
|
|
328
|
+
npm run example:kx
|
|
329
|
+
npm run example:sign
|
|
330
|
+
npm run example:basic
|
|
331
|
+
npm run example:group
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
Contoh output (ringkas):
|
|
335
|
+
|
|
336
|
+
Key exchange + sealed box:
|
|
337
|
+
```text
|
|
338
|
+
[kx] generate keypairs
|
|
339
|
+
[kx] client public key (b64): ...
|
|
340
|
+
[kx] server public key (b64): ...
|
|
341
|
+
[kx] derive session keys
|
|
342
|
+
[kx] client tx (b64): ...
|
|
343
|
+
[kx] client rx (b64): ...
|
|
344
|
+
[kx] server tx (b64): ...
|
|
345
|
+
[kx] server rx (b64): ...
|
|
346
|
+
[kx] shared tx/rx matches (base64 compare):
|
|
347
|
+
[kx] client tx == server rx: true
|
|
348
|
+
[kx] client rx == server tx: true
|
|
349
|
+
[sealed] encrypt to server public key
|
|
350
|
+
[sealed] ciphertext (b64): ...
|
|
351
|
+
[sealed] ciphertext bytes: ...
|
|
352
|
+
[sealed] decrypt with server keypair
|
|
353
|
+
[sealed] plaintext: hello sealed box
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
Signature:
|
|
357
|
+
```text
|
|
358
|
+
[sign] generate keypair
|
|
359
|
+
[sign] public key (b64): ...
|
|
360
|
+
[sign] private key bytes: 64
|
|
361
|
+
[sign] message bytes: ...
|
|
362
|
+
[sign] create signature
|
|
363
|
+
[sign] signature (b64): ...
|
|
364
|
+
[sign] signature bytes: 64
|
|
365
|
+
[sign] verify signature (valid)
|
|
366
|
+
[sign] result: true
|
|
367
|
+
[sign] verify signature (invalid)
|
|
368
|
+
[sign] result: false
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Basic flow:
|
|
372
|
+
```text
|
|
373
|
+
[alur1] masterKey: ...
|
|
374
|
+
[alur1] wrappedMasterKey: ...
|
|
375
|
+
[alur1] wrappedMasterKeyByRecovery: ...
|
|
376
|
+
[alur1] wrappedRecoveryKey: ...
|
|
377
|
+
[alur1] wrappedFileKey: ...
|
|
378
|
+
[alur1] encryptedFile: ...
|
|
379
|
+
[alur2] masterKey2: ...
|
|
380
|
+
[alur2] fileKey2: ...
|
|
381
|
+
[alur2] fileBytes2: file rahasia
|
|
382
|
+
[alur3] masterKey3: ...
|
|
383
|
+
[alur3] wrappedMasterKeyNew: ...
|
|
384
|
+
[alur3] wrappedMasterKeyByRecoveryNew: ...
|
|
385
|
+
[done] semua alur OK
|
|
386
|
+
```
|
|
82
387
|
|
|
83
|
-
|
|
388
|
+
Contoh output error (AAD/password salah):
|
|
389
|
+
```text
|
|
390
|
+
Error: Gagal membuka file: ciphertext/AAD/keys tidak valid
|
|
391
|
+
Error: Gagal membuka key: ciphertext/AAD/keys tidak valid
|
|
392
|
+
Error: Gagal membuka payload: ciphertext/AAD/keys tidak valid
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Group:
|
|
396
|
+
```text
|
|
397
|
+
[group] create members
|
|
398
|
+
[group] create group state
|
|
399
|
+
[group] welcome epoch: 0
|
|
400
|
+
[group] members: alice, bob
|
|
401
|
+
[group] bob open welcome
|
|
402
|
+
[group] bob groupKey epoch 0 (b64): ...
|
|
403
|
+
[group] add carol with commit
|
|
404
|
+
[group] commit epoch: 1
|
|
405
|
+
[group] commit action: add
|
|
406
|
+
[group] commit members: alice, bob, carol
|
|
407
|
+
[group] bob apply commit
|
|
408
|
+
[group] bob groupKey epoch 1 (b64): ...
|
|
409
|
+
```
|
|
84
410
|
|
|
85
|
-
|
|
411
|
+
## Glossary
|
|
412
|
+
|
|
413
|
+
| Istilah | Makna |
|
|
86
414
|
| --- | --- |
|
|
87
415
|
| Master Key (MK) | Kunci utama untuk membuka kunci lain (file key, recovery key). |
|
|
88
416
|
| Recovery Key | Kunci cadangan jika lupa password. |
|
|
@@ -96,76 +424,6 @@ Panduan ringkas istilah dan konsep yang dipakai di modul ini.
|
|
|
96
424
|
| Envelope | Struktur payload berisi `nonce`, `ct`, dan `v`. |
|
|
97
425
|
| Versioning (v) | Versi format payload. |
|
|
98
426
|
|
|
99
|
-
##
|
|
100
|
-
|
|
101
|
-
| Function/Property | Description |
|
|
102
|
-
| --- | --- |
|
|
103
|
-
| `sodium.ready` | Menunggu libsodium siap dipakai. |
|
|
104
|
-
| `sodium.randombytes_buf` | Generate bytes acak (kunci, nonce). |
|
|
105
|
-
| `sodium.crypto_pwhash` | Derivasi key dari password (Argon2). |
|
|
106
|
-
| `sodium.crypto_pwhash_SALTBYTES` | Panjang salt untuk KDF. |
|
|
107
|
-
| `sodium.crypto_pwhash_OPSLIMIT_MODERATE` | Default opslimit KDF. |
|
|
108
|
-
| `sodium.crypto_pwhash_MEMLIMIT_MODERATE` | Default memlimit KDF. |
|
|
109
|
-
| `sodium.crypto_aead_xchacha20poly1305_ietf_encrypt` | Enkripsi AEAD XChaCha20-Poly1305. |
|
|
110
|
-
| `sodium.crypto_aead_xchacha20poly1305_ietf_decrypt` | Dekripsi AEAD XChaCha20-Poly1305. |
|
|
111
|
-
| `sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES` | Panjang nonce AEAD XChaCha20-Poly1305. |
|
|
112
|
-
| `sodium.to_base64` | Encode bytes ke base64. |
|
|
113
|
-
| `sodium.from_base64` | Decode base64 ke bytes. |
|
|
114
|
-
| `sodium.base64_variants.ORIGINAL` | Variant base64 yang dipakai. |
|
|
115
|
-
| `sodium.from_string` | Encode string ke bytes (UTF-8). |
|
|
116
|
-
| `sodium.to_string` | Decode bytes ke string (UTF-8). |
|
|
117
|
-
| `sodium.memcmp` | Bandingkan bytes secara aman (constant-time). |
|
|
118
|
-
|
|
119
|
-
## Planned libsodium Functions
|
|
120
|
-
|
|
121
|
-
| Function/Property | Planned use |
|
|
122
|
-
| --- | --- |
|
|
123
|
-
| `sodium.crypto_generichash` | Hash konteks/AAD jika perlu bentuk stabil dan ringkas. |
|
|
124
|
-
| `sodium.crypto_kdf_derive_from_key` | Derivasi sub-key per domain dari satu kunci root. |
|
|
125
|
-
| `sodium.crypto_aead_aes256gcm_encrypt` | Alternatif AEAD dengan akselerasi hardware. |
|
|
126
|
-
| `sodium.crypto_aead_aes256gcm_decrypt` | Pasangan decrypt AES-GCM. |
|
|
127
|
-
| `sodium.crypto_secretbox_easy` | Enkripsi simetris sederhana untuk payload non-AEAD. |
|
|
128
|
-
| `sodium.crypto_secretbox_open_easy` | Dekripsi secretbox. |
|
|
129
|
-
|
|
130
|
-
## AAD Example
|
|
131
|
-
|
|
132
|
-
```ts
|
|
133
|
-
import { fromString } from "@nexwage/crypto";
|
|
134
|
-
|
|
135
|
-
const aad = fromString("uid:user_123|app:v1|slot:password");
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## KDF Parameters
|
|
139
|
-
|
|
140
|
-
| Parameter | Purpose |
|
|
141
|
-
| --- | --- |
|
|
142
|
-
| opslimit | Jumlah operasi KDF (lebih besar = lebih lambat, lebih tahan brute-force). |
|
|
143
|
-
| memlimit | Batas memori KDF (lebih besar = lebih tahan GPU/ASIC). |
|
|
144
|
-
| salt | Salt acak 32-byte untuk mencegah rainbow table. |
|
|
427
|
+
## Lisensi
|
|
145
428
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
```json
|
|
149
|
-
{
|
|
150
|
-
"v": 1,
|
|
151
|
-
"nonce": "base64",
|
|
152
|
-
"ct": "base64"
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## Versioning Rules
|
|
157
|
-
|
|
158
|
-
1. Saat encrypt, isi `v` dengan versi terbaru.
|
|
159
|
-
2. Saat decrypt, cek `v` lalu pilih logika yang sesuai.
|
|
160
|
-
3. Jika `v` tidak didukung, tolak dekripsi atau lakukan migrasi.
|
|
161
|
-
|
|
162
|
-
Contoh payload:
|
|
163
|
-
|
|
164
|
-
```json
|
|
165
|
-
{
|
|
166
|
-
"v": 1,
|
|
167
|
-
"kdf": { "v": 1, "salt": "...", "opslimit": 3, "memlimit": 268435456 },
|
|
168
|
-
"nonce": "....",
|
|
169
|
-
"ct": "...."
|
|
170
|
-
}
|
|
171
|
-
```
|
|
429
|
+
ISC
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export type GroupMember = {
|
|
2
|
+
id: string;
|
|
3
|
+
encPublicKey: Uint8Array;
|
|
4
|
+
signPublicKey?: Uint8Array;
|
|
5
|
+
};
|
|
6
|
+
export type GroupMemberWire = {
|
|
7
|
+
id: string;
|
|
8
|
+
encPublicKey: string;
|
|
9
|
+
signPublicKey?: string;
|
|
10
|
+
};
|
|
11
|
+
export type GroupState = {
|
|
12
|
+
groupId: string;
|
|
13
|
+
epoch: number;
|
|
14
|
+
members: GroupMember[];
|
|
15
|
+
secret: Uint8Array;
|
|
16
|
+
};
|
|
17
|
+
export type GroupWelcome = {
|
|
18
|
+
groupId: string;
|
|
19
|
+
epoch: number;
|
|
20
|
+
members: GroupMemberWire[];
|
|
21
|
+
encryptedSecrets: Record<string, string>;
|
|
22
|
+
};
|
|
23
|
+
export type GroupCommitAction = "add" | "remove" | "rotate";
|
|
24
|
+
export type GroupCommit = {
|
|
25
|
+
groupId: string;
|
|
26
|
+
epoch: number;
|
|
27
|
+
senderId: string;
|
|
28
|
+
action: GroupCommitAction;
|
|
29
|
+
members: GroupMemberWire[];
|
|
30
|
+
removedMemberIds?: string[];
|
|
31
|
+
encryptedSecrets: Record<string, string>;
|
|
32
|
+
signature: string;
|
|
33
|
+
};
|
|
34
|
+
type KeyPair = {
|
|
35
|
+
publicKey: Uint8Array;
|
|
36
|
+
privateKey: Uint8Array;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Buat group baru dengan secret awal.
|
|
40
|
+
*
|
|
41
|
+
* @param groupId - ID group.
|
|
42
|
+
* @param members - Daftar member awal.
|
|
43
|
+
* @returns State group dan welcome payload.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createGroupState(groupId: string, members: GroupMember[]): Promise<{
|
|
46
|
+
state: GroupState;
|
|
47
|
+
welcome: GroupWelcome;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* Buka welcome payload untuk memperoleh state group.
|
|
51
|
+
*
|
|
52
|
+
* @param welcome - Welcome payload.
|
|
53
|
+
* @param recipientId - ID member penerima.
|
|
54
|
+
* @param recipientKeyPair - Keypair enkripsi penerima.
|
|
55
|
+
* @returns State group.
|
|
56
|
+
*/
|
|
57
|
+
export declare function openGroupWelcome(welcome: GroupWelcome, recipientId: string, recipientKeyPair: KeyPair): Promise<GroupState>;
|
|
58
|
+
/**
|
|
59
|
+
* Derivasi group key dari secret + epoch.
|
|
60
|
+
*
|
|
61
|
+
* @param state - State group.
|
|
62
|
+
* @returns Group key 32-byte.
|
|
63
|
+
*/
|
|
64
|
+
export declare function deriveGroupKey(state: GroupState): Promise<Uint8Array>;
|
|
65
|
+
/**
|
|
66
|
+
* Buat commit untuk add/remove/rotate member.
|
|
67
|
+
*
|
|
68
|
+
* @param state - State group saat ini.
|
|
69
|
+
* @param senderId - ID pengirim commit.
|
|
70
|
+
* @param senderSignPrivateKey - Private key Ed25519 pengirim.
|
|
71
|
+
* @param action - Jenis aksi commit.
|
|
72
|
+
* @param memberUpdates - Member yang ditambah/dihapus.
|
|
73
|
+
* @returns Commit + state baru.
|
|
74
|
+
*/
|
|
75
|
+
export declare function createGroupCommit(state: GroupState, senderId: string, senderSignPrivateKey: Uint8Array, action: GroupCommitAction, memberUpdates?: {
|
|
76
|
+
add?: GroupMember[];
|
|
77
|
+
removeIds?: string[];
|
|
78
|
+
}): Promise<{
|
|
79
|
+
commit: GroupCommit;
|
|
80
|
+
state: GroupState;
|
|
81
|
+
}>;
|
|
82
|
+
/**
|
|
83
|
+
* Verifikasi signature commit.
|
|
84
|
+
*
|
|
85
|
+
* @param commit - Commit payload.
|
|
86
|
+
* @param senderSignPublicKey - Public key Ed25519 pengirim.
|
|
87
|
+
* @returns True jika valid.
|
|
88
|
+
*/
|
|
89
|
+
export declare function verifyGroupCommitSignature(commit: GroupCommit, senderSignPublicKey: Uint8Array): Promise<boolean>;
|
|
90
|
+
/**
|
|
91
|
+
* Apply commit ke state lokal.
|
|
92
|
+
*
|
|
93
|
+
* @param state - State saat ini.
|
|
94
|
+
* @param commit - Commit terbaru.
|
|
95
|
+
* @param recipientId - ID member penerima.
|
|
96
|
+
* @param recipientKeyPair - Keypair enkripsi penerima.
|
|
97
|
+
* @param senderSignPublicKey - Public key Ed25519 pengirim.
|
|
98
|
+
* @returns State baru.
|
|
99
|
+
*/
|
|
100
|
+
export declare function applyGroupCommit(state: GroupState, commit: GroupCommit, recipientId: string, recipientKeyPair: KeyPair, senderSignPublicKey: Uint8Array): Promise<GroupState>;
|
|
101
|
+
export {};
|
|
102
|
+
//# sourceMappingURL=group.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group.d.ts","sourceRoot":"","sources":["../../src/crypto/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AAE,MAAM,EACf,OAAO,EAAE,WAAW,EAAE,GACrB,OAAO,CAAC;IAAE,KAAK,EAAE,UAAU,CAAC;IAAC,OAAO,EAAE,YAAY,CAAA;CAAE,CAAC,CAwBvD;AAED;;;;;;;GAOG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,MAAM,EACnB,gBAAgB,EAAE,OAAO,GACxB,OAAO,CAAC,UAAU,CAAC,CAYrB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAa3E;AAED;;;;;;;;;GASG;AACH,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,MAAM,EAChB,oBAAoB,EAAE,UAAU,EAChC,MAAM,EAAE,iBAAiB,EACzB,aAAa,CAAC,EAAE;IACd,GAAG,CAAC,EAAE,WAAW,EAAE,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB,GACA,OAAO,CAAC;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAAC,CAyDrD;AAED;;;;;;GAMG;AACH,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,WAAW,EACnB,mBAAmB,EAAE,UAAU,GAC9B,OAAO,CAAC,OAAO,CAAC,CAclB;AAED;;;;;;;;;GASG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,WAAW,EACnB,WAAW,EAAE,MAAM,EACnB,gBAAgB,EAAE,OAAO,EACzB,mBAAmB,EAAE,UAAU,GAC9B,OAAO,CAAC,UAAU,CAAC,CAsBrB"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import sodium from "libsodium-wrappers-sumo";
|
|
2
|
+
import { assertByteLength } from "../utils/bytes.js";
|
|
3
|
+
import { decodeBase64, encodeBase64 } from "../utils/encoding.js";
|
|
4
|
+
function sortMembers(members) {
|
|
5
|
+
return [...members].sort((a, b) => a.id.localeCompare(b.id));
|
|
6
|
+
}
|
|
7
|
+
function toWireMember(member) {
|
|
8
|
+
const base = {
|
|
9
|
+
id: member.id,
|
|
10
|
+
encPublicKey: encodeBase64(member.encPublicKey),
|
|
11
|
+
};
|
|
12
|
+
return member.signPublicKey
|
|
13
|
+
? { ...base, signPublicKey: encodeBase64(member.signPublicKey) }
|
|
14
|
+
: base;
|
|
15
|
+
}
|
|
16
|
+
function toStateMember(member) {
|
|
17
|
+
const base = {
|
|
18
|
+
id: member.id,
|
|
19
|
+
encPublicKey: decodeBase64(member.encPublicKey),
|
|
20
|
+
};
|
|
21
|
+
return member.signPublicKey
|
|
22
|
+
? { ...base, signPublicKey: decodeBase64(member.signPublicKey) }
|
|
23
|
+
: base;
|
|
24
|
+
}
|
|
25
|
+
function canonicalizeCommitForSigning(commit) {
|
|
26
|
+
const members = sortMembers(commit.members);
|
|
27
|
+
const encryptedSecrets = Object.keys(commit.encryptedSecrets)
|
|
28
|
+
.sort()
|
|
29
|
+
.reduce((acc, key) => {
|
|
30
|
+
const value = commit.encryptedSecrets[key];
|
|
31
|
+
if (value) {
|
|
32
|
+
acc[key] = value;
|
|
33
|
+
}
|
|
34
|
+
return acc;
|
|
35
|
+
}, {});
|
|
36
|
+
return JSON.stringify({
|
|
37
|
+
groupId: commit.groupId,
|
|
38
|
+
epoch: commit.epoch,
|
|
39
|
+
senderId: commit.senderId,
|
|
40
|
+
action: commit.action,
|
|
41
|
+
members,
|
|
42
|
+
removedMemberIds: commit.removedMemberIds ?? [],
|
|
43
|
+
encryptedSecrets,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async function encryptSecretForMember(secret, member) {
|
|
47
|
+
await sodium.ready;
|
|
48
|
+
assertByteLength("encPublicKey", member.encPublicKey, sodium.crypto_box_PUBLICKEYBYTES);
|
|
49
|
+
const ciphertext = sodium.crypto_box_seal(secret, member.encPublicKey);
|
|
50
|
+
return encodeBase64(ciphertext);
|
|
51
|
+
}
|
|
52
|
+
async function decryptSecretForMember(ciphertextB64, keyPair) {
|
|
53
|
+
await sodium.ready;
|
|
54
|
+
assertByteLength("encPublicKey", keyPair.publicKey, sodium.crypto_box_PUBLICKEYBYTES);
|
|
55
|
+
assertByteLength("encPrivateKey", keyPair.privateKey, sodium.crypto_box_SECRETKEYBYTES);
|
|
56
|
+
const ciphertext = decodeBase64(ciphertextB64);
|
|
57
|
+
return sodium.crypto_box_seal_open(ciphertext, keyPair.publicKey, keyPair.privateKey);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Buat group baru dengan secret awal.
|
|
61
|
+
*
|
|
62
|
+
* @param groupId - ID group.
|
|
63
|
+
* @param members - Daftar member awal.
|
|
64
|
+
* @returns State group dan welcome payload.
|
|
65
|
+
*/
|
|
66
|
+
export async function createGroupState(groupId, members) {
|
|
67
|
+
await sodium.ready;
|
|
68
|
+
const secret = sodium.randombytes_buf(32);
|
|
69
|
+
const sortedMembers = sortMembers(members);
|
|
70
|
+
const encryptedSecrets = {};
|
|
71
|
+
for (const member of sortedMembers) {
|
|
72
|
+
encryptedSecrets[member.id] = await encryptSecretForMember(secret, member);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
state: {
|
|
76
|
+
groupId,
|
|
77
|
+
epoch: 0,
|
|
78
|
+
members: sortedMembers,
|
|
79
|
+
secret,
|
|
80
|
+
},
|
|
81
|
+
welcome: {
|
|
82
|
+
groupId,
|
|
83
|
+
epoch: 0,
|
|
84
|
+
members: sortedMembers.map(toWireMember),
|
|
85
|
+
encryptedSecrets,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Buka welcome payload untuk memperoleh state group.
|
|
91
|
+
*
|
|
92
|
+
* @param welcome - Welcome payload.
|
|
93
|
+
* @param recipientId - ID member penerima.
|
|
94
|
+
* @param recipientKeyPair - Keypair enkripsi penerima.
|
|
95
|
+
* @returns State group.
|
|
96
|
+
*/
|
|
97
|
+
export async function openGroupWelcome(welcome, recipientId, recipientKeyPair) {
|
|
98
|
+
const ciphertextB64 = welcome.encryptedSecrets[recipientId];
|
|
99
|
+
if (!ciphertextB64) {
|
|
100
|
+
throw new Error("Welcome tidak berisi secret untuk member ini");
|
|
101
|
+
}
|
|
102
|
+
const secret = await decryptSecretForMember(ciphertextB64, recipientKeyPair);
|
|
103
|
+
return {
|
|
104
|
+
groupId: welcome.groupId,
|
|
105
|
+
epoch: welcome.epoch,
|
|
106
|
+
members: welcome.members.map(toStateMember),
|
|
107
|
+
secret,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Derivasi group key dari secret + epoch.
|
|
112
|
+
*
|
|
113
|
+
* @param state - State group.
|
|
114
|
+
* @returns Group key 32-byte.
|
|
115
|
+
*/
|
|
116
|
+
export async function deriveGroupKey(state) {
|
|
117
|
+
await sodium.ready;
|
|
118
|
+
const epochBytes = new Uint8Array(4);
|
|
119
|
+
const view = new DataView(epochBytes.buffer);
|
|
120
|
+
view.setUint32(0, state.epoch, false);
|
|
121
|
+
const context = new TextEncoder().encode("group-key");
|
|
122
|
+
const input = new Uint8Array(context.length + epochBytes.length + state.secret.length);
|
|
123
|
+
input.set(context, 0);
|
|
124
|
+
input.set(epochBytes, context.length);
|
|
125
|
+
input.set(state.secret, context.length + epochBytes.length);
|
|
126
|
+
return sodium.crypto_generichash(32, input);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Buat commit untuk add/remove/rotate member.
|
|
130
|
+
*
|
|
131
|
+
* @param state - State group saat ini.
|
|
132
|
+
* @param senderId - ID pengirim commit.
|
|
133
|
+
* @param senderSignPrivateKey - Private key Ed25519 pengirim.
|
|
134
|
+
* @param action - Jenis aksi commit.
|
|
135
|
+
* @param memberUpdates - Member yang ditambah/dihapus.
|
|
136
|
+
* @returns Commit + state baru.
|
|
137
|
+
*/
|
|
138
|
+
export async function createGroupCommit(state, senderId, senderSignPrivateKey, action, memberUpdates) {
|
|
139
|
+
await sodium.ready;
|
|
140
|
+
assertByteLength("senderSignPrivateKey", senderSignPrivateKey, sodium.crypto_sign_SECRETKEYBYTES);
|
|
141
|
+
const removeIds = new Set(memberUpdates?.removeIds ?? []);
|
|
142
|
+
const members = action === "add"
|
|
143
|
+
? sortMembers([...(state.members ?? []), ...(memberUpdates?.add ?? [])])
|
|
144
|
+
: action === "remove"
|
|
145
|
+
? sortMembers(state.members.filter((m) => !removeIds.has(m.id)))
|
|
146
|
+
: sortMembers(state.members);
|
|
147
|
+
const secret = sodium.randombytes_buf(32);
|
|
148
|
+
const encryptedSecrets = {};
|
|
149
|
+
for (const member of members) {
|
|
150
|
+
encryptedSecrets[member.id] = await encryptSecretForMember(secret, member);
|
|
151
|
+
}
|
|
152
|
+
const basePayload = {
|
|
153
|
+
groupId: state.groupId,
|
|
154
|
+
epoch: state.epoch + 1,
|
|
155
|
+
senderId,
|
|
156
|
+
action,
|
|
157
|
+
members: members.map(toWireMember),
|
|
158
|
+
encryptedSecrets,
|
|
159
|
+
};
|
|
160
|
+
const payload = action === "remove"
|
|
161
|
+
? {
|
|
162
|
+
...basePayload,
|
|
163
|
+
removedMemberIds: Array.from(removeIds.values()),
|
|
164
|
+
}
|
|
165
|
+
: basePayload;
|
|
166
|
+
const toSign = canonicalizeCommitForSigning(payload);
|
|
167
|
+
const signature = sodium.crypto_sign_detached(new TextEncoder().encode(toSign), senderSignPrivateKey);
|
|
168
|
+
return {
|
|
169
|
+
commit: {
|
|
170
|
+
...payload,
|
|
171
|
+
signature: encodeBase64(signature),
|
|
172
|
+
},
|
|
173
|
+
state: {
|
|
174
|
+
groupId: state.groupId,
|
|
175
|
+
epoch: state.epoch + 1,
|
|
176
|
+
members,
|
|
177
|
+
secret,
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Verifikasi signature commit.
|
|
183
|
+
*
|
|
184
|
+
* @param commit - Commit payload.
|
|
185
|
+
* @param senderSignPublicKey - Public key Ed25519 pengirim.
|
|
186
|
+
* @returns True jika valid.
|
|
187
|
+
*/
|
|
188
|
+
export async function verifyGroupCommitSignature(commit, senderSignPublicKey) {
|
|
189
|
+
await sodium.ready;
|
|
190
|
+
assertByteLength("senderSignPublicKey", senderSignPublicKey, sodium.crypto_sign_PUBLICKEYBYTES);
|
|
191
|
+
const { signature, ...payload } = commit;
|
|
192
|
+
const toVerify = canonicalizeCommitForSigning(payload);
|
|
193
|
+
return sodium.crypto_sign_verify_detached(decodeBase64(signature), new TextEncoder().encode(toVerify), senderSignPublicKey);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Apply commit ke state lokal.
|
|
197
|
+
*
|
|
198
|
+
* @param state - State saat ini.
|
|
199
|
+
* @param commit - Commit terbaru.
|
|
200
|
+
* @param recipientId - ID member penerima.
|
|
201
|
+
* @param recipientKeyPair - Keypair enkripsi penerima.
|
|
202
|
+
* @param senderSignPublicKey - Public key Ed25519 pengirim.
|
|
203
|
+
* @returns State baru.
|
|
204
|
+
*/
|
|
205
|
+
export async function applyGroupCommit(state, commit, recipientId, recipientKeyPair, senderSignPublicKey) {
|
|
206
|
+
if (commit.groupId !== state.groupId) {
|
|
207
|
+
throw new Error("groupId commit tidak cocok");
|
|
208
|
+
}
|
|
209
|
+
if (commit.epoch !== state.epoch + 1) {
|
|
210
|
+
throw new Error("epoch commit tidak valid");
|
|
211
|
+
}
|
|
212
|
+
const ok = await verifyGroupCommitSignature(commit, senderSignPublicKey);
|
|
213
|
+
if (!ok) {
|
|
214
|
+
throw new Error("signature commit tidak valid");
|
|
215
|
+
}
|
|
216
|
+
const ciphertextB64 = commit.encryptedSecrets[recipientId];
|
|
217
|
+
if (!ciphertextB64) {
|
|
218
|
+
throw new Error("commit tidak berisi secret untuk member ini");
|
|
219
|
+
}
|
|
220
|
+
const secret = await decryptSecretForMember(ciphertextB64, recipientKeyPair);
|
|
221
|
+
return {
|
|
222
|
+
groupId: commit.groupId,
|
|
223
|
+
epoch: commit.epoch,
|
|
224
|
+
members: commit.members.map(toStateMember),
|
|
225
|
+
secret,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=group.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group.js","sourceRoot":"","sources":["../../src/crypto/group.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AA2ClE,SAAS,WAAW,CAA2B,OAAY;IACzD,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/D,CAAC;AAED,SAAS,YAAY,CAAC,MAAmB;IACvC,MAAM,IAAI,GAAG;QACX,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,YAAY,EAAE,YAAY,CAAC,MAAM,CAAC,YAAY,CAAC;KAChD,CAAC;IACF,OAAO,MAAM,CAAC,aAAa;QACzB,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,aAAa,EAAE,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE;QAChE,CAAC,CAAC,IAAI,CAAC;AACX,CAAC;AAED,SAAS,aAAa,CAAC,MAAuB;IAC5C,MAAM,IAAI,GAAG;QACX,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,YAAY,EAAE,YAAY,CAAC,MAAM,CAAC,YAAY,CAAC;KAChD,CAAC;IACF,OAAO,MAAM,CAAC,aAAa;QACzB,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,aAAa,EAAE,YAAY,CAAC,MAAM,CAAC,aAAa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,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAC9C,CAAC;IACD,MAAM,EAAE,GAAG,MAAM,0BAA0B,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;IACzE,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAClD,CAAC;IACD,MAAM,aAAa,GAAG,MAAM,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAC3D,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IACjE,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,sBAAsB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;IAC7E,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;QAC1C,MAAM;KACP,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type KeyExchangeKeyPair = {
|
|
2
|
+
publicKey: Uint8Array;
|
|
3
|
+
privateKey: Uint8Array;
|
|
4
|
+
};
|
|
5
|
+
export type SessionKeys = {
|
|
6
|
+
rx: Uint8Array;
|
|
7
|
+
tx: Uint8Array;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Generate key pair untuk key exchange (X25519).
|
|
11
|
+
*
|
|
12
|
+
* @returns Public/private key pair.
|
|
13
|
+
*/
|
|
14
|
+
export declare function generateKeyExchangeKeyPair(): Promise<KeyExchangeKeyPair>;
|
|
15
|
+
/**
|
|
16
|
+
* Generate public key (X25519) dari private key untuk key exchange.
|
|
17
|
+
*
|
|
18
|
+
* @param privateKey - Private key X25519.
|
|
19
|
+
* @returns Public key X25519.
|
|
20
|
+
*/
|
|
21
|
+
export declare function deriveKeyExchangePublicKeyFromPrivateKey(privateKey: Uint8Array): Promise<Uint8Array>;
|
|
22
|
+
/**
|
|
23
|
+
* Rekonstruksi keypair key exchange dari private key (public key akan diderivasi).
|
|
24
|
+
*
|
|
25
|
+
* @param privateKey - Private key X25519.
|
|
26
|
+
* @returns Public/private key pair.
|
|
27
|
+
*/
|
|
28
|
+
export declare function keyExchangeKeyPairFromPrivateKey(privateKey: Uint8Array): Promise<KeyExchangeKeyPair>;
|
|
29
|
+
/**
|
|
30
|
+
* Derivasi session keys untuk sisi client.
|
|
31
|
+
*
|
|
32
|
+
* @param clientKeyPair - Key pair client.
|
|
33
|
+
* @param serverPublicKey - Public key server.
|
|
34
|
+
* @returns Session keys { rx, tx }.
|
|
35
|
+
*/
|
|
36
|
+
export declare function deriveClientSessionKeys(clientKeyPair: KeyExchangeKeyPair, serverPublicKey: Uint8Array): Promise<SessionKeys>;
|
|
37
|
+
/**
|
|
38
|
+
* Derivasi session keys untuk sisi server.
|
|
39
|
+
*
|
|
40
|
+
* @param serverKeyPair - Key pair server.
|
|
41
|
+
* @param clientPublicKey - Public key client.
|
|
42
|
+
* @returns Session keys { rx, tx }.
|
|
43
|
+
*/
|
|
44
|
+
export declare function deriveServerSessionKeys(serverKeyPair: KeyExchangeKeyPair, clientPublicKey: Uint8Array): Promise<SessionKeys>;
|
|
45
|
+
/**
|
|
46
|
+
* Enkripsi sealed box ke penerima menggunakan public key (tanpa keypair pengirim).
|
|
47
|
+
*
|
|
48
|
+
* @param message - Plaintext.
|
|
49
|
+
* @param recipientPublicKey - Public key penerima.
|
|
50
|
+
* @returns Ciphertext.
|
|
51
|
+
*/
|
|
52
|
+
export declare function encryptSealedBox(message: Uint8Array, recipientPublicKey: Uint8Array): Promise<Uint8Array>;
|
|
53
|
+
/**
|
|
54
|
+
* Dekripsi sealed box menggunakan keypair penerima.
|
|
55
|
+
*
|
|
56
|
+
* @param ciphertext - Ciphertext.
|
|
57
|
+
* @param recipientKeyPair - Key pair penerima.
|
|
58
|
+
* @returns Plaintext.
|
|
59
|
+
*/
|
|
60
|
+
export declare function decryptSealedBox(ciphertext: Uint8Array, recipientKeyPair: KeyExchangeKeyPair): Promise<Uint8Array>;
|
|
61
|
+
//# sourceMappingURL=key-exchange.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-exchange.d.ts","sourceRoot":"","sources":["../../src/crypto/key-exchange.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,EAAE,UAAU,CAAC;IACtB,UAAU,EAAE,UAAU,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,EAAE,EAAE,UAAU,CAAC;IACf,EAAE,EAAE,UAAU,CAAC;CAChB,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,0BAA0B,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAI9E;AAED;;;;;GAKG;AACH,wBAAsB,wCAAwC,CAC5D,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,UAAU,CAAC,CAQrB;AAED;;;;;GAKG;AACH,wBAAsB,gCAAgC,CACpD,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,kBAAkB,CAAC,CAG7B;AAED;;;;;;GAMG;AACH,wBAAsB,uBAAuB,CAC3C,aAAa,EAAE,kBAAkB,EACjC,eAAe,EAAE,UAAU,GAC1B,OAAO,CAAC,WAAW,CAAC,CAuBtB;AAED;;;;;;GAMG;AACH,wBAAsB,uBAAuB,CAC3C,aAAa,EAAE,kBAAkB,EACjC,eAAe,EAAE,UAAU,GAC1B,OAAO,CAAC,WAAW,CAAC,CAuBtB;AAED;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,UAAU,EACnB,kBAAkB,EAAE,UAAU,GAC7B,OAAO,CAAC,UAAU,CAAC,CAQrB;AAED;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CACpC,UAAU,EAAE,UAAU,EACtB,gBAAgB,EAAE,kBAAkB,GACnC,OAAO,CAAC,UAAU,CAAC,CAiBrB"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import sodium from "libsodium-wrappers-sumo";
|
|
2
|
+
import { assertByteLength } from "../utils/bytes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Generate key pair untuk key exchange (X25519).
|
|
5
|
+
*
|
|
6
|
+
* @returns Public/private key pair.
|
|
7
|
+
*/
|
|
8
|
+
export async function generateKeyExchangeKeyPair() {
|
|
9
|
+
await sodium.ready;
|
|
10
|
+
const { publicKey, privateKey } = sodium.crypto_kx_keypair();
|
|
11
|
+
return { publicKey, privateKey };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generate public key (X25519) dari private key untuk key exchange.
|
|
15
|
+
*
|
|
16
|
+
* @param privateKey - Private key X25519.
|
|
17
|
+
* @returns Public key X25519.
|
|
18
|
+
*/
|
|
19
|
+
export async function deriveKeyExchangePublicKeyFromPrivateKey(privateKey) {
|
|
20
|
+
await sodium.ready;
|
|
21
|
+
assertByteLength("privateKey", privateKey, sodium.crypto_kx_SECRETKEYBYTES);
|
|
22
|
+
return sodium.crypto_scalarmult_base(privateKey);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Rekonstruksi keypair key exchange dari private key (public key akan diderivasi).
|
|
26
|
+
*
|
|
27
|
+
* @param privateKey - Private key X25519.
|
|
28
|
+
* @returns Public/private key pair.
|
|
29
|
+
*/
|
|
30
|
+
export async function keyExchangeKeyPairFromPrivateKey(privateKey) {
|
|
31
|
+
const publicKey = await deriveKeyExchangePublicKeyFromPrivateKey(privateKey);
|
|
32
|
+
return { publicKey, privateKey };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Derivasi session keys untuk sisi client.
|
|
36
|
+
*
|
|
37
|
+
* @param clientKeyPair - Key pair client.
|
|
38
|
+
* @param serverPublicKey - Public key server.
|
|
39
|
+
* @returns Session keys { rx, tx }.
|
|
40
|
+
*/
|
|
41
|
+
export async function deriveClientSessionKeys(clientKeyPair, serverPublicKey) {
|
|
42
|
+
await sodium.ready;
|
|
43
|
+
assertByteLength("clientPublicKey", clientKeyPair.publicKey, sodium.crypto_kx_PUBLICKEYBYTES);
|
|
44
|
+
assertByteLength("clientPrivateKey", clientKeyPair.privateKey, sodium.crypto_kx_SECRETKEYBYTES);
|
|
45
|
+
assertByteLength("serverPublicKey", serverPublicKey, sodium.crypto_kx_PUBLICKEYBYTES);
|
|
46
|
+
const { sharedRx, sharedTx } = sodium.crypto_kx_client_session_keys(clientKeyPair.publicKey, clientKeyPair.privateKey, serverPublicKey);
|
|
47
|
+
return { rx: sharedRx, tx: sharedTx };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Derivasi session keys untuk sisi server.
|
|
51
|
+
*
|
|
52
|
+
* @param serverKeyPair - Key pair server.
|
|
53
|
+
* @param clientPublicKey - Public key client.
|
|
54
|
+
* @returns Session keys { rx, tx }.
|
|
55
|
+
*/
|
|
56
|
+
export async function deriveServerSessionKeys(serverKeyPair, clientPublicKey) {
|
|
57
|
+
await sodium.ready;
|
|
58
|
+
assertByteLength("serverPublicKey", serverKeyPair.publicKey, sodium.crypto_kx_PUBLICKEYBYTES);
|
|
59
|
+
assertByteLength("serverPrivateKey", serverKeyPair.privateKey, sodium.crypto_kx_SECRETKEYBYTES);
|
|
60
|
+
assertByteLength("clientPublicKey", clientPublicKey, sodium.crypto_kx_PUBLICKEYBYTES);
|
|
61
|
+
const { sharedRx, sharedTx } = sodium.crypto_kx_server_session_keys(serverKeyPair.publicKey, serverKeyPair.privateKey, clientPublicKey);
|
|
62
|
+
return { rx: sharedRx, tx: sharedTx };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Enkripsi sealed box ke penerima menggunakan public key (tanpa keypair pengirim).
|
|
66
|
+
*
|
|
67
|
+
* @param message - Plaintext.
|
|
68
|
+
* @param recipientPublicKey - Public key penerima.
|
|
69
|
+
* @returns Ciphertext.
|
|
70
|
+
*/
|
|
71
|
+
export async function encryptSealedBox(message, recipientPublicKey) {
|
|
72
|
+
await sodium.ready;
|
|
73
|
+
assertByteLength("recipientPublicKey", recipientPublicKey, sodium.crypto_box_PUBLICKEYBYTES);
|
|
74
|
+
return sodium.crypto_box_seal(message, recipientPublicKey);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Dekripsi sealed box menggunakan keypair penerima.
|
|
78
|
+
*
|
|
79
|
+
* @param ciphertext - Ciphertext.
|
|
80
|
+
* @param recipientKeyPair - Key pair penerima.
|
|
81
|
+
* @returns Plaintext.
|
|
82
|
+
*/
|
|
83
|
+
export async function decryptSealedBox(ciphertext, recipientKeyPair) {
|
|
84
|
+
await sodium.ready;
|
|
85
|
+
assertByteLength("recipientPublicKey", recipientKeyPair.publicKey, sodium.crypto_box_PUBLICKEYBYTES);
|
|
86
|
+
assertByteLength("recipientPrivateKey", recipientKeyPair.privateKey, sodium.crypto_box_SECRETKEYBYTES);
|
|
87
|
+
return sodium.crypto_box_seal_open(ciphertext, recipientKeyPair.publicKey, recipientKeyPair.privateKey);
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=key-exchange.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-exchange.js","sourceRoot":"","sources":["../../src/crypto/key-exchange.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAYrD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B;IAC9C,MAAM,MAAM,CAAC,KAAK,CAAC;IACnB,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC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
1
|
+
export type SigningKeyPair = {
|
|
2
|
+
publicKey: Uint8Array;
|
|
3
|
+
privateKey: Uint8Array;
|
|
4
|
+
};
|
|
5
|
+
export type SignedMessageV1 = {
|
|
6
|
+
v: 1;
|
|
7
|
+
nonce: string;
|
|
8
|
+
ts: number;
|
|
9
|
+
message: string;
|
|
10
|
+
signature: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Generate key pair untuk tanda tangan (Ed25519).
|
|
14
|
+
*
|
|
15
|
+
* @returns Public/private key pair.
|
|
16
|
+
*/
|
|
17
|
+
export declare function generateSigningKeyPair(): Promise<SigningKeyPair>;
|
|
18
|
+
/**
|
|
19
|
+
* Buat signature detached untuk message.
|
|
20
|
+
*
|
|
21
|
+
* @param message - Data input.
|
|
22
|
+
* @param privateKey - Private key Ed25519.
|
|
23
|
+
* @returns Signature.
|
|
24
|
+
*/
|
|
25
|
+
export declare function signMessage(message: Uint8Array, privateKey: Uint8Array): Promise<Uint8Array>;
|
|
26
|
+
/**
|
|
27
|
+
* Verifikasi signature untuk message.
|
|
28
|
+
*
|
|
29
|
+
* @param message - Data input.
|
|
30
|
+
* @param signature - Signature detached.
|
|
31
|
+
* @param publicKey - Public key Ed25519.
|
|
32
|
+
* @returns True jika valid.
|
|
33
|
+
*/
|
|
34
|
+
export declare function verifySignature(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Buat signature dengan nonce/timestamp untuk anti‑replay.
|
|
37
|
+
*
|
|
38
|
+
* @param message - Data input.
|
|
39
|
+
* @param privateKey - Private key Ed25519.
|
|
40
|
+
* @param opts - Opsi nonce/timestamp.
|
|
41
|
+
* @returns Payload signature terstruktur.
|
|
42
|
+
*/
|
|
43
|
+
export declare function signMessageWithNonce(message: Uint8Array, privateKey: Uint8Array, opts?: {
|
|
44
|
+
nonce?: Uint8Array;
|
|
45
|
+
timestampMs?: number;
|
|
46
|
+
}): Promise<SignedMessageV1>;
|
|
47
|
+
/**
|
|
48
|
+
* Verifikasi payload signature anti‑replay.
|
|
49
|
+
*
|
|
50
|
+
* @param payload - Payload signature.
|
|
51
|
+
* @param publicKey - Public key Ed25519.
|
|
52
|
+
* @returns True jika valid.
|
|
53
|
+
*/
|
|
54
|
+
export declare function verifySignedMessage(payload: SignedMessageV1, publicKey: Uint8Array): Promise<boolean>;
|
|
55
|
+
//# sourceMappingURL=signature.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../../src/crypto/signature.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,cAAc,GAAG;IAC3B,SAAS,EAAE,UAAU,CAAC;IACtB,UAAU,EAAE,UAAU,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,CAAC,EAAE,CAAC,CAAC;IACL,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAaF;;;;GAIG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,cAAc,CAAC,CAItE;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,UAAU,CAAC,CAQrB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,UAAU,EACnB,SAAS,EAAE,UAAU,EACrB,SAAS,EAAE,UAAU,GACpB,OAAO,CAAC,OAAO,CAAC,CAQlB;AAED;;;;;;;GAOG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,UAAU,EACtB,IAAI,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,UAAU,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,GAClD,OAAO,CAAC,eAAe,CAAC,CA4B1B;AAED;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,eAAe,EACxB,SAAS,EAAE,UAAU,GACpB,OAAO,CAAC,OAAO,CAAC,CAqBlB"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import sodium from "libsodium-wrappers-sumo";
|
|
2
|
+
import { assertByteLength } from "../utils/bytes.js";
|
|
3
|
+
import { decodeBase64, encodeBase64 } from "../utils/encoding.js";
|
|
4
|
+
const SIGNED_NONCE_BYTES = 24;
|
|
5
|
+
function canonicalizeSignedPayload(payload) {
|
|
6
|
+
return JSON.stringify({
|
|
7
|
+
v: payload.v,
|
|
8
|
+
nonce: payload.nonce,
|
|
9
|
+
ts: payload.ts,
|
|
10
|
+
message: payload.message,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generate key pair untuk tanda tangan (Ed25519).
|
|
15
|
+
*
|
|
16
|
+
* @returns Public/private key pair.
|
|
17
|
+
*/
|
|
18
|
+
export async function generateSigningKeyPair() {
|
|
19
|
+
await sodium.ready;
|
|
20
|
+
const { publicKey, privateKey } = sodium.crypto_sign_keypair();
|
|
21
|
+
return { publicKey, privateKey };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Buat signature detached untuk message.
|
|
25
|
+
*
|
|
26
|
+
* @param message - Data input.
|
|
27
|
+
* @param privateKey - Private key Ed25519.
|
|
28
|
+
* @returns Signature.
|
|
29
|
+
*/
|
|
30
|
+
export async function signMessage(message, privateKey) {
|
|
31
|
+
await sodium.ready;
|
|
32
|
+
assertByteLength("privateKey", privateKey, sodium.crypto_sign_SECRETKEYBYTES);
|
|
33
|
+
return sodium.crypto_sign_detached(message, privateKey);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Verifikasi signature untuk message.
|
|
37
|
+
*
|
|
38
|
+
* @param message - Data input.
|
|
39
|
+
* @param signature - Signature detached.
|
|
40
|
+
* @param publicKey - Public key Ed25519.
|
|
41
|
+
* @returns True jika valid.
|
|
42
|
+
*/
|
|
43
|
+
export async function verifySignature(message, signature, publicKey) {
|
|
44
|
+
await sodium.ready;
|
|
45
|
+
assertByteLength("publicKey", publicKey, sodium.crypto_sign_PUBLICKEYBYTES);
|
|
46
|
+
return sodium.crypto_sign_verify_detached(signature, message, publicKey);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Buat signature dengan nonce/timestamp untuk anti‑replay.
|
|
50
|
+
*
|
|
51
|
+
* @param message - Data input.
|
|
52
|
+
* @param privateKey - Private key Ed25519.
|
|
53
|
+
* @param opts - Opsi nonce/timestamp.
|
|
54
|
+
* @returns Payload signature terstruktur.
|
|
55
|
+
*/
|
|
56
|
+
export async function signMessageWithNonce(message, privateKey, opts) {
|
|
57
|
+
await sodium.ready;
|
|
58
|
+
assertByteLength("privateKey", privateKey, sodium.crypto_sign_SECRETKEYBYTES);
|
|
59
|
+
const nonce = opts?.nonce ?? sodium.randombytes_buf(SIGNED_NONCE_BYTES);
|
|
60
|
+
assertByteLength("nonce", nonce, SIGNED_NONCE_BYTES);
|
|
61
|
+
const payload = {
|
|
62
|
+
v: 1,
|
|
63
|
+
nonce: encodeBase64(nonce),
|
|
64
|
+
ts: opts?.timestampMs ?? Date.now(),
|
|
65
|
+
message: encodeBase64(message),
|
|
66
|
+
};
|
|
67
|
+
const toSign = canonicalizeSignedPayload(payload);
|
|
68
|
+
const signature = sodium.crypto_sign_detached(new TextEncoder().encode(toSign), privateKey);
|
|
69
|
+
return {
|
|
70
|
+
...payload,
|
|
71
|
+
signature: encodeBase64(signature),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Verifikasi payload signature anti‑replay.
|
|
76
|
+
*
|
|
77
|
+
* @param payload - Payload signature.
|
|
78
|
+
* @param publicKey - Public key Ed25519.
|
|
79
|
+
* @returns True jika valid.
|
|
80
|
+
*/
|
|
81
|
+
export async function verifySignedMessage(payload, publicKey) {
|
|
82
|
+
await sodium.ready;
|
|
83
|
+
assertByteLength("publicKey", publicKey, sodium.crypto_sign_PUBLICKEYBYTES);
|
|
84
|
+
const nonce = decodeBase64(payload.nonce);
|
|
85
|
+
assertByteLength("nonce", nonce, SIGNED_NONCE_BYTES);
|
|
86
|
+
const signature = decodeBase64(payload.signature);
|
|
87
|
+
const toVerify = canonicalizeSignedPayload({
|
|
88
|
+
v: payload.v,
|
|
89
|
+
nonce: payload.nonce,
|
|
90
|
+
ts: payload.ts,
|
|
91
|
+
message: payload.message,
|
|
92
|
+
});
|
|
93
|
+
return sodium.crypto_sign_verify_detached(signature, new TextEncoder().encode(toVerify), publicKey);
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=signature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../../src/crypto/signature.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAelE,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAE9B,SAAS,yBAAyB,CAAC,OAA2C;IAC5E,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,CAAC,EAAE,OAAO,CAAC,CAAC;QACZ,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,EAAE,EAAE,OAAO,CAAC,EAAE;QACd,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB;IAC1C,MAAM,MAAM,CAAC,KAAK,CAAC;IACnB,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC,mBAAmB,EAAE,CAAC;IAC/D,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AACnC,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MAAM,CAAC,oBAAoB,CAC3C,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAChC,UAAU,CACX,CAAC;IAEF,OAAO;QACL,GAAG,OAAO;QACV,SAAS,EAAE,YAAY,CAAC,SAAS,CAAC;KACnC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAAwB,EACxB,SAAqB;IAErB,MAAM,MAAM,CAAC,KAAK,CAAC;IACnB,gBAAgB,CACd,WAAW,EACX,SAAS,EACT,MAAM,CAAC,0BAA0B,CAClC,CAAC;IACF,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC1C,gBAAgB,CAAC,OAAO,EAAE,KAAK,EAAE,kBAAkB,CAAC,CAAC;IACrD,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAClD,MAAM,QAAQ,GAAG,yBAAyB,CAAC;QACzC,CAAC,EAAE,OAAO,CAAC,CAAC;QACZ,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,EAAE,EAAE,OAAO,CAAC,EAAE;QACd,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,2BAA2B,CACvC,SAAS,EACT,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,EAClC,SAAS,CACV,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,13 @@ export type { AeadEnvelopeV1, PasswordKdfParamsV1, PasswordWrappedKeyV1 } from "
|
|
|
2
2
|
export { generateFileKey, wrapFileKeyWithMasterKey, unwrapFileKeyWithMasterKey, encryptFileData, decryptFileData, } from "./crypto/file-encryption.js";
|
|
3
3
|
export { generateMasterKey, generateMasterKeyWithPassword, wrapMasterKeyWithPassword, unwrapMasterKeyWithPassword, } from "./crypto/master-key.js";
|
|
4
4
|
export { generateRecoveryKey, wrapMasterKeyWithRecoveryKey, unwrapMasterKeyWithRecoveryKey, wrapRecoveryKeyWithMasterKey, unwrapRecoveryKeyWithMasterKey, } from "./crypto/recovery-key.js";
|
|
5
|
+
export type { KeyExchangeKeyPair, SessionKeys } from "./crypto/key-exchange.js";
|
|
6
|
+
export { generateKeyExchangeKeyPair, deriveKeyExchangePublicKeyFromPrivateKey, keyExchangeKeyPairFromPrivateKey, deriveClientSessionKeys, deriveServerSessionKeys, encryptSealedBox, decryptSealedBox, } from "./crypto/key-exchange.js";
|
|
7
|
+
export type { SigningKeyPair } from "./crypto/signature.js";
|
|
8
|
+
export { generateSigningKeyPair, signMessage, signMessageWithNonce, verifySignature, verifySignedMessage, } from "./crypto/signature.js";
|
|
9
|
+
export type { SignedMessageV1 } from "./crypto/signature.js";
|
|
10
|
+
export type { GroupMember, GroupMemberWire, GroupState, GroupWelcome, GroupCommit, GroupCommitAction, } from "./crypto/group.js";
|
|
11
|
+
export { createGroupState, openGroupWelcome, deriveGroupKey, createGroupCommit, verifyGroupCommitSignature, applyGroupCommit, } from "./crypto/group.js";
|
|
5
12
|
export type { PasswordKdfOptions } from "./crypto/password-kdf.js";
|
|
6
13
|
export { deriveKeyFromPassword, wrapKeyWithPassword, unwrapKeyWithPassword, } from "./crypto/password-kdf.js";
|
|
7
14
|
export { encodeBase64, decodeBase64 } from "./utils/encoding.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAE5F,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,0BAA0B,EAC1B,eAAe,EACf,eAAe,GAChB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,yBAAyB,EACzB,2BAA2B,GAC5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,GACjB,MAAM,0BAA0B,CAAC;AAElC,YAAY,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EACL,sBAAsB,EACtB,WAAW,EACX,oBAAoB,EACpB,eAAe,EACf,mBAAmB,GACpB,MAAM,uBAAuB,CAAC;AAE/B,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D,YAAY,EACV,WAAW,EACX,eAAe,EACf,UAAU,EACV,YAAY,EACZ,WAAW,EACX,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,iBAAiB,EACjB,0BAA0B,EAC1B,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAE3B,YAAY,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEjE,OAAO,EACL,MAAM,EACN,KAAK,IAAI,WAAW,EACpB,cAAc,EACd,YAAY,EACZ,qBAAqB,EACrB,4BAA4B,EAC5B,4BAA4B,EAC5B,gCAAgC,EAChC,gCAAgC,EAChC,kCAAkC,EAClC,QAAQ,EACR,UAAU,EACV,qBAAqB,EACrB,UAAU,EACV,QAAQ,EACR,MAAM,GACP,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export { generateFileKey, wrapFileKeyWithMasterKey, unwrapFileKeyWithMasterKey, encryptFileData, decryptFileData, } from "./crypto/file-encryption.js";
|
|
2
2
|
export { generateMasterKey, generateMasterKeyWithPassword, wrapMasterKeyWithPassword, unwrapMasterKeyWithPassword, } from "./crypto/master-key.js";
|
|
3
3
|
export { generateRecoveryKey, wrapMasterKeyWithRecoveryKey, unwrapMasterKeyWithRecoveryKey, wrapRecoveryKeyWithMasterKey, unwrapRecoveryKeyWithMasterKey, } from "./crypto/recovery-key.js";
|
|
4
|
+
export { generateKeyExchangeKeyPair, deriveKeyExchangePublicKeyFromPrivateKey, keyExchangeKeyPairFromPrivateKey, deriveClientSessionKeys, deriveServerSessionKeys, encryptSealedBox, decryptSealedBox, } from "./crypto/key-exchange.js";
|
|
5
|
+
export { generateSigningKeyPair, signMessage, signMessageWithNonce, verifySignature, verifySignedMessage, } from "./crypto/signature.js";
|
|
6
|
+
export { createGroupState, openGroupWelcome, deriveGroupKey, createGroupCommit, verifyGroupCommitSignature, applyGroupCommit, } from "./crypto/group.js";
|
|
4
7
|
export { deriveKeyFromPassword, wrapKeyWithPassword, unwrapKeyWithPassword, } from "./crypto/password-kdf.js";
|
|
5
8
|
export { encodeBase64, decodeBase64 } from "./utils/encoding.js";
|
|
6
9
|
export { sodium, ready as sodiumReady, randombytesBuf, cryptoPwhash, cryptoPwhashSaltBytes, cryptoPwhashOpslimitModerate, cryptoPwhashMemlimitModerate, aeadXChaCha20Poly1305IetfEncrypt, aeadXChaCha20Poly1305IetfDecrypt, aeadXChaCha20Poly1305IetfNpubBytes, toBase64, fromBase64, base64VariantOriginal, fromString, toString, memcmp, } from "./sodium.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,0BAA0B,EAC1B,eAAe,EACf,eAAe,GAChB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,8BAA8B,EAC9B,4BAA4B,EAC5B,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEjE,OAAO,EACL,MAAM,EACN,KAAK,IAAI,WAAW,EACpB,cAAc,EACd,YAAY,EACZ,qBAAqB,EACrB,4BAA4B,EAC5B,4BAA4B,EAC5B,gCAAgC,EAChC,gCAAgC,EAChC,kCAAkC,EAClC,QAAQ,EACR,UAAU,EACV,qBAAqB,EACrB,UAAU,EACV,QAAQ,EACR,MAAM,GACP,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,0BAA0B,EAC1B,eAAe,EACf,eAAe,GAChB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,8BAA8B,EAC9B,4BAA4B,EAC5B,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,0BAA0B,EAC1B,wCAAwC,EACxC,gCAAgC,EAChC,uBAAuB,EACvB,uBAAuB,EACvB,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,sBAAsB,EACtB,WAAW,EACX,oBAAoB,EACpB,eAAe,EACf,mBAAmB,GACpB,MAAM,uBAAuB,CAAC;AAY/B,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,iBAAiB,EACjB,0BAA0B,EAC1B,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEjE,OAAO,EACL,MAAM,EACN,KAAK,IAAI,WAAW,EACpB,cAAc,EACd,YAAY,EACZ,qBAAqB,EACrB,4BAA4B,EAC5B,4BAA4B,EAC5B,gCAAgC,EAChC,gCAAgC,EAChC,kCAAkC,EAClC,QAAQ,EACR,UAAU,EACV,qBAAqB,EACrB,UAAU,EACV,QAAQ,EACR,MAAM,GACP,MAAM,aAAa,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexwage/crypto",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Minimal crypto building blocks using libsodium (XChaCha20-Poly1305 + Argon2).",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -13,7 +13,11 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --coverage",
|
|
15
15
|
"start": "tsx examples/basic-flow.ts",
|
|
16
|
-
"build": "tsc -p tsconfig.json"
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"example:kx": "tsx examples/asymmetric-key-exchange.ts",
|
|
18
|
+
"example:sign": "tsx examples/asymmetric-signature.ts",
|
|
19
|
+
"example:basic": "tsx examples/basic-flow.ts",
|
|
20
|
+
"example:group": "tsx examples/group-basic.ts"
|
|
17
21
|
},
|
|
18
22
|
"keywords": [],
|
|
19
23
|
"author": "",
|