Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for secp256k1 #562

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ecdsa-modified-1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ KJUR.crypto.ECDSA = function(params) {
return "P-384";
if (s === "secp521r1" || s === "NIST P-521" || s === "P-521")
return "P-521";
if (s === "secp256k1")
return "secp256k1";
return null;
};

Expand Down
4 changes: 2 additions & 2 deletions src/keyutil-1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,7 @@ KEYUTIL.getJWK = function(keyinfo, nokid, nox5c, nox5t, nox5t2) {
jwk.e = hextob64u(keyObj.e.toString(16));
} else if (keyObj instanceof KJUR.crypto.ECDSA && keyObj.isPrivate) {
var name = keyObj.getShortNISTPCurveName();
if (name !== "P-256" && name !== "P-384" && name !== "P-521")
if (name !== "P-256" && name !== "P-384" && name !== "P-521" && name !== "secp256k1")
throw new Error("unsupported curve name for JWT: " + name);
var xy = keyObj.getPublicKeyXYHex();
jwk.kty = "EC";
Expand All @@ -1872,7 +1872,7 @@ KEYUTIL.getJWK = function(keyinfo, nokid, nox5c, nox5t, nox5t2) {
jwk.d = hextob64u(keyObj.prvKeyHex);
} else if (keyObj instanceof KJUR.crypto.ECDSA && keyObj.isPublic) {
var name = keyObj.getShortNISTPCurveName();
if (name !== "P-256" && name !== "P-384" && name !== "P-521")
if (name !== "P-256" && name !== "P-384" && name !== "P-521" && name !== "secp256k1")
throw new Error("unsupported curve name for JWT: " + name);
var xy = keyObj.getPublicKeyXYHex();
jwk.kty = "EC";
Expand Down
2 changes: 1 addition & 1 deletion test/qunit-do-ecdsamod.html
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
ec1 = new KJUR.crypto.ECDSA({'curve': 'secp521r1', 'pub': ECK1PUBHEX});
equal(ec1.getShortNISTPCurveName(), "P-521", "secp521r1 - P-521");
ec1 = new KJUR.crypto.ECDSA({'curve': 'secp256k1', 'pub': ECK1PUBHEX});
equal(ec1.getShortNISTPCurveName(), null, "secp256k1 - null");
equal(ec1.getShortNISTPCurveName(), "secp256k1", "secp256k1 - null");
});

test("readPKCS5PrvKeyHex k1", function() {
Expand Down
46 changes: 46 additions & 0 deletions test/qunit-do-keyutil-jwk.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@
"use" : "enc",
"kid" : "1"
};
// Arbitrary test data for secp256k1
var pubsecp256k1 =
{
"kty" : "EC",
"crv" : "secp256k1",
"x" : "ETkUVhg6-xtkT_JL6KAFjw3ZB26ZtVQhpAE871EpVm4",
"y" : "kTKkieii1A4UCAmZHT1us1Eq_mLX1FUonGRLCpfo-YQ",
"use" : "enc",
"kid" : "1"
}
var prvsecp256k1 =
{
"kty" : "EC",
"crv" : "secp256k1",
"x" : "ETkUVhg6-xtkT_JL6KAFjw3ZB26ZtVQhpAE871EpVm4",
"y" : "kTKkieii1A4UCAmZHT1us1Eq_mLX1FUonGRLCpfo-YQ",
"d" : "cdsLOXwL9pUNlwVbigl2UYBwHOtRVCn9N0AmHHr-IA0",
"use" : "enc",
"kid" : "1"
};
// RFC7517 A.2.2 RSA PRV
var prvRSA1 =
{ "kty" : "RSA",
Expand Down Expand Up @@ -334,6 +354,32 @@
deepEqual(jwk, expected, "EC P-256");
});

test("getJWK() - public EC secp256k1", function() {
var ec = KEYUTIL.getKey(pubsecp256k1);
var jwk = KEYUTIL.getJWK(ec);
var expected = {
kty: "EC",
crv: "secp256k1",
x : "ETkUVhg6-xtkT_JL6KAFjw3ZB26ZtVQhpAE871EpVm4",
y : "kTKkieii1A4UCAmZHT1us1Eq_mLX1FUonGRLCpfo-YQ",
kid: "y_68HYcuzVEv4WsG5AElS1p4JUOp7jZDgrlrZXjmEWM"
};
deepEqual(jwk, expected, "EC secp256k1");
});

test("getJWK() - private EC secp256k1", function() {
var ec = KEYUTIL.getKey(prvsecp256k1);
var jwk = KEYUTIL.getJWK(ec);
var expected = {
kty: "EC",
crv: "secp256k1",
x : "ETkUVhg6-xtkT_JL6KAFjw3ZB26ZtVQhpAE871EpVm4",
y : "kTKkieii1A4UCAmZHT1us1Eq_mLX1FUonGRLCpfo-YQ",
d : "cdsLOXwL9pUNlwVbigl2UYBwHOtRVCn9N0AmHHr-IA0"
};
deepEqual(jwk, expected, "EC secp256k1");
});

test("getJWK() - public EC P-521", function() {
var pubP521jwk = Object.assign({}, prvP521jwk);
delete pubP521jwk.d;
Expand Down