1+ // Copyright (c) 2026 Suryansh Deshwal
2+ // Licensed under the Apache License, Version 2.0
3+
14package secure
25
36import (
47 "crypto/aes"
58 "crypto/cipher"
69 "crypto/ecdh"
710 "crypto/ed25519"
11+ "crypto/hkdf"
812 "crypto/rand"
913 "crypto/sha256"
1014 "encoding/base64"
@@ -14,8 +18,6 @@ import (
1418 "io"
1519 "net"
1620
17- "golang.org/x/crypto/hkdf"
18-
1921 "github.com/vx6/vx6/internal/identity"
2022 "github.com/vx6/vx6/internal/proto"
2123)
@@ -140,9 +142,8 @@ func handshake(conn net.Conn, kind byte, id identity.Identity, initiator bool) (
140142
141143func deriveKey (sharedSecret , transcript []byte ) ([]byte , error ) {
142144 salt := sha256 .Sum256 (transcript )
143- kdf := hkdf .New (sha256 .New , sharedSecret , salt [:], []byte ("vx6-session-v2" ))
144- key := make ([]byte , 32 )
145- if _ , err := io .ReadFull (kdf , key ); err != nil {
145+ key , err := hkdf .Key (sha256 .New , sharedSecret , salt [:], "vx6-session-v2" , 32 )
146+ if err != nil {
146147 return nil , err
147148 }
148149 return key , nil
@@ -221,7 +222,7 @@ func (c *Conn) Write(p []byte) (int, error) {
221222}
222223
223224func buildHello (id identity.Identity , kind byte , eph []byte ) (hello , error ) {
224- sig := ed25519 .Sign (id .PrivateKey , signingPayload (kind , id .NodeID , eph ))
225+ sig := ed25519 .Sign (id .PrivateKey , signingPayload (sessionVersion , kind , id .NodeID , eph ))
225226 return hello {
226227 Version : sessionVersion ,
227228 NodeID : id .NodeID ,
@@ -258,7 +259,7 @@ func readHello(r io.Reader, kind byte) (hello, error) {
258259 if identity .NodeIDFromPublicKey (ed25519 .PublicKey (pub )) != h .NodeID {
259260 return hello {}, fmt .Errorf ("handshake node id mismatch" )
260261 }
261- if ! ed25519 .Verify (ed25519 .PublicKey (pub ), signingPayload (kind , h .NodeID , eph ), sig ) {
262+ if ! ed25519 .Verify (ed25519 .PublicKey (pub ), signingPayload (h . Version , kind , h .NodeID , eph ), sig ) {
262263 return hello {}, fmt .Errorf ("handshake signature verification failed" )
263264 }
264265
@@ -281,9 +282,11 @@ func (h hello) ephemeralBytes() ([]byte, error) {
281282 return eph , nil
282283}
283284
284- func signingPayload (kind byte , nodeID string , eph []byte ) []byte {
285+ func signingPayload (version uint8 , kind byte , nodeID string , eph []byte ) []byte {
285286 var out []byte
286287 out = append (out , []byte ("vx6-secure\n " )... )
288+ out = append (out , version )
289+ out = append (out , '\n' )
287290 out = append (out , kind )
288291 out = append (out , '\n' )
289292 out = append (out , []byte (nodeID )... )
0 commit comments