generated from fission-codes/project-template
-
Notifications
You must be signed in to change notification settings - Fork 24
/
browser.ts
339 lines (253 loc) · 8.2 KB
/
browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import * as uint8arrays from "uint8arrays"
import { webcrypto } from "one-webcrypto"
import tweetnacl from "tweetnacl"
import * as keystoreAES from "keystore-idb/aes/index.js"
import * as keystoreIDB from "keystore-idb/constants.js"
import { HashAlg, SymmAlg, SymmKeyLength } from "keystore-idb/types.js"
import { RSAKeyStore } from "keystore-idb/rsa/index.js"
import rsaOperations from "keystore-idb/rsa/index.js"
import * as typeChecks from "../../../common/type-checks.js"
import { Implementation, ImplementationOptions, VerifyArgs } from "../implementation.js"
// AES
export const aes = {
decrypt: aesDecrypt,
encrypt: aesEncrypt,
exportKey: aesExportKey,
genKey: aesGenKey,
}
export function importAesKey(key: Uint8Array, alg: SymmAlg): Promise<CryptoKey> {
return webcrypto.subtle.importKey(
"raw",
key,
{
name: alg,
length: SymmKeyLength.B256,
},
true,
[ "encrypt", "decrypt" ]
)
}
export async function aesDecrypt(encrypted: Uint8Array, key: CryptoKey | Uint8Array, alg: SymmAlg, iv?: Uint8Array): Promise<Uint8Array> {
const cryptoKey = typeChecks.isCryptoKey(key) ? key : await importAesKey(key, alg)
const decrypted = iv
? await webcrypto.subtle.decrypt(
{ name: alg, iv },
cryptoKey,
encrypted
)
// the keystore version prefixes the `iv` into the cipher text
: await keystoreAES.decryptBytes(encrypted, cryptoKey, { alg })
return new Uint8Array(decrypted)
}
export async function aesEncrypt(data: Uint8Array, key: CryptoKey | Uint8Array, alg: SymmAlg, iv?: Uint8Array): Promise<Uint8Array> {
const cryptoKey = typeChecks.isCryptoKey(key) ? key : await importAesKey(key, alg)
// the keystore version prefixes the `iv` into the cipher text
const encrypted = iv
? await webcrypto.subtle.encrypt(
{ name: alg, iv },
cryptoKey,
data
)
: await keystoreAES.encryptBytes(data, cryptoKey, { alg })
return new Uint8Array(encrypted)
}
export async function aesExportKey(key: CryptoKey): Promise<Uint8Array> {
const buffer = await webcrypto.subtle.exportKey("raw", key)
return new Uint8Array(buffer)
}
export function aesGenKey(alg: SymmAlg): Promise<CryptoKey> {
return keystoreAES.makeKey({ length: SymmKeyLength.B256, alg })
}
// DID
export const did: Implementation[ "did" ] = {
keyTypes: {
"bls12-381": {
magicBytes: new Uint8Array([ 0xea, 0x01 ]),
verify: () => { throw new Error("Not implemented") },
},
"ed25519": {
magicBytes: new Uint8Array([ 0xed, 0x01 ]),
verify: ed25519Verify,
},
"rsa": {
magicBytes: new Uint8Array([ 0x00, 0xf5, 0x02 ]),
verify: rsaVerify,
},
}
}
export async function ed25519Verify({ message, publicKey, signature }: VerifyArgs): Promise<boolean> {
return tweetnacl.sign.detached.verify(message, signature, publicKey)
}
export async function rsaVerify({ message, publicKey, signature }: VerifyArgs): Promise<boolean> {
return rsaOperations.verify(
message,
signature,
await webcrypto.subtle.importKey(
"spki",
publicKey,
{ name: keystoreIDB.RSA_WRITE_ALG, hash: RSA_HASHING_ALGORITHM },
false,
[ "verify" ]
),
8
)
}
// HASH
export const hash = {
sha256
}
export async function sha256(bytes: Uint8Array): Promise<Uint8Array> {
return new Uint8Array(await webcrypto.subtle.digest("sha-256", bytes))
}
// KEYSTORE
export function ksClearStore(ks: RSAKeyStore): Promise<void> {
return ks.destroy()
}
export async function ksDecrypt(ks: RSAKeyStore, cipherText: Uint8Array): Promise<Uint8Array> {
const exchangeKey = await ks.exchangeKey()
return rsaDecrypt(
cipherText,
exchangeKey.privateKey
)
}
export async function ksExportSymmKey(ks: RSAKeyStore, keyName: string): Promise<Uint8Array> {
if (await ks.keyExists(keyName) === false) {
throw new Error(`Expected a key under the name '${keyName}', but couldn't find anything`)
// We're throwing an error here so that the function `getSymmKey` below doesn't create a key.
}
const key = await ks.getSymmKey(keyName)
const raw = await webcrypto.subtle.exportKey("raw", key)
return new Uint8Array(raw)
}
export function ksGetAlgorithm(ks: RSAKeyStore): Promise<string> {
return Promise.resolve("rsa")
}
export function ksGetUcanAlgorithm(ks: RSAKeyStore): Promise<string> {
return Promise.resolve("RS256")
}
export function ksImportSymmKey(ks: RSAKeyStore, key: Uint8Array, name: string): Promise<void> {
return ks.importSymmKey(uint8arrays.toString(key, "base64pad"), name)
}
export function ksKeyExists(ks: RSAKeyStore, keyName: string): Promise<boolean> {
return ks.keyExists(keyName)
}
export async function ksPublicExchangeKey(ks: RSAKeyStore): Promise<Uint8Array> {
const keypair = await ks.exchangeKey()
const spki = await webcrypto.subtle.exportKey("spki", keypair.publicKey)
return new Uint8Array(spki)
}
export async function ksPublicWriteKey(ks: RSAKeyStore): Promise<Uint8Array> {
const keypair = await ks.writeKey()
const spki = await webcrypto.subtle.exportKey("spki", keypair.publicKey)
return new Uint8Array(spki)
}
export async function ksSign(ks: RSAKeyStore, message: Uint8Array): Promise<Uint8Array> {
const writeKey = await ks.writeKey()
const arrayBuffer = await rsaOperations.sign(
message,
writeKey.privateKey,
ks.cfg.charSize
)
return new Uint8Array(arrayBuffer)
}
// MISC
export const misc = {
randomNumbers,
}
export function randomNumbers(options: { amount: number }): Uint8Array {
return webcrypto.getRandomValues(new Uint8Array(options.amount))
}
// RSA
export const rsa = {
decrypt: rsaDecrypt,
encrypt: rsaEncrypt,
exportPublicKey: rsaExportPublicKey,
genKey: rsaGenKey,
}
// RSA
// ---
// Exchange keys:
export const RSA_ALGORITHM = "RSA-OAEP"
export const RSA_HASHING_ALGORITHM = "SHA-256"
export function importRsaKey(key: Uint8Array, keyUsages: KeyUsage[]): Promise<CryptoKey> {
return webcrypto.subtle.importKey(
"spki",
key,
{ name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
false,
keyUsages
)
}
export async function rsaDecrypt(data: Uint8Array, privateKey: CryptoKey | Uint8Array): Promise<Uint8Array> {
const arrayBuffer = await webcrypto.subtle.decrypt(
{
name: RSA_ALGORITHM
},
typeChecks.isCryptoKey(privateKey)
? privateKey
: await importRsaKey(privateKey, [ "decrypt" ])
,
data
)
return new Uint8Array(arrayBuffer)
}
export async function rsaEncrypt(message: Uint8Array, publicKey: CryptoKey | Uint8Array): Promise<Uint8Array> {
const key = typeChecks.isCryptoKey(publicKey)
? publicKey
: await importRsaKey(publicKey, [ "encrypt" ])
const arrayBuffer = await webcrypto.subtle.encrypt(
{
name: RSA_ALGORITHM
},
key,
message
)
return new Uint8Array(arrayBuffer)
}
export async function rsaExportPublicKey(key: CryptoKey): Promise<Uint8Array> {
const buffer = await webcrypto.subtle.exportKey("spki", key)
return new Uint8Array(buffer)
}
export function rsaGenKey(): Promise<CryptoKeyPair> {
return webcrypto.subtle.generateKey(
{
name: RSA_ALGORITHM,
modulusLength: 2048,
publicExponent: new Uint8Array([ 0x01, 0x00, 0x01 ]),
hash: { name: RSA_HASHING_ALGORITHM }
},
true,
[ "encrypt", "decrypt" ]
)
}
// 🛳
export async function implementation(
{ storeName, exchangeKeyName, writeKeyName }: ImplementationOptions
): Promise<Implementation> {
const ks = await RSAKeyStore.init({
charSize: 8,
hashAlg: HashAlg.SHA_256,
storeName,
exchangeKeyName,
writeKeyName,
})
return {
aes,
did,
hash,
misc,
rsa,
keystore: {
clearStore: (...args) => ksClearStore(ks, ...args),
decrypt: (...args) => ksDecrypt(ks, ...args),
exportSymmKey: (...args) => ksExportSymmKey(ks, ...args),
getAlgorithm: (...args) => ksGetAlgorithm(ks, ...args),
getUcanAlgorithm: (...args) => ksGetUcanAlgorithm(ks, ...args),
importSymmKey: (...args) => ksImportSymmKey(ks, ...args),
keyExists: (...args) => ksKeyExists(ks, ...args),
publicExchangeKey: (...args) => ksPublicExchangeKey(ks, ...args),
publicWriteKey: (...args) => ksPublicWriteKey(ks, ...args),
sign: (...args) => ksSign(ks, ...args),
},
}
}