Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for calling ECDSA signatures RSA signatures, cuz node allows it #33

Merged
merged 3 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.11"
- "0.12"
- "4"
- "5"
- "6"
- "7"
matrix:
include:
- node_js: "4"
Expand Down
8 changes: 4 additions & 4 deletions browser/algorithms.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "302d300d06096086480165030402040500041c"
},
"RSA-SHA224": {
"sign": "rsa",
"sign": "ecdsa/rsa",
"hash": "sha224",
"id": "302d300d06096086480165030402040500041c"
},
Expand All @@ -15,7 +15,7 @@
"id": "3031300d060960864801650304020105000420"
},
"RSA-SHA256": {
"sign": "rsa",
"sign": "ecdsa/rsa",
"hash": "sha256",
"id": "3031300d060960864801650304020105000420"
},
Expand All @@ -25,7 +25,7 @@
"id": "3041300d060960864801650304020205000430"
},
"RSA-SHA384": {
"sign": "rsa",
"sign": "ecdsa/rsa",
"hash": "sha384",
"id": "3041300d060960864801650304020205000430"
},
Expand All @@ -35,7 +35,7 @@
"id": "3051300d060960864801650304020305000440"
},
"RSA-SHA512": {
"sign": "rsa",
"sign": "ecdsa/rsa",
"hash": "sha512",
"id": "3051300d060960864801650304020305000440"
},
Expand Down
4 changes: 2 additions & 2 deletions browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Sign.prototype.update = function update (data, enc) {
Sign.prototype.sign = function signMethod (key, enc) {
this.end()
var hash = this._hash.digest()
var sig = sign(Buffer.concat([this._tag, hash]), key, this._hashType, this._signType)
var sig = sign(hash, key, this._hashType, this._signType, this._tag)

return enc ? sig.toString(enc) : sig
}
Expand Down Expand Up @@ -72,7 +72,7 @@ Verify.prototype.verify = function verifyMethod (key, sig, enc) {

this.end()
var hash = this._hash.digest()
return verify(sig, Buffer.concat([this._tag, hash]), key, this._signType)
return verify(sig, hash, key, this._signType, this._tag)
}

function createSign (algorithm) {
Expand Down
9 changes: 5 additions & 4 deletions browser/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ var BN = require('bn.js')
var parseKeys = require('parse-asn1')
var curves = require('./curves.json')

function sign (hash, key, hashType, signType) {
function sign (hash, key, hashType, signType, tag) {
var priv = parseKeys(key)
if (priv.curve) {
if (signType !== 'ecdsa') throw new Error('wrong private key type')
// rsa keys can be interpreted as ecdsa ones in openssl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here about why this is the case?
ecdsa/rsa seems strange

if (signType !== 'ecdsa' && signType !== 'ecdsa/rsa') throw new Error('wrong private key type')
return ecSign(hash, priv)
} else if (priv.type === 'dsa') {
if (signType !== 'dsa') throw new Error('wrong private key type')
return dsaSign(hash, priv, hashType)
} else {
if (signType !== 'rsa') throw new Error('wrong private key type')
if (signType !== 'rsa' && signType !== 'ecdsa/rsa') throw new Error('wrong private key type')
}

hash = Buffer.concat([tag, hash])
Copy link
Member

@dcousens dcousens Nov 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you move this in?

Doesn't really matter I suppose, just seems odd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah it's because we only need the tag when it is an rsa key NOT when it's an ecdsa key so since we can't tell based on the signature type asked for we have to do it based on what rutine they are using, so this code path only is executed for rsa signatures never (ec)dsa

var len = priv.modulus.byteLength()
var pad = [ 0, 1 ]
while (hash.length + pad.length + 1 < len) pad.push(0xff)
Expand Down
8 changes: 5 additions & 3 deletions browser/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ var EC = require('elliptic').ec
var parseKeys = require('parse-asn1')
var curves = require('./curves.json')

function verify (sig, hash, key, signType) {
function verify (sig, hash, key, signType, tag) {
var pub = parseKeys(key)
if (pub.type === 'ec') {
if (signType !== 'ecdsa') throw new Error('wrong public key type')
// rsa keys can be interpreted as ecdsa ones in openssl
if (signType !== 'ecdsa' && signType !== 'ecdsa/rsa') throw new Error('wrong public key type')
return ecVerify(sig, hash, pub)
} else if (pub.type === 'dsa') {
if (signType !== 'dsa') throw new Error('wrong public key type')
return dsaVerify(sig, hash, pub)
} else {
if (signType !== 'rsa') throw new Error('wrong public key type')
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
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ fixtures.valid.ec.forEach(function (f) {

t.end()
})
if (f.scheme !== 'DSA' && f.scheme.toLowerCase().indexOf('dsa') === -1) {
test(f.message + ' named rsa through', function (t) {
var scheme = 'RSA-' + f.scheme.toUpperCase()
var nSign = nCrypto.createSign(scheme)
var bSign = bCrypto.createSign(scheme)

var bSig = bSign.update(message).sign(priv)
var nSig = nSign.update(message).sign(priv)
t.notEqual(bSig.toString('hex'), nSig.toString('hex'), 'not equal sigs')
t.equals(bSig.toString('hex'), f.signature, 'sig is determanistic')

var nVer = nCrypto.createVerify(f.scheme)
t.ok(nVer.update(message).verify(pub, bSig), 'node validate browser sig')

var bVer = bCrypto.createVerify(f.scheme)
t.ok(bVer.update(message).verify(pub, nSig), 'browser validate node sig')

t.end()
})
}
})

fixtures.valid.kvectors.forEach(function (f) {
Expand Down