Clean up CurvesWithOID

This commit is contained in:
larabr 2024-02-01 18:08:21 +01:00
parent fb710bc2dc
commit aa9b009d91
3 changed files with 23 additions and 31 deletions

View File

@ -238,24 +238,24 @@ async function jsPublicEphemeralKey(curve, Q) {
* @async
*/
async function webPrivateEphemeralKey(curve, V, Q, d) {
const recipient = privateToJWK(curve.payloadSize, curve.web.web, Q, d);
const recipient = privateToJWK(curve.payloadSize, curve.web, Q, d);
let privateKey = webCrypto.importKey(
'jwk',
recipient,
{
name: 'ECDH',
namedCurve: curve.web.web
namedCurve: curve.web
},
true,
['deriveKey', 'deriveBits']
);
const jwk = rawPublicToJWK(curve.payloadSize, curve.web.web, V);
const jwk = rawPublicToJWK(curve.payloadSize, curve.web, V);
let sender = webCrypto.importKey(
'jwk',
jwk,
{
name: 'ECDH',
namedCurve: curve.web.web
namedCurve: curve.web
},
true,
[]
@ -264,11 +264,11 @@ async function webPrivateEphemeralKey(curve, V, Q, d) {
let S = webCrypto.deriveBits(
{
name: 'ECDH',
namedCurve: curve.web.web,
namedCurve: curve.web,
public: sender
},
privateKey,
curve.web.sharedSize
curve.sharedSize
);
let secret = webCrypto.exportKey(
'jwk',
@ -289,11 +289,11 @@ async function webPrivateEphemeralKey(curve, V, Q, d) {
* @async
*/
async function webPublicEphemeralKey(curve, Q) {
const jwk = rawPublicToJWK(curve.payloadSize, curve.web.web, Q);
const jwk = rawPublicToJWK(curve.payloadSize, curve.web, Q);
let keyPair = webCrypto.generateKey(
{
name: 'ECDH',
namedCurve: curve.web.web
namedCurve: curve.web
},
true,
['deriveKey', 'deriveBits']
@ -303,7 +303,7 @@ async function webPublicEphemeralKey(curve, Q) {
jwk,
{
name: 'ECDH',
namedCurve: curve.web.web
namedCurve: curve.web
},
false,
[]
@ -312,11 +312,11 @@ async function webPublicEphemeralKey(curve, Q) {
let s = webCrypto.deriveBits(
{
name: 'ECDH',
namedCurve: curve.web.web,
namedCurve: curve.web,
public: recipient
},
keyPair.privateKey,
curve.web.sharedSize
curve.sharedSize
);
let p = webCrypto.exportKey(
'jwk',
@ -338,7 +338,7 @@ async function webPublicEphemeralKey(curve, Q) {
* @async
*/
async function nodePrivateEphemeralKey(curve, V, d) {
const recipient = nodeCrypto.createECDH(curve.node.node);
const recipient = nodeCrypto.createECDH(curve.node);
recipient.setPrivateKey(d);
const sharedKey = new Uint8Array(recipient.computeSecret(V));
const secretKey = new Uint8Array(recipient.getPrivateKey());
@ -354,7 +354,7 @@ async function nodePrivateEphemeralKey(curve, V, d) {
* @async
*/
async function nodePublicEphemeralKey(curve, Q) {
const sender = nodeCrypto.createECDH(curve.node.node);
const sender = nodeCrypto.createECDH(curve.node);
sender.generateKeys();
const sharedKey = new Uint8Array(sender.computeSecret(Q));
const publicKey = new Uint8Array(sender.getPublicKey());

View File

@ -129,32 +129,25 @@ const curves = {
};
class CurveWithOID {
constructor(oidOrName, params) {
constructor(oidOrName) {
try {
if (util.isArray(oidOrName) ||
util.isUint8Array(oidOrName)) {
// by oid byte array
oidOrName = new OID(oidOrName);
}
if (oidOrName instanceof OID) {
// by curve OID
oidOrName = oidOrName.getName();
}
// by curve name or oid string
this.name = enums.write(enums.curve, oidOrName);
this.name = oidOrName instanceof OID ?
oidOrName.getName() :
enums.write(enums.curve,oidOrName);
} catch (err) {
throw new UnsupportedError('Unknown curve');
}
params = params || curves[this.name];
const params = curves[this.name];
this.keyType = params.keyType;
this.oid = params.oid;
this.hash = params.hash;
this.cipher = params.cipher;
this.node = params.node && curves[this.name];
this.web = params.web && curves[this.name];
this.node = params.node;
this.web = params.web;
this.payloadSize = params.payloadSize;
this.sharedSize = params.sharedSize;
if (this.web && util.getWebCrypto()) {
this.type = 'web';
} else if (this.node && util.getNodeCrypto()) {

View File

@ -241,7 +241,6 @@ export default () => describe('Elliptic Curve Cryptography @lightweight', functi
});
const curves = ['secp256k1' , 'nistP256', 'nistP384', 'nistP521', 'brainpoolP256r1', 'brainpoolP384r1', 'brainpoolP512r1'];
curves.forEach(curveName => it(`${curveName} - Sign and verify message`, async function () {
const curve = new elliptic_curves.CurveWithOID(curveName);
const { Q: keyPublic, secret: keyPrivate } = await elliptic_curves.generate(curveName);
const message = new Uint8Array([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
@ -249,8 +248,8 @@ export default () => describe('Elliptic Curve Cryptography @lightweight', functi
]);
const messageDigest = await hashMod.digest(openpgp.enums.hash.sha512, message);
await testNativeAndFallback(async () => {
const signature = await elliptic_curves.ecdsa.sign(curve.oid, openpgp.enums.hash.sha512, message, keyPublic, keyPrivate, messageDigest);
await expect(elliptic_curves.ecdsa.verify(curve.oid, openpgp.enums.hash.sha512, signature, message, keyPublic, messageDigest)).to.eventually.be.true;
const signature = await elliptic_curves.ecdsa.sign(curveName, openpgp.enums.hash.sha512, message, keyPublic, keyPrivate, messageDigest);
await expect(elliptic_curves.ecdsa.verify(curveName, openpgp.enums.hash.sha512, signature, message, keyPublic, messageDigest)).to.eventually.be.true;
});
}));
});