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
- Variables
- func ConfigDecodeKey(b string) ([]byte, error)
- func ConfigEncodeKey(b []byte) string
- func GenerateEd25519Key(src io.Reader) (PrivKey, PubKey, error)
- func GenerateKeyPair(typ KeyType, bits int) (PrivKey, PubKey, error)
- func GenerateKeyPairWithReader(typ KeyType, bits int, src io.Reader) (PrivKey, PubKey, error)
- func KeyPairFromStdKey(priv stdcrypto.PrivateKey) (PrivKey, PubKey, error)
- func MarshalPrivateKey(k PrivKey) ([]byte, error)
- func MarshalPublicKey(k PubKey) ([]byte, error)
- func PrivKeyToStdKey(priv PrivKey) (stdcrypto.PrivateKey, error)
- func PubKeyToStdKey(pub PubKey) (stdcrypto.PublicKey, error)
- type Ed25519PrivateKey
- func (k *Ed25519PrivateKey) Equals(o Key) bool
- func (k *Ed25519PrivateKey) GetPublic() PubKey
- func (k *Ed25519PrivateKey) GetStdKey() ed25519.PrivateKey
- func (k *Ed25519PrivateKey) Raw() ([]byte, error)
- func (k *Ed25519PrivateKey) Sign(msg []byte) ([]byte, error)
- func (k *Ed25519PrivateKey) Type() KeyType
- type Ed25519PublicKey
- type Key
- type KeyType
- func (x KeyType) Enum() *KeyType
- func (x KeyType) MarshalJSON() ([]byte, error)
- func (x KeyType) MarshalProtoJSON(s *json.MarshalState)
- func (x KeyType) MarshalProtoText() string
- func (x KeyType) MarshalText() ([]byte, error)
- func (x KeyType) String() string
- func (x *KeyType) UnmarshalJSON(b []byte) error
- func (x *KeyType) UnmarshalProtoJSON(s *json.UnmarshalState)
- func (x *KeyType) UnmarshalText(b []byte) error
- type PrivKey
- type PrivKeyUnmarshaller
- type PrivateKey
- func (m *PrivateKey) CloneMessageVT() protobuf_go_lite.CloneMessage
- func (m *PrivateKey) CloneVT() *PrivateKey
- func (this *PrivateKey) EqualMessageVT(thatMsg any) bool
- func (this *PrivateKey) EqualVT(that *PrivateKey) bool
- func (x *PrivateKey) GetData() []byte
- func (x *PrivateKey) GetKeyType() KeyType
- func (x *PrivateKey) MarshalJSON() ([]byte, error)
- func (x *PrivateKey) MarshalProtoJSON(s *json.MarshalState)
- func (x *PrivateKey) MarshalProtoText() string
- func (m *PrivateKey) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *PrivateKey) MarshalToVT(dAtA []byte) (int, error)
- func (m *PrivateKey) MarshalVT() (dAtA []byte, err error)
- func (*PrivateKey) ProtoMessage()
- func (x *PrivateKey) Reset()
- func (m *PrivateKey) SizeVT() (n int)
- func (x *PrivateKey) String() string
- func (x *PrivateKey) UnmarshalJSON(b []byte) error
- func (x *PrivateKey) UnmarshalProtoJSON(s *json.UnmarshalState)
- func (m *PrivateKey) UnmarshalVT(dAtA []byte) error
- type PubKey
- type PubKeyUnmarshaller
- type PublicKey
- func (m *PublicKey) CloneMessageVT() protobuf_go_lite.CloneMessage
- func (m *PublicKey) CloneVT() *PublicKey
- func (this *PublicKey) EqualMessageVT(thatMsg any) bool
- func (this *PublicKey) EqualVT(that *PublicKey) bool
- func (x *PublicKey) GetData() []byte
- func (x *PublicKey) GetKeyType() KeyType
- func (x *PublicKey) MarshalJSON() ([]byte, error)
- func (x *PublicKey) MarshalProtoJSON(s *json.MarshalState)
- func (x *PublicKey) MarshalProtoText() string
- func (m *PublicKey) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *PublicKey) MarshalToVT(dAtA []byte) (int, error)
- func (m *PublicKey) MarshalVT() (dAtA []byte, err error)
- func (*PublicKey) ProtoMessage()
- func (x *PublicKey) Reset()
- func (m *PublicKey) SizeVT() (n int)
- func (x *PublicKey) String() string
- func (x *PublicKey) UnmarshalJSON(b []byte) error
- func (x *PublicKey) UnmarshalProtoJSON(s *json.UnmarshalState)
- func (m *PublicKey) UnmarshalVT(dAtA []byte) error
Constants ¶
const Ed25519 = int(KeyType_Ed25519)
Ed25519 is the Ed25519 key type constant for use with GenerateKeyPair.
Variables ¶
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") )
var ( KeyType_name = map[int32]string{ 0: "RSA", 1: "Ed25519", } KeyType_value = map[string]int32{ "RSA": 0, "Ed25519": 1, } )
Enum value maps for KeyType.
var PrivKeyUnmarshallers = map[KeyType]PrivKeyUnmarshaller{ KeyType_Ed25519: UnmarshalEd25519PrivateKey, }
PrivKeyUnmarshallers is a map of unmarshallers by key type.
var PubKeyUnmarshallers = map[KeyType]PubKeyUnmarshaller{ KeyType_Ed25519: UnmarshalEd25519PublicKey, }
PubKeyUnmarshallers is a map of unmarshallers by key type.
Functions ¶
func ConfigDecodeKey ¶
ConfigDecodeKey decodes from b64 (for config file) to a byte array that can be unmarshalled.
func ConfigEncodeKey ¶
ConfigEncodeKey encodes a marshalled key to b64 (for config file).
func GenerateEd25519Key ¶
GenerateEd25519Key generates a new ed25519 private and public key pair.
func GenerateKeyPair ¶
GenerateKeyPair generates a private and public key.
func GenerateKeyPairWithReader ¶
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 ¶
MarshalPrivateKey converts a key object into its protobuf serialized form.
func MarshalPublicKey ¶
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.
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).
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.
func (KeyType) MarshalJSON ¶
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 (KeyType) MarshalText ¶
MarshalText marshals the KeyType to text.
func (*KeyType) UnmarshalJSON ¶
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 ¶
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 ¶
UnmarshalEd25519PrivateKey returns a private key from input bytes.
func UnmarshalPrivateKey ¶
UnmarshalPrivateKey converts a protobuf serialized private key into its representative object.
type PrivKeyUnmarshaller ¶
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 ¶
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 ¶
PublicKeyFromProto converts an unserialized protobuf PublicKey message into its representative object.
func UnmarshalEd25519PublicKey ¶
UnmarshalEd25519PublicKey returns a public key from input bytes.
func UnmarshalPublicKey ¶
UnmarshalPublicKey converts a protobuf serialized public key into its representative object.
type PubKeyUnmarshaller ¶
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 ¶
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) EqualMessageVT ¶
func (*PublicKey) GetKeyType ¶
func (*PublicKey) MarshalJSON ¶
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 (*PublicKey) MarshalToSizedBufferVT ¶
func (*PublicKey) ProtoMessage ¶
func (*PublicKey) ProtoMessage()
func (*PublicKey) UnmarshalJSON ¶
UnmarshalJSON unmarshals the PublicKey from JSON.
func (*PublicKey) UnmarshalProtoJSON ¶
func (x *PublicKey) UnmarshalProtoJSON(s *json.UnmarshalState)
UnmarshalProtoJSON unmarshals the PublicKey message from JSON.