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

Commit 730df5f

Browse files
committed
Merge branch 'maraino-v2' into v2
* maraino-v2: Add additional test for ED25519 serialization Fix whitespaces. Reverse bytes in Ed25519 keys.
2 parents 628223f + e8202b5 commit 730df5f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

jwk.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (k *JSONWebKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
230230
case *rsa.PrivateKey:
231231
input, err = rsaThumbprintInput(key.N, key.E)
232232
case ed25519.PrivateKey:
233-
input, err = edThumbprintInput(ed25519.PublicKey(key[0:32]))
233+
input, err = edThumbprintInput(ed25519.PublicKey(key[32:]))
234234
default:
235235
return nil, fmt.Errorf("square/go-jose: unknown key type '%s'", reflect.TypeOf(key))
236236
}
@@ -421,8 +421,8 @@ func (key rawJSONWebKey) edPrivateKey() (ed25519.PrivateKey, error) {
421421
}
422422

423423
privateKey := make([]byte, ed25519.PrivateKeySize)
424-
copy(privateKey[0:32], key.X.bytes())
425-
copy(privateKey[32:], key.D.bytes())
424+
copy(privateKey[0:32], key.D.bytes())
425+
copy(privateKey[32:], key.X.bytes())
426426
rv := ed25519.PrivateKey(privateKey)
427427
return rv, nil
428428
}
@@ -483,9 +483,9 @@ func (key rawJSONWebKey) rsaPrivateKey() (*rsa.PrivateKey, error) {
483483
}
484484

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

488-
raw.D = newBuffer(ed[32:])
488+
raw.D = newBuffer(ed[0:32])
489489
return raw, nil
490490
}
491491

jwk_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"strings"
3030
"testing"
3131

32+
"github.com/stretchr/testify/assert"
3233
"golang.org/x/crypto/ed25519"
3334

3435
"gopkg.in/square/go-jose.v2/json"
@@ -441,8 +442,8 @@ var cookbookJWKs = []string{
441442
stripWhitespace(`{
442443
"kty": "OKP",
443444
"crv": "Ed25519",
444-
"d": "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A",
445-
"x": "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"
445+
"x": "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
446+
"d": "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A"
446447
}`),
447448

448449
// EC Private
@@ -560,6 +561,20 @@ func TestWebKeyVectorsValid(t *testing.T) {
560561
}
561562
}
562563

564+
func TestEd25519Serialization(t *testing.T) {
565+
jwk := JSONWebKey{
566+
Key: ed25519PrivateKey,
567+
}
568+
serialized, _ := json.Marshal(jwk)
569+
570+
var jwk2 JSONWebKey
571+
json.Unmarshal(serialized, &jwk2)
572+
573+
assert.True(t, bytes.Equal(
574+
[]byte(jwk.Key.(ed25519.PrivateKey).Public().(ed25519.PublicKey)),
575+
[]byte(jwk2.Key.(ed25519.PrivateKey).Public().(ed25519.PublicKey))))
576+
}
577+
563578
func TestThumbprint(t *testing.T) {
564579
for i, key := range cookbookJWKs {
565580
var jwk2 JSONWebKey

0 commit comments

Comments
 (0)