crypto

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 22 Imported by: 1

Documentation

Overview

Package crypto implements a few abstractions around the go crypto packages to manage encryption keys, encrypt small data, and streams.

Index

Constants

View Source
const (
	AES256               int = iota // AES256-GCM, AES256-CBC+HMAC-SHA256, PBKDF2.
	Chacha20Poly1305                // Chacha20Poly1305, Argon2.
	AES256WithTPMRSA2048            // Like AES256, with RSA2048 masterkey on TPM.

	DefaultAlgo = AES256
	PickFastest = -1
)

Variables

View Source
var (
	// Indicates that the ciphertext could not be decrypted.
	ErrDecryptFailed = errors.New("decryption failed")
	// Indicates that the plaintext could not be encrypted.
	ErrEncryptFailed = errors.New("encryption failed")
	// Indicates an invalid alg value.
	ErrUnexpectedAlgo = errors.New("unexpected algorithm")
)

Functions

func Fastest

func Fastest(opts ...Option) (int, error)

Fastest runs an in-memory speedtest and returns the fastest encryption algorithm on the local computer.

Types

type AESKey

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

AESKey is an encryption key that can be used to encrypt and decrypt data and streams.

func (AESKey) Decrypt

func (k AESKey) Decrypt(data []byte) ([]byte, error)

Decrypt decrypts data that was encrypted with Encrypt and the same key.

func (AESKey) DecryptKey

func (k AESKey) DecryptKey(encryptedKey []byte) (EncryptionKey, error)

DecryptKey decrypts an encrypted key.

func (AESKey) Encrypt

func (k AESKey) Encrypt(data []byte) ([]byte, error)

Encrypt encrypts data using the key.

func (AESKey) Hash

func (k AESKey) Hash(b []byte) []byte

Hash returns the HMAC-SHA256 hash of b.

func (*AESKey) Logger

func (k *AESKey) Logger() Logger

func (AESKey) NewKey

func (k AESKey) NewKey() (EncryptionKey, error)

NewKey creates a new encryption key.

func (AESKey) ReadEncryptedKey

func (k AESKey) ReadEncryptedKey(r io.Reader) (EncryptionKey, error)

ReadEncryptedKey reads an encrypted key and decrypts it.

func (AESKey) StartReader

func (k AESKey) StartReader(ctx []byte, r io.Reader) (StreamReader, error)

StartReader opens a reader to decrypt a stream of data.

func (AESKey) StartWriter

func (k AESKey) StartWriter(ctx []byte, w io.Writer) (StreamWriter, error)

StartWriter opens a writer to encrypt a stream of data.

func (*AESKey) Wipe

func (k *AESKey) Wipe()

Wipe zeros the key material.

func (AESKey) WriteEncryptedKey

func (k AESKey) WriteEncryptedKey(w io.Writer) error

WriteEncryptedKey writes the encrypted key to the writer.

type AESMasterKey

type AESMasterKey struct {
	*AESKey
}

func (AESMasterKey) Save

func (mk AESMasterKey) Save(passphrase []byte, file string) error

Save encrypts the key with passphrase and saves it to file.

type AESStreamReader

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

AESStreamReader decrypts an input stream.

func (*AESStreamReader) Close

func (r *AESStreamReader) Close() error

func (*AESStreamReader) Read

func (r *AESStreamReader) Read(b []byte) (n int, err error)

func (*AESStreamReader) Seek

func (r *AESStreamReader) Seek(offset int64, whence int) (int64, error)

Seek moves the next read to a new offset. The offset is in the decrypted stream.

type AESStreamWriter

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

AESStreamWriter encrypts a stream of data.

func (*AESStreamWriter) Close

func (w *AESStreamWriter) Close() (err error)

func (*AESStreamWriter) Write

func (w *AESStreamWriter) Write(b []byte) (n int, err error)

type Chacha20Poly1305Key

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

Chacha20Poly1305Key is an encryption key that can be used to encrypt and decrypt data and streams.

