crypto

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package crypto implements cryptographic key utilities for bifrost.

It provides Key, PrivKey, and PubKey interfaces with an Ed25519 implementation. Wire-compatible with go-libp2p's crypto protobuf format.

Loosely based on the go-libp2p crypto implementation, covered under the MIT license: https://github.com/libp2p/go-libp2p/tree/master/core/crypto Original reference commit: github.com/aperturerobotics/go-libp2p@5cfbb50b74e0

Index

Constants

View Source
const Ed25519 = int(KeyType_Ed25519)

Ed25519 is the Ed25519 key type constant for use with GenerateKeyPair.

Variables

View Source
var (
	// ErrBadKeyType is returned when a key is not supported.
	ErrBadKeyType = errors.New("invalid or unsupported key type")
	// ErrNilPrivateKey is returned when a nil private key is provided.
	ErrNilPrivateKey = errors.New("private key is nil")
	// ErrNilPublicKey is returned when a nil public key is provided.
	ErrNilPublicKey = errors.New("public key is nil")
)
View Source
var (
	KeyType_name = map[int32]string{
		0: "RSA",
		1: "Ed25519",
	}
	KeyType_value = map[string]int32{
		"RSA":     0,
		"Ed25519": 1,
	}
)

Enum value maps for KeyType.

View Source
var PrivKeyUnmarshallers = map[KeyType]PrivKeyUnmarshaller{
	KeyType_Ed25519: UnmarshalEd25519PrivateKey,
}

PrivKeyUnmarshallers is a map of unmarshallers by key type.

View Source
var PubKeyUnmarshallers = map[KeyType]PubKeyUnmarshaller{
	KeyType_Ed25519: UnmarshalEd25519PublicKey,
}

PubKeyUnmarshallers is a map of unmarshallers by key type.

Functions

func ConfigDecodeKey

func ConfigDecodeKey(b string) ([]byte, error)

ConfigDecodeKey decodes from b64 (for config file) to a byte array that can be unmarshalled.

func ConfigEncodeKey

func ConfigEncodeKey(b []byte) string

ConfigEncodeKey encodes a marshalled key to b64 (for config file).

func GenerateEd25519Key

func GenerateEd25519Key(src io.Reader) (PrivKey, PubKey, error)

GenerateEd25519Key generates a new ed25519 private and public key pair.

func GenerateKeyPair

func GenerateKeyPair(typ KeyType, bits int) (PrivKey, PubKey, error)

GenerateKeyPair generates a private and public key.

func GenerateKeyPairWithReader

func GenerateKeyPairWithReader(typ KeyType, bits int, src io.Reader) (PrivKey, PubKey, error)

GenerateKeyPairWithReader returns a keypair of the given type and bit-size.

func KeyPairFromStdKey

func KeyPairFromStdKey(priv stdcrypto.PrivateKey) (PrivKey, PubKey, error)

KeyPairFromStdKey wraps standard library private keys in bifrost crypto keys.

func MarshalPrivateKey

func MarshalPrivateKey(k PrivKey) ([]byte, error)

MarshalPrivateKey converts a key object into its protobuf serialized form.

func MarshalPublicKey

func MarshalPublicKey(k PubKey) ([]byte, error)

MarshalPublicKey converts a public key object into a protobuf serialized public key.

func PrivKeyToStdKey

func PrivKeyToStdKey(priv PrivKey) (stdcrypto.PrivateKey, error)

PrivKeyToStdKey converts a bifrost private key to a standard library private key.

func PubKeyToStdKey

func PubKeyToStdKey(pub PubKey) (stdcrypto.PublicKey, error)

PubKeyToStdKey converts a bifrost public key to a standard library public key.

Types

type Ed25519PrivateKey

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

Ed25519PrivateKey is an ed25519 private key.

func (*Ed25519PrivateKey) Equals

func (k *Ed25519PrivateKey) Equals(o Key) bool

Equals compares two ed25519 private keys.

func (*Ed25519PrivateKey) GetPublic

func (k *Ed25519PrivateKey) GetPublic() PubKey

GetPublic returns an ed25519 public key from a private key.

func (*Ed25519PrivateKey) GetStdKey

func (k *Ed25519PrivateKey) GetStdKey() ed25519.PrivateKey

GetStdKey returns the standard library ed25519.PrivateKey.

func (*Ed25519PrivateKey) Raw

func (k *Ed25519PrivateKey) Raw() ([]byte, error)

Raw returns the raw private key bytes.

func (*Ed25519PrivateKey) Sign

func (k *Ed25519PrivateKey) Sign(msg []byte) ([]byte, error)

Sign returns a signature from an input message.

func (*Ed25519PrivateKey) Type

func (k *Ed25519PrivateKey) Type() KeyType

Type returns the key type (Ed25519).

type Ed25519PublicKey

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

Ed25519PublicKey is an ed25519 public key.

func (*Ed25519PublicKey) Equals

func (k *Ed25519PublicKey) Equals(o Key) bool

Equals compares two ed25519 public keys.

func (*Ed25519PublicKey) GetStdKey

func (k *Ed25519PublicKey) GetStdKey() ed25519.PublicKey

