crypto

package
v1.68.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2023 License: BSD-3-Clause Imports: 12 Imported by: 195

README

crypto

功能

  • 支持 ed25519, secp256k1, sm2
  • 统一的 PrivKey,Pubkey, Signature 接口, 详见 crypto.go

依赖

  • sm2 编译依赖 gmssl 2.0 版本
  • 安装:bash ./deps/install_gmssl.sh

配置

支持加密插件使能开启, 分叉高度等配置

EnableTypes

指定开启若干加密插件(插件名称),不配置默认启用所有

[crypto] #示例
enableTypes=["secp256k1", "sm2"]
EnableHeight

分叉高度配置, 即区块高度达到配置高度后启用插件, 不配置采用内置的启用高度, 负数表示不启用

[crypto] #示例
[crypto.enableHeight]
"secp256k1" = 0
"sm2"= 100

cryptoID

加密插件ID值, int32类型

手动指定

注册插件时, 支持手动指定插件ID(crypto.WithRegOptionTypeID(manualID))

  • 手动指定ID范围, (0, 4096), 低位12位
  • 建议单元测试调用CryptoGetCryptoList接口查看已注册ID情况.
自动生成

注册时未指定手动ID, 将根据插件名称随机生成, 存在冲突时需要手动指定解决

相关说明

cryptoID主要用于交易签名算法类型判定, cryptoID不等同于交易SignatureID

  • 接口chain33/types/ExtractCryptoID, 基于signID解析cryptoID
  • 底层设计, 参考chain33/types/sign.md

Documentation

Overview

Package crypto 加解密、签名接口定义

Index

Constants

View Source
const (
	// MaxManualTypeID 手动指定ID最大值 4095
	MaxManualTypeID = 1<<12 - 1
)

Variables

View Source
var (
	//ErrNotSupportAggr 不支持聚合签名
	ErrNotSupportAggr = errors.New("AggregateCrypto not support")
	//ErrSign 签名错误
	ErrSign = errors.New("error signature")
	//ErrUnknownDriver 未注册加密插件
	ErrUnknownDriver = errors.New("ErrUnknownDriver")
	//ErrDriverNotEnable 加密插件未开启
	ErrDriverNotEnable = errors.New("ErrUnknownDriver")
)

Functions

func BasicValidation added in v1.65.1

func BasicValidation(c Crypto, msg, pub, sig []byte) error

BasicValidation 公私钥数据签名验证基础实现

func CRandBytes

func CRandBytes(numBytes int) []byte

CRandBytes This uses the OS and the Seed(s).

func CRandHex

func CRandHex(numDigits int) string

CRandHex RandHex(24) gives 96 bits of randomness, strong enough for most purposes.

func CReader

func CReader() io.Reader

CReader Returns a crand.Reader mixed with user-supplied entropy

func GenDriverTypeID added in v1.65.1

func GenDriverTypeID(name string) int32

GenDriverTypeID 根据名称生成driver type id

func GetCryptoList added in v1.65.3

func GetCryptoList() ([]string, []int32)

GetCryptoList 获取加密插件列表,名称和对应的类型值

func GetName

func GetName(ty int) string

GetName 获取name

func GetType

func GetType(name string) int

GetType 获取type

func Init added in v1.65.1

func Init(cfg *Config, subCfg map[string][]byte)

Init init crypto

func MixEntropy

func MixEntropy(seedBytes []byte)

MixEntropy Mix additional bytes of randomness, e.g. from hardware, user-input, etc. It is OK to call it multiple times. It does not diminish security.

func Register

func Register(name string, crypto Crypto, options ...RegOption)

Register 注册加密算法,支持选项,设置typeID相关参数

func Ripemd160

func Ripemd160(bytes []byte) []byte

Ripemd160 加密算法

func Sha256

func Sha256(bytes []byte) []byte

Sha256 加密算法

func Sm3Hash

func Sm3Hash(msg []byte) []byte

Sm3Hash 加密算法

Types

type AggregateCrypto added in v1.65.0

