params.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // This file contains parameters for ECIES encryption, specifying the
  31. // symmetric encryption and HMAC parameters.
  32. import (
  33. "crypto"
  34. "crypto/aes"
  35. "crypto/cipher"
  36. "crypto/elliptic"
  37. "crypto/sha256"
  38. "crypto/sha512"
  39. "fmt"
  40. "hash"
  41. ethcrypto "github.com/ethereum/go-ethereum/crypto"
  42. )
  43. var (
  44. DefaultCurve = ethcrypto.S256()
  45. ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm")
  46. ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
  47. ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen)
  48. )
  49. // KeyLen is limited to prevent overflow of the counter
  50. // in concatKDF. While the theoretical limit is much higher,
  51. // no known cipher uses keys larger than 512 bytes.
  52. const maxKeyLen = 512
  53. type ECIESParams struct {
  54. Hash func() hash.Hash // hash function
  55. hashAlgo crypto.Hash
  56. Cipher func([]byte) (cipher.Block, error) // symmetric cipher
  57. BlockSize int // block size of symmetric cipher
  58. KeyLen int // length of symmetric key
  59. }
  60. // Standard ECIES parameters:
  61. // * ECIES using AES128 and HMAC-SHA-256-16
  62. // * ECIES using AES256 and HMAC-SHA-256-32
  63. // * ECIES using AES256 and HMAC-SHA-384-48
  64. // * ECIES using AES256 and HMAC-SHA-512-64
  65. var (
  66. ECIES_AES128_SHA256 = &ECIESParams{
  67. Hash: sha256.New,
  68. hashAlgo: crypto.SHA256,
  69. Cipher: aes.NewCipher,
  70. BlockSize: aes.BlockSize,
  71. KeyLen: 16,
  72. }
  73. ECIES_AES256_SHA256 = &ECIESParams{
  74. Hash: sha256.New,
  75. hashAlgo: crypto.SHA256,
  76. Cipher: aes.NewCipher,
  77. BlockSize: aes.BlockSize,
  78. KeyLen: 32,
  79. }
  80. ECIES_AES256_SHA384 = &ECIESParams{
  81. Hash: sha512.New384,
  82. hashAlgo: crypto.SHA384,
  83. Cipher: aes.NewCipher,
  84. BlockSize: aes.BlockSize,
  85. KeyLen: 32,
  86. }
  87. ECIES_AES256_SHA512 = &ECIESParams{
  88. Hash: sha512.New,
  89. hashAlgo: crypto.SHA512,
  90. Cipher: aes.NewCipher,
  91. BlockSize: aes.BlockSize,
  92. KeyLen: 32,
  93. }
  94. )
  95. var paramsFromCurve = map[elliptic.Curve]*ECIESParams{
  96. ethcrypto.S256(): ECIES_AES128_SHA256,
  97. elliptic.P256(): ECIES_AES128_SHA256,
  98. elliptic.P384(): ECIES_AES256_SHA384,
  99. elliptic.P521(): ECIES_AES256_SHA512,
  100. }
  101. func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) {
  102. paramsFromCurve[curve] = params
  103. }
  104. // ParamsFromCurve selects parameters optimal for the selected elliptic curve.
  105. // Only the curves P256, P384, and P512 are supported.
  106. func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) {
  107. return paramsFromCurve[curve]
  108. }
  109. func pubkeyParams(key *PublicKey) (*ECIESParams, error) {
  110. params := key.Params
  111. if params == nil {
  112. if params = ParamsFromCurve(key.Curve); params == nil {
  113. return nil, ErrUnsupportedECIESParameters
  114. }
  115. }
  116. if params.KeyLen > maxKeyLen {
  117. return nil, ErrInvalidKeyLen
  118. }
  119. return params, nil
  120. }