-
Notifications
You must be signed in to change notification settings - Fork 25
/
UsersSerialize.js
375 lines (339 loc) · 17.7 KB
/
UsersSerialize.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
// @flow
import { tcrypto, random, utils } from '@tanker/crypto';
import { expect } from '@tanker/test-utils';
import { encodeListLength } from '../Blocks/Serialize';
import {
serializeUserDeviceV3,
unserializeUserDeviceV1,
unserializeUserDeviceV2,
unserializeUserDeviceV3,
serializeDeviceRevocationV2,
unserializeDeviceRevocationV1,
unserializeDeviceRevocationV2,
} from '../Users/Serialize';
import makeUint8Array from './makeUint8Array';
// NOTE: If you ever have to change something here, change it in the Go code too!
// The test vectors should stay the same
describe('payload test vectors', () => {
it('correctly deserializes a DeviceCreation v1 test vector', async () => {
const deviceCreation = {
ephemeral_public_signature_key: new Uint8Array([
0x4e, 0x2a, 0x65, 0xdf, 0xe6, 0x5d, 0x00, 0x58, 0xf4, 0xdf, 0xb0, 0x5d, 0x37, 0x64, 0x18, 0x1d,
0x10, 0x61, 0xf7, 0x54, 0xbb, 0x70, 0x30, 0x4f, 0x08, 0x6e, 0x32, 0x14, 0x85, 0x7a, 0xee, 0xe5
]),
user_id: new Uint8Array([
0xbd, 0xec, 0xe7, 0xbe, 0x4c, 0xd6, 0xc8, 0x33, 0xec, 0xf9, 0x42, 0xe1, 0xa9, 0xc4, 0xa7, 0x3e,
0x39, 0xac, 0xdd, 0x6d, 0x99, 0x37, 0xc2, 0x9a, 0xbf, 0xf8, 0x6c, 0x4f, 0xce, 0x3a, 0x34, 0xcd
]),
delegation_signature: new Uint8Array([
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
]),
public_signature_key: new Uint8Array([
0x21, 0x2c, 0x54, 0x3a, 0xae, 0xcf, 0xc6, 0xef, 0x0b, 0x60, 0xae, 0xe6, 0x11, 0x52, 0xa1, 0x30,
0x60, 0xbc, 0x34, 0xbc, 0x1b, 0x89, 0x39, 0xe1, 0xd9, 0x94, 0x9a, 0xaa, 0x14, 0x4c, 0x41, 0x60
]),
public_encryption_key: new Uint8Array([
0x42, 0x9a, 0xfa, 0x09, 0xee, 0xea, 0xce, 0x12, 0xec, 0x59, 0x06, 0x35, 0xa8, 0x7f, 0x82, 0xe6,
0x39, 0xc8, 0xce, 0xd0, 0xc8, 0xe5, 0x57, 0x16, 0x72, 0x94, 0x9e, 0xfb, 0xed, 0x59, 0xde, 0x2e
]),
user_key_pair: null,
is_ghost_device: false,
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
revoked: Number.MAX_SAFE_INTEGER,
};
const payload = utils.concatArrays(
deviceCreation.ephemeral_public_signature_key,
deviceCreation.user_id,
deviceCreation.delegation_signature,
deviceCreation.public_signature_key,
deviceCreation.public_encryption_key
);
expect(unserializeUserDeviceV1(payload)).to.deep.equal(deviceCreation);
});
it('correctly deserializes a DeviceCreation v2 test vector', async () => {
const deviceCreation = {
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
ephemeral_public_signature_key: new Uint8Array([
0x4e, 0x2a, 0x65, 0xdf, 0xe6, 0x5d, 0x00, 0x58, 0xf4, 0xdf, 0xb0, 0x5d, 0x37, 0x64, 0x18, 0x1d,
0x10, 0x61, 0xf7, 0x54, 0xbb, 0x70, 0x30, 0x4f, 0x08, 0x6e, 0x32, 0x14, 0x85, 0x7a, 0xee, 0xe5
]),
user_id: new Uint8Array([
0xbd, 0xec, 0xe7, 0xbe, 0x4c, 0xd6, 0xc8, 0x33, 0xec, 0xf9, 0x42, 0xe1, 0xa9, 0xc4, 0xa7, 0x3e,
0x39, 0xac, 0xdd, 0x6d, 0x99, 0x37, 0xc2, 0x9a, 0xbf, 0xf8, 0x6c, 0x4f, 0xce, 0x3a, 0x34, 0xcd
]),
delegation_signature: new Uint8Array([
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
]),
public_signature_key: new Uint8Array([
0x21, 0x2c, 0x54, 0x3a, 0xae, 0xcf, 0xc6, 0xef, 0x0b, 0x60, 0xae, 0xe6, 0x11, 0x52, 0xa1, 0x30,
0x60, 0xbc, 0x34, 0xbc, 0x1b, 0x89, 0x39, 0xe1, 0xd9, 0x94, 0x9a, 0xaa, 0x14, 0x4c, 0x41, 0x60
]),
public_encryption_key: new Uint8Array([
0x42, 0x9a, 0xfa, 0x09, 0xee, 0xea, 0xce, 0x12, 0xec, 0x59, 0x06, 0x35, 0xa8, 0x7f, 0x82, 0xe6,
0x39, 0xc8, 0xce, 0xd0, 0xc8, 0xe5, 0x57, 0x16, 0x72, 0x94, 0x9e, 0xfb, 0xed, 0x59, 0xde, 0x2e
]),
user_key_pair: null,
is_ghost_device: false,
revoked: Number.MAX_SAFE_INTEGER,
};
const payload = utils.concatArrays(
deviceCreation.last_reset,
deviceCreation.ephemeral_public_signature_key,
deviceCreation.user_id,
deviceCreation.delegation_signature,
deviceCreation.public_signature_key,
deviceCreation.public_encryption_key
);
expect(unserializeUserDeviceV2(payload)).to.deep.equal(deviceCreation);
});
it('correctly deserializes a DeviceCreation v3 test vector', async () => {
const payload = new Uint8Array([
// ephemeral_public_signature_key
0x65, 0x70, 0x68, 0x20, 0x70, 0x75, 0x62, 0x20, 0x6b, 0x65, 0x79, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// user_id
0x75, 0x73, 0x65, 0x72, 0x20, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// delegation_signature
0x64, 0x65, 0x6c, 0x65, 0x67, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x73,
0x69, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// public_signature_key
0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x73, 0x69, 0x67, 0x6e, 0x61,
0x74, 0x75, 0x72, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// public_encryption_key
0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x65, 0x6e, 0x63, 0x20, 0x6b,
0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// user public_encryption_key
0x75, 0x73, 0x65, 0x72, 0x20, 0x70, 0x75, 0x62, 0x20, 0x65, 0x6e, 0x63,
0x20, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// user encrypted_private_encryption_key
0x75, 0x73, 0x65, 0x72, 0x20, 0x65, 0x6e, 0x63, 0x20, 0x6b, 0x65, 0x79,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// is_ghost_device
0x01,
]);
const deviceCreation = {
last_reset: new Uint8Array(32),
ephemeral_public_signature_key: makeUint8Array('eph pub key', tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
user_id: makeUint8Array('user id', tcrypto.HASH_SIZE),
delegation_signature: makeUint8Array('delegation sig', tcrypto.SIGNATURE_SIZE),
public_signature_key: makeUint8Array('public signature key', tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
public_encryption_key: makeUint8Array('public enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
user_key_pair: {
public_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_private_encryption_key: makeUint8Array('user enc key', tcrypto.SEALED_KEY_SIZE),
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
};
expect(unserializeUserDeviceV3(payload)).to.deep.equal(deviceCreation);
});
it('correctly deserializes a DeviceRevocationV1 test vector', async () => {
const deviceRevocation = {
device_id: new Uint8Array([
0xe9, 0x0b, 0x0a, 0x13, 0x05, 0xb1, 0x82, 0x85, 0xab, 0x9d, 0xbe, 0x3f, 0xdb, 0x57, 0x2b, 0x71,
0x6c, 0x0d, 0xa1, 0xa3, 0xad, 0xb8, 0x86, 0x9b, 0x39, 0x58, 0xcb, 0x00, 0xfa, 0x31, 0x5d, 0x87
]),
};
const payload = deviceRevocation.device_id;
expect(unserializeDeviceRevocationV1(payload)).to.deep.equal(deviceRevocation);
});
it('correctly deserializes a DeviceRevocationV2 test vector', async () => {
const deviceRevocation = {
device_id: new Uint8Array([
0xe9, 0x0b, 0x0a, 0x13, 0x05, 0xb1, 0x82, 0x85, 0xab, 0x9d, 0xbe, 0x3f, 0xdb, 0x57, 0x2b, 0x71,
0x6c, 0x0d, 0xa1, 0xa3, 0xad, 0xb8, 0x86, 0x9b, 0x39, 0x58, 0xcb, 0x00, 0xfa, 0x31, 0x5d, 0x87
]),
user_keys: {
public_encryption_key: new Uint8Array([
0x42, 0x9a, 0xfa, 0x09, 0xee, 0xea, 0xce, 0x12, 0xec, 0x59, 0x06, 0x35, 0xa8, 0x7f, 0x82, 0xe6,
0x39, 0xc8, 0xce, 0xd0, 0xc8, 0xe5, 0x57, 0x16, 0x72, 0x94, 0x9e, 0xfb, 0xed, 0x59, 0xde, 0x2e
]),
previous_public_encryption_key: new Uint8Array([
0x8e, 0x3e, 0x33, 0x57, 0x3d, 0xd5, 0x3c, 0xe7, 0x29, 0xbc, 0x73, 0x90, 0x7f, 0x83, 0x20, 0xee,
0xe9, 0x0b, 0x0a, 0x13, 0x05, 0xb1, 0x82, 0x85, 0xab, 0x9d, 0xbe, 0x3f, 0xdb, 0x57, 0x2b, 0x71,
]),
encrypted_previous_encryption_key: new Uint8Array([
0xf1, 0x28, 0xa8, 0x12, 0x03, 0x8e, 0x7c, 0x9c, 0x39, 0xad, 0x73, 0x21, 0xa3, 0xee, 0x50, 0x53,
0xc1, 0x1d, 0xda, 0x76, 0xaf, 0xc8, 0xfd, 0x70, 0x74, 0x5c, 0xbb, 0xd6, 0xb8, 0x7f, 0x8f, 0x6b,
0xe1, 0xaf, 0x36, 0x80, 0x3f, 0xf3, 0xbc, 0xb2, 0xfb, 0x4e, 0xe1, 0x7d, 0xea, 0xbd, 0x19, 0x6b,
0x8e, 0x3e, 0x33, 0x57, 0x3d, 0xd5, 0x3c, 0xe7, 0x29, 0xbc, 0x73, 0x90, 0x7f, 0x83, 0x20, 0xee,
0x0e, 0xc0, 0x91, 0x63, 0xe7, 0xc2, 0x04, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]),
private_keys: [
{
recipient: new Uint8Array([
0xd0, 0xa8, 0x9e, 0xff, 0x7d, 0x59, 0x48, 0x3a, 0xee, 0x7c, 0xe4, 0x99, 0x49, 0x4d, 0x1c, 0xd7,
0x87, 0x54, 0x41, 0xf5, 0xba, 0x51, 0xd7, 0x65, 0xbf, 0x91, 0x45, 0x08, 0x03, 0xf1, 0xe9, 0xc7,
]),
key: new Uint8Array([
0xe1, 0xaf, 0x36, 0x80, 0x3f, 0xf3, 0xbc, 0xb2, 0xfb, 0x4e, 0xe1, 0x7d, 0xea, 0xbd, 0x19, 0x6b,
0x8e, 0x3e, 0x33, 0x57, 0x3d, 0xd5, 0x3c, 0xe7, 0x29, 0xbc, 0x73, 0x90, 0x7f, 0x83, 0x20, 0xee,
0xf1, 0x28, 0xa8, 0x12, 0x03, 0x8e, 0x7c, 0x9c, 0x39, 0xad, 0x73, 0x21, 0xa3, 0xee, 0x50, 0x53,
0xc1, 0x1d, 0xda, 0x76, 0xaf, 0xc8, 0xfd, 0x70, 0x74, 0x5c, 0xbb, 0xd6, 0xb8, 0x7f, 0x8f, 0x6b,
0x0e, 0xc0, 0x91, 0x63, 0xe7, 0xc2, 0x04, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]),
},
],
},
};
const payload = utils.concatArrays(
deviceRevocation.device_id,
deviceRevocation.user_keys.public_encryption_key,
deviceRevocation.user_keys.previous_public_encryption_key,
deviceRevocation.user_keys.encrypted_previous_encryption_key,
encodeListLength(deviceRevocation.user_keys.private_keys),
...deviceRevocation.user_keys.private_keys.map((userKey) => utils.concatArrays(userKey.recipient, userKey.key)),
);
expect(unserializeDeviceRevocationV2(payload)).to.deep.equal(deviceRevocation);
});
});
describe('payloads', () => {
it('should serialize/unserialize a UserDeviceV3', async () => {
const ephemeralKeys = tcrypto.makeSignKeyPair();
const signatureKeys = tcrypto.makeSignKeyPair();
const encryptionKeys = tcrypto.makeEncryptionKeyPair();
const userDevice = {
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
ephemeral_public_signature_key: ephemeralKeys.publicKey,
user_id: utils.fromString('12341234123412341234123412341234'),
delegation_signature: utils.fromString('1234123412341234123412341234123412341234123412341234123412341234'),
public_signature_key: signatureKeys.publicKey,
public_encryption_key: encryptionKeys.publicKey,
user_key_pair: {
public_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_private_encryption_key: makeUint8Array('user enc priv key', tcrypto.SEALED_KEY_SIZE),
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
};
expect(unserializeUserDeviceV3(serializeUserDeviceV3(userDevice))).to.deep.equal(userDevice);
});
it('should serialize/unserialize a DeviceRevocation', async () => {
const deviceRevocation = {
device_id: random(tcrypto.HASH_SIZE),
user_keys: {
public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
previous_public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_previous_encryption_key: random(tcrypto.SEALED_KEY_SIZE),
private_keys: [],
},
};
expect(unserializeDeviceRevocationV2(serializeDeviceRevocationV2(deviceRevocation))).to.deep.equal(deviceRevocation);
});
it('should throw when serializing invalid revocation blocks', async () => {
const initValidBlock = () => ({
device_id: new Uint8Array(tcrypto.HASH_SIZE),
user_keys: {
public_encryption_key: new Uint8Array(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
previous_public_encryption_key: new Uint8Array(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_previous_encryption_key: new Uint8Array(tcrypto.SEALED_KEY_SIZE),
private_keys: [{
recipient: new Uint8Array(tcrypto.HASH_SIZE),
key: new Uint8Array(tcrypto.SEALED_KEY_SIZE),
}],
},
});
let invalidBlock = initValidBlock();
invalidBlock.device_id = new Uint8Array(0);
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
invalidBlock = initValidBlock();
invalidBlock.user_keys = {};
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
invalidBlock = initValidBlock();
invalidBlock.user_keys.public_encryption_key = new Uint8Array(0);
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
invalidBlock = initValidBlock();
invalidBlock.user_keys.previous_public_encryption_key = new Uint8Array(0);
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
invalidBlock = initValidBlock();
invalidBlock.user_keys.encrypted_previous_encryption_key = new Uint8Array(0);
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
invalidBlock = initValidBlock();
invalidBlock.user_keys.private_keys[0].recipient = new Uint8Array(0);
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
invalidBlock = initValidBlock();
invalidBlock.user_keys.private_keys[0].key = new Uint8Array(0);
expect(() => serializeDeviceRevocationV2(invalidBlock)).to.throw();
});
it('should throw if the last reset is not null when serializing a new userDeviceV3', async () => {
const ephemeralKeys = tcrypto.makeSignKeyPair();
const signatureKeys = tcrypto.makeSignKeyPair();
const encryptionKeys = tcrypto.makeEncryptionKeyPair();
const userDevice = {
last_reset: new Uint8Array(Array.from({ length: tcrypto.HASH_SIZE }, () => 1)),
ephemeral_public_signature_key: ephemeralKeys.publicKey,
user_id: utils.fromString('12341234123412341234123412341234'),
delegation_signature: utils.fromString('1234123412341234123412341234123412341234123412341234123412341234'),
public_signature_key: signatureKeys.publicKey,
public_encryption_key: encryptionKeys.publicKey,
user_key_pair: {
public_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_private_encryption_key: makeUint8Array('user enc priv key', tcrypto.SEALED_KEY_SIZE),
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
};
expect(() => serializeUserDeviceV3(userDevice)).to.throw();
});
describe('serialization of invalid user device', () => {
let userDevice;
beforeEach(() => {
userDevice = {
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
ephemeral_public_signature_key: new Uint8Array(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
user_id: new Uint8Array(tcrypto.HASH_SIZE),
delegation_signature: new Uint8Array(tcrypto.SIGNATURE_SIZE),
public_signature_key: new Uint8Array(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
public_encryption_key: new Uint8Array(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
user_key_pair: {
public_encryption_key: new Uint8Array(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_private_encryption_key: new Uint8Array(tcrypto.SEALED_KEY_SIZE),
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
};
});
const fields = [
'ephemeral_public_signature_key',
'user_id',
'delegation_signature',
'public_signature_key',
'public_encryption_key',
];
fields.forEach(field => {
it(`should throw if user device with invalid ${field}`, async () => {
userDevice[field] = new Uint8Array(0);
expect(() => serializeUserDeviceV3(userDevice)).to.throw();
});
});
it('should throw if user device with invalid encrypted_private_encryption_key', async () => {
userDevice.user_key_pair.encrypted_private_encryption_key = new Uint8Array(0);
expect(() => serializeUserDeviceV3(userDevice)).to.throw();
});
it('should throw if user device with invalid public_encryption_key', () => {
userDevice.user_key_pair.public_encryption_key = new Uint8Array(0);
expect(() => serializeUserDeviceV3(userDevice)).to.throw();
});
});
});