Skip to content

Commit 593b04d

Browse files
committed
feat: add support for Ed25519 and Ed448 (EdDSA)
1 parent 97243df commit 593b04d

10 files changed

+31
-0
lines changed

lib/oneShotAlgs.js

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ module.exports = function(alg, key) {
5252
digest: 'sha512',
5353
key: { key, dsaEncoding: 'ieee-p1363' },
5454
};
55+
case 'EdDSA':
56+
return {
57+
digest: undefined,
58+
key: { key },
59+
};
5560
default:
5661
throw new Error('unreachable');
5762
}

lib/validateAsymmetricKey.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { ASYMMETRIC_KEY_DETAILS_SUPPORTED, RSA_PSS_KEY_DETAILS_SUPPORTED } = requ
22

33
const allowedAlgorithmsForKeys = {
44
'ec': ['ES256', 'ES256K', 'ES384', 'ES512'],
5+
'ed25519': ['EdDSA'],
6+
'ed448': ['EdDSA'],
57
'rsa': ['RS256', 'PS256', 'RS384', 'PS384', 'RS512', 'PS512'],
68
'rsa-pss': ['PS256', 'PS384', 'PS512']
79
};

sign.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SUPPORTED_ALGS = [
1515
'PS256', 'PS384', 'PS512',
1616
'ES256', 'ES256K', 'ES384', 'ES512',
1717
'HS256', 'HS384', 'HS512',
18+
'EdDSA',
1819
'none',
1920
];
2021

test/ed25519-private.pem

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MC4CAQAwBQYDK2VwBCIEINm0OEjPHWFVPXX+RWO48diNrzeWvhxLYT0UfBHb6ZBA
3+
-----END PRIVATE KEY-----

test/ed25519-public-invalid.pem

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MCowBQYDK2VwAyEAnbt7ZRTDvGWNmgiJQ+oOodLqvFS0fl1mlRHTaetHI0Q=
3+
-----END PUBLIC KEY-----

test/ed25519-public.pem

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MCowBQYDK2VwAyEAbelG8IgnkVHYUdI5CN54QDdYkvgJkeDc7V8EVBN6zVg=
3+
-----END PUBLIC KEY-----

test/jwt.asymmetric_signing.tests.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const algorithms = {
2626
pub_key: loadKey('secp256k1-public.pem'),
2727
invalid_pub_key: loadKey('secp256k1-public-invalid.pem')
2828
},
29+
EdDSA: {
30+
priv_key: loadKey('ed25519-private.pem'),
31+
pub_key: loadKey('ed25519-public.pem'),
32+
invalid_pub_key: loadKey('ed25519-public-invalid.pem')
33+
},
2934
PS256: {
3035
pub_key: loadKey('pub.pem'),
3136
priv_key: loadKey('priv.pem'),

test/roundtrip.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ for (const [alg, opts] of [
1414
["ES256K"],
1515
["ES384"],
1616
["ES512"],
17+
["EdDSA", { crv: "Ed25519" }],
18+
["EdDSA", { crv: "Ed448" }],
1719
]) {
1820
const conditionalDescribe =
1921
parseInt(process.versions.node, 10) >= 18 ? describe : describe.skip;

test/schema.tests.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('schema', function() {
1010
var cert_secp256k1_priv = fs.readFileSync(__dirname + '/secp256k1-private.pem');
1111
var cert_secp384r1_priv = fs.readFileSync(__dirname + '/secp384r1-private.pem');
1212
var cert_secp521r1_priv = fs.readFileSync(__dirname + '/secp521r1-private.pem');
13+
var cert_ed25519_priv = fs.readFileSync(__dirname + '/ed25519-private.pem');
1314

1415
function sign(options, secretOrPrivateKey) {
1516
jwt.sign({foo: 123}, secretOrPrivateKey, options);
@@ -30,6 +31,7 @@ describe('schema', function() {
3031
sign({algorithm: 'ES256K'}, cert_secp256k1_priv);
3132
sign({algorithm: 'ES384'}, cert_secp384r1_priv);
3233
sign({algorithm: 'ES512'}, cert_secp521r1_priv);
34+
sign({algorithm: 'EdDSA'}, cert_ed25519_priv);
3335
sign({algorithm: 'HS256'}, 'superSecret');
3436
sign({algorithm: 'HS384'}, 'superSecret');
3537
sign({algorithm: 'HS512'}, 'superSecret');

verify.js

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const EC_KEY_ALGS = ['ES256', 'ES256K', 'ES384', 'ES512'];
1111
const RSA_KEY_ALGS = ['RS256', 'RS384', 'RS512', 'PS256', 'PS384', 'PS512'];
1212
const PUB_KEY_ALGS = [].concat(RSA_KEY_ALGS, EC_KEY_ALGS);
1313
const HS_ALGS = ['HS256', 'HS384', 'HS512'];
14+
const EdDSA_ALGS = ['EdDSA'];
1415

1516
function processPayload(header, payload, signature, options, done) {
1617
const clockTimestamp = options.clockTimestamp || Math.floor(Date.now() / 1000);
@@ -222,6 +223,10 @@ module.exports = function(jwtString, secretOrPublicKey, options, callback) {
222223
case 'ec':
223224
options.algorithms = EC_KEY_ALGS
224225
break;
226+
case 'ed25519':
227+
case 'ed448':
228+
options.algorithms = EdDSA_ALGS;
229+
break;
225230
default:
226231
options.algorithms = PUB_KEY_ALGS;
227232
}

0 commit comments

Comments
 (0)