@getpaseo/relay 0.5.0-beta.4 → 0.5.0
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/dist/crypto.js +20 -1
- package/package.json +1 -1
package/dist/crypto.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import nacl from "tweetnacl";
|
|
15
15
|
import { fromByteArray, toByteArray } from "base64-js";
|
|
16
16
|
const NONCE_LENGTH = nacl.box.nonceLength; // 24
|
|
17
|
+
const ZERO_X25519_SHARED_RESULT = new Uint8Array(nacl.box.sharedKeyLength);
|
|
17
18
|
let prngReady = false;
|
|
18
19
|
function getGlobalCrypto() {
|
|
19
20
|
const g = globalThis;
|
|
@@ -48,6 +49,18 @@ function encodeBase64(bytes) {
|
|
|
48
49
|
function decodeBase64(base64) {
|
|
49
50
|
return toByteArray(base64);
|
|
50
51
|
}
|
|
52
|
+
function decodePublicKeyBase64(base64) {
|
|
53
|
+
if (typeof base64 !== "string" ||
|
|
54
|
+
base64.length % 4 !== 0 ||
|
|
55
|
+
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(base64)) {
|
|
56
|
+
throw new Error("Invalid public key encoding");
|
|
57
|
+
}
|
|
58
|
+
const bytes = decodeBase64(base64);
|
|
59
|
+
if (encodeBase64(bytes) !== base64) {
|
|
60
|
+
throw new Error("Invalid public key encoding");
|
|
61
|
+
}
|
|
62
|
+
return bytes;
|
|
63
|
+
}
|
|
51
64
|
function toUint8(data) {
|
|
52
65
|
return typeof data === "string" ? new TextEncoder().encode(data) : new Uint8Array(data);
|
|
53
66
|
}
|
|
@@ -68,7 +81,7 @@ export function exportPublicKey(publicKey) {
|
|
|
68
81
|
return encodeBase64(publicKey);
|
|
69
82
|
}
|
|
70
83
|
export function importPublicKey(base64) {
|
|
71
|
-
const bytes =
|
|
84
|
+
const bytes = decodePublicKeyBase64(base64);
|
|
72
85
|
if (bytes.byteLength !== nacl.box.publicKeyLength) {
|
|
73
86
|
throw new Error(`Invalid public key length (expected ${nacl.box.publicKeyLength})`);
|
|
74
87
|
}
|
|
@@ -94,6 +107,12 @@ export function deriveSharedKey(ourSecretKey, peerPublicKey) {
|
|
|
94
107
|
if (peerPublicKey.byteLength !== nacl.box.publicKeyLength) {
|
|
95
108
|
throw new Error(`Invalid peer public key length (expected ${nacl.box.publicKeyLength})`);
|
|
96
109
|
}
|
|
110
|
+
const rawSharedResult = nacl.scalarMult(ourSecretKey, peerPublicKey);
|
|
111
|
+
const isAllZero = nacl.verify(rawSharedResult, ZERO_X25519_SHARED_RESULT);
|
|
112
|
+
rawSharedResult.fill(0);
|
|
113
|
+
if (isAllZero) {
|
|
114
|
+
throw new Error("Invalid peer public key");
|
|
115
|
+
}
|
|
97
116
|
return nacl.box.before(peerPublicKey, ourSecretKey);
|
|
98
117
|
}
|
|
99
118
|
/**
|