crpt

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: MIT Imports: 10 Imported by: 2

README

go-crpt

go-crpt provides interfaces and implementations for common crypto operations in Go.

It's especially useful for DLT projects.

Usage

See godoc.

Currently available implementations:

ed25519

EPackage ed25519 provides an Ed25519 implementation backed by curve25519-voi package.

Note: earlier version of this package uses the following packages, replaced by curve25519-voi because benchmarks show that it has the better performance and supports signing pre-hashed messages by implementing Ed25519ph.

Old docs:

Package ed25519 provides an Ed25519 implementation backed by crypto/ed25519 std package, but using ed25519consensus package for signature verification, which conforms to ZIP 215 specification, making it suitable for consensus-critical contexts, see README from ed25519consensus for the explanation.

It also provides an Ed25519-SHA3-512 implementation backed by go-ed25519-sha3-512 package, which is a fork of crypto/ed25519 std package, modified to use SHA3-512 instead of SHA-512. using go-ed25519consensus-sha3-512 package for signature verification, which is a fork of ed25519consensus package, modified to use SHA3-512 instead of SHA-512. So it's also suitable for consensus-critical contexts.

Inspirations

License

MIT © DAOT Labs.

Documentation

Overview

Package crpt provides interface for common crypto operations.

Index

Constants

View Source
const NotHashed crypto.Hash = 0

Passing NotHashed as hashFunc to Crpt.Sign indicates that message is not hashed

Variables

View Source
var (
	ErrKeyTypeNotSupported  = errors.New("key type not supported")
	ErrUnimplemented        = errors.New("not implemented")
	ErrWrongPublicKeySize   = errors.New("wrong public key size")
	ErrWrongPrivateKeySize  = errors.New("wrong private key size")
	ErrWrongSignatureSize   = errors.New("wrong signature size")
	ErrEmptyMessage         = errors.New("message is empty")
	ErrInvalidHashFunc      = errors.New("invalid hash function")
	ErrNoMatchingCryptoHash = errors.New("no matching crypto.Hash exists")
)
View Source
var CryptoHashToMulticodec = []uint64{
	0,
	0xd4,
	0xd5,
	0x11,
	0x1013,
	0x12,
	0x20,
	0x13,
	0,
	0x1053,
	0x17,
	0x16,
	0x15,
	0x14,
	0x1014,
	0x1015,
	0xb260,
	0xb220,
	0xb230,
	0xb240,
}

CryptoHashToMulticodec is the mapping from `crypto.Hash` to `multicodec` See: https://github.com/multiformats/multicodec/blob/master/table.csv

View Source
var MulticodecToCryptoHash = map[uint64]crypto.Hash{
	multihash.SHA1:     crypto.SHA1,
	multihash.SHA2_256: crypto.SHA256,
	multihash.SHA2_512: crypto.SHA512,
	multihash.SHA3_224: crypto.SHA224,
	multihash.SHA3_256: crypto.SHA3_256,
	multihash.SHA3_384: crypto.SHA3_384,
	multihash.SHA3_512: crypto.SHA3_512,
	multihash.MD5:      crypto.MD5,
}

MulticodecToCryptoHash is the mapping from `multicodec` to `crypto.Hash` See: https://github.com/multiformats/multicodec/blob/master/table.csv

Functions

func GenerateKey added in v0.4.0

func GenerateKey(t KeyType, rand io.Reader) (PublicKey, PrivateKey, error)

GenerateKey generates a public/private key pair using entropy from rand.

func RegisterCrpt added in v0.4.0

func RegisterCrpt(t KeyType, c Crpt)

RegisterCrpt registers a function that returns a new instance of the given Crpt instance. This is intended to be called from the init function in packages that implement Crpt interface.

func Verify added in v0.4.0

func Verify(pub PublicKey, message, digest []byte, hashFunc crypto.Hash, sig Signature) (bool, error)

Verify reports whether `sig` is a valid signature of message or digest by `pub`.

See PublicKey.Verify for more details.

func VerifyDigest added in v0.5.0

func VerifyDigest(pub PublicKey, digest []byte, hashFunc crypto.Hash, sig Signature) (bool, error)

VerifyDigest reports whether `sig` is a valid signature of digest by `pub`.

See PublicKey.VerifyDigest for more details.

func VerifyMessage added in v0.5.0

func VerifyMessage(pub PublicKey, message []byte, sig Signature) (bool, error)

VerifyMessage reports whether `sig` is a valid signature of message by `pub`.

See PublicKey.VerifyMessage for more details.

Types

type Address

type Address = gbytes.HexBytes

Address represents an address derived from a PublicKey. An address is a []byte, but hex-encoded even in JSON.

type BaseCrpt added in v0.5.0

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

BaseCrpt is a helper struct meant to be anonymously embedded by pointer in all Crpt implementations.

func NewBaseCrpt added in v0.5.0

func NewBaseCrpt(t KeyType, hashFunc crypto.Hash, canSignPreHashedMessages bool, parentCrpt Crpt,
) (*BaseCrpt, error)

func (*BaseCrpt) Hash added in v0.5.0

func (c *BaseCrpt) Hash(b []byte) Hash

Hash implements Crpt.Hash using BaseCrpt.hashFunc.

func (*BaseCrpt) HashFunc added in v0.5.0

func (c *BaseCrpt) HashFunc() crypto.Hash

HashFunc implements crpt.HashFunc.

func (*BaseCrpt) HashToTyped added in v0.5.0

func (c *BaseCrpt) HashToTyped(h Hash) TypedHash

HashToTyped decorates a hash into a TypedHash.

func (*BaseCrpt) HashTyped added in v0.5.0

func (c *BaseCrpt) HashTyped(b []byte) TypedHash

Hash implements Crpt.Hash using BaseCrpt.hashFunc.

func (*BaseCrpt) KeyType added in v0.5.0

func (c *BaseCrpt) KeyType() KeyType

KeyType implements crpt.KeyType.

func (*BaseCrpt) MerkleHashFromByteSlices added in v0.5.0

func (c *BaseCrpt) MerkleHashFromByteSlices(items [][]byte) (rootHash []byte)

Sign implements Crpt.MerkleHashFromByteSlices using `crpt/go-merkle`.

func (*BaseCrpt) MerkleHashTypedFromByteSlices added in v0.5.0

func (c *BaseCrpt) MerkleHashTypedFromByteSlices(items [][]byte) (rootHash TypedHash)

Sign implements Crpt.MerkleHashTypedFromByteSlices using `crpt/go-merkle`.

func (*BaseCrpt) MerkleProofsFromByteSlices added in v0.5.0

func (c *BaseCrpt) MerkleProofsFromByteSlices(items [][]byte,
) (rootHash []byte, proofs []*merkle.Proof)

Sign implements Crpt.MerkleProofsFromByteSlices using `crpt/go-merkle`.

func (*BaseCrpt) MerkleProofsTypedFromByteSlices added in v0.5.0

func (c *BaseCrpt) MerkleProofsTypedFromByteSlices(items [][]byte,
) (rootHash TypedHash, proofs []*merkle.Proof)

Sign implements Crpt.MerkleProofsTypedFromByteSlices using `crpt/go-merkle`.

func (*BaseCrpt) Sign added in v0.5.0

func (c *BaseCrpt) Sign(priv PrivateKey, message, digest []byte, hashFunc crypto.Hash, rand io.Reader,
) (Signature, error)

Sign implements Crpt.Sign, see Crpt.Sign for details.

func (*BaseCrpt) SumHashTyped added in v0.5.0

func (c *BaseCrpt) SumHashTyped(h hash.Hash, b []byte) []byte

SumHashTyped implements crpt.SumHashTyped.

type BatchVerifier added in v0.5.0

