rfc8291

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AUTH_SECRET_LEN = 16
	SALT_LEN        = 16

	AES_GCM_OVERHEAD = 16

	HKDF_IKM_LEN   = 32
	HKDF_CEK_LEN   = 16
	HKDF_NONCE_LEN = 12
)

Variables

This section is empty.

Functions

func Marshal

func Marshal(p Payload) (data []byte)

Marshal serializes a Payload into the aes128gcm binary format.

func NewSecrets

func NewSecrets(curve ecdh.Curve) (auth, salt []byte, key *ecdh.PrivateKey)

NewSecrets generates new random auth secret, salt, and ECDH private key.

Types

type Aes128gcmScheme

type Aes128gcmScheme struct{}

Aes128gcmScheme implements EncodingScheme for the aes128gcm encoding.

func (Aes128gcmScheme) DeriveCEKAndNonce

func (s Aes128gcmScheme) DeriveCEKAndNonce(hash func() hash.Hash, ikm, salt []byte, uaKey, asKey *ecdh.PublicKey) (cek, nonce []byte, err error)

func (Aes128gcmScheme) DeriveIKM

func (s Aes128gcmScheme) DeriveIKM(hash func() hash.Hash, authSecret, ecdhSecret []byte, uaKey, asKey *ecdh.PublicKey) ([]byte, error)

func (Aes128gcmScheme) Pad

func (s Aes128gcmScheme) Pad(plaintext []byte) []byte

func (Aes128gcmScheme) Unpad

func (s Aes128gcmScheme) Unpad(data []byte) ([]byte, error)

type AesgcmScheme

type AesgcmScheme struct{}

AesgcmScheme implements EncodingScheme for the aesgcm encoding.

func (AesgcmScheme) DeriveCEKAndNonce

func (s AesgcmScheme) DeriveCEKAndNonce(hash func() hash.Hash, ikm, salt []byte, uaKey, asKey *ecdh.PublicKey) (cek, nonce []byte, err error)

func (AesgcmScheme) DeriveIKM

func (s AesgcmScheme) DeriveIKM(hash func() hash.Hash, authSecret, ecdhSecret []byte, uaKey, asKey *ecdh.PublicKey) ([]byte, error)

func (AesgcmScheme) Pad

func (s AesgcmScheme) Pad(plaintext []byte) []byte

func (AesgcmScheme) Unpad

func (s AesgcmScheme) Unpad(data []byte) ([]byte, error)

type CryptoParams

type CryptoParams struct {
	Salt            []byte
	SenderPublicKey *ecdh.PublicKey
}

CryptoParams holds the extracted cryptographic parameters for decryption.

func ParseAesgcmHeaders

func ParseAesgcmHeaders(encryptionHeader, cryptoKeyHeader string, curve ecdh.Curve) (*CryptoParams, error)

ParseAesgcmHeaders extracts salt and sender public key from aesgcm HTTP headers. encryptionHeader: e.g., "salt=FiyMDLvlVl678odI9AWL3A" cryptoKeyHeader: e.g., "dh=BMLYo...;p256ecdsa=BF5o..."

type Encoding

type Encoding string

Encoding represents the Content-Encoding type for WebPush messages.

const (
	EncodingAes128gcm Encoding = "aes128gcm"
	EncodingAesgcm    Encoding = "aesgcm"
)

type EncodingScheme

type EncodingScheme interface {
	DeriveIKM(hash func() hash.Hash, authSecret, ecdhSecret []byte, uaKey, asKey *ecdh.PublicKey) ([]byte, error)
	DeriveCEKAndNonce(hash func() hash.Hash, ikm, salt []byte, uaKey, asKey *ecdh.PublicKey) (cek, nonce []byte, err error)
	Pad(plaintext []byte) []byte
	Unpad(data []byte) ([]byte, error)
}

EncodingScheme defines the encoding-specific operations for WebPush encryption.

func Scheme

func Scheme(encoding Encoding) (EncodingScheme, error)

Scheme returns the EncodingScheme implementation for the given encoding type.

type EncryptResult

type EncryptResult struct {
	Ciphertext      []byte // The encrypted data (for request body)
	Salt            []byte // For Encryption header: salt=<base64url>
	SenderPublicKey []byte // For Crypto-Key header: dh=<base64url>
}

EncryptResult holds the result of aesgcm encryption. Unlike aes128gcm which embeds crypto params in the payload, aesgcm requires these to be sent as HTTP headers.

type Payload

type Payload struct {
	RS         uint32
	Salt       []byte
	KeyId      []byte
	CipherText []byte
}

Payload represents the aes128gcm message format with embedded crypto parameters.

func Unmarshal

func Unmarshal(data []byte) (p Payload, err error)

Unmarshal parses the aes128gcm binary format into a Payload.

type RFC8291

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

RFC8291 implements WebPush message encryption and decryption.

func NewRFC8291

func NewRFC8291(hash func() hash.Hash) *RFC8291

NewRFC8291 creates a new RFC8291 instance. Default hash is SHA256.

func (*RFC8291) Decrypt

func (c *RFC8291) Decrypt(
	data []byte,
	encoding Encoding,
	encryptionHeader string,
	cryptoKeyHeader string,
	authSecret []byte,
	receiverPrivateKey *ecdh.PrivateKey,
) ([]byte, error)

Decrypt decrypts a push notification, automatically selecting the correct encoding scheme based on the encoding parameter.

For aes128gcm: crypto params are extracted from the data payload. For aesgcm: crypto params are extracted from the HTTP headers.

func (*RFC8291) DecryptAes128gcm

func (c *RFC8291) DecryptAes128gcm(
	ciphertext []byte,
	salt []byte,
	authSecret []byte,
	receiverPrivateKey *ecdh.PrivateKey,
	senderPublicKey *ecdh.PublicKey,
) ([]byte, error)

DecryptAes128gcm decrypts a message encrypted with the aes128gcm encoding scheme.

func (*RFC8291) DecryptAesgcm

func (c *RFC8291) DecryptAesgcm(
	ciphertext []byte,
	salt []byte,
	authSecret []byte,
	receiverPrivateKey *ecdh.PrivateKey,
	senderPublicKey *ecdh.PublicKey,
) ([]byte, error)

DecryptAesgcm decrypts a message encrypted with the aesgcm encoding scheme.

func (*RFC8291) EncryptAes128gcm

func (c *RFC8291) EncryptAes128gcm(
	plaintext []byte,
	salt []byte,
	authSecret []byte,
	receiverPublicKey *ecdh.PublicKey,
	senderPrivateKey *ecdh.PrivateKey,
) ([]byte, error)

EncryptAes128gcm encrypts a message using the aes128gcm encoding scheme. Returns the complete payload with embedded crypto parameters.

func (*RFC8291) EncryptAesgcm

func (c *RFC8291) EncryptAesgcm(
	plaintext []byte,
	salt []byte,
	authSecret []byte,
	receiverPublicKey *ecdh.PublicKey,
	senderPrivateKey *ecdh.PrivateKey,
) (*EncryptResult, error)

EncryptAesgcm encrypts a message using the aesgcm encoding scheme. Returns the ciphertext and crypto parameters needed for HTTP headers.

Jump to

Keyboard shortcuts

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