Pass curve object instead of oid to checkPublicPointEnconding

This commit is contained in:
larabr 2024-05-16 17:06:57 +02:00
parent 89ce97a3e2
commit 698305c0ed
4 changed files with 12 additions and 11 deletions

View File

@ -131,7 +131,7 @@ export async function encrypt(oid, kdfParams, data, Q, fingerprint) {
const m = pkcs5.encode(data); const m = pkcs5.encode(data);
const curve = new CurveWithOID(oid); const curve = new CurveWithOID(oid);
checkPublicPointEnconding(oid, Q); checkPublicPointEnconding(curve, Q);
const { publicKey, sharedKey } = await genPublicEphemeralKey(curve, Q); const { publicKey, sharedKey } = await genPublicEphemeralKey(curve, Q);
const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint); const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint);
const { keySize } = getCipherParams(kdfParams.cipher); const { keySize } = getCipherParams(kdfParams.cipher);
@ -194,8 +194,8 @@ async function genPrivateEphemeralKey(curve, V, Q, d) {
*/ */
export async function decrypt(oid, kdfParams, V, C, Q, d, fingerprint) { export async function decrypt(oid, kdfParams, V, C, Q, d, fingerprint) {
const curve = new CurveWithOID(oid); const curve = new CurveWithOID(oid);
checkPublicPointEnconding(oid, Q); checkPublicPointEnconding(curve, Q);
checkPublicPointEnconding(oid, V); checkPublicPointEnconding(curve, V);
const { sharedKey } = await genPrivateEphemeralKey(curve, V, Q, d); const { sharedKey } = await genPrivateEphemeralKey(curve, V, Q, d);
const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint); const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint);
const { keySize } = getCipherParams(kdfParams.cipher); const { keySize } = getCipherParams(kdfParams.cipher);

View File

@ -46,7 +46,7 @@ const nodeCrypto = util.getNodeCrypto();
*/ */
export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed) { export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed) {
const curve = new CurveWithOID(oid); const curve = new CurveWithOID(oid);
checkPublicPointEnconding(oid, publicKey); checkPublicPointEnconding(curve, publicKey);
if (message && !util.isStream(message)) { if (message && !util.isStream(message)) {
const keyPair = { publicKey, privateKey }; const keyPair = { publicKey, privateKey };
switch (curve.type) { switch (curve.type) {
@ -93,7 +93,7 @@ export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed
*/ */
export async function verify(oid, hashAlgo, signature, message, publicKey, hashed) { export async function verify(oid, hashAlgo, signature, message, publicKey, hashed) {
const curve = new CurveWithOID(oid); const curve = new CurveWithOID(oid);
checkPublicPointEnconding(oid, publicKey); checkPublicPointEnconding(curve, publicKey);
// See https://github.com/openpgpjs/openpgpjs/pull/948. // See https://github.com/openpgpjs/openpgpjs/pull/948.
// NB: the impact was more likely limited to Brainpool curves, since thanks // NB: the impact was more likely limited to Brainpool curves, since thanks
// to WebCrypto availability, NIST curve should not have been affected. // to WebCrypto availability, NIST curve should not have been affected.

View File

@ -25,7 +25,7 @@ import nacl from '@openpgp/tweetnacl';
import util from '../../../util'; import util from '../../../util';
import enums from '../../../enums'; import enums from '../../../enums';
import hash from '../../hash'; import hash from '../../hash';
import { checkPublicPointEnconding } from './oid_curves'; import { CurveWithOID, checkPublicPointEnconding } from './oid_curves';
/** /**
* Sign a message using the provided legacy EdDSA key * Sign a message using the provided legacy EdDSA key
@ -42,7 +42,8 @@ import { checkPublicPointEnconding } from './oid_curves';
* @async * @async
*/ */
export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed) { export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed) {
checkPublicPointEnconding(oid, publicKey); const curve = new CurveWithOID(oid);
checkPublicPointEnconding(curve, publicKey);
if (hash.getHashByteLength(hashAlgo) < hash.getHashByteLength(enums.hash.sha256)) { if (hash.getHashByteLength(hashAlgo) < hash.getHashByteLength(enums.hash.sha256)) {
// see https://tools.ietf.org/id/draft-ietf-openpgp-rfc4880bis-10.html#section-15-7.2 // see https://tools.ietf.org/id/draft-ietf-openpgp-rfc4880bis-10.html#section-15-7.2
throw new Error('Hash algorithm too weak for EdDSA.'); throw new Error('Hash algorithm too weak for EdDSA.');
@ -69,7 +70,8 @@ export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed
* @async * @async
*/ */
export async function verify(oid, hashAlgo, { r, s }, m, publicKey, hashed) { export async function verify(oid, hashAlgo, { r, s }, m, publicKey, hashed) {
checkPublicPointEnconding(oid, publicKey); const curve = new CurveWithOID(oid);
checkPublicPointEnconding(curve, publicKey);
if (hash.getHashByteLength(hashAlgo) < hash.getHashByteLength(enums.hash.sha256)) { if (hash.getHashByteLength(hashAlgo) < hash.getHashByteLength(enums.hash.sha256)) {
throw new Error('Hash algorithm too weak for EdDSA.'); throw new Error('Hash algorithm too weak for EdDSA.');
} }

View File

@ -282,9 +282,8 @@ async function validateStandardParams(algo, oid, Q, d) {
* Check whether the public point has a valid encoding. * Check whether the public point has a valid encoding.
* NB: this function does not check e.g. whether the point belongs to the curve. * NB: this function does not check e.g. whether the point belongs to the curve.
*/ */
function checkPublicPointEnconding(oid, V) { function checkPublicPointEnconding(curve, V) {
const curveName = oid.getName(); const { payloadSize, wireFormatLeadingByte, name: curveName } = curve;
const { payloadSize, wireFormatLeadingByte } = curves[curveName];
const pointSize = (curveName === enums.curve.curve25519Legacy || curveName === enums.curve.ed25519Legacy) ? payloadSize : payloadSize * 2; const pointSize = (curveName === enums.curve.curve25519Legacy || curveName === enums.curve.ed25519Legacy) ? payloadSize : payloadSize * 2;