2021-07-13 18:54:53 +00:00
|
|
|
// Copyright 2015-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2021-07-13 18:54:53 +00:00
|
|
|
import * as Curve from '../Curve';
|
2021-09-24 00:49:05 +00:00
|
|
|
import {
|
|
|
|
PaddedLengths,
|
|
|
|
encryptProfileItemWithPadding,
|
|
|
|
decryptProfileName,
|
|
|
|
encryptProfile,
|
|
|
|
decryptProfile,
|
|
|
|
getRandomBytes,
|
|
|
|
constantTimeEqual,
|
|
|
|
generateRegistrationId,
|
|
|
|
deriveSecrets,
|
|
|
|
encryptDeviceName,
|
|
|
|
decryptDeviceName,
|
|
|
|
deriveAccessKey,
|
|
|
|
getAccessKeyVerifier,
|
|
|
|
verifyAccessKey,
|
|
|
|
deriveMasterKeyFromGroupV1,
|
|
|
|
encryptSymmetric,
|
|
|
|
decryptSymmetric,
|
|
|
|
hmacSha256,
|
|
|
|
verifyHmacSha256,
|
|
|
|
uuidToBytes,
|
|
|
|
bytesToUuid,
|
|
|
|
} from '../Crypto';
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
describe('Crypto', () => {
|
|
|
|
describe('encrypting and decrypting profile data', () => {
|
|
|
|
const NAME_PADDED_LENGTH = 53;
|
|
|
|
describe('encrypting and decrypting profile names', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
it('pads, encrypts, decrypts, and unpads a short string', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const name = 'Alice';
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = Bytes.fromString(name);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfileItemWithPadding(
|
2021-07-19 19:26:06 +00:00
|
|
|
buffer,
|
|
|
|
key,
|
|
|
|
PaddedLengths.Name
|
|
|
|
);
|
|
|
|
assert.equal(encrypted.byteLength, NAME_PADDED_LENGTH + 16 + 12);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const { given, family } = decryptProfileName(
|
|
|
|
Bytes.toBase64(encrypted),
|
2021-07-13 18:54:53 +00:00
|
|
|
key
|
|
|
|
);
|
|
|
|
assert.strictEqual(family, null);
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.strictEqual(Bytes.toString(given), name);
|
2021-07-13 18:54:53 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('handles a given name of the max, 53 characters', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const name = '01234567890123456789012345678901234567890123456789123';
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = Bytes.fromString(name);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfileItemWithPadding(
|
2021-07-19 19:26:06 +00:00
|
|
|
buffer,
|
|
|
|
key,
|
|
|
|
PaddedLengths.Name
|
|
|
|
);
|
|
|
|
assert.equal(encrypted.byteLength, NAME_PADDED_LENGTH + 16 + 12);
|
2021-09-24 00:49:05 +00:00
|
|
|
const { given, family } = decryptProfileName(
|
|
|
|
Bytes.toBase64(encrypted),
|
2021-07-13 18:54:53 +00:00
|
|
|
key
|
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.strictEqual(Bytes.toString(given), name);
|
2021-07-13 18:54:53 +00:00
|
|
|
assert.strictEqual(family, null);
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('handles family/given name of the max, 53 characters', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const name =
|
|
|
|
'01234567890123456789\u000001234567890123456789012345678912';
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = Bytes.fromString(name);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfileItemWithPadding(
|
2021-07-19 19:26:06 +00:00
|
|
|
buffer,
|
|
|
|
key,
|
|
|
|
PaddedLengths.Name
|
|
|
|
);
|
|
|
|
assert.equal(encrypted.byteLength, NAME_PADDED_LENGTH + 16 + 12);
|
2021-09-24 00:49:05 +00:00
|
|
|
const { given, family } = decryptProfileName(
|
|
|
|
Bytes.toBase64(encrypted),
|
2021-07-13 18:54:53 +00:00
|
|
|
key
|
|
|
|
);
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.strictEqual(Bytes.toString(given), '01234567890123456789');
|
2021-07-13 18:54:53 +00:00
|
|
|
assert.strictEqual(
|
2021-09-24 00:49:05 +00:00
|
|
|
family && Bytes.toString(family),
|
2021-07-13 18:54:53 +00:00
|
|
|
'01234567890123456789012345678912'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('handles a string with family/given name', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const name = 'Alice\0Jones';
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = Bytes.fromString(name);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfileItemWithPadding(
|
2021-07-19 19:26:06 +00:00
|
|
|
buffer,
|
|
|
|
key,
|
|
|
|
PaddedLengths.Name
|
|
|
|
);
|
|
|
|
assert.equal(encrypted.byteLength, NAME_PADDED_LENGTH + 16 + 12);
|
2021-09-24 00:49:05 +00:00
|
|
|
const { given, family } = decryptProfileName(
|
|
|
|
Bytes.toBase64(encrypted),
|
2021-07-13 18:54:53 +00:00
|
|
|
key
|
|
|
|
);
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.strictEqual(Bytes.toString(given), 'Alice');
|
|
|
|
assert.strictEqual(family && Bytes.toString(family), 'Jones');
|
2021-07-13 18:54:53 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('works for empty string', () => {
|
|
|
|
const name = Bytes.fromString('');
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfileItemWithPadding(
|
2021-07-19 19:26:06 +00:00
|
|
|
name,
|
|
|
|
key,
|
|
|
|
PaddedLengths.Name
|
|
|
|
);
|
|
|
|
assert.equal(encrypted.byteLength, NAME_PADDED_LENGTH + 16 + 12);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const { given, family } = decryptProfileName(
|
|
|
|
Bytes.toBase64(encrypted),
|
2021-07-13 18:54:53 +00:00
|
|
|
key
|
|
|
|
);
|
|
|
|
assert.strictEqual(family, null);
|
|
|
|
assert.strictEqual(given.byteLength, 0);
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.strictEqual(Bytes.toString(given), '');
|
2021-07-13 18:54:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('encrypting and decrypting profile avatars', () => {
|
|
|
|
it('encrypts and decrypts', async () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = Bytes.fromString('This is an avatar');
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfile(buffer, key);
|
2021-07-13 18:54:53 +00:00
|
|
|
assert(encrypted.byteLength === buffer.byteLength + 16 + 12);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const decrypted = decryptProfile(encrypted, key);
|
|
|
|
assert(constantTimeEqual(buffer, decrypted));
|
2021-07-13 18:54:53 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('throws when decrypting with the wrong key', () => {
|
|
|
|
const buffer = Bytes.fromString('This is an avatar');
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
const badKey = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptProfile(buffer, key);
|
2021-07-13 18:54:53 +00:00
|
|
|
assert(encrypted.byteLength === buffer.byteLength + 16 + 12);
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.throws(
|
|
|
|
() => decryptProfile(encrypted, badKey),
|
2021-07-13 18:54:53 +00:00
|
|
|
'Failed to decrypt profile data. Most likely the profile key has changed.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('generateRegistrationId', () => {
|
|
|
|
it('generates an integer between 0 and 16383 (inclusive)', () => {
|
|
|
|
for (let i = 0; i < 100; i += 1) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const id = generateRegistrationId();
|
2021-07-13 18:54:53 +00:00
|
|
|
assert.isAtLeast(id, 0);
|
|
|
|
assert.isAtMost(id, 16383);
|
|
|
|
assert(Number.isInteger(id));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deriveSecrets', () => {
|
|
|
|
it('derives key parts via HKDF', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
const input = getRandomBytes(32);
|
|
|
|
const salt = getRandomBytes(32);
|
|
|
|
const info = Bytes.fromString('Hello world');
|
|
|
|
const result = deriveSecrets(input, salt, info);
|
2021-07-13 18:54:53 +00:00
|
|
|
assert.lengthOf(result, 3);
|
|
|
|
result.forEach(part => {
|
|
|
|
// This is a smoke test; HKDF is tested as part of @signalapp/signal-client.
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.instanceOf(part, Uint8Array);
|
2021-07-13 18:54:53 +00:00
|
|
|
assert.strictEqual(part.byteLength, 32);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('accessKey/profileKey', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
it('verification roundtrips', () => {
|
|
|
|
const profileKey = getRandomBytes(32);
|
|
|
|
const accessKey = deriveAccessKey(profileKey);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const verifier = getAccessKeyVerifier(accessKey);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const correct = verifyAccessKey(accessKey, verifier);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
assert.strictEqual(correct, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deriveMasterKeyFromGroupV1', () => {
|
|
|
|
const vectors = [
|
|
|
|
{
|
|
|
|
gv1: '00000000000000000000000000000000',
|
|
|
|
masterKey:
|
|
|
|
'dbde68f4ee9169081f8814eabc65523fea1359235c8cfca32b69e31dce58b039',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
gv1: '000102030405060708090a0b0c0d0e0f',
|
|
|
|
masterKey:
|
|
|
|
'70884f78f07a94480ee36b67a4b5e975e92e4a774561e3df84c9076e3be4b9bf',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
gv1: '7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f',
|
|
|
|
masterKey:
|
|
|
|
'e69bf7c183b288b4ea5745b7c52b651a61e57769fafde683a6fdf1240f1905f2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
gv1: 'ffffffffffffffffffffffffffffffff',
|
|
|
|
masterKey:
|
|
|
|
'dd3a7de23d10f18b64457fbeedc76226c112a730e4b76112e62c36c4432eb37d',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
vectors.forEach((vector, index) => {
|
2021-09-24 00:49:05 +00:00
|
|
|
it(`vector ${index}`, () => {
|
|
|
|
const gv1 = Bytes.fromHex(vector.gv1);
|
2021-07-13 18:54:53 +00:00
|
|
|
const expectedHex = vector.masterKey;
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const actual = deriveMasterKeyFromGroupV1(gv1);
|
|
|
|
const actualHex = Bytes.toHex(actual);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
assert.strictEqual(actualHex, expectedHex);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('symmetric encryption', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
it('roundtrips', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const message = 'this is my message';
|
2021-09-24 00:49:05 +00:00
|
|
|
const plaintext = Bytes.fromString(message);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptSymmetric(key, plaintext);
|
|
|
|
const decrypted = decryptSymmetric(key, encrypted);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const equal = constantTimeEqual(plaintext, decrypted);
|
2021-07-13 18:54:53 +00:00
|
|
|
if (!equal) {
|
|
|
|
throw new Error('The output and input did not match!');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('roundtrip fails if nonce is modified', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const message = 'this is my message';
|
2021-09-24 00:49:05 +00:00
|
|
|
const plaintext = Bytes.fromString(message);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptSymmetric(key, plaintext);
|
|
|
|
encrypted[2] += 2;
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
decryptSymmetric(key, encrypted);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (error) {
|
|
|
|
assert.strictEqual(
|
|
|
|
error.message,
|
|
|
|
'decryptSymmetric: Failed to decrypt; MAC verification failed'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Expected error to be thrown');
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('roundtrip fails if mac is modified', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const message = 'this is my message';
|
2021-09-24 00:49:05 +00:00
|
|
|
const plaintext = Bytes.fromString(message);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptSymmetric(key, plaintext);
|
|
|
|
encrypted[encrypted.length - 3] += 2;
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
decryptSymmetric(key, encrypted);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (error) {
|
|
|
|
assert.strictEqual(
|
|
|
|
error.message,
|
|
|
|
'decryptSymmetric: Failed to decrypt; MAC verification failed'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Expected error to be thrown');
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('roundtrip fails if encrypted contents are modified', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const message = 'this is my message';
|
2021-09-24 00:49:05 +00:00
|
|
|
const plaintext = Bytes.fromString(message);
|
|
|
|
const key = getRandomBytes(32);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptSymmetric(key, plaintext);
|
|
|
|
encrypted[35] += 9;
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
decryptSymmetric(key, encrypted);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (error) {
|
|
|
|
assert.strictEqual(
|
|
|
|
error.message,
|
|
|
|
'decryptSymmetric: Failed to decrypt; MAC verification failed'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Expected error to be thrown');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('encrypted device name', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
it('roundtrips', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const deviceName = 'v1.19.0 on Windows 10';
|
|
|
|
const identityKey = Curve.generateKeyPair();
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptDeviceName(deviceName, identityKey.pubKey);
|
|
|
|
const decrypted = decryptDeviceName(encrypted, identityKey.privKey);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
assert.strictEqual(decrypted, deviceName);
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('fails if iv is changed', () => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const deviceName = 'v1.19.0 on Windows 10';
|
|
|
|
const identityKey = Curve.generateKeyPair();
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encrypted = encryptDeviceName(deviceName, identityKey.pubKey);
|
|
|
|
encrypted.syntheticIv = getRandomBytes(16);
|
2021-07-13 18:54:53 +00:00
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
decryptDeviceName(encrypted, identityKey.privKey);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (error) {
|
|
|
|
assert.strictEqual(
|
|
|
|
error.message,
|
|
|
|
'decryptDeviceName: synthetic IV did not match'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('verifyHmacSha256', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
it('rejects if their MAC is too short', () => {
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
const plaintext = Bytes.fromString('Hello world');
|
|
|
|
const ourMac = hmacSha256(key, plaintext);
|
2021-07-13 18:54:53 +00:00
|
|
|
const theirMac = ourMac.slice(0, -1);
|
|
|
|
let error;
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
verifyHmacSha256(plaintext, key, theirMac, ourMac.byteLength);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
assert.instanceOf(error, Error);
|
|
|
|
assert.strictEqual(error.message, 'Bad MAC length');
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('rejects if their MAC is too long', () => {
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
const plaintext = Bytes.fromString('Hello world');
|
|
|
|
const ourMac = hmacSha256(key, plaintext);
|
|
|
|
const theirMac = Bytes.concatenate([ourMac, new Uint8Array([0xff])]);
|
2021-07-13 18:54:53 +00:00
|
|
|
let error;
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
verifyHmacSha256(plaintext, key, theirMac, ourMac.byteLength);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
assert.instanceOf(error, Error);
|
|
|
|
assert.strictEqual(error.message, 'Bad MAC length');
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('rejects if our MAC is shorter than the specified length', () => {
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
const plaintext = Bytes.fromString('Hello world');
|
|
|
|
const ourMac = hmacSha256(key, plaintext);
|
2021-07-13 18:54:53 +00:00
|
|
|
const theirMac = ourMac;
|
|
|
|
let error;
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
verifyHmacSha256(plaintext, key, theirMac, ourMac.byteLength + 1);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
assert.instanceOf(error, Error);
|
|
|
|
assert.strictEqual(error.message, 'Bad MAC length');
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it("rejects if the MACs don't match", () => {
|
|
|
|
const plaintext = Bytes.fromString('Hello world');
|
|
|
|
const ourKey = getRandomBytes(32);
|
|
|
|
const ourMac = hmacSha256(ourKey, plaintext);
|
|
|
|
const theirKey = getRandomBytes(32);
|
|
|
|
const theirMac = hmacSha256(theirKey, plaintext);
|
2021-07-13 18:54:53 +00:00
|
|
|
let error;
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
verifyHmacSha256(plaintext, ourKey, theirMac, ourMac.byteLength);
|
2021-07-13 18:54:53 +00:00
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
assert.instanceOf(error, Error);
|
|
|
|
assert.strictEqual(error.message, 'Bad MAC');
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('resolves with undefined if the MACs match exactly', () => {
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
const plaintext = Bytes.fromString('Hello world');
|
|
|
|
const theirMac = hmacSha256(key, plaintext);
|
|
|
|
const result = verifyHmacSha256(
|
2021-07-13 18:54:53 +00:00
|
|
|
plaintext,
|
|
|
|
key,
|
|
|
|
theirMac,
|
|
|
|
theirMac.byteLength
|
|
|
|
);
|
|
|
|
assert.isUndefined(result);
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('resolves with undefined if the first `length` bytes of the MACs match', () => {
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
const plaintext = Bytes.fromString('Hello world');
|
|
|
|
const theirMac = hmacSha256(key, plaintext).slice(0, -5);
|
|
|
|
const result = verifyHmacSha256(
|
2021-07-13 18:54:53 +00:00
|
|
|
plaintext,
|
|
|
|
key,
|
|
|
|
theirMac,
|
|
|
|
theirMac.byteLength
|
|
|
|
);
|
|
|
|
assert.isUndefined(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
describe('uuidToBytes', () => {
|
|
|
|
it('converts valid UUIDs to Uint8Arrays', () => {
|
|
|
|
const expectedResult = new Uint8Array([
|
|
|
|
0x22,
|
|
|
|
0x6e,
|
|
|
|
0x44,
|
|
|
|
0x02,
|
|
|
|
0x7f,
|
|
|
|
0xfc,
|
|
|
|
0x45,
|
|
|
|
0x43,
|
|
|
|
0x85,
|
|
|
|
0xc9,
|
|
|
|
0x46,
|
|
|
|
0x22,
|
|
|
|
0xc5,
|
|
|
|
0x0a,
|
|
|
|
0x5b,
|
|
|
|
0x14,
|
|
|
|
]);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
assert.deepEqual(
|
2021-09-24 00:49:05 +00:00
|
|
|
uuidToBytes('226e4402-7ffc-4543-85c9-4622c50a5b14'),
|
2021-07-13 18:54:53 +00:00
|
|
|
expectedResult
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2021-09-24 00:49:05 +00:00
|
|
|
uuidToBytes('226E4402-7FFC-4543-85C9-4622C50A5B14'),
|
2021-07-13 18:54:53 +00:00
|
|
|
expectedResult
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('returns an empty Uint8Array for strings of the wrong length', () => {
|
|
|
|
assert.deepEqual(uuidToBytes(''), new Uint8Array(0));
|
|
|
|
assert.deepEqual(uuidToBytes('abc'), new Uint8Array(0));
|
2021-07-13 18:54:53 +00:00
|
|
|
assert.deepEqual(
|
2021-09-24 00:49:05 +00:00
|
|
|
uuidToBytes('032deadf0d5e4ee78da28e75b1dfb284'),
|
|
|
|
new Uint8Array(0)
|
2021-07-13 18:54:53 +00:00
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2021-09-24 00:49:05 +00:00
|
|
|
uuidToBytes('deaed5eb-d983-456a-a954-9ad7a006b271aaaaaaaaaa'),
|
|
|
|
new Uint8Array(0)
|
2021-07-13 18:54:53 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
describe('bytesToUuid', () => {
|
|
|
|
it('converts valid Uint8Arrays to UUID strings', () => {
|
|
|
|
const buf = new Uint8Array([
|
|
|
|
0x22,
|
|
|
|
0x6e,
|
|
|
|
0x44,
|
|
|
|
0x02,
|
|
|
|
0x7f,
|
|
|
|
0xfc,
|
|
|
|
0x45,
|
|
|
|
0x43,
|
|
|
|
0x85,
|
|
|
|
0xc9,
|
|
|
|
0x46,
|
|
|
|
0x22,
|
|
|
|
0xc5,
|
|
|
|
0x0a,
|
|
|
|
0x5b,
|
|
|
|
0x14,
|
|
|
|
]);
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
assert.deepEqual(
|
2021-09-24 00:49:05 +00:00
|
|
|
bytesToUuid(buf),
|
2021-07-13 18:54:53 +00:00
|
|
|
'226e4402-7ffc-4543-85c9-4622c50a5b14'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined if passed an all-zero buffer', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.isUndefined(bytesToUuid(new Uint8Array(16)));
|
2021-07-13 18:54:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined if passed the wrong number of bytes', () => {
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.isUndefined(bytesToUuid(new Uint8Array(0)));
|
|
|
|
assert.isUndefined(bytesToUuid(new Uint8Array([0x22])));
|
|
|
|
assert.isUndefined(bytesToUuid(new Uint8Array(Array(17).fill(0x22))));
|
2021-07-13 18:54:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|