type BatchVerifier interface {
	// Add appends an entry into the BatchVerifier.
	Add(key PublicKey, message []byte, sig Signature) error

	// Verify verifies all the entries in the BatchVerifier, and returns
	// if every signature in the batch is valid, and a vector of bools
	// indicating the verification status of each signature (in the order
	// that signatures were added to the batch).
	Verify(rand io.Reader) (bool, []bool)
}

BatchVerifier provides batch signature verification.

If a new key type implements batch verification, the key type must be registered in `github.com/crpt/go-crpt/batch`

type Crpt

type Crpt interface {
	// KeyType reports the key type used.
	//
	// Crpt implementations generally don't need to implement this method as it is
	// already implemented by embedded BaseCrpt.
	KeyType() KeyType

	// HashFunc reports the hash function to be used for Crpt.Hash.
	//
	// Crpt implementations generally don't need to implement this method as it is
	// already implemented by embedded BaseCrpt.
	HashFunc() crypto.Hash

	// Hash calculates the hash of msg using underlying BaseCrpt.hashFunc.
	//
	// Crpt implementations generally don't need to implement this method as it is
	// already implemented by embedded BaseCrpt.
	Hash(msg []byte) Hash

	// HashTyped calculates the hash of msg using underlying BaseCrpt.hashFunc
	// and return its TypedHash bytes representation.
	//
	// Crpt implementations generally don't need to implement this method as it is
	// already implemented by embedded BaseCrpt.
	HashTyped(msg []byte) TypedHash

	// SumHashTyped appends the current hash of `h` in TypeHash bytes
	// representation to`b` and returns the resulting/slice.
	// It does not change the underlying hash state.
	SumHashTyped(h hash.Hash, b []byte) []byte

	// HashToTyped decorates a hash into a TypedHash.
	HashToTyped(Hash) TypedHash

	// PublicKeyFromBytes constructs a PublicKey from raw bytes.
	PublicKeyFromBytes(pub []byte) (PublicKey, error)

	// PrivateKeyFromBytes constructs a PrivateKey from raw bytes.
	PrivateKeyFromBytes(priv []byte) (PrivateKey, error)

	// SignatureToTyped decorates a Signature into a TypedSignature.
	SignatureToTyped(sig Signature) (TypedSignature, error)

	// GenerateKey generates a public/private key pair using entropy from rand.
	GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error)

	// Sign signs message or digest and returns a Signature, possibly using entropy from rand.
	//
	// See PrivateKey.Sign for more details.
	Sign(priv PrivateKey, message, digest []byte, hashFunc crypto.Hash, rand io.Reader) (Signature, error)

	// SignMessage directly signs message with `priv`, possibly using entropy from rand.
	//
	// See PrivateKey.SignMessage for more details.
	SignMessage(priv PrivateKey, message []byte, rand io.Reader) (Signature, error)

	// SignDigest signs digest with `priv` and returns a Signature, possibly using entropy from rand.
	//
	// See PrivateKey.SignDigest for more details.
	SignDigest(priv PrivateKey, digest []byte, hashFunc crypto.Hash, rand io.Reader) (Signature, error)

	// Verify reports whether `sig` is a valid signature of message or digest by `pub`.
	//
	// See PublicKey.Verify for more details.
	Verify(pub PublicKey, message, digest []byte, hashFunc crypto.Hash, sig Signature) (bool, error)

	// VerifyMessage reports whether `sig` is a valid signature of message by `pub`.
	//
	// See PublicKey.VerifyMessage for more details.
	VerifyMessage(pub PublicKey, message []byte, sig Signature) (bool, error)

	// VerifyDigest reports whether `sig` is a valid signature of digest by `pub`.
	//
	// See PublicKey.VerifyDigest for more details.
	VerifyDigest(pub PublicKey, digest []byte, hashFunc crypto.Hash, sig Signature) (bool, error)

	// MerkleHashFromByteSlices computes a Merkle tree where the leaves are the byte slice,
	// in the provided order. It follows RFC-6962.
	MerkleHashFromByteSlices(items [][]byte) (rootHash []byte)

	// MerkleHashTypedFromByteSlices computes a Merkle tree where the leaves are the byte slice,
	// in the provided order. It follows RFC-6962.
	// This returns the TypedHash bytes representation.
	MerkleHashTypedFromByteSlices(items [][]byte) (rootHash TypedHash)

	// MerkleProofsFromByteSlices computes inclusion proof for given items.
	// proofs[0] is the proof for items[0].
	MerkleProofsFromByteSlices(items [][]byte) (rootHash []byte, proofs []*merkle.Proof)

	// MerkleProofsTypedFromByteSlices computes inclusion proof for given items.
	// proofs[0] is the proof for items[0].
	// This returns the TypedHash bytes representation.
	MerkleProofsTypedFromByteSlices(items [][]byte) (rootHash TypedHash, proofs []*merkle.Proof)
}

