core

package
v0.0.0-...-80327fb Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PreBodySize      = 1 + 1
	FixedMessageSize = 16384 // 2^14
)

Message and message field sizes.

View Source
const (
	FixedOverhead         = 32 + 64 + 24 + 16
	FixedCipherSize       = FixedMessageSize - PreBodySize - FixedOverhead
	StreamCipherBlockSize = 65536 // 2^16
	SigSize               = ed25519.SignatureSize
	NodeIDSize            = ed25519.PublicKeySize
)

Cryptographic field sizes.

View Source
const (
	NetworkIDSize = 4
	RPCIDSize     = 20
	DynXSize      = 64 // SHA3-512 output size, checked in init
	HdrNonceSize  = 16

	HeaderSize = NetworkIDSize + 1 + NodeIDSize + DynXSize + net.IPv6len +
		2 + RPCIDSize + 8 + HdrNonceSize
)

Header and header field sizes.

View Source
const (
	KeySize = sha512.Size

	NodeTripleSize = NodeIDSize + net.IPv6len + 2

	StorePayloadSize           = KeySize + 8
	FindNodePayloadSize        = 1 + NodeIDSize
	FindNodeRespPayloadMinSize = 1
	FindValuePayloadSize       = KeySize + 8
	ErrorPayloadMinSize        = 1 + 1
)

Payload and payload field sizes.

View Source
const (
	KindFixed uint8 = iota
	KindStream
)

Supported message body kinds.

View Source
const (
	TypePing uint8 = iota
	TypeStore
	TypeData
	TypeFindNode
	TypeFindNodeResp
	TypeFindValue
	TypeError
)

Supported payload types.

View Source
const (
	Version = 0
)

Most recent (supported) PROTOCOL version.

Variables

View Source
var SupportedVersions = map[uint8]bool{
	Version: true,
}

SupportedVersions gives a hash set of supported PROTOCOL versions

Functions

func LCP

func LCP(x, y []byte) int

LCP computes the longest common prefix, in bits, between two node IDs. Panics if the length of x or y is incorrect.

Types

type DataPayload

type DataPayload struct {
	Length uint64
	Value  io.Reader
}

DataPayload satisfies StreamPayload for the DATA message payload format.

func (*DataPayload) BodyKind

func (data *DataPayload) BodyKind() uint8

func (*DataPayload) MarshalStream

func (data *DataPayload) MarshalStream(w io.Writer) error

func (*DataPayload) MsgType

func (data *DataPayload) MsgType() uint8

func (*DataPayload) UnmarshalStream

func (data *DataPayload) UnmarshalStream(r io.Reader) error

type ErrorPayload

type ErrorPayload struct {
	Msg []byte
}

ErrorPayload satisfies FixedPayload for the ERROR message payload format.

func (*ErrorPayload) BodyKind

func (er *ErrorPayload) BodyKind() uint8

func (*ErrorPayload) MarshalBinary

func (er *ErrorPayload) MarshalBinary() ([]byte, error)

func (*ErrorPayload) MsgType

func (er *ErrorPayload) MsgType() uint8

func (*ErrorPayload) UnmarshalBinaryN

func (er *ErrorPayload) UnmarshalBinaryN(data []byte) (int, error)

type FindNodePayload

type FindNodePayload struct {
	Count  uint8
	Target []byte
}

FindNodePayload satisfies FixedPayload for the FIND_NODE message payload format.

func (*FindNodePayload) BodyKind

func (fnode *FindNodePayload) BodyKind() uint8

func (*FindNodePayload) MarshalBinary

func (fnode *FindNodePayload) MarshalBinary() ([]byte, error)

func (*FindNodePayload) MsgType

func (fnode *FindNodePayload) MsgType() uint8

func (*FindNodePayload) UnmarshalBinaryN

func (fnode *FindNodePayload) UnmarshalBinaryN(data []byte) (int, error)

type FindNodeRespPayload

type FindNodeRespPayload struct {
	Nodes []*NodeTriple
}

FindNodeRespPayload satisfies FixedPayload for the FIND_NODE_RESP message payload format.

func (*FindNodeRespPayload) BodyKind

func (fnresp *FindNodeRespPayload) BodyKind() uint8

func (*FindNodeRespPayload) MarshalBinary

func (fnresp *FindNodeRespPayload) MarshalBinary() ([]byte, error)

func (*FindNodeRespPayload) MsgType

func (fnresp *FindNodeRespPayload) MsgType() uint8

func (*FindNodeRespPayload) UnmarshalBinaryN

func (fnresp *FindNodeRespPayload) UnmarshalBinaryN(data []byte) (int, error)

type FindValuePayload

type FindValuePayload struct {
	Key []byte
}

FindValuePayload satisfies FixedPayload for the FIND_VALUE message payload format.

func (*FindValuePayload) BodyKind

func (fval *FindValuePayload) BodyKind() uint8

func (*FindValuePayload) MarshalBinary

func (fval *FindValuePayload) MarshalBinary() ([]byte, error)

