1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-04 23:40:26 +08:00

fix: addtional data error

This commit is contained in:
源文雨
2023-08-04 17:09:54 +08:00
parent 8163c38884
commit 6f75de6b95
2 changed files with 22 additions and 1 deletions

View File

@@ -5,6 +5,8 @@ import (
"crypto/rand"
"io"
"testing"
"golang.org/x/crypto/chacha20poly1305"
)
func TestXOR(t *testing.T) {
@@ -28,3 +30,22 @@ func TestXOR(t *testing.T) {
}
}
}
func TestXChacha20(t *testing.T) {
l := Link{}
k := make([]byte, 32)
_, err := rand.Read(k)
if err != nil {
t.Fatal(err)
}
l.aead, err = chacha20poly1305.NewX(k)
if err != nil {
t.Fatal(err)
}
data := []byte("12345678")
for i := uint64(0); i < 100000; i++ {
if !bytes.Equal(l.DecodePreshared(uint16(i), l.EncodePreshared(uint16(i), data)), data) {
t.Fatal("unexpected preshared at", i, "addt", uint16(i))
}
}
}

View File

@@ -72,7 +72,7 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
logrus.Debugln("[send] data len after zstd:", len(p.Data))
}
if l.aead != nil {
p.Data = l.EncodePreshared(sndcnt, p.Data)
p.Data = l.EncodePreshared(sndcnt&0x0fff, p.Data)
logrus.Debugln("[send] data len after xchacha20:", len(p.Data))
}
p.Data = l.Encode(teatype, p.Data)