-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from omsmith/happy-path-fuzzing
(test) happy-path "fuzzing" utility
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters