Skip to content

Commit 8e4f881

Browse files
FanglidingRPRX
andcommitted
Commands: Add vlessenc (generate complete json pair directly) (#5078)
#5078 (comment) --------- Co-authored-by: RPRX <[email protected]>
1 parent cbade89 commit 8e4f881

File tree

6 files changed

+81
-17
lines changed

6 files changed

+81
-17
lines changed

main/commands/all/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ func init() {
1818
cmdWG,
1919
cmdMLDSA65,
2020
cmdMLKEM768,
21+
cmdVLESSEnc,
2122
)
2223
}

main/commands/all/curve25519.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,25 @@ func Curve25519Genkey(StdEncoding bool, input_base64 string) {
2121
if len(input_base64) > 0 {
2222
privateKey, _ = encoding.DecodeString(input_base64)
2323
if len(privateKey) != 32 {
24-
fmt.Println("Invalid length of X25519 private key.")
24+
fmt.Println("invalid length of X25519 private key")
2525
return
2626
}
2727
}
28+
privateKey, password, hash32, err := genCurve25519(privateKey)
29+
if err != nil {
30+
fmt.Println(err)
31+
return
32+
}
33+
fmt.Printf("PrivateKey: %v\nPassword: %v\nHash32: %v\n",
34+
encoding.EncodeToString(privateKey),
35+
encoding.EncodeToString(password),
36+
encoding.EncodeToString(hash32[:]))
37+
}
38+
39+
func genCurve25519(inputPrivateKey []byte) (privateKey []byte, password []byte, hash32 [32]byte, returnErr error) {
40+
if len(inputPrivateKey) > 0 {
41+
privateKey = inputPrivateKey
42+
}
2843
if privateKey == nil {
2944
privateKey = make([]byte, 32)
3045
rand.Read(privateKey)
@@ -39,13 +54,10 @@ func Curve25519Genkey(StdEncoding bool, input_base64 string) {
3954

4055
key, err := ecdh.X25519().NewPrivateKey(privateKey)
4156
if err != nil {
42-
fmt.Println(err.Error())
57+
returnErr = err
4358
return
4459
}
45-
password := key.PublicKey().Bytes()
46-
hash32 := blake3.Sum256(password)
47-
fmt.Printf("PrivateKey: %v\nPassword: %v\nHash32: %v",
48-
encoding.EncodeToString(privateKey),
49-
encoding.EncodeToString(password),
50-
encoding.EncodeToString(hash32[:]))
60+
password = key.PublicKey().Bytes()
61+
hash32 = blake3.Sum256(password)
62+
return
5163
}

main/commands/all/mldsa65.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func executeMLDSA65(cmd *base.Command, args []string) {
4040
rand.Read(seed[:])
4141
}
4242
pub, _ := mldsa65.NewKeyFromSeed(&seed)
43-
fmt.Printf("Seed: %v\nVerify: %v",
43+
fmt.Printf("Seed: %v\nVerify: %v\n",
4444
base64.RawURLEncoding.EncodeToString(seed[:]),
4545
base64.RawURLEncoding.EncodeToString(pub.Bytes()))
4646
}

main/commands/all/mlkem768.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212

1313
var cmdMLKEM768 = &base.Command{
1414
UsageLine: `{{.Exec}} mlkem768 [-i "seed (base64.RawURLEncoding)"]`,
15-
Short: `Generate key pair for ML-KEM-768 post-quantum key exchange (VLESS)`,
15+
Short: `Generate key pair for ML-KEM-768 post-quantum key exchange (VLESS Encryption)`,
1616
Long: `
17-
Generate key pair for ML-KEM-768 post-quantum key exchange (VLESS).
17+
Generate key pair for ML-KEM-768 post-quantum key exchange (VLESS Encryption).
1818
1919
Random: {{.Exec}} mlkem768
2020
@@ -40,11 +40,21 @@ func executeMLKEM768(cmd *base.Command, args []string) {
4040
} else {
4141
rand.Read(seed[:])
4242
}
43-
key, _ := mlkem.NewDecapsulationKey768(seed[:])
44-
client := key.EncapsulationKey().Bytes()
45-
hash32 := blake3.Sum256(client)
46-
fmt.Printf("Seed: %v\nClient: %v\nHash32: %v",
43+
seed, client, hash32 := genMLKEM768(&seed)
44+
fmt.Printf("Seed: %v\nClient: %v\nHash32: %v\n",
4745
base64.RawURLEncoding.EncodeToString(seed[:]),
4846
base64.RawURLEncoding.EncodeToString(client),
4947
base64.RawURLEncoding.EncodeToString(hash32[:]))
5048
}
49+
50+
func genMLKEM768(inputSeed *[64]byte) (seed [64]byte, client []byte, hash32 [32]byte) {
51+
if inputSeed == nil {
52+
rand.Read(seed[:])
53+
} else {
54+
seed = *inputSeed
55+
}
56+
key, _ := mlkem.NewDecapsulationKey768(seed[:])
57+
client = key.EncapsulationKey().Bytes()
58+
hash32 = blake3.Sum256(client)
59+
return
60+
}

main/commands/all/vlessenc.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package all
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/xtls/xray-core/main/commands/base"
9+
)
10+
11+
var cmdVLESSEnc = &base.Command{
12+
UsageLine: `{{.Exec}} vlessenc`,
13+
Short: `Generate decryption/encryption json pair (VLESS Encryption)`,
14+
Long: `
15+
Generate decryption/encryption json pair (VLESS Encryption).
16+
`,
17+
}
18+
19+
func init() {
20+
cmdVLESSEnc.Run = executeVLESSEnc // break init loop
21+
}
22+
23+
func executeVLESSEnc(cmd *base.Command, args []string) {
24+
privateKey, password, _, _ := genCurve25519(nil)
25+
serverKey := base64.RawURLEncoding.EncodeToString(privateKey)
26+
clientKey := base64.RawURLEncoding.EncodeToString(password)
27+
decryption := generateDotConfig("mlkem768x25519plus", "native", "600s", serverKey)
28+
encryption := generateDotConfig("mlkem768x25519plus", "native", "0rtt", clientKey)
29+
seed, client, _ := genMLKEM768(nil)
30+
serverKeyPQ := base64.RawURLEncoding.EncodeToString(seed[:])
31+
clientKeyPQ := base64.RawURLEncoding.EncodeToString(client)
32+
decryptionPQ := generateDotConfig("mlkem768x25519plus", "native", "600s", serverKeyPQ)
33+
encryptionPQ := generateDotConfig("mlkem768x25519plus", "native", "0rtt", clientKeyPQ)
34+
fmt.Printf("Choose one Authentication to use, do not mix them. Ephemeral key exchange is Post-Quantum safe anyway.\n\n")
35+
fmt.Printf("Authentication: X25519, not Post-Quantum\n\"decryption\": \"%v\"\n\"encryption\": \"%v\"\n\n", decryption, encryption)
36+
fmt.Printf("Authentication: ML-KEM-768, Post-Quantum\n\"decryption\": \"%v\"\n\"encryption\": \"%v\"\n", decryptionPQ, encryptionPQ)
37+
}
38+
39+
func generateDotConfig(fields ...string) string {
40+
return strings.Join(fields, ".")
41+
}

main/commands/all/x25519.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66

77
var cmdX25519 = &base.Command{
88
UsageLine: `{{.Exec}} x25519 [-i "private key (base64.RawURLEncoding)"] [--std-encoding]`,
9-
Short: `Generate key pair for X25519 key exchange (VLESS, REALITY)`,
9+
Short: `Generate key pair for X25519 key exchange (REALITY, VLESS Encryption)`,
1010
Long: `
11-
Generate key pair for X25519 key exchange (VLESS, REALITY).
11+
Generate key pair for X25519 key exchange (REALITY, VLESS Encryption).
1212
1313
Random: {{.Exec}} x25519
1414

0 commit comments

Comments
 (0)