crypto

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: Apache-2.0 Imports: 31 Imported by: 4

Documentation

Index

Constants

View Source
const (
	//Aes128CBC = "aes-128-cbc"
	Aes256CBC = "aes-256-cbc"

	Secp256K1 = tmsecp256k1.KeyType

	SymmAlgo = Aes256CBC
	DKLEN    = 32

	AsymmAlgo = Secp256K1
)
View Source
const DefaultValKeyDir = "walkeys/vals"
View Source
const DefaultWalletKeyDir = "walkeys"
View Source
const DefaultWalletKeyDirPerm = 0700

Variables

View Source
var (
	// ErrInvalidBlockSize indicates hash blocksize <= 0.
	ErrInvalidBlockSize = xerrors.NewOrdinary("invalid blocksize")

	// ErrInvalidPKCS5Data indicates bad input to PKCS7 pad or unpad.
	ErrInvalidPKCS5Data = xerrors.NewOrdinary("invalid PKCS5 data (empty or not padded)")
	// ErrInvalidPKCS5Padding indicates PKCS5 unpad fails to bad input.
	ErrInvalidPKCS5Padding = xerrors.NewOrdinary("invalid PKCS5 padding on input")

	// ErrInvalidPKCS7Data indicates bad input to PKCS7 pad or unpad.
	ErrInvalidPKCS7Data = xerrors.NewOrdinary("invalid PKCS7 data (empty or not padded)")
	// ErrInvalidPKCS7Padding indicates PKCS7 unpad fails to bad input.
	ErrInvalidPKCS7Padding = xerrors.NewOrdinary("invalid PKCS7 padding on input")
)

Functions

func CompressPubkey

func CompressPubkey(pub *ecdsa.PublicKey) abytes.HexBytes

func DecompressPubkey

func DecompressPubkey(bz []byte) (*ecdsa.PublicKey, xerrors.XError)

func DefaultHash

func DefaultHash(datas ...[]byte) []byte

func DefaultHasher

func DefaultHasher() hash.Hash

func DefaultHasherName

func DefaultHasherName() string

func ImportPrvKey

func ImportPrvKey(d []byte) (*ecdsa.PrivateKey, error)

func ImportPrvKeyHex

func ImportPrvKeyHex(d string) (*ecdsa.PrivateKey, error)

func NewPrvKey

func NewPrvKey() (*ecdsa.PrivateKey, error)

func PKCS5Padding

func PKCS5Padding(b []byte, blocksize int) ([]byte, error)

func PKCS5UnPadding

func PKCS5UnPadding(b []byte, blocksize int) ([]byte, error)

func PKCS7Padding

func PKCS7Padding(b []byte, blocksize int) ([]byte, error)

pkcs7Padding right-pads the given byte slice with 1 to n bytes, where n is the block size. The size of the result is x times n, where x is at least 1.

func PKCS7UnPadding

func PKCS7UnPadding(b []byte, blocksize int) ([]byte, error)

pkcs7UnPadding validates and unpads data from the given bytes slice. The returned value will be 1 to n bytes smaller depending on the amount of padding, where n is the block size.

func Pub2Addr

func Pub2Addr(pub *ecdsa.PublicKey) types.Address

func PubBytes2Addr

func PubBytes2Addr(pubBytes []byte) (types.Address, xerrors.XError)

pubBytes is 33 bytes compressed format

func Sig2Addr

func Sig2Addr(msg, sig []byte) (types.Address, abytes.HexBytes, xerrors.XError)

func Sign

func Sign(msg []byte, prv *ecdsa.PrivateKey) ([]byte, error)

func VerifySig

func VerifySig(pubkey, msg, sig []byte) bool

Types

type SFilePV

type SFilePV struct {
	Key           SFilePVKey
	LastSignState SFilePVLastSignState
}

SFilePV implements PrivValidator using data persisted to disk to prevent double signing. NOTE: the directories containing pv.Key.filePath and pv.LastSignState.filePath must already exist. It includes the LastSignature and LastSignBytes so we don't lose the signature if the process crashes after signing but before the resulting consensus message is processed.

func GenSFilePV

func GenSFilePV(keyFilePath, stateFilePath string) *SFilePV

GenSFilePV generates a new validator with randomly generated private key and sets the filePaths, but does not call Save().

func LoadOrGenSFilePV

func LoadOrGenSFilePV(keyFilePath, stateFilePath string, s []byte) *SFilePV

LoadOrGenSFilePV loads a SFilePV from the given filePaths or else generates a new one and saves it to the filePaths.

func LoadSFilePV

func LoadSFilePV(keyFilePath, stateFilePath string, s []byte) *SFilePV

LoadSFilePV loads a SFilePV from the filePaths. The SFilePV handles double signing prevention by persisting data to the stateFilePath. If either file path does not exist, the program will exit.

func LoadSFilePVEmptyState

