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

Adding new function that converts a ed25519 key to JWK #161

Merged
merged 3 commits into from
Aug 6, 2022
Merged
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
48 changes: 48 additions & 0 deletions cryptosuite/jsonwebkey2020.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package cryptosuite

import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"

"encoding/base64"
"fmt"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/goccy/go-json"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jws"
"github.com/lestrrat-go/jwx/x25519"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -145,6 +152,13 @@ func GenerateRSAJSONWebKey2020() (*JSONWebKey2020, error) {
if err != nil {
return nil, err
}

return JSONWebKey2020FromRSA(privKey)
}

// JSONWebKey2020FromRSA returns a JsonWebKey2020 value, containing both public and private keys
// for an RSA-2048 key. This function coverts a rsa.PrivateKey to a JsonWebKey2020
func JSONWebKey2020FromRSA(privKey rsa.PrivateKey) (*JSONWebKey2020, error) {
rsaJWK := jwk.NewRSAPrivateKey()
if err := rsaJWK.FromRaw(&privKey); err != nil {
return nil, errors.Wrap(err, "failed to generate rsa jwk")
Expand Down Expand Up @@ -180,6 +194,12 @@ func GenerateEd25519JSONWebKey2020() (*JSONWebKey2020, error) {
if err != nil {
return nil, err
}
return JSONWebKey2020FromEd25519(privKey)
}

// JSONWebKey2020FromEd25519 returns a JsonWebKey2020 value, containing both public and
// private keys for an Ed25519 key. This function coverts a ed25519.PrivateKey to a JsonWebKey2020
func JSONWebKey2020FromEd25519(privKey ed25519.PrivateKey) (*JSONWebKey2020, error) {
ed25519JWK := jwk.NewOKPPrivateKey()
if err := ed25519JWK.FromRaw(privKey); err != nil {
return nil, errors.Wrap(err, "failed to generate ed25519 jwk")
Expand Down Expand Up @@ -211,6 +231,14 @@ func GenerateX25519JSONWebKey2020() (*JSONWebKey2020, error) {
if err != nil {
return nil, err
}

return JSONWebKey2020FromX25519(privKey)

}

// JSONWebKey2020FromX25519 returns a JsonWebKey2020 value, containing both public and
// private keys for an x25519 key. This function coverts a x25519.PrivateKey to a JsonWebKey2020
func JSONWebKey2020FromX25519(privKey x25519.PrivateKey) (*JSONWebKey2020, error) {
x25519JWK := jwk.NewOKPPrivateKey()
if err := x25519JWK.FromRaw(privKey); err != nil {
return nil, errors.Wrap(err, "failed to generate x25519 jwk")
Expand Down Expand Up @@ -246,6 +274,12 @@ func GenerateSECP256k1JSONWebKey2020() (*JSONWebKey2020, error) {
logrus.WithError(err).Error("could not generate secp256k1 key")
return nil, err
}
return JSONWebKey2020FromSECP256k1(privKey)
}

// JSONWebKey2020FromSECP256k1 returns a JsonWebKey2020 value, containing both public and
// private keys for an secp256k1 key. This function coverts a secp256k1.PrivateKey to a JsonWebKey2020
func JSONWebKey2020FromSECP256k1(privKey secp256k1.PrivateKey) (*JSONWebKey2020, error) {
ecdsaPrivKey := privKey.ToECDSA()
secp256k1JWK := jwk.NewECDSAPrivateKey()
if err := secp256k1JWK.FromRaw(ecdsaPrivKey); err != nil {
Expand Down Expand Up @@ -283,6 +317,12 @@ func GenerateP256JSONWebKey2020() (*JSONWebKey2020, error) {
logrus.WithError(err).Error("could not generate p-256 key")
return nil, err
}
return JSONWebKey2020FromP256(privKey)
}

// JSONWebKey2020FromP256 returns a JsonWebKey2020 value, containing both public and
// private keys for an P-256 ECDSA key. This function coverts a P-256 ecdsa.PrivateKey to a JsonWebKey2020
func JSONWebKey2020FromP256(privKey ecdsa.PrivateKey) (*JSONWebKey2020, error) {
p256JWK := jwk.NewECDSAPrivateKey()
if err := p256JWK.FromRaw(&privKey); err != nil {
err := errors.Wrap(err, "failed to generate p-256 jwk")
Expand Down Expand Up @@ -319,6 +359,14 @@ func GenerateP384JSONWebKey2020() (*JSONWebKey2020, error) {
logrus.WithError(err).Error("could not generate p-384 key")
return nil, err
}

return JSONWebKey2020FromP384(privKey)

}

// JSONWebKey2020FromP384 returns a JsonWebKey2020 value, containing both public and
// private keys for an P-384 ECDSA key. This function coverts a P-384 ecdsa.PrivateKey to a JsonWebKey2020
func JSONWebKey2020FromP384(privKey ecdsa.PrivateKey) (*JSONWebKey2020, error) {
p384JWK := jwk.NewECDSAPrivateKey()
if err := p384JWK.FromRaw(&privKey); err != nil {
err := errors.Wrap(err, "failed to generate p-384 jwk")
Expand Down