1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/browserify-sign/browser/verify.js

87 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-01-16 21:26:16 +08:00
'use strict';
2023-12-18 13:12:25 +08:00
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
2024-01-16 21:26:16 +08:00
var Buffer = require('safe-buffer').Buffer;
var BN = require('bn.js');
var EC = require('elliptic').ec;
var parseKeys = require('parse-asn1');
var curves = require('./curves.json');
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
function verify(sig, hash, key, signType, tag) {
var pub = parseKeys(key);
2023-12-18 13:12:25 +08:00
if (pub.type === 'ec') {
// rsa keys can be interpreted as ecdsa ones in openssl
2024-01-16 21:26:16 +08:00
if (signType !== 'ecdsa' && signType !== 'ecdsa/rsa') { throw new Error('wrong public key type'); }
return ecVerify(sig, hash, pub);
2023-12-18 13:12:25 +08:00
} else if (pub.type === 'dsa') {
2024-01-16 21:26:16 +08:00
if (signType !== 'dsa') { throw new Error('wrong public key type'); }
return dsaVerify(sig, hash, pub);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
if (signType !== 'rsa' && signType !== 'ecdsa/rsa') { throw new Error('wrong public key type'); }
hash = Buffer.concat([tag, hash]);
var len = pub.modulus.byteLength();
var pad = [1];
var padNum = 0;
2023-12-18 13:12:25 +08:00
while (hash.length + pad.length + 2 < len) {
2024-01-16 21:26:16 +08:00
pad.push(0xff);
padNum += 1;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
pad.push(0x00);
var i = -1;
2023-12-18 13:12:25 +08:00
while (++i < hash.length) {
2024-01-16 21:26:16 +08:00
pad.push(hash[i]);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
pad = Buffer.from(pad);
var red = BN.mont(pub.modulus);
sig = new BN(sig).toRed(red);
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
sig = sig.redPow(new BN(pub.publicExponent));
sig = Buffer.from(sig.fromRed().toArray());
var out = padNum < 8 ? 1 : 0;
len = Math.min(sig.length, pad.length);
if (sig.length !== pad.length) { out = 1; }
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
i = -1;
while (++i < len) { out |= sig[i] ^ pad[i]; }
return out === 0;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
function ecVerify(sig, hash, pub) {
var curveId = curves[pub.data.algorithm.curve.join('.')];
if (!curveId) { throw new Error('unknown curve ' + pub.data.algorithm.curve.join('.')); }
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
var curve = new EC(curveId);
var pubkey = pub.data.subjectPrivateKey.data;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
return curve.verify(hash, sig, pubkey);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
function dsaVerify(sig, hash, pub) {
var p = pub.data.p;
var q = pub.data.q;
var g = pub.data.g;
var y = pub.data.pub_key;
var unpacked = parseKeys.signature.decode(sig, 'der');
var s = unpacked.s;
var r = unpacked.r;
checkValue(s, q);
checkValue(r, q);
var montp = BN.mont(p);
var w = s.invm(q);
2023-12-18 13:12:25 +08:00
var v = g.toRed(montp)
.redPow(new BN(hash).mul(w).mod(q))
.fromRed()
.mul(y.toRed(montp).redPow(r.mul(w).mod(q)).fromRed())
.mod(p)
2024-01-16 21:26:16 +08:00
.mod(q);
return v.cmp(r) === 0;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
function checkValue(b, q) {
if (b.cmpn(0) <= 0) { throw new Error('invalid sig'); }
if (b.cmp(q) >= 0) { throw new Error('invalid sig'); }
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
module.exports = verify;