type AggregateCrypto interface {
	Aggregate(sigs []Signature) (Signature, error)
	AggregatePublic(pubs []PubKey) (PubKey, error)
	VerifyAggregatedOne(pubs []PubKey, m []byte, sig Signature) error
	VerifyAggregatedN(pubs []PubKey, ms [][]byte, sig Signature) error
}

AggregateCrypto 聚合签名

func ToAggregate added in v1.65.0

func ToAggregate(c Crypto) (AggregateCrypto, error)

ToAggregate 判断签名是否可以支持聚合签名,并且返回聚合签名的接口

type CertSignature

type CertSignature struct {
	Signature []byte
	Cert      []byte
}

CertSignature 签名

type Config added in v1.65.1

type Config struct {
	//支持只指定若干加密类型,不配置默认启用所有的加密插件, 如 types=["secp256k1", "sm2"]
	EnableTypes []string `json:"enableTypes,omitempty"`
	//支持对EnableTypes的每个加密插件分别设置启用高度, 不配置采用内置的启用高度
	// [crypto.enableHeight]
	// secp256k1=0
	EnableHeight map[string]int64 `json:"enableHeight,omitempty"`
}

Config crypto模块配置

type Crypto

type Crypto interface {
	GenKey() (PrivKey, error)
	SignatureFromBytes([]byte) (Signature, error)
	PrivKeyFromBytes([]byte) (PrivKey, error)
	PubKeyFromBytes([]byte) (PubKey, error)
	Validate(msg, pub, sig []byte) error
}

Crypto 加密

func Load added in v1.66.1

func Load(name string, blockHeight int64, opts ...LoadOption) (Crypto, error)

Load 加载加密插件, 内部会检测在指定区块高度是否使能

不考虑使能情况, 只做插件加载, blockHeight传负值, 如-1

type Driver added in v1.65.1

type Driver struct {
	// contains filtered or unexported fields
}

Driver 加密插件及相关信息

type DriverInitFunc added in v1.65.1

type DriverInitFunc func(jsonCfg []byte)

DriverInitFunc 插件初始化接口,参数是序列化的json数据,需要unmarshal为自定义的结构

type LoadOption added in v1.66.1

type LoadOption func(*Driver) error

LoadOption load crypto可选参数

func WithLoadOptionEnableCheck added in v1.66.1

func WithLoadOptionEnableCheck(blockHeight int64) LoadOption

WithLoadOptionEnableCheck 在New阶段根据当前区块高度进行插件使能检测

type PrivKey

type PrivKey interface {
	Bytes() []byte
	Sign(msg []byte) Signature
	PubKey() PubKey
	Equals(PrivKey) bool
}

PrivKey 私钥

type PubKey

type PubKey interface {
	Bytes() []byte
	KeyString() string
	VerifyBytes(msg []byte, sig Signature) bool
	Equals(PubKey) bool
}

PubKey 公钥

type RegOption added in v1.65.3

type RegOption func(*Driver) error

RegOption Register Driver可选参数,设置相关参数默认值

func WithRegOptionCGO added in v1.65.3

func WithRegOptionCGO() RegOption

WithRegOptionCGO 设置为CGO版本

func WithRegOptionDefaultDisable added in v1.65.3

func WithRegOptionDefaultDisable() RegOption

WithRegOptionDefaultDisable 设置默认不启用

func WithRegOptionInitFunc added in v1.65.3

func WithRegOptionInitFunc(fn DriverInitFunc) RegOption

WithRegOptionInitFunc 设置插件初始化接口

func WithRegOptionTypeID added in v1.65.3

func WithRegOptionTypeID(id int32) RegOption

WithRegOptionTypeID 手动指定typeID, 不指定情况,系统将根据name自动生成typeID

type Signature

type Signature interface {
	Bytes() []byte
	IsZero() bool
	String() string
	Equals(Signature) bool
}

Signature 签名

Directories

Path Synopsis
Package client 实现系统消息队列事件处理,新增client包避免循环引用
Package client 实现系统消息队列事件处理,新增client包避免循环引用
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL