s2k.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 s2k implements the various OpenPGP string-to-key transforms as
  5. // specified in RFC 4800 section 3.7.1.
  6. package s2k
  7. import (
  8. "code.google.com/p/go.crypto/openpgp/errors"
  9. "crypto"
  10. "hash"
  11. "io"
  12. "strconv"
  13. )
  14. // Simple writes to out the result of computing the Simple S2K function (RFC
  15. // 4880, section 3.7.1.1) using the given hash and input passphrase.
  16. func Simple(out []byte, h hash.Hash, in []byte) {
  17. Salted(out, h, in, nil)
  18. }
  19. var zero [1]byte
  20. // Salted writes to out the result of computing the Salted S2K function (RFC
  21. // 4880, section 3.7.1.2) using the given hash, input passphrase and salt.
  22. func Salted(out []byte, h hash.Hash, in []byte, salt []byte) {
  23. done := 0
  24. var digest []byte
  25. for i := 0; done < len(out); i++ {
  26. h.Reset()
  27. for j := 0; j < i; j++ {
  28. h.Write(zero[:])
  29. }
  30. h.Write(salt)
  31. h.Write(in)
  32. digest = h.Sum(digest[:0])
  33. n := copy(out[done:], digest)
  34. done += n
  35. }
  36. }
  37. // Iterated writes to out the result of computing the Iterated and Salted S2K
  38. // function (RFC 4880, section 3.7.1.3) using the given hash, input passphrase,
  39. // salt and iteration count.
  40. func Iterated(out []byte, h hash.Hash, in []byte, salt []byte, count int) {
  41. combined := make([]byte, len(in)+len(salt))
  42. copy(combined, salt)
  43. copy(combined[len(salt):], in)
  44. if count < len(combined) {
  45. count = len(combined)
  46. }
  47. done := 0
  48. var digest []byte
  49. for i := 0; done < len(out); i++ {
  50. h.Reset()
  51. for j := 0; j < i; j++ {
  52. h.Write(zero[:])
  53. }
  54. written := 0
  55. for written < count {
  56. if written+len(combined) > count {
  57. todo := count - written
  58. h.Write(combined[:todo])
  59. written = count
  60. } else {
  61. h.Write(combined)
  62. written += len(combined)
  63. }
  64. }
  65. digest = h.Sum(digest[:0])
  66. n := copy(out[done:], digest)
  67. done += n
  68. }
  69. }
  70. // Parse reads a binary specification for a string-to-key transformation from r
  71. // and returns a function which performs that transform.
  72. func Parse(r io.Reader) (f func(out, in []byte), err error) {
  73. var buf [9]byte
  74. _, err = io.ReadFull(r, buf[:2])
  75. if err != nil {
  76. return
  77. }
  78. hash, ok := HashIdToHash(buf[1])
  79. if !ok {
  80. return nil, errors.UnsupportedError("hash for S2K function: " + strconv.Itoa(int(buf[1])))
  81. }
  82. h := hash.New()
  83. if h == nil {
  84. return nil, errors.UnsupportedError("hash not available: " + strconv.Itoa(int(hash)))
  85. }
  86. switch buf[0] {
  87. case 0:
  88. f := func(out, in []byte) {
  89. Simple(out, h, in)
  90. }
  91. return f, nil
  92. case 1:
  93. _, err = io.ReadFull(r, buf[:8])
  94. if err != nil {
  95. return
  96. }
  97. f := func(out, in []byte) {
  98. Salted(out, h, in, buf[:8])
  99. }
  100. return f, nil
  101. case 3:
  102. _, err = io.ReadFull(r, buf[:9])
  103. if err != nil {
  104. return
  105. }
  106. count := (16 + int(buf[8]&15)) << (uint32(buf[8]>>4) + 6)
  107. f := func(out, in []byte) {
  108. Iterated(out, h, in, buf[:8], count)
  109. }
  110. return f, nil
  111. }
  112. return nil, errors.UnsupportedError("S2K function")
  113. }
  114. // Serialize salts and stretches the given passphrase and writes the resulting
  115. // key into key. It also serializes an S2K descriptor to w.
  116. func Serialize(w io.Writer, key []byte, rand io.Reader, passphrase []byte) error {
  117. var buf [11]byte
  118. buf[0] = 3 /* iterated and salted */
  119. buf[1], _ = HashToHashId(crypto.SHA1)
  120. salt := buf[2:10]
  121. if _, err := io.ReadFull(rand, salt); err != nil {
  122. return err
  123. }
  124. const count = 65536 // this is the default in gpg
  125. buf[10] = 96 // 65536 iterations
  126. if _, err := w.Write(buf[:]); err != nil {
  127. return err
  128. }
  129. Iterated(key, crypto.SHA1.New(), passphrase, salt, count)
  130. return nil
  131. }
  132. // hashToHashIdMapping contains pairs relating OpenPGP's hash identifier with
  133. // Go's crypto.Hash type. See RFC 4880, section 9.4.
  134. var hashToHashIdMapping = []struct {
  135. id byte
  136. hash crypto.Hash
  137. }{
  138. {1, crypto.MD5},
  139. {2, crypto.SHA1},
  140. {3, crypto.RIPEMD160},
  141. {8, crypto.SHA256},
  142. {9, crypto.SHA384},
  143. {10, crypto.SHA512},
  144. {11, crypto.SHA224},
  145. }
  146. // HashIdToHash returns a crypto.Hash which corresponds to the given OpenPGP
  147. // hash id.
  148. func HashIdToHash(id byte) (h crypto.Hash, ok bool) {
  149. for _, m := range hashToHashIdMapping {
  150. if m.id == id {
  151. return m.hash, true
  152. }
  153. }
  154. return 0, false
  155. }
  156. // HashIdToHash returns an OpenPGP hash id which corresponds the given Hash.
  157. func HashToHashId(h crypto.Hash) (id byte, ok bool) {
  158. for _, m := range hashToHashIdMapping {
  159. if m.hash == h {
  160. return m.id, true
  161. }
  162. }
  163. return 0, false
  164. }