Skip to content

Commit

Permalink
other 3 curves
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Jan 28, 2015
1 parent d2a3f77 commit b78737b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 8 deletions.
8 changes: 8 additions & 0 deletions curves.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

exports['1.3.132.0.10'] = 'secp256k1'

exports['1.3.132.0.33'] = 'p224'

exports['1.2.840.10045.3.1.1'] = 'p192'

exports['1.2.840.10045.3.1.7'] = 'p256'
11 changes: 7 additions & 4 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var BN = require('bn.js')
var elliptic = require('elliptic')
var crt = require('browserify-rsa')
var createHmac = require('create-hmac')
var curves = require('./curves')

module.exports = sign
function sign (hash, key, hashType) {
Expand All @@ -28,10 +29,12 @@ function sign (hash, key, hashType) {
return out
}
function ecSign (hash, priv) {
var curve
if (priv.curve.join('.') === '1.3.132.0.10') {
curve = new elliptic.ec('secp256k1')
}
var curveId = curves[priv.curve.join('.')]
if (!curveId)
throw new Error('unknown curve ' + priv.curve.join('.'))

var curve = new elliptic.ec(curveId)

var key = curve.genKeyPair()
key._importPrivate(priv.privateKey)
var out = key.sign(hash)
Expand Down
Empty file added test/ec.ed25519.priv
Empty file.
8 changes: 8 additions & 0 deletions test/ec.p192.priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBAQ==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MF8CAQEEGI/bh7nWZQRAp0AIxBNqsMXz+DwJVKoM/aAKBggqhkjOPQMBAaE0AzIA
BFzxNC3rlmiF62MjpZfy/ab10lNiPaOL/dek9Xy+9M30oUJ/UjgQFwORngqesMDO
zw==
-----END EC PRIVATE KEY-----
4 changes: 4 additions & 0 deletions test/ec.p192.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEXPE0LeuWaIXrYyOll/L9pvXSU2I9
o4v916T1fL70zfShQn9SOBAXA5GeCp6wwM7P
-----END PUBLIC KEY-----
8 changes: 8 additions & 0 deletions test/ec.p224.priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BgUrgQQAIQ==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MGgCAQEEHGQ9hiazyngRvJ2EanIkar0nXNvW8miMLJKrF66gBwYFK4EEACGhPAM6
AASB1YtPpkhuE8bDeilf/BdR9GAjbBLHkjys8XrsiyA8mxHHbpd2axdkr2xranzV
xQrdZsW2WrIryQ==
-----END EC PRIVATE KEY-----
4 changes: 4 additions & 0 deletions test/ec.p224.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
ME4wEAYHKoZIzj0CAQYFK4EEACEDOgAEgdWLT6ZIbhPGw3opX/wXUfRgI2wSx5I8
rPF67IsgPJsRx26XdmsXZK9sa2p81cUK3WbFtlqyK8k=
-----END PUBLIC KEY-----
8 changes: 8 additions & 0 deletions test/ec.p256.priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIMaAq8nKT3IqVaY871MJS3yYP0XPDwEMN7dtRTwSQWh0oAoGCCqGSM49
AwEHoUQDQgAEYjGh+ItrT/vuufz2DabVwvbDl5u6jybK9YuBnzWEG2YGDEOU1fks
Spz6bUlYuycFT072o3nDqx5qRLoWpx8Gjg==
-----END EC PRIVATE KEY-----
4 changes: 4 additions & 0 deletions test/ec.p256.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEYjGh+ItrT/vuufz2DabVwvbDl5u6
jybK9YuBnzWEG2YGDEOU1fksSpz6bUlYuycFT072o3nDqx5qRLoWpx8Gjg==
-----END PUBLIC KEY-----
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ var ec = {
private: fs.readFileSync(__dirname + '/ec.priv'),
public: fs.readFileSync(__dirname + '/ec.pub')
}
var ec224 = {
private: fs.readFileSync(__dirname + '/ec.p224.priv'),
public: fs.readFileSync(__dirname + '/ec.p224.pub')
}
var ec192 = {
private: fs.readFileSync(__dirname + '/ec.p192.priv'),
public: fs.readFileSync(__dirname + '/ec.p192.pub')
}

var ec256 = {
private: fs.readFileSync(__dirname + '/ec.p256.priv'),
public: fs.readFileSync(__dirname + '/ec.p256.pub')
}
var ecpass = {
private: {
key: fs.readFileSync(__dirname + '/ec.pass.priv'),
Expand Down Expand Up @@ -115,6 +128,9 @@ ectestIt(dsa, new Buffer('dsa with 1024 keys'), 'DSA')
ectestIt(dsa2, new Buffer('dsa with 2048 keys'), 'DSA-SHA1')
testIt(rsa1024, new Buffer('md5 with 1024 keys'), 'RSA-MD5')
ectestIt(ec, new Buffer('ecdsa with sha1'), 'ecdsa-with-SHA1')
ectestIt(ec192, new Buffer('ecdsa with p192 key with sha1'), 'ecdsa-with-SHA1')
ectestIt(ec224, new Buffer('ecdsa with p224 key with sha1'), 'ecdsa-with-SHA1')
ectestIt(ec256, new Buffer('ecdsa with p256 key with sha1'), 'ecdsa-with-SHA1')
testIt(rsa1024, new Buffer('md5 with 1024 keys'), 'RSA-MD5')
testIt(rsa2028, new Buffer('md5 with 2028 keys'), 'RSA-MD5')
testIt(nonrsa1024, new Buffer('md5 with 1024 keys non-rsa key'), 'RSA-MD5')
Expand Down
11 changes: 7 additions & 4 deletions verify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
var parseKeys = require('parse-asn1')
var elliptic = require('elliptic')
var curves = require('./curves')
var BN = require('bn.js')
module.exports = verify
function verify (sig, hash, key) {
Expand Down Expand Up @@ -37,10 +38,12 @@ function verify (sig, hash, key) {
return !out
}
function ecVerify (sig, hash, pub) {
var curve
if (pub.data.algorithm.curve.join('.') === '1.3.132.0.10') {
curve = new elliptic.ec('secp256k1')
}
var curveId = curves[pub.data.algorithm.curve.join('.')]
if (!curveId)
throw new Error('unknown curve ' + pub.data.algorithm.curve.join('.'))

var curve = new elliptic.ec(curveId)

var pubkey = pub.data.subjectPrivateKey.data
return curve.verify(hash.toString('hex'), sig.toString('hex'), pubkey.toString('hex'))
}
Expand Down

0 comments on commit b78737b

Please sign in to comment.