-
Notifications
You must be signed in to change notification settings - Fork 1
/
cryptography-service.js
283 lines (224 loc) · 6.86 KB
/
cryptography-service.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
export let serviceName = 'cryptography';
export default /*@ngInject*/ function cryptography(
$q,
$window,
forge
) {
//TODO: add user setting to disable encryption
let cachedCredentials;
const AES_ENCRYPTION_MODE = 'AES-GCM';
const AES_KEY_STRENGTH = 128;
// From Forge docs:
// Note: a key size of 16 bytes will use AES-128, 24 => AES-192, 32 => AES-256
const PBKDF2_KEY_LENGTH = AES_KEY_STRENGTH / 8;
const PBKDF2_SALT_LENGTH = PBKDF2_KEY_LENGTH;
const AES_IV_LENGTH = PBKDF2_KEY_LENGTH;
// TODO: research what the up-to-date recommendation for PBKDF2 iterations is
// Could probably afford to use more if needed
const PBKDF2_ITERATIONS = 10000;
const HMAC_DIGEST_ALGORITHM = 'sha256';
const ENCRYPTED_OBJECT = {
name: 'tocEncryptedObject',
//TODO: use JSON Schema URI for versioning
schema: {
type: 'object',
properties: {
ct: {
type: 'string'
},
tag: {
type: 'string'
},
iv: {
type: 'string'
}
},
required: ['ct', 'tag', 'iv']
}
};
const UNENCRYPTED_OBJECT = {
name: 'tocUnencryptedObject',
schema: {
type: 'object',
properties: {
pt: {
type: 'string'
}
},
required: ['pt']
}
};
// replaces forwardslash in base64 string for use in paths for indexeddb
let escapeBase64 = function escapeBase64(base64) {
return base64.replace(/\//g, '.');
};
let unescapeBase64 = function unescapeBase64(base64) {
return base64.replace(/\./g, '/');
};
let getRandomBytes = function getRandom(size) {
let deferredRandomBytes = $q.defer();
forge.random.getBytes(size, (error, bytes) => {
if (error) {
return deferredRandomBytes.reject(error);
}
deferredRandomBytes.resolve(bytes);
});
return deferredRandomBytes.promise;
};
let getRandomBase64 = function getRandomBase64(size) {
return getRandomBytes(size)
.then((randomBytes) => forge.util.encode64(randomBytes));
};
let encodeBase64Sync = function encodeBase64(bytes) {
let base64 = forge.util.encode64(bytes);
return base64;
};
let decodeBase64Sync = function decodeBase64(base64) {
let bytes = forge.util.decode64(base64);
return bytes;
};
let encodeBase64 = function encodeBase64(bytes) {
let base64 = encodeBase64Sync(bytes);
return $q.when(base64);
};
let decodeBase64 = function decodeBase64(base64) {
let bytes = decodeBase64Sync(base64);
return $q.when(bytes);
};
let checkCredentials =
function checkCredentials(credentials) {
if (credentials && credentials.key) {
return;
}
throw new Error('cryptography: mising credentials');
};
let getHmac = function getHmac(object, credentials = cachedCredentials) {
checkCredentials(credentials);
let plaintext = JSON.stringify(object);
let hmac = forge.hmac.create();
hmac.start(HMAC_DIGEST_ALGORITHM, credentials.key);
hmac.update(plaintext);
return hmac.digest().getBytes();
};
let getMd5 = function getMd5(plaintext) {
let md5 = forge.md.md5.create();
md5.update(plaintext);
return md5.digest().toHex();
};
let getSha256 = function getSha256(plaintext) {
let sha256 = forge.md.sha256.create();
sha256.update(plaintext);
return sha256.digest().toHex();
};
let encryptBase =
function encryptBase(object, ivBytes, credentials = cachedCredentials) {
checkCredentials(credentials);
let plaintext = JSON.stringify(object);
let cipher = forge.cipher.createCipher(
AES_ENCRYPTION_MODE,
credentials.key
);
cipher.start({iv: ivBytes});
cipher.update(forge.util.createBuffer(plaintext));
cipher.finish();
let iv = forge.util.encode64(ivBytes);
let ct = forge.util.encode64(cipher.output.getBytes());
let tag = forge.util.encode64(cipher.mode.tag.getBytes());
return { iv, ct, tag };
};
// Hand-rolled deterministic encryption scheme.
// TODO: replace with something standard like SIV-AES
let encryptDeterministic =
function encryptDeterministic(object, credentials = cachedCredentials) {
let ivBytes = getHmac(object);
return encryptBase(object, ivBytes, credentials);
};
let encrypt = function encrypt(object, credentials = cachedCredentials) {
let ivBytes = forge.random.getBytesSync(AES_IV_LENGTH);
return encryptBase(object, ivBytes, credentials);
};
let decrypt =
function decrypt(encryptedObject, credentials = cachedCredentials) {
checkCredentials(credentials);
let ciphertext = forge.util.decode64(encryptedObject.ct);
let iv = forge.util.decode64(encryptedObject.iv);
let tag = forge.util.decode64(encryptedObject.tag);
let decipher = forge.cipher.createDecipher(
AES_ENCRYPTION_MODE,
credentials.key
);
decipher.start({
iv: forge.util.createBuffer(iv),
tag: forge.util.createBuffer(tag)
});
decipher.update(forge.util.createBuffer(ciphertext));
let result = decipher.finish();
if (!result) {
throw new Error('cryptography: decryption failed');
}
return JSON.parse(decipher.output.getBytes());
};
let createSalt = function createSalt() {
return getRandomBase64(PBKDF2_SALT_LENGTH);
};
let createChallenge = function createChallenge(salt) {
return $q.when(encrypt(salt));
};
let derive = function derive(rawCredentials) {
let salt = forge.util.decode64(rawCredentials.salt);
let key = forge.pkcs5.pbkdf2(
rawCredentials.password,
salt,
PBKDF2_ITERATIONS,
PBKDF2_KEY_LENGTH
);
return $q.when({ key });
};
let isInitialized = function isInitialized() {
return cachedCredentials !== undefined;
};
let cache = function cache(derivedCredentials) {
checkCredentials(derivedCredentials);
cachedCredentials = derivedCredentials;
return $q.when(derivedCredentials);
};
let initialize = function initializeCryptography(credentials) {
let cachingCredentials = credentials.key ?
cache({ key: decodeBase64Sync(credentials.key) }) :
derive(credentials)
.then(cache);
return cachingCredentials
.then((derivedCredentials) => {
return {
key: encodeBase64Sync(derivedCredentials.key)
}
});
};
let destroy = function destroyCryptography() {
cachedCredentials = undefined;
return $q.when();
};
let cryptographyService = {
ENCRYPTED_OBJECT,
UNENCRYPTED_OBJECT,
escapeBase64,
unescapeBase64,
encodeBase64,
decodeBase64,
getRandomBase64,
getHmac,
getMd5,
getSha256,
encryptDeterministic,
encrypt,
decrypt,
createSalt,
createChallenge,
derive,
isInitialized,
initialize,
destroy
};
$window.tocCrypto = cryptographyService;
return cryptographyService;
}