Crpt is the common crypto operations interface implemented by all crypto implementations.

type Hash added in v0.4.2

type Hash []byte

Hash represents a hash.

type KeyType added in v0.2.0

type KeyType uint8
const (
	Ed25519 KeyType = 1 + iota
	// This may change as new implementations come out.
	MaxCrpt
)

func (KeyType) Available added in v0.5.0

func (t KeyType) Available() bool

Available reports whether the given KeyType implementation is available.

type PrivateKey

type PrivateKey interface {
	// KeyType returns the key type.
	KeyType() KeyType

	// Equal reports whether this key is equal to another key.
	// Runs in constant time based on length of the keys to prevent time attacks.
	Equal(PrivateKey) bool

	// Bytes returns the bytes representation of the private key.
	//
	// NOTE: It's not safe to modify the returned slice because the implementations most likely
	// return the underlying byte slice directly for the performance reason. Copy it if you need to modify.
	Bytes() []byte

	// TypedBytes returns the TypedPrivateKey bytes representation of the private key.
	//
	// NOTE: It's not safe to modify the returned slice because the implementations most likely
	// return the underlying byte slice directly for the performance reason. Copy it if you need to modify.
	TypedBytes() TypedPrivateKey

	// Public returns the public key corresponding to the private key.
	//
	// NOTE: It's not safe to modify the returned slice because the implementations most likely
	// return the underlying byte slice directly for the performance reason. Copy it if you need to modify.
	Public() PublicKey

	// Sign signs message or digest and returns a Signature, possibly using entropy from rand.
	//
	// In most case, it is recommended to use Sign instead of SignMessage or SignDigest. If not
	// providing digest (nil or empty), the caller can pass NotHashed as the value for hashFunc.
	//
	// If digest is provided (not empty), and the Crpt implementation is appropriate
	// for signing the pre-hashed messages (see SignMessage for details), Sign should
	// just call SignDigest, otherwise it should just call SignMessage.
	//
	// Crpt implementations generally don't need to implement this method as it is
	// already implemented by embedded BaseCrpt.
	Sign(message, digest []byte, hashFunc crypto.Hash, rand io.Reader) (Signature, error)

	// SignMessage directly signs message with `priv`, possibly using entropy from rand.
	SignMessage(message []byte, rand io.Reader) (Signature, error)

	// SignDigest signs digest with `priv` and returns a Signature, possibly using entropy from rand.
	//
	// The caller must hash the message and pass the hash (as digest) and the hash
	// function used (as hashFunc) to SignDigest.
	//
	// Some Crpt implementations are not appropriate for signing the pre-hashed messages, which will
	// return ErrUnimplemented.
	SignDigest(digest []byte, hashFunc crypto.Hash, rand io.Reader) (Signature, error)
}

PrivateKey represents a private key with a specific key type.

func PrivateKeyFromBytes added in v0.4.0

func PrivateKeyFromBytes(t KeyType, priv []byte) (PrivateKey, error)

