Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use SHA3 instead of crypto/rand to generate VMess padding, reduce syscalls #2862

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion common/crypto/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/rand"
"io"

"golang.org/x/crypto/sha3"

"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/bytespool"
Expand Down Expand Up @@ -233,6 +235,7 @@ type AuthenticationWriter struct {
sizeParser ChunkSizeEncoder
transferType protocol.TransferType
padding PaddingLengthGenerator
prng io.Reader
}

func NewAuthenticationWriter(auth Authenticator, sizeParser ChunkSizeEncoder, writer io.Writer, transferType protocol.TransferType, padding PaddingLengthGenerator) *AuthenticationWriter {
Expand All @@ -241,10 +244,15 @@ func NewAuthenticationWriter(auth Authenticator, sizeParser ChunkSizeEncoder, wr
writer: buf.NewWriter(writer),
sizeParser: sizeParser,
transferType: transferType,
prng: sha3.NewShake128(),
}
if padding != nil {
w.padding = padding
}

// need to seed with at least 32 bytes (256 bits) random bytes
common.Must2(io.CopyN(w.prng.(sha3.ShakeHash), rand.Reader, 32))

return w
}

Expand All @@ -271,7 +279,7 @@ func (w *AuthenticationWriter) seal(b []byte) (*buf.Buffer, error) {
// These paddings will send in clear text.
// To avoid leakage of PRNG internal state, a cryptographically secure PRNG should be used.
paddingBytes := eb.Extend(paddingSize)
common.Must2(rand.Read(paddingBytes))
common.Must2(w.prng.Read(paddingBytes))
}

return eb, nil
Expand Down
Loading