Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Encryption.js « Classes « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1454e6aebb40ab924ea08743109f8924a98190f2 (plain)
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
import sodium from 'libsodium-wrappers';

export default class Encryption {

    constructor() {
        this.fields = {
            password: ['url', 'label', 'notes', 'password', 'username', 'customFields'],
            folder  : ['label'],
            tag     : ['label', 'color']
        };
        this._enabled = false;
        this._keys = {};
        this._current = '';
        this._legacyEncoding = false;
        this.ready();
    }

    /**
     *
     * @returns {boolean}
     */
    get enabled() {
        return this._enabled;
    }

    /**
     *
     * @returns {boolean}
     */
    get keys() {
        return Object.keys(this._keys);
    }

    // noinspection JSMethodCanBeStatic
    /**
     *
     * @returns {Promise<boolean>}
     */
    async ready() {
        await sodium.ready;
    }

    /**
     * Returns true if the user has base64 encoded properties
     *
     * @return {boolean}
     */
    hasLegacyEncoding() {
        return this._legacyEncoding;
    }

    /**
     * Encrypts an object
     *
     * @param object
     * @param type
     * @returns {{_encrypted}|*}
     */
    encryptObject(object, type) {
        if(!this._enabled) throw new Error('Encryption not available');
        if(!this.fields.hasOwnProperty(type)) throw new Error('Invalid object type');
        if(object.hasOwnProperty('_encrypted') && object._encrypted) return object;

        let fields = this.fields[type],
            key    = this._getKey(this._current);

        for(let i = 0; i < fields.length; i++) {
            let field = fields[i],
                data  = object[field];

            if(data === null || data.length === 0) continue;
            object[field] = this.encryptString(data, key);
        }

        object.cseType = 'CSEv1r1';
        object.cseKey = this._current;
        object._encrypted = true;

        return object;
    }

    /**
     * Decrypts an object
     *
     * @param object
     * @param type
     * @returns {{_encrypted}|*}
     */
    decryptObject(object, type) {
        if(!this._enabled) throw new Error('Encryption not available');
        if(!this.fields.hasOwnProperty(type)) throw new Error('Invalid object type');
        if(object.cseType !== 'CSEv1r1') throw new Error('Unsupported encryption type');
        if(object.hasOwnProperty('_encrypted') && !object._encrypted) return object;

        let fields = this.fields[type],
            key    = this._getKey(object.cseKey);

        for(let i = 0; i < fields.length; i++) {
            let field = fields[i],
                data  = object[field];

            if(data === null || data.length === 0) continue;
            object[field] = this.decryptString(data, key);
        }

        object._encrypted = false;

        return object;
    }

    /**
     * Encrypts the message with the user defined password
     *
     * @param message
     * @param password
     * @returns {*}
     */
    encryptWithPassword(message, password) {
        let salt      = this._generateRandom(sodium.crypto_pwhash_SALTBYTES),
            key       = this._passwordToKey(password, salt),
            encrypted = this.encrypt(message, key);

        return sodium.to_hex(new Uint8Array([...salt, ...encrypted]));
    }

    /**
     * Decrypts the message with the user defined password
     *
     * @param message
     * @param password
     * @returns {*}
     */
    decryptWithPassword(message, password) {
        let encrypted = sodium.from_hex(message),
            salt      = encrypted.slice(0, sodium.crypto_pwhash_SALTBYTES),
            text      = encrypted.slice(sodium.crypto_pwhash_SALTBYTES),
            key       = this._passwordToKey(password, salt);

        return sodium.to_string(this.decrypt(text, key));
    }

    /**
     * Encrypt the message with the given key and return a hex encoded string
     *
     * @param message
     * @param key
     * @returns {string}
     */
    encryptString(message, key) {
        return sodium.to_hex(this.encrypt(message, key));
    }

    /**
     * Encrypt the message with the given key
     *
     * @param message
     * @param key
     * @returns {Uint8Array}
     */
    encrypt(message, key) {
        let nonce = this._generateRandom(sodium.crypto_secretbox_NONCEBYTES);

        return new Uint8Array([...nonce, ...sodium.crypto_secretbox_easy(message, nonce, key)]);
    }

