-
Notifications
You must be signed in to change notification settings - Fork 799
/
ecdh_x_symkey.js
47 lines (42 loc) · 1.33 KB
/
ecdh_x_symkey.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
/**
* Encoded symmetric key for x25519 and x448
* The payload format varies for v3 and v6 PKESK:
* the former includes an algorithm byte preceeding the encrypted session key.
*
* @module type/x25519x448_symkey
*/
import util from '../util';
class ECDHXSymmetricKey {
static fromObject({ wrappedKey, algorithm }) {
const instance = new ECDHXSymmetricKey();
instance.wrappedKey = wrappedKey;
instance.algorithm = algorithm;
return instance;
}
/**
* - 1 octect for the length `l`
* - `l` octects of encoded session key data (with optional leading algorithm byte)
* @param {Uint8Array} bytes
* @returns {Number} Number of read bytes.
*/
read(bytes) {
let read = 0;
let followLength = bytes[read++];
this.algorithm = followLength % 2 ? bytes[read++] : null; // session key size is always even
followLength -= followLength % 2;
this.wrappedKey = util.readExactSubarray(bytes, read, read + followLength); read += followLength;
}
/**
* Write an MontgomerySymmetricKey as an Uint8Array
* @returns {Uint8Array} Serialised data
*/
write() {
return util.concatUint8Array([
this.algorithm ?
new Uint8Array([this.wrappedKey.length + 1, this.algorithm]) :
new Uint8Array([this.wrappedKey.length]),
this.wrappedKey
]);
}
}
export default ECDHXSymmetricKey;