func (Chacha20Poly1305Key) Decrypt

func (k Chacha20Poly1305Key) Decrypt(data []byte) ([]byte, error)

Decrypt decrypts data that was encrypted with Encrypt and the same key.

func (Chacha20Poly1305Key) DecryptKey

func (k Chacha20Poly1305Key) DecryptKey(encryptedKey []byte) (EncryptionKey, error)

DecryptKey decrypts an encrypted key.

func (Chacha20Poly1305Key) Encrypt

func (k Chacha20Poly1305Key) Encrypt(data []byte) ([]byte, error)

Encrypt encrypts data using the key.

func (Chacha20Poly1305Key) Hash

func (k Chacha20Poly1305Key) Hash(b []byte) []byte

Hash returns the HMAC-SHA256 hash of b.

func (*Chacha20Poly1305Key) Logger

func (k *Chacha20Poly1305Key) Logger() Logger

func (Chacha20Poly1305Key) NewKey

func (k Chacha20Poly1305Key) NewKey() (EncryptionKey, error)

NewKey creates a new encryption key.

func (Chacha20Poly1305Key) ReadEncryptedKey

func (k Chacha20Poly1305Key) ReadEncryptedKey(r io.Reader) (EncryptionKey, error)

ReadEncryptedKey reads an encrypted key and decrypts it.

func (Chacha20Poly1305Key) StartReader

func (k Chacha20Poly1305Key) StartReader(ctx []byte, r io.Reader) (StreamReader, error)

StartReader opens a reader to decrypt a stream of data.

func (Chacha20Poly1305Key) StartWriter

func (k Chacha20Poly1305Key) StartWriter(ctx []byte, w io.Writer) (StreamWriter, error)

StartWriter opens a writer to encrypt a stream of data.

func (*Chacha20Poly1305Key) Wipe

func (k *Chacha20Poly1305Key) Wipe()

Wipe zeros the key material.

func (Chacha20Poly1305Key) WriteEncryptedKey

func (k Chacha20Poly1305Key) WriteEncryptedKey(w io.Writer) error

WriteEncryptedKey writes the encrypted key to the writer.

type Chacha20Poly1305MasterKey

type Chacha20Poly1305MasterKey struct {
	*Chacha20Poly1305Key
}

func (Chacha20Poly1305MasterKey) Save

func (mk Chacha20Poly1305MasterKey) Save(passphrase []byte, file string) error

Save encrypts the key with passphrase and saves it to file.

type Chacha20Poly1305StreamReader

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

Chacha20Poly1305StreamReader decrypts an input stream.

func (*Chacha20Poly1305StreamReader) Close

func (*Chacha20Poly1305StreamReader) Read

func (r *Chacha20Poly1305StreamReader) Read(b []byte) (n int, err error)

func (*Chacha20Poly1305StreamReader) Seek

func (r *Chacha20Poly1305StreamReader) Seek(offset int64, whence int) (int64, error)

Seek moves the next read to a new offset. The offset is in the decrypted stream.

type Chacha20Poly1305StreamWriter

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

Chacha20Poly1305StreamWriter encrypts a stream of data.

func (*Chacha20Poly1305StreamWriter) Close

func (w *Chacha20Poly1305StreamWriter) Close() (err error)

func (*Chacha20Poly1305StreamWriter) Write

func (w *Chacha20Poly1305StreamWriter) Write(b []byte) (n int, err error)

type EncryptionKey

