Merge branch 'master' of github.com:openpgpjs/openpgpjs

This commit is contained in:
Bart Butler 2018-03-01 11:55:17 -08:00
commit 20a0e76998
8 changed files with 71 additions and 64 deletions

View File

@ -194,7 +194,7 @@ async function generate(curve) {
curve = new Curve(curve);
const keyPair = await curve.genKeyPair();
return {
oid: new OID(curve.oid.slice(2)),
oid: curve.oid,
Q: new BN(keyPair.getPublic()),
d: new BN(keyPair.getPrivate()),
hash: curve.hash,

View File

@ -29,7 +29,7 @@
*/
import BN from 'bn.js';
import { curves, webCurves, nodeCurves } from './curves';
import { webCurves, nodeCurves } from './curves';
import hash from '../../hash';
import util from '../../../util';
import enums from '../../../enums';
@ -132,9 +132,10 @@ async function webSign(curve, hash_algo, message, keyPair) {
key,
message
));
return {
r: signature.slice(0, len),
s: signature.slice(len, len << 1)
r: new BN(signature.slice(0, len)),
s: new BN(signature.slice(len, len << 1))
};
}
@ -159,9 +160,10 @@ async function webVerify(curve, hash_algo, { r, s }, message, publicKey) {
["verify"]
);
r = [].concat(Array(len - r.length).fill(0), r);
s = [].concat(Array(len - s.length).fill(0), s);
const signature = new Uint8Array([].concat(r, s)).buffer;
const signature = util.concatUint8Array([
new Uint8Array(len - r.length), r,
new Uint8Array(len - s.length), s
]).buffer;
return webCrypto.verify(
{

View File

@ -40,15 +40,16 @@ import enums from '../enums';
/**
* @constructor
*/
function OID(oid) {
export default function OID(oid) {
if (oid instanceof OID) {
this.oid = oid.oid;
} else if (util.isArray(oid) ||
util.isUint8Array(oid)) {
oid = new Uint8Array(oid);
if (oid[0] === 0x06) { // DER encoded oid byte array
oid = oid.slice(2);
oid = oid.subarray(2);
}
this.oid = util.Uint8Array_to_str(oid);
this.oid = oid;
} else {
this.oid = '';
}
@ -63,7 +64,7 @@ OID.prototype.read = function (input) {
if (input.length >= 1) {
const length = input[0];
if (input.length >= 1+length) {
this.oid = util.Uint8Array_to_str(input.subarray(1, 1+length));
this.oid = input.subarray(1, 1+length);
return 1+this.oid.length;
}
}
@ -75,7 +76,7 @@ OID.prototype.read = function (input) {
* @return {Uint8Array} Array with the serialized value the OID
*/
OID.prototype.write = function () {
return util.str_to_Uint8Array(String.fromCharCode(this.oid.length)+this.oid);
return util.concatUint8Array([new Uint8Array([this.oid.length]), this.oid]);
};
/**
@ -83,7 +84,7 @@ OID.prototype.write = function () {
* @return {string} String with the hex value of the OID
*/
OID.prototype.toHex = function() {
return util.str_to_hex(this.oid);
return util.Uint8Array_to_hex(this.oid);
};
/**
@ -100,8 +101,5 @@ OID.prototype.getName = function() {
};
OID.fromClone = function (clone) {
const oid = new OID(clone.oid);
return oid;
return new OID(clone.oid);
};
export default OID;

View File

@ -16,7 +16,8 @@ describe('Elliptic Curve Cryptography', function () {
0x3C, 0x2A, 0x35, 0x25, 0x26, 0x11, 0xD7, 0xEA,
0xA1, 0xED, 0x29, 0x95, 0xB5, 0xE1, 0x5F, 0x1D
]),
pub: new Uint8Array([0x04,
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,
@ -35,7 +36,8 @@ describe('Elliptic Curve Cryptography', function () {
0xF1, 0xBE, 0x76, 0xCB, 0xE8, 0xAB, 0x3B, 0xBD,
0xB6, 0x84, 0xC7, 0x8B, 0x91, 0x2F, 0x76, 0x8B
]),
pub: new Uint8Array([0x04,
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,
@ -61,7 +63,8 @@ describe('Elliptic Curve Cryptography', function () {
0x57, 0x48, 0xA3, 0x73, 0xDB, 0xE0, 0x19, 0x50,
0x2E, 0x79
]),
pub: new Uint8Array([0x04,
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,
@ -87,7 +90,8 @@ describe('Elliptic Curve Cryptography', function () {
0xAD, 0x05, 0xAB, 0x8F, 0x87, 0x9B, 0x57, 0x48,
0xAE, 0x8A, 0xE0, 0xF9, 0x39, 0xBD, 0x24, 0x00
]),
pub: new Uint8Array([0x04,
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,
@ -105,7 +109,8 @@ describe('Elliptic Curve Cryptography', function () {
0xBD, 0xD3, 0xFE, 0xAE, 0x3F, 0xB2, 0xCF, 0xEE,
0xA7, 0xDB, 0xD0, 0x58, 0xA7, 0x47, 0xF8, 0x7C
]),
pub: new Uint8Array([0x04,
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,
@ -215,11 +220,7 @@ describe('Elliptic Curve Cryptography', function () {
}
const ecdsa = elliptic_curves.ecdsa;
return ecdsa.verify(
oid,
hash,
{r: new BN(r), s: new BN(s)},
message,
new Uint8Array(pub)
oid, hash, { r: new Uint8Array(r), s: new Uint8Array(s) }, message, new Uint8Array(pub)
);
};
const secp256k1_dummy_value = new Uint8Array([
@ -228,7 +229,8 @@ describe('Elliptic Curve Cryptography', function () {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]);
const secp256k1_dummy_point = new Uint8Array([0x04,
const secp256k1_dummy_point = new Uint8Array([
0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -237,7 +239,8 @@ describe('Elliptic Curve Cryptography', function () {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
const secp256k1_invalid_point = new Uint8Array([0x04,
const secp256k1_invalid_point = new Uint8Array([
0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -332,7 +335,8 @@ describe('Elliptic Curve Cryptography', function () {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]);
const secp256k1_point = new Uint8Array([0x04,
const secp256k1_point = new Uint8Array([
0x04,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

View File

@ -4,6 +4,7 @@ const expect = require('chai').expect;
describe('Oid tests', function() {
const OID = openpgp.OID;
const util = openpgp.util;
const p256_oid = new Uint8Array([0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07]);
const p384_oid = new Uint8Array([0x2B, 0x81, 0x04, 0x00, 0x22]);
const p521_oid = new Uint8Array([0x2B, 0x81, 0x04, 0x00, 0x23]);
@ -14,7 +15,7 @@ describe('Oid tests', function() {
expect(oid).to.exist;
expect(oid.oid).to.exist;
expect(oid.oid).to.have.length(data.length);
expect(oid.oid).to.equal(openpgp.util.Uint8Array_to_str(data));
expect(oid.toHex()).to.equal(util.Uint8Array_to_hex(data));
});
});
it('Reading and writing', function() {
@ -25,12 +26,14 @@ describe('Oid tests', function() {
expect(oid.read(data)).to.equal(data.length);
expect(oid.oid).to.exist;
expect(oid.oid).to.have.length(data.length-1);
expect(oid.oid).to.equal(openpgp.util.Uint8Array_to_str(data.subarray(1)));
expect(oid.toHex()).to.equal(util.Uint8Array_to_hex(data.subarray(1)));
const result = oid.write();
expect(result).to.exist;
expect(result).to.have.length(data.length);
expect(result[0]).to.equal(data.length-1);
expect(openpgp.util.Uint8Array_to_str(result.subarray(1))).to.equal(openpgp.util.Uint8Array_to_str(data.subarray(1)));
expect(
util.Uint8Array_to_hex(result.subarray(1))
).to.equal(util.Uint8Array_to_hex(data.subarray(1)));
});
});
});

View File

@ -338,9 +338,9 @@ describe('X25519 Cryptography', function () {
describe('Ed25519 Test Vectors from RFC8032', function () {
// https://tools.ietf.org/html/rfc8032#section-7.1
const signature = openpgp.crypto.signature;
const curve = new elliptic.Curve('ed25519');
const util = openpgp.util;
function testVector(vector) {
const curve = new elliptic.Curve('ed25519');
const S = curve.keyFromSecret(vector.SECRET_KEY);
const P = curve.keyFromPublic('40'+vector.PUBLIC_KEY);
expect(S.getPublic()).to.deep.equal(P.getPublic());