gcm encryption

This commit is contained in:
Abhinav Adduri 2017-06-20 13:03:04 -07:00 committed by Danny Coates
parent a7fcb1a44f
commit 50995238bd
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 30 additions and 32 deletions

View File

@ -54,23 +54,23 @@ class FileReceiver extends EventEmitter {
{ {
kty: 'oct', kty: 'oct',
k: location.hash.slice(1), k: location.hash.slice(1),
alg: 'A128CBC', alg: 'A256GCM',
ext: true ext: true
}, },
{ {
name: 'AES-CBC' name: 'AES-GCM'
}, },
true, true,
['encrypt', 'decrypt'] ['encrypt', 'decrypt']
) )
]) ]).then(([fdata, key]) => {
.then(([fdata, key]) => {
const salt = this.salt; const salt = this.salt;
return Promise.all([ return Promise.all([
window.crypto.subtle.decrypt( window.crypto.subtle.decrypt(
{ {
name: 'AES-CBC', name: 'AES-GCM',
iv: salt iv: salt,
tagLength: 128
}, },
key, key,
fdata.data fdata.data
@ -79,10 +79,6 @@ class FileReceiver extends EventEmitter {
resolve(fdata.fname); resolve(fdata.fname);
}) })
]); ]);
})
.catch(err => {
Raven.captureException(err);
return Promise.reject(err);
}); });
} }
} }

View File

@ -7,7 +7,7 @@ class FileSender extends EventEmitter {
constructor(file) { constructor(file) {
super(); super();
this.file = file; this.file = file;
this.iv = window.crypto.getRandomValues(new Uint8Array(16)); this.iv = window.crypto.getRandomValues(new Uint8Array(12));
} }
static delete(fileId, token) { static delete(fileId, token) {
@ -39,12 +39,13 @@ class FileSender extends EventEmitter {
return Promise.all([ return Promise.all([
window.crypto.subtle.generateKey( window.crypto.subtle.generateKey(
{ {
name: 'AES-CBC', name: 'AES-GCM',
length: 128 length: 256,
tagLength: 128
}, },
true, true,
['encrypt', 'decrypt'] ['encrypt', 'decrypt']
), ).catch(err => console.log('There was an error generating a crypto key')),
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsArrayBuffer(this.file); reader.readAsArrayBuffer(this.file);
@ -57,12 +58,13 @@ class FileSender extends EventEmitter {
return Promise.all([ return Promise.all([
window.crypto.subtle.encrypt( window.crypto.subtle.encrypt(
{ {
name: 'AES-CBC', name: 'AES-GCM',
iv: this.iv iv: this.iv,
tagLength: 128
}, },
secretKey, secretKey,
plaintext plaintext
), ).catch(err => console.log('Error with encrypting.')),
window.crypto.subtle.exportKey('jwk', secretKey) window.crypto.subtle.exportKey('jwk', secretKey)
]); ]);
}) })

View File

@ -12,7 +12,7 @@ function ivToStr(iv) {
} }
function strToIv(str) { function strToIv(str) {
const iv = new Uint8Array(16); const iv = new Uint8Array(12);
for (let i = 0; i < str.length; i += 2) { for (let i = 0; i < str.length; i += 2) {
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16); iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
} }

View File

@ -171,5 +171,5 @@ app.listen(conf.listen_port, () => {
}); });
const validateID = route_id => { const validateID = route_id => {
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null; return route_id.match(/^[0-9a-fA-F]{24}$/) !== null;
}; };