github.com/klaytn/klaytn@v1.12.1/crypto/ecies/params.go (about)

     1  // Copyright (c) 2018 The klaytn Authors.
     2  // Copyright (c) 2013 Kyle Isom <kyle@tyrfingr.is>
     3  // Copyright (c) 2012 The Go Authors. All rights reserved.
     4  //
     5  // Redistribution and use in source and binary forms, with or without
     6  // modification, are permitted provided that the following conditions are
     7  // met:
     8  //
     9  //    * Redistributions of source code must retain the above copyright
    10  // notice, this list of conditions and the following disclaimer.
    11  //    * Redistributions in binary form must reproduce the above
    12  // copyright notice, this list of conditions and the following disclaimer
    13  // in the documentation and/or other materials provided with the
    14  // distribution.
    15  //    * Neither the name of Google Inc. nor the names of its
    16  // contributors may be used to endorse or promote products derived from
    17  // this software without specific prior written permission.
    18  //
    19  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    20  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    21  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    22  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    23  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    24  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    25  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    26  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    27  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    28  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    29  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  //
    31  // This file is derived from crypto/ecies/params.go (2018/06/04).
    32  // Modified and improved for the klaytn development.
    33  
    34  package ecies
    35  
    36  // This file contains parameters for ECIES encryption, specifying the
    37  // symmetric encryption and HMAC parameters.
    38  
    39  import (
    40  	"crypto"
    41  	"crypto/aes"
    42  	"crypto/cipher"
    43  	"crypto/elliptic"
    44  	"crypto/sha256"
    45  	"crypto/sha512"
    46  	"fmt"
    47  	"hash"
    48  
    49  	ethcrypto "github.com/klaytn/klaytn/crypto"
    50  )
    51  
    52  var (
    53  	DefaultCurve                  = ethcrypto.S256()
    54  	ErrUnsupportedECDHAlgorithm   = fmt.Errorf("ecies: unsupported ECDH algorithm")
    55  	ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
    56  )
    57  
    58  type ECIESParams struct {
    59  	Hash      func() hash.Hash // hash function
    60  	hashAlgo  crypto.Hash
    61  	Cipher    func([]byte) (cipher.Block, error) // symmetric cipher
    62  	BlockSize int                                // block size of symmetric cipher
    63  	KeyLen    int                                // length of symmetric key
    64  }
    65  
    66  // Standard ECIES parameters:
    67  // * ECIES using AES128 and HMAC-SHA-256-16
    68  // * ECIES using AES256 and HMAC-SHA-256-32
    69  // * ECIES using AES256 and HMAC-SHA-384-48
    70  // * ECIES using AES256 and HMAC-SHA-512-64
    71  
    72  var (
    73  	ECIES_AES128_SHA256 = &ECIESParams{
    74  		Hash:      sha256.New,
    75  		hashAlgo:  crypto.SHA256,
    76  		Cipher:    aes.NewCipher,
    77  		BlockSize: aes.BlockSize,
    78  		KeyLen:    16,
    79  	}
    80  
    81  	ECIES_AES256_SHA256 = &ECIESParams{
    82  		Hash:      sha256.New,
    83  		hashAlgo:  crypto.SHA256,
    84  		Cipher:    aes.NewCipher,
    85  		BlockSize: aes.BlockSize,
    86  		KeyLen:    32,
    87  	}
    88  
    89  	ECIES_AES256_SHA384 = &ECIESParams{
    90  		Hash:      sha512.New384,
    91  		hashAlgo:  crypto.SHA384,
    92  		Cipher:    aes.NewCipher,
    93  		BlockSize: aes.BlockSize,
    94  		KeyLen:    32,
    95  	}
    96  
    97  	ECIES_AES256_SHA512 = &ECIESParams{
    98  		Hash:      sha512.New,
    99  		hashAlgo:  crypto.SHA512,
   100  		Cipher:    aes.NewCipher,
   101  		BlockSize: aes.BlockSize,
   102  		KeyLen:    32,
   103  	}
   104  )
   105  
   106  var paramsFromCurve = map[elliptic.Curve]*ECIESParams{
   107  	ethcrypto.S256(): ECIES_AES128_SHA256,
   108  	elliptic.P256():  ECIES_AES128_SHA256,
   109  	elliptic.P384():  ECIES_AES256_SHA384,
   110  	elliptic.P521():  ECIES_AES256_SHA512,
   111  }
   112  
   113  func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) {
   114  	paramsFromCurve[curve] = params
   115  }
   116  
   117  // ParamsFromCurve selects parameters optimal for the selected elliptic curve.
   118  // Only the curves P256, P384, and P512 are supported.
   119  func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) {
   120  	return paramsFromCurve[curve]
   121  }