1.FabricV2.2具体使用的密码学算法
虽然FabricV2.2中支持的许多密码学算法,但是实际使用时默认只是分别对对称加密算法、非对称加密算法以及哈希算法的这三种加密算法大类中各自挑选几个小类算法
首先Fabric中的代码采用的是factory设计模式来产生具体的BCCSP套件
1.1.研究BCCSP中Factory文件夹
由于在编译Fabric过程中默认会ignored pkcs11.go和pkcs11factory.go by the build tool because of the custom flag,所以无需考虑 pkcs11.go和pkcs11factory.go两个包
以上主要有一核心两分支:
1核心是指factory.go;2分支是指pkcs11factory.go和swfactory.go;
nopkcs11.go则是不采用pkcs11硬件实现BCCSP的配置.
opts.go表明哪些配置信息可以决定初始化pkcs11factory还是swfactory
由于我们核心在于bccsp软件实现的密码套件,那么我们要重点研究factory.go、swfactory.go、nopkcs11.go、opts.go文件来调研Fabric默认的加密算法的种类
fabric/bccsp/factory/factory.go
如图所示Get方法根据FactoryOpts参数决定BCCSPFactory实际的工厂类型(PKCS11Factory或SWFactory)
GetDefault方法是采用单例设计模型获取defaultBCCSP
fabric/bccsp/factory/opts.go中
问题:如何初始化defaultBCCSP实例呢?
上图所述:如果config==nil时defaultBCCSP实例中哈希散列算法采用的SHA2-256
当BCCSPFactory指定为swfactory.go时,那么Get方法的解析如下:
fabric/bccsp/factory/swfactory.go
// SwOpts contains options for the SWFactory
type SwOpts struct {
// Default algorithms when not specified (Deprecated?)
SecLevel int `mapstructure:"security" json:"security" yaml:"Security"`
HashFamily string `mapstructure:"hash" json:"hash" yaml:"Hash"`
FileKeystore *FileKeystoreOpts `mapstructure:"filekeystore,omitempty" json:"filekeystore,omitempty" yaml:"FileKeyStore"`
DummyKeystore *DummyKeystoreOpts `mapstructure:"dummykeystore,omitempty" json:"dummykeystore,omitempty"`
InmemKeystore *InmemKeystoreOpts `mapstructure:"inmemkeystore,omitempty" json:"inmemkeystore,omitempty"`
}
默认:SecLevel=256,HashFamily=“SHA2”,FileKeystore记录的是key文件路径,其他字段暂时无需研究
//Get函数是根据FactoryOpts参数生成具体的BCCSP加密服务套件
// Get returns an instance of BCCSP using Opts.
func (f *SWFactory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
// Validate arguments
if config == nil || config.SwOpts == nil {
return nil, errors.New("Invalid config. It must not be nil.")
}
swOpts := config.SwOpts
var ks bccsp.KeyStore
switch {
case swOpts.FileKeystore != nil:
fks, err := sw.NewFileBasedKeyStore(nil, swOpts.FileKeystore.KeyStorePath, false)
if err != nil {
return nil, errors.Wrapf(err, "Failed to initialize software key store")
}
ks = fks
case swOpts.InmemKeystore != nil:
ks = sw.NewInMemoryKeyStore()
default:
// Default to ephemeral key store
ks = sw.NewDummyKeyStore()
}
return sw.NewWithParams(swOpts.SecLevel, swOpts.HashFamily, ks)//重点核心
}
所以实际上SWFactory实例化BCCSP套件的核心方法应该是NewWithParams(),那么NewWithParams具体做了什么呢?
上图所示Fabric中AES、ECDSA以及SHA的具体种类是由fabric/bccsp/sw/conf.go文件中setSecurityLevel方法设置的
4.2.结论:
以下都是基于SW实现的BCCSP套件
- Fabric的BCCSP套件是可以动态选择的但是有默认的配置 SecurityLevel=256,HashFamily=SHA2
- BCCSP套件根据SecurityLevel和HashFamily两个参数选择AES、SHA、ECDSA的具体类别:
- 当HashFamily=SHA2
- SecurityLevel=256时,SHA=SHA2-256(实现是由crypto/sha256标准包sha256.New实现),ECDSA的椭圆曲线是P256(实现为crypto/elliptic标准库的elliptic.P256()),AES的长度选择为32 (默认配置)
- SecurityLevel=384时,SHA=SHA2-512(实现是由crypto/sha512标准包sha512.New384实现),ECDSA的椭圆曲线是P384(实现为crypto/elliptic标准库的elliptic.P384()),AES的长度选择为32
- 当HashFamily=SHA3
- SecurityLevel=256时,SHA=SHA3-256(实现是由第三方包golang.org/x/crypto/sha3中sha3.New256),ECDSA的椭圆曲线是P256(实现crypto/elliptic标准库的elliptic.P256()),AES的长度选择为32
- SecurityLevel=384时,SHA=SHA3-384(实现是由第三方包golang.org/x/crypto/sha3中sha3.New384),ECDSA的椭圆曲线是P384(实现为crypto/elliptic标准库的elliptic.P384()),AES的长度选择为32
- 当HashFamily=SHA2