-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptosuitrimpl.go
91 lines (78 loc) · 2.76 KB
/
cryptosuitrimpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cncc
/**
* @Author: WeiBingtao/13156050650@163.com
* @Version: 1.0
* @Description:
* @Date: 2020/10/9 下午3:04
*/
import (
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp/cncc"
bccspSw "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp/factory/cncc"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite/bccsp/wrapper"
"github.com/pkg/errors"
)
var logger = logging.NewLogger("fabsdk/core")
//GetSuiteByConfig returns cryptosuite adaptor for bccsp loaded according to given config
func GetSuiteByConfig(config core.CryptoSuiteConfig) (core.CryptoSuite, error) {
// TODO: delete this check?
if config.SecurityProvider() != "cncc_gm" {
return nil, errors.Errorf("Unsupported BCCSP Provider: %s", config.SecurityProvider())
}
opts := getOptsByConfig(config)
bccsp, err := getBCCSPFromOpts(opts)
if err != nil {
return nil, err
}
return wrapper.NewCryptoSuite(bccsp), nil
}
//GetSuiteWithDefaultEphemeral returns cryptosuite adaptor for bccsp with default ephemeral options (intended to aid testing)
func GetSuiteWithDefaultEphemeral() (core.CryptoSuite, error) {
opts := getEphemeralOpts()
bccsp, err := getBCCSPFromOpts(opts)
if err != nil {
return nil, err
}
return wrapper.NewCryptoSuite(bccsp), nil
}
func getBCCSPFromOpts(config *cncc.CNCC_GMOpts) (bccsp.BCCSP, error) {
f := &bccspSw.CNCC_GMFactory{}
csp, err := f.Get(config)
if err != nil {
return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name())
}
return csp, nil
}
// GetSuite returns a new instance of the software-based BCCSP
// set at the passed security level, hash family and KeyStore.
//func GetSuite(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (core.CryptoSuite, error) {
// bccsp, err := gm.New(securityLevel, hashFamily, keyStore)
// if err != nil {
// return nil, err
// }
// return wrapper.NewCryptoSuite(bccsp), nil
//}
//GetOptsByConfig Returns Factory opts for given SDK config
func getOptsByConfig(c core.CryptoSuiteConfig) *cncc.CNCC_GMOpts {
opts := &cncc.CNCC_GMOpts{
HashFamily: c.SecurityAlgorithm(),
SecLevel: c.SecurityLevel(),
FileKeystore: &cncc.FileKeystoreOpts{
KeyStorePath: c.KeyStorePath(),
},
//Ephemeral: c.Ephemeral(),
}
logger.Debugf("Initialized CNCC_GM cryptosuite, %v", c.SecurityAlgorithm())
return opts
}
func getEphemeralOpts() *cncc.CNCC_GMOpts {
opts := &cncc.CNCC_GMOpts{
HashFamily: "GMSM3",
SecLevel: 256,
Ephemeral: true,
}
logger.Debug("Initialized ephemeral CNCC_GM cryptosuite with default opts")
return opts
}