HSM介绍
Hardware Security Modules
- 签名 / 验签
- 加密 / 解密
- CA generation / verify
- PKCS#11
- SDF
- 支持国密
- 硬件钱包也是一种HSM
Fabric中对HSM的支持
支持PKCS11标准
BCCSP(Blockchain Cryptographic Service Provider)为Fabric其他模块(如MSP组件等)提供密码服务套件,包括加密、解密、签名与验证、哈希函数等方法,如代码清单7-51所示。目前,BCCSP模块提供了PKCS11类型和SW类型两种基本实现。其中,PKCS11(Public-Key Cryptography Standards)类型是基于HSM(Hardware Security Modules)硬件安全模块(https://github.com/miekg/pkcs11)的加密服务实现,属于通用的接口标准,利用pkcs11库提供的上下文对象pkcs11.Ctx在SessionHandle基础上支持密码服务,SW类型是基于软件的加密服务实现的,直接使用crypto库下的包支持密码服务。同时,BCCSP模块可提供对应的两类工厂对象,其他模块则基于这两类工厂对象调用对应的密码服务接口。
docker 部署 Fabric
如果您使用 docker 部署 Fabric,则需要使用以下命令构建自己的映像并启用 PKCS11:
bash
make docker GO_TAGS=pkcs11
如果您使用 docker compose 部署节点,则在构建自己的映像后,您可以更新 docker compose 文件以使用卷在容器内挂载 softhsm 库和配置文件。例如,您可以将以下环境和卷变量添加到 docker compose 文件中:
yaml
environment:
- SOFTHSM2_CONF=/etc/hyperledger/fabric/config.file
volumes:
- /home/softhsm/config.file:/etc/hyperledger/fabric/config.file
- /usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so:/etc/hyperledger/fabric/libsofthsm2.so
对定制化国密友好
将安全与密码服务封装为BCCSP组件,提供生成密钥、消息签名与验签、加密与解密、获取哈希函数等服务功能,具有可插拔组件特性,能够扩展定制的密码安全服务算法(如国密等)
附BCCSP接口的源码
go
// BCCSP is the blockchain cryptographic service provider that offers
// the implementation of cryptographic standards and algorithms.
// BCCSP 是区块链加密服务提供商,提供加密标准和算法的实施。
type BCCSP interface {
// KeyGen generates a key using opts.
KeyGen(opts KeyGenOpts) (k Key, err error)
// KeyDeriv derives a key from k using opts.
// The opts argument should be appropriate for the primitive used.
KeyDeriv(k Key, opts KeyDerivOpts) (dk Key, err error)
// KeyImport imports a key from its raw representation using opts.
// The opts argument should be appropriate for the primitive used.
KeyImport(raw interface{}, opts KeyImportOpts) (k Key, err error)
// GetKey returns the key this CSP associates to
// the Subject Key Identifier ski.
GetKey(ski []byte) (k Key, err error)
// Hash hashes messages msg using options opts.
// If opts is nil, the default hash function will be used.
Hash(msg []byte, opts HashOpts) (hash []byte, err error)
// GetHash returns and instance of hash.Hash using options opts.
// If opts is nil, the default hash function will be returned.
GetHash(opts HashOpts) (h hash.Hash, err error)
// Sign signs digest using key k.
// The opts argument should be appropriate for the algorithm used.
//
// Note that when a signature of a hash of a larger message is needed,
// the caller is responsible for hashing the larger message and passing
// the hash (as digest).
Sign(k Key, digest []byte, opts SignerOpts) (signature []byte, err error)
// Verify verifies signature against key k and digest
// The opts argument should be appropriate for the algorithm used.
Verify(k Key, signature, digest []byte, opts SignerOpts) (valid bool, err error)
// Encrypt encrypts plaintext using key k.
// The opts argument should be appropriate for the algorithm used.
Encrypt(k Key, plaintext []byte, opts EncrypterOpts) (ciphertext []byte, err error)
// Decrypt decrypts ciphertext using key k.
// The opts argument should be appropriate for the algorithm used.
Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) (plaintext []byte, err error)
}
结论:
- Fabric代码中已支持HSM的通用接口标准PKCS11, 接入HSM相对容易
- 由于案例与密码服务已组件化, 定制国密算法也相对容易
Fabric中的应用场景
(0) 加密算法
bash
openssl x509 -text -noout -in cert.pem | grep 'ASN1 OID'
openssl x509 -text -noout -in cert.pem | grep Signature Algorithm
| Algorithm | Fabric's TLS and MSP | Snapcale's TLS |
|---|---|---|
| SHA | SHA256 | SHA256 |
| Public Key | prime256v1 (即 secp256r1) | secp384r1 |
| Signature | ecdsa-with-SHA256 | ecdsa-with-SHA256 |
(1)用于Fabric-CA上
客户端使用Fabric-CA的Flow,如下图:

可在 Fabric-CA Server 上用HSM, 用以保护 Root CA的私钥
要求 HSM 具备的能力:
- 产生/导入 一个根私钥
r_key - 安全保管私钥
r_key - 用
r_key制作/导入 自签名的X509根证书r_cert - 用
r_key和r_cert为客户端颁发证书c_cert与私钥c_key,外部可获取c_cert和c_key
(2)用于分层架构的Fabric-CA上
架构如下图:

-
可在 Fabric-CA Root Server 上用HSM, 用以保护 Root CA的私钥
要求 HSM 具备的能力:
- 产生/导入 一个根私钥
r_key - 安全保管私钥
r_key - 用
r_key制作/导入 自签名的X509根证书r_cert - 用
r_key和r_cert为 Intermediate CA颁发证书i_cert与私钥i_key,外部可获取
- 产生/导入 一个根私钥
-
或者在 Fabric-CA Intermediate Server 上用HSM, 用以保护 Root CA的私钥
要求 HSM 具备的能力:
- 导入 一个Intermediate私钥
i_key和证书i_cert - 安全保管私钥
i_key - 用
i_key和i_cert为客户端颁发证书c_cert与私钥c_key,外部可获取c_cert和c_key
- 导入 一个Intermediate私钥
国密相关资料
国密SM2不能通过签名和消息恢复公钥
原因
在secp256k1我们可以根据签名{r,s}和消息恢复公钥,但是SM2却不能通过签名和消息恢复公钥,因为在SM2的h值计算过程中,SM2用到了公钥的坐标,所以必须知道公钥了,和只有签名和消息recover公钥相矛盾。
消息摘要h的生成过程(h在签名,verify和recover时都是需要的)
mathematica
ZA = SM3 [ENTLA+IDA+a+b+Gx+Gy+Px+Py]
h = SM3 [ZA||MSG]
需要用recover模式验签的区块链的变通方法
因为SM2无法通过签名和消息recover公钥,所以在对交易验证的过程,我们都是取出公钥然后验证。即通过签名者的账户名取出其公钥,再与事务中的签名进行校验
但是,在我们的系统里,一个账号可以拥有几个公钥,所以需要遍历账号的公钥验证交易,导致多公钥账号的交易执行性能会低些。幸运的是,我们统计系统中所有的账号,99%只有一对公私钥,所以理论上不会对系统整体性能造成影响。
[Summary] 对于HSM的具体要求如下:
- 具备key pair 生成能力: (pri_key, pub_key)
- 底层支持:
- FIPS ECC curve: secp256k1, secp256r1, secp384r1, SHA256, SHA512.
- 国密
参考:
https://blog.csdn.net/weixin_39606244/article/details/112961507
: 区块链知识系列
: 密码学系列
: 零知识证明系列
: 共识系列
: 公链调研系列
: BTC系列
: 以太坊系列
: EOS系列
: Filecoin系列
: 联盟链系列
: Fabric系列
: 智能合约系列
: Token系列