Skip to content

Commit

Permalink
fix(crypto): xchacha20poly1305 encode
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Jul 12, 2024
1 parent 9336ab6 commit 677b11f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 12 additions & 8 deletions gold/link/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ func (l *Link) Decode(teatype uint8, additional uint16, b []byte) (db []byte, er
}

// 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()
// Select a random nonce, and leave capacity for the ciphertext.
nonce := make([]byte, nsz, nsz+len(b)+aead.Overhead())
// Accocate capacity for all the stuffs.
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)
if err != nil {
return
panic(err)
}
// Encrypt the message and append the ciphertext to the nonce.
var buf [2]byte
binary.LittleEndian.PutUint16(buf[:], additional)
eb = aead.Seal(nonce, nonce, b, buf[:])
return
eb := aead.Seal(nonce[nsz:nsz], nonce, b, buf[:2])
return nonce[:nsz+len(eb)]
}

// decode 使用 xchacha20poly1305 解密
Expand All @@ -107,6 +108,9 @@ func decode(aead cipher.AEAD, additional uint16, b []byte) ([]byte, error) {
}
// Split nonce and ciphertext.
nonce, ciphertext := b[:nsz], b[nsz:]
if len(ciphertext) == 0 {
return nil, nil
}
// Decrypt the message and check it wasn't tampered with.
var buf [2]byte
binary.LittleEndian.PutUint16(buf[:], additional)
Expand Down
14 changes: 9 additions & 5 deletions gold/link/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ func TestXChacha20(t *testing.T) {
if err != nil {
t.Fatal(err)
}
data := []byte("12345678")
for i := uint64(0); i < 100000; i++ {
db, err := decode(aead, uint16(i), encode(aead, uint16(i), data))
data := make([]byte, 4096)
_, err = rand.Read(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 {
t.Fatal(err)
}
if !bytes.Equal(db, data) {
t.Fatal("unexpected preshared at", i, "addt", uint16(i))
if !bytes.Equal(db, data[:i]) {
t.Fatal("unexpected preshared at idx(len)", i, "addt", uint16(i))
}
}
}
Expand Down

0 comments on commit 677b11f

Please sign in to comment.