|
@@ -2,7 +2,7 @@
|
|
|
// Use of this source code is governed by a BSD-style
|
|
// Use of this source code is governed by a BSD-style
|
|
|
// license that can be found in the LICENSE file.
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
-package ed25519
|
|
|
|
|
|
|
+package ed25519_test
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"bufio"
|
|
"bufio"
|
|
@@ -15,6 +15,7 @@ import (
|
|
|
"strings"
|
|
"strings"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
+ "golang.org/x/crypto/ed25519"
|
|
|
"golang.org/x/crypto/ed25519/internal/edwards25519"
|
|
"golang.org/x/crypto/ed25519/internal/edwards25519"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -28,7 +29,7 @@ func (zeroReader) Read(buf []byte) (int, error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalMarshal(t *testing.T) {
|
|
func TestUnmarshalMarshal(t *testing.T) {
|
|
|
- pub, _, _ := GenerateKey(rand.Reader)
|
|
|
|
|
|
|
+ pub, _, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
|
var A edwards25519.ExtendedGroupElement
|
|
var A edwards25519.ExtendedGroupElement
|
|
|
var pubBytes [32]byte
|
|
var pubBytes [32]byte
|
|
@@ -47,28 +48,28 @@ func TestUnmarshalMarshal(t *testing.T) {
|
|
|
|
|
|
|
|
func TestSignVerify(t *testing.T) {
|
|
func TestSignVerify(t *testing.T) {
|
|
|
var zero zeroReader
|
|
var zero zeroReader
|
|
|
- public, private, _ := GenerateKey(zero)
|
|
|
|
|
|
|
+ public, private, _ := ed25519.GenerateKey(zero)
|
|
|
|
|
|
|
|
message := []byte("test message")
|
|
message := []byte("test message")
|
|
|
- sig := Sign(private, message)
|
|
|
|
|
- if !Verify(public, message, sig) {
|
|
|
|
|
|
|
+ sig := ed25519.Sign(private, message)
|
|
|
|
|
+ if !ed25519.Verify(public, message, sig) {
|
|
|
t.Errorf("valid signature rejected")
|
|
t.Errorf("valid signature rejected")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
wrongMessage := []byte("wrong message")
|
|
wrongMessage := []byte("wrong message")
|
|
|
- if Verify(public, wrongMessage, sig) {
|
|
|
|
|
|
|
+ if ed25519.Verify(public, wrongMessage, sig) {
|
|
|
t.Errorf("signature of different message accepted")
|
|
t.Errorf("signature of different message accepted")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestCryptoSigner(t *testing.T) {
|
|
func TestCryptoSigner(t *testing.T) {
|
|
|
var zero zeroReader
|
|
var zero zeroReader
|
|
|
- public, private, _ := GenerateKey(zero)
|
|
|
|
|
|
|
+ public, private, _ := ed25519.GenerateKey(zero)
|
|
|
|
|
|
|
|
signer := crypto.Signer(private)
|
|
signer := crypto.Signer(private)
|
|
|
|
|
|
|
|
publicInterface := signer.Public()
|
|
publicInterface := signer.Public()
|
|
|
- public2, ok := publicInterface.(PublicKey)
|
|
|
|
|
|
|
+ public2, ok := publicInterface.(ed25519.PublicKey)
|
|
|
if !ok {
|
|
if !ok {
|
|
|
t.Fatalf("expected PublicKey from Public() but got %T", publicInterface)
|
|
t.Fatalf("expected PublicKey from Public() but got %T", publicInterface)
|
|
|
}
|
|
}
|
|
@@ -84,7 +85,7 @@ func TestCryptoSigner(t *testing.T) {
|
|
|
t.Fatalf("error from Sign(): %s", err)
|
|
t.Fatalf("error from Sign(): %s", err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if !Verify(public, message, signature) {
|
|
|
|
|
|
|
+ if !ed25519.Verify(public, message, signature) {
|
|
|
t.Errorf("Verify failed on signature from Sign()")
|
|
t.Errorf("Verify failed on signature from Sign()")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -121,31 +122,31 @@ func TestGolden(t *testing.T) {
|
|
|
sig, _ := hex.DecodeString(parts[3])
|
|
sig, _ := hex.DecodeString(parts[3])
|
|
|
// The signatures in the test vectors also include the message
|
|
// The signatures in the test vectors also include the message
|
|
|
// at the end, but we just want R and S.
|
|
// at the end, but we just want R and S.
|
|
|
- sig = sig[:SignatureSize]
|
|
|
|
|
|
|
+ sig = sig[:ed25519.SignatureSize]
|
|
|
|
|
|
|
|
- if l := len(pubKey); l != PublicKeySize {
|
|
|
|
|
|
|
+ if l := len(pubKey); l != ed25519.PublicKeySize {
|
|
|
t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
|
|
t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var priv [PrivateKeySize]byte
|
|
|
|
|
|
|
+ var priv [ed25519.PrivateKeySize]byte
|
|
|
copy(priv[:], privBytes)
|
|
copy(priv[:], privBytes)
|
|
|
copy(priv[32:], pubKey)
|
|
copy(priv[32:], pubKey)
|
|
|
|
|
|
|
|
- sig2 := Sign(priv[:], msg)
|
|
|
|
|
|
|
+ sig2 := ed25519.Sign(priv[:], msg)
|
|
|
if !bytes.Equal(sig, sig2[:]) {
|
|
if !bytes.Equal(sig, sig2[:]) {
|
|
|
t.Errorf("different signature result on line %d: %x vs %x", lineNo, sig, sig2)
|
|
t.Errorf("different signature result on line %d: %x vs %x", lineNo, sig, sig2)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if !Verify(pubKey, msg, sig2) {
|
|
|
|
|
|
|
+ if !ed25519.Verify(pubKey, msg, sig2) {
|
|
|
t.Errorf("signature failed to verify on line %d", lineNo)
|
|
t.Errorf("signature failed to verify on line %d", lineNo)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- priv2 := NewKeyFromSeed(priv[:32])
|
|
|
|
|
|
|
+ priv2 := ed25519.NewKeyFromSeed(priv[:32])
|
|
|
if !bytes.Equal(priv[:], priv2) {
|
|
if !bytes.Equal(priv[:], priv2) {
|
|
|
t.Errorf("recreating key pair gave different private key on line %d: %x vs %x", lineNo, priv[:], priv2)
|
|
t.Errorf("recreating key pair gave different private key on line %d: %x vs %x", lineNo, priv[:], priv2)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if pubKey2 := priv2.Public().(PublicKey); !bytes.Equal(pubKey, pubKey2) {
|
|
|
|
|
|
|
+ if pubKey2 := priv2.Public().(ed25519.PublicKey); !bytes.Equal(pubKey, pubKey2) {
|
|
|
t.Errorf("recreating key pair gave different public key on line %d: %x vs %x", lineNo, pubKey, pubKey2)
|
|
t.Errorf("recreating key pair gave different public key on line %d: %x vs %x", lineNo, pubKey, pubKey2)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -178,7 +179,7 @@ func TestMalleability(t *testing.T) {
|
|
|
0xb1, 0x08, 0xc3, 0xbd, 0xae, 0x36, 0x9e, 0xf5, 0x49, 0xfa,
|
|
0xb1, 0x08, 0xc3, 0xbd, 0xae, 0x36, 0x9e, 0xf5, 0x49, 0xfa,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if Verify(publicKey, msg, sig) {
|
|
|
|
|
|
|
+ if ed25519.Verify(publicKey, msg, sig) {
|
|
|
t.Fatal("non-canonical signature accepted")
|
|
t.Fatal("non-canonical signature accepted")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -186,7 +187,7 @@ func TestMalleability(t *testing.T) {
|
|
|
func BenchmarkKeyGeneration(b *testing.B) {
|
|
func BenchmarkKeyGeneration(b *testing.B) {
|
|
|
var zero zeroReader
|
|
var zero zeroReader
|
|
|
for i := 0; i < b.N; i++ {
|
|
for i := 0; i < b.N; i++ {
|
|
|
- if _, _, err := GenerateKey(zero); err != nil {
|
|
|
|
|
|
|
+ if _, _, err := ed25519.GenerateKey(zero); err != nil {
|
|
|
b.Fatal(err)
|
|
b.Fatal(err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -194,27 +195,27 @@ func BenchmarkKeyGeneration(b *testing.B) {
|
|
|
|
|
|
|
|
func BenchmarkSigning(b *testing.B) {
|
|
func BenchmarkSigning(b *testing.B) {
|
|
|
var zero zeroReader
|
|
var zero zeroReader
|
|
|
- _, priv, err := GenerateKey(zero)
|
|
|
|
|
|
|
+ _, priv, err := ed25519.GenerateKey(zero)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
b.Fatal(err)
|
|
b.Fatal(err)
|
|
|
}
|
|
}
|
|
|
message := []byte("Hello, world!")
|
|
message := []byte("Hello, world!")
|
|
|
b.ResetTimer()
|
|
b.ResetTimer()
|
|
|
for i := 0; i < b.N; i++ {
|
|
for i := 0; i < b.N; i++ {
|
|
|
- Sign(priv, message)
|
|
|
|
|
|
|
+ ed25519.Sign(priv, message)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkVerification(b *testing.B) {
|
|
func BenchmarkVerification(b *testing.B) {
|
|
|
var zero zeroReader
|
|
var zero zeroReader
|
|
|
- pub, priv, err := GenerateKey(zero)
|
|
|
|
|
|
|
+ pub, priv, err := ed25519.GenerateKey(zero)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
b.Fatal(err)
|
|
b.Fatal(err)
|
|
|
}
|
|
}
|
|
|
message := []byte("Hello, world!")
|
|
message := []byte("Hello, world!")
|
|
|
- signature := Sign(priv, message)
|
|
|
|
|
|
|
+ signature := ed25519.Sign(priv, message)
|
|
|
b.ResetTimer()
|
|
b.ResetTimer()
|
|
|
for i := 0; i < b.N; i++ {
|
|
for i := 0; i < b.N; i++ {
|
|
|
- Verify(pub, message, signature)
|
|
|
|
|
|
|
+ ed25519.Verify(pub, message, signature)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|