key-encoder-js icon indicating copy to clipboard operation
key-encoder-js copied to clipboard

separate out curves

Open mvayngrib opened this issue 8 years ago • 7 comments

mvayngrib avatar Sep 12 '16 15:09 mvayngrib

Thanks for the PR @mvayngrib! Love how you added a bunch of new curves.

I can merge this in soon but it'd be great if you could add some tests in with at least one of these curves before I do. Would you be up for that?

shea256 avatar Sep 12 '16 20:09 shea256

@shea256 do you remember where you got the OpenSSL reference values?

mvayngrib avatar Sep 12 '16 21:09 mvayngrib

I don't remember.

Here's one list thought: https://github.com/warner/python-ecdsa/blob/master/ecdsa/curves.py#L43

shea256 avatar Sep 12 '16 21:09 shea256

I think I got it from an RFC but I'm not sure which one at the moment unfortunately.

shea256 avatar Sep 12 '16 21:09 shea256

@mvayngrib Did you see the other curves in there?

shea256 avatar Sep 13 '16 19:09 shea256

@shea256 no, sadly i didn't. For now i'm just testing it by cross sign-verify native<=>elliptic, which passes (the code is non-deterministic, but since we're planning to use openssl references anyway, who cares).

also: i also added lazy-eval for creating instances of elliptic curves. They're very expensive to create so it's better not to pre-create them.

test('elliptic sign => native verify compat', function (t) {
    var data = 'some data'
    var algorithm = 'sha256'
    var hash = crypto.createHash(algorithm).update(data).digest()
    for (var name in aliases) {
        var curve = curves[name].curve
        var encoder = new KeyEncoder(name)
        var key = curve.genKeyPair()
        var sig = key.sign(hash).toDER('hex')
        var pubHex = key.getPublic('hex')
        var pub = encoder.encodePublic(pubHex, 'raw', 'pem')
        var verified = crypto.createVerify(algorithm).update(data).verify(pub, sig, 'hex')
        t.ok(verified)
    }

    t.end()
})

test('native sign => elliptic verify compat', function (t) {
    var data = 'some data'
    var algorithm = 'sha256'
    var hash = crypto.createHash(algorithm).update(data).digest()
    for (var name in aliases) {
        var curve = curves[name].curve
        var encoder = new KeyEncoder(name)
        var ecdh = crypto.createECDH(aliases[name])
        ecdh.generateKeys()
        var priv = ecdh.getPrivateKey()
        var pem = encoder.encodePrivate(priv, 'raw', 'pem')
        var sig = crypto.createSign(algorithm).update(data).sign(pem, 'hex')
        var verified = curve.keyFromPrivate(priv).verify(hash, sig)
        t.ok(verified)
    }

    t.end()
})

mvayngrib avatar Sep 15 '16 19:09 mvayngrib

@mvayngrib Thanks, great work!

I just incorporated your 2 commits and your tests in this latest PR: https://github.com/blockstack/key-encoder-js/pull/3

Please let me know what you think.

shea256 avatar Sep 16 '16 14:09 shea256

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Mar 08 '20 06:03 CLAassistant