    /**
     * Decrypt the hex or base64 encoded message with the given key
     *
     * @param encodedString
     * @param key
     * @returns {string}
     */
    decryptString(encodedString, key) {
        try {
            let encryptedString = sodium.from_hex(encodedString);
            return sodium.to_string(this.decrypt(encryptedString, key));
        } catch(e) {
            let encryptedString = sodium.from_base64(encodedString);
            let result = sodium.to_string(this.decrypt(encryptedString, key));
            this._legacyEncoding = true;
            return result;
        }
    }

    // noinspection JSMethodCanBeStatic
    /**
     * Decrypt the message with the given key
     *
     * @param encrypted
     * @param key
     * @returns {Uint8Array}
     */
    decrypt(encrypted, key) {
        if(encrypted.length < sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES) throw new Error('Invalid encrypted text length');

        let nonce      = encrypted.slice(0, sodium.crypto_secretbox_NONCEBYTES),
            ciphertext = encrypted.slice(sodium.crypto_secretbox_NONCEBYTES);

        return sodium.crypto_secretbox_open_easy(ciphertext, nonce, key);
    }

    // noinspection JSMethodCanBeStatic
    /**
     * Generate a challenge solution with the user provided password
     * and the server provided salts
     *
     * @param salts
     * @param password
     * @returns {string}
     */
    solveChallenge(password, salts) {
        if(password.length < 12) throw new Error('Password is too short');
        if(password.length > 128) throw new Error('Password is too long');

        let passwordSalt     = sodium.from_hex(salts[0]),
            genericHashKey   = sodium.from_hex(salts[1]),
            passwordHashSalt = sodium.from_hex(salts[2]),
            genericHash      = sodium.crypto_generichash(
                sodium.crypto_generichash_BYTES_MAX,
                new Uint8Array([...sodium.from_string(password), ...passwordSalt]),
                genericHashKey
            );

        let passwordHash = sodium.crypto_pwhash(
            sodium.crypto_box_SEEDBYTES,
            genericHash,
            passwordHashSalt,
            sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
            sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
            sodium.crypto_pwhash_ALG_DEFAULT
        );


        return sodium.to_hex(passwordHash);
    }

    /**
     * Create the salts and the secret for the server
     * using the user provided password
     *
     * @param password
     * @returns {{salts: *[], secret: *}}
     */
    createChallenge(password) {
        if(password.length < 12) throw new Error('Password is too short');
        if(password.length > 128) throw new Error('Password is too long');

        let passwordSalt   = this._generateRandom(256),
            genericHashKey = this._generateRandom(sodium.crypto_generichash_KEYBYTES_MAX),
            genericHash    = sodium.crypto_generichash(
                sodium.crypto_generichash_BYTES_MAX,
                new Uint8Array([...sodium.from_string(password), ...passwordSalt]),
                genericHashKey
            );

        let passwordHashSalt = this._generateRandom(sodium.crypto_pwhash_SALTBYTES),
            passwordHash     = sodium.crypto_pwhash(
                sodium.crypto_box_SEEDBYTES,
                genericHash,
                passwordHashSalt,
                sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
                sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
                sodium.crypto_pwhash_ALG_DEFAULT
            );

        return {
            salts : [
                sodium.to_hex(passwordSalt),
                sodium.to_hex(genericHashKey),
                sodium.to_hex(passwordHashSalt)
            ],
            secret: sodium.to_hex(passwordHash)
        };
    }

    /**
     * Decrypt and activate the keychain
     *
     * @param keychainText
     * @param password
     */
    setKeychain(keychainText, password) {
        let encrypted;
        try {
            encrypted = sodium.from_hex(keychainText);
        } catch(e) {
            encrypted = sodium.from_base64(keychainText);
            this._legacyEncoding = true;
        }

        let salt     = encrypted.slice(0, sodium.crypto_pwhash_SALTBYTES),
            text     = encrypted.slice(sodium.crypto_pwhash_SALTBYTES),
            key      = this._passwordToKey(password, salt),
            keychain = JSON.parse(sodium.to_string(this.decrypt(text, key)));

        this._current = keychain.current;
        for(let id in keychain.keys) {
            if(keychain.keys.hasOwnProperty(id)) {
                this._keys[id] = sodium.from_hex(keychain.keys[id]);
            }
        }

        this._enabled = true;
    }

