public

package
v0.0.0-...-0ca8a0b Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package public contains utilities for public key encryption with Curve25519 and digital signatures with Ed25519.

Index

Constants

View Source
const (
	PrivateType            = "CRYPTUTIL PRIVATE KEY"
	PublicType             = "CRYPTUTIL PUBLIC KEY"
	EncryptedType          = "CRYPTUTIL ENCRYPTED MESSAGE"
	SignatureType          = "CRYPTUTIL SIGNATURE"
	SignedAndEncryptedType = "CRYPTUTIL SIGNED AND ENCRYPTED MESSAGE"
)

These types are used in PEM-encoded keys.

Variables

View Source
var (
	ErrCorruptPrivateKey = errors.New("public: private key is corrupt")
	ErrCorruptPublicKey  = errors.New("public: public key is corrupt")
)

These errors are used to signal invalid public and private keys.

Functions

func Decrypt

func Decrypt(priv *PrivateKey, enc []byte) (message []byte, ok bool)

Decrypt opens the secured message using the private key.

func DecryptAndVerify

func DecryptAndVerify(priv *PrivateKey, pub *PublicKey, enc []byte) ([]byte, bool)

DecryptAndVerify decrypts the message and verifies its signature.

func Encrypt

func Encrypt(pub *PublicKey, message []byte) (out []byte, ok bool)

Encrypt generates an ephemeral curve25519 key pair and encrypts a new message to the peer's public key.

func EncryptAndSign

func EncryptAndSign(priv *PrivateKey, pub *PublicKey, message []byte) ([]byte, bool)

EncryptAndSign signs the message with the private key, then encrypts it to the peer's public key.

func ExportPrivate

func ExportPrivate(priv *PrivateKey, passphrase []byte) ([]byte, error)

ExportPrivate PEM-encodes the locked private key. The private key is secured with the passphrase using LockKey.

func KeyExchange

func KeyExchange(priv *PrivateKey, peer *PublicKey) []byte

KeyExchange performs an ECDH key exchange with the private and public key pairs.

func LockKey

func LockKey(priv *PrivateKey, passphrase []byte) ([]byte, bool)

LockKey secures the private key with the passphrase, using Scrypt and NaCl's secretbox.

func MarshalPrivate

func MarshalPrivate(priv *PrivateKey) ([]byte, error)

MarshalPrivate serialises a private key into a byte slice. If the key is invalid, a corrupt key error is returned.

func MarshalPublic

func MarshalPublic(pub *PublicKey) ([]byte, error)

MarshalPublic serialises a public key into a byte slice.

func Sign

func Sign(priv *PrivateKey, message []byte) ([]byte, bool)

Sign signs the message with the private key using Ed25519.

func Verify

func Verify(pub *PublicKey, message []byte, sig []byte) bool

Verify verifies the signature on the message with the public key using Ed25519.

Types

type PrivateKey

type PrivateKey struct {
	D *[32]byte // Decryption key (encryption private key).
	S *[64]byte // Signature key (signing private key).
	*PublicKey
}

A PrivateKey contains both encryption and signing keys.

func GenerateKey

func GenerateKey() (*PrivateKey, error)

GenerateKey creates a new set of encryption and signature keys using the operating system's random number generator.

func ImportPrivate

func ImportPrivate(enc, passphrase []byte) (*PrivateKey, error)

ImportPrivate parses a PEM-encoded private key. UnlockKey is called on the contents of the PEM-encoded file.

func UnlockKey

func UnlockKey(locked, passphrase []byte) (*PrivateKey, bool)

UnlockKey recovers the secured private key with the passphrase.

func UnmarshalPrivate

func UnmarshalPrivate(in []byte) (*PrivateKey, error)

UnmarshalPrivate parses a byte slice into a private key.

func (*PrivateKey) Valid

func (priv *PrivateKey) Valid() bool

Valid runs checks to make sure the private key is valid.

func (*PrivateKey) Zero

func (priv *PrivateKey) Zero()

Zero clears out the private key. The public key components will remain intact.

type PublicKey

type PublicKey struct {
	E *[32]byte // Encryption key (encryption public key).
	V *[32]byte // Verification key (decryption public key).
}

A PublicKey contains the public components of the key pair.

func UnmarshalPublic

func UnmarshalPublic(in []byte) (*PublicKey, error)

UnmarshalPublic parses a byte slice into a public key.

func (*PublicKey) Valid

func (pub *PublicKey) Valid() bool

Valid ensures the public key is a valid public key.

Jump to

Keyboard shortcuts

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