]> git.feebdaed.xyz Git - 0xmirror/quic-go.git/commitdiff
handshake: pass cipherSuite by value instead of pointer (#5459)
authorMarten Seemann <martenseemann@gmail.com>
Tue, 25 Nov 2025 03:19:56 +0000 (11:19 +0800)
committerGitHub <noreply@github.com>
Tue, 25 Nov 2025 03:19:56 +0000 (04:19 +0100)
internal/handshake/aead.go
internal/handshake/aead_test.go
internal/handshake/cipher_suite.go
internal/handshake/handshake_helpers_test.go
internal/handshake/header_protector.go
internal/handshake/updatable_aead.go
internal/handshake/updatable_aead_test.go

index 1baf5d6b0b31a25b02f4c00edd3aee9896b90047..ce83ab18c73c00dcebea3c1cf6896a2554f3c4e8 100644 (file)
@@ -6,7 +6,7 @@ import (
        "github.com/quic-go/quic-go/internal/protocol"
 )
 
-func createAEAD(suite *cipherSuite, trafficSecret []byte, v protocol.Version) *xorNonceAEAD {
+func createAEAD(suite cipherSuite, trafficSecret []byte, v protocol.Version) *xorNonceAEAD {
        keyLabel := hkdfLabelKeyV1
        ivLabel := hkdfLabelIVV1
        if v == protocol.Version2 {
index 693ca1e5fc89e1127176880081414559af4f02da..a71eeeaa8e20a94ec95f103f2f17631f0106e6eb 100644 (file)
@@ -13,7 +13,7 @@ import (
        "github.com/stretchr/testify/require"
 )
 
-func getSealerAndOpener(t *testing.T, cs *cipherSuite, v protocol.Version) (LongHeaderSealer, LongHeaderOpener) {
+func getSealerAndOpener(t *testing.T, cs cipherSuite, v protocol.Version) (LongHeaderSealer, LongHeaderOpener) {
        t.Helper()
        key := make([]byte, 16)
        hpKey := make([]byte, 16)
@@ -83,7 +83,7 @@ func TestEncryptAndDecryptHeader(t *testing.T) {
        }
 }
 
-func testEncryptAndDecryptHeader(t *testing.T, cs *cipherSuite, v protocol.Version) {
+func testEncryptAndDecryptHeader(t *testing.T, cs cipherSuite, v protocol.Version) {
        sealer, opener := getSealerAndOpener(t, cs, v)
        var lastFourBitsDifferent int
 
index d8a381daf05ba3b7105e52960609cf4af5734ede..03fe0dad8e19bdda890b9d944130487e1a09e2b1 100644 (file)
@@ -23,14 +23,14 @@ type cipherSuite struct {
 
 func (s cipherSuite) IVLen() int { return aeadNonceLength }
 
-func getCipherSuite(id uint16) *cipherSuite {
+func getCipherSuite(id uint16) cipherSuite {
        switch id {
        case tls.TLS_AES_128_GCM_SHA256:
-               return &cipherSuite{ID: tls.TLS_AES_128_GCM_SHA256, Hash: crypto.SHA256, KeyLen: 16, AEAD: aeadAESGCMTLS13}
+               return cipherSuite{ID: tls.TLS_AES_128_GCM_SHA256, Hash: crypto.SHA256, KeyLen: 16, AEAD: aeadAESGCMTLS13}
        case tls.TLS_CHACHA20_POLY1305_SHA256:
-               return &cipherSuite{ID: tls.TLS_CHACHA20_POLY1305_SHA256, Hash: crypto.SHA256, KeyLen: 32, AEAD: aeadChaCha20Poly1305}
+               return cipherSuite{ID: tls.TLS_CHACHA20_POLY1305_SHA256, Hash: crypto.SHA256, KeyLen: 32, AEAD: aeadChaCha20Poly1305}
        case tls.TLS_AES_256_GCM_SHA384:
-               return &cipherSuite{ID: tls.TLS_AES_256_GCM_SHA384, Hash: crypto.SHA384, KeyLen: 32, AEAD: aeadAESGCMTLS13}
+               return cipherSuite{ID: tls.TLS_AES_256_GCM_SHA384, Hash: crypto.SHA384, KeyLen: 32, AEAD: aeadAESGCMTLS13}
        default:
                panic(fmt.Sprintf("unknown cypher suite: %d", id))
        }
index a684c2ccacfd31e603979254abde24121b0989f9..5bde8b1e9addd5526027c92060ca712964f54c50 100644 (file)
@@ -28,7 +28,7 @@ func TestSplitHexString(t *testing.T) {
        require.Equal(t, []byte{0xde, 0xad, 0xbe, 0xef}, splitHexString(t, "dead beef"))
 }
 
-var cipherSuites = []*cipherSuite{
+var cipherSuites = []cipherSuite{
        getCipherSuite(tls.TLS_AES_128_GCM_SHA256),
        getCipherSuite(tls.TLS_AES_256_GCM_SHA384),
        getCipherSuite(tls.TLS_CHACHA20_POLY1305_SHA256),
index 2c5ee42f12d5b8b8c9e6d98b50d03bd8f64749db..93c3cd9809d3ae4257a2192a6b3a94d47bf1b4d0 100644 (file)
@@ -24,7 +24,7 @@ func hkdfHeaderProtectionLabel(v protocol.Version) string {
        return "quic hp"
 }
 
-func newHeaderProtector(suite *cipherSuite, trafficSecret []byte, isLongHeader bool, v protocol.Version) headerProtector {
+func newHeaderProtector(suite cipherSuite, trafficSecret []byte, isLongHeader bool, v protocol.Version) headerProtector {
        hkdfLabel := hkdfHeaderProtectionLabel(v)
        switch suite.ID {
        case tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384:
@@ -44,7 +44,7 @@ type aesHeaderProtector struct {
 
 var _ headerProtector = &aesHeaderProtector{}
 
-func newAESHeaderProtector(suite *cipherSuite, trafficSecret []byte, isLongHeader bool, hkdfLabel string) headerProtector {
+func newAESHeaderProtector(suite cipherSuite, trafficSecret []byte, isLongHeader bool, hkdfLabel string) headerProtector {
        hpKey := hkdfExpandLabel(suite.Hash, trafficSecret, []byte{}, hkdfLabel, suite.KeyLen)
        block, err := aes.NewCipher(hpKey)
        if err != nil {
@@ -88,7 +88,7 @@ type chachaHeaderProtector struct {
 
 var _ headerProtector = &chachaHeaderProtector{}
 
-func newChaChaHeaderProtector(suite *cipherSuite, trafficSecret []byte, isLongHeader bool, hkdfLabel string) headerProtector {
+func newChaChaHeaderProtector(suite cipherSuite, trafficSecret []byte, isLongHeader bool, hkdfLabel string) headerProtector {
        hpKey := hkdfExpandLabel(suite.Hash, trafficSecret, []byte{}, hkdfLabel, suite.KeyLen)
 
        p := &chachaHeaderProtector{
index ae63eec89151a8ea28c03a8e648c18e7427c1514..f88f76ad8b0985907606f4d880ae9d88a7fa9398 100644 (file)
@@ -32,7 +32,7 @@ func SetKeyUpdateInterval(v uint64) (reset func()) {
 var FirstKeyUpdateInterval uint64 = 100
 
 type updatableAEAD struct {
-       suite *cipherSuite
+       suite cipherSuite
 
        keyPhase           protocol.KeyPhase
        largestAcked       protocol.PacketNumber
@@ -136,10 +136,10 @@ func (a *updatableAEAD) getNextTrafficSecret(hash crypto.Hash, ts []byte) []byte
 // SetReadKey sets the read key.
 // For the client, this function is called before SetWriteKey.
 // For the server, this function is called after SetWriteKey.
-func (a *updatableAEAD) SetReadKey(suite *cipherSuite, trafficSecret []byte) {
+func (a *updatableAEAD) SetReadKey(suite cipherSuite, trafficSecret []byte) {
        a.rcvAEAD = createAEAD(suite, trafficSecret, a.version)
        a.headerDecrypter = newHeaderProtector(suite, trafficSecret, false, a.version)
-       if a.suite == nil {
+       if a.suite.ID == 0 { // suite is not set yet
                a.setAEADParameters(a.rcvAEAD, suite)
        }
 
@@ -150,10 +150,10 @@ func (a *updatableAEAD) SetReadKey(suite *cipherSuite, trafficSecret []byte) {
 // SetWriteKey sets the write key.
 // For the client, this function is called after SetReadKey.
 // For the server, this function is called before SetReadKey.
-func (a *updatableAEAD) SetWriteKey(suite *cipherSuite, trafficSecret []byte) {
+func (a *updatableAEAD) SetWriteKey(suite cipherSuite, trafficSecret []byte) {
        a.sendAEAD = createAEAD(suite, trafficSecret, a.version)
        a.headerEncrypter = newHeaderProtector(suite, trafficSecret, false, a.version)
-       if a.suite == nil {
+       if a.suite.ID == 0 { // suite is not set yet
                a.setAEADParameters(a.sendAEAD, suite)
        }
 
@@ -161,7 +161,7 @@ func (a *updatableAEAD) SetWriteKey(suite *cipherSuite, trafficSecret []byte) {
        a.nextSendAEAD = createAEAD(suite, a.nextSendTrafficSecret, a.version)
 }
 
-func (a *updatableAEAD) setAEADParameters(aead cipher.AEAD, suite *cipherSuite) {
+func (a *updatableAEAD) setAEADParameters(aead cipher.AEAD, suite cipherSuite) {
        a.nonceBuf = make([]byte, aead.NonceSize())
        a.aeadOverhead = aead.Overhead()
        a.suite = suite
index dcf16c701c3600967016474b97e1582f90405dd0..b379d78e880273331e8d05e55a2596e7825166f2 100644 (file)
@@ -24,7 +24,7 @@ const (
        ad  = "Donec in velit neque."
 )
 
-func randomCipherSuite() *cipherSuite { return cipherSuites[mrand.IntN(len(cipherSuites))] }
+func randomCipherSuite() cipherSuite { return cipherSuites[mrand.IntN(len(cipherSuites))] }
 
 func setupEndpoints(t *testing.T, serverRTTStats *utils.RTTStats) (client, server *updatableAEAD, serverEventRecorder *events.Recorder) {
        cs := randomCipherSuite()