    /**
     * Remove the current keychain
     */
    unsetKeychain() {
        this._enabled = false;
        this._keys = {};
        this._current = '';
    }

    /**
     * Add a new key to the keychain and return the full keychain
     *
     * @param password
     * @param addKey
     * @returns {*}
     */
    getKeychain(password, addKey = false) {
        if(this._enabled === false) {
            this._keys = {};
        }

        if(addKey || this._current.length === 0) {
            this._current = this.getUuid();
            this._keys[this._current] = this._generateRandom(sodium.crypto_secretbox_KEYBYTES);
            this._enabled = true;
        }

        let keychain = {
            keys   : {},
            current: this._current
        };

        for(let id in this._keys) {
            if(this._keys.hasOwnProperty(id)) {
                keychain.keys[id] = sodium.to_hex(this._keys[id]);
            }
        }

        let salt      = this._generateRandom(sodium.crypto_pwhash_SALTBYTES),
            key       = this._passwordToKey(password, salt),
            encrypted = this.encrypt(JSON.stringify(keychain), key);

        return sodium.to_hex(new Uint8Array([...salt, ...encrypted]));
    }

    /**
     * Create a uuidv4
     *
     * @returns {string}
     */
    getUuid() {
        return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
    }

    /**
     *
     * @param uuid
     * @returns {Uint8Array}
     * @private
     */
    _getKey(uuid) {
        if(this._keys.hasOwnProperty(uuid)) {
            return this._keys[uuid];
        }

        throw new Error('Unknown CSE key id');
    }

    // noinspection JSMethodCanBeStatic
    /**
     *
     * @param password
     * @param salt
     * @returns {Uint8Array}
     * @private
     */
    _passwordToKey(password, salt) {
        return sodium.crypto_pwhash(
            sodium.crypto_box_SEEDBYTES,
            password,
            salt,
            sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
            sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
            sodium.crypto_pwhash_ALG_DEFAULT
        );
    }

    // noinspection JSMethodCanBeStatic
    /**
     *
     * @param length
     * @returns {Uint8Array}
     * @private
     */
    _generateRandom(length) {
        let array = new Uint8Array(length);
        window.crypto.getRandomValues(array);

        return array;
    }

    // noinspection JSMethodCanBeStatic
    /**
     * Generate a hash of the given text with the given algorithm
     *
     * @param value
     * @param algorithm
     * @returns {Promise<string>}
     */
    async getHash(value, algorithm = 'SHA-1') {
        if(['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'].indexOf(algorithm) !== -1) {
            let msgBuffer  = new TextEncoder('utf-8').encode(value),
                hashBuffer = await
                    crypto.subtle.digest(algorithm, msgBuffer);
            return sodium.to_hex(new Uint8Array(hashBuffer));
        } else if(algorithm.substr(0, 7) === 'BLAKE2b') {
            let bytes = sodium.crypto_generichash_BYTES_MAX;
            if(algorithm.indexOf('-') !== -1) {
                bytes = algorithm.split('-')[1];
                if(sodium.crypto_generichash_BYTES_MAX < bytes) bytes = sodium.crypto_generichash_BYTES_MAX;
                if(sodium.crypto_generichash_BYTES_MIN > bytes) bytes = sodium.crypto_generichash_BYTES_MIN;
            }

            return sodium.to_hex(sodium.crypto_generichash(bytes, sodium.from_string(value)));
        } else if(algorithm === 'Argon2') {
            return sodium.crypto_pwhash_str(value, sodium.crypto_pwhash_OPSLIMIT_MIN, sodium.crypto_pwhash_MEMLIMIT_MIN);
        }
    }
}