-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathV2.js
428 lines (346 loc) · 8.97 KB
/
V2.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
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
const sodium = require('libsodium-wrappers-sumo');
const utils = require('../utils')
const decapsulate = require('../decapsulate');
const PasetoError = require('../error/PasetoError');
const InvalidVersionError = require('../error/InvalidVersionError');
/***
* V2
*
* protocol version 2
*
* @constructor
* @api public
*/
module.exports = V2;
function V2() {
this._repr = 'v2';
this._constants = {
SYMMETRIC_KEY_BYTES: 32
}
}
/***
* private
*
* generate a private key for use with the protocol
*
* @function
* @api public
*
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.private = pk;
function pk(cb) {
const done = utils.ret(cb);
// minor hack to mimic php api without circular dependency - probably a better way to do this
const PrivateKey = require('../key/private');
const pk = new PrivateKey(new V2());
return pk.generate().then((err) => {
if (err) { return done(err); }
return done(null, pk);
});
}
/***
* symmetric
*
* generate a symmetric key for use with the protocol
*
* @function
* @api public
*
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.symmetric = sk;
function sk(cb) {
const done = utils.ret(cb);
// minor hack to mimic php api without circular dependency - probably a better way to do this
const SymmetricKey = require('../key/symmetric');
const sk = new SymmetricKey(new V2());
return sk.generate().then((err) => {
if (err) { return done(err); }
return done(null, sk);
});
}
/***
* repr
*
* get protocol representation
*
* @function
* @api public
*
* @returns {String}
*/
V2.prototype.repr = repr;
function repr() {
return this._repr;
}
/***
* sklength
*
* get symmetric key length
*
* @function
* @api public
*
* @returns {Number}
*/
V2.prototype.sklength = sklength;
function sklength() {
return this._constants.SYMMETRIC_KEY_BYTES;
}
/***
* __encrypt
*
* symmetric authenticated encryption
*
* @function
* @api private
*
* @param {String|Buffer} data
* @param {Object} key
* @param {String|Buffer} footer
* @param {String|Buffer} nonce
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.__encrypt = __encrypt;
function __encrypt(data, key, footer, nonce, cb) {
footer = footer || '';
const self = this;
const done = utils.ret(cb);
if (key.purpose() !== 'local') {
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
}
if (!(key.protocol() instanceof V2)) {
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
}
return sodium.ready.then(() => {
const header = utils.local(self);
[ data, footer, nonce ] = (utils.parse('utf-8'))(data, footer, nonce);
let token;
try {
token = aeadEncrypt(key, header, data, footer, nonce);
} catch (ex) {
return done(ex);
}
return done(null, token);
});
}
/***
* encrypt
*
* symmetric authenticated encryption (public api)
*
* @function
* @api public
*
* @param {String|Buffer} data
* @param {Object} key
* @param {String|Buffer} footer
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.encrypt = encrypt;
function encrypt(data, key, footer, cb) {
return this.__encrypt(data, key, footer, '', cb);
}
/***
* decrypt
*
* symmetric authenticated decryption
*
* @function
* @api public
*
* @param {String} token
* @param {Object} key
* @param {String|Buffer} footer
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.decrypt = decrypt;
function decrypt(token, key, footer, cb) {
const self = this;
const done = utils.ret(cb);
if (key.purpose() !== 'local') {
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
}
if (!(key.protocol() instanceof V2)) {
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
}
return sodium.ready.then(() => {
let payload, data, header = utils.local(self);
try {
[ header, payload, footer ] = decapsulate(header, token, footer);
data = aeadDecrypt(key, header, payload, footer);
} catch (ex) {
return done(ex);
}
return done(null, data);
});
}
/***
* sign
*
* asymmetric authentication
*
* @function
* @api public
*
* @param {String|Buffer} data
* @param {Object} key
* @param {String|Buffer} footer
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.sign = sign;
function sign(data, key, footer, cb) {
footer = footer || '';
const self = this;
const done = utils.ret(cb);
if (key.purpose() !== 'public') {
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
}
if (!(key.protocol() instanceof V2)) {
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
}
return sodium.ready.then(() => {
const header = utils.public(self);
[ data, footer ] = (utils.parse('utf-8'))(data, footer);
// sign
let payload;
try {
payload = utils.pae(header, data, footer);
} catch (ex) {
return done(ex);
}
const _signature = sodium.crypto_sign_detached(payload, key.raw());
const signature = Buffer.from(_signature);
// format
const token = header.toString('utf-8') + utils.toB64URLSafe(Buffer.concat([ data, signature ]));
return (!Buffer.byteLength(footer))
? done(null, token)
: done(null, token + '.' + utils.toB64URLSafe(footer));
});
}
/***
* verify
*
* asymmetric authentication
*
* @function
* @api public
*
* @param {String} token
* @param {Object} key
* @param {String|Buffer} footer
* @param {Function} cb
* @returns {Callback|Promise}
*/
V2.prototype.verify = verify;
function verify(token, key, footer, cb) {
const self = this;
const done = utils.ret(cb);
if (key.purpose() !== 'public') {
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
}
if (!(key.protocol() instanceof V2)) {
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
}
return sodium.ready.then(() => {
let payload, header = utils.public(self);
try {
[ header, payload, footer ] = decapsulate(header, token, footer);
} catch (ex) {
return done(ex);
}
// recover data
const plen = Buffer.byteLength(payload);
const data = Buffer.from(payload).slice(0, plen - sodium.crypto_sign_BYTES);
const signature = Buffer.from(payload).slice(plen - sodium.crypto_sign_BYTES);
// verify signature
let expected;
try {
expected = utils.pae(header, data, footer);
} catch (ex) {
return done(ex);
}
const valid = sodium.crypto_sign_verify_detached(signature, expected, key.raw());
if (!valid) { return done(new PasetoError('Invalid signature for this message')); }
// format
return done(null, data.toString('utf-8'));
});
}
/***
* aeadEncrypt
*
* internals of symmetric authenticated encryption
*
* @function
* @api private
*
* @param {Object} key
* @param {Buffer} header
* @param {Buffer} plaintext
* @param {Buffer} footer
* @param {Buffer} nonce
* @returns {Callback|Promise}
*/
V2.prototype.aeadEncrypt = aeadEncrypt;
function aeadEncrypt(key, header, plaintext, footer, nonce) {
// build nonce
const nlen = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
const nkey = nonce || sodium.randombytes_buf(nlen);
const _nonce = sodium.crypto_generichash(nlen, plaintext, nkey);
nonce = Buffer.from(_nonce);
// encrypt
let ad;
try {
ad = utils.pae(header, nonce, footer);
} catch (ex) {
return done(ex);
}
const _ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, ad, null, nonce, key.raw());
const ciphertext = Buffer.from(_ciphertext);
// format
const payload = Buffer.concat([ nonce, ciphertext ]);
const token = header.toString('utf-8') + utils.toB64URLSafe(payload);
return (!Buffer.byteLength(footer))
? token
: token + '.' + utils.toB64URLSafe(footer);
}
/***
* aeadDecrypt
*
* internals of symmetric authenticated decryption
*
* @function
* @api private
*
* @param {Object} key
* @param {Buffer} header
* @param {Buffer} payload
* @param {Buffer} footer
* @returns {Callback|Promise}
*/
V2.prototype.aeadDecrypt = aeadDecrypt;
function aeadDecrypt(key, header, payload, footer) {
// recover nonce
const plen = Buffer.byteLength(payload);
const nlen = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
const nonce = Buffer.from(payload).slice(0, nlen);
// decrypt and verify
let ad;
try {
ad = utils.pae(header, nonce, footer);
} catch (ex) {
return done(ex);
}
const ciphertext = Buffer.from(payload).slice(nlen, plen);
const _plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null, ciphertext, ad, nonce, key.raw());
const plaintext = Buffer.from(_plaintext);
// format
return plaintext.toString('utf-8');
}