github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/transport/internet/quic/config.go (about)

     1  package quic
     2  
     3  import (
     4  	"crypto/aes"
     5  	"crypto/cipher"
     6  	"crypto/sha256"
     7  
     8  	"github.com/xtls/xray-core/common"
     9  	"github.com/xtls/xray-core/common/protocol"
    10  	"github.com/xtls/xray-core/transport/internet"
    11  	"golang.org/x/crypto/chacha20poly1305"
    12  )
    13  
    14  func getAuth(config *Config) (cipher.AEAD, error) {
    15  	security := config.Security.GetSecurityType()
    16  	if security == protocol.SecurityType_NONE {
    17  		return nil, nil
    18  	}
    19  
    20  	salted := []byte(config.Key + "xray-quic-salt")
    21  	key := sha256.Sum256(salted)
    22  
    23  	if security == protocol.SecurityType_AES128_GCM {
    24  		block, err := aes.NewCipher(key[:16])
    25  		common.Must(err)
    26  		return cipher.NewGCM(block)
    27  	}
    28  
    29  	if security == protocol.SecurityType_CHACHA20_POLY1305 {
    30  		return chacha20poly1305.New(key[:])
    31  	}
    32  
    33  	return nil, newError("unsupported security type")
    34  }
    35  
    36  func getHeader(config *Config) (internet.PacketHeader, error) {
    37  	if config.Header == nil {
    38  		return nil, nil
    39  	}
    40  
    41  	msg, err := config.Header.GetInstance()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return internet.CreatePacketHeader(msg)
    47  }