PrivateKeyFromBytes constructs a PrivateKey from raw bytes.

func PrivateKeyFromTypedBytes added in v0.4.0

func PrivateKeyFromTypedBytes(priv TypedPrivateKey) (PrivateKey, error)

PrivateKeyFromTypedBytes constructs a PrivateKey from its TypedPrivateKey bytes representation.

type PublicKey

type PublicKey interface {
	// KeyType returns the key type.
	KeyType() KeyType

	// Equal reports whether this key is equal to another key.
	// Runs in constant time based on length of the keys to prevent time attacks.
	Equal(PublicKey) bool

	// Bytes returns the bytes representation of the public key.
	//
	// NOTE: It's not safe to modify the returned slice because the implementations most likely
	// return the underlying byte slice directly for the performance reason. Copy it if you need to modify.
	Bytes() []byte

	// TypedBytes returns the TypedPublicKey bytes representation of the public key.
	//
	// NOTE: It's not safe to modify the returned slice because the implementations most likely
	// return the underlying byte slice directly for the performance reason. Copy it if you need to modify.
	TypedBytes() TypedPublicKey

	// Address returns the address derived from the public key.
	//
	// NOTE: It's not safe to modify the returned slice because the implementations most likely
	// return the underlying byte slice directly for the performance reason. Copy it if you need to modify.
	Address() Address

	// Verify reports whether `sig` is a valid signature of message or digest by the public key.
	//
	// In most case, it is recommended to use Verify instead of VerifyMessage or VerifyDigest.
	//If not providing digest (nil or empty), the caller can pass NotHashed as the value for hashFunc.
	//
	// If digest is provided (not empty), and the Crpt implementation is appropriate for signing the
	// pre-hashed messages (see SignMessage for details), Verify should try VerifyDigest first, then
	// it should try VerifyMessage, it should returns true if either returns true.
	//
	// Crpt implementations generally don't need to implement this method as it is
	// already implemented by embedded BaseCrpt.
	Verify(message, digest []byte, hashFunc crypto.Hash, sig Signature) (bool, error)

	// VerifyMessage reports whether `sig` is a valid signature of message by the public key.
	VerifyMessage(message []byte, sig Signature) (bool, error)

	// VerifyDigest reports whether `sig` is a valid signature of digest by the public key.
	//
	// The caller must hash the message and pass the hash (as digest) and the hash
	// function used (as hashFunc) to SignDigest.
	//
	// Some Crpt implementations are not appropriate for signing the pre-hashed messages, which will
	// return ErrUnimplemented.
	VerifyDigest(digest []byte, hashFunc crypto.Hash, sig Signature) (bool, error)
}

PublicKey represents a public key with a specific key type.

func PublicKeyFromBytes added in v0.4.0

func PublicKeyFromBytes(t KeyType, pub []byte) (PublicKey, error)

PublicKeyFromBytes constructs a PublicKey from raw bytes.

func PublicKeyFromTypedBytes added in v0.4.0

func PublicKeyFromTypedBytes(pub TypedPublicKey) (PublicKey, error)

PublicKeyFromTypedBytes constructs a PublicKey from its TypedPublicKey bytes representation.

type Signature

type Signature []byte

Signature represents a digital signature produced by signing a message.

func Sign added in v0.4.0

func Sign(priv PrivateKey, message, digest []byte, hashFunc crypto.Hash, rand io.Reader,
) (Signature, error)

Sign signs message or digest and returns a Signature, possibly using entropy from rand.

See PrivateKey.Sign for more details.

func SignDigest added in v0.4.0

func SignDigest(priv PrivateKey, digest []byte, hashFunc crypto.Hash, rand io.Reader,
) (Signature, error)

SignDigest signs digest with `priv` and returns a Signature, possibly using entropy from rand.

See PrivateKey.SignDigest for more details.

func SignMessage added in v0.4.0

func SignMessage(priv PrivateKey, message []byte, rand io.Reader,
) (Signature, error)

