github.com/Hnampk/my-fabric@v0.0.0-20201028083322-75069da399c0/bccsp/sw/conf.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package sw
     8  
     9  import (
    10  	"crypto/elliptic"
    11  	"crypto/sha256"
    12  	"crypto/sha512"
    13  	"fmt"
    14  	"hash"
    15  
    16  	"golang.org/x/crypto/sha3"
    17  )
    18  
    19  type config struct {
    20  	ellipticCurve elliptic.Curve
    21  	hashFunction  func() hash.Hash
    22  	aesBitLength  int
    23  }
    24  
    25  func (conf *config) setSecurityLevel(securityLevel int, hashFamily string) (err error) {
    26  	switch hashFamily {
    27  	case "SHA2":
    28  		err = conf.setSecurityLevelSHA2(securityLevel)
    29  	case "SHA3":
    30  		err = conf.setSecurityLevelSHA3(securityLevel)
    31  	default:
    32  		err = fmt.Errorf("Hash Family not supported [%s]", hashFamily)
    33  	}
    34  	return
    35  }
    36  
    37  func (conf *config) setSecurityLevelSHA2(level int) (err error) {
    38  	switch level {
    39  	case 256:
    40  		conf.ellipticCurve = elliptic.P256()
    41  		conf.hashFunction = sha256.New
    42  		conf.aesBitLength = 32
    43  	case 384:
    44  		conf.ellipticCurve = elliptic.P384()
    45  		conf.hashFunction = sha512.New384
    46  		conf.aesBitLength = 32
    47  	default:
    48  		err = fmt.Errorf("Security level not supported [%d]", level)
    49  	}
    50  	return
    51  }
    52  
    53  func (conf *config) setSecurityLevelSHA3(level int) (err error) {
    54  	switch level {
    55  	case 256:
    56  		conf.ellipticCurve = elliptic.P256()
    57  		conf.hashFunction = sha3.New256
    58  		conf.aesBitLength = 32
    59  	case 384:
    60  		conf.ellipticCurve = elliptic.P384()
    61  		conf.hashFunction = sha3.New384
    62  		conf.aesBitLength = 32
    63  	default:
    64  		err = fmt.Errorf("Security level not supported [%d]", level)
    65  	}
    66  	return
    67  }