crypto

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NonceBytes is the length of GCM nonce.
	NonceBytes = 12

	// KeyBytes is the length of GCM key.
	KeyBytes = 32
)
View Source
const (
	AnysyncSpacePath = "m/SLIP-0021/anysync/space"
	AnysyncTreePath  = "m/SLIP-0021/anysync/tree/%s"
)

Variables

View Source
var (
	ErrInvalidWordCount = errors.New("error invalid word count for mnemonic")
	ErrInvalidMnemonic  = errors.New("error invalid mnemonic")
)
View Source
var ErrIncorrectKeyType = errors.New("incorrect key type")
View Source
var ErrX25519DecryptionFailed = errors.New("failed decryption with x25519 key")

Functions

func DecodeBytesFromString

func DecodeBytesFromString(str string) (bytes []byte, err error)

func DecodeKeyFromString

func DecodeKeyFromString[T Key](str string, construct func([]byte) (T, error), def T) (T, error)

func DecryptX25519

func DecryptX25519(privKey, pubKey *[32]byte, encrypted []byte) ([]byte, error)

DecryptX25519 takes a x25519 private and public key and decrypts the message

func Ed25519PrivateKeyToCurve25519

func Ed25519PrivateKeyToCurve25519(pk ed25519.PrivateKey) []byte

Ed25519PrivateKeyToCurve25519 converts an Ed25519 private key to a Curve25519 private key This code is originally taken from here https://github.com/jorrizza/ed2curve25519/blob/master/ed2curve25519.go

func Ed25519PublicKeyToCurve25519

func Ed25519PublicKeyToCurve25519(pk ed25519.PublicKey) []byte

Ed25519PublicKeyToCurve25519 converts an Ed25519 public key to a Curve25519 public key

func EncodeBytesToString

func EncodeBytesToString(bytes []byte) string

func EncodeKeyToString

func EncodeKeyToString[T Key](key T) (str string, err error)

func EncryptX25519

func EncryptX25519(pubKey *[32]byte, msg []byte) []byte

EncryptX25519 takes a x25519 public key and encrypts the message

func GenerateEd25519Key

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

GenerateEd25519Key generates a new ed25519 private and public key pair.

func GenerateRandomEd25519KeyPair

func GenerateRandomEd25519KeyPair() (PrivKey, PubKey, error)

func IdFromSigningPubKey

func IdFromSigningPubKey(pubKey PubKey) (peer.ID, error)

func KeyEquals

func KeyEquals(k1, k2 Key) bool

Types

type AESKey

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

func NewAES

func NewAES() *AESKey

NewAES returns AESKey if err is nil and panics otherwise.

func NewRandomAES

func NewRandomAES() (*AESKey, error)

NewRandomAES returns a random key.

func UnmarshallAESKey

func UnmarshallAESKey(k []byte) (*AESKey, error)

UnmarshallAESKey returns a key by decoding bytes.

func UnmarshallAESKeyProto added in v0.2.15

func UnmarshallAESKeyProto(k []byte) (*AESKey, error)

UnmarshallAESKeyProto returns a key by decoding bytes.

func UnmarshallAESKeyString

func UnmarshallAESKeyString(k string) (*AESKey, error)

UnmarshallAESKeyString returns a key by decoding a base32-encoded string.

func (*AESKey) Bytes

func (k *AESKey) Bytes() []byte

Bytes returns raw key bytes.

func (*AESKey) Decrypt

func (k *AESKey) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt uses key to perform AES-256 GCM decryption on ciphertext.

func (*AESKey) Encrypt

func (k *AESKey) Encrypt(plaintext []byte) ([]byte, error)

Encrypt performs AES-256 GCM encryption on plaintext.

func (*AESKey) Equals

func (k *AESKey) Equals(key Key) bool

func (*AESKey) Marshall added in v0.2.15

func (k *AESKey) Marshall() ([]byte, error)

Marshall marshalls the key into proto

func (*AESKey) Raw

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

func (*AESKey) String

func (k *AESKey) String() string

String returns the base32-encoded string representation of raw key bytes.

type DerivationResult

type DerivationResult struct {
	// m/44'/code'/index'
	MasterKey PrivKey
	// m/44'/code'/index'/0'
	Identity      PrivKey
	OldAccountKey PrivKey
	MasterNode    slip10.Node

	// Anytype uses ED25519
	// Ethereum and Bitcoin use ECDSA secp256k1 elliptic curves
	//
	// this key is used to sign ethereum transactions to use Any Naming Service
	// same mnemonic/seed phrase is used as for AnyType identity
	// m/44'/60'/0'/0/index
	EthereumIdentity ecdsa.PrivateKey
}

type Ed25519PrivKey

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

Ed25519PrivKey is an ed25519 private key.

func (*Ed25519PrivKey) Decrypt

func (k *Ed25519PrivKey) Decrypt(msg []byte) ([]byte, error)

Decrypt decrypts the message

func (*Ed25519PrivKey) Equals

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

Equals compares two ed25519 private keys.

func (*Ed25519PrivKey) GetPublic

func (k *Ed25519PrivKey) GetPublic() PubKey

GetPublic returns an ed25519 public key from a private key.

func (*Ed25519PrivKey) LibP2P

func (k *Ed25519PrivKey) LibP2P() (crypto.PrivKey, error)

LibP2P converts the key to libp2p format

func (*Ed25519PrivKey) Marshall

func (k *Ed25519PrivKey) Marshall() ([]byte, error)

Marshall marshalls the key into proto

func (*Ed25519PrivKey) Raw

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

Raw private key bytes.

func (*Ed25519PrivKey) Sign

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

Sign returns a signature from an input message.

