Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AES encryption error in browsers for messages larger than 3MB #1506

Merged
merged 1 commit into from Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/crypto/mode/cfb.js
Expand Up @@ -23,8 +23,8 @@
*/

import { AES_CFB } from '@openpgp/asmcrypto.js/dist_es8/aes/cfb';

import * as stream from '@openpgp/web-stream-tools';
import { getCipher } from '../crypto';
import * as cipher from '../cipher';
import util from '../../util';
import enums from '../../enums';
Expand Down Expand Up @@ -160,7 +160,7 @@ function xorMut(a, b) {
async function webEncrypt(algo, key, pt, iv) {
const ALGO = 'AES-CBC';
const _key = await webCrypto.importKey('raw', key, { name: ALGO }, false, ['encrypt']);
const { blockSize } = crypto.getCipher(algo);
const { blockSize } = getCipher(algo);
const cbc_pt = util.concatUint8Array([new Uint8Array(blockSize), pt]);
const ct = new Uint8Array(await webCrypto.encrypt({ name: ALGO, iv }, _key, cbc_pt)).subarray(0, pt.length);
xorMut(ct, pt);
Expand Down
18 changes: 16 additions & 2 deletions test/crypto/crypto.js
@@ -1,4 +1,5 @@
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../..');
const sandbox = require('sinon/lib/sinon/sandbox');
const crypto = require('../../src/crypto');
const util = require('../../src/util');

Expand Down Expand Up @@ -237,13 +238,13 @@ module.exports = () => describe('API functional testing', function() {
algo => algo !== 'idea' && algo !== 'plaintext'
);

async function testCFB(plaintext) {
async function testCFB(plaintext, config = openpgp.config) {
await Promise.all(symmAlgoNames.map(async function(algoName) {
const algo = openpgp.enums.write(openpgp.enums.symmetric, algoName);
const { blockSize } = crypto.getCipher(algo);
const symmKey = await crypto.generateSessionKey(algo);
const IV = new Uint8Array(blockSize);
const symmencData = await crypto.mode.cfb.encrypt(algo, symmKey, util.stringToUint8Array(plaintext), IV, openpgp.config);
const symmencData = await crypto.mode.cfb.encrypt(algo, symmKey, util.stringToUint8Array(plaintext), IV, config);
const text = util.uint8ArrayToString(await crypto.mode.cfb.decrypt(algo, symmKey, symmencData, new Uint8Array(blockSize)));
expect(text).to.equal(plaintext);
}));
Expand All @@ -253,6 +254,19 @@ module.exports = () => describe('API functional testing', function() {
await testCFB('1234567');
await testCFB('foobarfoobar1234567890');
await testCFB('12345678901234567890123456789012345678901234567890');
// test using webCrypto
const sinonSandbox = sandbox.create();
const webCrypto = util.getWebCrypto();
if (webCrypto) {
const webCryptoSpy = sinonSandbox.spy(webCrypto, 'encrypt');
try {
await testCFB('12345678901234567890123456789012345678901234567890', { ...openpgp.config, minBytesForWebCrypto: 0 });
} finally {
expect(webCryptoSpy.called).to.be.true;
sinonSandbox.restore();
}
}

});

it('Asymmetric using RSA with eme_pkcs1 padding', async function () {
Expand Down