Skip to content

Commit

Permalink
Fix interop with CipherSweet-PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Sep 28, 2022
1 parent 9e8f1fe commit 27161a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
24 changes: 8 additions & 16 deletions lib/backend/boringcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,15 @@ module.exports = class BoringCrypto extends Backend {
}

const nonce = await Util.randomBytes(NONCE_SIZE);
if (aad.length >= 0) {
if (!Buffer.isBuffer(aad)) {
aad = await Util.toBuffer(aad);
}
aad = Buffer.concat([nonce, aad]);
} else {
aad = nonce;
if (!Buffer.isBuffer(aad)) {
aad = await Util.toBuffer(aad);
}
const xchacha = new XChaCha20();
const ciphertext = await xchacha.encrypt(
plaintext,
nonce,
(await this.getEncryptionKey(encKey)).getRawKey()
(await this.getEncryptionKey(encKey)).getRawKey(),
0
);

const mac = await sodium.crypto_generichash(
Expand Down Expand Up @@ -150,13 +146,8 @@ module.exports = class BoringCrypto extends Backend {
const tag = decoded.slice(NONCE_SIZE, NONCE_SIZE + TAG_SIZE);
const encrypted = decoded.slice(NONCE_SIZE + TAG_SIZE);

if (aad.length >= 0) {
if (!Buffer.isBuffer(aad)) {
aad = await Util.toBuffer(aad);
}
aad = Buffer.concat([nonce, aad]);
} else {
aad = nonce;
if (!Buffer.isBuffer(aad)) {
aad = await Util.toBuffer(aad);
}
const calc = await sodium.crypto_generichash(
Buffer.concat([
Expand All @@ -181,7 +172,8 @@ module.exports = class BoringCrypto extends Backend {
const decrypted = await xchacha.decrypt(
encrypted,
nonce,
(await this.getEncryptionKey(encKey)).getRawKey()
(await this.getEncryptionKey(encKey)).getRawKey(),
0
);

await sodium.sodium_memzero(encKey);
Expand Down
5 changes: 2 additions & 3 deletions test/boringcrypto-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ describe('BoringCrypto Tests', function () {
exampleDecrypt = await brng.decrypt(exampleCipher, exampleKey);
expect(exampleDecrypt.toString('utf-8')).to.be.equal('This is just a test message');


exampleCipher = 'brng:o4ldrGv1xFJR8unSt7JjdBOxVHUwd5N2i013RCCyvO3cuQA6XgOh4D2vXL-vdOZx0NinrVedOrL7ROX4eh8zblQiORY_bbUZl7MS2akjGLC-FVA=';
exampleCipher = 'brng:m3y71cMwhTB2e8YjPLzZ2mwBoMRP1BgqVs_He47bRT5DJbWVBwG_cNsn6xvsl4rT2Cu1QSOEFt_lRECl3w524LlzGwgZ30UDm1KfgaTi9scjmu4=';
exampleDecrypt = await brng.decrypt(exampleCipher, exampleKey);
expect(exampleDecrypt.toString('utf-8')).to.be.equal('This is just a test message');

Expand All @@ -63,7 +62,7 @@ describe('BoringCrypto Tests', function () {
exampleDecrypt = await brng.decrypt(exampleCipher, exampleKey);
expect(exampleDecrypt.toString('utf-8')).to.be.equal('This is just a test message');

exampleCipher = 'brng:VCF5AO8OXAKPb2f_g6zdJyEn-WVWfahxpMmbTMVUMctG3mP3fCqjnyJDfeTeXMqwn57ezd3ZZUl722Y6XggD8pAPKW9pOKL5rKnszCTpAOLE7Iw=';
exampleCipher = 'brng:s0oCG2qoJMTWNreJ3AYQhTYSL423gsDYFKmSMDBzOUubIbiNPWSFZmD8uXMO5dmAhuCf5dvTCtfVvl8MADVL0dmub-znB7nEDYH2eMJBCmX-Qyc=';
exampleDecrypt = await brng.decrypt(exampleCipher, exampleKey);
expect(exampleDecrypt.toString('utf-8')).to.be.equal('This is just a test message');
});
Expand Down

0 comments on commit 27161a7

Please sign in to comment.