Skip to content

Commit

Permalink
standard format
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Sep 5, 2015
1 parent ddc0820 commit 4f8a8e9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 71 deletions.
70 changes: 40 additions & 30 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
'use strict'
var sign = require('./sign')
var verify = require('./verify')
var stream = require('stream')
var inherits = require('inherits')
var _algos = require('./algos')
var createHash = require('create-hash')
var inherits = require('inherits')
var sign = require('./sign')
var stream = require('stream')
var verify = require('./verify')

var algos = {}
Object.keys(_algos).forEach(function (key) {
algos[key] = algos[key.toLowerCase()] = _algos[key]
})

exports.createSign = exports.Sign = createSign

function createSign (algorithm) {
return new Sign(algorithm)
}

exports.createVerify = exports.Verify = createVerify

function createVerify (algorithm) {
return new Verify(algorithm)
}

inherits(Sign, stream.Writable)

function Sign (algorithm) {
stream.Writable.call(this)

var data = algos[algorithm]
if (!data)
if (!data) {
throw new Error('Unknown message digest')
}

this._hashType = data.hash
this._hash = createHash(data.hash)
this._tag = data.id
this._signType = data.sign
}
inherits(Sign, stream.Writable)

Sign.prototype._write = function _write (data, _, done) {
this._hash.update(data)
done()
}

Sign.prototype.update = function update (data, enc) {
if (typeof data === 'string')
if (typeof data === 'string') {
data = new Buffer(data, enc)
}

this._hash.update(data)
return this
}
Expand All @@ -52,42 +43,61 @@ 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)
if (enc) {
sig = sig.toString(enc)
}
return sig

return enc ? sig.toString(enc) : sig
}

inherits(Verify, stream.Writable)
function Verify (algorithm) {
stream.Writable.call(this)

var data = algos[algorithm]
if (!data)
if (!data) {
throw new Error('Unknown message digest')
}

this._hash = createHash(data.hash)
this._tag = data.id
this._signType = data.sign
}
inherits(Verify, stream.Writable)

Verify.prototype._write = function _write (data, _, done) {
this._hash.update(data)

done()
}

Verify.prototype.update = function update (data, enc) {
if (typeof data === 'string')
if (typeof data === 'string') {
data = new Buffer(data, enc)
}

this._hash.update(data)
return this
}

Verify.prototype.verify = function verifyMethod (key, sig, enc) {
if (typeof sig === 'string') {
sig = new Buffer(sig, enc)
}

this.end()
var hash = this._hash.digest()
if (typeof sig === 'string')
sig = new Buffer(sig, enc)

return verify(sig, Buffer.concat([this._tag, hash]), key, this._signType)
}

function createSign (algorithm) {
return new Sign(algorithm)
}

function createVerify (algorithm) {
return new Verify(algorithm)
}

module.exports = {
Sign: createSign,
Verify: createVerify,
createSign: createSign,
createVerify: createVerify
}
62 changes: 33 additions & 29 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@ var crt = require('browserify-rsa')
var createHmac = require('create-hmac')
var curves = require('./curves')

module.exports = sign
function sign (hash, key, hashType, signType) {
var priv = parseKeys(key)
if (priv.curve) {
if (signType !== 'ecdsa') {
throw new Error('wrong private key type')
}
if (signType !== 'ecdsa') throw new Error('wrong private key type')

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

var len = priv.modulus.byteLength()
var pad = [ 0, 1 ]
while (hash.length + pad.length + 1 < len) {
Expand All @@ -38,10 +32,12 @@ function sign (hash, key, hashType, signType) {
var out = crt(pad, priv)
return out
}

function ecSign (hash, priv) {
var curveId = curves[priv.curve.join('.')]
if (!curveId)
if (!curveId) {
throw new Error('unknown curve ' + priv.curve.join('.'))
}

var curve = new elliptic.ec(curveId)

Expand Down Expand Up @@ -125,6 +121,7 @@ function getKey (x, q, hash, algo) {
v: v
}
}

function bits2int (obits, q) {
var bits = new BN(obits)
var shift = (obits.length << 3) - q.bitLength()
Expand All @@ -133,6 +130,7 @@ function bits2int (obits, q) {
}
return bits
}

function bits2octets (bits, q) {
bits = bits2int(bits, q)
bits = bits.mod(q)
Expand All @@ -144,31 +142,37 @@ function bits2octets (bits, q) {
}
return out
}
module.exports.makeKey = makeKey

function makeKey (q, kv, algo) {
var t
var k
while (true) {
var t, k

do {
t = new Buffer('')

while (t.length * 8 < q.bitLength()) {
kv.v = createHmac(algo, kv.k)
.update(kv.v)
.digest()
t = Buffer.concat([t, kv.v])
}

k = bits2int(t, q)
kv.k = createHmac(algo, kv.k)
.update(kv.v)
.update(new Buffer([0]))
.digest()
kv.k = createHmac(algo, kv.k)
.update(kv.v)
.update(new Buffer([0]))
.digest()
kv.v = createHmac(algo, kv.k)
.update(kv.v)
.digest()
if (k.cmp(q) === -1) {
return k
}
}
}
function makeR (g, k, p, q) {
return g.toRed(BN.mont(p)).redPow(k).fromRed().mod(q)
.update(kv.v)
.digest()
} while (k.cmp(q) !== -1)

return k
}

// function makeR (g, k, p, q) {
// return g.toRed(BN.mont(p)).redPow(k).fromRed().mod(q)
// }

module.exports = sign
module.exports.getKey = getKey
module.exports.makeKey = makeKey
25 changes: 13 additions & 12 deletions verify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
// 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, signType) {
var pub = parseKeys(key)
Expand Down Expand Up @@ -57,16 +55,17 @@ function verify (sig, hash, key, signType) {
}
return out === 0
}

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('.'))
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, sig, pubkey)
}

function dsaVerify (sig, hash, pub) {
var p = pub.data.p
var q = pub.data.q
Expand All @@ -77,19 +76,19 @@ function dsaVerify (sig, hash, pub) {
var r = unpacked.r
checkValue(s, q)
checkValue(r, q)
var montq = BN.mont(q)
var montp = BN.mont(p)
var w = s.invm(q)
var w = s.invm(q)
var v = g.toRed(montp)
.redPow(new BN(hash).mul(w).mod(q))
.fromRed()
.mul(
y.toRed(montp)
.redPow(r.mul(w).mod(q))
.redPow(new BN(hash).mul(w).mod(q))
.fromRed()
.mul(
y.toRed(montp)
.redPow(r.mul(w).mod(q))
.fromRed()
).mod(p).mod(q)
return !v.cmp(r)
}

function checkValue (b, q) {
if (b.cmpn(0) <= 0) {
throw new Error('invalid sig')
Expand All @@ -98,3 +97,5 @@ function checkValue (b, q) {
throw new Error('invalid sig')
}
}

module.exports = verify

0 comments on commit 4f8a8e9

Please sign in to comment.