ed25519_test.go 6.5 KB

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