github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/ecies/params.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package ecies
    19  
    20  // This file contains parameters for ECIES encryption, specifying the
    21  // symmetric encryption and HMAC parameters.
    22  
    23  import (
    24  	"crypto"
    25  	"crypto/aes"
    26  	"crypto/cipher"
    27  	"crypto/elliptic"
    28  	"crypto/sha256"
    29  	"crypto/sha512"
    30  	"fmt"
    31  	"hash"
    32  
    33  	ethcrypto "github.com/AigarNetwork/aigar/crypto"
    34  )
    35  
    36  var (
    37  	DefaultCurve                  = ethcrypto.S256()
    38  	ErrUnsupportedECDHAlgorithm   = fmt.Errorf("ecies: unsupported ECDH algorithm")
    39  	ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
    40  )
    41  
    42  type ECIESParams struct {
    43  	Hash      func() hash.Hash // hash function
    44  	hashAlgo  crypto.Hash
    45  	Cipher    func([]byte) (cipher.Block, error) // symmetric cipher
    46  	BlockSize int                                // block size of symmetric cipher
    47  	KeyLen    int                                // length of symmetric key
    48  }
    49  
    50  // Standard ECIES parameters:
    51  // * ECIES using AES128 and HMAC-SHA-256-16
    52  // * ECIES using AES256 and HMAC-SHA-256-32
    53  // * ECIES using AES256 and HMAC-SHA-384-48
    54  // * ECIES using AES256 and HMAC-SHA-512-64
    55  
    56  var (
    57  	ECIES_AES128_SHA256 = &ECIESParams{
    58  		Hash:      sha256.New,
    59  		hashAlgo:  crypto.SHA256,
    60  		Cipher:    aes.NewCipher,
    61  		BlockSize: aes.BlockSize,
    62  		KeyLen:    16,
    63  	}
    64  
    65  	ECIES_AES256_SHA256 = &ECIESParams{
    66  		Hash:      sha256.New,
    67  		hashAlgo:  crypto.SHA256,
    68  		Cipher:    aes.NewCipher,
    69  		BlockSize: aes.BlockSize,
    70  		KeyLen:    32,
    71  	}
    72  
    73  	ECIES_AES256_SHA384 = &ECIESParams{
    74  		Hash:      sha512.New384,
    75  		hashAlgo:  crypto.SHA384,
    76  		Cipher:    aes.NewCipher,
    77  		BlockSize: aes.BlockSize,
    78  		KeyLen:    32,
    79  	}
    80  
    81  	ECIES_AES256_SHA512 = &ECIESParams{
    82  		Hash:      sha512.New,
    83  		hashAlgo:  crypto.SHA512,
    84  		Cipher:    aes.NewCipher,
    85  		BlockSize: aes.BlockSize,
    86  		KeyLen:    32,
    87  	}
    88  )
    89  
    90  var paramsFromCurve = map[elliptic.Curve]*ECIESParams{
    91  	ethcrypto.S256(): ECIES_AES128_SHA256,
    92  	elliptic.P256():  ECIES_AES128_SHA256,
    93  	elliptic.P384():  ECIES_AES256_SHA384,
    94  	elliptic.P521():  ECIES_AES256_SHA512,
    95  }
    96  
    97  func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) {
    98  	paramsFromCurve[curve] = params
    99  }
   100  
   101  // ParamsFromCurve selects parameters optimal for the selected elliptic curve.
   102  // Only the curves P256, P384, and P512 are supported.
   103  func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) {
   104  	return paramsFromCurve[curve]
   105  }