ecies.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Copyright (c) 2013 Kyle Isom <kyle@tyrfingr.is>
  2. // Copyright (c) 2012 The Go Authors. All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. package ecies
  30. import (
  31. "crypto/cipher"
  32. "crypto/ecdsa"
  33. "crypto/elliptic"
  34. "crypto/hmac"
  35. "crypto/subtle"
  36. "encoding/binary"
  37. "fmt"
  38. "hash"
  39. "io"
  40. "math/big"
  41. )
  42. var (
  43. ErrImport = fmt.Errorf("ecies: failed to import key")
  44. ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve")
  45. ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key")
  46. ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity")
  47. ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big")
  48. )
  49. // PublicKey is a representation of an elliptic curve public key.
  50. type PublicKey struct {
  51. X *big.Int
  52. Y *big.Int
  53. elliptic.Curve
  54. Params *ECIESParams
  55. }
  56. // Export an ECIES public key as an ECDSA public key.
  57. func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey {
  58. return &ecdsa.PublicKey{Curve: pub.Curve, X: pub.X, Y: pub.Y}
  59. }
  60. // Import an ECDSA public key as an ECIES public key.
  61. func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey {
  62. return &PublicKey{
  63. X: pub.X,
  64. Y: pub.Y,
  65. Curve: pub.Curve,
  66. Params: ParamsFromCurve(pub.Curve),
  67. }
  68. }
  69. // PrivateKey is a representation of an elliptic curve private key.
  70. type PrivateKey struct {
  71. PublicKey
  72. D *big.Int
  73. }
  74. // Export an ECIES private key as an ECDSA private key.
  75. func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey {
  76. pub := &prv.PublicKey
  77. pubECDSA := pub.ExportECDSA()
  78. return &ecdsa.PrivateKey{PublicKey: *pubECDSA, D: prv.D}
  79. }
  80. // Import an ECDSA private key as an ECIES private key.
  81. func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey {
  82. pub := ImportECDSAPublic(&prv.PublicKey)
  83. return &PrivateKey{*pub, prv.D}
  84. }
  85. // Generate an elliptic curve public / private keypair. If params is nil,
  86. // the recommended default parameters for the key will be chosen.
  87. func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error) {
  88. pb, x, y, err := elliptic.GenerateKey(curve, rand)
  89. if err != nil {
  90. return
  91. }
  92. prv = new(PrivateKey)
  93. prv.PublicKey.X = x
  94. prv.PublicKey.Y = y
  95. prv.PublicKey.Curve = curve
  96. prv.D = new(big.Int).SetBytes(pb)
  97. if params == nil {
  98. params = ParamsFromCurve(curve)
  99. }
  100. prv.PublicKey.Params = params
  101. return
  102. }
  103. // MaxSharedKeyLength returns the maximum length of the shared key the
  104. // public key can produce.
  105. func MaxSharedKeyLength(pub *PublicKey) int {
  106. return (pub.Curve.Params().BitSize + 7) / 8
  107. }
  108. // ECDH key agreement method used to establish secret keys for encryption.
  109. func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error) {
  110. if prv.PublicKey.Curve != pub.Curve {
  111. return nil, ErrInvalidCurve
  112. }
  113. if skLen+macLen > MaxSharedKeyLength(pub) {
  114. return nil, ErrSharedKeyTooBig
  115. }
  116. x, _ := pub.Curve.ScalarMult(pub.X, pub.Y, prv.D.Bytes())
  117. if x == nil {
  118. return nil, ErrSharedKeyIsPointAtInfinity
  119. }
  120. sk = make([]byte, skLen+macLen)
  121. skBytes := x.Bytes()
  122. copy(sk[len(sk)-len(skBytes):], skBytes)
  123. return sk, nil
  124. }
  125. var (
  126. ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long")
  127. ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
  128. )
  129. // NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).
  130. func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) []byte {
  131. counterBytes := make([]byte, 4)
  132. k := make([]byte, 0, roundup(kdLen, hash.Size()))
  133. for counter := uint32(1); len(k) < kdLen; counter++ {
  134. binary.BigEndian.PutUint32(counterBytes, counter)
  135. hash.Reset()
  136. hash.Write(counterBytes)
  137. hash.Write(z)
  138. hash.Write(s1)
  139. k = hash.Sum(k)
  140. }
  141. return k[:kdLen]
  142. }
  143. // roundup rounds size up to the next multiple of blocksize.
  144. func roundup(size, blocksize int) int {
  145. return size + blocksize - (size % blocksize)
  146. }
  147. // deriveKeys creates the encryption and MAC keys using concatKDF.
  148. func deriveKeys(hash hash.Hash, z, s1 []byte, keyLen int) (Ke, Km []byte) {
  149. K := concatKDF(hash, z, s1, 2*keyLen)
  150. Ke = K[:keyLen]
  151. Km = K[keyLen:]
  152. hash.Reset()
  153. hash.Write(Km)
  154. Km = hash.Sum(Km[:0])
  155. return Ke, Km
  156. }
  157. // messageTag computes the MAC of a message (called the tag) as per
  158. // SEC 1, 3.5.
  159. func messageTag(hash func() hash.Hash, km, msg, shared []byte) []byte {
  160. mac := hmac.New(hash, km)
  161. mac.Write(msg)
  162. mac.Write(shared)
  163. tag := mac.Sum(nil)
  164. return tag
  165. }
  166. // Generate an initialisation vector for CTR mode.
  167. func generateIV(params *ECIESParams, rand io.Reader) (iv []byte, err error) {
  168. iv = make([]byte, params.BlockSize)
  169. _, err = io.ReadFull(rand, iv)
  170. return
  171. }
  172. // symEncrypt carries out CTR encryption using the block cipher specified in the
  173. func symEncrypt(rand io.Reader, params *ECIESParams, key, m []byte) (ct []byte, err error) {
  174. c, err := params.Cipher(key)
  175. if err != nil {
  176. return
  177. }
  178. iv, err := generateIV(params, rand)
  179. if err != nil {
  180. return
  181. }
  182. ctr := cipher.NewCTR(c, iv)
  183. ct = make([]byte, len(m)+params.BlockSize)
  184. copy(ct, iv)
  185. ctr.XORKeyStream(ct[params.BlockSize:], m)
  186. return
  187. }
  188. // symDecrypt carries out CTR decryption using the block cipher specified in
  189. // the parameters
  190. func symDecrypt(params *ECIESParams, key, ct []byte) (m []byte, err error) {
  191. c, err := params.Cipher(key)
  192. if err != nil {
  193. return
  194. }
  195. ctr := cipher.NewCTR(c, ct[:params.BlockSize])
  196. m = make([]byte, len(ct)-params.BlockSize)
  197. ctr.XORKeyStream(m, ct[params.BlockSize:])
  198. return
  199. }
  200. // Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1.
  201. //
  202. // s1 and s2 contain shared information that is not part of the resulting
  203. // ciphertext. s1 is fed into key derivation, s2 is fed into the MAC. If the
  204. // shared information parameters aren't being used, they should be nil.
  205. func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err error) {
  206. params, err := pubkeyParams(pub)
  207. if err != nil {
  208. return nil, err
  209. }
  210. R, err := GenerateKey(rand, pub.Curve, params)
  211. if err != nil {
  212. return nil, err
  213. }
  214. z, err := R.GenerateShared(pub, params.KeyLen, params.KeyLen)
  215. if err != nil {
  216. return nil, err
  217. }
  218. hash := params.Hash()
  219. Ke, Km := deriveKeys(hash, z, s1, params.KeyLen)
  220. em, err := symEncrypt(rand, params, Ke, m)
  221. if err != nil || len(em) <= params.BlockSize {
  222. return nil, err
  223. }
  224. d := messageTag(params.Hash, Km, em, s2)
  225. Rb := elliptic.Marshal(pub.Curve, R.PublicKey.X, R.PublicKey.Y)
  226. ct = make([]byte, len(Rb)+len(em)+len(d))
  227. copy(ct, Rb)
  228. copy(ct[len(Rb):], em)
  229. copy(ct[len(Rb)+len(em):], d)
  230. return ct, nil
  231. }
  232. // Decrypt decrypts an ECIES ciphertext.
  233. func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {
  234. if len(c) == 0 {
  235. return nil, ErrInvalidMessage
  236. }
  237. params, err := pubkeyParams(&prv.PublicKey)
  238. if err != nil {
  239. return nil, err
  240. }
  241. hash := params.Hash()
  242. var (
  243. rLen int
  244. hLen int = hash.Size()
  245. mStart int
  246. mEnd int
  247. )
  248. switch c[0] {
  249. case 2, 3, 4:
  250. rLen = (prv.PublicKey.Curve.Params().BitSize + 7) / 4
  251. if len(c) < (rLen + hLen + 1) {
  252. return nil, ErrInvalidMessage
  253. }
  254. default:
  255. return nil, ErrInvalidPublicKey
  256. }
  257. mStart = rLen
  258. mEnd = len(c) - hLen
  259. R := new(PublicKey)
  260. R.Curve = prv.PublicKey.Curve
  261. R.X, R.Y = elliptic.Unmarshal(R.Curve, c[:rLen])
  262. if R.X == nil {
  263. return nil, ErrInvalidPublicKey
  264. }
  265. z, err := prv.GenerateShared(R, params.KeyLen, params.KeyLen)
  266. if err != nil {
  267. return nil, err
  268. }
  269. Ke, Km := deriveKeys(hash, z, s1, params.KeyLen)
  270. d := messageTag(params.Hash, Km, c[mStart:mEnd], s2)
  271. if subtle.ConstantTimeCompare(c[mEnd:], d) != 1 {
  272. return nil, ErrInvalidMessage
  273. }
  274. return symDecrypt(params, Ke, c[mStart:mEnd])
  275. }