Skip to content

Commit

Permalink
Merge pull request #8 from omsmith/happy-path-fuzzing
Browse files Browse the repository at this point in the history
(test) happy-path "fuzzing" utility
  • Loading branch information
omsmith committed Nov 6, 2015
2 parents ce9153e + 3a468d4 commit 78e5590
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fuzz/happy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

var assert = require('assert'),
crypto = require('crypto');

var keygen = require('./keygen');

var conv = require('../');

var sslalgs = {
'ES256': 'RSA-SHA256',
'ES384': 'RSA-SHA384',
'ES512': 'RSA-SHA512'
};

['ES256', 'ES384', 'ES512'].forEach(function (alg) {
for (var i = 0; i < 10; ++i) {
var pem = keygen(alg);

for (var j = 0; j < 10000; ++j) {
var der = crypto
.createSign(sslalgs[alg])
.update(crypto.randomBytes((Math.random() * (256 - 1) | 0) + 1))
.sign(pem);

assert(conv.joseToDer(conv.derToJose(der, alg), alg).equals(der));
}
}
});
46 changes: 46 additions & 0 deletions fuzz/keygen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

var base64url = require('base64-url').escape,
EC = require('elliptic').ec,
jwkToPem = require('jwk-to-pem');

var curves = {
ES256: 'p256',
ES384: 'p384',
ES512: 'p521'
},
jwkCurves = {
ES256: 'P-256',
ES384: 'P-384',
ES512: 'P-521'
};

function b64(val) {
val = val.toString('hex', 2);
val = new Buffer(val, 'hex');
val = val.toString('base64');
val = base64url(val);
return val;
}

function keygen(alg) {
var curve = new EC(curves[alg]);

var keypair = curve.genKeyPair();
var priv = keypair.getPrivate();
var pub = keypair.getPublic();

var jwk = {
kty: 'EC',
crv: jwkCurves[alg],
x: b64(pub.getX()),
y: b64(pub.getY()),
d: b64(priv)
};

var pem = jwkToPem(jwk, { private: true });

return pem;
}

module.exports = keygen;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"bench": "^0.3.5",
"chai": "^3.0.0",
"coveralls": "^2.11.2",
"elliptic": "^6.0.1",
"istanbul": "^0.3.15",
"jwk-to-pem": "^1.2.2",
"mocha": "^2.2.5"
}
}

0 comments on commit 78e5590

Please sign in to comment.