github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bccsp/sw/conf.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package sw
    17  
    18  import (
    19  	"crypto/elliptic"
    20  	"crypto/sha256"
    21  	"crypto/sha512"
    22  	"fmt"
    23  	"hash"
    24  
    25  	"golang.org/x/crypto/sha3"
    26  )
    27  
    28  type config struct {
    29  	keyStorePath  string
    30  	securityLevel int
    31  	hashFamily    string
    32  
    33  	ellipticCurve elliptic.Curve
    34  	hashFunction  func() hash.Hash
    35  	aesBitLength  int
    36  	rsaBitLength  int
    37  }
    38  
    39  func (conf *config) setSecurityLevel(securityLevel int, hashFamily string) (err error) {
    40  	switch hashFamily {
    41  	case "SHA2":
    42  		err = conf.setSecurityLevelSHA2(securityLevel)
    43  	case "SHA3":
    44  		err = conf.setSecurityLevelSHA3(securityLevel)
    45  	default:
    46  		err = fmt.Errorf("Hash Family not supported [%s]", hashFamily)
    47  	}
    48  	return
    49  }
    50  
    51  func (conf *config) setSecurityLevelSHA2(level int) (err error) {
    52  	switch level {
    53  	case 256:
    54  		conf.ellipticCurve = elliptic.P256()
    55  		conf.hashFunction = sha256.New
    56  		conf.rsaBitLength = 2048
    57  		conf.aesBitLength = 32
    58  	case 384:
    59  		conf.ellipticCurve = elliptic.P384()
    60  		conf.hashFunction = sha512.New384
    61  		conf.rsaBitLength = 3072
    62  		conf.aesBitLength = 32
    63  	default:
    64  		err = fmt.Errorf("Security level not supported [%d]", level)
    65  	}
    66  	return
    67  }
    68  
    69  func (conf *config) setSecurityLevelSHA3(level int) (err error) {
    70  	switch level {
    71  	case 256:
    72  		conf.ellipticCurve = elliptic.P256()
    73  		conf.hashFunction = sha3.New256
    74  		conf.rsaBitLength = 2048
    75  		conf.aesBitLength = 32
    76  	case 384:
    77  		conf.ellipticCurve = elliptic.P384()
    78  		conf.hashFunction = sha3.New384
    79  		conf.rsaBitLength = 3072
    80  		conf.aesBitLength = 32
    81  	default:
    82  		err = fmt.Errorf("Security level not supported [%d]", level)
    83  	}
    84  	return
    85  }