github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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  	ellipticCurve elliptic.Curve
    30  	hashFunction  func() hash.Hash
    31  	aesBitLength  int
    32  	rsaBitLength  int
    33  }
    34  
    35  func (conf *config) setSecurityLevel(securityLevel int, hashFamily string) (err error) {
    36  	switch hashFamily {
    37  	case "SHA2":
    38  		err = conf.setSecurityLevelSHA2(securityLevel)
    39  	case "SHA3":
    40  		err = conf.setSecurityLevelSHA3(securityLevel)
    41  	default:
    42  		err = fmt.Errorf("Hash Family not supported [%s]", hashFamily)
    43  	}
    44  	return
    45  }
    46  
    47  func (conf *config) setSecurityLevelSHA2(level int) (err error) {
    48  	switch level {
    49  	case 256:
    50  		conf.ellipticCurve = elliptic.P256()
    51  		conf.hashFunction = sha256.New
    52  		conf.rsaBitLength = 2048
    53  		conf.aesBitLength = 32
    54  	case 384:
    55  		conf.ellipticCurve = elliptic.P384()
    56  		conf.hashFunction = sha512.New384
    57  		conf.rsaBitLength = 3072
    58  		conf.aesBitLength = 32
    59  	default:
    60  		err = fmt.Errorf("Security level not supported [%d]", level)
    61  	}
    62  	return
    63  }
    64  
    65  func (conf *config) setSecurityLevelSHA3(level int) (err error) {
    66  	switch level {
    67  	case 256:
    68  		conf.ellipticCurve = elliptic.P256()
    69  		conf.hashFunction = sha3.New256
    70  		conf.rsaBitLength = 2048
    71  		conf.aesBitLength = 32
    72  	case 384:
    73  		conf.ellipticCurve = elliptic.P384()
    74  		conf.hashFunction = sha3.New384
    75  		conf.rsaBitLength = 3072
    76  		conf.aesBitLength = 32
    77  	default:
    78  		err = fmt.Errorf("Security level not supported [%d]", level)
    79  	}
    80  	return
    81  }