type EncryptionKey interface {
	Logger() Logger

	// Encrypt encrypts data using the key.
	Encrypt(data []byte) ([]byte, error)
	// Decrypt decrypts data that was encrypted with Encrypt and the same key.
	Decrypt(data []byte) ([]byte, error)
	// Hash returns a cryptographially secure hash of b.
	Hash(b []byte) []byte
	// StartReader opens a reader to decrypt a stream of data.
	StartReader(ctx []byte, r io.Reader) (StreamReader, error)
	// StartWriter opens a writer to encrypt a stream of data.
	StartWriter(ctx []byte, w io.Writer) (StreamWriter, error)
	// NewKey creates a new encryption key.
	NewKey() (EncryptionKey, error)
	// DecryptKey decrypts an encrypted key.
	DecryptKey(encryptedKey []byte) (EncryptionKey, error)
	// ReadEncryptedKey reads an encrypted key and decrypts it.
	ReadEncryptedKey(r io.Reader) (EncryptionKey, error)
	// WriteEncryptedKey writes the encrypted key to the writer.
	WriteEncryptedKey(w io.Writer) error
	// Wipe zeros the key material.
	Wipe()
}

EncryptionKey is an encryption key that can be used to encrypt and decrypt data and streams.

type Logger

type Logger interface {
	Debug(...any)
	Debugf(string, ...any)
	Info(...any)
	Infof(string, ...any)
	Error(...any)
	Errorf(string, ...any)
	Fatal(...any)
	Fatalf(string, ...any)
}

Logger is the interface for writing debug logs.

func StdLogger

func StdLogger() Logger

type MasterKey

type MasterKey interface {
	EncryptionKey

	// Save encrypts the MasterKey with passphrase and saves it to file.
	Save(passphrase []byte, file string) error
}

MasterKey is an encryption key that is normally stored on disk encrypted with a passphrase. It is used to create file keys used to encrypt the content of files.

func CreateAESMasterKey

func CreateAESMasterKey(opts ...Option) (MasterKey, error)

CreateAESMasterKey creates a new master key.

func CreateAESMasterKeyForTest

func CreateAESMasterKeyForTest() (MasterKey, error)

CreateAESMasterKeyForTest creates a new master key to tests.

func CreateChacha20Poly1305MasterKey

func CreateChacha20Poly1305MasterKey(opts ...Option) (MasterKey, error)

CreateChacha20Poly1305MasterKey creates a new master key.

func CreateChacha20Poly1305MasterKeyForTest

func CreateChacha20Poly1305MasterKeyForTest() (MasterKey, error)

CreateChacha20Poly1305MasterKeyForTest creates a new master key to tests.

func CreateMasterKey

func CreateMasterKey(opts ...Option) (MasterKey, error)

CreateMasterKey creates a new master key.

func ReadAESMasterKey

func ReadAESMasterKey(passphrase []byte, file string, opts ...Option) (MasterKey, error)

ReadAESMasterKey reads an encrypted master key from file and decrypts it.

func ReadChacha20Poly1305MasterKey

func ReadChacha20Poly1305MasterKey(passphrase []byte, file string, opts ...Option) (MasterKey, error)

ReadChacha20Poly1305MasterKey reads an encrypted master key from file and decrypts it.

func ReadMasterKey

func ReadMasterKey(passphrase []byte, file string, opts ...Option) (MasterKey, error)

ReadMasterKey reads an encrypted master key from file and decrypts it.

type Option

type Option func(*option)

Option is used to specify the parameters of MasterKey.

func WithAlgo

func WithAlgo(alg int) Option

WithAlgo specifies the cryptographic algorithm to use.

func WithLogger

func WithLogger(l Logger) Option

WithLogger specifies the logger to use.

func WithStrictWipe

func WithStrictWipe(v bool) Option

WithStrictWipe specifies whether strict wipe is required. When enabled, keys must be wiped by calling Wipe() when they are no longer needed. Otherwise, program execution will be stopped with a fatal error.

func WithTPM added in v0.2.0

func WithTPM(tpm *tpm.TPM) Option

WithTPM specifies that the master key should be in the Trusted Platform Module (TPM). When this option is used, the data encrypted with the master key can only ever be decrypted with the same TPM.

type StreamReader

type StreamReader interface {
	io.Reader
	io.Seeker
	io.Closer
}

StreamReader decrypts a stream.

type StreamWriter

type StreamWriter interface {
	io.Writer
	io.Closer
}

StreamWriter encrypts a stream.

Jump to

Keyboard shortcuts

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