-
Notifications
You must be signed in to change notification settings - Fork 21
/
mpw.js
385 lines (329 loc) · 11.6 KB
/
mpw.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
/*! by Tom Thorogood <[email protected]> */
/*! This work is licensed under the Creative Commons Attribution 4.0
International License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/4.0/ or see LICENSE. */
// JS Web Crypto implementation of http://masterpasswordapp.com/algorithm.html
class MPW {
constructor(name, password, version = MPW.VERSION) {
// The algorithm version
this.version = version;
// Store name on the object, this is not used at all internally
this.name = name;
// Check for valid algorithm versions
if (version >= 0 && version <= MPW.VERSION) {
// Calculate the master key which will be used to calculate
// the password seed
this.key = MPW.calculateKey(name, password, version);
} else {
this.key = Promise.reject(new Error(`Algorithm version ${version} not implemented`));
}
}
// calculateKey takes ~ 1450.000ms to complete
static calculateKey(name, password, version = MPW.VERSION) {
if (!name || !name.length) {
return Promise.reject(new Error("Argument name not present"));
}
if (!password || !password.length) {
return Promise.reject(new Error("Argument password not present"));
}
try {
// Cache the number of characters in name for older buggy
// versions of MPW
let nameCharLength = name.length;
// Convert password string to a Uint8Array w/ UTF-8
password = MPW.txtencoder.encode(password);
// Convert name string to a Uint8Array w/ UTF-8
name = MPW.txtencoder.encode(name);
// Convert MPW.NS string to a Uint8Array w/ UTF-8
let NS = MPW.txtencoder.encode(MPW.NS);
// Create salt array and a DataView representing it
var salt = new Uint8Array(
NS.length
+ 4/*sizeof(uint32)*/ + name.length
);
let saltView = new DataView(salt.buffer, salt.byteOffset, salt.byteLength);
let i = 0;
// Set salt[0,] to NS
salt.set(NS, i); i += NS.length;
if (version < 3) {
// Set data[i,i+4] to nameCharLength UINT32 in big-endian form
saltView.setUint32(i, nameCharLength, false/*big-endian*/); i += 4/*sizeof(uint32)*/;
} else {
// Set salt[i,i+4] to name.length UINT32 in big-endian form
saltView.setUint32(i, name.length, false/*big-endian*/); i += 4/*sizeof(uint32)*/;
}
// Set salt[i,] to name
salt.set(name, i); i += name.length;
} catch (e) {
return Promise.reject(e);
}
// Derive the master key w/ scrypt
// why is buflen 64*8==512 and not 32*8==256 ?
let key = window.scrypt(password, salt, 32768/*= n*/, 8/*= r*/, 2/*= p*/, 64/*= buflen*/);
// If the Web Crypto API is supported import the key, otherwise return
return window.crypto.subtle
? key.then(
// Import the key into WebCrypto to use later with sign while
// being non-extractable
key => window.crypto.subtle.importKey("raw", key, {
name: "HMAC",
hash: {
name: "SHA-256"
}
}, false/*not extractable*/, [ "sign" ])/*= key*/
)
: key;
}
// calculateSeed takes ~ 3.000ms to complete + the time of calculateKey once
calculateSeed(site, counter = 1, context = null, NS = MPW.NS) {
if (!site) {
return Promise.reject(new Error("Argument site not present"));
}
if (counter < 1 || counter > 4294967295/*Math.pow(2, 32) - 1*/) {
return Promise.reject(new Error("Argument counter out of range"));
}
try {
// Cache the number of characters in site for older buggy
// versions of MPW
let siteCharLength = site.length;
// Convert salt string to a Uint8Array w/ UTF-8
site = MPW.txtencoder.encode(site);
// Convert NS string to a Uint8Array w/ UTF-8
NS = MPW.txtencoder.encode(NS);
if (context) {
// Convert context string to a Uint8Array w/ UTF-8
context = MPW.txtencoder.encode(context);
}
// Create data array and a DataView representing it
var data = new Uint8Array(
NS.length
+ 4/*sizeof(uint32)*/ + site.length
+ 4/*sizeof(int32)*/
+ (context
? 4/*sizeof(uint32)*/ + context.length
: 0)
);
let dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
let i = 0;
// Set data[0,] to NS
data.set(NS, i); i += NS.length;
if (this.version < 2) {
// Set data[i,i+4] to siteCharLength UINT32 in big-endian form
dataView.setUint32(i, siteCharLength, false/*big-endian*/); i += 4/*sizeof(uint32)*/;
} else {
// Set data[i,i+4] to site.length UINT32 in big-endian form
dataView.setUint32(i, site.length, false/*big-endian*/); i += 4/*sizeof(uint32)*/;
}
// Set data[i,] to site
data.set(site, i); i += site.length;
// Set data[i,i+4] to counter INT32 in big-endian form
dataView.setInt32(i, counter, false/*big-endian*/); i += 4/*sizeof(int32)*/;
if (context) {
// Set data[i,i+4] to context.length UINT32 in big-endian form
dataView.setUint32(i, context.length, false/*big-endian*/); i += 4/*sizeof(uint32)*/;
// Set data[i,] to context
data.set(context, i); i += context.length;
}
} catch (e) {
return Promise.reject(e);
}
// If the Web Crypto API is supported use it, otherwise rely on crypto-js
if (window.crypto.subtle) {
return this.key.then(
// Sign data using HMAC-SHA-256 w/ this.key
key => window.crypto.subtle.sign({
name: "HMAC",
hash: {
name: "SHA-256"
}
}, key, data)/*= seed*/
).then(
// Convert the seed to Uint8Array from ArrayBuffer
seed => new Uint8Array(seed)/*= seed*/
);
} else {
return this.key.then(function (key) {
// Create crypto-js WordArrays from Uint8Arrays data and key
data = CryptoJS.lib.WordArray.create(data);
key = CryptoJS.lib.WordArray.create(key);
// Sign data using HMAC-SHA-256 w/ key
return CryptoJS.HmacSHA256(data, key)/*= seed*/;
}).then(function (hash) {
// Create seed array and a DataView representing it
let seed = new Uint8Array(hash.words.length * 4/*sizeof(int32)*/);
let seedView = new DataView(seed.buffer, seed.byteOffset, seed.byteLength);
// Loop over hash.words which are INT32
for (let i = 0; i < hash.words.length; i++) {
// Set seed[i*4,i*4+4] to hash.words[i] INT32 in big-endian form
seedView.setInt32(i * 4/*sizeof(int32)*/, hash.words[i], false/*big-endian*/);
}
// Return the seed Uint8Array
return seed;
});
}
}
// generate takes ~ 0.200ms to complete + the time of calculateSeed
generate(site, counter = 1, context = null, template = "long", NS = MPW.NS) {
// Does the requested template exist?
if (!(template in MPW.templates)) {
return Promise.reject(new Error("Argument template invalid"));
}
// Calculate the seed
let seed = this.calculateSeed(site, counter, context, NS);
if (this.version < 1) {
// Convert seed from host byte order to network byte
// to be compatible with v0 of MPW
// Follows the implementation at https://github.com/...
// Lyndir/MasterPassword/blob/master/MasterPassword/...
// Java/masterpassword-algorithm/src/main/java/com/...
// lyndir/masterpassword/MasterKeyV0.java#L105
seed = seed.then(function (seedBytes) {
let seed = new Uint16Array(seedBytes.length);
for (let i = 0; i < seed.length; i++) {
seed[i] = (seedBytes[i] > 127 ? 0x00ff : 0x0000) | (seedBytes[i] << 8);
}
return seed;
});
}
return seed.then(function (seed) {
// Find the selected template array
template = MPW.templates[template];
// Select the specific template based on seed[0]
template = template[seed[0] % template.length];
// Split the template string (e.g. xxx...xxx)
return template.split("").map(function (c, i) {
// Use MPW.passchars to map the template string (e.g. xxx...xxx)
// to characters (e.g. c -> bcdfghjklmnpqrstvwxyz)
let chars = MPW.passchars[c];
// Select the character using seed[i + 1]
return chars[seed[i + 1] % chars.length];
}).join("");
})/*= password*/;
}
// generate a password with the password namespace
generateAuthentication(site, counter = 1, context = "", template = "long") {
return this.generate(site, counter, context, template, MPW.AuthenticationNS);
}
// generate a username with the login namespace
generateIdentification(site, counter = 1, context = "", template = "name") {
return this.generate(site, counter, context, template, MPW.IdentificationNS);
}
// generate a security answer with the answer namespace
generateRecovery(site, counter = 1, context = "", template = "phrase") {
return this.generate(site, counter, context, template, MPW.RecoveryNS);
}
// generate a password with the password namespace
//
// Deprecated: use generateAuthentication instead.
generatePassword(site, counter = 1, template = "long") {
return this.generate(site, counter, null, template, MPW.PasswordNS);
}
// generate a username with the login namespace
//
// Deprecated: use generateIdentification instead.
generateLogin(site, counter = 1, template = "name") {
return this.generate(site, counter, null, template, MPW.LoginNS);
}
// generate a security answer with the answer namespace
//
// Deprecated: use generateRecovery instead.
generateAnswer(site, counter = 1, context = "", template = "phrase") {
return this.generate(site, counter, context, template, MPW.AnswerNS);
}
invalidate() {
// Replace this.key w/ a Promise.reject
// Preventing all future access
this.key = Promise.reject(new Error("invalid state"));
}
static test() {
// Pretty simple test here
return new MPW("user", "password").generate("example.com", 1, null, "long", MPW.NS).then(function (password) {
console.assert(password === "ZedaFaxcZaso9*", `Self-test failed; expected: ZedaFaxcZaso9*; got: ${password}`);
return password === "ZedaFaxcZaso9*"
? Promise.resolve()
: Promise.reject(new Error(`Self-test failed; expected: ZedaFaxcZaso9*; got: ${password}`));
});
}
}
// A TextEncoder in UTF-8 to convert strings to `Uint8Array`s
MPW.txtencoder = new TextEncoder;
// The latest version of MPW supported
MPW.VERSION = 3;
// The namespace used in calculateKey
MPW.NS = "com.lyndir.masterpassword";
// The namespaces used in calculateSeed
MPW.AuthenticationNS = "com.lyndir.masterpassword";
MPW.IdentificationNS = "com.lyndir.masterpassword.login";
MPW.RecoveryNS = "com.lyndir.masterpassword.answer";
// Legacy namespaces used in calculateSeed
MPW.PasswordNS = MPW.AuthenticationNS;
MPW.LoginNS = MPW.IdentificationNS;
MPW.AnswerNS = MPW.RecoveryNS;
// The templates that passwords may be created from
// The characters map to MPW.passchars
MPW.templates = {
maximum: [
"anoxxxxxxxxxxxxxxxxx",
"axxxxxxxxxxxxxxxxxno"
],
long: [
"CvcvnoCvcvCvcv",
"CvcvCvcvnoCvcv",
"CvcvCvcvCvcvno",
"CvccnoCvcvCvcv",
"CvccCvcvnoCvcv",
"CvccCvcvCvcvno",
"CvcvnoCvccCvcv",
"CvcvCvccnoCvcv",
"CvcvCvccCvcvno",
"CvcvnoCvcvCvcc",
"CvcvCvcvnoCvcc",
"CvcvCvcvCvccno",
"CvccnoCvccCvcv",
"CvccCvccnoCvcv",
"CvccCvccCvcvno",
"CvcvnoCvccCvcc",
"CvcvCvccnoCvcc",
"CvcvCvccCvccno",
"CvccnoCvcvCvcc",
"CvccCvcvnoCvcc",
"CvccCvcvCvccno"
],
medium: [
"CvcnoCvc",
"CvcCvcno"
],
basic: [
"aaanaaan",
"aannaaan",
"aaannaaa"
],
short: [
"Cvcn"
],
pin: [
"nnnn"
],
name: [
"cvccvcvcv"
],
phrase: [
"cvcc cvc cvccvcv cvc",
"cvc cvccvcvcv cvcv",
"cv cvccv cvc cvcvccv"
]
};
// The password character mapping
// c in template becomes bcdfghjklmnpqrstvwxyz
MPW.passchars = {
V: "AEIOU",
C: "BCDFGHJKLMNPQRSTVWXYZ",
v: "aeiou",
c: "bcdfghjklmnpqrstvwxyz",
A: "AEIOUBCDFGHJKLMNPQRSTVWXYZ",
a: "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz",
n: "0123456789",
o: "@&%?,=[]_:-+*$#!'^~;()/.",
x: "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz0123456789!@#$%^&*()",
" ": " "
};