Drop internal OID hex mapping from enums.curves

This commit is contained in:
larabr 2024-02-01 16:48:12 +01:00
parent c499c791af
commit fb710bc2dc
3 changed files with 47 additions and 43 deletions

View File

@ -49,52 +49,52 @@ const nodeCurves = nodeCrypto ? {
} : {}; } : {};
const curves = { const curves = {
nistP256: { [enums.curve.nistP256]: {
oid: [0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07], oid: [0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha256, hash: enums.hash.sha256,
cipher: enums.symmetric.aes128, cipher: enums.symmetric.aes128,
node: nodeCurves.nistP256, node: nodeCurves[enums.curve.nistP256],
web: webCurves.nistP256, web: webCurves[enums.curve.nistP256],
payloadSize: 32, payloadSize: 32,
sharedSize: 256 sharedSize: 256
}, },
nistP384: { [enums.curve.nistP384]: {
oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22], oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha384, hash: enums.hash.sha384,
cipher: enums.symmetric.aes192, cipher: enums.symmetric.aes192,
node: nodeCurves.nistP384, node: nodeCurves[enums.curve.nistP384],
web: webCurves.nistP384, web: webCurves[enums.curve.nistP384],
payloadSize: 48, payloadSize: 48,
sharedSize: 384 sharedSize: 384
}, },
nistP521: { [enums.curve.nistP521]: {
oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23], oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha512, hash: enums.hash.sha512,
cipher: enums.symmetric.aes256, cipher: enums.symmetric.aes256,
node: nodeCurves.nistP521, node: nodeCurves[enums.curve.nistP521],
web: webCurves.nistP521, web: webCurves[enums.curve.nistP521],
payloadSize: 66, payloadSize: 66,
sharedSize: 528 sharedSize: 528
}, },
secp256k1: { [enums.curve.secp256k1]: {
oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x0A], oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x0A],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha256, hash: enums.hash.sha256,
cipher: enums.symmetric.aes128, cipher: enums.symmetric.aes128,
node: nodeCurves.secp256k1, node: nodeCurves[enums.curve.secp256k1],
payloadSize: 32 payloadSize: 32
}, },
ed25519Legacy: { [enums.curve.ed25519Legacy]: {
oid: [0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xDA, 0x47, 0x0F, 0x01], oid: [0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xDA, 0x47, 0x0F, 0x01],
keyType: enums.publicKey.eddsaLegacy, keyType: enums.publicKey.eddsaLegacy,
hash: enums.hash.sha512, hash: enums.hash.sha512,
node: false, // nodeCurves.ed25519 TODO node: false, // nodeCurves.ed25519 TODO
payloadSize: 32 payloadSize: 32
}, },
curve25519Legacy: { [enums.curve.curve25519Legacy]: {
oid: [0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01], oid: [0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01],
keyType: enums.publicKey.ecdh, keyType: enums.publicKey.ecdh,
hash: enums.hash.sha256, hash: enums.hash.sha256,
@ -102,28 +102,28 @@ const curves = {
node: false, // nodeCurves.curve25519 TODO node: false, // nodeCurves.curve25519 TODO
payloadSize: 32 payloadSize: 32
}, },
brainpoolP256r1: { [enums.curve.brainpoolP256r1]: {
oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07], oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha256, hash: enums.hash.sha256,
cipher: enums.symmetric.aes128, cipher: enums.symmetric.aes128,
node: nodeCurves.brainpoolP256r1, node: nodeCurves[enums.curve.brainpoolP256r1],
payloadSize: 32 payloadSize: 32
}, },
brainpoolP384r1: { [enums.curve.brainpoolP384r1]: {
oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B], oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha384, hash: enums.hash.sha384,
cipher: enums.symmetric.aes192, cipher: enums.symmetric.aes192,
node: nodeCurves.brainpoolP384r1, node: nodeCurves[enums.curve.brainpoolP384r1],
payloadSize: 48 payloadSize: 48
}, },
brainpoolP512r1: { [enums.curve.brainpoolP512r1]: {
oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D], oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D],
keyType: enums.publicKey.ecdsa, keyType: enums.publicKey.ecdsa,
hash: enums.hash.sha512, hash: enums.hash.sha512,
cipher: enums.symmetric.aes256, cipher: enums.symmetric.aes256,
node: nodeCurves.brainpoolP512r1, node: nodeCurves[enums.curve.brainpoolP512r1],
payloadSize: 64 payloadSize: 64
} }
}; };
@ -217,7 +217,7 @@ async function generate(curveName) {
* @returns {enums.hash} hash algorithm * @returns {enums.hash} hash algorithm
*/ */
function getPreferredHashAlgo(oid) { function getPreferredHashAlgo(oid) {
return curves[enums.write(enums.curve, oid.toHex())].hash; return curves[oid.getName()].hash;
} }
/** /**
@ -232,14 +232,14 @@ function getPreferredHashAlgo(oid) {
*/ */
async function validateStandardParams(algo, oid, Q, d) { async function validateStandardParams(algo, oid, Q, d) {
const supportedCurves = { const supportedCurves = {
nistP256: true, [enums.curve.nistP256]: true,
nistP384: true, [enums.curve.nistP384]: true,
nistP521: true, [enums.curve.nistP521]: true,
secp256k1: true, [enums.curve.secp256k1]: true,
curve25519Legacy: algo === enums.publicKey.ecdh, [enums.curve.curve25519Legacy]: algo === enums.publicKey.ecdh,
brainpoolP256r1: true, [enums.curve.brainpoolP256r1]: true,
brainpoolP384r1: true, [enums.curve.brainpoolP384r1]: true,
brainpoolP512r1: true [enums.curve.brainpoolP512r1]: true
}; };
// Check whether the given curve is supported // Check whether the given curve is supported
@ -262,7 +262,7 @@ async function validateStandardParams(algo, oid, Q, d) {
return true; return true;
} }
const nobleCurve = await util.getNobleCurve(enums.publicKey.ecdsa, enums.write(enums.curve, oid.toHex())); // excluding curve25519Legacy, ecdh and ecdsa use the same curves const nobleCurve = await util.getNobleCurve(enums.publicKey.ecdsa, curveName); // excluding curve25519Legacy, ecdh and ecdsa use the same curves
/* /*
* Re-derive public point Q' = dG from private key * Re-derive public point Q' = dG from private key
* Expect Q == Q' * Expect Q == Q'

View File

@ -16,46 +16,37 @@ export default {
'nistP256': 'nistP256', 'nistP256': 'nistP256',
/** @deprecated use `nistP256` instead */ /** @deprecated use `nistP256` instead */
'p256': 'nistP256', 'p256': 'nistP256',
'2a8648ce3d030107': 'nistP256',
/** NIST P-384 Curve */ /** NIST P-384 Curve */
'nistP384': 'nistP384', 'nistP384': 'nistP384',
/** @deprecated use `nistP384` instead */ /** @deprecated use `nistP384` instead */
'p384': 'nistP384', 'p384': 'nistP384',
'2b81040022': 'nistP384',
/** NIST P-521 Curve */ /** NIST P-521 Curve */
'nistP521': 'nistP521', 'nistP521': 'nistP521',
/** @deprecated use `nistP521` instead */ /** @deprecated use `nistP521` instead */
'p521': 'nistP521', 'p521': 'nistP521',
'2b81040023': 'nistP521',
/** SECG SECP256k1 Curve */ /** SECG SECP256k1 Curve */
'secp256k1': 'secp256k1', 'secp256k1': 'secp256k1',
'2b8104000a': 'secp256k1',
/** Ed25519 - deprecated by crypto-refresh (replaced by standaone Ed25519 algo) */ /** Ed25519 - deprecated by crypto-refresh (replaced by standaone Ed25519 algo) */
'ed25519Legacy': 'ed25519Legacy', 'ed25519Legacy': 'ed25519Legacy',
/** @deprecated use `ed25519Legacy` instead */ /** @deprecated use `ed25519Legacy` instead */
'ed25519': 'ed25519Legacy', 'ed25519': 'ed25519Legacy',
'2b06010401da470f01': 'ed25519Legacy',
/** Curve25519 - deprecated by crypto-refresh (replaced by standaone X25519 algo) */ /** Curve25519 - deprecated by crypto-refresh (replaced by standaone X25519 algo) */
'curve25519Legacy': 'curve25519Legacy', 'curve25519Legacy': 'curve25519Legacy',
/** @deprecated use `curve25519Legacy` instead */ /** @deprecated use `curve25519Legacy` instead */
'curve25519': 'curve25519Legacy', 'curve25519': 'curve25519Legacy',
'2b060104019755010501': 'curve25519Legacy',
/** BrainpoolP256r1 Curve */ /** BrainpoolP256r1 Curve */
'brainpoolP256r1': 'brainpoolP256r1', 'brainpoolP256r1': 'brainpoolP256r1',
'2b2403030208010107': 'brainpoolP256r1',
/** BrainpoolP384r1 Curve */ /** BrainpoolP384r1 Curve */
'brainpoolP384r1': 'brainpoolP384r1', 'brainpoolP384r1': 'brainpoolP384r1',
'2b240303020801010b': 'brainpoolP384r1',
/** BrainpoolP512r1 Curve */ /** BrainpoolP512r1 Curve */
'2b240303020801010d': 'brainpoolP512r1',
'brainpoolP512r1': 'brainpoolP512r1' 'brainpoolP512r1': 'brainpoolP512r1'
}, },

