-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
181 lines (172 loc) · 6.81 KB
/
index.ts
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
import {ReadableStreamSizedReader} from "readable-stream-sized-reader";
import {mergeUint8Arrays} from "binconv/dist/src/mergeUint8Arrays";
const ivBitSize = 128;
const blockByteSize = 16;
export async function deriveKeyAndIvByPbkdf2(params: { salt: Uint8Array, password: string, keyBits: 128 | 256, iterations: number, hash: HashAlgorithmIdentifier }): Promise<{ key: Uint8Array, iv: Uint8Array }> {
const keyMaterial = await window.crypto.subtle.importKey(
"raw",
new TextEncoder().encode(params.password),
"PBKDF2",
false,
["deriveBits", "deriveKey"]
);
const keyIvBits = await window.crypto.subtle.deriveBits(
{
name: "PBKDF2",
salt: params.salt,
iterations: params.iterations,
hash: params.hash
},
keyMaterial,
params.keyBits + ivBitSize,
);
return {
key: new Uint8Array(keyIvBits.slice(0, params.keyBits / 8)),
iv: new Uint8Array(keyIvBits.slice(params.keyBits / 8)),
}
}
function incrementCounter(counter: Uint8Array) {
for (let i = counter.length - 1; i >= 0; i--) {
counter[i]++;
if(counter[i] !== 0) break;
}
}
export function aesCtrEncrypt(readableStream: ReadableStream<Uint8Array>, params: { salt: Uint8Array, key: Uint8Array, iv: Uint8Array } ): ReadableStream<Uint8Array> {
const reader = new ReadableStreamSizedReader(readableStream.getReader());
// Rest of read size for block size
let restByteSize = blockByteSize;
// Chunk which contains previous chunk
const block: Uint8Array = new Uint8Array(blockByteSize);
// Offset which current chunk should set on
let blockOffset = 0;
// Copy iv as counter because counter is updated destructively
const counter = params.iv.slice();
const cryptoKeyPromise = crypto.subtle.importKey("raw", params.key, { name: "AES-CTR" }, false, ["encrypt", "decrypt"]);
return new ReadableStream({
async pull(ctrl) {
const result = await reader.read(restByteSize, false);
if(result.done) {
ctrl.close();
return;
}
restByteSize -= result.value.byteLength;
block.set(result.value, blockOffset);
const cryptoKey = await cryptoKeyPromise;
const blockEncrypted = await crypto.subtle.encrypt({ name: "AES-CTR", counter: counter, length: ivBitSize }, cryptoKey, block);
const encrypted = blockEncrypted.slice(blockOffset, blockOffset + result.value.byteLength);
blockOffset += result.value.byteLength;
ctrl.enqueue(new Uint8Array(encrypted));
// If one block encrypted
if (restByteSize === 0) {
// Increment counter destructively
incrementCounter(counter);
restByteSize = blockByteSize;
blockOffset = 0;
}
}
});
}
export function aesCtrEncryptWithPbkdf2(readableStream: ReadableStream<Uint8Array>, pbkdf2Options: { password: string, keyBits: 128 | 256, iterations: number, hash: HashAlgorithmIdentifier } ): ReadableStream<Uint8Array> {
const salt = crypto.getRandomValues(new Uint8Array(8));
let reader: ReadableStreamDefaultReader<Uint8Array>;
return new ReadableStream({
async start(ctrl) {
ctrl.enqueue(mergeUint8Arrays([
new TextEncoder().encode('Salted__'),
salt
]));
const {key, iv} = await deriveKeyAndIvByPbkdf2({
salt,
...pbkdf2Options
});
reader = aesCtrEncrypt(readableStream, { salt, key, iv }).getReader();
},
async pull(ctrl) {
const result = await reader.read();
if (result.done) {
ctrl.close();
return;
}
ctrl.enqueue(result.value);
}
});
}
function throwIfUnexpectedLength(result: ReadableStreamDefaultReadResult<Uint8Array>, expectedByteLength: number): asserts result is ReadableStreamDefaultReadValueResult<Uint8Array> {
if (result.done) {
throw new Error("unexpected done");
}
if (result.value.byteLength !== expectedByteLength) {
throw new Error(`expected ${expectedByteLength}, but ${result.value.byteLength}`);
}
}
export function aesCtrDecrypt(readableStream: ReadableStream<Uint8Array>, params: { salt: Uint8Array, key: Uint8Array, iv: Uint8Array } ): ReadableStream<Uint8Array> {
const reader = new ReadableStreamSizedReader(readableStream.getReader());
const cryptoKeyPromise = crypto.subtle.importKey("raw", params.key, { name: "AES-CTR" }, false, ["encrypt", "decrypt"]);
// Copy iv as counter because counter is updated destructively
const counter: Uint8Array = params.iv.slice();
// Rest of read size for block size
let restByteSize = blockByteSize;
// Block which contains previous chunk
const block: Uint8Array = new Uint8Array(blockByteSize);
// Offset which current chunk should set on
let blockOffset = 0;
return new ReadableStream({
async pull(ctrl) {
const result = await reader.read(restByteSize, false);
if (result.done) {
ctrl.close();
return;
}
restByteSize -= result.value.byteLength;
block.set(result.value, blockOffset);
const cryptoKey = await cryptoKeyPromise;
const blockDecrypted = await crypto.subtle.decrypt({ name: "AES-CTR", counter, length: ivBitSize }, cryptoKey, block);
const decrypted = blockDecrypted.slice(blockOffset, blockOffset + result.value.byteLength);
blockOffset += result.value.byteLength;
ctrl.enqueue(new Uint8Array(decrypted));
// If one block decrypted
if (restByteSize === 0) {
// Increment counter destructively
incrementCounter(counter);
restByteSize = blockByteSize;
blockOffset = 0;
}
}
});
}
export function aesCtrDecryptWithPbkdf2(encryptedReadableStream: ReadableStream<Uint8Array>, pbkdf2Options: { password: string, keyBits: 128 | 256, iterations: number, hash: HashAlgorithmIdentifier } ): ReadableStream<Uint8Array> {
const encryptedReaderWithSalt = new ReadableStreamSizedReader(encryptedReadableStream.getReader());
let decryptedReader: ReadableStreamDefaultReader<Uint8Array>;
return new ReadableStream({
async start() {
const Salted__ = await encryptedReaderWithSalt.read(8);
throwIfUnexpectedLength(Salted__, 8);
const saltResult = await encryptedReaderWithSalt.read(8);
throwIfUnexpectedLength(saltResult, 8);
const salt = saltResult.value;
const {key, iv} = await deriveKeyAndIvByPbkdf2({
salt,
...pbkdf2Options
});
const encryptedReadableStream = new ReadableStream<Uint8Array>({
async pull(ctrl) {
const result = await encryptedReaderWithSalt.read();
if (result.done) {
ctrl.close();
return;
}
ctrl.enqueue(result.value);
}
});
decryptedReader = aesCtrDecrypt(encryptedReadableStream, { salt, key, iv }).getReader();
},
async pull(ctrl) {
const result = await decryptedReader.read();
if (result.done) {
ctrl.close();
return;
}
ctrl.enqueue(result.value);
}
});
}