From 61a0e3fa6947a14a2083f43a18c537fb313b6f8d Mon Sep 17 00:00:00 2001
From: Daniel Huigens <d.huigens@protonmail.com>
Date: Wed, 11 Jul 2018 13:52:46 +0200
Subject: [PATCH] Fix tests on IE11

---
 test/crypto/elliptic.js | 15 ++++++++-------
 test/general/key.js     | 33 +++++++++++++--------------------
 test/general/openpgp.js |  2 +-
 3 files changed, 22 insertions(+), 28 deletions(-)

diff --git a/test/crypto/elliptic.js b/test/crypto/elliptic.js
index a399542f..22da58f2 100644
--- a/test/crypto/elliptic.js
+++ b/test/crypto/elliptic.js
@@ -138,6 +138,7 @@ describe('Elliptic Curve Cryptography', function () {
       ])
     }
   };
+  signature_data.hashed = openpgp.crypto.hash.digest(8, signature_data.message);
   describe('Basic Operations', function () {
     it('Creating curve from name or oid', function (done) {
       for (let name_or_oid in openpgp.enums.curves) {
@@ -171,24 +172,24 @@ describe('Elliptic Curve Cryptography', function () {
       const curve = new elliptic_curves.Curve('p256');
       const key = curve.keyFromPublic(signature_data.pub);
       expect(
-        key.verify(signature_data.message, signature_data.signature, 8)
+        key.verify(signature_data.message, signature_data.signature, 8, signature_data.hashed)
       ).to.eventually.be.true.notify(done);
     });
     it('Invalid signature', function (done) {
       const curve = new elliptic_curves.Curve('p256');
       const key = curve.keyFromPublic(key_data.p256.pub);
       expect(
-        key.verify(signature_data.message, signature_data.signature, 8)
+        key.verify(signature_data.message, signature_data.signature, 8, signature_data.hashed)
       ).to.eventually.be.false.notify(done);
     });
     it('Signature generation', function () {
       const curve = new elliptic_curves.Curve('p256');
       let key = curve.keyFromPrivate(key_data.p256.priv);
-      return key.sign(signature_data.message, 8).then(async ({ r, s }) => {
+      return key.sign(signature_data.message, 8, signature_data.hashed).then(async ({ r, s }) => {
         const signature = { r: new Uint8Array(r.toArray()), s: new Uint8Array(s.toArray()) };
         key = curve.keyFromPublic(key_data.p256.pub);
         await expect(
-          key.verify(signature_data.message, signature, 8)
+          key.verify(signature_data.message, signature, 8, signature_data.hashed)
         ).to.eventually.be.true;
       });
     });
@@ -213,7 +214,7 @@ describe('Elliptic Curve Cryptography', function () {
       }
       const ecdsa = elliptic_curves.ecdsa;
       return ecdsa.verify(
-        oid, hash, { r: new Uint8Array(r), s: new Uint8Array(s) }, message, new Uint8Array(pub)
+        oid, hash, { r: new Uint8Array(r), s: new Uint8Array(s) }, message, new Uint8Array(pub), openpgp.crypto.hash.digest(hash, message)
       );
     };
     const secp256k1_dummy_value = new Uint8Array([
@@ -295,8 +296,8 @@ describe('Elliptic Curve Cryptography', function () {
         const keyPrivate = new Uint8Array(keyPair.getPrivate());
         const oid = curve.oid;
         const message = p384_message;
-        return elliptic_curves.ecdsa.sign(oid, 10, message, keyPrivate).then(async signature => {
-          await expect(elliptic_curves.ecdsa.verify(oid, 10, signature, message, keyPublic))
+        return elliptic_curves.ecdsa.sign(oid, 10, message, keyPrivate, openpgp.crypto.hash.digest(10, message)).then(async signature => {
+          await expect(elliptic_curves.ecdsa.verify(oid, 10, signature, message, keyPublic, openpgp.crypto.hash.digest(10, message)))
             .to.eventually.be.true;
         });
       });
diff --git a/test/general/key.js b/test/general/key.js
index 48eff2b3..5cd45f5d 100644
--- a/test/general/key.js
+++ b/test/general/key.js
@@ -7,27 +7,17 @@ chai.use(require('chai-as-promised'));
 const { expect } = chai;
 
 describe('Key', function() {
-  let webCrypto = openpgp.util.getWebCryptoAll();
+  let rsaGenStub;
+  let rsaGenValue = openpgp.crypto.publicKey.rsa.generate(openpgp.util.getWebCryptoAll() ? 2048 : 512, "10001");
 
-  if (webCrypto) {
-    let generateKey = webCrypto.generateKey;
-    let keyGenStub;
-    let keyGenValue;
+  beforeEach(function() {
+    rsaGenStub = stub(openpgp.crypto.publicKey.rsa, 'generate');
+    rsaGenStub.returns(rsaGenValue);
+  });
 
-    beforeEach(function() {
-      keyGenStub = stub(webCrypto, 'generateKey');
-      keyGenStub.callsFake(function() {
-        if (!keyGenValue) {
-          keyGenValue = generateKey.apply(webCrypto, arguments);
-        }
-        return keyGenValue;
-      });
-    });
-
-    afterEach(function() {
-      keyGenStub.restore();
-    });
-  }
+  afterEach(function() {
+    rsaGenStub.restore();
+  });
 
   describe('V4', tests);
 
@@ -1763,10 +1753,12 @@ VYGdb3eNlV8CfoEC
   it('Generate key - setting date to the past', function() {
     const past = new Date(0);
     const opt = {
+      numBits: 512,
       userIds: { name: 'Test User', email: 'text@example.com' },
       passphrase: 'secret',
       date: past
     };
+    if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
 
     return openpgp.generateKey(opt).then(function(newKey) {
       expect(newKey.key).to.exist;
@@ -1821,7 +1813,8 @@ VYGdb3eNlV8CfoEC
 
   it('Generate key - override main key options for subkey', function() {
     const userId = 'test <a@b.com>';
-    const opt = {numBits: 2048, userIds: [userId], passphrase: '123', subkeys:[{curve: 'curve25519'}]};
+    const opt = {numBits: 512, userIds: [userId], passphrase: '123', subkeys:[{curve: 'curve25519'}]};
+    if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
     return openpgp.generateKey(opt).then(function(key) {
       key = key.key;
       expect(key.users.length).to.equal(1);
diff --git a/test/general/openpgp.js b/test/general/openpgp.js
index 6bd5a44f..7b1c6174 100644
--- a/test/general/openpgp.js
+++ b/test/general/openpgp.js
@@ -374,7 +374,7 @@ describe('OpenPGP.js public api tests', function() {
 
   describe('generateKey - validate user ids', function() {
     let rsaGenStub;
-    let rsaGenValue = openpgp.crypto.publicKey.rsa.generate(2048, "10001");
+    let rsaGenValue = openpgp.crypto.publicKey.rsa.generate(openpgp.util.getWebCryptoAll() ? 2048 : 512, "10001");
 
     beforeEach(function() {
       rsaGenStub = stub(openpgp.crypto.publicKey.rsa, 'generate');