mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-27 22:30:26 +08:00
fix(crypto): xchacha20poly1305 encode
This commit is contained in:
@@ -84,19 +84,20 @@ func (l *Link) Decode(teatype uint8, additional uint16, b []byte) (db []byte, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// encode 使用 xchacha20poly1305 加密
|
// encode 使用 xchacha20poly1305 加密
|
||||||
func encode(aead cipher.AEAD, additional uint16, b []byte) (eb []byte) {
|
func encode(aead cipher.AEAD, additional uint16, b []byte) []byte {
|
||||||
nsz := aead.NonceSize()
|
nsz := aead.NonceSize()
|
||||||
// Select a random nonce, and leave capacity for the ciphertext.
|
// Accocate capacity for all the stuffs.
|
||||||
nonce := make([]byte, nsz, nsz+len(b)+aead.Overhead())
|
buf := make([]byte, 2+nsz+len(b)+aead.Overhead())
|
||||||
|
binary.LittleEndian.PutUint16(buf[:2], additional)
|
||||||
|
nonce := buf[2 : 2+nsz]
|
||||||
|
// Select a random nonce
|
||||||
_, err := rand.Read(nonce)
|
_, err := rand.Read(nonce)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
panic(err)
|
||||||
}
|
}
|
||||||
// Encrypt the message and append the ciphertext to the nonce.
|
// Encrypt the message and append the ciphertext to the nonce.
|
||||||
var buf [2]byte
|
eb := aead.Seal(nonce[nsz:nsz], nonce, b, buf[:2])
|
||||||
binary.LittleEndian.PutUint16(buf[:], additional)
|
return nonce[:nsz+len(eb)]
|
||||||
eb = aead.Seal(nonce, nonce, b, buf[:])
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode 使用 xchacha20poly1305 解密
|
// decode 使用 xchacha20poly1305 解密
|
||||||
@@ -107,6 +108,9 @@ func decode(aead cipher.AEAD, additional uint16, b []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
// Split nonce and ciphertext.
|
// Split nonce and ciphertext.
|
||||||
nonce, ciphertext := b[:nsz], b[nsz:]
|
nonce, ciphertext := b[:nsz], b[nsz:]
|
||||||
|
if len(ciphertext) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
// Decrypt the message and check it wasn't tampered with.
|
// Decrypt the message and check it wasn't tampered with.
|
||||||
var buf [2]byte
|
var buf [2]byte
|
||||||
binary.LittleEndian.PutUint16(buf[:], additional)
|
binary.LittleEndian.PutUint16(buf[:], additional)
|
||||||
|
|||||||
@@ -43,14 +43,18 @@ func TestXChacha20(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
data := []byte("12345678")
|
data := make([]byte, 4096)
|
||||||
for i := uint64(0); i < 100000; i++ {
|
_, err = rand.Read(data)
|
||||||
db, err := decode(aead, uint16(i), encode(aead, uint16(i), data))
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i := 0; i < 4096; i++ {
|
||||||
|
db, err := decode(aead, uint16(i), encode(aead, uint16(i), data[:i]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !bytes.Equal(db, data) {
|
if !bytes.Equal(db, data[:i]) {
|
||||||
t.Fatal("unexpected preshared at", i, "addt", uint16(i))
|
t.Fatal("unexpected preshared at idx(len)", i, "addt", uint16(i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user