Skip to content

Commit 85994cd

Browse files
roadicingljharb
andcommittedOct 21, 2023
[Fix] properly check the upper bound for DSA signatures
Co-authored-by: roadicing <roadicing@gmail.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
1 parent 9ac5a5e commit 85994cd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎browser/verify.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function dsaVerify(sig, hash, pub) {
8080

8181
function checkValue(b, q) {
8282
if (b.cmpn(0) <= 0) { throw new Error('invalid sig'); }
83-
if (b.cmp(q) >= q) { throw new Error('invalid sig'); }
83+
if (b.cmp(q) >= 0) { throw new Error('invalid sig'); }
8484
}
8585

8686
module.exports = verify;

‎test/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var asn1 = require('parse-asn1/asn1');
55
var test = require('tape').test;
66
var nCrypto = require('crypto');
77
var semver = require('semver');
8+
var BN = require('bn.js');
9+
var parseKeys = require('parse-asn1');
10+
811
var bCrypto = require('../browser');
912
var fixtures = require('./fixtures');
1013

@@ -154,6 +157,35 @@ fixtures.valid.ec.forEach(function (f) {
154157
t.end();
155158
});
156159
}
160+
161+
var s = parseKeys(pub).data.q;
162+
test(
163+
f.message + ' against a fake signature',
164+
{ skip: !s || '(this test only applies to DSA signatures and not EC signatures, this is ' + f.scheme + ')' },
165+
function (t) {
166+
var messageBase64 = Buffer.from(f.message, 'base64');
167+
168+
// forge a fake signature
169+
var r = new BN('1');
170+
171+
try {
172+
var fakeSig = asn1.signature.encode({ r: r, s: s }, 'der');
173+
} catch (e) {
174+
t.ifError(e);
175+
t.end();
176+
return;
177+
}
178+
179+
var bVer = bCrypto.createVerify(f.scheme);
180+
t['throws'](
181+
function () { bVer.update(messageBase64).verify(pub, fakeSig); },
182+
Error,
183+
'fake signature is invalid'
184+
);
185+
186+
t.end();
187+
}
188+
);
157189
});
158190

159191
fixtures.valid.kvectors.forEach(function (f) {

0 commit comments

Comments
 (0)
Please sign in to comment.