Skip to content

Commit

Permalink
Use AEAD protection by default (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
larabr authored and twiss committed Aug 20, 2020
1 parent dff1e4a commit 76e7dc1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 67 deletions.
5 changes: 2 additions & 3 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ export default {

/**
* Use Authenticated Encryption with Additional Data (AEAD) protection for symmetric encryption.
* **NOT INTEROPERABLE WITH OTHER OPENPGP IMPLEMENTATIONS**
* **FUTURE OPENPGP.JS VERSIONS MAY BREAK COMPATIBILITY WHEN USING THIS OPTION**
* Note: not all OpenPGP implementations are compatible with this option.
* @see {@link https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-07|RFC4880bis-07}
* @memberof module:config
* @property {Boolean} aead_protect
*/
aead_protect: false,
aead_protect: true,
/**
* Default Authenticated Encryption with Additional Data (AEAD) encryption mode
* Only has an effect when aead_protect is set to true.
Expand Down
22 changes: 20 additions & 2 deletions test/general/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,16 @@ function versionSpecificTests() {
expect(key.users[0].selfCertifications[0].preferredHashAlgorithms).to.eql([hash.sha256, hash.sha512, hash.sha1]);
const compr = openpgp.enums.compression;
expect(key.users[0].selfCertifications[0].preferredCompressionAlgorithms).to.eql([compr.zlib, compr.zip, compr.uncompressed]);
expect(key.users[0].selfCertifications[0].features).to.eql(openpgp.config.v5_keys ? [7] : [1]);

let expectedFeatures;
if (openpgp.config.v5_keys) {
expectedFeatures = [7]; // v5 + aead + mdc
} else if (openpgp.config.aead_protect) {
expectedFeatures = [3]; // aead + mdc
} else {
expectedFeatures = [1]; // mdc
}
expect(key.users[0].selfCertifications[0].features).to.eql(expectedFeatures);
};
const opt = {numBits: 512, userIds: 'test <a@b.com>', passphrase: 'hello'};
if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
Expand Down Expand Up @@ -1977,7 +1986,16 @@ function versionSpecificTests() {
expect(key.users[0].selfCertifications[0].preferredHashAlgorithms).to.eql([hash.sha224, hash.sha256, hash.sha512, hash.sha1]);
const compr = openpgp.enums.compression;
expect(key.users[0].selfCertifications[0].preferredCompressionAlgorithms).to.eql([compr.zlib, compr.zip, compr.uncompressed]);
expect(key.users[0].selfCertifications[0].features).to.eql(openpgp.config.v5_keys ? [7] : [1]);

let expectedFeatures;
if (openpgp.config.v5_keys) {
expectedFeatures = [7]; // v5 + aead + mdc
} else if (openpgp.config.aead_protect) {
expectedFeatures = [3]; // aead + mdc
} else {
expectedFeatures = [1]; // mdc
}
expect(key.users[0].selfCertifications[0].features).to.eql(expectedFeatures);
};
const opt = {numBits: 512, userIds: 'test <a@b.com>', passphrase: 'hello'};
if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
Expand Down
112 changes: 50 additions & 62 deletions test/general/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ function tests() {
});

it('Encrypt and decrypt larger message roundtrip', async function() {
let aead_protectValue = openpgp.config.aead_protect;
openpgp.config.aead_protect = false;
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
Expand All @@ -253,10 +255,13 @@ function tests() {
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
if (i <= 10) throw new Error('Data arrived early.');
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
openpgp.config.aead_protect = aead_protectValue;
});

it('Encrypt and decrypt larger message roundtrip (allow_unauthenticated_stream=true)', async function() {
let aead_protectValue = openpgp.config.aead_protect;
let allow_unauthenticated_streamValue = openpgp.config.allow_unauthenticated_stream;
openpgp.config.aead_protect = false;
openpgp.config.allow_unauthenticated_stream = true;
try {
const encrypted = await openpgp.encrypt({
Expand All @@ -280,6 +285,7 @@ function tests() {
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
expect(decrypted.signatures).to.exist.and.have.length(0);
} finally {
openpgp.config.aead_protect = aead_protectValue;
openpgp.config.allow_unauthenticated_stream = allow_unauthenticated_streamValue;
}
});
Expand Down Expand Up @@ -378,6 +384,8 @@ function tests() {
});

it('Detect MDC modifications (allow_unauthenticated_stream=true)', async function() {
let aead_protectValue = openpgp.config.aead_protect;
openpgp.config.aead_protect = false;
let allow_unauthenticated_streamValue = openpgp.config.allow_unauthenticated_stream;
openpgp.config.allow_unauthenticated_stream = true;
try {
Expand Down Expand Up @@ -407,6 +415,7 @@ function tests() {
await expect(reader.readToEnd()).to.be.rejectedWith('Modification detected.');
expect(decrypted.signatures).to.exist.and.have.length(0);
} finally {
openpgp.config.aead_protect = aead_protectValue;
openpgp.config.allow_unauthenticated_stream = allow_unauthenticated_streamValue;
}
});
Expand Down Expand Up @@ -509,40 +518,28 @@ function tests() {
});

it('Encrypt and decrypt larger message roundtrip (AEAD)', async function() {
let aead_protectValue = openpgp.config.aead_protect;
let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4;

try {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
armor: false
});
expect(util.isStream(encrypted)).to.equal(expectedType);
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
armor: false
});
expect(util.isStream(encrypted)).to.equal(expectedType);

const message = await openpgp.message.read(encrypted);
const decrypted = await openpgp.decrypt({
passwords: ['test'],
message,
format: 'binary'
});
expect(util.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
} finally {
openpgp.config.aead_protect = aead_protectValue;
openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteValue;
}
const message = await openpgp.message.read(encrypted);
const decrypted = await openpgp.decrypt({
passwords: ['test'],
message,
format: 'binary'
});
expect(util.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
});

it('Encrypt and decrypt larger text message roundtrip (AEAD)', async function() {
let aead_protectValue = openpgp.config.aead_protect;
let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 0;
try {
let plaintext = [];
Expand Down Expand Up @@ -577,7 +574,6 @@ function tests() {
dataArrived();
expect((await reader.readToEnd()).toString('utf8')).to.equal(util.concat(plaintext));
} finally {
openpgp.config.aead_protect = aead_protectValue;
openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteValue;
}
});
Expand Down Expand Up @@ -608,33 +604,24 @@ function tests() {
});

it('Input stream should be canceled when canceling decrypted stream (AEAD)', async function() {
const aead_protectValue = openpgp.config.aead_protect;
const aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4;
try {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
});
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
});

const message = await openpgp.message.readArmored(encrypted);
const decrypted = await openpgp.decrypt({
passwords: ['test'],
message,
format: 'binary'
});
expect(util.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data);
expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived();
reader.releaseLock();
await openpgp.stream.cancel(decrypted.data, new Error('canceled by test'));
expect(canceled).to.be.true;
} finally {
openpgp.config.aead_protect = aead_protectValue;
openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteValue;
}
const message = await openpgp.message.readArmored(encrypted);
const decrypted = await openpgp.decrypt({
passwords: ['test'],
message,
format: 'binary'
});
expect(util.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data);
expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived();
reader.releaseLock();
await openpgp.stream.cancel(decrypted.data, new Error('canceled by test'));
expect(canceled).to.be.true;
});

it('Sign/verify: Input stream should be canceled when canceling verified stream', async function() {
Expand Down Expand Up @@ -690,10 +677,6 @@ function tests() {
});

it("Don't pull entire input stream when we're not pulling decrypted stream (AEAD)", async function() {
let aead_protectValue = openpgp.config.aead_protect;
let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4;
let coresStub = stub(openpgp.util, 'getHardwareConcurrency');
coresStub.returns(1);
try {
Expand All @@ -715,8 +698,6 @@ function tests() {
await new Promise(resolve => setTimeout(resolve, 3000));
expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 100);
} finally {
openpgp.config.aead_protect = aead_protectValue;
openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteValue;
coresStub.restore();
}
});
Expand Down Expand Up @@ -891,8 +872,11 @@ function tests() {

describe('Streaming', function() {
let currentTest = 0;
const aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;

before(async function() {
openpgp.config.aead_chunk_size_byte = 4;

pubKey = await openpgp.key.readArmored(pub_key);
privKey = await openpgp.key.readArmored(priv_key);
await privKey.decrypt(passphrase);
Expand Down Expand Up @@ -925,6 +909,10 @@ describe('Streaming', function() {
});
});

after(function() {
openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteValue;
});

tryTests('WhatWG Streams', tests, {
if: true,
beforeEach: function() {
Expand Down

0 comments on commit 76e7dc1

Please sign in to comment.