dsa.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 dsa implements the Digital Signature Algorithm, as defined in FIPS 186-3.
  5. package dsa
  6. import (
  7. "errors"
  8. "io"
  9. "math/big"
  10. )
  11. // Parameters represents the domain parameters for a key. These parameters can
  12. // be shared across many keys. The bit length of Q must be a multiple of 8.
  13. type Parameters struct {
  14. P, Q, G *big.Int
  15. }
  16. // PublicKey represents a DSA public key.
  17. type PublicKey struct {
  18. Parameters
  19. Y *big.Int
  20. }
  21. // PrivateKey represents a DSA private key.
  22. type PrivateKey struct {
  23. PublicKey
  24. X *big.Int
  25. }
  26. // ErrInvalidPublicKey results when a public key is not usable by this code.
  27. // FIPS is quite strict about the format of DSA keys, but other code may be
  28. // less so. Thus, when using keys which may have been generated by other code,
  29. // this error must be handled.
  30. var ErrInvalidPublicKey = errors.New("crypto/dsa: invalid public key")
  31. // ParameterSizes is a enumeration of the acceptable bit lengths of the primes
  32. // in a set of DSA parameters. See FIPS 186-3, section 4.2.
  33. type ParameterSizes int
  34. const (
  35. L1024N160 ParameterSizes = iota
  36. L2048N224
  37. L2048N256
  38. L3072N256
  39. )
  40. // numMRTests is the number of Miller-Rabin primality tests that we perform. We
  41. // pick the largest recommended number from table C.1 of FIPS 186-3.
  42. const numMRTests = 64
  43. // GenerateParameters puts a random, valid set of DSA parameters into params.
  44. // This function takes many seconds, even on fast machines.
  45. func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) (err error) {
  46. // This function doesn't follow FIPS 186-3 exactly in that it doesn't
  47. // use a verification seed to generate the primes. The verification
  48. // seed doesn't appear to be exported or used by other code and
  49. // omitting it makes the code cleaner.
  50. var L, N int
  51. switch sizes {
  52. case L1024N160:
  53. L = 1024
  54. N = 160
  55. case L2048N224:
  56. L = 2048
  57. N = 224
  58. case L2048N256:
  59. L = 2048
  60. N = 256
  61. case L3072N256:
  62. L = 3072
  63. N = 256
  64. default:
  65. return errors.New("crypto/dsa: invalid ParameterSizes")
  66. }
  67. qBytes := make([]byte, N/8)
  68. pBytes := make([]byte, L/8)
  69. q := new(big.Int)
  70. p := new(big.Int)
  71. rem := new(big.Int)
  72. one := new(big.Int)
  73. one.SetInt64(1)
  74. GeneratePrimes:
  75. for {
  76. _, err = io.ReadFull(rand, qBytes)
  77. if err != nil {
  78. return
  79. }
  80. qBytes[len(qBytes)-1] |= 1
  81. qBytes[0] |= 0x80
  82. q.SetBytes(qBytes)
  83. if !q.ProbablyPrime(numMRTests) {
  84. continue
  85. }
  86. for i := 0; i < 4*L; i++ {
  87. _, err = io.ReadFull(rand, pBytes)
  88. if err != nil {
  89. return
  90. }
  91. pBytes[len(pBytes)-1] |= 1
  92. pBytes[0] |= 0x80
  93. p.SetBytes(pBytes)
  94. rem.Mod(p, q)
  95. rem.Sub(rem, one)
  96. p.Sub(p, rem)
  97. if p.BitLen() < L {
  98. continue
  99. }
  100. if !p.ProbablyPrime(numMRTests) {
  101. continue
  102. }
  103. params.P = p
  104. params.Q = q
  105. break GeneratePrimes
  106. }
  107. }
  108. h := new(big.Int)
  109. h.SetInt64(2)
  110. g := new(big.Int)
  111. pm1 := new(big.Int).Sub(p, one)
  112. e := new(big.Int).Div(pm1, q)
  113. for {
  114. g.Exp(h, e, p)
  115. if g.Cmp(one) == 0 {
  116. h.Add(h, one)
  117. continue
  118. }
  119. params.G = g
  120. return
  121. }
  122. }
  123. // GenerateKey generates a public&private key pair. The Parameters of the
  124. // PrivateKey must already be valid (see GenerateParameters).
  125. func GenerateKey(priv *PrivateKey, rand io.Reader) error {
  126. if priv.P == nil || priv.Q == nil || priv.G == nil {
  127. return errors.New("crypto/dsa: parameters not set up before generating key")
  128. }
  129. x := new(big.Int)
  130. xBytes := make([]byte, priv.Q.BitLen()/8)
  131. for {
  132. _, err := io.ReadFull(rand, xBytes)
  133. if err != nil {
  134. return err
  135. }
  136. x.SetBytes(xBytes)
  137. if x.Sign() != 0 && x.Cmp(priv.Q) < 0 {
  138. break
  139. }
  140. }
  141. priv.X = x
  142. priv.Y = new(big.Int)
  143. priv.Y.Exp(priv.G, x, priv.P)
  144. return nil
  145. }
  146. // fermatInverse calculates the inverse of k in GF(P) using Fermat's method.
  147. // This has better constant-time properties than Euclid's method (implemented
  148. // in math/big.Int.ModInverse) although math/big itself isn't strictly
  149. // constant-time so it's not perfect.
  150. func fermatInverse(k, P *big.Int) *big.Int {
  151. two := big.NewInt(2)
  152. pMinus2 := new(big.Int).Sub(P, two)
  153. return new(big.Int).Exp(k, pMinus2, P)
  154. }
  155. // Sign signs an arbitrary length hash (which should be the result of hashing a
  156. // larger message) using the private key, priv. It returns the signature as a
  157. // pair of integers. The security of the private key depends on the entropy of
  158. // rand.
  159. //
  160. // Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
  161. // to the byte-length of the subgroup. This function does not perform that
  162. // truncation itself.
  163. func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) {
  164. // FIPS 186-3, section 4.6
  165. n := priv.Q.BitLen()
  166. if n&7 != 0 {
  167. err = ErrInvalidPublicKey
  168. return
  169. }
  170. n >>= 3
  171. for {
  172. k := new(big.Int)
  173. buf := make([]byte, n)
  174. for {
  175. _, err = io.ReadFull(rand, buf)
  176. if err != nil {
  177. return
  178. }
  179. k.SetBytes(buf)
  180. if k.Sign() > 0 && k.Cmp(priv.Q) < 0 {
  181. break
  182. }
  183. }
  184. kInv := fermatInverse(k, priv.Q)
  185. r = new(big.Int).Exp(priv.G, k, priv.P)
  186. r.Mod(r, priv.Q)
  187. if r.Sign() == 0 {
  188. continue
  189. }
  190. z := k.SetBytes(hash)
  191. s = new(big.Int).Mul(priv.X, r)
  192. s.Add(s, z)
  193. s.Mod(s, priv.Q)
  194. s.Mul(s, kInv)
  195. s.Mod(s, priv.Q)
  196. if s.Sign() != 0 {
  197. break
  198. }
  199. }
  200. return
  201. }
  202. // Verify verifies the signature in r, s of hash using the public key, pub. It
  203. // reports whether the signature is valid.
  204. //
  205. // Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
  206. // to the byte-length of the subgroup. This function does not perform that
  207. // truncation itself.
  208. func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
  209. // FIPS 186-3, section 4.7
  210. if r.Sign() < 1 || r.Cmp(pub.Q) >= 0 {
  211. return false
  212. }
  213. if s.Sign() < 1 || s.Cmp(pub.Q) >= 0 {
  214. return false
  215. }
  216. w := new(big.Int).ModInverse(s, pub.Q)
  217. n := pub.Q.BitLen()
  218. if n&7 != 0 {
  219. return false
  220. }
  221. z := new(big.Int).SetBytes(hash)
  222. u1 := new(big.Int).Mul(z, w)
  223. u1.Mod(u1, pub.Q)
  224. u2 := w.Mul(r, w)
  225. u2.Mod(u2, pub.Q)
  226. v := u1.Exp(pub.G, u1, pub.P)
  227. u2.Exp(pub.Y, u2, pub.P)
  228. v.Mul(v, u2)
  229. v.Mod(v, pub.P)
  230. v.Mod(v, pub.Q)
  231. return v.Cmp(r) == 0
  232. }