Documentation
¶
Index ¶
- Variables
- func Copy(dst io.Writer, src io.Reader, key Key, nonce Nonce, aad []byte, mode Mode, ...) error
- func CopyBuffer(dst io.Writer, src io.Reader, key Key, nonce Nonce, aad []byte, mode Mode, ...) error
- type Algorithm
- func (Algorithm) KeySize() int
- func (a Algorithm) NewKey(bytes []byte) (*Key, error)
- func (a Algorithm) NewNonce(bytes []byte) (*Nonce, error)
- func (Algorithm) NewRandomKey() *Key
- func (a Algorithm) NewRandomNonce() *Nonce
- func (a Algorithm) NonceSize() int
- func (Algorithm) Overhead() int
- func (a Algorithm) String() string
- type Key
- type Mode
- type Nonce
- type TagVerificationFailedError
- type Writer
- type WrongKeySizeError
- type WrongNonceSizeError
Constants ¶
This section is empty.
Variables ¶
var ChaCha20Poly1305 = Algorithm{"ChaCha20-Poly1305", 12} //nolint:gochecknoglobals // this is a constant (kinda)
ChaCha20Poly1305 is the Algorithm instance representing ChaCha20-Poly1305.
var XChaCha20Poly1305 = Algorithm{"XChaCha20-Poly1305", 24} //nolint:gochecknoglobals // this is a constant (kinda)
XChaCha20Poly1305 is the Algorithm instance representing XChaCha20-Poly1305.
Functions ¶
func Copy ¶
func Copy(dst io.Writer, src io.Reader, key Key, nonce Nonce, aad []byte, mode Mode, buffer []byte) error
Copy calls CopyBuffer allocating a new buffer.
func CopyBuffer ¶
func CopyBuffer(dst io.Writer, src io.Reader, key Key, nonce Nonce, aad []byte, mode Mode, buffer []byte) error
CopyBuffer processes (encrypts/decrypts) the entire contents from src and writes the resulting ciper/cleartext into dst. As errors are obviously non-recoverable, this function doesn't return the number of bytes written (unlike io.CopyBuffer).
Types ¶
type Algorithm ¶
type Algorithm struct {
// contains filtered or unexported fields
}
Algorithm represents and algorithm variant (ChaCha20-Poly1305 or XChaCha20-Poly1305); it provides some metadata and constcutors for keys and nonces.
Rather than instantiating this type, use one of the provided instances.
func (Algorithm) KeySize ¶
KeySize gives the algorithm's required byte length for keys, which is 32.
func (Algorithm) NewKey ¶
NewKey creates a Key copying the bytes in the give slice, whose length must match the length required by the algorithm (ie. 32).
Returns a WrongKeySizeError if the slice is the wrong size.
func (Algorithm) NewNonce ¶
NewNonce creates a Nonce copying the bytes in the give slice, whose length must match the length required by the algorithm (ie. 12 or 24).
Returns WrongNonceSizeError if the slice is the wrong size.
func (Algorithm) NewRandomKey ¶
NewRandomKey returns a random Key.
func (Algorithm) NewRandomNonce ¶
NewRandomNonce returns a random Nonce.
func (Algorithm) NonceSize ¶
NonceSize gives the algorithm's required byte length for nonces, which is 12 for ChaCha20-Poly1305 and 24 for XChaCha20-Poly1305.
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
func (Key) AppendBytes ¶
AppendBytes appends the key's bytes to the given slice, returning the resulting slice.
type Nonce ¶
type Nonce struct {
// contains filtered or unexported fields
}
func (Nonce) AppendBytes ¶
AppendBytes appends the nonce's bytes to the given slice, returning the resulting slice.
type TagVerificationFailedError ¶
type TagVerificationFailedError struct{}
func (TagVerificationFailedError) Error ¶
func (TagVerificationFailedError) Error() string
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a io.WriteCloser (and io.ReeaderFrom) that encrypts or decrypts data according to ChaCha20-Poly1305 (see rfc8439) and writes it to an underlying io.Writer.
func NewWriter ¶
func NewWriter(key Key, nonce Nonce, additionalData []byte, mode Mode, writer io.Writer, buffer []byte) *Writer
NewWriter creates a new Writer that wraps the provided one.
The writer will use will use ChaCha20-POly1305 or XChaCha20-POly1305 depending on the provided nonce.
The provided buffer will be used during I/O only (ie. ReadFrom() and Write()) and can be shared or used for other purposes as long it's not used concurrently.
func (*Writer) Close ¶
Close finalizes the poly1305 MAC, writing it to the underlying writer if encrypting, and verifying it's the expected one if decrypting.
When encrypting, this will fail if the underlying Writer returns an error, which will be returned untouched.
When decrypting, this will fail with a TagVerificationFailedError if the Poly1305 tag at the end of the ciphertext does not match the one computed while decrypting.
After an error occurrs here, in Write(), or in WriteFrom() This function wil consistently return the same error (wrapped).
func (*Writer) ReadFrom ¶
ReadFrom reads data from r until EOF or error and returns the number of bytes read (see io.ReaderFrom).
This will fail if an error is returned while reading from the given Reader, or while writitng to the underlying Writer. In either case, any error which will be returned untouched (except EOF from the Reader, of course).
After an error occurrs here, in Write(), or in Close() This function wil consistently return the same error (wrapped).
func (*Writer) Write ¶
Write encrypts/decrypts the provided bytes and writes them to the underlying writer.
When encrypting, all provided bytes will be encrypted and written to the underlying writer.
When decrypting, the last 16 bytes of ciphertext are the expected poly1305 MAC and so a call to Write() will always keep the latest 16 bytes aside for this purpose. These 16 bytes will not be written to the underlying Writer(this function will still report them as read, of course).
This will fail if the underlying Writer returns an error, which will be returned untouched.
After an error occurrs here, in WriteFrom(), or in Close() This function wil consistently return the same error (wrapped).
type WrongKeySizeError ¶
type WrongKeySizeError struct {
// contains filtered or unexported fields
}
func (WrongKeySizeError) Error ¶
func (e WrongKeySizeError) Error() string
type WrongNonceSizeError ¶
type WrongNonceSizeError struct {
// contains filtered or unexported fields
}
func (WrongNonceSizeError) Error ¶
func (e WrongNonceSizeError) Error() string