Documentation
¶
Overview ¶
Package crypto contains low-level cryptographic primitives used across the Signal protocol implementation.
Index ¶
- Constants
- Variables
- func AES256CTRHMACSHA256Decrypt(ciphertext, cipherKey, macKey []byte) ([]byte, error)
- func AES256CTRHMACSHA256Encrypt(plaintext, cipherKey, macKey []byte) ([]byte, error)
- func AESCBCDecrypt(key, iv, ciphertext []byte) ([]byte, error)
- func AESCBCEncrypt(key, iv, plaintext []byte) ([]byte, error)
- func AESGCMDecrypt(key, ciphertext, nonce, associatedData []byte) ([]byte, error)
- func AESGCMEncrypt(key, plaintext, associatedData []byte) (ciphertext, nonce []byte, err error)
- func AESGCMEncryptWithNonce(key, plaintext, nonce, associatedData []byte) ([]byte, error)
- func AESGCMSIVDecrypt(key, nonce, ciphertext, associatedData []byte) ([]byte, error)
- func AESGCMSIVEncrypt(key, nonce, plaintext, associatedData []byte) ([]byte, error)
- func ChaChaDecrypt(key, ciphertext, nonce, associatedData []byte) ([]byte, error)
- func ChaChaEncrypt(key, plaintext, associatedData []byte) (ciphertext, nonce []byte, err error)
- func ChaChaEncryptWithNonce(key, plaintext, nonce, associatedData []byte) ([]byte, error)
- func DH(privateKey, publicKey [32]byte) ([32]byte, error)
- func HKDF(inputKeyMaterial, salt, info []byte, length int) ([]byte, error)
- func HKDFExpand(prk, info []byte, length int) ([]byte, error)
- func HKDFExtract(salt, inputKeyMaterial []byte) []byte
- func HMAC256(key, data []byte) []byte
- func HMAC512(key, data []byte) []byte
- func HMACVerify(key, data, expectedMAC []byte) bool
- func IsKyber1024PublicKey(data []byte) bool
- func IsValidPublicKey(publicKey [32]byte) bool
- func Kyber1024Decapsulate(privateKey []byte, ciphertext []byte) ([]byte, error)
- func Kyber1024Encapsulate(publicKey []byte) ([]byte, []byte, error)
- func Kyber1024EncapsulateDeterministically(publicKey []byte, seed []byte) ([]byte, []byte, error)
- func RandomBytes(length int) ([]byte, error)
- func RandomScalar() ([32]byte, error)
- func SetRandReader(r io.Reader) func()
- func ValidatePublicKey(publicKey [32]byte) error
- func XEdDSASign(privateKey [32]byte, messagePieces ...[]byte) ([]byte, error)
- func XEdDSASigningPublicKey(privateKey [32]byte) ([32]byte, error)
- func XEdDSAVerify(publicKey [32]byte, signature []byte, messagePieces ...[]byte) bool
- func ZeroBytes(b []byte)
- func ZeroKey(k *[32]byte)
- type AEAD
- type AESGCM
- type ChaChaAEAD
- type KeyPair
- type KyberKeyPair
Constants ¶
const AESGCMSIVNonceSize = 12
AESGCMSIVNonceSize is the nonce size for AES-GCM-SIV.
Variables ¶
var ErrCiphertextTooShort = errors.New("aead: ciphertext too short")
ErrCiphertextTooShort is returned when the ciphertext is shorter than the nonce/tag requirements.
var ErrHKDFLength = errors.New("hkdf: invalid length")
ErrHKDFLength is returned when HKDF output length is invalid.
var ErrInvalidKey = errors.New("aead: invalid key length")
ErrInvalidKey is returned when the key length is incorrect.
var ErrInvalidLength = errors.New("random: invalid length")
ErrInvalidLength is returned when a negative or oversized length is requested.
var ErrInvalidPublicKey = errors.New("curve25519: invalid public key")
ErrInvalidPublicKey is returned when a provided Curve25519 public key is invalid or low-order.
Functions ¶
func AES256CTRHMACSHA256Decrypt ¶
AES256CTRHMACSHA256Decrypt verifies the truncated HMAC-SHA256 and decrypts AES-256-CTR ciphertext.
func AES256CTRHMACSHA256Encrypt ¶
AES256CTRHMACSHA256Encrypt encrypts plaintext with AES-256-CTR and appends a truncated HMAC-SHA256.
func AESCBCDecrypt ¶
AESCBCDecrypt decrypts ciphertext with AES-CBC and PKCS#7 unpadding.
func AESCBCEncrypt ¶
AESCBCEncrypt encrypts plaintext with AES-CBC and PKCS#7 padding.
func AESGCMDecrypt ¶
AESGCMDecrypt decrypts using AES-256-GCM with the provided nonce.
func AESGCMEncrypt ¶
AESGCMEncrypt is a convenience wrapper returning ciphertext and nonce separately.
func AESGCMEncryptWithNonce ¶
AESGCMEncryptWithNonce encrypts using AES-256-GCM with a caller-provided nonce.
func AESGCMSIVDecrypt ¶
AESGCMSIVDecrypt decrypts ciphertext using AES-GCM-SIV with the provided nonce and associated data.
func AESGCMSIVEncrypt ¶
AESGCMSIVEncrypt encrypts plaintext using AES-GCM-SIV with the provided nonce and associated data.
func ChaChaDecrypt ¶
ChaChaDecrypt decrypts using ChaCha20-Poly1305 with the provided nonce.
func ChaChaEncrypt ¶
ChaChaEncrypt is a convenience wrapper returning ciphertext and nonce separately.
func ChaChaEncryptWithNonce ¶
ChaChaEncryptWithNonce encrypts using ChaCha20-Poly1305 with a caller-provided nonce.
func DH ¶
DH performs a Curve25519 Diffie-Hellman and rejects low-order public keys by ensuring the derived shared secret is not all zeros.
func HKDFExpand ¶
HKDFExpand performs HKDF-Expand with SHA-256.
func HKDFExtract ¶
HKDFExtract performs HKDF-Extract with SHA-256.
func HMACVerify ¶
HMACVerify computes HMAC-SHA256 over data and compares it to expectedMAC in constant time. Returns false if lengths differ.
func IsKyber1024PublicKey ¶
IsKyber1024PublicKey returns true if the serialized public key matches Kyber1024 sizing.
func IsValidPublicKey ¶
IsValidPublicKey returns true if the Curve25519 public key is not low-order.
func Kyber1024Decapsulate ¶
Kyber1024Decapsulate decapsulates a shared secret from the serialized private key and ciphertext.
func Kyber1024Encapsulate ¶
Kyber1024Encapsulate encapsulates a shared secret to the serialized public key.
func Kyber1024EncapsulateDeterministically ¶
Kyber1024EncapsulateDeterministically encapsulates using a fixed seed.
func RandomBytes ¶
RandomBytes returns securely generated random bytes of the requested length.
func RandomScalar ¶
RandomScalar returns a clamped Curve25519 private scalar.
func SetRandReader ¶
SetRandReader overrides the randomness source and returns a restore function. Intended for tests that need deterministic output.
func ValidatePublicKey ¶
ValidatePublicKey rejects low-order Curve25519 public keys.
func XEdDSASign ¶
XEdDSASign returns an XEdDSA signature over the provided message pieces.
func XEdDSASigningPublicKey ¶
XEdDSASigningPublicKey derives the Ed25519-style public key used to compute the sign bit.
func XEdDSAVerify ¶
XEdDSAVerify validates an XEdDSA signature over the provided message pieces.
Types ¶
type AEAD ¶
type AEAD interface {
Encrypt(key, plaintext, ad []byte) ([]byte, error)
Decrypt(key, ciphertext, ad []byte) ([]byte, error)
KeySize() int
NonceSize() int
}
AEAD defines authenticated encryption with associated data using combined nonce+ciphertext encoding. Encrypt returns nonce||ciphertext||tag; Decrypt expects the same format and uses NonceSize to split.
type AESGCM ¶
type AESGCM struct {
// contains filtered or unexported fields
}
AESGCM implements AEAD using AES-256-GCM.
func (*AESGCM) Encrypt ¶
Encrypt generates a random nonce, encrypts plaintext, and returns nonce||ciphertext||tag.
type ChaChaAEAD ¶
type ChaChaAEAD struct {
// contains filtered or unexported fields
}
ChaChaAEAD implements AEAD using ChaCha20-Poly1305.
func NewChaChaAEAD ¶
func NewChaChaAEAD(r io.Reader) *ChaChaAEAD
NewChaChaAEAD returns a ChaCha20-Poly1305 AEAD; if r is nil crypto/rand.Reader is used.
func (*ChaChaAEAD) Decrypt ¶
func (c *ChaChaAEAD) Decrypt(key, ciphertext, ad []byte) ([]byte, error)
Decrypt splits nonce||ciphertext||tag and decrypts.
func (*ChaChaAEAD) Encrypt ¶
func (c *ChaChaAEAD) Encrypt(key, plaintext, ad []byte) ([]byte, error)
Encrypt generates a random nonce, encrypts plaintext, and returns nonce||ciphertext||tag.
func (*ChaChaAEAD) KeySize ¶
func (c *ChaChaAEAD) KeySize() int
KeySize returns the expected key length in bytes.
func (*ChaChaAEAD) NonceSize ¶
func (c *ChaChaAEAD) NonceSize() int
NonceSize returns the nonce length in bytes.
type KeyPair ¶
KeyPair holds a Curve25519 key pair.
func GenerateKeyPair ¶
GenerateKeyPair creates a new Curve25519 key pair using crypto/rand.
func KeyPairFromPrivate ¶
KeyPairFromPrivate derives the public key for the provided private key bytes.
type KyberKeyPair ¶
KyberKeyPair holds serialized Kyber1024 keys (type prefix + raw key bytes).
func GenerateKyber1024KeyPair ¶
func GenerateKyber1024KeyPair() (*KyberKeyPair, error)
GenerateKyber1024KeyPair creates a Kyber1024 key pair serialized with type prefixes.