Adds worker tests for NIST P-256 and X25519

This commit is contained in:
Mahrud Sayrafi 2018-02-27 16:40:28 -08:00
parent f04273cd8d
commit ecc38d0c6e
No known key found for this signature in database
GPG Key ID: C24071B956C3245F
3 changed files with 179 additions and 89 deletions

View File

@ -27,7 +27,7 @@ import util from '../util';
/** /**
* @constructor * @constructor
*/ */
function ECDHSymmetricKey(data) { export default function ECDHSymmetricKey(data) {
if (typeof data === 'undefined') { if (typeof data === 'undefined') {
data = new Uint8Array([]); data = new Uint8Array([]);
} else if (util.isString(data)) { } else if (util.isString(data)) {
@ -65,5 +65,3 @@ ECDHSymmetricKey.prototype.write = function () {
ECDHSymmetricKey.fromClone = function (clone) { ECDHSymmetricKey.fromClone = function (clone) {
return new ECDHSymmetricKey(clone.data); return new ECDHSymmetricKey(clone.data);
}; };
export default ECDHSymmetricKey;

View File

@ -1,3 +1,5 @@
/* globals tryTests: true */
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp');
const chai = require('chai'); const chai = require('chai');
@ -234,4 +236,75 @@ describe('Elliptic Curve Cryptography', function () {
expect(key.publicKeyArmored).to.exist; expect(key.publicKeyArmored).to.exist;
}); });
}); });
function omnibus() {
it('Omnibus NIST P-256 Test', function () {
const options = { userIds: {name: "Hi", email: "hi@hel.lo"}, curve: "p256" };
return openpgp.generateKey(options).then(function (firstKey) {
const hi = firstKey.key;
const pubHi = hi.toPublic();
const options = { userIds: { name: "Bye", email: "bye@good.bye" }, curve: "p256" };
return openpgp.generateKey(options).then(function (secondKey) {
const bye = secondKey.key;
const pubBye = bye.toPublic();
return Promise.all([
// Signing message
openpgp.sign(
{ data: 'Hi, this is me, Hi!', privateKeys: hi }
).then(signed => {
const msg = openpgp.cleartext.readArmored(signed.data);
// Verifying signed message
return Promise.all([
openpgp.verify(
{ message: msg, publicKeys: pubHi }
).then(output => expect(output.signatures[0].valid).to.be.true),
// Verifying detached signature
openpgp.verify(
{ message: openpgp.message.fromText('Hi, this is me, Hi!'),
publicKeys: pubHi,
signature: openpgp.signature.readArmored(signed.data) }
).then(output => expect(output.signatures[0].valid).to.be.true)
]);
}),
// Encrypting and signing
openpgp.encrypt(
{ data: 'Hi, Hi wrote this but only Bye can read it!',
publicKeys: [pubBye],
privateKeys: [hi] }
).then(encrypted => {
const msg = openpgp.message.readArmored(encrypted.data);
// Decrypting and verifying
return openpgp.decrypt(
{ message: msg,
privateKeys: bye,
publicKeys: [pubHi] }
).then(output => {
expect(output.data).to.equal('Hi, Hi wrote this but only Bye can read it!');
expect(output.signatures[0].valid).to.be.true;
});
})
]);
});
});
});
}
omnibus();
tryTests('ECC Worker Tests', omnibus, {
if: typeof window !== 'undefined' && window.Worker,
before: function() {
openpgp.initWorker({ path:'../dist/openpgp.worker.js' });
},
beforeEach: function() {
openpgp.config.use_native = true;
},
after: function() {
openpgp.destroyWorker();
}
});
// TODO find test vectors
}); });

View File

@ -147,6 +147,8 @@ describe('X25519 Cryptography', function () {
done(); done();
}); });
// This test is slow because the keys are generated by GPG2, which
// by default chooses a larger number for S2K iterations than we do.
it('Load private key', function (done) { it('Load private key', function (done) {
load_priv_key('light'); load_priv_key('light');
load_priv_key('night'); load_priv_key('night');
@ -217,7 +219,8 @@ describe('X25519 Cryptography', function () {
}); });
}); });
// TODO generate, export, then reimport key and validate // TODO export, then reimport key and validate
function omnibus() {
it('Omnibus Ed25519/Curve25519 Test', function () { it('Omnibus Ed25519/Curve25519 Test', function () {
const options = { const options = {
userIds: {name: "Hi", email: "hi@hel.lo"}, userIds: {name: "Hi", email: "hi@hel.lo"},
@ -315,6 +318,22 @@ describe('X25519 Cryptography', function () {
}); });
}); });
}); });
}
omnibus();
tryTests('X25519 Worker Tests', omnibus, {
if: typeof window !== 'undefined' && window.Worker,
before: function() {
openpgp.initWorker({ path:'../dist/openpgp.worker.js' });
},
beforeEach: function() {
openpgp.config.use_native = true;
},
after: function() {
openpgp.destroyWorker();
}
});
describe('Ed25519 Test Vectors from RFC8032', function () { describe('Ed25519 Test Vectors from RFC8032', function () {
// https://tools.ietf.org/html/rfc8032#section-7.1 // https://tools.ietf.org/html/rfc8032#section-7.1