Skip to content

Commit 2e33e1c

Browse files
committed
feat: add EC P-256K JWK and ES256K sign/verify support
1 parent c5c985b commit 2e33e1c

File tree

15 files changed

+62
-9
lines changed

15 files changed

+62
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Legend:
4343
| -- | -- | -- |
4444
| RSASSA-PKCS1-v1_5 || RS256, RS384, RS512 |
4545
| RSASSA-PSS || PS256, PS384, PS512 |
46-
| ECDSA || ES256, ES384, ES512 |
46+
| ECDSA || ES256, ES256K, ES384, ES512 |
4747
| HMAC with SHA-2 || HS256, HS384, HS512 |
4848

4949
| JWE Key Management Algorithms | Supported ||

lib/help/asn1/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ const RSAPublicKey = asn1.define('RSAPublicKey', require('./rsa_public_key'))
2121
types.set('RSAPublicKey', RSAPublicKey)
2222

2323
module.exports = types
24-
module.exports.BN = asn1.bignum

lib/help/ecdsa_signatures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const getParamSize = keySize => ((keySize / 8) | 0) + (keySize % 8 === 0 ? 0 : 1
1010

1111
const paramBytesForAlg = {
1212
ES256: getParamSize(256),
13+
ES256K: getParamSize(256),
1314
ES384: getParamSize(384),
1415
ES512: getParamSize(521)
1516
}

lib/help/key_utils.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@ const base64url = require('./base64url')
22
const errors = require('../errors')
33
const asn1 = require('./asn1')
44

5-
const EC_CURVES = new Set(['P-256', 'P-384', 'P-521'])
5+
const EC_CURVES = new Set([
6+
'P-256',
7+
'P-256K',
8+
'P-384',
9+
'P-521'
10+
])
611

712
const oidHexToCurve = new Map([
813
['06082a8648ce3d030107', 'P-256'],
14+
['06052b8104000a', 'P-256K'],
915
['06052b81040022', 'P-384'],
1016
['06052b81040023', 'P-521']
1117
])
1218
const EC_KEY_OID = '1.2.840.10045.2.1'.split('.')
1319
const crvToOid = new Map([
1420
['P-256', '1.2.840.10045.3.1.7'.split('.')],
21+
['P-256K', '1.3.132.0.10'.split('.')],
1522
['P-384', '1.3.132.0.34'.split('.')],
1623
['P-521', '1.3.132.0.35'.split('.')]
1724
])
1825
const crvToOidBuf = new Map([
1926
['P-256', Buffer.from('06082a8648ce3d030107', 'hex')],
27+
['P-256K', Buffer.from('06052b8104000a', 'hex')],
2028
['P-384', Buffer.from('06052b81040022', 'hex')],
2129
['P-521', Buffer.from('06052b81040023', 'hex')]
2230
])

lib/help/node_alg.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = (alg) => {
44
case 'PS256':
55
case 'HS256':
66
case 'ES256':
7+
case 'ES256K':
78
return 'sha256'
89
case 'RS384':
910
case 'PS384':

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface KeyParameters {
88
use?: use
99
kid?: string
1010
}
11-
type curve = 'P-256' | 'P-384' | 'P-521'
11+
type curve = 'P-256' | 'P-256K' | 'P-384' | 'P-521'
1212
type keyType = 'RSA' | 'EC' | 'oct'
1313
type keyOperation = 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'wrapKey' | 'unwrapKey'
1414

lib/jwa/ecdh/derive.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const crvToCurve = (crv) => {
66
switch (crv) {
77
case 'P-256':
88
return 'prime256v1'
9+
case 'P-256K':
10+
return 'secp256k1'
911
case 'P-384':
1012
return 'secp384r1'
1113
case 'P-521':

lib/jwa/ecdsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const verify = (jwaAlg, nodeAlg, { [KEYOBJECT]: keyObject }, payload, signature)
2323
}
2424

2525
module.exports = (JWA) => {
26-
['ES256', 'ES384', 'ES512'].forEach((jwaAlg) => {
26+
['ES256', 'ES384', 'ES512', 'ES256K'].forEach((jwaAlg) => {
2727
const nodeAlg = resolveNodeAlg(jwaAlg)
2828

2929
assert(!JWA.sign.has(jwaAlg), `sign alg ${jwaAlg} already registered`)

lib/jwk/key/ec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const crvToDSA = (crv) => {
1818
switch (crv) {
1919
case 'P-256':
2020
return 'ES256'
21+
case 'P-256K':
22+
return 'ES256K'
2123
case 'P-384':
2224
return 'ES384'
2325
case 'P-521':
@@ -93,12 +95,18 @@ class ECKey extends Key {
9395
}
9496

9597
static async generate (crv = 'P-256', privat = true) {
98+
if (crv === 'P-256K') {
99+
crv = 'secp256k1'
100+
}
96101
const { privateKey, publicKey } = await generateKeyPair('ec', { namedCurve: crv })
97102

98103
return privat ? privateKey : publicKey
99104
}
100105

101106
static generateSync (crv = 'P-256', privat = true) {
107+
if (crv === 'P-256K') {
108+
crv = 'secp256k1'
109+
}
102110
const { privateKey, publicKey } = generateKeyPairSync('ec', { namedCurve: crv })
103111

104112
return privat ? privateKey : publicKey

test/fixtures/P-256K.key

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgxTAmXNRL8ksBlr+F3yXD
3+
rUdRDn1gyIvY/PC2e/iUK7ehRANCAARVFouq0yOD8lFoPORt+K3vOieQ4YNnjapt
4+
nKWOGqyDdeaoE8aEQH9IScXKYVYNTRPa9F7/hx2clSCcRG6OkgLE
5+
-----END PRIVATE KEY-----

0 commit comments

Comments
 (0)