@unknownncat/swt-libsignal 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/crypto.d.ts +1 -1
- package/dist/crypto.js +69 -35
- package/dist/curve.d.ts +1 -1
- package/dist/fingerprint.js +1 -1
- package/dist/index.d.ts +19 -20
- package/dist/index.js +12 -13
- package/dist/job_queue.js +22 -18
- package/dist/key-helper.d.ts +1 -1
- package/dist/key-helper.js +1 -1
- package/dist/protobuf.d.ts +1 -1
- package/dist/protobuf.js +1 -1
- package/dist/{base_key_type.d.ts → ratchet-types.d.ts} +5 -0
- package/dist/{chain_type.js → ratchet-types.js} +4 -0
- package/dist/session/builder/index.d.ts +2 -2
- package/dist/session/builder/index.js +2 -2
- package/dist/session/builder/session-builder.d.ts +3 -3
- package/dist/session/builder/session-builder.js +36 -16
- package/dist/session/builder/types.d.ts +1 -1
- package/dist/session/cipher/encoding.d.ts +1 -1
- package/dist/session/cipher/index.d.ts +3 -3
- package/dist/session/cipher/index.js +2 -3
- package/dist/session/cipher/session-cipher.d.ts +2 -2
- package/dist/session/cipher/session-cipher.js +108 -59
- package/dist/session/cipher/types.d.ts +2 -2
- package/dist/session/index.d.ts +5 -3
- package/dist/session/index.js +5 -3
- package/dist/session/record/index.d.ts +3 -3
- package/dist/session/record/index.js +3 -3
- package/dist/session/record/session-entry.d.ts +1 -1
- package/dist/session/record/session-entry.js +2 -3
- package/dist/session/record/session-record.d.ts +2 -2
- package/dist/session/record/session-record.js +4 -4
- package/dist/session/record/types.d.ts +3 -3
- package/dist/session/storage/adapter.d.ts +21 -0
- package/dist/session/storage/adapter.js +57 -0
- package/dist/session/storage/in-memory.d.ts +13 -0
- package/dist/session/storage/in-memory.js +33 -0
- package/dist/session/storage/index.d.ts +19 -0
- package/dist/session/storage/index.js +29 -0
- package/dist/session/storage/migrations.d.ts +2 -0
- package/dist/session/storage/migrations.js +6 -0
- package/dist/session/storage/types.d.ts +18 -0
- package/dist/types/asymmetric.d.ts +41 -0
- package/dist/types/crypto.d.ts +69 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +12 -3
- package/dist/base_key_type.js +0 -4
- package/dist/chain_type.d.ts +0 -5
- package/dist/teste.js +0 -18
- /package/dist/{teste.d.ts → session/storage/types.js} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 @unknownncat/swt-libsignal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/crypto.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { CryptoAPI } from '
|
|
1
|
+
import type { CryptoAPI } from './types/crypto';
|
|
2
2
|
export declare const crypto: CryptoAPI;
|
package/dist/crypto.js
CHANGED
|
@@ -8,34 +8,26 @@ function toBuffer(data) {
|
|
|
8
8
|
function toUint8(data) {
|
|
9
9
|
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
function encrypt(key, plaintext, options) {
|
|
12
12
|
const k = toBuffer(key);
|
|
13
|
-
if (k.length !== 32)
|
|
13
|
+
if (k.length !== 32)
|
|
14
14
|
throw new Error('Key must be 32 bytes');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
: randomBytes(12);
|
|
15
|
+
const iv = options?.iv ? toBuffer(options.iv) : randomBytes(12);
|
|
16
|
+
if (iv.length !== 12)
|
|
17
|
+
throw new Error('IV must be 12 bytes for AES-GCM');
|
|
19
18
|
const cipher = createCipheriv('aes-256-gcm', k, iv);
|
|
20
|
-
if (options?.aad)
|
|
19
|
+
if (options?.aad)
|
|
21
20
|
cipher.setAAD(toBuffer(options.aad));
|
|
22
|
-
|
|
23
|
-
const plainBuf = toBuffer(plaintext);
|
|
24
|
-
const encrypted = cipher.update(plainBuf);
|
|
25
|
-
const final = cipher.final();
|
|
26
|
-
const ciphertext = encrypted.length === 0
|
|
27
|
-
? final
|
|
28
|
-
: encrypted.length === final.length
|
|
29
|
-
? Buffer.concat([encrypted, final], encrypted.length + final.length)
|
|
30
|
-
: Buffer.concat([encrypted, final]);
|
|
21
|
+
const encrypted = Buffer.concat([cipher.update(toBuffer(plaintext)), cipher.final()]);
|
|
31
22
|
const tag = cipher.getAuthTag();
|
|
23
|
+
k.fill(0);
|
|
32
24
|
return {
|
|
33
|
-
ciphertext: toUint8(
|
|
25
|
+
ciphertext: toUint8(encrypted),
|
|
34
26
|
iv: toUint8(iv),
|
|
35
27
|
tag: toUint8(tag)
|
|
36
28
|
};
|
|
37
29
|
}
|
|
38
|
-
|
|
30
|
+
function decrypt(key, data, options) {
|
|
39
31
|
const k = toBuffer(key);
|
|
40
32
|
const decipher = createDecipheriv('aes-256-gcm', k, toBuffer(data.iv));
|
|
41
33
|
if (options?.aad) {
|
|
@@ -53,28 +45,70 @@ async function decrypt(key, data, options) {
|
|
|
53
45
|
return toUint8(plaintext);
|
|
54
46
|
}
|
|
55
47
|
function hkdf(ikm, salt, info, options) {
|
|
48
|
+
if (!Buffer.isBuffer(Buffer.from(ikm)) && !(ikm instanceof Uint8Array)) {
|
|
49
|
+
throw new TypeError('ikm must be Uint8Array');
|
|
50
|
+
}
|
|
51
|
+
if (ikm.length === 0) {
|
|
52
|
+
throw new Error('IKM cannot be empty');
|
|
53
|
+
}
|
|
54
|
+
if (ikm.length > 1024 * 1024) {
|
|
55
|
+
throw new Error('IKM too large (max 1MB)');
|
|
56
|
+
}
|
|
57
|
+
if (!(salt instanceof Uint8Array)) {
|
|
58
|
+
throw new TypeError('salt must be Uint8Array');
|
|
59
|
+
}
|
|
60
|
+
if (!(info instanceof Uint8Array)) {
|
|
61
|
+
throw new TypeError('info must be Uint8Array');
|
|
62
|
+
}
|
|
56
63
|
const length = options.length;
|
|
64
|
+
if (!Number.isInteger(length) || length <= 0) {
|
|
65
|
+
throw new Error(`length must be positive integer, got ${length}`);
|
|
66
|
+
}
|
|
67
|
+
if (length > 255 * 32) {
|
|
68
|
+
throw new Error(`length exceeds maximum (${255 * 32} bytes)`);
|
|
69
|
+
}
|
|
57
70
|
const hashLen = 32;
|
|
58
71
|
const blocksNeeded = Math.ceil(length / hashLen);
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
.digest();
|
|
62
|
-
const output = Buffer.allocUnsafe(length);
|
|
72
|
+
const ikmBuf = toBuffer(ikm);
|
|
73
|
+
const saltBuf = toBuffer(salt);
|
|
63
74
|
const infoBuf = toBuffer(info);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
const prkKey = saltBuf.length ? saltBuf : Buffer.alloc(hashLen, 0);
|
|
76
|
+
try {
|
|
77
|
+
const prk = createHmac('sha256', prkKey)
|
|
78
|
+
.update(ikmBuf)
|
|
79
|
+
.digest();
|
|
80
|
+
const output = Buffer.alloc(length);
|
|
81
|
+
let prev = EMPTY_BUFFER;
|
|
82
|
+
const counter = Buffer.alloc(1);
|
|
83
|
+
for (let i = 0; i < blocksNeeded; i++) {
|
|
84
|
+
const hmac = createHmac('sha256', prk);
|
|
85
|
+
if (prev.length) {
|
|
86
|
+
hmac.update(prev);
|
|
87
|
+
}
|
|
88
|
+
if (infoBuf.length) {
|
|
89
|
+
hmac.update(infoBuf);
|
|
90
|
+
}
|
|
91
|
+
counter[0] = i + 1;
|
|
92
|
+
hmac.update(counter);
|
|
93
|
+
prev = hmac.digest();
|
|
94
|
+
const copyLen = Math.min(hashLen, length - (i * hashLen));
|
|
95
|
+
prev.copy(output, i * hashLen, 0, copyLen);
|
|
96
|
+
}
|
|
97
|
+
prk.fill(0);
|
|
98
|
+
prev.fill(0);
|
|
99
|
+
ikmBuf.fill(0);
|
|
100
|
+
if (saltBuf.length)
|
|
101
|
+
saltBuf.fill(0);
|
|
102
|
+
infoBuf.fill(0);
|
|
103
|
+
return toUint8(output);
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
ikmBuf.fill(0);
|
|
107
|
+
if (saltBuf.length)
|
|
108
|
+
saltBuf.fill(0);
|
|
109
|
+
infoBuf.fill(0);
|
|
110
|
+
throw err;
|
|
76
111
|
}
|
|
77
|
-
return toUint8(output);
|
|
78
112
|
}
|
|
79
113
|
function sha512(data) {
|
|
80
114
|
return toUint8(createHash('sha512')
|
package/dist/curve.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DHKeyPair, SignalAsymmetricAPI } from '
|
|
1
|
+
import type { DHKeyPair, SignalAsymmetricAPI } from './types/asymmetric';
|
|
2
2
|
declare function generateKeyPair(): Promise<DHKeyPair>;
|
|
3
3
|
declare function calculateSignature(identityPrivateKey: Uint8Array, data: Uint8Array): Uint8Array<ArrayBufferLike>;
|
|
4
4
|
export declare const signalCrypto: SignalAsymmetricAPI;
|
package/dist/fingerprint.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
export { ProtocolAddress } from './protocol_address
|
|
2
|
-
export { SessionRecord, SessionEntry } from './session/record/index
|
|
3
|
-
export type { ChainKey, ChainState, CurrentRatchet, IndexInfo } from './session/record/index
|
|
4
|
-
export { SessionCipher } from './session/cipher/index
|
|
5
|
-
export type { EncryptResult, DecryptResult, DecryptWithSessionResult, SessionCipherStorage } from './session/cipher/index
|
|
6
|
-
export { SessionBuilder } from './session/builder/session-builder
|
|
7
|
-
export type { PreKeyBundle, SessionBuilderStorage, KeyPair, IdentityKeyPair } from './session/builder/index
|
|
8
|
-
export { crypto } from './crypto
|
|
9
|
-
export { signalCrypto, generateKeyPair, calculateSignature } from './curve
|
|
10
|
-
export type { CryptoAPI } from '
|
|
11
|
-
export type { SignalAsymmetricAPI, IdentityKeyPair as AsymIdentityKeyPair, DHKeyPair } from '
|
|
12
|
-
export { generateIdentityKeyPair, generateRegistrationId, generateSignedPreKey, generatePreKey } from './key-helper
|
|
13
|
-
export type { SignedPreKey, PreKey } from './key-helper
|
|
14
|
-
export { FingerprintGenerator } from './fingerprint
|
|
15
|
-
export { SignalError, UntrustedIdentityKeyError, SessionError, MessageCounterError, PreKeyError } from './signal-errors
|
|
16
|
-
export
|
|
17
|
-
export { BaseKeyType } from './
|
|
18
|
-
export
|
|
19
|
-
export {
|
|
20
|
-
export { enqueue } from './job_queue.js';
|
|
1
|
+
export { ProtocolAddress } from './protocol_address';
|
|
2
|
+
export { SessionRecord, SessionEntry } from './session/record/index';
|
|
3
|
+
export type { ChainKey, ChainState, CurrentRatchet, IndexInfo } from './session/record/index';
|
|
4
|
+
export { SessionCipher, WhisperMessageEncoder } from './session/cipher/index';
|
|
5
|
+
export type { EncryptResult, DecryptResult, DecryptWithSessionResult, SessionCipherStorage } from './session/cipher/index';
|
|
6
|
+
export { SessionBuilder } from './session/builder/session-builder';
|
|
7
|
+
export type { PreKeyBundle, SessionBuilderStorage, KeyPair, IdentityKeyPair } from './session/builder/index';
|
|
8
|
+
export { crypto } from './crypto';
|
|
9
|
+
export { signalCrypto, generateKeyPair, calculateSignature } from './curve';
|
|
10
|
+
export type { CryptoAPI } from './types/crypto';
|
|
11
|
+
export type { SignalAsymmetricAPI, IdentityKeyPair as AsymIdentityKeyPair, DHKeyPair } from './types/asymmetric';
|
|
12
|
+
export { generateIdentityKeyPair, generateRegistrationId, generateSignedPreKey, generatePreKey } from './key-helper';
|
|
13
|
+
export type { SignedPreKey, PreKey } from './key-helper';
|
|
14
|
+
export { FingerprintGenerator } from './fingerprint';
|
|
15
|
+
export { SignalError, UntrustedIdentityKeyError, SessionError, MessageCounterError, PreKeyError } from './signal-errors';
|
|
16
|
+
export * from './ratchet-types';
|
|
17
|
+
export type { BaseKeyType as BaseKeyTypeValue } from './ratchet-types';
|
|
18
|
+
export { PreKeyWhisperMessage, WhisperMessage } from './protobuf';
|
|
19
|
+
export { enqueue } from './job_queue';
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
export { ProtocolAddress } from './protocol_address
|
|
2
|
-
export { SessionRecord, SessionEntry } from './session/record/index
|
|
3
|
-
export { SessionCipher } from './session/cipher/index
|
|
4
|
-
export { SessionBuilder } from './session/builder/session-builder
|
|
5
|
-
export { crypto } from './crypto
|
|
6
|
-
export { signalCrypto, generateKeyPair, calculateSignature } from './curve
|
|
7
|
-
export { generateIdentityKeyPair, generateRegistrationId, generateSignedPreKey, generatePreKey } from './key-helper
|
|
8
|
-
export { FingerprintGenerator } from './fingerprint
|
|
9
|
-
export { SignalError, UntrustedIdentityKeyError, SessionError, MessageCounterError, PreKeyError } from './signal-errors
|
|
10
|
-
export
|
|
11
|
-
export {
|
|
12
|
-
export {
|
|
13
|
-
export { enqueue } from './job_queue.js';
|
|
1
|
+
export { ProtocolAddress } from './protocol_address';
|
|
2
|
+
export { SessionRecord, SessionEntry } from './session/record/index';
|
|
3
|
+
export { SessionCipher, WhisperMessageEncoder } from './session/cipher/index';
|
|
4
|
+
export { SessionBuilder } from './session/builder/session-builder';
|
|
5
|
+
export { crypto } from './crypto';
|
|
6
|
+
export { signalCrypto, generateKeyPair, calculateSignature } from './curve';
|
|
7
|
+
export { generateIdentityKeyPair, generateRegistrationId, generateSignedPreKey, generatePreKey } from './key-helper';
|
|
8
|
+
export { FingerprintGenerator } from './fingerprint';
|
|
9
|
+
export { SignalError, UntrustedIdentityKeyError, SessionError, MessageCounterError, PreKeyError } from './signal-errors';
|
|
10
|
+
export * from './ratchet-types';
|
|
11
|
+
export { PreKeyWhisperMessage, WhisperMessage } from './protobuf';
|
|
12
|
+
export { enqueue } from './job_queue';
|
package/dist/job_queue.js
CHANGED
|
@@ -2,28 +2,32 @@ const queueBuckets = new Map();
|
|
|
2
2
|
const GC_LIMIT = 10_000;
|
|
3
3
|
async function asyncQueueExecutor(bucket, state) {
|
|
4
4
|
const queue = state.queue;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
try {
|
|
6
|
+
while (state.offset < queue.length) {
|
|
7
|
+
const limit = Math.min(queue.length, state.offset + GC_LIMIT);
|
|
8
|
+
for (let i = state.offset; i < limit; i++) {
|
|
9
|
+
const job = queue[i];
|
|
10
|
+
if (!job)
|
|
11
|
+
continue;
|
|
12
|
+
try {
|
|
13
|
+
const result = await job.awaitable();
|
|
14
|
+
job.resolve(result);
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
job.reject(err);
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
state.offset = limit;
|
|
21
|
+
if (limit < queue.length) {
|
|
22
|
+
queue.splice(0, limit);
|
|
23
|
+
state.offset = 0;
|
|
17
24
|
}
|
|
18
25
|
}
|
|
19
|
-
state.offset = limit;
|
|
20
|
-
if (limit < queue.length) {
|
|
21
|
-
queue.splice(0, limit);
|
|
22
|
-
state.offset = 0;
|
|
23
|
-
}
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
finally {
|
|
28
|
+
state.running = false;
|
|
29
|
+
queueBuckets.delete(bucket);
|
|
30
|
+
}
|
|
27
31
|
}
|
|
28
32
|
export function enqueue(bucket, awaitable) {
|
|
29
33
|
let state = queueBuckets.get(bucket);
|
package/dist/key-helper.d.ts
CHANGED
package/dist/key-helper.js
CHANGED
package/dist/protobuf.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { PreKeyWhisperMessage, WhisperMessage } from "./generated/WhisperTextProtocol
|
|
1
|
+
export { PreKeyWhisperMessage, WhisperMessage } from "./generated/WhisperTextProtocol";
|
package/dist/protobuf.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
export { PreKeyWhisperMessage, WhisperMessage } from "./generated/WhisperTextProtocol
|
|
2
|
+
export { PreKeyWhisperMessage, WhisperMessage } from "./generated/WhisperTextProtocol";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './types
|
|
2
|
-
export * from './session-builder
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './session-builder';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './types
|
|
2
|
-
export * from './session-builder
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './session-builder';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ProtocolAddress } from '../../protocol_address
|
|
2
|
-
import { SessionRecord } from '../record/index
|
|
3
|
-
import type { PreKeyBundle, PreKeyWhisperMessage, SessionBuilderStorage } from './types
|
|
1
|
+
import type { ProtocolAddress } from '../../protocol_address';
|
|
2
|
+
import { SessionRecord } from '../record/index';
|
|
3
|
+
import type { PreKeyBundle, PreKeyWhisperMessage, SessionBuilderStorage } from './types';
|
|
4
4
|
export declare class SessionBuilder {
|
|
5
5
|
private readonly addr;
|
|
6
6
|
private readonly storage;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { BaseKeyType } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { enqueue } from '../../job_queue.js';
|
|
1
|
+
import { BaseKeyType, ChainType } from "../../ratchet-types";
|
|
2
|
+
import { SessionRecord } from '../record/index';
|
|
3
|
+
import { crypto } from '../../crypto';
|
|
4
|
+
import { signalCrypto } from '../../curve';
|
|
5
|
+
import { UntrustedIdentityKeyError, PreKeyError } from '../../signal-errors';
|
|
6
|
+
import { enqueue } from '../../job_queue';
|
|
8
7
|
export class SessionBuilder {
|
|
9
8
|
addr;
|
|
10
9
|
storage;
|
|
@@ -13,12 +12,33 @@ export class SessionBuilder {
|
|
|
13
12
|
this.storage = storage;
|
|
14
13
|
}
|
|
15
14
|
async initOutgoing(device) {
|
|
15
|
+
if (!device) {
|
|
16
|
+
throw new TypeError('device must be a PreKeyBundle');
|
|
17
|
+
}
|
|
18
|
+
if (!device.identityKey || device.identityKey.length === 0) {
|
|
19
|
+
throw new Error('Invalid device.identityKey');
|
|
20
|
+
}
|
|
21
|
+
if (!device.signedPreKey) {
|
|
22
|
+
throw new Error('Missing device.signedPreKey');
|
|
23
|
+
}
|
|
24
|
+
if (!device.signedPreKey.publicKey || device.signedPreKey.publicKey.length === 0) {
|
|
25
|
+
throw new Error('Invalid device.signedPreKey.publicKey');
|
|
26
|
+
}
|
|
27
|
+
if (!device.signedPreKey.signature || device.signedPreKey.signature.length === 0) {
|
|
28
|
+
throw new Error('Invalid device.signedPreKey.signature');
|
|
29
|
+
}
|
|
30
|
+
if (device.registrationId == null || device.registrationId < 0) {
|
|
31
|
+
throw new Error('Invalid device.registrationId');
|
|
32
|
+
}
|
|
16
33
|
const fqAddr = this.addr.toString();
|
|
17
34
|
return enqueue(fqAddr, async () => {
|
|
18
35
|
if (!await this.storage.isTrustedIdentity(this.addr.id, device.identityKey)) {
|
|
19
36
|
throw new UntrustedIdentityKeyError(this.addr.id, device.identityKey);
|
|
20
37
|
}
|
|
21
|
-
signalCrypto.verify(device.identityKey, device.signedPreKey.publicKey, device.signedPreKey.signature);
|
|
38
|
+
const signatureValid = signalCrypto.verify(device.identityKey, device.signedPreKey.publicKey, device.signedPreKey.signature);
|
|
39
|
+
if (!signatureValid) {
|
|
40
|
+
throw new Error(`Invalid signature on signedPreKey from ${this.addr}. Possible MITM attack.`);
|
|
41
|
+
}
|
|
22
42
|
const baseKey = await signalCrypto.generateDHKeyPair();
|
|
23
43
|
const session = await this.initSession(true, { pubKey: baseKey.publicKey, privKey: baseKey.privateKey }, undefined, device.identityKey, device.preKey?.publicKey, device.signedPreKey.publicKey, device.registrationId);
|
|
24
44
|
const pendingPreKey = {
|
|
@@ -70,26 +90,26 @@ export class SessionBuilder {
|
|
|
70
90
|
return message.preKeyId;
|
|
71
91
|
}
|
|
72
92
|
async initSession(isInitiator, ourEphemeralKey, ourSignedKey, theirIdentityPubKey, theirEphemeralPubKey, theirSignedPubKey, registrationId) {
|
|
93
|
+
let ourEphemeralForInitSession = ourEphemeralKey;
|
|
94
|
+
let theirEphemeralForInitSession = theirEphemeralPubKey;
|
|
73
95
|
if (isInitiator) {
|
|
74
96
|
ourSignedKey = ourEphemeralKey;
|
|
75
97
|
}
|
|
76
98
|
else {
|
|
77
99
|
theirSignedPubKey = theirEphemeralPubKey;
|
|
78
100
|
}
|
|
79
|
-
const sharedSecretLen = (!
|
|
101
|
+
const sharedSecretLen = (!ourEphemeralForInitSession || !theirEphemeralForInitSession) ? 128 : 160;
|
|
80
102
|
const sharedSecret = new Uint8Array(sharedSecretLen);
|
|
81
103
|
sharedSecret.fill(0xff, 0, 32);
|
|
82
104
|
const ourIdentityKey = await this.storage.getOurIdentity();
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
signalCrypto.calculateAgreement(theirSignedPubKey, ourSignedKey.privKey)
|
|
87
|
-
]);
|
|
105
|
+
const a1 = signalCrypto.calculateAgreement(theirSignedPubKey, ourIdentityKey.privKey);
|
|
106
|
+
const a2 = signalCrypto.calculateAgreement(theirIdentityPubKey, ourSignedKey.privKey);
|
|
107
|
+
const a3 = signalCrypto.calculateAgreement(theirSignedPubKey, ourSignedKey.privKey);
|
|
88
108
|
sharedSecret.set(a1, isInitiator ? 32 : 64);
|
|
89
109
|
sharedSecret.set(a2, isInitiator ? 64 : 32);
|
|
90
110
|
sharedSecret.set(a3, 96);
|
|
91
|
-
if (
|
|
92
|
-
const a4 = signalCrypto.calculateAgreement(
|
|
111
|
+
if (ourEphemeralForInitSession && theirEphemeralForInitSession) {
|
|
112
|
+
const a4 = signalCrypto.calculateAgreement(theirEphemeralForInitSession, ourEphemeralForInitSession.privKey);
|
|
93
113
|
sharedSecret.set(a4, 128);
|
|
94
114
|
}
|
|
95
115
|
const masterKey = this.deriveSecrets(sharedSecret, new Uint8Array(32), new TextEncoder().encode('WhisperText'), 2);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WhisperMessageProto, PreKeyWhisperMessageProto } from './types
|
|
1
|
+
import type { WhisperMessageProto, PreKeyWhisperMessageProto } from './types';
|
|
2
2
|
export declare class WhisperMessageEncoder {
|
|
3
3
|
static encodeWhisperMessage(msg: WhisperMessageProto): Uint8Array;
|
|
4
4
|
static decodeWhisperMessage(buf: Uint8Array): WhisperMessageProto;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export * from './
|
|
1
|
+
export { WhisperMessageEncoder } from "./encoding";
|
|
2
|
+
export { SessionCipher } from './session-cipher';
|
|
3
|
+
export type * from './types';
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export * from './session-cipher.js';
|
|
1
|
+
export { WhisperMessageEncoder } from "./encoding";
|
|
2
|
+
export { SessionCipher } from './session-cipher';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { EncryptResult, SessionCipherStorage } from './types
|
|
2
|
-
import { ProtocolAddress } from '../../protocol_address
|
|
1
|
+
import type { EncryptResult, SessionCipherStorage } from './types';
|
|
2
|
+
import { ProtocolAddress } from '../../protocol_address';
|
|
3
3
|
export declare class SessionCipher {
|
|
4
4
|
private readonly addr;
|
|
5
5
|
private readonly addrStr;
|