View File

@ -35,6 +35,18 @@
import util from '../util'; import util from '../util';
import enums from '../enums'; import enums from '../enums';
const knownOIDs = {
'2a8648ce3d030107': enums.curve.nistP256,
'2b81040022': enums.curve.nistP384,
'2b81040023': enums.curve.nistP521,
'2b8104000a': enums.curve.secp256k1,
'2b06010401da470f01': enums.curve.ed25519Legacy,
'2b060104019755010501': enums.curve.curve25519Legacy,
'2b2403030208010107': enums.curve.brainpoolP256r1,
'2b240303020801010b': enums.curve.brainpoolP384r1,
'2b240303020801010d': enums.curve.brainpoolP512r1
};
class OID { class OID {
constructor(oid) { constructor(oid) {
if (oid instanceof OID) { if (oid instanceof OID) {
@ -88,15 +100,16 @@ class OID {
/** /**
* If a known curve object identifier, return the canonical name of the curve * If a known curve object identifier, return the canonical name of the curve
* @returns {string} String with the canonical name of the curve. * @returns {enums.curve} String with the canonical name of the curve
* @throws if unknown
*/ */
getName() { getName() {
const hex = this.toHex(); const name = knownOIDs[this.toHex()];
if (enums.curve[hex]) { if (!name) {
return enums.write(enums.curve, hex);
} else {
throw new Error('Unknown curve object identifier.'); throw new Error('Unknown curve object identifier.');
} }
return name;
} }
} }