ecdsa_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2011 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 ecdsa
  5. import (
  6. "bufio"
  7. "compress/bzip2"
  8. "crypto/elliptic"
  9. "crypto/rand"
  10. "crypto/sha1"
  11. "crypto/sha256"
  12. "crypto/sha512"
  13. "encoding/hex"
  14. "hash"
  15. "io"
  16. "math/big"
  17. "os"
  18. "strings"
  19. "testing"
  20. )
  21. func testKeyGeneration(t *testing.T, c elliptic.Curve, tag string) {
  22. priv, err := GenerateKey(c, rand.Reader)
  23. if err != nil {
  24. t.Errorf("%s: error: %s", tag, err)
  25. return
  26. }
  27. if !c.IsOnCurve(priv.PublicKey.X, priv.PublicKey.Y) {
  28. t.Errorf("%s: public key invalid: %s", tag, err)
  29. }
  30. }
  31. func TestKeyGeneration(t *testing.T) {
  32. testKeyGeneration(t, elliptic.P224(), "p224")
  33. if testing.Short() {
  34. return
  35. }
  36. testKeyGeneration(t, elliptic.P256(), "p256")
  37. testKeyGeneration(t, elliptic.P384(), "p384")
  38. testKeyGeneration(t, elliptic.P521(), "p521")
  39. }
  40. func testSignAndVerify(t *testing.T, c elliptic.Curve, tag string) {
  41. priv, _ := GenerateKey(c, rand.Reader)
  42. hashed := []byte("testing")
  43. r, s, err := Sign(rand.Reader, priv, hashed)
  44. if err != nil {
  45. t.Errorf("%s: error signing: %s", tag, err)
  46. return
  47. }
  48. if !Verify(&priv.PublicKey, hashed, r, s) {
  49. t.Errorf("%s: Verify failed", tag)
  50. }
  51. hashed[0] ^= 0xff
  52. if Verify(&priv.PublicKey, hashed, r, s) {
  53. t.Errorf("%s: Verify always works!", tag)
  54. }
  55. }
  56. func TestSignAndVerify(t *testing.T) {
  57. testSignAndVerify(t, elliptic.P224(), "p224")
  58. if testing.Short() {
  59. return
  60. }
  61. testSignAndVerify(t, elliptic.P256(), "p256")
  62. testSignAndVerify(t, elliptic.P384(), "p384")
  63. testSignAndVerify(t, elliptic.P521(), "p521")
  64. }
  65. func fromHex(s string) *big.Int {
  66. r, ok := new(big.Int).SetString(s, 16)
  67. if !ok {
  68. panic("bad hex")
  69. }
  70. return r
  71. }
  72. func TestVectors(t *testing.T) {
  73. // This test runs the full set of NIST test vectors from
  74. // http://csrc.nist.gov/groups/STM/cavp/documents/dss/186-3ecdsatestvectors.zip
  75. //
  76. // The SigVer.rsp file has been edited to remove test vectors for
  77. // unsupported algorithms and has been compressed.
  78. if testing.Short() {
  79. return
  80. }
  81. f, err := os.Open("testdata/SigVer.rsp.bz2")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. buf := bufio.NewReader(bzip2.NewReader(f))
  86. lineNo := 1
  87. var h hash.Hash
  88. var msg []byte
  89. var hashed []byte
  90. var r, s *big.Int
  91. pub := new(PublicKey)
  92. for {
  93. line, err := buf.ReadString('\n')
  94. if len(line) == 0 {
  95. if err == io.EOF {
  96. break
  97. }
  98. t.Fatalf("error reading from input: %s", err)
  99. }
  100. lineNo++
  101. // Need to remove \r\n from the end of the line.
  102. if !strings.HasSuffix(line, "\r\n") {
  103. t.Fatalf("bad line ending (expected \\r\\n) on line %d", lineNo)
  104. }
  105. line = line[:len(line)-2]
  106. if len(line) == 0 || line[0] == '#' {
  107. continue
  108. }
  109. if line[0] == '[' {
  110. line = line[1 : len(line)-1]
  111. parts := strings.SplitN(line, ",", 2)
  112. switch parts[0] {
  113. case "P-224":
  114. pub.Curve = elliptic.P224()
  115. case "P-256":
  116. pub.Curve = elliptic.P256()
  117. case "P-384":
  118. pub.Curve = elliptic.P384()
  119. case "P-521":
  120. pub.Curve = elliptic.P521()
  121. default:
  122. pub.Curve = nil
  123. }
  124. switch parts[1] {
  125. case "SHA-1":
  126. h = sha1.New()
  127. case "SHA-224":
  128. h = sha256.New224()
  129. case "SHA-256":
  130. h = sha256.New()
  131. case "SHA-384":
  132. h = sha512.New384()
  133. case "SHA-512":
  134. h = sha512.New()
  135. default:
  136. h = nil
  137. }
  138. continue
  139. }
  140. if h == nil || pub.Curve == nil {
  141. continue
  142. }
  143. switch {
  144. case strings.HasPrefix(line, "Msg = "):
  145. if msg, err = hex.DecodeString(line[6:]); err != nil {
  146. t.Fatalf("failed to decode message on line %d: %s", lineNo, err)
  147. }
  148. case strings.HasPrefix(line, "Qx = "):
  149. pub.X = fromHex(line[5:])
  150. case strings.HasPrefix(line, "Qy = "):
  151. pub.Y = fromHex(line[5:])
  152. case strings.HasPrefix(line, "R = "):
  153. r = fromHex(line[4:])
  154. case strings.HasPrefix(line, "S = "):
  155. s = fromHex(line[4:])
  156. case strings.HasPrefix(line, "Result = "):
  157. expected := line[9] == 'P'
  158. h.Reset()
  159. h.Write(msg)
  160. hashed := h.Sum(hashed[:0])
  161. if Verify(pub, hashed, r, s) != expected {
  162. t.Fatalf("incorrect result on line %d", lineNo)
  163. }
  164. default:
  165. t.Fatalf("unknown variable on line %d: %s", lineNo, line)
  166. }
  167. }
  168. }