From aa9b009d91da167c18111bbd010179f4bd7f3974 Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:08:21 +0100 Subject: [PATCH] Clean up CurvesWithOID --- src/crypto/public_key/elliptic/ecdh.js | 26 ++++++++++---------- src/crypto/public_key/elliptic/oid_curves.js | 23 ++++++----------- test/crypto/elliptic.js | 5 ++-- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/crypto/public_key/elliptic/ecdh.js b/src/crypto/public_key/elliptic/ecdh.js index 7d749d63..cc4d3952 100644 --- a/src/crypto/public_key/elliptic/ecdh.js +++ b/src/crypto/public_key/elliptic/ecdh.js @@ -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()); diff --git a/src/crypto/public_key/elliptic/oid_curves.js b/src/crypto/public_key/elliptic/oid_curves.js index a3b98a09..5aeddfb6 100644 --- a/src/crypto/public_key/elliptic/oid_curves.js +++ b/src/crypto/public_key/elliptic/oid_curves.js @@ -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()) { diff --git a/test/crypto/elliptic.js b/test/crypto/elliptic.js index bc40eb8a..3f0a6383 100644 --- a/test/crypto/elliptic.js +++ b/test/crypto/elliptic.js @@ -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; }); })); });