(feat): add function to easily split keyring
Some checks failed
CI / checks (push) Failing after 3m23s

This commit is contained in:
Alois 2026-07-05 11:59:35 +02:00
commit 13432c1ac2

View file

@ -37,13 +37,13 @@ export interface MTPCrypto {
hkdfExpand(ikm: Uint8Array, salt: Uint8Array, info: Uint8Array, len: number): Uint8Array; hkdfExpand(ikm: Uint8Array, salt: Uint8Array, info: Uint8Array, len: number): Uint8Array;
sha256(data: Uint8Array): Uint8Array; sha256(data: Uint8Array): Uint8Array;
sha256Double(data: Uint8Array): Uint8Array; sha256Double(data: Uint8Array): Uint8Array;
encrypt(secret: string, input: Uint8Array): Promise<Uint8Array>; keyringToKeys(keyring: string | MTPBytesInput): MTPKeyringKeys;
decrypt(secret: string, input: Uint8Array): Promise<Uint8Array>; encrypt(key: Uint8Array, input: Uint8Array): Promise<Uint8Array>;
encryptText(secret: string, plaintext: string): Promise<string>; decrypt(key: Uint8Array, input: Uint8Array): Promise<Uint8Array>;
decryptText(secret: string, ciphertext: string): Promise<string>; encryptText(key: Uint8Array, plaintext: string): Promise<string>;
decryptText(key: Uint8Array, ciphertext: string): Promise<string>;
encapsulate(otherPublicKey: Uint8Array): WasmEncapsulated; encapsulate(otherPublicKey: Uint8Array): WasmEncapsulated;
decapsulate(ownPrivateKey: Uint8Array, ciphertext: Uint8Array): Uint8Array; decapsulate(ownPrivateKey: Uint8Array, ciphertext: Uint8Array): Uint8Array;
getSharedSecret(ownPrivateKey: string, ownPublicKey: string, otherPublicKey: string): Promise<string>;
} }
export const crypto: MTPCrypto = { export const crypto: MTPCrypto = {
@ -55,9 +55,9 @@ export const crypto: MTPCrypto = {
hkdfExpand: (ikm, salt, info, len) => bindings.wasm_hkdf_expand(ikm, salt, info, len), hkdfExpand: (ikm, salt, info, len) => bindings.wasm_hkdf_expand(ikm, salt, info, len),
sha256: (data) => bindings.wasm_sha256(data), sha256: (data) => bindings.wasm_sha256(data),
sha256Double: (data) => bindings.wasm_sha256_double(data), sha256Double: (data) => bindings.wasm_sha256_double(data),
keyringToKeys: (keyring) => keyringToKeys(keyring),
encrypt: async (secret, input) => { encrypt: async (key, input) => {
const key = secretKeyFromString(secret);
const cipher = new bindings.WasmChaCha20Poly1305(key); const cipher = new bindings.WasmChaCha20Poly1305(key);
try { try {
return cipher.encrypt(input, new Uint8Array(0)); return cipher.encrypt(input, new Uint8Array(0));
@ -66,8 +66,7 @@ export const crypto: MTPCrypto = {
} }
}, },
decrypt: async (secret, input) => { decrypt: async (key, input) => {
const key = secretKeyFromString(secret);
const cipher = new bindings.WasmChaCha20Poly1305(key); const cipher = new bindings.WasmChaCha20Poly1305(key);
try { try {
return cipher.decrypt(input, new Uint8Array(0)); return cipher.decrypt(input, new Uint8Array(0));
@ -76,8 +75,7 @@ export const crypto: MTPCrypto = {
} }
}, },
encryptText: async (secret, plaintext) => { encryptText: async (key, plaintext) => {
const key = secretKeyFromString(secret);
const cipher = new bindings.WasmChaCha20Poly1305(key); const cipher = new bindings.WasmChaCha20Poly1305(key);
try { try {
const ciphertext = cipher.encrypt(utf8Encode(plaintext), new Uint8Array(0)); const ciphertext = cipher.encrypt(utf8Encode(plaintext), new Uint8Array(0));
@ -87,8 +85,7 @@ export const crypto: MTPCrypto = {
} }
}, },
decryptText: async (secret, ciphertext) => { decryptText: async (key, ciphertext) => {
const key = secretKeyFromString(secret);
const cipher = new bindings.WasmChaCha20Poly1305(key); const cipher = new bindings.WasmChaCha20Poly1305(key);
try { try {
const decoded = bytesFromString(ciphertext, "ciphertext"); const decoded = bytesFromString(ciphertext, "ciphertext");
@ -103,19 +100,6 @@ export const crypto: MTPCrypto = {
decapsulate: (ownPrivateKey, ciphertext) => decapsulate: (ownPrivateKey, ciphertext) =>
bindings.wasm_kem_decapsulate(ownPrivateKey, ciphertext), bindings.wasm_kem_decapsulate(ownPrivateKey, ciphertext),
getSharedSecret: async (ownPrivateKey, ownPublicKey, otherPublicKey) => {
const ownPub = bytesFromString(ownPublicKey, "ownPublicKey");
const otherPub = bytesFromString(otherPublicKey, "otherPublicKey");
const enc = bindings.wasm_kem_encapsulate(otherPub);
try {
const sharedSecret = enc.shared_secret;
const derived = bindings.wasm_hkdf_expand(sharedSecret, ownPub, otherPub, 32);
return bytesToHex(derived);
} finally {
enc.free();
}
},
}; };
export type MTPRawBindings = typeof bindings; export type MTPRawBindings = typeof bindings;
@ -191,6 +175,15 @@ export interface MTPClientCredentials {
hostPublicKey?: Uint8Array; hostPublicKey?: Uint8Array;
} }
export interface MTPKeyringKeys {
kemPublicKey: Uint8Array;
kemSecretKey: Uint8Array;
sigPqPublicKey: Uint8Array;
sigPqSecretKey: Uint8Array;
sigClPublicKey: Uint8Array;
sigClSecretKey: Uint8Array;
}
export interface MTPClientOptions { export interface MTPClientOptions {
url: string; url: string;
descriptor?: string; descriptor?: string;
@ -330,7 +323,7 @@ function bytesToHex(bytes) {
return out; return out;
} }
function bytesToBase64(bytes) { export function bytesToBase64(bytes) {
if (typeof btoa === "function") { if (typeof btoa === "function") {
let binary = ""; let binary = "";
for (let i = 0; i < bytes.length; i += 1) { for (let i = 0; i < bytes.length; i += 1) {
@ -344,6 +337,21 @@ function bytesToBase64(bytes) {
throw new TypeError("base64 encoding is not available in this environment"); throw new TypeError("base64 encoding is not available in this environment");
} }
export function base64ToBytes(input) {
if (typeof atob === "function") {
const binary = atob(input);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i += 1) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
if (typeof Buffer !== "undefined") {
return new Uint8Array(Buffer.from(input, "base64"));
}
throw new TypeError("base64 decoding is not available in this environment");
}
function utf8Encode(text) { function utf8Encode(text) {
if (typeof TextEncoder !== "undefined") { if (typeof TextEncoder !== "undefined") {
return new TextEncoder().encode(text); return new TextEncoder().encode(text);
@ -414,7 +422,7 @@ function utf8Decode(bytes) {
const SYMMETRIC_KEY_SALT = utf8Encode("mtp-symmetric-key"); const SYMMETRIC_KEY_SALT = utf8Encode("mtp-symmetric-key");
function secretKeyFromString(secret) { export function secretKeyFromString(secret) {
if (typeof secret !== "string" || !secret.trim()) { if (typeof secret !== "string" || !secret.trim()) {
throw new TypeError("secret must be a non-empty string"); throw new TypeError("secret must be a non-empty string");
} }
@ -474,6 +482,34 @@ function generateKeyringBytes() {
return keyring_generate(); return keyring_generate();
} }
export function keyringToKeys(keyring) {
const bytes = typeof keyring === "string"
? bytesFromString(keyring, "keyring")
: bytesFrom(keyring, "keyring");
if (bytes.length < 12) {
throw new TypeError("keyring data is too short to contain 6 keys");
}
let offset = 0;
const readKey = () => {
const len = (bytes[offset] << 8) | bytes[offset + 1];
offset += 2;
const key = bytes.slice(offset, offset + len);
offset += len;
return key;
};
return {
kemPublicKey: readKey(),
kemSecretKey: readKey(),
sigPqPublicKey: readKey(),
sigPqSecretKey: readKey(),
sigClPublicKey: readKey(),
sigClSecretKey: readKey(),
};
}
function serializeCredentials(credentials) { function serializeCredentials(credentials) {
return JSON.stringify({ return JSON.stringify({
clientId: credentials.clientId?.toString() ?? null, clientId: credentials.clientId?.toString() ?? null,