GetStdKey returns the standard library ed25519.PublicKey.

func (*Ed25519PublicKey) Raw

func (k *Ed25519PublicKey) Raw() ([]byte, error)

Raw returns the raw public key bytes.

func (*Ed25519PublicKey) Type

func (k *Ed25519PublicKey) Type() KeyType

Type returns the key type (Ed25519).

func (*Ed25519PublicKey) Verify

func (k *Ed25519PublicKey) Verify(data []byte, sig []byte) (bool, error)

Verify checks a signature against the input data.

type Key

type Key interface {
	// Equals checks whether two PubKeys are the same.
	Equals(Key) bool

	// Raw returns the raw bytes of the key (not wrapped in the protobuf).
	//
	// This function is the inverse of {Priv,Pub}KeyUnmarshaler.
	Raw() ([]byte, error)

	// Type returns the protobuf key type.
	Type() KeyType
}

Key represents a crypto key that can be compared to another key.

type KeyType

type KeyType int32

KeyType defines the list of supported crypto key types. This is intended to be drop-in compatible with go-libp2p KeyType.

const (
	KeyType_RSA     KeyType = 0
	KeyType_Ed25519 KeyType = 1
)

func (KeyType) Enum

func (x KeyType) Enum() *KeyType

func (KeyType) MarshalJSON

func (x KeyType) MarshalJSON() ([]byte, error)

MarshalJSON marshals the KeyType to JSON.

func (KeyType) MarshalProtoJSON

func (x KeyType) MarshalProtoJSON(s *json.MarshalState)

MarshalProtoJSON marshals the KeyType to JSON.

func (KeyType) MarshalProtoText

func (x KeyType) MarshalProtoText() string

func (KeyType) MarshalText

func (x KeyType) MarshalText() ([]byte, error)

MarshalText marshals the KeyType to text.

func (KeyType) String

func (x KeyType) String() string

func (*KeyType) UnmarshalJSON

func (x *KeyType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the KeyType from JSON.

func (*KeyType) UnmarshalProtoJSON

func (x *KeyType) UnmarshalProtoJSON(s *json.UnmarshalState)

UnmarshalProtoJSON unmarshals the KeyType from JSON.

func (*KeyType) UnmarshalText

func (x *KeyType) UnmarshalText(b []byte) error

UnmarshalText unmarshals the KeyType from text.

type PrivKey

type PrivKey interface {
	Key

	// Sign cryptographically signs the given bytes.
	Sign([]byte) ([]byte, error)

	// GetPublic returns a public key paired with this private key.
	GetPublic() PubKey
}

PrivKey represents a private key that can be used to generate a public key and sign data.

func UnmarshalEd25519PrivateKey

func UnmarshalEd25519PrivateKey(data []byte) (PrivKey, error)

UnmarshalEd25519PrivateKey returns a private key from input bytes.

func UnmarshalPrivateKey

func UnmarshalPrivateKey(data []byte) (PrivKey, error)

UnmarshalPrivateKey converts a protobuf serialized private key into its representative object.

type PrivKeyUnmarshaller

type PrivKeyUnmarshaller func(data []byte) (PrivKey, error)

PrivKeyUnmarshaller is a func that creates a PrivKey from a given slice of bytes.

type PrivateKey

type PrivateKey struct {

	// KeyType is the type of the key.
	KeyType KeyType `protobuf:"varint,1,opt,name=key_type,json=keyType,proto3" json:"keyType,omitempty"`
	// Data is the data of the key.
	Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
	// contains filtered or unexported fields
}

PrivateKey is the protobuf message for private keys.

This is wire-compatible with the libp2p PrivateKey type.

func (*PrivateKey) CloneMessageVT

func (m *PrivateKey) CloneMessageVT() protobuf_go_lite.CloneMessage

func (*PrivateKey) CloneVT

func (m *PrivateKey) CloneVT() *PrivateKey

func (*PrivateKey) EqualMessageVT

func (this *PrivateKey) EqualMessageVT(thatMsg any) bool

func (*PrivateKey) EqualVT

func (this *PrivateKey) EqualVT(that *PrivateKey) bool

func (*PrivateKey) GetData

func (x *PrivateKey) GetData() []byte

func (*PrivateKey) GetKeyType

func (x *PrivateKey) GetKeyType() KeyType

func (*PrivateKey) MarshalJSON

func (x *PrivateKey) MarshalJSON() ([]byte, error)

MarshalJSON marshals the PrivateKey to JSON.

func (*PrivateKey) MarshalProtoJSON

func (x *PrivateKey) MarshalProtoJSON(s *json.MarshalState)

MarshalProtoJSON marshals the PrivateKey message to JSON.

func (*PrivateKey) MarshalProtoText

func (x *PrivateKey) MarshalProtoText() string

func (*PrivateKey) MarshalToSizedBufferVT

func (m *PrivateKey) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*PrivateKey) MarshalToVT

func (m *PrivateKey) MarshalToVT(dAtA []byte) (int, error)

func (*PrivateKey) MarshalVT

func (m *PrivateKey) MarshalVT() (dAtA []byte, err error)

