Skip to content

Commit 04cb6f5

Browse files
authored
Merge commit from fork
1 parent b8a7edd commit 04cb6f5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/elliptic/ec/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,29 @@ EC.prototype.sign = function sign(msg, key, enc, options) {
115115
if (!options)
116116
options = {};
117117

118+
if (typeof msg !== 'string' && typeof msg !== 'number' && !BN.isBN(msg)) {
119+
assert(typeof msg === 'object' && msg && typeof msg.length === 'number',
120+
'Expected message to be an array-like, a hex string, or a BN instance');
121+
assert((msg.length >>> 0) === msg.length); // non-negative 32-bit integer
122+
for (var i = 0; i < msg.length; i++) assert((msg[i] & 255) === msg[i]);
123+
}
124+
118125
key = this.keyFromPrivate(key, enc);
119126
msg = this._truncateToN(msg, false, options.msgBitLength);
120127

128+
// Would fail further checks, but let's make the error message clear
129+
assert(!msg.isNeg(), 'Can not sign a negative message');
130+
121131
// Zero-extend key to provide enough entropy
122132
var bytes = this.n.byteLength();
123133
var bkey = key.getPrivate().toArray('be', bytes);
124134

125135
// Zero-extend nonce to have the same byte size as N
126136
var nonce = msg.toArray('be', bytes);
127137

138+
// Recheck nonce to be bijective to msg
139+
assert((new BN(nonce)).eq(msg), 'Can not sign message');
140+
128141
// Instantiate Hmac_DRBG
129142
var drbg = new HmacDRBG({
130143
hash: this.hash,

0 commit comments

Comments
 (0)