mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2026-03-21 15:49:09 +00:00
Store named key params in key objects (#1141)
- Store private and public params separately and by name in objects, instead of as an array - Do not keep params in MPI form, but convert them to Uint8Arrays when generating/parsing the key - Modify low-level crypto functions to always accept and return Uint8Arrays instead of BigIntegers - Move PKCS1 padding to lower level functions
This commit is contained in:
@@ -8,7 +8,7 @@ const expect = chai.expect;
|
||||
module.exports = () => describe('API functional testing', function() {
|
||||
const util = openpgp.util;
|
||||
const crypto = openpgp.crypto;
|
||||
const RSApubMPIstrs = [
|
||||
const RSAPublicKeyMaterial = util.concatUint8Array([
|
||||
new Uint8Array([0x08,0x00,0xac,0x15,0xb3,0xd6,0xd2,0x0f,0xf0,0x7a,0xdd,0x21,0xb7,
|
||||
0xbf,0x61,0xfa,0xca,0x93,0x86,0xc8,0x55,0x5a,0x4b,0xa6,0xa4,0x1a,
|
||||
0x60,0xa2,0x3a,0x37,0x06,0x08,0xd8,0x15,0x8e,0x85,0x45,0xaa,0xb7,
|
||||
@@ -30,8 +30,8 @@ module.exports = () => describe('API functional testing', function() {
|
||||
0xee,0xc9,0xa4,0xcd,0x15,0xdc,0x1b,0x8d,0x64,0xc1,0x36,0x17,0xc4,
|
||||
0x8d,0x5e,0x99,0x7a,0x5b,0x9f,0x39,0xd0,0x00,0x6e,0xf9]),
|
||||
new Uint8Array([0x00,0x11,0x01,0x00,0x01])
|
||||
];
|
||||
const RSAsecMPIstrs = [
|
||||
]);
|
||||
const RSAPrivateKeyMaterial = util.concatUint8Array([
|
||||
new Uint8Array([0x07,0xfe,0x23,0xff,0xce,0x45,0x6c,0x60,0x65,0x40,0x6e,0xae,0x35,
|
||||
0x10,0x56,0x60,0xee,0xab,0xfa,0x10,0x42,0xba,0xc7,0x04,0xaf,0x63,
|
||||
0xcd,0x3f,0x62,0xca,0x4b,0xfa,0xe1,0xa9,0x70,0xcd,0x34,0x8b,0xc8,
|
||||
@@ -82,9 +82,9 @@ module.exports = () => describe('API functional testing', function() {
|
||||
0x51,0xe0,0x22,0xf0,0xff,0xa7,0x42,0xd4,0xde,0x0b,0x47,0x8f,0x2b,
|
||||
0xf5,0x4d,0x04,0x32,0x91,0x89,0x4b,0x0e,0x05,0x8d,0x70,0xf9,0xbb,
|
||||
0xe7,0xd6,0x76,0xea,0x0e,0x1a,0x90,0x30,0xf5,0x98,0x01,0xc5,0x73])
|
||||
];
|
||||
]);
|
||||
|
||||
const DSApubMPIstrs = [
|
||||
const DSAPublicKeyMaterial = util.concatUint8Array([
|
||||
new Uint8Array([0x08,0x00,0xa8,0x85,0x5c,0x28,0x05,0x94,0x03,0xbe,0x07,0x6c,0x13,0x3e,0x65,
|
||||
0xfb,0xb5,0xe1,0x99,0x7c,0xfa,0x84,0xe3,0xac,0x47,0xa5,0xc4,0x46,0xd8,0x5f,
|
||||
0x44,0xe9,0xc1,0x6b,0x69,0xf7,0x10,0x76,0x49,0xa7,0x25,0x85,0xf4,0x1b,0xed,
|
||||
@@ -142,14 +142,14 @@ module.exports = () => describe('API functional testing', function() {
|
||||
0x67,0x8d,0x9d,0x14,0xb6,0x9d,0x32,0x82,0xd0,0xb5,0xc6,0x57,0xf0,0x91,0xd9,
|
||||
0xc3,0x26,0xae,0x9f,0xa9,0x67,0x49,0x96,0x5c,0x07,0x3e,0x47,0x5c,0xed,0x60,
|
||||
0x07,0xac,0x6a])
|
||||
];
|
||||
const DSAsecMPIstrs = [
|
||||
]);
|
||||
const DSAPrivateKeyMaterial = util.concatUint8Array([
|
||||
new Uint8Array([0x01,0x00,0x9b,0x58,0xa8,0xf4,0x04,0xb1,0xd5,0x14,0x09,0xe1,0xe1,0xa1,0x8a,
|
||||
0x0b,0xa3,0xc3,0xa3,0x66,0xaa,0x27,0x99,0x50,0x1c,0x4d,0xba,0x24,0xee,0xdf,
|
||||
0xdf,0xb8,0x8e,0x8e])
|
||||
];
|
||||
]);
|
||||
|
||||
const ElgamalpubMPIstrs = [
|
||||
const elGamalPublicKeyMaterial = util.concatUint8Array([
|
||||
new Uint8Array([0x08,0x00,0xea,0xcc,0xbe,0xe2,0xe4,0x5a,0x51,0x18,0x93,0xa1,0x12,0x2f,0x00,
|
||||
0x99,0x42,0xd8,0x5c,0x1c,0x2f,0xb6,0x3c,0xd9,0x94,0x61,0xb4,0x55,0x8d,0x4e,
|
||||
0x73,0xe6,0x69,0xbc,0x1d,0x33,0xe3,0x2d,0x91,0x23,0x69,0x95,0x98,0xd7,0x18,
|
||||
@@ -187,64 +187,37 @@ module.exports = () => describe('API functional testing', function() {
|
||||
0xda,0xba,0x19,0xf3,0xcb,0x10,0xa0,0x6b,0xd0,0x2d,0xbe,0x40,0x42,0x7b,0x9b,
|
||||
0x15,0xa4,0x2d,0xec,0xcf,0x09,0xd6,0xe3,0x92,0xc3,0x8d,0x65,0x6b,0x60,0x97,
|
||||
0xda,0x6b,0xca])
|
||||
];
|
||||
]);
|
||||
|
||||
const ElgamalsecMPIstrs = [
|
||||
const elGamalPrivateKeyMaterial = util.concatUint8Array([
|
||||
new Uint8Array([0x01,0x52,0x02,0x80,0x87,0xf6,0xe4,0x49,0xd7,0x2e,0x3e,0xfe,0x60,0xb9,0xa3,
|
||||
0x2a,0xf0,0x67,0x58,0xe9,0xf6,0x47,0x83,0xde,0x7e,0xfb,0xbb,0xbd,0xdf,0x48,
|
||||
0x12,0x1b,0x06,0x7d,0x13,0xbc,0x3b,0x49,0xf9,0x86,0xd4,0x53,0xed,0x2d,0x68])
|
||||
];
|
||||
]);
|
||||
|
||||
const RSApubMPIs = [];
|
||||
let i;
|
||||
for (i = 0; i < 2; i++) {
|
||||
RSApubMPIs[i] = new openpgp.MPI();
|
||||
RSApubMPIs[i].read(RSApubMPIstrs[i]);
|
||||
}
|
||||
const algoRSA = openpgp.enums.publicKey.rsaEncryptSign;
|
||||
const RSAPublicParams = crypto.parsePublicKeyParams(algoRSA, RSAPublicKeyMaterial).publicParams;
|
||||
const RSAPrivateParams = crypto.parsePrivateKeyParams(algoRSA, RSAPrivateKeyMaterial).privateParams;
|
||||
|
||||
const RSAsecMPIs = [];
|
||||
for (i = 0; i < 4; i++) {
|
||||
RSAsecMPIs[i] = new openpgp.MPI();
|
||||
RSAsecMPIs[i].read(RSAsecMPIstrs[i]);
|
||||
}
|
||||
const algoDSA = openpgp.enums.publicKey.dsa;
|
||||
const DSAPublicParams = crypto.parsePublicKeyParams(algoDSA, DSAPublicKeyMaterial).publicParams;
|
||||
const DSAPrivateParams = crypto.parsePrivateKeyParams(algoDSA, DSAPrivateKeyMaterial).privateParams;
|
||||
|
||||
const DSAsecMPIs = [];
|
||||
for (i = 0; i < 1; i++) {
|
||||
DSAsecMPIs[i] = new openpgp.MPI();
|
||||
DSAsecMPIs[i].read(DSAsecMPIstrs[i]);
|
||||
}
|
||||
|
||||
const DSApubMPIs = [];
|
||||
for (i = 0; i < 4; i++) {
|
||||
DSApubMPIs[i] = new openpgp.MPI();
|
||||
DSApubMPIs[i].read(DSApubMPIstrs[i]);
|
||||
}
|
||||
const ElgamalsecMPIs = [];
|
||||
for (i = 0; i < 1; i++) {
|
||||
ElgamalsecMPIs[i] = new openpgp.MPI();
|
||||
ElgamalsecMPIs[i].read(ElgamalsecMPIstrs[i]);
|
||||
}
|
||||
|
||||
const ElgamalpubMPIs = [];
|
||||
for (i = 0; i < 3; i++) {
|
||||
ElgamalpubMPIs[i] = new openpgp.MPI();
|
||||
ElgamalpubMPIs[i].read(ElgamalpubMPIstrs[i]);
|
||||
}
|
||||
const algoElGamal = openpgp.enums.publicKey.elgamal;
|
||||
const elGamalPublicParams = crypto.parsePublicKeyParams(algoElGamal, elGamalPublicKeyMaterial).publicParams;
|
||||
const elGamalPrivateParams = crypto.parsePrivateKeyParams(algoElGamal, elGamalPrivateKeyMaterial).privateParams;
|
||||
|
||||
const data = util.strToUint8Array("foobar");
|
||||
|
||||
describe('Sign and verify', function () {
|
||||
it('RSA', async function () {
|
||||
// FIXME
|
||||
//Originally we passed public and secret MPI separately, now they are joined. Is this what we want to do long term?
|
||||
// RSA
|
||||
return crypto.signature.sign(
|
||||
1, 2, RSApubMPIs.concat(RSAsecMPIs), data, await crypto.hash.digest(2, data)
|
||||
1, 2, RSAPublicParams, RSAPrivateParams, data, await crypto.hash.digest(2, data)
|
||||
).then(async RSAsignedData => {
|
||||
const RSAsignedDataMPI = new openpgp.MPI();
|
||||
RSAsignedDataMPI.read(RSAsignedData);
|
||||
return crypto.signature.verify(
|
||||
1, 2, [RSAsignedDataMPI], RSApubMPIs, data, await crypto.hash.digest(2, data)
|
||||
1, 2, [RSAsignedDataMPI], RSAPublicParams, data, await crypto.hash.digest(2, data)
|
||||
).then(success => {
|
||||
return expect(success).to.be.true;
|
||||
});
|
||||
@@ -252,9 +225,8 @@ module.exports = () => describe('API functional testing', function() {
|
||||
});
|
||||
|
||||
it('DSA', async function () {
|
||||
// DSA
|
||||
return crypto.signature.sign(
|
||||
17, 2, DSApubMPIs.concat(DSAsecMPIs), data, await crypto.hash.digest(2, data)
|
||||
17, 2, DSAPublicParams, DSAPrivateParams, data, await crypto.hash.digest(2, data)
|
||||
).then(async DSAsignedData => {
|
||||
DSAsignedData = util.uint8ArrayToStr(DSAsignedData);
|
||||
const DSAmsgMPIs = [];
|
||||
@@ -263,7 +235,7 @@ module.exports = () => describe('API functional testing', function() {
|
||||
DSAmsgMPIs[0].read(DSAsignedData.substring(0,34));
|
||||
DSAmsgMPIs[1].read(DSAsignedData.substring(34,68));
|
||||
return crypto.signature.verify(
|
||||
17, 2, DSAmsgMPIs, DSApubMPIs, data, await crypto.hash.digest(2, data)
|
||||
17, 2, DSAmsgMPIs, DSAPublicParams, data, await crypto.hash.digest(2, data)
|
||||
).then(success => {
|
||||
return expect(success).to.be.true;
|
||||
});
|
||||
@@ -356,9 +328,9 @@ module.exports = () => describe('API functional testing', function() {
|
||||
|
||||
it('Asymmetric using RSA with eme_pkcs1 padding', async function () {
|
||||
const symmKey = await crypto.generateSessionKey('aes256');
|
||||
return crypto.publicKeyEncrypt(1, RSApubMPIs, symmKey).then(RSAEncryptedData => {
|
||||
return crypto.publicKeyEncrypt(algoRSA, RSAPublicParams, symmKey).then(RSAEncryptedData => {
|
||||
return crypto.publicKeyDecrypt(
|
||||
1, RSApubMPIs.concat(RSAsecMPIs), RSAEncryptedData
|
||||
algoRSA, RSAPublicParams, RSAPrivateParams, RSAEncryptedData
|
||||
).then(data => {
|
||||
expect(data).to.deep.equal(symmKey);
|
||||
});
|
||||
@@ -367,9 +339,9 @@ module.exports = () => describe('API functional testing', function() {
|
||||
|
||||
it('Asymmetric using Elgamal with eme_pkcs1 padding', async function () {
|
||||
const symmKey = await crypto.generateSessionKey('aes256');
|
||||
return crypto.publicKeyEncrypt(16, ElgamalpubMPIs, symmKey).then(ElgamalEncryptedData => {
|
||||
return crypto.publicKeyEncrypt(algoElGamal, elGamalPublicParams, symmKey).then(ElgamalEncryptedData => {
|
||||
return crypto.publicKeyDecrypt(
|
||||
16, ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedData
|
||||
algoElGamal, elGamalPublicParams, elGamalPrivateParams, ElgamalEncryptedData
|
||||
).then(data => {
|
||||
expect(data).to.deep.equal(symmKey);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user