signal-desktop/libtextsecure/test/crypto_test.js
Lilia ae190fed44
Profiles (#1453)
* Add AES-GCM encryption for profiles

With tests.

* Add profileKey to DataMessage protobuf

// FREEBIE

* Decrypt and save profile names

// FREEBIE

* Save incoming profile keys

* Move pad/unpad to crypto module

// FREEBIE

* Support fetching avatars from the cdn

// FREEBIE

* Translate failed authentication errors

When AES-GCM authentication fails, webcrypto returns a very generic error. The
same error is thrown for invalid length inputs, but our earlier checks in
decryptProfile should rule out those failure modes and leave us safe to assume
that we either had bad ciphertext or the wrong key.

// FREEBIE

* Handle profile avatars (wip) and log decrypt errors

// FREEBIE

* Display profile avatars

Synced contact avatars will still override profile avatars.

* Display profile names in convo list

Only if we don't have a synced contact name.

// FREEBIE

* Make cdn url an environment config

Use different ones for staging and production

// FREEBIE

* Display profile name in conversation header

* Display profile name in group messages

* Update conversation header if profile avatar changes

// FREEBIE

* Style profile names small with ~

* Save profileKeys from contact sync messages

// FREEBIE

* Save profile keys from provisioning messages

For standalone accounts, generate a random profile key.

// FREEBIE

* Special case for one-time sync of our profile key

Android will use a contact sync message to sync a profile key from Android
clients who have just upgraded and generated their profile key. Normally we
should receive this data in a provisioning message.

// FREEBIE

* Infer profile sharing from synced data messages

* Populate profile keys on outgoing messages

Requires that `profileSharing` be set on the conversation.

// FREEBIE

* Support for the profile key update flag

When receiving a message with this flag, don't init a message record, just
process the profile key and move on.

// FREEBIE

* Display profile names in group member list

* Refresh contact's profile on profile key changes

// FREEBIE

* Catch errors on profile save

// FREEBIE

* Save our own synced contact info

Don't return early if we get a contact sync for our own number

// FREEBIE
2017-09-14 17:04:00 -07:00

54 lines
2.5 KiB
JavaScript

describe('encrypting and decrypting profile data', function() {
var NAME_PADDED_LENGTH = 26;
describe('encrypting and decrypting profile names', function() {
it('pads, encrypts, decrypts, and unpads a short string', function() {
var name = 'Alice';
var buffer = dcodeIO.ByteBuffer.wrap(name).toArrayBuffer();
var key = libsignal.crypto.getRandomBytes(32);
return textsecure.crypto.encryptProfileName(buffer, key).then(function(encrypted) {
assert(encrypted.byteLength === NAME_PADDED_LENGTH + 16 + 12);
return textsecure.crypto.decryptProfileName(encrypted, key).then(function(decrypted) {
assert.strictEqual(dcodeIO.ByteBuffer.wrap(decrypted).toString('utf8'), 'Alice');
});
});
});
it('works for empty string', function() {
var name = dcodeIO.ByteBuffer.wrap('').toArrayBuffer();
var key = libsignal.crypto.getRandomBytes(32);
return textsecure.crypto.encryptProfileName(name.buffer, key).then(function(encrypted) {
assert(encrypted.byteLength === NAME_PADDED_LENGTH + 16 + 12);
return textsecure.crypto.decryptProfileName(encrypted, key).then(function(decrypted) {
assert.strictEqual(decrypted.byteLength, 0);
assert.strictEqual(dcodeIO.ByteBuffer.wrap(decrypted).toString('utf8'), '');
});
});
});
});
describe('encrypting and decrypting profile avatars', function() {
it('encrypts and decrypts', function() {
var buffer = dcodeIO.ByteBuffer.wrap('This is an avatar').toArrayBuffer();
var key = libsignal.crypto.getRandomBytes(32);
return textsecure.crypto.encryptProfile(buffer, key).then(function(encrypted) {
assert(encrypted.byteLength === buffer.byteLength + 16 + 12);
return textsecure.crypto.decryptProfile(encrypted, key).then(function(decrypted) {
assertEqualArrayBuffers(buffer, decrypted)
});
});
});
it('throws when decrypting with the wrong key', function() {
var buffer = dcodeIO.ByteBuffer.wrap('This is an avatar').toArrayBuffer();
var key = libsignal.crypto.getRandomBytes(32);
var bad_key = libsignal.crypto.getRandomBytes(32);
return textsecure.crypto.encryptProfile(buffer, key).then(function(encrypted) {
assert(encrypted.byteLength === buffer.byteLength + 16 + 12);
return textsecure.crypto.decryptProfile(encrypted, bad_key).catch(function(error) {
assert.strictEqual(error.name, 'ProfileDecryptError');
});
});
});
});
});