signal-desktop/ts/util/zkgroup.ts

259 lines
7.1 KiB
TypeScript
Raw Normal View History

// Copyright 2020-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { ProfileKeyCredentialRequestContext } from '@signalapp/signal-client/zkgroup';
import {
2020-09-09 02:25:05 +00:00
AuthCredential,
ClientZkAuthOperations,
ClientZkGroupCipher,
ClientZkProfileOperations,
2020-09-09 02:25:05 +00:00
GroupMasterKey,
GroupSecretParams,
ProfileKey,
2020-09-09 02:25:05 +00:00
ProfileKeyCiphertext,
2020-10-06 17:06:34 +00:00
ProfileKeyCredential,
2020-09-09 02:25:05 +00:00
ProfileKeyCredentialPresentation,
ProfileKeyCredentialResponse,
ServerPublicParams,
2020-09-09 02:25:05 +00:00
UuidCiphertext,
} from '@signalapp/signal-client/zkgroup';
2021-10-26 22:59:08 +00:00
import { UUID } from '../types/UUID';
import type { UUIDStringType } from '../types/UUID';
export * from '@signalapp/signal-client/zkgroup';
2020-09-09 02:25:05 +00:00
// Scenarios
export function decryptGroupBlob(
clientZkGroupCipher: ClientZkGroupCipher,
2021-06-22 14:46:42 +00:00
ciphertext: Uint8Array
): Uint8Array {
return clientZkGroupCipher.decryptBlob(Buffer.from(ciphertext));
2020-09-09 02:25:05 +00:00
}
export function decryptProfileKeyCredentialPresentation(
clientZkGroupCipher: ClientZkGroupCipher,
2021-06-22 14:46:42 +00:00
presentationBuffer: Uint8Array
2021-10-26 22:59:08 +00:00
): { profileKey: Uint8Array; uuid: UUIDStringType } {
2020-09-09 02:25:05 +00:00
const presentation = new ProfileKeyCredentialPresentation(
Buffer.from(presentationBuffer)
2020-09-09 02:25:05 +00:00
);
const uuidCiphertext = presentation.getUuidCiphertext();
const uuid = clientZkGroupCipher.decryptUuid(uuidCiphertext);
const profileKeyCiphertext = presentation.getProfileKeyCiphertext();
const profileKey = clientZkGroupCipher.decryptProfileKey(
profileKeyCiphertext,
uuid
);
return {
profileKey: profileKey.serialize(),
2021-10-26 22:59:08 +00:00
uuid: UUID.cast(uuid),
2020-09-09 02:25:05 +00:00
};
}
export function decryptProfileKey(
clientZkGroupCipher: ClientZkGroupCipher,
2021-06-22 14:46:42 +00:00
profileKeyCiphertextBuffer: Uint8Array,
2021-10-26 22:59:08 +00:00
uuid: UUIDStringType
2021-06-22 14:46:42 +00:00
): Uint8Array {
2020-09-09 02:25:05 +00:00
const profileKeyCiphertext = new ProfileKeyCiphertext(
Buffer.from(profileKeyCiphertextBuffer)
2020-09-09 02:25:05 +00:00
);
const profileKey = clientZkGroupCipher.decryptProfileKey(
profileKeyCiphertext,
uuid
);
return profileKey.serialize();
2020-09-09 02:25:05 +00:00
}
export function decryptUuid(
clientZkGroupCipher: ClientZkGroupCipher,
2021-06-22 14:46:42 +00:00
uuidCiphertextBuffer: Uint8Array
2020-09-09 02:25:05 +00:00
): string {
const uuidCiphertext = new UuidCiphertext(Buffer.from(uuidCiphertextBuffer));
2020-09-09 02:25:05 +00:00
return clientZkGroupCipher.decryptUuid(uuidCiphertext);
}
export function deriveProfileKeyVersion(
profileKeyBase64: string,
2021-10-26 22:59:08 +00:00
uuid: UUIDStringType
): string {
const profileKeyArray = Buffer.from(profileKeyBase64, 'base64');
const profileKey = new ProfileKey(profileKeyArray);
const profileKeyVersion = profileKey.getProfileKeyVersion(uuid);
return profileKeyVersion.toString();
}
export function deriveGroupPublicParams(
2021-06-22 14:46:42 +00:00
groupSecretParamsBuffer: Uint8Array
): Uint8Array {
2020-09-09 02:25:05 +00:00
const groupSecretParams = new GroupSecretParams(
Buffer.from(groupSecretParamsBuffer)
2020-09-09 02:25:05 +00:00
);
return groupSecretParams.getPublicParams().serialize();
2020-09-09 02:25:05 +00:00
}
2021-06-22 14:46:42 +00:00
export function deriveGroupID(groupSecretParamsBuffer: Uint8Array): Uint8Array {
2020-09-09 02:25:05 +00:00
const groupSecretParams = new GroupSecretParams(
Buffer.from(groupSecretParamsBuffer)
2020-09-09 02:25:05 +00:00
);
return groupSecretParams.getPublicParams().getGroupIdentifier().serialize();
2020-09-09 02:25:05 +00:00
}
export function deriveGroupSecretParams(
2021-06-22 14:46:42 +00:00
masterKeyBuffer: Uint8Array
): Uint8Array {
const masterKey = new GroupMasterKey(Buffer.from(masterKeyBuffer));
2020-09-09 02:25:05 +00:00
const groupSecretParams = GroupSecretParams.deriveFromMasterKey(masterKey);
return groupSecretParams.serialize();
2020-09-09 02:25:05 +00:00
}
export function encryptGroupBlob(
clientZkGroupCipher: ClientZkGroupCipher,
2021-06-22 14:46:42 +00:00
plaintext: Uint8Array
): Uint8Array {
return clientZkGroupCipher.encryptBlob(Buffer.from(plaintext));
2020-09-09 02:25:05 +00:00
}
export function encryptUuid(
clientZkGroupCipher: ClientZkGroupCipher,
2021-10-26 22:59:08 +00:00
uuidPlaintext: UUIDStringType
2021-06-22 14:46:42 +00:00
): Uint8Array {
2020-09-09 02:25:05 +00:00
const uuidCiphertext = clientZkGroupCipher.encryptUuid(uuidPlaintext);
return uuidCiphertext.serialize();
}
export function generateProfileKeyCredentialRequest(
clientZkProfileCipher: ClientZkProfileOperations,
2021-10-26 22:59:08 +00:00
uuid: UUIDStringType,
profileKeyBase64: string
): { context: ProfileKeyCredentialRequestContext; requestHex: string } {
const profileKeyArray = Buffer.from(profileKeyBase64, 'base64');
const profileKey = new ProfileKey(profileKeyArray);
2021-11-11 22:43:05 +00:00
const context =
clientZkProfileCipher.createProfileKeyCredentialRequestContext(
uuid,
profileKey
);
const request = context.getRequest();
const requestArray = request.serialize();
return {
context,
requestHex: requestArray.toString('hex'),
};
}
2020-09-09 02:25:05 +00:00
export function getAuthCredentialPresentation(
clientZkAuthOperations: ClientZkAuthOperations,
authCredentialBase64: string,
groupSecretParamsBase64: string
2021-06-22 14:46:42 +00:00
): Uint8Array {
2020-09-09 02:25:05 +00:00
const authCredential = new AuthCredential(
Buffer.from(authCredentialBase64, 'base64')
2020-09-09 02:25:05 +00:00
);
const secretParams = new GroupSecretParams(
Buffer.from(groupSecretParamsBase64, 'base64')
2020-09-09 02:25:05 +00:00
);
const presentation = clientZkAuthOperations.createAuthCredentialPresentation(
secretParams,
authCredential
);
return presentation.serialize();
2020-09-09 02:25:05 +00:00
}
2020-10-06 17:06:34 +00:00
export function createProfileKeyCredentialPresentation(
clientZkProfileCipher: ClientZkProfileOperations,
profileKeyCredentialBase64: string,
groupSecretParamsBase64: string
2021-06-22 14:46:42 +00:00
): Uint8Array {
const profileKeyCredentialArray = Buffer.from(
profileKeyCredentialBase64,
'base64'
2020-10-06 17:06:34 +00:00
);
const profileKeyCredential = new ProfileKeyCredential(
profileKeyCredentialArray
);
const secretParams = new GroupSecretParams(
Buffer.from(groupSecretParamsBase64, 'base64')
2020-10-06 17:06:34 +00:00
);
2021-11-11 22:43:05 +00:00
const presentation =
clientZkProfileCipher.createProfileKeyCredentialPresentation(
secretParams,
profileKeyCredential
);
2020-10-06 17:06:34 +00:00
return presentation.serialize();
2020-10-06 17:06:34 +00:00
}
2020-09-09 02:25:05 +00:00
export function getClientZkAuthOperations(
serverPublicParamsBase64: string
): ClientZkAuthOperations {
const serverPublicParams = new ServerPublicParams(
Buffer.from(serverPublicParamsBase64, 'base64')
2020-09-09 02:25:05 +00:00
);
return new ClientZkAuthOperations(serverPublicParams);
}
export function getClientZkGroupCipher(
groupSecretParamsBase64: string
): ClientZkGroupCipher {
const serverPublicParams = new GroupSecretParams(
Buffer.from(groupSecretParamsBase64, 'base64')
2020-09-09 02:25:05 +00:00
);
return new ClientZkGroupCipher(serverPublicParams);
}
export function getClientZkProfileOperations(
serverPublicParamsBase64: string
): ClientZkProfileOperations {
const serverPublicParams = new ServerPublicParams(
Buffer.from(serverPublicParamsBase64, 'base64')
2020-09-09 02:25:05 +00:00
);
return new ClientZkProfileOperations(serverPublicParams);
}
export function handleProfileKeyCredential(
clientZkProfileCipher: ClientZkProfileOperations,
context: ProfileKeyCredentialRequestContext,
responseBase64: string
): string {
2020-09-09 02:25:05 +00:00
const response = new ProfileKeyCredentialResponse(
Buffer.from(responseBase64, 'base64')
2020-09-09 02:25:05 +00:00
);
2021-11-11 22:43:05 +00:00
const profileKeyCredential =
clientZkProfileCipher.receiveProfileKeyCredential(context, response);
const credentialArray = profileKeyCredential.serialize();
return credentialArray.toString('base64');
}
2021-07-19 19:26:06 +00:00
export function deriveProfileKeyCommitment(
profileKeyBase64: string,
2021-10-26 22:59:08 +00:00
uuid: UUIDStringType
2021-07-19 19:26:06 +00:00
): string {
const profileKeyArray = Buffer.from(profileKeyBase64, 'base64');
2021-07-19 19:26:06 +00:00
const profileKey = new ProfileKey(profileKeyArray);
return profileKey.getCommitment(uuid).contents.toString('base64');
2021-07-19 19:26:06 +00:00
}