type Ed25519PubKey

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

Ed25519PubKey is an ed25519 public key.

func (*Ed25519PubKey) Account

func (k *Ed25519PubKey) Account() string

Account returns string representation of key in anytype account format

func (*Ed25519PubKey) Encrypt

func (k *Ed25519PubKey) Encrypt(msg []byte) (data []byte, err error)

Encrypt message

func (*Ed25519PubKey) Equals

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

Equals compares two ed25519 public keys.

func (*Ed25519PubKey) LibP2P

func (k *Ed25519PubKey) LibP2P() (crypto.PubKey, error)

LibP2P converts the key to libp2p format

func (*Ed25519PubKey) Marshall

func (k *Ed25519PubKey) Marshall() ([]byte, error)

Marshall marshalls the key into proto

func (*Ed25519PubKey) Network

func (k *Ed25519PubKey) Network() string

func (*Ed25519PubKey) PeerId

func (k *Ed25519PubKey) PeerId() string

PeerId returns string representation of key for peer id

func (*Ed25519PubKey) Raw

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

Raw public key bytes.

func (*Ed25519PubKey) Storage

func (k *Ed25519PubKey) Storage() []byte

Storage returns underlying byte storage

func (*Ed25519PubKey) Verify

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

Verify checks a signature agains the input data.

type Key

type Key interface {
	// Equals returns if the keys are equal
	Equals(Key) bool

	// Raw returns raw key
	Raw() ([]byte, error)
}

Key is an abstract interface for all types of keys

type KeyStorage

type KeyStorage interface {
	PubKeyFromProto(protoBytes []byte) (PubKey, error)
}

func NewKeyStorage

func NewKeyStorage() KeyStorage

type Mnemonic

type Mnemonic string

func (Mnemonic) Bytes

func (m Mnemonic) Bytes() ([]byte, error)

func (Mnemonic) DeriveKeys

func (m Mnemonic) DeriveKeys(index uint32) (res DerivationResult, err error)

func (Mnemonic) Seed

func (m Mnemonic) Seed() ([]byte, error)

type MnemonicGenerator

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

func NewMnemonicGenerator

func NewMnemonicGenerator() MnemonicGenerator

func (MnemonicGenerator) WithEntropy

func (g MnemonicGenerator) WithEntropy(b []byte) (Mnemonic, error)

func (MnemonicGenerator) WithRandomEntropy

func (g MnemonicGenerator) WithRandomEntropy(size int) (Mnemonic, error)

func (MnemonicGenerator) WithWordCount

func (g MnemonicGenerator) WithWordCount(wc int) (Mnemonic, error)

type PrivKey

type PrivKey interface {
	Key

	// Decrypt decrypts the message and returns the result
	Decrypt(message []byte) ([]byte, error)
	// Sign signs the raw bytes and returns the signature
	Sign([]byte) ([]byte, error)
	// GetPublic returns the associated public key
	GetPublic() PubKey
	// Marshall wraps key in proto encoding and marshalls it
	Marshall() ([]byte, error)
	// LibP2P returns libp2p model
	LibP2P() (crypto.PrivKey, error)
}

PrivKey is an interface for keys that should be used for signing and decryption

func NewEd25519PrivKey

func NewEd25519PrivKey(privKey ed25519.PrivateKey) PrivKey

func NewSigningEd25519PrivKeyFromBytes

func NewSigningEd25519PrivKeyFromBytes(bytes []byte) (PrivKey, error)

func UnmarshalEd25519PrivateKey

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

UnmarshalEd25519PrivateKey returns a private key from input bytes.

func UnmarshalEd25519PrivateKeyProto

func UnmarshalEd25519PrivateKeyProto(bytes []byte) (PrivKey, error)

type PubKey

type PubKey interface {
	Key

	// Encrypt encrypts the message and returns the result
	Encrypt(message []byte) ([]byte, error)
	// Verify verifies the signed message and the signature
	Verify(data []byte, sig []byte) (bool, error)
	// Marshall wraps key in proto encoding and marshalls it
	Marshall() ([]byte, error)
	// Storage returns underlying key storage
	Storage() []byte
	// Account returns string representation for anytype account
	Account() string
	// Network returns string representation for anytype network
	Network() string
	// PeerId returns string representation for peer id
	PeerId() string
	// LibP2P returns libp2p model
	LibP2P() (crypto.PubKey, error)
}

PubKey is the public key used to verify the signatures and decrypt messages

func DecodeAccountAddress

func DecodeAccountAddress(address string) (PubKey, error)

func DecodePeerId

func DecodePeerId(peerId string) (PubKey, error)

func NewEd25519PubKey

func NewEd25519PubKey(pubKey ed25519.PublicKey) PubKey

func NewSigningEd25519PubKeyFromBytes

func NewSigningEd25519PubKeyFromBytes(bytes []byte) (PubKey, error)

func UnmarshalEd25519PublicKey

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

UnmarshalEd25519PublicKey returns a public key from input bytes.

func UnmarshalEd25519PublicKeyProto

func UnmarshalEd25519PublicKeyProto(bytes []byte) (PubKey, error)

type SymKey

type SymKey interface {
	Key

	// Decrypt decrypts the message and returns the result
	Decrypt(message []byte) ([]byte, error)
	// Encrypt encrypts the message and returns the result
	Encrypt(message []byte) ([]byte, error)
	// Marshall wraps key in proto encoding and marshalls it
	Marshall() ([]byte, error)
}

func DeriveSymmetricKey

func DeriveSymmetricKey(seed []byte, path string) (SymKey, error)

DeriveSymmetricKey derives a symmetric key from seed and path using slip-21

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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