func LoadSFilePVEmptyState(keyFilePath, stateFilePath string, s []byte) *SFilePV

LoadSFilePVEmptyState loads a SFilePV from the given keyFilePath, with an empty LastSignState. If the keyFilePath does not exist, the program will exit.

func NewSFilePV

func NewSFilePV(privKey crypto.PrivKey, keyFilePath, stateFilePath string) *SFilePV

NewSFilePV generates a new validator from the given key and paths.

func (*SFilePV) GetAddress

func (pv *SFilePV) GetAddress() types.Address

GetAddress returns the address of the validator. Implements PrivValidator.

func (*SFilePV) GetPubKey

func (pv *SFilePV) GetPubKey() (crypto.PubKey, error)

GetPubKey returns the public key of the validator. Implements PrivValidator.

func (*SFilePV) ResetWith

func (pv *SFilePV) ResetWith(s []byte)

func (*SFilePV) SaveWith

func (pv *SFilePV) SaveWith(s []byte)

func (*SFilePV) SignProposal

func (pv *SFilePV) SignProposal(chainID string, proposal *tmproto.Proposal) error

SignProposal signs a canonical representation of the proposal, along with the chainID. Implements PrivValidator.

func (*SFilePV) SignVote

func (pv *SFilePV) SignVote(chainID string, vote *tmproto.Vote) error

SignVote signs a canonical representation of the vote, along with the chainID. Implements PrivValidator.

func (*SFilePV) String

func (pv *SFilePV) String() string

String returns a string representation of the SFilePV.

type SFilePVKey

type SFilePVKey struct {
	Address types.Address  `json:"address"`
	PubKey  crypto.PubKey  `json:"pub_key"`
	PrivKey crypto.PrivKey `json:"priv_key"`
	// contains filtered or unexported fields
}

SFilePVKey stores the immutable part of PrivValidator.

func (SFilePVKey) SaveWith

func (pvKey SFilePVKey) SaveWith(s []byte)

Save persists the SFilePVKey to its filePath.

type SFilePVLastSignState

type SFilePVLastSignState struct {
	Height    int64           `json:"height"`
	Round     int32           `json:"round"`
	Step      int8            `json:"step"`
	Signature []byte          `json:"signature,omitempty"`
	SignBytes abytes.HexBytes `json:"signbytes,omitempty"`
	// contains filtered or unexported fields
}

SFilePVLastSignState stores the mutable part of PrivValidator.

func (*SFilePVLastSignState) CheckHRS

func (lss *SFilePVLastSignState) CheckHRS(height int64, round int32, step int8) (bool, error)

CheckHRS checks the given height, round, step (HRS) against that of the SFilePVLastSignState. It returns an error if the arguments constitute a regression, or if they match but the SignBytes are empty. The returned boolean indicates whether the last Signature should be reused - it returns true if the HRS matches the arguments and the SignBytes are not empty (indicating we have already signed for this HRS, and can reuse the existing signature). It panics if the HRS matches the arguments, there's a SignBytes, but no Signature.

func (*SFilePVLastSignState) Save

func (lss *SFilePVLastSignState) Save()

Save persists the FilePvLastSignState to its filePath.

type WalletKey

type WalletKey struct {
	Version          int               `json:"version"`
	Address          types.Address     `json:"address"`
	Algo             string            `json:"algo"`
	CipherTextParams *cipherTextParams `json:"cp"`
	DKParams         *dkParams         `json:"dkp"`
	// contains filtered or unexported fields
}

func CreateWalletKeyFiles

func CreateWalletKeyFiles(s []byte, cnt int, dir string) ([]*WalletKey, error)

func NewWalletKey

func NewWalletKey(s []byte) *WalletKey

func NewWalletKeyWith

func NewWalletKeyWith(keyBytes, pass []byte) *WalletKey

func OpenWalletKey

func OpenWalletKey(r io.Reader) (*WalletKey, error)

func (*WalletKey) IsLock

func (wk *WalletKey) IsLock() bool

func (*WalletKey) Lock

func (wk *WalletKey) Lock()

func (*WalletKey) LockWith

func (wk *WalletKey) LockWith(pass []byte)

func (*WalletKey) PrvKey

func (wk *WalletKey) PrvKey() []byte

func (*WalletKey) PrvKeyClone

func (wk *WalletKey) PrvKeyClone() []byte

func (*WalletKey) PubKey

func (wk *WalletKey) PubKey() []byte

func (*WalletKey) Save

func (wk *WalletKey) Save(wr io.Writer) (int, error)

func (*WalletKey) Sign

func (wk *WalletKey) Sign(msg []byte) ([]byte, error)

func (*WalletKey) String

func (wk *WalletKey) String() string

func (*WalletKey) Unlock

func (wk *WalletKey) Unlock(s []byte) error

func (*WalletKey) VerifySig

func (wk *WalletKey) VerifySig(msg, sig []byte) bool

Jump to

Keyboard shortcuts

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