diff --git a/Gruntfile.js b/Gruntfile.js index 50aef8cf..84481853 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -15,6 +15,7 @@ module.exports = function(grunt) { 'src/crypto/public_key/elgamal.js', 'src/crypto/public_key/index.js', 'src/crypto/public_key/rsa.js', + 'src/crypto/public_key/elliptic/*.js', 'src/crypto/*.js', 'src/encoding/**/*.js', 'src/hkp/**/*.js', diff --git a/package.json b/package.json index d9e43876..bc7f0f1a 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "test": "grunt test" }, "devDependencies": { - "aes": "^0.1.0", "asmcrypto-lite": "^1.0.0", "babel-core": "^6.26.0", "babel-preset-es2015": "^6.3.13", diff --git a/src/crypto/public_key/elliptic/curves.js b/src/crypto/public_key/elliptic/curves.js new file mode 100644 index 00000000..c6b33477 --- /dev/null +++ b/src/crypto/public_key/elliptic/curves.js @@ -0,0 +1,118 @@ +// OpenPGP.js - An OpenPGP implementation in javascript +// Copyright (C) 2015-2016 Decentral +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3.0 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +// Wrapper of an instance of an Elliptic Curve + +/** + * @requires crypto/public_key/elliptic/key + * @requires crypto/public_key/jsbn + * @requires enums + * @requires util + * @module crypto/public_key/elliptic/curve + */ + +'use strict'; + +import {ec as EC} from 'elliptic'; +import {KeyPair} from './key.js'; +import BigInteger from '../jsbn.js'; +import enums from '../../../enums.js'; +import util from '../../../util.js'; + +function Curve(name, {oid, hash, cipher}) { + this.curve = new EC(name); + this.name = name; + this.oid = oid; + this.hash = hash; + this.cipher = cipher; +} + +Curve.prototype.keyFromPrivate = function (priv) { + return new KeyPair(this.curve, {priv: priv}); +}; + +Curve.prototype.keyFromPublic = function (pub) { + return new KeyPair(this.curve, {pub: pub}); +}; + +Curve.prototype.genKeyPair = function () { + var r = this.curve.genKeyPair(); + return new KeyPair(this.curve, { + pub: r.getPublic().encode(), + priv: r.getPrivate().toArray() + }); +}; + +const curves = { + p256: { + oid: util.bin2str([0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07]), + bits: 256, + hash: enums.hash.sha256, + cipher: enums.symmetric.aes128, + }, + p384: { + oid: util.bin2str([0x2B, 0x81, 0x04, 0x00, 0x22]), + bits: 384, + hash: enums.hash.sha384, + cipher: enums.symmetric.aes192, + }, + p521: { + oid: util.bin2str([0x2B, 0x81, 0x04, 0x00, 0x23]), + bits: 521, + hash: enums.hash.sha512, + cipher: enums.symmetric.aes256, + }, + secp256k1: { + oid: util.bin2str([0x2B, 0x81, 0x04, 0x00, 0x0A]), + bits: 256, + hash: enums.hash.sha256, + cipher: enums.symmetric.aes128, + } +}; + +function get(oid_or_name) { + for (var name in curves) { + if (curves[name].oid === oid_or_name || name === oid_or_name) { + return new Curve(name, { + oid: curves[name].oid, + hash: curves[name].hash, + cipher: curves[name].cipher + }); + } + } + throw new Error('Not valid curve'); +} + +function generate(curve) { + return new Promise(function (resolve) { + curve = get(curve); + var keyPair = curve.genKeyPair(); + resolve({ + oid: curve.oid, + R: new BigInteger(keyPair.getPublic()), + r: new BigInteger(keyPair.getPrivate()), + hash: curve.hash, + cipher: curve.cipher + }); + }); +} + +module.exports = { + Curve: Curve, + generate: generate, + get: get +}; diff --git a/src/crypto/public_key/elliptic/index.js b/src/crypto/public_key/elliptic/index.js new file mode 100644 index 00000000..1868f192 --- /dev/null +++ b/src/crypto/public_key/elliptic/index.js @@ -0,0 +1,33 @@ +// OpenPGP.js - An OpenPGP implementation in javascript +// Copyright (C) 2015-2016 Decentral +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3.0 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +// Function to access Elliptic Curve Cryptography + +/** + * @requires crypto/public_key/elliptic/curve + * @requires crypto/public_key/elliptic/ecdsa + * @module crypto/public_key/elliptic + */ + +'use strict'; + +import {get, generate} from './curves.js'; + +module.exports = { + get: get, + generate: generate +}; diff --git a/src/crypto/public_key/elliptic/key.js b/src/crypto/public_key/elliptic/key.js new file mode 100644 index 00000000..875a62f5 --- /dev/null +++ b/src/crypto/public_key/elliptic/key.js @@ -0,0 +1,74 @@ +// OpenPGP.js - An OpenPGP implementation in javascript +// Copyright (C) 2015-2016 Decentral +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3.0 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +// Wrapper for a KeyPair of an Elliptic Curve + +/** + * @requires crypto/hash + * @requires util + * @module crypto/public_key/elliptic/key + */ + +'use strict'; + +import hash from '../../hash'; +import util from '../../../util.js'; + +function KeyPair(curve, options) { + this.curve = curve; + this.keyPair = this.curve.keyPair(options); +} + +KeyPair.prototype.sign = function (message, hash_algo) { + if (typeof message === 'string') { + message = util.str2Uint8Array(message); + } + const digest = (typeof hash_algo === 'undefined') ? message : hash.digest(hash_algo, message); + const signature = this.keyPair.sign(digest); + return { + r: signature.r.toArray(), + s: signature.s.toArray() + }; +}; + +KeyPair.prototype.verify = function (message, signature, hash_algo) { + if (typeof message === 'string') { + message = util.str2Uint8Array(message); + } + const digest = (typeof hash_algo === 'undefined') ? message : hash.digest(hash_algo, message); + return this.keyPair.verify(digest, signature); +}; + +KeyPair.prototype.derive = function (pub) { + return this.keyPair.derive(pub.keyPair.getPublic()).toArray(); +}; + +KeyPair.prototype.getPublic = function () { + return this.keyPair.getPublic().encode(); +}; + +KeyPair.prototype.getPrivate = function () { + return this.keyPair.getPrivate().toArray(); +}; + +KeyPair.prototype.isValid = function () { + return this.keyPair.validate().result; +}; + +module.exports = { + KeyPair: KeyPair +}; diff --git a/src/crypto/public_key/index.js b/src/crypto/public_key/index.js index 8e5dfc7b..ed29c250 100644 --- a/src/crypto/public_key/index.js +++ b/src/crypto/public_key/index.js @@ -1,6 +1,7 @@ /** * @requires crypto/public_key/dsa * @requires crypto/public_key/elgamal + * @requires crypto/public_key/elliptic * @requires crypto/public_key/rsa * @module crypto/public_key */ @@ -11,11 +12,14 @@ import rsa from './rsa.js'; /** @see module:crypto/public_key/elgamal */ import elgamal from './elgamal.js'; +/** @see module:crypto/public_key/elliptic */ +import elliptic from './elliptic'; /** @see module:crypto/public_key/dsa */ import dsa from './dsa.js'; export default { rsa: rsa, elgamal: elgamal, + elliptic: elliptic, dsa: dsa }; diff --git a/test/crypto/elliptic.js b/test/crypto/elliptic.js new file mode 100644 index 00000000..55524053 --- /dev/null +++ b/test/crypto/elliptic.js @@ -0,0 +1,196 @@ +'use strict'; + +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); + +var expect = require('chai').expect; + +describe('Elliptic Curve Cryptography', function () { + var elliptic_curves = openpgp.crypto.publicKey.elliptic; + var key_data = { + p256: { + priv: new Uint8Array([ + 0x2B, 0x48, 0x2B, 0xE9, 0x88, 0x74, 0xE9, 0x49, + 0x1F, 0x89, 0xCC, 0xFF, 0x0A, 0x26, 0x05, 0xA2, + 0x3C, 0x2A, 0x35, 0x25, 0x26, 0x11, 0xD7, 0xEA, + 0xA1, 0xED, 0x29, 0x95, 0xB5, 0xE1, 0x5F, 0x1D]), + pub: new Uint8Array([0x04, + 0x80, 0x2C, 0x40, 0x76, 0x31, 0x20, 0xB6, 0x9B, + 0x48, 0x3B, 0x05, 0xEB, 0x6C, 0x1E, 0x3F, 0x49, + 0x84, 0xF7, 0xD2, 0xAD, 0x16, 0xA1, 0x6F, 0x62, + 0xFD, 0xCA, 0xEC, 0xB4, 0xA0, 0xBD, 0x4C, 0x1A, + 0x6F, 0xAA, 0xE7, 0xFD, 0xC4, 0x7D, 0x89, 0xCC, + 0x06, 0xCA, 0xFE, 0xAE, 0xCD, 0x0E, 0x9E, 0x62, + 0x57, 0xA4, 0xC3, 0xE7, 0x5E, 0x69, 0x10, 0xEE, + 0x67, 0xC2, 0x09, 0xF9, 0xEF, 0xE7, 0x9E, 0x56]) + }, + p384: { + priv: new Uint8Array([ + 0xB5, 0x38, 0xDA, 0xF3, 0x77, 0x58, 0x3F, 0x94, + 0x5B, 0xC2, 0xCA, 0xC6, 0xA9, 0xFC, 0xAA, 0x3F, + 0x97, 0xB0, 0x54, 0x26, 0x10, 0xB4, 0xEC, 0x2A, + 0xA7, 0xC1, 0xA3, 0x4B, 0xC0, 0xBD, 0xFE, 0x3E, + 0xF1, 0xBE, 0x76, 0xCB, 0xE8, 0xAB, 0x3B, 0xBD, + 0xB6, 0x84, 0xC7, 0x8B, 0x91, 0x2F, 0x76, 0x8B]), + pub: new Uint8Array([0x04, + 0x44, 0x83, 0xA0, 0x3E, 0x5B, 0x0A, 0x0D, 0x9B, + 0xA0, 0x06, 0xDF, 0x38, 0xC7, 0x64, 0xCD, 0x62, + 0x7D, 0x5E, 0x3D, 0x3B, 0x50, 0xF5, 0x06, 0xC7, + 0xF7, 0x9B, 0xF0, 0xDE, 0xB1, 0x0C, 0x64, 0x74, + 0x0D, 0x03, 0x67, 0x24, 0xA0, 0xFF, 0xD1, 0x3D, + 0x03, 0x96, 0x48, 0xE7, 0x73, 0x5E, 0xF1, 0xC0, + 0x62, 0xCC, 0x33, 0x5A, 0x2A, 0x66, 0xA7, 0xAB, + 0xCA, 0x77, 0x52, 0xB8, 0xCD, 0xB5, 0x91, 0x16, + 0xAF, 0x42, 0xBB, 0x79, 0x0A, 0x59, 0x51, 0x68, + 0x8E, 0xEA, 0x32, 0x7D, 0x4A, 0x4A, 0xBB, 0x26, + 0x13, 0xFB, 0x95, 0xC0, 0xB1, 0xA4, 0x54, 0xCA, + 0xFA, 0x85, 0x8A, 0x4B, 0x58, 0x7C, 0x61, 0x39]) + }, + p521: { + priv: new Uint8Array([ + 0x00, 0xBB, 0x35, 0x27, 0xBC, 0xD6, 0x7E, 0x35, + 0xD5, 0xC5, 0x99, 0xC9, 0xB4, 0x6C, 0xEE, 0xDE, + 0x79, 0x2D, 0x77, 0xBD, 0x0A, 0x08, 0x9A, 0xC2, + 0x21, 0xF8, 0x35, 0x1C, 0x49, 0x5C, 0x40, 0x11, + 0xAC, 0x95, 0x2A, 0xEE, 0x91, 0x3A, 0x60, 0x5A, + 0x25, 0x5A, 0x95, 0x38, 0xDC, 0xEB, 0x59, 0x8E, + 0x33, 0xAD, 0xC0, 0x0B, 0x56, 0xB1, 0x06, 0x8C, + 0x57, 0x48, 0xA3, 0x73, 0xDB, 0xE0, 0x19, 0x50, + 0x2E, 0x79]), + pub: new Uint8Array([0x04, + 0x01, 0x0D, 0xD5, 0xCA, 0xD8, 0xB0, 0xEF, 0x9F, + 0x2B, 0x7E, 0x58, 0x99, 0xDE, 0x05, 0xF6, 0xF6, + 0x64, 0x6B, 0xCD, 0x59, 0x2E, 0x39, 0xB8, 0x82, + 0xB3, 0x13, 0xE6, 0x7D, 0x50, 0x85, 0xC3, 0xFA, + 0x93, 0xA5, 0x3F, 0x92, 0x85, 0x42, 0x36, 0xC0, + 0x83, 0xC9, 0xA4, 0x38, 0xB3, 0xD1, 0x99, 0xDA, + 0xE1, 0x02, 0x37, 0x7A, 0x3A, 0xC2, 0xB4, 0x55, + 0xEC, 0x1C, 0x0F, 0x00, 0x97, 0xFC, 0x75, 0x93, + 0xFE, 0x87, 0x00, 0x7D, 0xBE, 0x1A, 0xF5, 0xF9, + 0x57, 0x5C, 0xF2, 0x50, 0x2D, 0x14, 0x32, 0xEE, + 0x9B, 0xBE, 0xB3, 0x0E, 0x12, 0x2F, 0xF8, 0x85, + 0x11, 0x1A, 0x4F, 0x88, 0x50, 0xA4, 0xDB, 0x37, + 0xA6, 0x53, 0x5C, 0xB7, 0x87, 0xA6, 0x06, 0x21, + 0x15, 0xCC, 0x12, 0xC0, 0x1C, 0x83, 0x6F, 0x7B, + 0x5A, 0x8A, 0x36, 0x4E, 0x46, 0x9E, 0x54, 0x3F, + 0xE2, 0xF7, 0xED, 0x63, 0xC9, 0x92, 0xA4, 0x38, + 0x2B, 0x9C, 0xE2, 0xB7]) + }, + secp256k1: { + priv: new Uint8Array([ + 0x9E, 0xB0, 0x30, 0xD6, 0xE1, 0xCE, 0xAA, 0x0B, + 0x7B, 0x8F, 0xDE, 0x5D, 0x91, 0x4D, 0xDC, 0xA0, + 0xAD, 0x05, 0xAB, 0x8F, 0x87, 0x9B, 0x57, 0x48, + 0xAE, 0x8A, 0xE0, 0xF9, 0x39, 0xBD, 0x24, 0x00]), + pub: new Uint8Array([0x04, + 0xA8, 0x02, 0x35, 0x2C, 0xB7, 0x24, 0x95, 0x51, + 0x0A, 0x65, 0x26, 0x7D, 0xDF, 0xEA, 0x64, 0xB3, + 0xA8, 0xE1, 0x4F, 0xDD, 0x12, 0x84, 0x7E, 0x59, + 0xDB, 0x81, 0x0F, 0x89, 0xED, 0xFB, 0x29, 0xFB, + 0x07, 0x60, 0x29, 0x7D, 0x39, 0x8F, 0xB8, 0x68, + 0xF0, 0xFD, 0xA6, 0x67, 0x83, 0x55, 0x75, 0x7D, + 0xB8, 0xFD, 0x0B, 0xDF, 0x76, 0xCE, 0xBC, 0x95, + 0x4B, 0x92, 0x26, 0xFC, 0xAA, 0x7A, 0x7C, 0x3F]) + } + }; + var signature_data = { + priv: new Uint8Array([ + 0x14, 0x2B, 0xE2, 0xB7, 0x4D, 0xBD, 0x1B, 0x22, + 0x4D, 0xDF, 0x96, 0xA4, 0xED, 0x8E, 0x5B, 0xF9, + 0xBD, 0xD3, 0xFE, 0xAE, 0x3F, 0xB2, 0xCF, 0xEE, + 0xA7, 0xDB, 0xD0, 0x58, 0xA7, 0x47, 0xF8, 0x7C]), + pub: new Uint8Array([0x04, + 0xD3, 0x36, 0x11, 0xF9, 0xF9, 0xAB, 0x39, 0x23, + 0x15, 0xB9, 0x71, 0x7B, 0x2A, 0x0B, 0xA6, 0x6D, + 0x39, 0x6D, 0x64, 0x87, 0x22, 0x9A, 0xA3, 0x0A, + 0x55, 0x27, 0x14, 0x2E, 0x1C, 0x61, 0xA2, 0x8A, + 0xDA, 0x4E, 0x8F, 0xCE, 0x04, 0xBE, 0xE2, 0xC3, + 0x82, 0x0B, 0x21, 0x4C, 0xBC, 0xED, 0x0E, 0xE2, + 0xF1, 0x14, 0x33, 0x9A, 0x86, 0x5F, 0xC6, 0xF9, + 0x8E, 0x95, 0x24, 0x10, 0x1F, 0x0F, 0x13, 0xE4]), + message: new Uint8Array([ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]), + signature: { + r: new Uint8Array([ + 0xF1, 0x78, 0x1C, 0xA5, 0x13, 0x21, 0x0C, 0xBA, + 0x6F, 0x18, 0x5D, 0xB3, 0x01, 0xE2, 0x17, 0x1B, + 0x67, 0x65, 0x7F, 0xC6, 0x1F, 0x50, 0x12, 0xFB, + 0x2F, 0xD3, 0xA4, 0x29, 0xE3, 0xC2, 0x44, 0x9F]), + s: new Uint8Array([ + 0x7F, 0x08, 0x69, 0x6D, 0xBB, 0x1B, 0x9B, 0xF2, + 0x62, 0x1C, 0xCA, 0x80, 0xC6, 0x15, 0xB2, 0xAE, + 0x60, 0x50, 0xD1, 0xA7, 0x1B, 0x32, 0xF3, 0xB1, + 0x01, 0x0B, 0xDF, 0xC6, 0xAB, 0xF0, 0xEB, 0x01]) + } + }; + describe('Basic Operations', function () { + it('Creating curve with name', function (done) { + var names = ['p256', 'p384', 'p521', 'secp256k1']; + names.forEach(function (name) { + expect(elliptic_curves.get(name)).to.exist; + }); + done(); + }); + it('Creating curve from oid', function (done) { + var oids = ['2A8648CE3D030107', '2B81040022', '2B81040023', '2B8104000A']; + oids.forEach(function (oid) { + expect(elliptic_curves.get(openpgp.util.hex2bin(oid))).to.exist; + }); + done(); + }); + it('Creating KeyPair', function (done) { + var names = ['p256', 'p384', 'p521', 'secp256k1']; + names.forEach(function (name) { + var curve = elliptic_curves.get(name); + var keyPair = curve.genKeyPair(); + expect(keyPair).to.exist; + expect(keyPair.isValid()).to.be.true; + }); + done(); + }); + it('Creating KeyPair from data', function (done) { + for (var name in key_data) { + var pair = key_data[name]; + var curve = elliptic_curves.get(name); + expect(curve).to.exist; + var keyPair = curve.keyFromPrivate(pair.priv); + expect(keyPair).to.exist; + var pub = keyPair.getPublic(); + expect(pub).to.exist; + expect(openpgp.util.hexidump(pub)).to.equal(openpgp.util.hexidump(pair.pub)); + } + done(); + }); + it('Signature verification', function (done) { + var curve = elliptic_curves.get('p256'); + var key = curve.keyFromPublic(signature_data.pub); + expect(key.verify(signature_data.message, signature_data.signature, 8)).to.be.true; + done(); + }); + it('Invalid signature', function (done) { + var curve = elliptic_curves.get('p256'); + var key = curve.keyFromPublic(key_data.p256.pub); + expect(key.verify(signature_data.message, signature_data.signature, 8)).to.be.false; + done(); + }); + it('Signature generation', function (done) { + var curve = elliptic_curves.get('p256'); + var key = curve.keyFromPrivate(key_data.p256.priv); + var signature = key.sign(signature_data.message, 8); + key = curve.keyFromPublic(key_data.p256.pub); + expect(key.verify(signature_data.message, signature, 8)).to.be.true; + done(); + }); + it('Shared secret generation', function (done) { + var curve = elliptic_curves.get('p256'); + var key1 = curve.keyFromPrivate(key_data.p256.priv); + var key2 = curve.keyFromPublic(signature_data.pub); + var shared1 = openpgp.util.hexidump(key1.derive(key2)); + key1 = curve.keyFromPublic(key_data.p256.pub); + key2 = curve.keyFromPrivate(signature_data.priv); + var shared2 = openpgp.util.hexidump(key2.derive(key1)); + expect(shared1).to.equal(shared2); + done(); + }); + }); +}); diff --git a/test/crypto/index.js b/test/crypto/index.js index 7429aaba..a61e6f6f 100644 --- a/test/crypto/index.js +++ b/test/crypto/index.js @@ -3,6 +3,7 @@ describe('Crypto', function () { require('./hash'); require('./random.js'); require('./crypto.js'); + require('./elliptic.js'); require('./pkcs5.js'); require('./rfc3394.js'); });