Documentation
¶
Overview ¶
Package cipher implements the cipher functions specified in the noise protocol.
It currently supports two ciphers:
- ChaCha20Poly1350, which uses https://golang.org/x/crypto/chacha20poly1305.
- AESGCM, which uses https://pkg.go.dev/crypto/aes. Be cautious when using this cipher as it might be vunlerable to side channel attack.
Index ¶
Examples ¶
Constants ¶
View Source
const ( // KeySize defines the size of the cipher key, in bytes. KeySize = 32 // MaxNonce is an 8-byte unsigned integer and equals to 2^64-1. MaxNonce = ^uint64(0) )
Variables ¶
Functions ¶
func SupportedCiphers ¶
func SupportedCiphers() string
SupportedCiphers gives the names of all the ciphers registered. If no new ciphers are registered, it returns a string as "AESGCM, ChaChaPoly", orders not preserved.
Types ¶
type AEAD ¶
type AEAD interface { fmt.Stringer // Cipher returns a cipher.AEAD. This function enforces that any cipher // implement this AEAD interface must also satisfy the cipher.AEAD. Cipher() cipher.AEAD // Decrypt uses a cipher key k of 32 bytes, an 8-byte unsigned integer nonce // n, and associated data ad, and returns the plaintext, unless // authentication fails, in which case an error is returned. Decrypt(n uint64, ad, ciphertext []byte) ([]byte, error) // EncodeNonce turns the nonce used in the noise protocol into a format // that's accepted by the specific cipher specs. EncodeNonce(n uint64) []byte // Encrypt uses the cipher key k of 32 bytes and an 8-byte unsigned integer // nonce n which must be unique for the key k, and returns the ciphertext. // Encryption must be done with an "AEAD" encryption mode with the // associated data ad and returns a ciphertext that is the same size as the // plaintext plus 16 bytes for authentication data. Encrypt(n uint64, ad, plaintext []byte) ([]byte, error) // InitCipher creates a cipher with the secret key. InitCipher(key [KeySize]byte) error // Rekey creates a new 32-byte cipher key as a pseudorandom function of key. // It returns the first 32 bytes from calling Encrypt with, // - n as maxnonce, which equals 2^64-1, // - ad as zerolen, a zero-length byte sequence, // - plaintext as zeros, a sequence of 32 bytes filled with zeros. Rekey() [KeySize]byte // Reset cleans all the states to zero value, if any. Reset() }
AEAD specifies an interface for building a cipher used by the babbel package.
func FromString ¶
FromString uses the provided cipher name, s, to query a built-in cipher.
Example ¶
package main import ( "fmt" "github.com/yyforyongyu/babble/cipher" ) func main() { // load cipher AESGCM aesgcm, _ := cipher.FromString("AESGCM") fmt.Println(aesgcm) // load cipher ChaChaPoly ccp, _ := cipher.FromString("ChaChaPoly") fmt.Println(ccp) }
Output:
Click to show internal directories.
Click to hide internal directories.