From e44fbbccabecbeed93dc618c1d98feb7d528100b Mon Sep 17 00:00:00 2001
From: Daniel Huigens <d.huigens@protonmail.com>
Date: Tue, 10 Apr 2018 17:55:12 +0200
Subject: [PATCH] Add more OCB tests

---
 test/crypto/ocb.js      | 29 +++++++++++++++++++++++++++++
 test/general/openpgp.js | 12 ++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/test/crypto/ocb.js b/test/crypto/ocb.js
index a3358913..04de3065 100644
--- a/test/crypto/ocb.js
+++ b/test/crypto/ocb.js
@@ -149,4 +149,33 @@ describe('Symmetric AES-OCB', function() {
       expect(openpgp.util.Uint8Array_to_hex(pt)).to.equal(vec.P.toLowerCase());
     }
   });
+
+  it('Different key size test vectors', async function() {
+    const TAGLEN = 128;
+    const outputs = {
+      128: '67E944D23256C5E0B6C61FA22FDF1EA2',
+      192: 'F673F2C3E7174AAE7BAE986CA9F29E17',
+      256: 'D90EB8E9C977C88B79DD793D7FFA161C'
+    };
+
+    for (const KEYLEN of [128, 192, 256]) {
+      const K = new Uint8Array(KEYLEN / 8);
+      K[K.length - 1] = TAGLEN;
+
+      const C = [];
+      let N;
+      for (let i = 0; i < 128; i++) {
+        const S = new Uint8Array(i);
+        N = openpgp.util.concatUint8Array([new Uint8Array(8), openpgp.util.writeNumber(3 * i + 1, 4)]);
+        C.push(await ocb.encrypt('aes' + KEYLEN, S, K, N, S));
+        N = openpgp.util.concatUint8Array([new Uint8Array(8), openpgp.util.writeNumber(3 * i + 2, 4)]);
+        C.push(await ocb.encrypt('aes' + KEYLEN, S, K, N, new Uint8Array()));
+        N = openpgp.util.concatUint8Array([new Uint8Array(8), openpgp.util.writeNumber(3 * i + 3, 4)]);
+        C.push(await ocb.encrypt('aes' + KEYLEN, new Uint8Array(), K, N, S));
+      }
+      N = openpgp.util.concatUint8Array([new Uint8Array(8), openpgp.util.writeNumber(385, 4)]);
+      const output = await ocb.encrypt('aes' + KEYLEN, new Uint8Array(), K, N, openpgp.util.concatUint8Array(C));
+      expect(openpgp.util.Uint8Array_to_hex(output)).to.equal(outputs[KEYLEN].toLowerCase());
+    }
+  });
 });
diff --git a/test/general/openpgp.js b/test/general/openpgp.js
index 6ad80330..a6898a23 100644
--- a/test/general/openpgp.js
+++ b/test/general/openpgp.js
@@ -598,6 +598,7 @@ describe('OpenPGP.js public api tests', function() {
     let use_nativeVal;
     let aead_protectVal;
     let aead_protect_versionVal;
+    let aead_modeVal;
 
     beforeEach(function(done) {
       publicKey = openpgp.key.readArmored(pub_key);
@@ -622,6 +623,7 @@ describe('OpenPGP.js public api tests', function() {
       use_nativeVal = openpgp.config.use_native;
       aead_protectVal = openpgp.config.aead_protect;
       aead_protect_versionVal = openpgp.config.aead_protect_version;
+      aead_modeVal = openpgp.config.aead_mode;
       done();
     });
 
@@ -630,6 +632,7 @@ describe('OpenPGP.js public api tests', function() {
       openpgp.config.use_native = use_nativeVal;
       openpgp.config.aead_protect = aead_protectVal;
       openpgp.config.aead_protect_version = aead_protect_versionVal;
+      openpgp.config.aead_mode = aead_modeVal;
     });
 
     it('Decrypting key with wrong passphrase rejected', async function () {
@@ -688,6 +691,15 @@ describe('OpenPGP.js public api tests', function() {
       }
     });
 
+    tryTests('OCB mode', tests, {
+      if: true,
+      beforeEach: function() {
+        openpgp.config.aead_protect = true;
+        openpgp.config.aead_protect_version = 4;
+        openpgp.config.aead_mode = openpgp.enums.aead.ocb;
+      }
+    });
+
     function tests() {
       it('Configuration', function() {
         openpgp.config.show_version = false;