ed25519_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ed25519_test
  5. import (
  6. "bufio"
  7. "bytes"
  8. "compress/gzip"
  9. "crypto"
  10. "crypto/rand"
  11. "encoding/hex"
  12. "os"
  13. "strings"
  14. "testing"
  15. "golang.org/x/crypto/ed25519"
  16. "golang.org/x/crypto/ed25519/internal/edwards25519"
  17. )
  18. type zeroReader struct{}
  19. func (zeroReader) Read(buf []byte) (int, error) {
  20. for i := range buf {
  21. buf[i] = 0
  22. }
  23. return len(buf), nil
  24. }
  25. func TestUnmarshalMarshal(t *testing.T) {
  26. pub, _, _ := ed25519.GenerateKey(rand.Reader)
  27. var A edwards25519.ExtendedGroupElement
  28. var pubBytes [32]byte
  29. copy(pubBytes[:], pub)
  30. if !A.FromBytes(&pubBytes) {
  31. t.Fatalf("ExtendedGroupElement.FromBytes failed")
  32. }
  33. var pub2 [32]byte
  34. A.ToBytes(&pub2)
  35. if pubBytes != pub2 {
  36. t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", pubBytes, pub2)
  37. }
  38. }
  39. func TestSignVerify(t *testing.T) {
  40. var zero zeroReader
  41. public, private, _ := ed25519.GenerateKey(zero)
  42. message := []byte("test message")
  43. sig := ed25519.Sign(private, message)
  44. if !ed25519.Verify(public, message, sig) {
  45. t.Errorf("valid signature rejected")
  46. }
  47. wrongMessage := []byte("wrong message")
  48. if ed25519.Verify(public, wrongMessage, sig) {
  49. t.Errorf("signature of different message accepted")
  50. }
  51. }
  52. func TestCryptoSigner(t *testing.T) {
  53. var zero zeroReader
  54. public, private, _ := ed25519.GenerateKey(zero)
  55. signer := crypto.Signer(private)
  56. publicInterface := signer.Public()
  57. public2, ok := publicInterface.(ed25519.PublicKey)
  58. if !ok {
  59. t.Fatalf("expected PublicKey from Public() but got %T", publicInterface)
  60. }
  61. if !bytes.Equal(public, public2) {
  62. t.Errorf("public keys do not match: original:%x vs Public():%x", public, public2)
  63. }
  64. message := []byte("message")
  65. var noHash crypto.Hash
  66. signature, err := signer.Sign(zero, message, noHash)
  67. if err != nil {
  68. t.Fatalf("error from Sign(): %s", err)
  69. }
  70. if !ed25519.Verify(public, message, signature) {
  71. t.Errorf("Verify failed on signature from Sign()")
  72. }
  73. }
  74. func TestGolden(t *testing.T) {
  75. // sign.input.gz is a selection of test cases from
  76. // https://ed25519.cr.yp.to/python/sign.input
  77. testDataZ, err := os.Open("testdata/sign.input.gz")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. defer testDataZ.Close()
  82. testData, err := gzip.NewReader(testDataZ)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer testData.Close()
  87. scanner := bufio.NewScanner(testData)
  88. lineNo := 0
  89. for scanner.Scan() {
  90. lineNo++
  91. line := scanner.Text()
  92. parts := strings.Split(line, ":")
  93. if len(parts) != 5 {
  94. t.Fatalf("bad number of parts on line %d", lineNo)
  95. }
  96. privBytes, _ := hex.DecodeString(parts[0])
  97. pubKey, _ := hex.DecodeString(parts[1])
  98. msg, _ := hex.DecodeString(parts[2])
  99. sig, _ := hex.DecodeString(parts[3])
  100. // The signatures in the test vectors also include the message
  101. // at the end, but we just want R and S.
  102. sig = sig[:ed25519.SignatureSize]
  103. if l := len(pubKey); l != ed25519.PublicKeySize {
  104. t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
  105. }
  106. var priv [ed25519.PrivateKeySize]byte
  107. copy(priv[:], privBytes)
  108. copy(priv[32:], pubKey)
  109. sig2 := ed25519.Sign(priv[:], msg)
  110. if !bytes.Equal(sig, sig2[:]) {
  111. t.Errorf("different signature result on line %d: %x vs %x", lineNo, sig, sig2)
  112. }
  113. if !ed25519.Verify(pubKey, msg, sig2) {
  114. t.Errorf("signature failed to verify on line %d", lineNo)
  115. }
  116. priv2 := ed25519.NewKeyFromSeed(priv[:32])
  117. if !bytes.Equal(priv[:], priv2) {
  118. t.Errorf("recreating key pair gave different private key on line %d: %x vs %x", lineNo, priv[:], priv2)
  119. }
  120. if pubKey2 := priv2.Public().(ed25519.PublicKey); !bytes.Equal(pubKey, pubKey2) {
  121. t.Errorf("recreating key pair gave different public key on line %d: %x vs %x", lineNo, pubKey, pubKey2)
  122. }
  123. if seed := priv2.Seed(); !bytes.Equal(priv[:32], seed) {
  124. t.Errorf("recreating key pair gave different seed on line %d: %x vs %x", lineNo, priv[:32], seed)
  125. }
  126. }
  127. if err := scanner.Err(); err != nil {
  128. t.Fatalf("error reading test data: %s", err)
  129. }
  130. }
  131. func TestMalleability(t *testing.T) {
  132. // https://tools.ietf.org/html/rfc8032#section-5.1.7 adds an additional test
  133. // that s be in [0, order). This prevents someone from adding a multiple of
  134. // order to s and obtaining a second valid signature for the same message.
  135. msg := []byte{0x54, 0x65, 0x73, 0x74}
  136. sig := []byte{
  137. 0x7c, 0x38, 0xe0, 0x26, 0xf2, 0x9e, 0x14, 0xaa, 0xbd, 0x05, 0x9a,
  138. 0x0f, 0x2d, 0xb8, 0xb0, 0xcd, 0x78, 0x30, 0x40, 0x60, 0x9a, 0x8b,
  139. 0xe6, 0x84, 0xdb, 0x12, 0xf8, 0x2a, 0x27, 0x77, 0x4a, 0xb0, 0x67,
  140. 0x65, 0x4b, 0xce, 0x38, 0x32, 0xc2, 0xd7, 0x6f, 0x8f, 0x6f, 0x5d,
  141. 0xaf, 0xc0, 0x8d, 0x93, 0x39, 0xd4, 0xee, 0xf6, 0x76, 0x57, 0x33,
  142. 0x36, 0xa5, 0xc5, 0x1e, 0xb6, 0xf9, 0x46, 0xb3, 0x1d,
  143. }
  144. publicKey := []byte{
  145. 0x7d, 0x4d, 0x0e, 0x7f, 0x61, 0x53, 0xa6, 0x9b, 0x62, 0x42, 0xb5,
  146. 0x22, 0xab, 0xbe, 0xe6, 0x85, 0xfd, 0xa4, 0x42, 0x0f, 0x88, 0x34,
  147. 0xb1, 0x08, 0xc3, 0xbd, 0xae, 0x36, 0x9e, 0xf5, 0x49, 0xfa,
  148. }
  149. if ed25519.Verify(publicKey, msg, sig) {
  150. t.Fatal("non-canonical signature accepted")
  151. }
  152. }
  153. func BenchmarkKeyGeneration(b *testing.B) {
  154. var zero zeroReader
  155. for i := 0; i < b.N; i++ {
  156. if _, _, err := ed25519.GenerateKey(zero); err != nil {
  157. b.Fatal(err)
  158. }
  159. }
  160. }
  161. func BenchmarkSigning(b *testing.B) {
  162. var zero zeroReader
  163. _, priv, err := ed25519.GenerateKey(zero)
  164. if err != nil {
  165. b.Fatal(err)
  166. }
  167. message := []byte("Hello, world!")
  168. b.ResetTimer()
  169. for i := 0; i < b.N; i++ {
  170. ed25519.Sign(priv, message)
  171. }
  172. }
  173. func BenchmarkVerification(b *testing.B) {
  174. var zero zeroReader
  175. pub, priv, err := ed25519.GenerateKey(zero)
  176. if err != nil {
  177. b.Fatal(err)
  178. }
  179. message := []byte("Hello, world!")
  180. signature := ed25519.Sign(priv, message)
  181. b.ResetTimer()
  182. for i := 0; i < b.N; i++ {
  183. ed25519.Verify(pub, message, signature)
  184. }
  185. }