Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const testSignature = function (msg) {
const sig = cryptoUtils.sign(key, cryptoUtils.hash(msg));
if (sig) {
// test that signatures have low-S
const halfOrder = halfOrdersForCurve[key._key.ecparams.name];
const sigObject = new Signature(sig);
if (sigObject.s.cmp(halfOrder) === 1) {
t.fail('Invalid signature object: S value larger than N/2');
} else {
t.pass('Valid signature object generated from sign()');
}
// using internal calls to verify the signature
const pubKey = cryptoUtils._ecdsa.keyFromPublic(key.getPublicKey()._key.pubKeyHex, 'hex');
// note that the signature is generated on the hash of the message, not the message itself
t.equal(pubKey.verify(cryptoUtils.hash(msg), Buffer.from(sig)), true,
'CryptoSuite_ECDSA_AES function tests: sign() method produced proper signature that was successfully verified');
} else {
t.fail('Invalid signature generated by sign()');
}
};
var testSignature = function (msg) {
var sig = cryptoUtils.sign(key, cryptoUtils.hash(msg));
if (sig) {
// test that signatures have low-S
var halfOrder = halfOrdersForCurve[key._key.ecparams.name];
var sigObject = new Signature(sig);
if (sigObject.s.cmp(halfOrder) == 1) {
t.fail('Invalid signature object: S value larger than N/2');
} else {
t.pass('Valid signature object generated from sign()');
}
// using internal calls to verify the signature
var pubKey = cryptoUtils._ecdsa.keyFromPublic(key.getPublicKey()._key.pubKeyHex, 'hex');
// note that the signature is generated on the hash of the message, not the message itself
t.equal(pubKey.verify(cryptoUtils.hash(msg), new Buffer(sig)), true,
'CryptoSuite_ECDSA_AES function tests: sign() method produced proper signature that was successfully verified');
} else {
t.fail('Invalid signature generated by sign()');
}
};
function _checkMalleability(sig, curveParams) {
const curve = curveParams.name;
const halfOrder = halfOrdersForCurve[curve];
if (!halfOrder) {
throw new Error(`Can not find the half order needed to calculate "s" value for immalleable signatures. Unsupported curve name: ${curve}`);
}
// first need to unmarshall the signature bytes into the object with r and s values
const sigObject = new Signature(sig, 'hex');
if (!sigObject.r || !sigObject.s) {
throw new Error('Failed to load the signature object from the bytes.');
}
// in order to guarantee 's' falls in the lower range of the order, as explained in the above link,
// first see if 's' is larger than half of the order, if so, it is considered invalid in this context
if (sigObject.s.cmp(halfOrder) === 1) { // module 'bn.js', file lib/bn.js, method cmp()
return false;
}
return true;
}
function _checkMalleability(sig, curveParams) {
var halfOrder = halfOrdersForCurve[curveParams.name];
if (!halfOrder) {
throw new Error('Can not find the half order needed to calculate "s" value for immalleable signatures. Unsupported curve name: ' + curve);
}
// first need to unmarshall the signature bytes into the object with r and s values
var sigObject = new Signature(sig, 'hex');
if (!sigObject.r || !sigObject.s) {
throw new Error('Failed to load the signature object from the bytes.');
}
// in order to guarantee 's' falls in the lower range of the order, as explained in the above link,
// first see if 's' is larger than half of the order, if so, it is considered invalid in this context
if (sigObject.s.cmp(halfOrder) == 1) { // module 'bn.js', file lib/bn.js, method cmp()
return false;
}
return true;
}
function _checkMalleability(sig, curveParams) {
const halfOrder = ordersForCurve[curveParams.name].halfOrder;
if (!halfOrder) {
throw new Error('Can not find the half order needed to calculate "s" value for immalleable signatures. Unsupported curve name: ' + curveParams.name);
}
// first need to unmarshall the signature bytes into the object with r and s values
const sigObject = new Signature(sig, 'hex');
if (!sigObject.r || !sigObject.s) {
throw new Error('Failed to load the signature object from the bytes.');
}
// in order to guarantee 's' falls in the lower range of the order, as explained in the above link,
// first see if 's' is larger than half of the order, if so, it is considered invalid in this context
if (sigObject.s.cmp(halfOrder) === 1) { // module 'bn.js', file lib/bn.js, method cmp()
return false;
}
return true;
}
_pkcs11Verify(pkcs11, pkcs11Session, key, digest, signature) {
try {
/*
* Restore ASN1 DER signature to raw signature.
* Error will be thrown if signature is not properly encoded.
*/
const rns = new ecsig(signature, 'hex');
logger.debug(__func() + 'ECDSA R+S signature: ' +
util.inspect(rns, {depth: null}));
const sig = Buffer.concat([rns.r.toArrayLike(Buffer, '', 0),
rns.s.toArrayLike(Buffer, '', 0)]);
logger.debug(__func() + 'ECDSA RAW signature: ' +
util.inspect(sig, {depth: null}));
/*
* key can be either a private or a public key.
*/
pkcs11.C_VerifyInit(pkcs11Session,
{mechanism: pkcs11js.CKM_ECDSA},
key._handle);
return pkcs11.C_Verify(pkcs11Session, digest, sig);
} catch (e) {
/*
* Error is thrown when signature verification fails.
/*
* key has been checked to be an ECDSA private key.
*/
pkcs11.C_SignInit(pkcs11Session, {mechanism: pkcs11js.CKM_ECDSA},
key._handle);
const sig = pkcs11.C_Sign(pkcs11Session, digest,
Buffer.alloc(this._keySize));
logger.debug(__func() + 'ECDSA RAW signature: ' +
util.inspect(sig, {depth: null}));
/*
* ASN1 DER encoding against malleability.
*/
const r = new BN(sig.slice(0, sig.length / 2).toString('hex'), 16);
const s = new BN(sig.slice(sig.length / 2).toString('hex'), 16);
const signature = _preventMalleability({r: r, s: s}, this._ecdsaCurve);
const der = (new ecsig({r: signature.r, s: signature.s})).toDER();
logger.debug(__func() + 'ECDSA DER signature: ' +
util.inspect(Buffer.from(der), {depth: null}));
return Buffer.from(der);
}