Documentation
¶
Overview ¶
Package cryptoutil provides utility functions for cryptographic operations. It is the consumer's responsibility to ensure that inputs are reasonably sized so as to avoid memory exhaustion attacks.
Index ¶
- Constants
- Variables
- func DecryptSymmetricAESGCM(encryptedMsg []byte, secretKey Key32) ([]byte, error)
- func DecryptSymmetricGeneric(toAEADFunc ToAEADFunc, ciphertext []byte, secretKey Key32) ([]byte, error)
- func DecryptSymmetricXChaCha20Poly1305(encryptedMsg []byte, secretKey Key32) ([]byte, error)
- func EncryptSymmetricAESGCM(msg []byte, secretKey Key32) ([]byte, error)
- func EncryptSymmetricGeneric(toAEADFunc ToAEADFunc, msg []byte, secretKey Key32) ([]byte, error)
- func EncryptSymmetricXChaCha20Poly1305(msg []byte, secretKey Key32) ([]byte, error)
- func FromKey32(key Key32) ([]byte, error)
- func HmacSha256(msg []byte, key []byte) ([]byte, error)
- func RandomBytes(byteLen int) ([]byte, error)
- func Sha256Hash(msg []byte) []byte
- func SignSymmetric(msg []byte, secretKey Key32) ([]byte, error)
- func ValidateHmacSha256(attemptedMsg, attemptedKey, knownGoodMAC []byte) (bool, error)
- func VerifyAndReadAsymmetric(signedMsg []byte, publicKey Key32) ([]byte, error)
- func VerifyAndReadAsymmetricBase64(signedMsg, publicKey Base64) ([]byte, error)
- func VerifyAndReadSymmetric(signedMsg []byte, secretKey Key32) ([]byte, error)
- type Base64
- type Key32
- type ToAEADFunc
Constants ¶
const KeySize = 32
Variables ¶
Functions ¶
func DecryptSymmetricAESGCM ¶
DecryptSymmetricAESGCM decrypts a message using AES-256-GCM.
func DecryptSymmetricGeneric ¶
func DecryptSymmetricGeneric( toAEADFunc ToAEADFunc, ciphertext []byte, secretKey Key32, ) ([]byte, error)
DecryptSymmetricGeneric decrypts a message using a generic AEAD function.
func DecryptSymmetricXChaCha20Poly1305 ¶
DecryptSymmetricXChaCha20Poly1305 decrypts a message using XChaCha20-Poly1305.
func EncryptSymmetricAESGCM ¶
EncryptSymmetricAESGCM encrypts a message using AES-256-GCM.
func EncryptSymmetricGeneric ¶
func EncryptSymmetricGeneric( toAEADFunc ToAEADFunc, msg []byte, secretKey Key32, ) ([]byte, error)
EncryptSymmetricGeneric encrypts a message using a generic AEAD function.
func EncryptSymmetricXChaCha20Poly1305 ¶
EncryptSymmetricXChaCha20Poly1305 encrypts a message using XChaCha20-Poly1305.
func HmacSha256 ¶
HmacSha256 computes the HMAC-SHA-256 of a message using secret key. As a security precaution, returns an error if the key is nil or empty. If this isn't what you want, use the standard library directly.
func RandomBytes ¶
Random returns a slice of cryptographically random bytes of length byteLen.
func Sha256Hash ¶
Sha256Hash returns the SHA-256 hash of a message as a byte slice.
func SignSymmetric ¶
SignSymmetric signs a message using a symmetric key. It is a convenience wrapper around the nacl/auth package, which uses HMAC-SHA-512-256.
func ValidateHmacSha256 ¶
ValidateHmacSha256 constant-time compares the HMAC-SHA-256 of an attempted message and attempted key against a known good MAC. Returns true if the resulting MAC is valid and false if it is not. Does NOT necessarily return an error if the MAC is invalid, so callers must rely on the boolean return value to determine validity.
func VerifyAndReadAsymmetric ¶
VerifyAndReadAsymmetric verifies a signed message using an Ed25519 public key and returns the original message.
func VerifyAndReadAsymmetricBase64 ¶
VerifyAndReadAsymmetricBase64 verifies a signed message using a base64 encoded Ed25519 public key and returns the original message.
func VerifyAndReadSymmetric ¶
VerifyAndReadSymmetric verifies a signed message using a symmetric key and returns the original message. It is a convenience wrapper around the nacl/auth package, which uses HMAC-SHA-512-256.
Types ¶
type Key32 ¶
Alias for a pointer to a size 32 byte array.
func HkdfSha256 ¶
HkdfSha256 derives a new cryptographic key from a 32-byte secret key using HKDF. It uses SHA-256 as the hash function and returns a 32-byte key. Salt and/or info can be nil.
type ToAEADFunc ¶
var ToAEADFuncAESGCM ToAEADFunc = func(secretKey Key32) (cipher.AEAD, error) { block, err := aes.NewCipher(secretKey[:]) if err != nil { return nil, err } return cipher.NewGCM(block) }
ToAEADFuncAESGCM returns an AEAD function for AES-256-GCM.
var ToAEADFuncXChaCha20Poly1305 ToAEADFunc = func(secretKey Key32) (cipher.AEAD, error) { return chacha20poly1305.NewX(secretKey[:]) }
ToAEADFuncXChaCha20Poly1305 returns an AEAD function for XChaCha20-Poly1305.