openpgpjs/test/general/ecc_nist.js
larabr f64dc3f35f
enums.curve: rename NIST curves (p256,p384,p521`) and clean up unused internal values (#1721)
This is a breaking change, as NIST curves identifiers and values in
`enums.curves` have been renamed:
- the identifiers `enums.curve.p256`, `.p384`, `.p521` are now marked as
`@deprecated`
- the new identifiers are, respectively: `enums.curve.nistP256`, `.nistP384`,
`.nistP521`.
- the corresponding values have been changed from `'p256'`,`'p384'`,`'p521'` to
`'nistP256'`, `'nistP384'`, `'nistP521'`.

Affected high-level API functions:
- in `generateKey`, the `options.curve` argument will expect the updated string
values
- `Key.getAlgorithmInfo()` will return the updated `curve` values
2024-02-02 12:54:24 +01:00

80 lines
3.8 KiB
JavaScript

import { use as chaiUse, expect } from 'chai';
import chaiAsPromised from 'chai-as-promised'; // eslint-disable-line import/newline-after-import
chaiUse(chaiAsPromised);
import openpgp from '../initOpenpgp.js';
import util from '../../src/util.js';
import * as input from './testInputs.js';
export default () => describe('Elliptic Curve Cryptography for NIST P-256,P-384,P-521 curves @lightweight', function () {
function omnibus() {
it('Omnibus NIST P-256 Test', async function () {
const testData = input.createSomeMessage();
const testData2 = input.createSomeMessage();
const { privateKey: hi, publicKey: pubHi } = await openpgp.generateKey({ userIDs: { name: 'Hi', email: 'hi@hel.lo' }, curve: 'nistP256', format: 'object' });
const { privateKey: bye, publicKey: pubBye } = await openpgp.generateKey({ userIDs: { name: 'Bye', email: 'bye@good.bye' }, curve: 'nistP256', format: 'object' });
const cleartextMessage = await openpgp.sign({ message: await openpgp.createCleartextMessage({ text: testData }), signingKeys: hi });
await openpgp.verify({
message: await openpgp.readCleartextMessage({ cleartextMessage }),
verificationKeys: pubHi
}).then(output => expect(output.signatures[0].verified).to.eventually.be.true);
// Verifying detached signature
await openpgp.verify({
message: await openpgp.createMessage({ text: util.removeTrailingSpaces(testData) }),
verificationKeys: pubHi,
signature: (await openpgp.readCleartextMessage({ cleartextMessage })).signature
}).then(output => expect(output.signatures[0].verified).to.eventually.be.true);
// Encrypting and signing
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: testData2 }),
encryptionKeys: [pubBye],
signingKeys: [hi]
});
// Decrypting and verifying
return openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: encrypted }),
decryptionKeys: bye,
verificationKeys: [pubHi]
}).then(async output => {
expect(output.data).to.equal(testData2);
await expect(output.signatures[0].verified).to.eventually.be.true;
});
});
}
omnibus();
it('Sign message', async function () {
const testData = input.createSomeMessage();
const options = { userIDs: { name: 'Hi', email: 'hi@hel.lo' }, curve: 'nistP256', format: 'object' };
const { privateKey, publicKey } = await openpgp.generateKey(options);
const signature = await openpgp.sign({ message: await openpgp.createCleartextMessage({ text: testData }), signingKeys: privateKey });
const msg = await openpgp.readCleartextMessage({ cleartextMessage: signature });
const result = await openpgp.verify({ message: msg, verificationKeys: publicKey });
expect(await result.signatures[0].verified).to.be.true;
});
it('Encrypt and sign message', async function () {
const testData = input.createSomeMessage();
let options = { userIDs: { name: 'Hi', email: 'hi@hel.lo' }, curve: 'nistP256', format: 'object' };
const firstKey = await openpgp.generateKey(options);
options = { userIDs: { name: 'Bye', email: 'bye@good.bye' }, curve: 'nistP256', format: 'object' };
const secondKey = await openpgp.generateKey(options);
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: testData }),
encryptionKeys: secondKey.publicKey,
signingKeys: firstKey.privateKey
});
const message = await openpgp.readMessage({ armoredMessage: encrypted });
const result = await openpgp.decrypt({ message, decryptionKeys: secondKey.privateKey, verificationKeys: firstKey.publicKey });
expect(await result.signatures[0].verified).to.be.true;
});
// TODO find test vectors
});