func (*PrivateKey) ProtoMessage

func (*PrivateKey) ProtoMessage()

func (*PrivateKey) Reset

func (x *PrivateKey) Reset()

func (*PrivateKey) SizeVT

func (m *PrivateKey) SizeVT() (n int)

func (*PrivateKey) String

func (x *PrivateKey) String() string

func (*PrivateKey) UnmarshalJSON

func (x *PrivateKey) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the PrivateKey from JSON.

func (*PrivateKey) UnmarshalProtoJSON

func (x *PrivateKey) UnmarshalProtoJSON(s *json.UnmarshalState)

UnmarshalProtoJSON unmarshals the PrivateKey message from JSON.

func (*PrivateKey) UnmarshalVT

func (m *PrivateKey) UnmarshalVT(dAtA []byte) error

type PubKey

type PubKey interface {
	Key

	// Verify checks that 'sig' is the signed hash of 'data'.
	Verify(data []byte, sig []byte) (bool, error)
}

PubKey is a public key that can be used to verify data signed with the corresponding private key.

func ECDSAPublicKeyFromStdKey

func ECDSAPublicKeyFromStdKey(pub *ecdsa.PublicKey) PubKey

ECDSAPublicKeyFromStdKey wraps a standard library *ecdsa.PublicKey. This is provided for interop with x509 certificates that may use ECDSA; bifrost does not generate ECDSA keys itself.

func PublicKeyFromProto

func PublicKeyFromProto(pmes *PublicKey) (PubKey, error)

PublicKeyFromProto converts an unserialized protobuf PublicKey message into its representative object.

func UnmarshalEd25519PublicKey

func UnmarshalEd25519PublicKey(data []byte) (PubKey, error)

UnmarshalEd25519PublicKey returns a public key from input bytes.

func UnmarshalPublicKey

func UnmarshalPublicKey(data []byte) (PubKey, error)

UnmarshalPublicKey converts a protobuf serialized public key into its representative object.

type PubKeyUnmarshaller

type PubKeyUnmarshaller func(data []byte) (PubKey, error)

PubKeyUnmarshaller is a func that creates a PubKey from a given slice of bytes.

type PublicKey

type PublicKey struct {

	// KeyType is the type of the key.
	KeyType KeyType `protobuf:"varint,1,opt,name=key_type,json=keyType,proto3" json:"keyType,omitempty"`
	// Data is the data of the key.
	Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
	// contains filtered or unexported fields
}

PublicKey is the protobuf message for public keys.

This is wire-compatible with the libp2p PublicKey type.

func PublicKeyToProto

func PublicKeyToProto(k PubKey) (*PublicKey, error)

PublicKeyToProto converts a public key object into an unserialized protobuf PublicKey message.

func (*PublicKey) CloneMessageVT

func (m *PublicKey) CloneMessageVT() protobuf_go_lite.CloneMessage

func (*PublicKey) CloneVT

func (m *PublicKey) CloneVT() *PublicKey

func (*PublicKey) EqualMessageVT

func (this *PublicKey) EqualMessageVT(thatMsg any) bool

func (*PublicKey) EqualVT

func (this *PublicKey) EqualVT(that *PublicKey) bool

func (*PublicKey) GetData

func (x *PublicKey) GetData() []byte

func (*PublicKey) GetKeyType

func (x *PublicKey) GetKeyType() KeyType

func (*PublicKey) MarshalJSON

func (x *PublicKey) MarshalJSON() ([]byte, error)

MarshalJSON marshals the PublicKey to JSON.

func (*PublicKey) MarshalProtoJSON

func (x *PublicKey) MarshalProtoJSON(s *json.MarshalState)

MarshalProtoJSON marshals the PublicKey message to JSON.

func (*PublicKey) MarshalProtoText

func (x *PublicKey) MarshalProtoText() string

func (*PublicKey) MarshalToSizedBufferVT

func (m *PublicKey) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*PublicKey) MarshalToVT

func (m *PublicKey) MarshalToVT(dAtA []byte) (int, error)

func (*PublicKey) MarshalVT

func (m *PublicKey) MarshalVT() (dAtA []byte, err error)

func (*PublicKey) ProtoMessage

func (*PublicKey) ProtoMessage()

func (*PublicKey) Reset

func (x *PublicKey) Reset()

func (*PublicKey) SizeVT

func (m *PublicKey) SizeVT() (n int)

func (*PublicKey) String

func (x *PublicKey) String() string

func (*PublicKey) UnmarshalJSON

func (x *PublicKey) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the PublicKey from JSON.

func (*PublicKey) UnmarshalProtoJSON

func (x *PublicKey) UnmarshalProtoJSON(s *json.UnmarshalState)

UnmarshalProtoJSON unmarshals the PublicKey message from JSON.

func (*PublicKey) UnmarshalVT

func (m *PublicKey) UnmarshalVT(dAtA []byte) error

Directories

Path Synopsis
Package p2ptls provides TLS identity for bifrost peer authentication.
Package p2ptls provides TLS identity for bifrost peer authentication.

Jump to

Keyboard shortcuts

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