Fabric系列 - HSM之1 介绍

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_keyr_cert为客户端颁发证书c_cert与私钥c_key,外部可获取c_certc_key

(2)用于分层架构的Fabric-CA上

架构如下图:

  1. 可在 Fabric-CA Root Server 上用HSM, 用以保护 Root CA的私钥

    要求 HSM 具备的能力:

    • 产生/导入 一个根私钥 r_key
    • 安全保管私钥 r_key
    • r_key制作/导入 自签名的X509根证书r_cert
    • r_keyr_cert为 Intermediate CA颁发证书i_cert与私钥i_key,外部可获取
  2. 或者在 Fabric-CA Intermediate Server 上用HSM, 用以保护 Root CA的私钥

    要求 HSM 具备的能力:

    • 导入 一个Intermediate私钥 i_key 和证书i_cert
    • 安全保管私钥 i_key
    • i_keyi_cert为客户端颁发证书c_cert与私钥c_key,外部可获取c_certc_key

国密相关资料

国密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的具体要求如下:

  1. 具备key pair 生成能力: (pri_key, pub_key)
  2. 底层支持:
    1. FIPS ECC curve: secp256k1, secp256r1, secp384r1, SHA256, SHA512.
    2. 国密

参考:

https://blog.csdn.net/weixin_39606244/article/details/112961507

https://zhuanlan.zhihu.com/p/369012651


: 区块链知识系列
: 密码学系列
: 零知识证明系列
: 共识系列
: 公链调研系列
: BTC系列
: 以太坊系列
: EOS系列
: Filecoin系列
: 联盟链系列
: Fabric系列
: 智能合约系列
: Token系列

相关推荐
Kim Jackson8 天前
我的世界Java版1.21.4的Fabric模组开发教程(二十二)创建生物(上)实体外观与动画设计
java·开发语言·fabric
宝桥南山8 天前
Microsoft Fabric - 试一下在Blazor应用中使用 GraphQL API去连接Lakehouse
microsoft·c#·asp.net·.netcore·fabric·db
Kim Jackson12 天前
我的世界Java版1.21.4的Fabric模组开发教程(二十三)创建生物(下)实体在游戏中的实现(1)
java·游戏·fabric
China_Yanhy12 天前
运维日记 - 猛男的AI拓荒录:Fabric (GitHub: danielmiessler/fabric) —— 让 AI 回归 Unix 哲学的终端神器
运维·人工智能·fabric
寒寒_1 个月前
使用Vue与Fabric.js创建图片标注工具
javascript·vue.js·fabric
Monkey-旭1 个月前
hutool库全面使用指南
android·java·算法·加解密·国密·sm4
y1233447788992 个月前
国密算法SM2实现(Openssl)
开发语言·openssl·国密
CC码码2 个月前
基于WebGPU实现canvas高级滤镜
前端·javascript·webgl·fabric
No Silver Bullet2 个月前
电子版授权书CA加签的原理与作用
ca