func (*FindValuePayload) MsgType

func (fval *FindValuePayload) MsgType() uint8

func (*FindValuePayload) UnmarshalBinaryN

func (fval *FindValuePayload) UnmarshalBinaryN(data []byte) (int, error)

type FixedPayload

type FixedPayload interface {
	MessagePayload
	MarshalBinary() (data []byte, err error)
	UnmarshalBinaryN(data []byte) (n int, err error)
}

FixedPayload is used for fixed-format messages. FixedPayload can be marshalled and unmarshalled with a fixed-size byte slice.

type Header struct {
	NetworkID []byte
	MsgType   uint8
	ID        []byte
	PuzDynX   []byte
	IP        net.IP
	Port      uint16
	RPCID     []byte
	Time      uint64
}

Header implements the message header format.

func (*Header) MarshalBinary

func (hdr *Header) MarshalBinary() ([]byte, error)

func (*Header) UnmarshalBinary

func (hdr *Header) UnmarshalBinary(data []byte) error

type Message

type Message struct {
	Version  uint8
	BodyKind uint8
	Hdr      *Header
	Payload  MessagePayload
}

Message implements the message format.

type MessageCodec

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

MessageCodec implements encryption and decryption of fixed and stream-format messages. It is safe for concurrent use.

func NewMessageCodec

func NewMessageCodec(c1, c2 int) *MessageCodec

NewMessageCodec constructs a MessageCodec with the c1 static puzzle constant and c2 dynamic puzzle constant. A given codec M is compatible with another codec N if M.C1 >= N.C1 and M.C2 >= N.C2.

func (*MessageCodec) DecodeFixed

func (codec *MessageCodec) DecodeFixed(msg *Message, data, priv []byte) error

DecodeFixed decodes data to a fixed-format msg.

func (*MessageCodec) DecodeStream

func (codec *MessageCodec) DecodeStream(msg *Message, r io.Reader, priv []byte) error

DecodeStream decodes from stream-format r to msg.

func (*MessageCodec) EncodeFixed

func (codec *MessageCodec) EncodeFixed(msg *Message, priv, targetPubl []byte) ([]byte, error)

EncodeFixed returns the fixed-format encoding of msg.

func (*MessageCodec) EncodeStream

func (codec *MessageCodec) EncodeStream(msg *Message, w io.Writer, priv, targetPubl []byte) error

EncodeStream writes the stream-format encoding of msg to w.

func (*MessageCodec) NewNodeID

func (codec *MessageCodec) NewNodeID() (publ ed25519.PublicKey, priv ed25519.PrivateKey, x []byte, err error)

NewNodeID generates a keypair where the public key (node ID) satisfies the codec's crypto puzzle constants and may be used in codec operations.

type MessagePayload

type MessagePayload interface {
	BodyKind() uint8
	MsgType() uint8
}

MessagePayload is used to identify the message body kind and message type of a payload.

type NodeTriple

type NodeTriple struct {
	ID   []byte
	IP   net.IP
	Port uint16
}

NodeTriple describes a node using only the fields necessary to contact it.

func (*NodeTriple) MarshalSlice

func (n *NodeTriple) MarshalSlice(data []byte) error

func (*NodeTriple) UnmarshalSlice

func (n *NodeTriple) UnmarshalSlice(data []byte) error

type PingPayload

type PingPayload struct {
}

PingPayload satisfies FixedPayload for the PING message payload format.

func (*PingPayload) BodyKind

func (ping *PingPayload) BodyKind() uint8

func (*PingPayload) MarshalBinary

func (ping *PingPayload) MarshalBinary() ([]byte, error)

func (*PingPayload) MsgType

func (ping *PingPayload) MsgType() uint8

func (*PingPayload) UnmarshalBinaryN

func (ping *PingPayload) UnmarshalBinaryN(data []byte) (int, error)

type StorePayload

type StorePayload struct {
	Key    []byte
	Length uint64
}

StorePayload satisfies FixedPayload for the STORE message payload format.

func (*StorePayload) BodyKind

func (store *StorePayload) BodyKind() uint8

func (*StorePayload) MarshalBinary

func (store *StorePayload) MarshalBinary() ([]byte, error)

func (*StorePayload) MsgType

func (store *StorePayload) MsgType() uint8

func (*StorePayload) UnmarshalBinaryN

func (store *StorePayload) UnmarshalBinaryN(data []byte) (int, error)

type StreamPayload

type StreamPayload interface {
	MessagePayload
	MarshalStream(w io.Writer) (err error)
	UnmarshalStream(r io.Reader) (err error)
}

StreamPayload is used for stream-format messages. StreamPayload can be marshalled and unmarshalled with io.Writer and io.Reader streams respectively.

Directories

Path Synopsis
Package crypto provides tools for encrypting and decrypting data in fixed and stream formats, and for converting Ed25519 keys to X25519 keys.
Package crypto provides tools for encrypting and decrypting data in fixed and stream formats, and for converting Ed25519 keys to X25519 keys.

Jump to

Keyboard shortcuts

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