github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/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  	sm4BitLength  int
    24  }
    25  
    26  func (conf *config) setSecurityLevel(securityLevel int, hashFamily string) (err error) {
    27  	switch hashFamily {
    28  	case "SHA2":
    29  		err = conf.setSecurityLevelSHA2(securityLevel)
    30  	case "SHA3":
    31  		err = conf.setSecurityLevelSHA3(securityLevel)
    32  	default:
    33  		err = fmt.Errorf("Hash Family not supported [%s]", hashFamily)
    34  	}
    35  	return
    36  }
    37  
    38  func (conf *config) setSecurityLevelSHA2(level int) (err error) {
    39  	switch level {
    40  	case 256:
    41  		conf.ellipticCurve = elliptic.P256()
    42  		conf.hashFunction = sha256.New
    43  		conf.aesBitLength = 32
    44  		conf.sm4BitLength = 32
    45  	case 384:
    46  		conf.ellipticCurve = elliptic.P384()
    47  		conf.hashFunction = sha512.New384
    48  		conf.aesBitLength = 32
    49  		conf.sm4BitLength = 32
    50  	default:
    51  		err = fmt.Errorf("Security level not supported [%d]", level)
    52  	}
    53  	return
    54  }
    55  
    56  func (conf *config) setSecurityLevelSHA3(level int) (err error) {
    57  	switch level {
    58  	case 256:
    59  		conf.ellipticCurve = elliptic.P256()
    60  		conf.hashFunction = sha3.New256
    61  		conf.aesBitLength = 32
    62  		conf.sm4BitLength = 32
    63  	case 384:
    64  		conf.ellipticCurve = elliptic.P384()
    65  		conf.hashFunction = sha3.New384
    66  		conf.aesBitLength = 32
    67  		conf.sm4BitLength = 32
    68  	default:
    69  		err = fmt.Errorf("security level not supported [%d]", level)
    70  	}
    71  	return
    72  }