SignMessage directly signs message with `priv`, possibly using entropy from rand.

See PrivateKey.SignMessage for more details.

func (Signature) Equal added in v0.3.2

func (sig Signature) Equal(o Signature) bool

Equal reports whether this signature is equal to another one. Runs in constant time based on length of the keys to prevent time attacks.

type TypedHash added in v0.2.0

type TypedHash []byte

TypedHash is a hash representation that replace the first byte with uint8 representation of the crypto.Hash used.

func HashToTyped added in v0.4.2

func HashToTyped(hashFunc crypto.Hash, h Hash) TypedHash

HashToTyped decorates a hash into a TypedHash with crypto.Hash.

func TypedHashFromMultihash added in v0.4.0

func TypedHashFromMultihash(mh multihash.Multihash) (TypedHash, error)

TypedHashFromMultihash turns a Multihash into a TypedHash.

func (TypedHash) Equal added in v0.3.2

func (h TypedHash) Equal(o TypedHash) bool

Equal reports whether this hash is equal to another hash.

type TypedPrivateKey added in v0.2.0

type TypedPrivateKey []byte

TypedPrivateKey consists of 1-byte KeyType of a PrivateKey concatenated with its bytes representation.

func (TypedPrivateKey) Equal added in v0.3.2

func (priv TypedPrivateKey) Equal(o TypedPrivateKey) bool

Equal reports whether this key is equal to another key. Runs in constant time based on length of the keys to prevent time attacks.

func (TypedPrivateKey) Raw added in v0.2.0

func (priv TypedPrivateKey) Raw() []byte

Raw return the raw bytes of the private key without the 1-byte key type prefix.

The returned byte slice is not safe to modify because it returns the underlying byte slice directly for the performance reason. Copy it if you need to modify.

type TypedPublicKey added in v0.2.0

type TypedPublicKey []byte

TypedPublicKey consists of 1-byte KeyType of a PublicKey concatenated with its bytes representation.

func (TypedPublicKey) Equal added in v0.3.2

func (pub TypedPublicKey) Equal(o TypedPublicKey) bool

Equal reports whether this key is equal to another key.

func (TypedPublicKey) Raw added in v0.2.0

func (pub TypedPublicKey) Raw() []byte

Raw return the raw bytes of the public key without the 1-byte key type prefix.

The returned byte slice is not safe to modify because it returns the underlying byte slice directly for the performance reason. Copy it if you need to modify.

type TypedSignature added in v0.2.0

type TypedSignature []byte

TypedSignature consists of 1-byte representation of crypto.Hash used as uint8 concatenated with the signature's bytes representation.

func SignatureToTyped added in v0.4.2

func SignatureToTyped(t KeyType, sig Signature) (TypedSignature, error)

SignatureToTyped decorates a Signature into a TypedSignature.

Directories

Path Synopsis
Package ed25519 provides an Ed25519 implementation backed by [crypto/ed25519](https://pkg.go.dev/crypto/ed25519) std package, but using [ed25519consensus](https://pkg.go.dev/github.com/hdevalence/ed25519consensus) package for signature verification, which conforms to [ZIP 215](https://zips.z.cash/zip-0215) specification, making it suitable for consensus-critical contexts, see [README from ed25519consensus](https://github.com/hdevalence/ed25519consensus) for the explanation.
Package ed25519 provides an Ed25519 implementation backed by [crypto/ed25519](https://pkg.go.dev/crypto/ed25519) std package, but using [ed25519consensus](https://pkg.go.dev/github.com/hdevalence/ed25519consensus) package for signature verification, which conforms to [ZIP 215](https://zips.z.cash/zip-0215) specification, making it suitable for consensus-critical contexts, see [README from ed25519consensus](https://github.com/hdevalence/ed25519consensus) for the explanation.
Package factory is used to create Crpt instances from options.
Package factory is used to create Crpt instances from options.
internal

Jump to

Keyboard shortcuts

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