github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/bccsp/sw/new.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 "github.com/hellobchain/newcryptosm/sm2" 14 "github.com/hellobchain/newcryptosm/sm3" 15 "reflect" 16 17 "github.com/hellobchain/third_party/hyperledger/fabric/bccsp" 18 "github.com/pkg/errors" 19 "golang.org/x/crypto/sha3" 20 ) 21 22 // NewDefaultSecurityLevel returns a new instance of the software-based BCCSP 23 // at security level 256, hash family SHA2 and using FolderBasedKeyStore as KeyStore. 24 func NewDefaultSecurityLevel(keyStorePath string) (bccsp.BCCSP, error) { 25 ks := &fileBasedKeyStore{} 26 if err := ks.Init(nil, keyStorePath, false); err != nil { 27 return nil, errors.Wrapf(err, "Failed initializing key store at [%v]", keyStorePath) 28 } 29 30 return NewWithParams(256, "SHA2", ks) 31 } 32 33 // NewDefaultSecurityLevel returns a new instance of the software-based BCCSP 34 // at security level 256, hash family SHA2 and using the passed KeyStore. 35 func NewDefaultSecurityLevelWithKeystore(keyStore bccsp.KeyStore) (bccsp.BCCSP, error) { 36 return NewWithParams(256, "SHA2", keyStore) 37 } 38 39 // NewWithParams returns a new instance of the software-based BCCSP 40 // set at the passed security level, hash family and KeyStore. 41 func NewWithParams(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (bccsp.BCCSP, error) { 42 // Init config 43 conf := &config{} 44 err := conf.setSecurityLevel(securityLevel, hashFamily) 45 if err != nil { 46 return nil, errors.Wrapf(err, "Failed initializing configuration at [%v,%v]", securityLevel, hashFamily) 47 } 48 49 swbccsp, err := New(keyStore) 50 if err != nil { 51 return nil, err 52 } 53 54 // Notice that errors are ignored here because some test will fail if one 55 // of the following call fails. 56 57 // Set the Encryptors 58 swbccsp.AddWrapper(reflect.TypeOf(&symmetryPrivateKey{}), &aescbcpkcs7Encryptor{}) 59 60 // Set the Decryptors 61 swbccsp.AddWrapper(reflect.TypeOf(&symmetryPrivateKey{}), &aescbcpkcs7Decryptor{}) 62 63 // Set the Signers 64 swbccsp.AddWrapper(reflect.TypeOf(&ecdsaPrivateKey{}), &ecdsaSigner{}) 65 66 // Set the Verifiers 67 swbccsp.AddWrapper(reflect.TypeOf(&ecdsaPrivateKey{}), &ecdsaPrivateKeyVerifier{}) 68 swbccsp.AddWrapper(reflect.TypeOf(&ecdsaPublicKey{}), &ecdsaPublicKeyKeyVerifier{}) 69 70 // Set the Hashers 71 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SHAOpts{}), &hasher{hash: conf.hashFunction}) 72 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SHA256Opts{}), &hasher{hash: sha256.New}) 73 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SHA384Opts{}), &hasher{hash: sha512.New384}) 74 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SHA3_256Opts{}), &hasher{hash: sha3.New256}) 75 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SHA3_384Opts{}), &hasher{hash: sha3.New384}) 76 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SM3Opts{}), &hasher{hash: sm3.New}) 77 78 // Set the key generators 79 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.ECDSAKeyGenOpts{}), &ecdsaKeyGenerator{curve: conf.ellipticCurve}) 80 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.ECDSAP256KeyGenOpts{}), &ecdsaKeyGenerator{curve: elliptic.P256()}) 81 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.ECDSAP384KeyGenOpts{}), &ecdsaKeyGenerator{curve: elliptic.P384()}) 82 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SM2KeyGenOpts{}), &sm2KeyGenerator{curve: sm2.SM2()}) 83 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.AESKeyGenOpts{}), &symmetryKeyGenerator{length: conf.aesBitLength}) 84 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.AES256KeyGenOpts{}), &symmetryKeyGenerator{length: 32}) 85 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.AES192KeyGenOpts{}), &symmetryKeyGenerator{length: 24}) 86 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.AES128KeyGenOpts{}), &symmetryKeyGenerator{length: 16}) 87 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SM4KeyGenOpts{}), &symmetryKeyGenerator{length: conf.sm4BitLength}) 88 89 // Set the key deriver 90 swbccsp.AddWrapper(reflect.TypeOf(&ecdsaPrivateKey{}), &ecdsaPrivateKeyKeyDeriver{}) 91 swbccsp.AddWrapper(reflect.TypeOf(&ecdsaPublicKey{}), &ecdsaPublicKeyKeyDeriver{}) 92 swbccsp.AddWrapper(reflect.TypeOf(&symmetryPrivateKey{}), &symmetryPrivateKeyKeyDeriver{conf: conf}) 93 94 // Set the key importers 95 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.AES256ImportKeyOpts{}), &aes256ImportKeyOptsKeyImporter{}) 96 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.SM4ImportKeyOpts{}), &sm4ImportKeyOptsKeyImporter{}) 97 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.HMACImportKeyOpts{}), &hmacImportKeyOptsKeyImporter{}) 98 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.ECDSAPKIXPublicKeyImportOpts{}), &ecdsaPKIXPublicKeyImportOptsKeyImporter{}) 99 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.ECDSAPrivateKeyImportOpts{}), &ecdsaPrivateKeyImportOptsKeyImporter{}) 100 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.ECDSAGoPublicKeyImportOpts{}), &ecdsaGoPublicKeyImportOptsKeyImporter{}) 101 swbccsp.AddWrapper(reflect.TypeOf(&bccsp.X509PublicKeyImportOpts{}), &x509PublicKeyImportOptsKeyImporter{bccsp: swbccsp}) 102 103 return swbccsp, nil 104 }