From 4a0b45d1ffb1afba0fd2c21db97f9b97e025f30c Mon Sep 17 00:00:00 2001 From: Hellojack <106379370+H1JK@users.noreply.github.com> Date: Wed, 8 Mar 2023 21:43:42 +0800 Subject: [PATCH] Output real private key in x25519 command (#1747) --- main/commands/all/x25519.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main/commands/all/x25519.go b/main/commands/all/x25519.go index 4ab1d09d..e7909d9b 100644 --- a/main/commands/all/x25519.go +++ b/main/commands/all/x25519.go @@ -4,7 +4,6 @@ import ( "crypto/rand" "encoding/base64" "fmt" - "io" "github.com/xtls/xray-core/main/commands/base" "golang.org/x/crypto/curve25519" @@ -44,17 +43,26 @@ func executeX25519(cmd *base.Command, args []string) { goto out } } + if privateKey == nil { privateKey = make([]byte, curve25519.ScalarSize) - if _, err = io.ReadFull(rand.Reader, privateKey); err != nil { + if _, err = rand.Read(privateKey); err != nil { output = err.Error() goto out } } + + // Modify random bytes using algorithm described at: + // https://cr.yp.to/ecdh.html. + privateKey[0] &= 248 + privateKey[31] &= 127 + privateKey[31] |= 64 + if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil { output = err.Error() goto out } + output = fmt.Sprintf("Private key: %v\nPublic key: %v", base64.RawURLEncoding.EncodeToString(privateKey), base64.RawURLEncoding.EncodeToString(publicKey))