-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.js
493 lines (461 loc) · 15.1 KB
/
crypto.js
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
async function msgEncryptAndSign(msg, privateKey, publicKey, privateKeyECDH) {
let ciphertext = await msgEncryptBasedECDH(msg, privateKey, publicKey);
let sig = await sign(ciphertext, privateKeyECDH);
return { msg: ciphertext, sig }
}
async function msgDecryptAndVerify(payload, privateKeyECDH, publicKeyECDH, publicKeyECDSA) {
const result = await verify(payload.msg, payload.sig, publicKeyECDSA);
if (!result) {
return new Error('Verification failed');
}
let plaintext = await msgDecryptBasedECDH(payload.msg, privateKeyECDH, publicKeyECDH);
return plaintext;
}
async function msgEncryptBasedECDH(msg, privateKey, publicKey) {
let secret = await deriveSecretKeyToECDH(privateKey, publicKey);
let ciphertext = await encrypt(secret, msg);
secret = "";
return ciphertext;
}
async function msgDecryptBasedECDH(msg, privateKey, publicKey) {
let secret = await deriveSecretKeyToECDH(privateKey, publicKey);
let plaintext = await decrypt(hex2buf(msg), secret);
secret = "";
return plaintext;
}
async function exportKeySuite(keySuite) {
return {
ECDH: await window.crypto.subtle.exportKey("jwk", keySuite.ECDH.publicKey),
ECDSA: await window.crypto.subtle.exportKey("jwk", keySuite.ECDSA.publicKey)
}
}
async function genKeySuite() {
let ecdh = await window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256"
},
false,
["deriveKey"]
);
let ecdsa = await window.crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256"
},
false,
["sign", "verify"]
);
return { ECDH: ecdh, ECDSA: ecdsa }
}
async function sign(msg, privateKey) {
let enc = new TextEncoder();
let signature = await window.crypto.subtle.sign(
{
name: "ECDSA",
hash: { name: "SHA-512" },
},
privateKey,
enc.encode(msg)
);
return buf2hex(signature)
}
async function verify(msg, sig, publicKey) {
let enc = new TextEncoder();
let result = await window.crypto.subtle.verify(
{
name: "ECDSA",
hash: { name: "SHA-512" },
},
publicKey,
hex2buf(sig),
enc.encode(msg)
)
return result
}
async function encrypt(secretKey, msg) {
let iv = window.crypto.getRandomValues(new Uint8Array(12));
let enc = new TextEncoder();
let ciphertext = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
secretKey,
enc.encode(msg)
);
enc = "";
const struct = Uint8Array.from([...iv, ...new Uint8Array(ciphertext)])
return buf2hex([...struct])
}
async function decrypt(msg, secretKey) {
try {
let decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: msg.slice(0, 12)
},
secretKey,
msg.slice(12, msg.length)
);
secretKey = "";
let dec = new TextDecoder();
return dec.decode(decrypted)
} catch (e) {
return e
}
}
function deriveSecretKeyToECDH(privateKey, publicKey) {
return window.crypto.subtle.deriveKey(
{
name: "ECDH",
public: publicKey
},
privateKey,
{
name: "AES-GCM",
length: 256
},
false,
["encrypt", "decrypt"]
);
}
function buf2hex(buffer) { // buffer is an ArrayBuffer
return [...new Uint8Array(buffer)]
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}
function hex2buf(msg) {
let bytes = new Uint8Array(Math.ceil(msg.length / 2));
for (var i = 0; i < bytes.length; i++) bytes[i] = parseInt(msg.substr(i * 2, 2), 16);
return bytes;
}
async function wrapKey(format, keyToWrap, password) {
let { wrappingKey, salt } = await deriveKeyToWrappingKey(password);
let iv = window.crypto.getRandomValues(new Uint8Array(12));
let wrappedKey = await window.crypto.subtle.wrapKey(
format,
keyToWrap,
wrappingKey,
{
name: "AES-GCM",
iv: iv
}
);
const struct = Uint8Array.from([...iv, ...new Uint8Array(wrappedKey), ...new Uint8Array(salt)])
return buf2hex([...struct])
}
function PGPWords(fingerprint) {
let words = "";
let j = 0;
for (let i = 0; i < fingerprint.length - 1; i += 2) {
const hex = fingerprint[i].concat(fingerprint[i + 1]);
words += `${raw_words[hex][j % 2]} `;
j++;
}
return words;
}
async function fingerprint(keySuite, keySuiteOtherParty) {
const rootObject = [];
const publicKeys = [keySuite.ECDH.publicKey, keySuite.ECDSA.publicKey, keySuiteOtherParty.ECDH, keySuiteOtherParty.ECDSA]
for (let i = 0; i < publicKeys.length; i++) {
const exportedPublicKey = await window.crypto.subtle.exportKey("jwk", publicKeys[i])
const hashedPublicKey = await rfc7638EC(exportedPublicKey);
rootObject.push(hashedPublicKey);
}
const data = new TextEncoder().encode(JSON.stringify(rootObject.sort()));
const hash = await window.crypto.subtle.digest('SHA-256', data);
return buf2hex(hash).toUpperCase()
}
async function rfc7638EC(publicKey) {
let lexOrder = {};
lexOrder.crv = publicKey.crv
lexOrder.kty = publicKey.kty
lexOrder.x = publicKey.x
lexOrder.y = publicKey.y
const data = new TextEncoder().encode(JSON.stringify(lexOrder));
const hash = await window.crypto.subtle.digest('SHA-256', data);
return buf2hex(hash);
}
async function deriveKeyToWrappingKey(password) {
const enc = new TextEncoder();
let kdf = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveBits", "deriveKey"]
);
let salt = window.crypto.getRandomValues(new Uint8Array(16));
let wrappingKey = await window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
salt: salt,
"iterations": 100000,
"hash": "SHA-256"
},
kdf,
{ "name": "AES-GCM", "length": 256 },
true,
["wrapKey", "unwrapKey"]
);
return { wrappingKey, salt }
}
const raw_words = {
'00': ['aardvark', 'adroitness'],
'01': ['absurd', 'adviser'],
'02': ['accrue', 'aftermath'],
'03': ['acme', 'aggregate'],
'04': ['adrift', 'alkali'],
'05': ['adult', 'almighty'],
'06': ['afflict', 'amulet'],
'07': ['ahead', 'amusement'],
'08': ['aimless', 'antenna'],
'09': ['Algol', 'applicant'],
'0A': ['allow', 'Apollo'],
'0B': ['alone', 'armistice'],
'0C': ['ammo', 'article'],
'0D': ['ancient', 'asteroid'],
'0E': ['apple', 'Atlantic'],
'0F': ['artist', 'atmosphere'],
'10': ['assume', 'autopsy'],
'11': ['Athens', 'Babylon'],
'12': ['atlas', 'backwater'],
'13': ['Aztec', 'barbecue'],
'14': ['baboon', 'belowground'],
'15': ['backfield', 'bifocals'],
'16': ['backward', 'bodyguard'],
'17': ['banjo', 'bookseller'],
'18': ['beaming', 'borderline'],
'19': ['bedlamp', 'bottomless'],
'1A': ['beehive', 'Bradbury'],
'1B': ['beeswax', 'bravado'],
'1C': ['befriend', 'Brazilian'],
'1D': ['Belfast', 'breakaway'],
'1E': ['berserk', 'Burlington'],
'1F': ['billiard', 'businessman'],
'20': ['bison', 'butterfat'],
'21': ['blackjack', 'Camelot'],
'22': ['blockade', 'candidate'],
'23': ['blowtorch', 'cannonball'],
'24': ['bluebird', 'Capricorn'],
'25': ['bombast', 'caravan'],
'26': ['bookshelf', 'caretaker'],
'27': ['brackish', 'celebrate'],
'28': ['breadline', 'cellulose'],
'29': ['breakup', 'certify'],
'2A': ['brickyard', 'chambermaid'],
'2B': ['briefcase', 'Cherokee'],
'2C': ['Burbank', 'Chicago'],
'2D': ['button', 'clergyman'],
'2E': ['buzzard', 'coherence'],
'2F': ['cement', 'combustion'],
'30': ['chairlift', 'commando'],
'31': ['chatter', 'company'],
'32': ['checkup', 'component'],
'33': ['chisel', 'concurrent'],
'34': ['choking', 'confidence'],
'35': ['chopper', 'conformist'],
'36': ['Christmas', 'congregate'],
'37': ['clamshell', 'consensus'],
'38': ['classic', 'consulting'],
'39': ['classroom', 'corporate'],
'3A': ['cleanup', 'corrosion'],
'3B': ['clockwork', 'councilman'],
'3C': ['cobra', 'crossover'],
'3D': ['commence', 'crucifix'],
'3E': ['concert', 'cumbersome'],
'3F': ['cowbell', 'customer'],
'40': ['crackdown', 'Dakota'],
'41': ['cranky', 'decadence'],
'42': ['crowfoot', 'December'],
'43': ['crucial', 'decimal'],
'44': ['crumpled', 'designing'],
'45': ['crusade', 'detector'],
'46': ['cubic', 'detergent'],
'47': ['dashboard', 'determine'],
'48': ['deadbolt', 'dictator'],
'49': ['deckhand', 'dinosaur'],
'4A': ['dogsled', 'direction'],
'4B': ['dragnet', 'disable'],
'4C': ['drainage', 'disbelief'],
'4D': ['dreadful', 'disruptive'],
'4E': ['drifter', 'distortion'],
'4F': ['dropper', 'document'],
'50': ['drumbeat', 'embezzle'],
'51': ['drunken', 'enchanting'],
'52': ['Dupont', 'enrollment'],
'53': ['dwelling', 'enterprise'],
'54': ['eating', 'equation'],
'55': ['edict', 'equipment'],
'56': ['egghead', 'escapade'],
'57': ['eightball', 'Eskimo'],
'58': ['endorse', 'everyday'],
'59': ['endow', 'examine'],
'5A': ['enlist', 'existence'],
'5B': ['erase', 'exodus'],
'5C': ['escape', 'fascinate'],
'5D': ['exceed', 'filament'],
'5E': ['eyeglass', 'finicky'],
'5F': ['eyetooth', 'forever'],
'60': ['facial', 'fortitude'],
'61': ['fallout', 'frequency'],
'62': ['flagpole', 'gadgetry'],
'63': ['flatfoot', 'Galveston'],
'64': ['flytrap', 'getaway'],
'65': ['fracture', 'glossary'],
'66': ['framework', 'gossamer'],
'67': ['freedom', 'graduate'],
'68': ['frighten', 'gravity'],
'69': ['gazelle', 'guitarist'],
'6A': ['Geiger', 'hamburger'],
'6B': ['glitter', 'Hamilton'],
'6C': ['glucose', 'handiwork'],
'6D': ['goggles', 'hazardous'],
'6E': ['goldfish', 'headwaters'],
'6F': ['gremlin', 'hemisphere'],
'70': ['guidance', 'hesitate'],
'71': ['hamlet', 'hideaway'],
'72': ['highchair', 'holiness'],
'73': ['hockey', 'hurricane'],
'74': ['indoors', 'hydraulic'],
'75': ['indulge', 'impartial'],
'76': ['inverse', 'impetus'],
'77': ['involve', 'inception'],
'78': ['island', 'indigo'],
'79': ['jawbone', 'inertia'],
'7A': ['keyboard', 'infancy'],
'7B': ['kickoff', 'inferno'],
'7C': ['kiwi', 'informant'],
'7D': ['klaxon', 'insincere'],
'7E': ['locale', 'insurgent'],
'7F': ['lockup', 'integrate'],
'80': ['merit', 'intention'],
'81': ['minnow', 'inventive'],
'82': ['miser', 'Istanbul'],
'83': ['Mohawk', 'Jamaica'],
'84': ['mural', 'Jupiter'],
'85': ['music', 'leprosy'],
'86': ['necklace', 'letterhead'],
'87': ['Neptune', 'liberty'],
'88': ['newborn', 'maritime'],
'89': ['nightbird', 'matchmaker'],
'8A': ['Oakland', 'maverick'],
'8B': ['obtuse', 'Medusa'],
'8C': ['offload', 'megaton'],
'8D': ['optic', 'microscope'],
'8E': ['orca', 'microwave'],
'8F': ['payday', 'midsummer'],
'90': ['peachy', 'millionaire'],
'91': ['pheasant', 'miracle'],
'92': ['physique', 'misnomer'],
'93': ['playhouse', 'molasses'],
'94': ['Pluto', 'molecule'],
'95': ['preclude', 'Montana'],
'96': ['prefer', 'monument'],
'97': ['preshrunk', 'mosquito'],
'98': ['printer', 'narrative'],
'99': ['prowler', 'nebula'],
'9A': ['pupil', 'newsletter'],
'9B': ['puppy', 'Norwegian'],
'9C': ['python', 'October'],
'9D': ['quadrant', 'Ohio'],
'9E': ['quiver', 'onlooker'],
'9F': ['quota', 'opulent'],
'A0': ['ragtime', 'Orlando'],
'A1': ['ratchet', 'outfielder'],
'A2': ['rebirth', 'Pacific'],
'A3': ['reform', 'pandemic'],
'A4': ['regain', 'Pandora'],
'A5': ['reindeer', 'paperweight'],
'A6': ['rematch', 'paragon'],
'A7': ['repay', 'paragraph'],
'A8': ['retouch', 'paramount'],
'A9': ['revenge', 'passenger'],
'AA': ['reward', 'pedigree'],
'AB': ['rhythm', 'Pegasus'],
'AC': ['ribcage', 'penetrate'],
'AD': ['ringbolt', 'perceptive'],
'AE': ['robust', 'performance'],
'AF': ['rocker', 'pharmacy'],
'B0': ['ruffled', 'phonetic'],
'B1': ['sailboat', 'photograph'],
'B2': ['sawdust', 'pioneer'],
'B3': ['scallion', 'pocketful'],
'B4': ['scenic', 'politeness'],
'B5': ['scorecard', 'positive'],
'B6': ['Scotland', 'potato'],
'B7': ['seabird', 'processor'],
'B8': ['select', 'provincial'],
'B9': ['sentence', 'proximate'],
'BA': ['shadow', 'puberty'],
'BB': ['shamrock', 'publisher'],
'BC': ['showgirl', 'pyramid'],
'BD': ['skullcap', 'quantity'],
'BE': ['skydive', 'racketeer'],
'BF': ['slingshot', 'rebellion'],
'C0': ['slowdown', 'recipe'],
'C1': ['snapline', 'recover'],
'C2': ['snapshot', 'repellent'],
'C3': ['snowcap', 'replica'],
'C4': ['snowslide', 'reproduce'],
'C5': ['solo', 'resistor'],
'C6': ['southward', 'responsive'],
'C7': ['soybean', 'retraction'],
'C8': ['spaniel', 'retrieval'],
'C9': ['spearhead', 'retrospect'],
'CA': ['spellbind', 'revenue'],
'CB': ['spheroid', 'revival'],
'CC': ['spigot', 'revolver'],
'CD': ['spindle', 'sandalwood'],
'CE': ['spyglass', 'sardonic'],
'CF': ['stagehand', 'Saturday'],
'D0': ['stagnate', 'savagery'],
'D1': ['stairway', 'scavenger'],
'D2': ['standard', 'sensation'],
'D3': ['stapler', 'sociable'],
'D4': ['steamship', 'souvenir'],
'D5': ['sterling', 'specialist'],
'D6': ['stockman', 'speculate'],
'D7': ['stopwatch', 'stethoscope'],
'D8': ['stormy', 'stupendous'],
'D9': ['sugar', 'supportive'],
'DA': ['surmount', 'surrender'],
'DB': ['suspense', 'suspicious'],
'DC': ['sweatband', 'sympathy'],
'DD': ['swelter', 'tambourine'],
'DE': ['tactics', 'telephone'],
'DF': ['talon', 'therapist'],
'E0': ['tapeworm', 'tobacco'],
'E1': ['tempest', 'tolerance'],
'E2': ['tiger', 'tomorrow'],
'E3': ['tissue', 'torpedo'],
'E4': ['tonic', 'tradition'],
'E5': ['topmost', 'travesty'],
'E6': ['tracker', 'trombonist'],
'E7': ['transit', 'truncated'],
'E8': ['trauma', 'typewriter'],
'E9': ['treadmill', 'ultimate'],
'EA': ['Trojan', 'undaunted'],
'EB': ['trouble', 'underfoot'],
'EC': ['tumor', 'unicorn'],
'ED': ['tunnel', 'unify'],
'EE': ['tycoon', 'universe'],
'EF': ['uncut', 'unravel'],
'F0': ['unearth', 'upcoming'],
'F1': ['unwind', 'vacancy'],
'F2': ['uproot', 'vagabond'],
'F3': ['upset', 'vertigo'],
'F4': ['upshot', 'Virginia'],
'F5': ['vapor', 'visitor'],
'F6': ['village', 'vocalist'],
'F7': ['virus', 'voyager'],
'F8': ['Vulcan', 'warranty'],
'F9': ['waffle', 'Waterloo'],
'FA': ['wallet', 'whimsical'],
'FB': ['watchword', 'Wichita'],
'FC': ['wayside', 'Wilmington'],
'FD': ['willow', 'Wyoming'],
'FE': ['woodlark', 'yesteryear'],
'FF': ['Zulu', 'Yucatan']
}