From 0e53833a2cb9a78601d1e5f1cfc7a9ff42dd7718 Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 23 Jul 2026 01:17:37 +0800 Subject: [PATCH] refactor: use standard library HKDF (#5440) --- pkg/util/net/conn.go | 11 +++-------- pkg/util/net/conn_test.go | 6 ++++++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/util/net/conn.go b/pkg/util/net/conn.go index 5dd605a5..fd71162e 100644 --- a/pkg/util/net/conn.go +++ b/pkg/util/net/conn.go @@ -16,6 +16,7 @@ package net import ( "context" + "crypto/hkdf" "crypto/sha256" "errors" "io" @@ -25,7 +26,6 @@ import ( libcrypto "github.com/fatedier/golib/crypto" quic "github.com/quic-go/quic-go" - "golang.org/x/crypto/hkdf" "github.com/fatedier/frp/pkg/util/xlog" ) @@ -335,11 +335,6 @@ func deriveAEADControlKeys(key []byte, algorithm string, transcriptHash []byte) } func deriveAEADControlKey(key []byte, algorithm string, transcriptHash []byte, direction string) ([]byte, error) { - info := []byte(aeadControlHKDFInfoPrefix + " " + algorithm + " " + direction) - reader := hkdf.New(sha256.New, key, transcriptHash, info) - out := make([]byte, libcrypto.AEADKeySize) - if _, err := io.ReadFull(reader, out); err != nil { - return nil, err - } - return out, nil + info := aeadControlHKDFInfoPrefix + " " + algorithm + " " + direction + return hkdf.Key(sha256.New, key, transcriptHash, info, libcrypto.AEADKeySize) } diff --git a/pkg/util/net/conn_test.go b/pkg/util/net/conn_test.go index 42d3c06b..93387384 100644 --- a/pkg/util/net/conn_test.go +++ b/pkg/util/net/conn_test.go @@ -114,5 +114,11 @@ func TestDeriveAEADControlKeysUsesDistinctDirections(t *testing.T) { bytes.Repeat([]byte{0x44}, 32), ) require.NoError(t, err) + require.Equal(t, []byte{ + 0xa0, 0x58, 0xcd, 0x02, 0x5d, 0x96, 0x98, 0x5f, + 0xeb, 0xeb, 0xff, 0x79, 0xa1, 0x9f, 0x62, 0xb7, + 0x15, 0xe0, 0x53, 0x91, 0x3d, 0xfc, 0x74, 0x77, + 0x05, 0x91, 0x4c, 0x62, 0x4b, 0xf3, 0xd4, 0x95, + }, clientToServerKey) require.NotEqual(t, clientToServerKey, serverToClientKey) }