add streaming
This commit is contained in:
parent
34cb970f11
commit
1bd7e4d486
16 changed files with 438 additions and 187 deletions
125
app/api.js
125
app/api.js
|
@ -91,47 +91,98 @@ export async function setPassword(id, owner_token, keychain) {
|
|||
return response.ok;
|
||||
}
|
||||
|
||||
export function uploadFile(
|
||||
encrypted,
|
||||
function asyncInitWebSocket(server) {
|
||||
return new Promise(resolve => {
|
||||
const ws = new WebSocket(server);
|
||||
ws.onopen = () => {
|
||||
resolve(ws);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function upload(
|
||||
ws,
|
||||
stream,
|
||||
streamInfo,
|
||||
metadata,
|
||||
verifierB64,
|
||||
keychain,
|
||||
onprogress
|
||||
) {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const upload = {
|
||||
cancel: function() {
|
||||
xhr.abort();
|
||||
},
|
||||
result: new Promise(function(resolve, reject) {
|
||||
xhr.addEventListener('loadend', function() {
|
||||
const authHeader = xhr.getResponseHeader('WWW-Authenticate');
|
||||
if (authHeader) {
|
||||
keychain.nonce = parseNonce(authHeader);
|
||||
}
|
||||
if (xhr.status === 200) {
|
||||
const responseObj = JSON.parse(xhr.responseText);
|
||||
return resolve({
|
||||
url: responseObj.url,
|
||||
id: responseObj.id,
|
||||
ownerToken: responseObj.owner
|
||||
});
|
||||
}
|
||||
reject(new Error(xhr.status));
|
||||
});
|
||||
})
|
||||
const metadataHeader = arrayToB64(new Uint8Array(metadata));
|
||||
const fileMeta = {
|
||||
fileMetadata: metadataHeader,
|
||||
authorization: `send-v1 ${verifierB64}`
|
||||
};
|
||||
const blob = new Blob([encrypted], { type: 'application/octet-stream' });
|
||||
xhr.upload.addEventListener('progress', function(event) {
|
||||
if (event.lengthComputable) {
|
||||
onprogress([event.loaded, event.total]);
|
||||
|
||||
//send file header
|
||||
ws.send(JSON.stringify(fileMeta));
|
||||
|
||||
function listenForRes() {
|
||||
return new Promise((resolve, reject) => {
|
||||
ws.addEventListener('message', function(msg) {
|
||||
const response = JSON.parse(msg.data);
|
||||
resolve({
|
||||
url: response.url,
|
||||
id: response.id,
|
||||
ownerToken: response.owner
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const resPromise = listenForRes();
|
||||
|
||||
const reader = stream.getReader();
|
||||
let state = await reader.read();
|
||||
let size = 0;
|
||||
while (!state.done) {
|
||||
const buf = state.value;
|
||||
ws.send(buf);
|
||||
if (ws.readyState !== 1) {
|
||||
throw new Error(0); //should this be here
|
||||
}
|
||||
});
|
||||
xhr.open('post', '/api/upload', true);
|
||||
xhr.setRequestHeader('X-File-Metadata', arrayToB64(new Uint8Array(metadata)));
|
||||
xhr.setRequestHeader('Authorization', `send-v1 ${verifierB64}`);
|
||||
xhr.send(blob);
|
||||
return upload;
|
||||
|
||||
onprogress([Math.min(streamInfo.fileSize, size), streamInfo.fileSize]);
|
||||
size += streamInfo.recordSize;
|
||||
state = await reader.read();
|
||||
}
|
||||
|
||||
const res = await resPromise;
|
||||
|
||||
ws.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function uploadWs(
|
||||
encrypted,
|
||||
info,
|
||||
metadata,
|
||||
verifierB64,
|
||||
keychain,
|
||||
onprogress
|
||||
) {
|
||||
const host = window.location.hostname;
|
||||
const port = window.location.port;
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const ws = await asyncInitWebSocket(`${protocol}//${host}:${port}/api/ws`);
|
||||
|
||||
//console.log(`made connection to websocket: ws://${host}:${port}/api/ws`)
|
||||
|
||||
return {
|
||||
cancel: function() {
|
||||
ws.close(4000, 'upload cancelled');
|
||||
},
|
||||
result: upload(
|
||||
ws,
|
||||
encrypted,
|
||||
info,
|
||||
metadata,
|
||||
verifierB64,
|
||||
keychain,
|
||||
onprogress
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
function download(id, keychain, onprogress, canceller) {
|
||||
|
@ -151,11 +202,7 @@ function download(id, keychain, onprogress, canceller) {
|
|||
}
|
||||
|
||||
const blob = new Blob([xhr.response]);
|
||||
const fileReader = new FileReader();
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
fileReader.onload = function() {
|
||||
resolve(this.result);
|
||||
};
|
||||
resolve(blob);
|
||||
});
|
||||
xhr.addEventListener('progress', function(event) {
|
||||
if (event.lengthComputable && event.target.status === 200) {
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
const streams = require('web-streams-polyfill');
|
||||
|
||||
class BlobSlicer {
|
||||
constructor(blob, size, decrypt) {
|
||||
this.blob = blob;
|
||||
this.size = size;
|
||||
this.index = 0;
|
||||
this.decrypt = decrypt;
|
||||
}
|
||||
|
||||
pull(controller) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const bytesLeft = this.blob.size - this.index;
|
||||
if (bytesLeft <= 0) {
|
||||
controller.close();
|
||||
return resolve();
|
||||
}
|
||||
let size = 0;
|
||||
if (this.decrypt && this.index === 0) {
|
||||
size = Math.min(21, bytesLeft);
|
||||
} else {
|
||||
size = Math.min(this.size, bytesLeft);
|
||||
}
|
||||
const blob = this.blob.slice(this.index, this.index + size);
|
||||
const reader = new FileReader();
|
||||
reader.onload = function() {
|
||||
controller.enqueue(new Uint8Array(this.result));
|
||||
resolve();
|
||||
};
|
||||
reader.onerror = reject;
|
||||
reader.readAsArrayBuffer(blob);
|
||||
this.index += size;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default class BlobSliceStream extends streams.ReadableStream {
|
||||
constructor(blob, size, decrypt) {
|
||||
super(new BlobSlicer(blob, size, decrypt));
|
||||
}
|
||||
}
|
117
app/ece.js
117
app/ece.js
|
@ -1,10 +1,12 @@
|
|||
require('buffer');
|
||||
import { ReadableStream, TransformStream } from 'web-streams-polyfill';
|
||||
|
||||
const NONCE_LENGTH = 12;
|
||||
const TAG_LENGTH = 16;
|
||||
const KEY_LENGTH = 16;
|
||||
const MODE_ENCRYPT = 'encrypt';
|
||||
const MODE_DECRYPT = 'decrypt';
|
||||
const RS = 1024 * 1024;
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
|
@ -14,27 +16,15 @@ function generateSalt(len) {
|
|||
return randSalt.buffer;
|
||||
}
|
||||
|
||||
/*
|
||||
mode: string, either 'encrypt' or 'decrypt'
|
||||
ikm: Uint8Array containing key of KEY_LENGTH length
|
||||
rs: int containing record size, optional
|
||||
salt: ArrayBuffer containing salt of KEY_LENGTH length, optional
|
||||
The transform stream takes data as UInt8Arrays on the writable side, and outputs
|
||||
UInt8Arrays on the readable side.
|
||||
*/
|
||||
export default class ECETransformer {
|
||||
export class ECETransformer {
|
||||
constructor(mode, ikm, rs, salt) {
|
||||
this.mode = mode;
|
||||
this.prevChunk;
|
||||
this.params = {};
|
||||
this.seq = 0;
|
||||
this.firstchunk = true;
|
||||
this.rs = rs || 1024;
|
||||
this.rs = rs;
|
||||
this.ikm = ikm.buffer;
|
||||
this.params.salt = salt;
|
||||
if (!salt) {
|
||||
this.params.salt = generateSalt(KEY_LENGTH);
|
||||
}
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
async generateKey() {
|
||||
|
@ -49,7 +39,7 @@ export default class ECETransformer {
|
|||
return window.crypto.subtle.deriveKey(
|
||||
{
|
||||
name: 'HKDF',
|
||||
salt: this.params.salt,
|
||||
salt: this.salt,
|
||||
info: encoder.encode('Content-Encoding: aes128gcm\0'),
|
||||
hash: 'SHA-256'
|
||||
},
|
||||
|
@ -77,7 +67,7 @@ export default class ECETransformer {
|
|||
await window.crypto.subtle.deriveKey(
|
||||
{
|
||||
name: 'HKDF',
|
||||
salt: this.params.salt,
|
||||
salt: this.salt,
|
||||
info: encoder.encode('Content-Encoding: nonce\0'),
|
||||
hash: 'SHA-256'
|
||||
},
|
||||
|
@ -95,15 +85,14 @@ export default class ECETransformer {
|
|||
}
|
||||
|
||||
generateNonce(seq) {
|
||||
const nonce = Buffer.from(this.params.nonceBase);
|
||||
if (seq > 0xffffffff) {
|
||||
throw new Error('record sequence number exceeds limit');
|
||||
}
|
||||
const nonce = Buffer.from(this.nonceBase);
|
||||
const m = nonce.readUIntBE(nonce.length - 4, 4);
|
||||
const xor = (m ^ seq) >>> 0; //forces unsigned int xor
|
||||
nonce.writeUIntBE(xor, nonce.length - 4, 4);
|
||||
|
||||
const m2 = nonce.readUIntBE(nonce.length - 8, 4);
|
||||
const xor2 = (m2 ^ (seq >>> 4)) >>> 0;
|
||||
nonce.writeUIntBE(xor2, nonce.length - 8, 4);
|
||||
|
||||
return nonce;
|
||||
}
|
||||
|
||||
|
@ -147,7 +136,7 @@ export default class ECETransformer {
|
|||
const nums = Buffer.alloc(5);
|
||||
nums.writeUIntBE(this.rs, 0, 4);
|
||||
nums.writeUIntBE(0, 4, 1);
|
||||
return Buffer.concat([Buffer.from(this.params.salt), nums]);
|
||||
return Buffer.concat([Buffer.from(this.salt), nums]);
|
||||
}
|
||||
|
||||
//salt is arraybuffer, rs is int, length is int
|
||||
|
@ -167,7 +156,7 @@ export default class ECETransformer {
|
|||
const nonce = this.generateNonce(seq);
|
||||
const encrypted = await window.crypto.subtle.encrypt(
|
||||
{ name: 'AES-GCM', iv: nonce },
|
||||
this.params.key,
|
||||
this.key,
|
||||
this.pad(buffer, isLast)
|
||||
);
|
||||
return Buffer.from(encrypted);
|
||||
|
@ -181,7 +170,7 @@ export default class ECETransformer {
|
|||
iv: nonce,
|
||||
tagLength: 128
|
||||
},
|
||||
this.params.key,
|
||||
this.key,
|
||||
buffer
|
||||
);
|
||||
|
||||
|
@ -190,8 +179,8 @@ export default class ECETransformer {
|
|||
|
||||
async start(controller) {
|
||||
if (this.mode === MODE_ENCRYPT) {
|
||||
this.params.key = await this.generateKey();
|
||||
this.params.nonceBase = await this.generateNonceBase();
|
||||
this.key = await this.generateKey();
|
||||
this.nonceBase = await this.generateNonceBase();
|
||||
controller.enqueue(this.createHeader());
|
||||
} else if (this.mode !== MODE_DECRYPT) {
|
||||
throw new Error('mode must be either encrypt or decrypt');
|
||||
|
@ -208,10 +197,10 @@ export default class ECETransformer {
|
|||
if (this.seq === 0) {
|
||||
//the first chunk during decryption contains only the header
|
||||
const header = this.readHeader(this.prevChunk);
|
||||
this.params.salt = header.salt;
|
||||
this.salt = header.salt;
|
||||
this.rs = header.rs;
|
||||
this.params.key = await this.generateKey();
|
||||
this.params.nonceBase = await this.generateNonceBase();
|
||||
this.key = await this.generateKey();
|
||||
this.nonceBase = await this.generateNonceBase();
|
||||
} else {
|
||||
controller.enqueue(
|
||||
await this.decryptRecord(this.prevChunk, this.seq - 1, isLast)
|
||||
|
@ -235,3 +224,71 @@ export default class ECETransformer {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BlobSlicer {
|
||||
constructor(blob, rs, mode) {
|
||||
this.blob = blob;
|
||||
this.index = 0;
|
||||
this.mode = mode;
|
||||
this.chunkSize = mode === MODE_ENCRYPT ? rs - 17 : rs;
|
||||
}
|
||||
|
||||
pull(controller) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const bytesLeft = this.blob.size - this.index;
|
||||
if (bytesLeft <= 0) {
|
||||
controller.close();
|
||||
return resolve();
|
||||
}
|
||||
let size = 1;
|
||||
if (this.mode === MODE_DECRYPT && this.index === 0) {
|
||||
size = Math.min(21, bytesLeft);
|
||||
} else {
|
||||
size = Math.min(this.chunkSize, bytesLeft);
|
||||
}
|
||||
const blob = this.blob.slice(this.index, this.index + size);
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
controller.enqueue(new Uint8Array(reader.result));
|
||||
resolve();
|
||||
};
|
||||
reader.onerror = reject;
|
||||
reader.readAsArrayBuffer(blob);
|
||||
this.index += size;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class BlobSliceStream extends ReadableStream {
|
||||
constructor(blob, size, mode) {
|
||||
super(new BlobSlicer(blob, size, mode));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
input: a blob containing data to be transformed
|
||||
key: Uint8Array containing key of size KEY_LENGTH
|
||||
mode: string, either 'encrypt' or 'decrypt'
|
||||
rs: int containing record size, optional
|
||||
salt: ArrayBuffer containing salt of KEY_LENGTH length, optional
|
||||
*/
|
||||
|
||||
export default class ECE {
|
||||
constructor(input, key, mode, rs, salt) {
|
||||
if (rs === undefined) {
|
||||
rs = RS;
|
||||
}
|
||||
if (salt === undefined) {
|
||||
salt = generateSalt(KEY_LENGTH);
|
||||
}
|
||||
|
||||
this.streamInfo = {
|
||||
recordSize: rs,
|
||||
fileSize: input.size + 16 * Math.floor(input.size / (rs - 17))
|
||||
};
|
||||
input = new BlobSliceStream(input, rs, mode);
|
||||
|
||||
const ts = new TransformStream(new ECETransformer(mode, key, rs, salt));
|
||||
this.stream = input.pipeThrough(ts);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,28 @@ export default class FileReceiver extends Nanobus {
|
|||
this.state = 'ready';
|
||||
}
|
||||
|
||||
async streamToArrayBuffer(stream) {
|
||||
const reader = stream.getReader();
|
||||
const chunks = [];
|
||||
let length = 0;
|
||||
|
||||
let state = await reader.read();
|
||||
while (!state.done) {
|
||||
chunks.push(state.value);
|
||||
length += state.value.length;
|
||||
state = await reader.read();
|
||||
}
|
||||
|
||||
const result = new Int8Array(length);
|
||||
let offset = 0;
|
||||
for (let i = 0; i < chunks.length; i++) {
|
||||
result.set(chunks[i], offset);
|
||||
offset += chunks[i].length;
|
||||
}
|
||||
|
||||
return result.buffer;
|
||||
}
|
||||
|
||||
async download(noSave = false) {
|
||||
this.state = 'downloading';
|
||||
this.downloadRequest = await downloadFile(
|
||||
|
@ -61,13 +83,19 @@ export default class FileReceiver extends Nanobus {
|
|||
this.emit('progress');
|
||||
}
|
||||
);
|
||||
|
||||
try {
|
||||
const ciphertext = await this.downloadRequest.result;
|
||||
this.downloadRequest = null;
|
||||
this.msg = 'decryptingFile';
|
||||
this.state = 'decrypting';
|
||||
this.emit('decrypting');
|
||||
const plaintext = await this.keychain.decryptFile(ciphertext);
|
||||
|
||||
const dec = await this.keychain.decryptStream(ciphertext);
|
||||
const plainstream = dec.stream;
|
||||
|
||||
const plaintext = await this.streamToArrayBuffer(plainstream);
|
||||
|
||||
if (!noSave) {
|
||||
await saveFile({
|
||||
plaintext,
|
||||
|
|
|
@ -3,7 +3,7 @@ import Nanobus from 'nanobus';
|
|||
import OwnedFile from './ownedFile';
|
||||
import Keychain from './keychain';
|
||||
import { arrayToB64, bytes } from './utils';
|
||||
import { uploadFile } from './api';
|
||||
import { uploadWs } from './api';
|
||||
|
||||
export default class FileSender extends Nanobus {
|
||||
constructor(file) {
|
||||
|
@ -59,20 +59,19 @@ export default class FileSender extends Nanobus {
|
|||
|
||||
async upload() {
|
||||
const start = Date.now();
|
||||
const plaintext = await this.readFile();
|
||||
if (this.cancelled) {
|
||||
throw new Error(0);
|
||||
}
|
||||
this.msg = 'encryptingFile';
|
||||
this.emit('encrypting');
|
||||
const encrypted = await this.keychain.encryptFile(plaintext);
|
||||
|
||||
const enc = await this.keychain.encryptStream(this.file);
|
||||
const metadata = await this.keychain.encryptMetadata(this.file);
|
||||
const authKeyB64 = await this.keychain.authKeyB64();
|
||||
if (this.cancelled) {
|
||||
throw new Error(0);
|
||||
}
|
||||
this.uploadRequest = uploadFile(
|
||||
encrypted,
|
||||
|
||||
this.uploadRequest = await uploadWs(
|
||||
enc.stream,
|
||||
enc.streamInfo,
|
||||
metadata,
|
||||
authKeyB64,
|
||||
this.keychain,
|
||||
|
@ -81,6 +80,11 @@ export default class FileSender extends Nanobus {
|
|||
this.emit('progress');
|
||||
}
|
||||
);
|
||||
|
||||
if (this.cancelled) {
|
||||
throw new Error(0);
|
||||
}
|
||||
|
||||
this.msg = 'fileSizeProgress';
|
||||
this.emit('progress'); // HACK to kick MS Edge
|
||||
try {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { arrayToB64, b64ToArray } from './utils';
|
||||
|
||||
import ECE from './ece.js';
|
||||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
|
@ -179,6 +179,16 @@ export default class Keychain {
|
|||
return ciphertext;
|
||||
}
|
||||
|
||||
async encryptStream(plaintext) {
|
||||
const enc = new ECE(plaintext, this.rawSecret, 'encrypt');
|
||||
return enc;
|
||||
}
|
||||
|
||||
async decryptStream(encstream) {
|
||||
const dec = new ECE(encstream, this.rawSecret, 'decrypt');
|
||||
return dec;
|
||||
}
|
||||
|
||||
async decryptFile(ciphertext) {
|
||||
const encryptKey = await this.encryptKeyPromise;
|
||||
const plaintext = await window.crypto.subtle.decrypt(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue