crypto

package
v0.0.0-...-6b8ee43 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package crypto implements the IKEv2 cryptographic primitives and proposal negotiation.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecryptionFailed = errors.New("decryption failed: authentication tag mismatch")
	ErrInvalidKeyLength = errors.New("invalid key length")
	ErrIntegrityFailed  = errors.New("integrity verification failed")
)
View Source
var (
	ErrInvalidPublicKey = errors.New("invalid DH public key")
	ErrUnsupportedGroup = errors.New("unsupported DH group")
)
View Source
var ErrNoProposalChosen = errors.New("NO_PROPOSAL_CHOSEN")
View Source
var ErrUnsupportedAlgorithm = errors.New("unsupported algorithm")

Functions

func ComputeIntegrity

func ComputeIntegrity(id IntegrityID, key, data []byte) ([]byte, error)

ComputeIntegrity computes a truncated HMAC for the given data.

func DecryptAESCBC

func DecryptAESCBC(key, data []byte) ([]byte, error)

DecryptAESCBC decrypts data produced by EncryptAESCBC (iv || ciphertext).

func DecryptAESCBCRaw

func DecryptAESCBCRaw(key, data []byte) ([]byte, error)

DecryptAESCBCRaw decrypts without unpadding (caller handles IKEv2 padding). Input is iv(blockSize) || ciphertext.

func DecryptAESGCM

func DecryptAESGCM(key, data, aad []byte) ([]byte, error)

DecryptAESGCM decrypts data produced by EncryptAESGCM (nonce || ciphertext || tag).

func DecryptIKEAEAD

func DecryptIKEAEAD(keyWithSalt, data, aad []byte) ([]byte, error)

DecryptIKEAEAD decrypts using the IKEv2 AEAD construction (RFC 5282). keyWithSalt is the AEAD key material: AES key || 4-byte salt. data is IV(8) || ciphertext || tag(16) from the wire.

func DeriveRekeyedSKEYSEED

func DeriveRekeyedSKEYSEED(prfID PRFID, skDOld, newSharedSecret, ni, nr []byte) ([]byte, error)

DeriveRekeyedSKEYSEED computes the seed key for a rekeyed IKE SA. RFC 7296 Section 2.18: SKEYSEED = prf(SK_d_old, g^ir_new | Ni | Nr).

func DeriveSKEYSEED

func DeriveSKEYSEED(prfID PRFID, ni, nr, sharedSecret []byte) ([]byte, error)

DeriveSKEYSEED computes the initial IKE SA seed key. RFC 7296 Section 2.14: SKEYSEED = prf(Ni | Nr, g^ir).

func EncryptAESCBC

func EncryptAESCBC(key, plaintext []byte) ([]byte, error)

EncryptAESCBC encrypts plaintext using AES-CBC with PKCS#7 padding. Returns iv || ciphertext.

func EncryptAESGCM

func EncryptAESGCM(key, plaintext, aad []byte) ([]byte, error)

EncryptAESGCM encrypts plaintext using AES-GCM with a 16-byte authentication tag. Returns nonce || ciphertext || tag.

func EncryptIKEAEAD

func EncryptIKEAEAD(keyWithSalt, plaintext, aad []byte) ([]byte, error)

EncryptIKEAEAD encrypts using the IKEv2 AEAD construction (RFC 5282). keyWithSalt is the AEAD key material: AES key || 4-byte salt. Returns IV(8) || ciphertext || tag(16) for the wire.

func PRF

func PRF(id PRFID, key, data []byte) ([]byte, error)

PRF computes prf(key, data) using the selected HMAC algorithm.

func PRFPlus

func PRFPlus(id PRFID, key, seed []byte, length int) ([]byte, error)

PRFPlus implements RFC 7296 Section 2.13 prf+ key expansion. T1 = prf(K, S | 0x01) T2 = prf(K, T1 | S | 0x02) T3 = prf(K, T2 | S | 0x03) ... prf+(K, S) = T1 | T2 | T3 | ...

func VerifyIntegrity

func VerifyIntegrity(id IntegrityID, key, data, expected []byte) error

VerifyIntegrity checks a truncated HMAC using constant-time comparison.

Types

type ChildSAKeys

type ChildSAKeys struct {
	EncryptKeyI []byte
	IntegKeyI   []byte
	EncryptKeyR []byte
	IntegKeyR   []byte
}

ChildSAKeys holds ESP keying material for one direction.

func DeriveChildSAKeys

func DeriveChildSAKeys(prfID PRFID, skD, ni, nr []byte, enc EncryptionTransform, integ IntegrityTransform) (*ChildSAKeys, error)

DeriveChildSAKeys derives ESP keys from SK_d and nonces. RFC 7296 Section 2.17: KEYMAT = prf+(SK_d, Ni | Nr).

func DeriveChildSAKeysPFS

func DeriveChildSAKeysPFS(prfID PRFID, skD, dhSharedSecret, ni, nr []byte, enc EncryptionTransform, integ IntegrityTransform) (*ChildSAKeys, error)

DeriveChildSAKeysPFS derives ESP keys with Perfect Forward Secrecy. RFC 7296 Section 2.17: KEYMAT = prf+(SK_d, g^ir | Ni | Nr).

func (*ChildSAKeys) Clear

func (k *ChildSAKeys) Clear()

type DHExchange

type DHExchange struct {
	GroupID   DHGroupID
	PublicKey []byte
	// contains filtered or unexported fields
}

func NewDHExchange

func NewDHExchange(groupID DHGroupID) (*DHExchange, error)

func (*DHExchange) Clear

func (ex *DHExchange) Clear()

func (*DHExchange) SharedSecret

func (ex *DHExchange) SharedSecret(remotePublic []byte) ([]byte, error)

type DHGroupID

type DHGroupID uint16

RFC 7296 Section 3.3.6: DH Group Transform IDs.

const (
	DH_MODP_2048 DHGroupID = 14
	DH_ECP_256   DHGroupID = 19
	DH_ECP_384   DHGroupID = 20
)

func (DHGroupID) String

func (id DHGroupID) String() string

type DHGroupTransform

type DHGroupTransform struct {
	ID DHGroupID
}

func LookupDHGroup

func LookupDHGroup(id uint8) (DHGroupTransform, error)

type ESPProposal

type ESPProposal struct {
	Number     uint16
	Encryption EncryptionTransform
	Integrity  IntegrityTransform
}

ESPProposal represents a single ESP/Child SA crypto proposal.

func NegotiateESP

func NegotiateESP(remote, local []ESPProposal) (ESPProposal, error)

NegotiateESP selects the first remote ESP proposal acceptable to local policy.

type EncryptionID

type EncryptionID uint16

RFC 7296 Section 3.3.3: Encryption Algorithm Transform IDs.

const (
	ENCR_AES_CBC    EncryptionID = 12
	ENCR_AES_GCM_16 EncryptionID = 20
)

func (EncryptionID) String

func (id EncryptionID) String() string

type EncryptionTransform

type EncryptionTransform struct {
	ID        EncryptionID
	KeyLength uint16 // in bits
	IsAEAD    bool
}

func LookupEncryption

func LookupEncryption(name string) (EncryptionTransform, error)

type IKEProposal

type IKEProposal struct {
	Number     uint16
	Encryption EncryptionTransform
	PRF        PRFTransform
	Integrity  IntegrityTransform
	DHGroup    DHGroupTransform
}

IKEProposal represents a single IKE SA crypto proposal.

func NegotiateIKE

func NegotiateIKE(remote, local []IKEProposal) (IKEProposal, error)

NegotiateIKE selects the first remote proposal acceptable to local policy. RFC 7296 Section 2.7: responder picks exactly one proposal.

type IntegrityID

type IntegrityID uint16

RFC 7296 Section 3.3.5: Integrity Algorithm Transform IDs.

const (
	AUTH_NONE              IntegrityID = 0
	AUTH_HMAC_SHA2_256_128 IntegrityID = 12
	AUTH_HMAC_SHA2_384_192 IntegrityID = 13
	AUTH_HMAC_SHA2_512_256 IntegrityID = 14
)

func (IntegrityID) String

func (id IntegrityID) String() string

type IntegrityTransform

type IntegrityTransform struct {
	ID              IntegrityID
	KeyLength       uint16 // in bytes
	TruncatedLength uint16 // ICV length in bytes
}

func LookupIntegrity

func LookupIntegrity(name string) (IntegrityTransform, error)

type PRFID

type PRFID uint16

RFC 7296 Section 3.3.4: PRF Transform IDs.

const (
	PRF_HMAC_SHA2_256 PRFID = 5
	PRF_HMAC_SHA2_384 PRFID = 6
	PRF_HMAC_SHA2_512 PRFID = 7
)

type PRFTransform

type PRFTransform struct {
	ID           PRFID
	KeyLength    uint16 // in bytes
	OutputLength uint16 // in bytes
}

func LookupPRF

func LookupPRF(name string) (PRFTransform, error)

type SKKeys

type SKKeys struct {
	SK_d  []byte // Key derivation key for Child SA KEYMAT.
	SK_ai []byte // Initiator integrity key.
	SK_ar []byte // Responder integrity key.
	SK_ei []byte // Initiator encryption key.
	SK_er []byte // Responder encryption key.
	SK_pi []byte // Initiator AUTH payload key.
	SK_pr []byte // Responder AUTH payload key.
}

SKKeys holds the IKE SA key hierarchy derived from SKEYSEED.

func DeriveSKKeys

func DeriveSKKeys(prfID PRFID, skeyseed, ni, nr, spiI, spiR []byte, enc EncryptionTransform, integ IntegrityTransform) (*SKKeys, error)

DeriveSKKeys expands SKEYSEED into the full SK_* key hierarchy. RFC 7296 Section 2.14: {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr} =

prf+(SKEYSEED, Ni | Nr | SPIi | SPIr).

func (*SKKeys) Clear

func (k *SKKeys) Clear()

type TransformType

type TransformType uint8

RFC 7296 Section 3.3.2: Transform Type Values.

const (
	TransformTypeENCR  TransformType = 1
	TransformTypePRF   TransformType = 2
	TransformTypeINTEG TransformType = 3
	TransformTypeDH    TransformType = 4
	TransformTypeESN   TransformType = 5
)

Jump to

Keyboard shortcuts

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