pss_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2013 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 rsa
  5. import (
  6. "bufio"
  7. "bytes"
  8. "compress/bzip2"
  9. "crypto"
  10. _ "crypto/md5"
  11. "crypto/rand"
  12. "crypto/sha1"
  13. _ "crypto/sha256"
  14. "encoding/hex"
  15. "math/big"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "testing"
  20. )
  21. func TestEMSAPSS(t *testing.T) {
  22. // Test vector in file pss-int.txt from: ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
  23. msg := []byte{
  24. 0x85, 0x9e, 0xef, 0x2f, 0xd7, 0x8a, 0xca, 0x00, 0x30, 0x8b,
  25. 0xdc, 0x47, 0x11, 0x93, 0xbf, 0x55, 0xbf, 0x9d, 0x78, 0xdb,
  26. 0x8f, 0x8a, 0x67, 0x2b, 0x48, 0x46, 0x34, 0xf3, 0xc9, 0xc2,
  27. 0x6e, 0x64, 0x78, 0xae, 0x10, 0x26, 0x0f, 0xe0, 0xdd, 0x8c,
  28. 0x08, 0x2e, 0x53, 0xa5, 0x29, 0x3a, 0xf2, 0x17, 0x3c, 0xd5,
  29. 0x0c, 0x6d, 0x5d, 0x35, 0x4f, 0xeb, 0xf7, 0x8b, 0x26, 0x02,
  30. 0x1c, 0x25, 0xc0, 0x27, 0x12, 0xe7, 0x8c, 0xd4, 0x69, 0x4c,
  31. 0x9f, 0x46, 0x97, 0x77, 0xe4, 0x51, 0xe7, 0xf8, 0xe9, 0xe0,
  32. 0x4c, 0xd3, 0x73, 0x9c, 0x6b, 0xbf, 0xed, 0xae, 0x48, 0x7f,
  33. 0xb5, 0x56, 0x44, 0xe9, 0xca, 0x74, 0xff, 0x77, 0xa5, 0x3c,
  34. 0xb7, 0x29, 0x80, 0x2f, 0x6e, 0xd4, 0xa5, 0xff, 0xa8, 0xba,
  35. 0x15, 0x98, 0x90, 0xfc,
  36. }
  37. salt := []byte{
  38. 0xe3, 0xb5, 0xd5, 0xd0, 0x02, 0xc1, 0xbc, 0xe5, 0x0c, 0x2b,
  39. 0x65, 0xef, 0x88, 0xa1, 0x88, 0xd8, 0x3b, 0xce, 0x7e, 0x61,
  40. }
  41. expected := []byte{
  42. 0x66, 0xe4, 0x67, 0x2e, 0x83, 0x6a, 0xd1, 0x21, 0xba, 0x24,
  43. 0x4b, 0xed, 0x65, 0x76, 0xb8, 0x67, 0xd9, 0xa4, 0x47, 0xc2,
  44. 0x8a, 0x6e, 0x66, 0xa5, 0xb8, 0x7d, 0xee, 0x7f, 0xbc, 0x7e,
  45. 0x65, 0xaf, 0x50, 0x57, 0xf8, 0x6f, 0xae, 0x89, 0x84, 0xd9,
  46. 0xba, 0x7f, 0x96, 0x9a, 0xd6, 0xfe, 0x02, 0xa4, 0xd7, 0x5f,
  47. 0x74, 0x45, 0xfe, 0xfd, 0xd8, 0x5b, 0x6d, 0x3a, 0x47, 0x7c,
  48. 0x28, 0xd2, 0x4b, 0xa1, 0xe3, 0x75, 0x6f, 0x79, 0x2d, 0xd1,
  49. 0xdc, 0xe8, 0xca, 0x94, 0x44, 0x0e, 0xcb, 0x52, 0x79, 0xec,
  50. 0xd3, 0x18, 0x3a, 0x31, 0x1f, 0xc8, 0x96, 0xda, 0x1c, 0xb3,
  51. 0x93, 0x11, 0xaf, 0x37, 0xea, 0x4a, 0x75, 0xe2, 0x4b, 0xdb,
  52. 0xfd, 0x5c, 0x1d, 0xa0, 0xde, 0x7c, 0xec, 0xdf, 0x1a, 0x89,
  53. 0x6f, 0x9d, 0x8b, 0xc8, 0x16, 0xd9, 0x7c, 0xd7, 0xa2, 0xc4,
  54. 0x3b, 0xad, 0x54, 0x6f, 0xbe, 0x8c, 0xfe, 0xbc,
  55. }
  56. hash := sha1.New()
  57. hash.Write(msg)
  58. hashed := hash.Sum(nil)
  59. encoded, err := emsaPSSEncode(hashed, 1023, salt, sha1.New())
  60. if err != nil {
  61. t.Errorf("Error from emsaPSSEncode: %s\n", err)
  62. }
  63. if !bytes.Equal(encoded, expected) {
  64. t.Errorf("Bad encoding. got %x, want %x", encoded, expected)
  65. }
  66. if err = emsaPSSVerify(hashed, encoded, 1023, len(salt), sha1.New()); err != nil {
  67. t.Errorf("Bad verification: %s", err)
  68. }
  69. }
  70. // TestPSSGolden tests all the test vectors in pss-vect.txt from
  71. // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
  72. func TestPSSGolden(t *testing.T) {
  73. inFile, err := os.Open("testdata/pss-vect.txt.bz2")
  74. if err != nil {
  75. t.Fatalf("Failed to open input file: %s", err)
  76. }
  77. defer inFile.Close()
  78. // The pss-vect.txt file contains RSA keys and then a series of
  79. // signatures. A goroutine is used to preprocess the input by merging
  80. // lines, removing spaces in hex values and identifying the start of
  81. // new keys and signature blocks.
  82. const newKeyMarker = "START NEW KEY"
  83. const newSignatureMarker = "START NEW SIGNATURE"
  84. values := make(chan string)
  85. go func() {
  86. defer close(values)
  87. scanner := bufio.NewScanner(bzip2.NewReader(inFile))
  88. var partialValue string
  89. lastWasValue := true
  90. for scanner.Scan() {
  91. line := scanner.Text()
  92. switch {
  93. case len(line) == 0:
  94. if len(partialValue) > 0 {
  95. values <- strings.Replace(partialValue, " ", "", -1)
  96. partialValue = ""
  97. lastWasValue = true
  98. }
  99. continue
  100. case strings.HasPrefix(line, "# ======") && lastWasValue:
  101. values <- newKeyMarker
  102. lastWasValue = false
  103. case strings.HasPrefix(line, "# ------") && lastWasValue:
  104. values <- newSignatureMarker
  105. lastWasValue = false
  106. case strings.HasPrefix(line, "#"):
  107. continue
  108. default:
  109. partialValue += line
  110. }
  111. }
  112. if err := scanner.Err(); err != nil {
  113. panic(err)
  114. }
  115. }()
  116. var key *PublicKey
  117. var hashed []byte
  118. hash := crypto.SHA1
  119. h := hash.New()
  120. opts := &PSSOptions{
  121. SaltLength: PSSSaltLengthEqualsHash,
  122. }
  123. for marker := range values {
  124. switch marker {
  125. case newKeyMarker:
  126. key = new(PublicKey)
  127. nHex, ok := <-values
  128. if !ok {
  129. continue
  130. }
  131. key.N = bigFromHex(nHex)
  132. key.E = intFromHex(<-values)
  133. // We don't care for d, p, q, dP, dQ or qInv.
  134. for i := 0; i < 6; i++ {
  135. <-values
  136. }
  137. case newSignatureMarker:
  138. msg := fromHex(<-values)
  139. <-values // skip salt
  140. sig := fromHex(<-values)
  141. h.Reset()
  142. h.Write(msg)
  143. hashed = h.Sum(hashed[:0])
  144. if err := VerifyPSS(key, hash, hashed, sig, opts); err != nil {
  145. t.Error(err)
  146. }
  147. default:
  148. t.Fatalf("unknown marker: " + marker)
  149. }
  150. }
  151. }
  152. // TestPSSOpenSSL ensures that we can verify a PSS signature from OpenSSL with
  153. // the default options. OpenSSL sets the salt length to be maximal.
  154. func TestPSSOpenSSL(t *testing.T) {
  155. hash := crypto.SHA256
  156. h := hash.New()
  157. h.Write([]byte("testing"))
  158. hashed := h.Sum(nil)
  159. // Generated with `echo -n testing | openssl dgst -sign key.pem -sigopt rsa_padding_mode:pss -sha256 > sig`
  160. sig := []byte{
  161. 0x95, 0x59, 0x6f, 0xd3, 0x10, 0xa2, 0xe7, 0xa2, 0x92, 0x9d,
  162. 0x4a, 0x07, 0x2e, 0x2b, 0x27, 0xcc, 0x06, 0xc2, 0x87, 0x2c,
  163. 0x52, 0xf0, 0x4a, 0xcc, 0x05, 0x94, 0xf2, 0xc3, 0x2e, 0x20,
  164. 0xd7, 0x3e, 0x66, 0x62, 0xb5, 0x95, 0x2b, 0xa3, 0x93, 0x9a,
  165. 0x66, 0x64, 0x25, 0xe0, 0x74, 0x66, 0x8c, 0x3e, 0x92, 0xeb,
  166. 0xc6, 0xe6, 0xc0, 0x44, 0xf3, 0xb4, 0xb4, 0x2e, 0x8c, 0x66,
  167. 0x0a, 0x37, 0x9c, 0x69,
  168. }
  169. if err := VerifyPSS(&rsaPrivateKey.PublicKey, hash, hashed, sig, nil); err != nil {
  170. t.Error(err)
  171. }
  172. }
  173. func TestPSSNilOpts(t *testing.T) {
  174. hash := crypto.SHA256
  175. h := hash.New()
  176. h.Write([]byte("testing"))
  177. hashed := h.Sum(nil)
  178. SignPSS(rand.Reader, rsaPrivateKey, hash, hashed, nil)
  179. }
  180. func TestPSSSigning(t *testing.T) {
  181. var saltLengthCombinations = []struct {
  182. signSaltLength, verifySaltLength int
  183. good bool
  184. }{
  185. {PSSSaltLengthAuto, PSSSaltLengthAuto, true},
  186. {PSSSaltLengthEqualsHash, PSSSaltLengthAuto, true},
  187. {PSSSaltLengthEqualsHash, PSSSaltLengthEqualsHash, true},
  188. {PSSSaltLengthEqualsHash, 8, false},
  189. {PSSSaltLengthAuto, PSSSaltLengthEqualsHash, false},
  190. {8, 8, true},
  191. }
  192. hash := crypto.MD5
  193. h := hash.New()
  194. h.Write([]byte("testing"))
  195. hashed := h.Sum(nil)
  196. var opts PSSOptions
  197. for i, test := range saltLengthCombinations {
  198. opts.SaltLength = test.signSaltLength
  199. sig, err := SignPSS(rand.Reader, rsaPrivateKey, hash, hashed, &opts)
  200. if err != nil {
  201. t.Errorf("#%d: error while signing: %s", i, err)
  202. continue
  203. }
  204. opts.SaltLength = test.verifySaltLength
  205. err = VerifyPSS(&rsaPrivateKey.PublicKey, hash, hashed, sig, &opts)
  206. if (err == nil) != test.good {
  207. t.Errorf("#%d: bad result, wanted: %t, got: %s", i, test.good, err)
  208. }
  209. }
  210. }
  211. func bigFromHex(hex string) *big.Int {
  212. n, ok := new(big.Int).SetString(hex, 16)
  213. if !ok {
  214. panic("bad hex: " + hex)
  215. }
  216. return n
  217. }
  218. func intFromHex(hex string) int {
  219. i, err := strconv.ParseInt(hex, 16, 32)
  220. if err != nil {
  221. panic(err)
  222. }
  223. return int(i)
  224. }
  225. func fromHex(hexStr string) []byte {
  226. s, err := hex.DecodeString(hexStr)
  227. if err != nil {
  228. panic(err)
  229. }
  230. return s
  231. }