Skip to content

Commit 97243df

Browse files
committed
feat: add support for curve secp256k1 (ES256K)
1 parent 4195fc3 commit 97243df

10 files changed

+30
-3
lines changed

lib/oneShotAlgs.js

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module.exports = function(alg, key) {
3737
digest: 'sha256',
3838
key: { key, dsaEncoding: 'ieee-p1363' },
3939
};
40+
case 'ES256K':
41+
return {
42+
digest: 'sha256',
43+
key: { key, dsaEncoding: 'ieee-p1363' },
44+
};
4045
case 'ES384':
4146
return {
4247
digest: 'sha384',

lib/validateAsymmetricKey.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const { ASYMMETRIC_KEY_DETAILS_SUPPORTED, RSA_PSS_KEY_DETAILS_SUPPORTED } = require('./flags');
22

33
const allowedAlgorithmsForKeys = {
4-
'ec': ['ES256', 'ES384', 'ES512'],
4+
'ec': ['ES256', 'ES256K', 'ES384', 'ES512'],
55
'rsa': ['RS256', 'PS256', 'RS384', 'PS384', 'RS512', 'PS512'],
66
'rsa-pss': ['PS256', 'PS384', 'PS512']
77
};
88

99
const allowedCurves = {
1010
ES256: 'prime256v1',
11+
ES256K: 'secp256k1',
1112
ES384: 'secp384r1',
1213
ES512: 'secp521r1',
1314
};

sign.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const encodeBase64url = require('./lib/base64url');
1313
const SUPPORTED_ALGS = [
1414
'RS256', 'RS384', 'RS512',
1515
'PS256', 'PS384', 'PS512',
16-
'ES256', 'ES384', 'ES512',
16+
'ES256', 'ES256K', 'ES384', 'ES512',
1717
'HS256', 'HS384', 'HS512',
1818
'none',
1919
];

test/jwt.asymmetric_signing.tests.js

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const algorithms = {
2121
pub_key: loadKey('ecdsa-public.pem'),
2222
invalid_pub_key: loadKey('ecdsa-public-invalid.pem')
2323
},
24+
ES256K: {
25+
priv_key: loadKey('secp256k1-private.pem'),
26+
pub_key: loadKey('secp256k1-public.pem'),
27+
invalid_pub_key: loadKey('secp256k1-public-invalid.pem')
28+
},
2429
PS256: {
2530
pub_key: loadKey('pub.pem'),
2631
priv_key: loadKey('priv.pem'),

test/roundtrip.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ for (const [alg, opts] of [
1111
["RS256"],
1212
["PS256"],
1313
["ES256"],
14+
["ES256K"],
1415
["ES384"],
1516
["ES512"],
1617
]) {

test/schema.tests.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('schema', function() {
77
describe('sign options', function() {
88
var cert_rsa_priv = fs.readFileSync(__dirname + '/rsa-private.pem');
99
var cert_ecdsa_priv = fs.readFileSync(__dirname + '/ecdsa-private.pem');
10+
var cert_secp256k1_priv = fs.readFileSync(__dirname + '/secp256k1-private.pem');
1011
var cert_secp384r1_priv = fs.readFileSync(__dirname + '/secp384r1-private.pem');
1112
var cert_secp521r1_priv = fs.readFileSync(__dirname + '/secp521r1-private.pem');
1213

@@ -26,6 +27,7 @@ describe('schema', function() {
2627
sign({algorithm: 'PS384'}, cert_rsa_priv);
2728
sign({algorithm: 'PS512'}, cert_rsa_priv);
2829
sign({algorithm: 'ES256'}, cert_ecdsa_priv);
30+
sign({algorithm: 'ES256K'}, cert_secp256k1_priv);
2931
sign({algorithm: 'ES384'}, cert_secp384r1_priv);
3032
sign({algorithm: 'ES512'}, cert_secp521r1_priv);
3133
sign({algorithm: 'HS256'}, 'superSecret');

test/secp256k1-private.pem

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHQCAQEEIFg3x9PMwysC/B5iW1zUFqDUfNbgP77i71jEPhoce0OkoAcGBSuBBAAK
3+
oUQDQgAEUdPp6J0l51augh0A0sB14n2j69er1ZTkhfv+XY3CIU/SFK/BmIt0KfAX
4+
VF2KGowflLSKkySNnfR93uwnf7y1MQ==
5+
-----END EC PRIVATE KEY-----

test/secp256k1-public-invalid.pem

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE7cjAbx1KnvP+g5lJQba/42ga/NL5rkIC
3+
rmuRulSLZ+X6oRvnxfhgDkQgkoJkNaqXR6vYE42kfbz5BOfIcNfkig==
4+
-----END PUBLIC KEY-----

test/secp256k1-public.pem

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEUdPp6J0l51augh0A0sB14n2j69er1ZTk
3+
hfv+XY3CIU/SFK/BmIt0KfAXVF2KGowflLSKkySNnfR93uwnf7y1MQ==
4+
-----END PUBLIC KEY-----

verify.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const validateAsymmetricKey = require('./lib/validateAsymmetricKey');
77
const crypto = require("crypto");
88
const oneShotAlgs = require('./lib/oneShotAlgs');
99

10-
const EC_KEY_ALGS = ['ES256', 'ES384', 'ES512'];
10+
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'];

0 commit comments

Comments
 (0)