2021-11-11 15:24:28 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
import type { ProfileKeyCredentialRequestContext } from '@signalapp/signal-client/zkgroup';
|
2020-04-15 23:12:28 +00:00
|
|
|
import {
|
2020-09-09 02:25:05 +00:00
|
|
|
AuthCredential,
|
|
|
|
ClientZkAuthOperations,
|
|
|
|
ClientZkGroupCipher,
|
2020-04-15 23:12:28 +00:00
|
|
|
ClientZkProfileOperations,
|
2020-09-09 02:25:05 +00:00
|
|
|
GroupMasterKey,
|
|
|
|
GroupSecretParams,
|
2020-04-15 23:12:28 +00:00
|
|
|
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,
|
2020-04-15 23:12:28 +00:00
|
|
|
ProfileKeyCredentialResponse,
|
|
|
|
ServerPublicParams,
|
2020-09-09 02:25:05 +00:00
|
|
|
UuidCiphertext,
|
2021-11-11 15:24:28 +00:00
|
|
|
} from '@signalapp/signal-client/zkgroup';
|
2021-10-26 22:59:08 +00:00
|
|
|
import { UUID } from '../types/UUID';
|
|
|
|
import type { UUIDStringType } from '../types/UUID';
|
2020-04-15 23:12:28 +00:00
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
export * from '@signalapp/signal-client/zkgroup';
|
2020-04-15 23:12:28 +00:00
|
|
|
|
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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
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(
|
2021-11-11 15:24:28 +00:00
|
|
|
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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
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(
|
2021-11-11 15:24:28 +00:00
|
|
|
Buffer.from(profileKeyCiphertextBuffer)
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const profileKey = clientZkGroupCipher.decryptProfileKey(
|
|
|
|
profileKeyCiphertext,
|
|
|
|
uuid
|
|
|
|
);
|
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
const uuidCiphertext = new UuidCiphertext(Buffer.from(uuidCiphertextBuffer));
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
return clientZkGroupCipher.decryptUuid(uuidCiphertext);
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:12:28 +00:00
|
|
|
export function deriveProfileKeyVersion(
|
|
|
|
profileKeyBase64: string,
|
2021-10-26 22:59:08 +00:00
|
|
|
uuid: UUIDStringType
|
2020-09-14 21:56:35 +00:00
|
|
|
): string {
|
2021-11-11 15:24:28 +00:00
|
|
|
const profileKeyArray = Buffer.from(profileKeyBase64, 'base64');
|
2020-04-15 23:12:28 +00:00
|
|
|
const profileKey = new ProfileKey(profileKeyArray);
|
|
|
|
|
|
|
|
const profileKeyVersion = profileKey.getProfileKeyVersion(uuid);
|
|
|
|
|
|
|
|
return profileKeyVersion.toString();
|
|
|
|
}
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
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(
|
2021-11-11 15:24:28 +00:00
|
|
|
Buffer.from(groupSecretParamsBuffer)
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
2020-04-15 23:12:28 +00:00
|
|
|
|
2021-11-11 15:24:28 +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(
|
2021-11-11 15:24:28 +00:00
|
|
|
Buffer.from(groupSecretParamsBuffer)
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
|
|
|
|
2021-11-11 15:24:28 +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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
const masterKey = new GroupMasterKey(Buffer.from(masterKeyBuffer));
|
2020-09-09 02:25:05 +00:00
|
|
|
const groupSecretParams = GroupSecretParams.deriveFromMasterKey(masterKey);
|
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
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);
|
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
return uuidCiphertext.serialize();
|
2020-04-15 23:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function generateProfileKeyCredentialRequest(
|
|
|
|
clientZkProfileCipher: ClientZkProfileOperations,
|
2021-10-26 22:59:08 +00:00
|
|
|
uuid: UUIDStringType,
|
2020-04-15 23:12:28 +00:00
|
|
|
profileKeyBase64: string
|
|
|
|
): { context: ProfileKeyCredentialRequestContext; requestHex: string } {
|
2021-11-11 15:24:28 +00:00
|
|
|
const profileKeyArray = Buffer.from(profileKeyBase64, 'base64');
|
2020-04-15 23:12:28 +00:00
|
|
|
const profileKey = new ProfileKey(profileKeyArray);
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const context =
|
|
|
|
clientZkProfileCipher.createProfileKeyCredentialRequestContext(
|
|
|
|
uuid,
|
|
|
|
profileKey
|
|
|
|
);
|
2020-04-15 23:12:28 +00:00
|
|
|
const request = context.getRequest();
|
|
|
|
const requestArray = request.serialize();
|
|
|
|
|
|
|
|
return {
|
|
|
|
context,
|
2021-11-11 15:24:28 +00:00
|
|
|
requestHex: requestArray.toString('hex'),
|
2020-04-15 23:12:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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(
|
2021-11-11 15:24:28 +00:00
|
|
|
Buffer.from(authCredentialBase64, 'base64')
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
|
|
|
const secretParams = new GroupSecretParams(
|
2021-11-11 15:24:28 +00:00
|
|
|
Buffer.from(groupSecretParamsBase64, 'base64')
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const presentation = clientZkAuthOperations.createAuthCredentialPresentation(
|
|
|
|
secretParams,
|
|
|
|
authCredential
|
|
|
|
);
|
2021-11-11 15:24:28 +00:00
|
|
|
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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
const profileKeyCredentialArray = Buffer.from(
|
|
|
|
profileKeyCredentialBase64,
|
|
|
|
'base64'
|
2020-10-06 17:06:34 +00:00
|
|
|
);
|
|
|
|
const profileKeyCredential = new ProfileKeyCredential(
|
|
|
|
profileKeyCredentialArray
|
|
|
|
);
|
|
|
|
const secretParams = new GroupSecretParams(
|
2021-11-11 15:24:28 +00:00
|
|
|
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
|
|
|
|
2021-11-11 15:24:28 +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(
|
2021-11-11 15:24:28 +00:00
|
|
|
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(
|
2021-11-11 15:24:28 +00:00
|
|
|
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(
|
2021-11-11 15:24:28 +00:00
|
|
|
Buffer.from(serverPublicParamsBase64, 'base64')
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return new ClientZkProfileOperations(serverPublicParams);
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:12:28 +00:00
|
|
|
export function handleProfileKeyCredential(
|
|
|
|
clientZkProfileCipher: ClientZkProfileOperations,
|
|
|
|
context: ProfileKeyCredentialRequestContext,
|
|
|
|
responseBase64: string
|
|
|
|
): string {
|
2020-09-09 02:25:05 +00:00
|
|
|
const response = new ProfileKeyCredentialResponse(
|
2021-11-11 15:24:28 +00:00
|
|
|
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);
|
2020-04-15 23:12:28 +00:00
|
|
|
|
|
|
|
const credentialArray = profileKeyCredential.serialize();
|
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
return credentialArray.toString('base64');
|
2020-04-15 23:12:28 +00:00
|
|
|
}
|
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 {
|
2021-11-11 15:24:28 +00:00
|
|
|
const profileKeyArray = Buffer.from(profileKeyBase64, 'base64');
|
2021-07-19 19:26:06 +00:00
|
|
|
const profileKey = new ProfileKey(profileKeyArray);
|
|
|
|
|
2021-11-11 15:24:28 +00:00
|
|
|
return profileKey.getCommitment(uuid).contents.toString('base64');
|
2021-07-19 19:26:06 +00:00
|
|
|
}
|