Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge branch 'maraino-v2' into v2
Browse files Browse the repository at this point in the history
* maraino-v2:
  Add additional test for ED25519 serialization
  Fix whitespaces.
  Reverse bytes in Ed25519 keys.
  • Loading branch information
csstaub committed Apr 10, 2019
2 parents 628223f + e8202b5 commit 730df5f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 5 additions & 5 deletions jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (k *JSONWebKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
case *rsa.PrivateKey:
input, err = rsaThumbprintInput(key.N, key.E)
case ed25519.PrivateKey:
input, err = edThumbprintInput(ed25519.PublicKey(key[0:32]))
input, err = edThumbprintInput(ed25519.PublicKey(key[32:]))
default:
return nil, fmt.Errorf("square/go-jose: unknown key type '%s'", reflect.TypeOf(key))
}
Expand Down Expand Up @@ -421,8 +421,8 @@ func (key rawJSONWebKey) edPrivateKey() (ed25519.PrivateKey, error) {
}

privateKey := make([]byte, ed25519.PrivateKeySize)
copy(privateKey[0:32], key.X.bytes())
copy(privateKey[32:], key.D.bytes())
copy(privateKey[0:32], key.D.bytes())
copy(privateKey[32:], key.X.bytes())
rv := ed25519.PrivateKey(privateKey)
return rv, nil
}
Expand Down Expand Up @@ -483,9 +483,9 @@ func (key rawJSONWebKey) rsaPrivateKey() (*rsa.PrivateKey, error) {
}

func fromEdPrivateKey(ed ed25519.PrivateKey) (*rawJSONWebKey, error) {
raw := fromEdPublicKey(ed25519.PublicKey(ed[0:32]))
raw := fromEdPublicKey(ed25519.PublicKey(ed[32:]))

raw.D = newBuffer(ed[32:])
raw.D = newBuffer(ed[0:32])
return raw, nil
}

Expand Down
19 changes: 17 additions & 2 deletions jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ed25519"

"gopkg.in/square/go-jose.v2/json"
Expand Down Expand Up @@ -441,8 +442,8 @@ var cookbookJWKs = []string{
stripWhitespace(`{
"kty": "OKP",
"crv": "Ed25519",
"d": "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A",
"x": "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"
"x": "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
"d": "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A"
}`),

// EC Private
Expand Down Expand Up @@ -560,6 +561,20 @@ func TestWebKeyVectorsValid(t *testing.T) {
}
}

func TestEd25519Serialization(t *testing.T) {
jwk := JSONWebKey{
Key: ed25519PrivateKey,
}
serialized, _ := json.Marshal(jwk)

var jwk2 JSONWebKey
json.Unmarshal(serialized, &jwk2)

assert.True(t, bytes.Equal(
[]byte(jwk.Key.(ed25519.PrivateKey).Public().(ed25519.PublicKey)),
[]byte(jwk2.Key.(ed25519.PrivateKey).Public().(ed25519.PublicKey))))
}

func TestThumbprint(t *testing.T) {
for i, key := range cookbookJWKs {
var jwk2 JSONWebKey
Expand Down

0 comments on commit 730df5f

Please sign in to comment.