Skip to content

Commit 3d765ec

Browse files
committed
get rid of unmaintained chacha20poly1305 implementation
1 parent ad2ac2e commit 3d765ec

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ toolchain go1.22.1
66

77
require (
88
github.com/AdguardTeam/golibs v0.20.3
9-
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
10-
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635
119
github.com/ameshkov/dnsstamps v1.0.3
1210
github.com/jessevdk/go-flags v1.5.0
1311
github.com/miekg/dns v1.1.58

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
github.com/AdguardTeam/golibs v0.20.3 h1:5RiDypxBebd4Y2eftwm6JJla18oBqRHwanR7q0rnrxw=
22
github.com/AdguardTeam/golibs v0.20.3/go.mod h1:/votX6WK1PdcZ3T2kBOPjPCGmfhlKixhI6ljYrFRPvI=
3-
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
4-
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
5-
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 h1:52m0LGchQBBVqJRyYYufQuIbVqRawmubW3OFGqK1ekw=
6-
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635/go.mod h1:lmLxL+FV291OopO93Bwf9fQLQeLyt33VJRUg5VJ30us=
73
github.com/ameshkov/dnsstamps v1.0.3 h1:Srzik+J9mivH1alRACTbys2xOxs0lRH9qnTA7Y1OYVo=
84
github.com/ameshkov/dnsstamps v1.0.3/go.mod h1:Ii3eUu73dx4Vw5O4wjzmT5+lkCwovjzaEZZ4gKyIH5A=
95
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

xsecretbox/sharedkey.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,34 @@ package xsecretbox
33
import (
44
"errors"
55

6-
"github.com/aead/chacha20/chacha"
6+
"golang.org/x/crypto/chacha20"
77
"golang.org/x/crypto/curve25519"
88
)
99

1010
// SharedKey computes a shared secret compatible with the one used by
1111
// `crypto_box_xchacha20poly1305`.
12-
func SharedKey(secretKey [32]byte, publicKey [32]byte) ([32]byte, error) {
13-
var sharedKey [32]byte
12+
func SharedKey(secretKey [curve25519.ScalarSize]byte, publicKey [curve25519.PointSize]byte) ([KeySize]byte, error) {
13+
var sharedKey [curve25519.PointSize]byte
1414

1515
sk, err := curve25519.X25519(secretKey[:], publicKey[:])
1616
if err != nil {
1717
return sharedKey, err
1818
}
1919

2020
c := byte(0)
21-
for i := 0; i < 32; i++ {
21+
for i := 0; i < KeySize; i++ {
2222
sharedKey[i] = sk[i]
2323
c |= sk[i]
2424
}
2525
if c == 0 {
2626
return sharedKey, errors.New("weak public key")
2727
}
28-
var nonce [16]byte
29-
chacha.HChaCha20(&sharedKey, &nonce, &sharedKey)
30-
return sharedKey, nil
28+
var nonce [16]byte // HChaCha20 uses only 16 bytes long nonces
29+
30+
hRes, err := chacha20.HChaCha20(sharedKey[:], nonce[:])
31+
if err != nil {
32+
return [KeySize]byte{}, err
33+
}
34+
35+
return ([KeySize]byte)(hRes), nil
3136
}

xsecretbox/xsecretbox.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ import (
44
"crypto/subtle"
55
"errors"
66

7-
"github.com/aead/chacha20/chacha"
8-
"github.com/aead/poly1305"
7+
"golang.org/x/crypto/chacha20"
8+
"golang.org/x/crypto/poly1305"
99
)
1010

1111
const (
1212
// KeySize is what the name suggests
13-
KeySize = 32
13+
KeySize = chacha20.KeySize
1414
// NonceSize is what the name suggests
15-
NonceSize = 24
15+
NonceSize = chacha20.NonceSizeX
1616
// TagSize is what the name suggests
17-
TagSize = 16
17+
TagSize = poly1305.TagSize
18+
// BlockSize is what the name suggests
19+
BlockSize = 64
1820
)
1921

2022
// Seal does what the name suggests
@@ -26,22 +28,25 @@ func Seal(out, nonce, message, key []byte) []byte {
2628
panic("unsupported key size")
2729
}
2830

29-
var firstBlock [64]byte
30-
cipher, _ := chacha.NewCipher(nonce, key, 20)
31+
var firstBlock [BlockSize]byte
32+
cipher, err := chacha20.NewUnauthenticatedCipher(key, nonce)
33+
if err != nil {
34+
panic(err)
35+
}
3136
cipher.XORKeyStream(firstBlock[:], firstBlock[:])
32-
var polyKey [32]byte
33-
copy(polyKey[:], firstBlock[:32])
37+
var polyKey [KeySize]byte
38+
copy(polyKey[:], firstBlock[:KeySize])
3439

3540
ret, out := sliceForAppend(out, TagSize+len(message))
3641
firstMessageBlock := message
37-
if len(firstMessageBlock) > 32 {
38-
firstMessageBlock = firstMessageBlock[:32]
42+
if len(firstMessageBlock) > (BlockSize - KeySize) {
43+
firstMessageBlock = firstMessageBlock[:(BlockSize - KeySize)]
3944
}
4045

4146
tagOut := out
4247
out = out[poly1305.TagSize:]
4348
for i, x := range firstMessageBlock {
44-
out[i] = firstBlock[32+i] ^ x
49+
out[i] = firstBlock[(BlockSize - KeySize)+i] ^ x
4550
}
4651
message = message[len(firstMessageBlock):]
4752
ciphertext := out
@@ -51,7 +56,7 @@ func Seal(out, nonce, message, key []byte) []byte {
5156
cipher.XORKeyStream(out, message)
5257

5358
var tag [TagSize]byte
54-
hash := poly1305.New(polyKey)
59+
hash := poly1305.New(&polyKey)
5560
_, _ = hash.Write(ciphertext)
5661
hash.Sum(tag[:0])
5762
copy(tagOut, tag[:])
@@ -71,15 +76,18 @@ func Open(out, nonce, box, key []byte) ([]byte, error) {
7176
return nil, errors.New("ciphertext is too short")
7277
}
7378

74-
var firstBlock [64]byte
75-
cipher, _ := chacha.NewCipher(nonce, key, 20)
79+
var firstBlock [BlockSize]byte
80+
cipher, err := chacha20.NewUnauthenticatedCipher(key, nonce)
81+
if err != nil {
82+
panic(err)
83+
}
7684
cipher.XORKeyStream(firstBlock[:], firstBlock[:])
77-
var polyKey [32]byte
78-
copy(polyKey[:], firstBlock[:32])
85+
var polyKey [KeySize]byte
86+
copy(polyKey[:], firstBlock[:KeySize])
7987

8088
var tag [TagSize]byte
8189
ciphertext := box[TagSize:]
82-
hash := poly1305.New(polyKey)
90+
hash := poly1305.New(&polyKey)
8391
_, _ = hash.Write(ciphertext)
8492
hash.Sum(tag[:0])
8593
if subtle.ConstantTimeCompare(tag[:], box[:TagSize]) != 1 {
@@ -89,11 +97,11 @@ func Open(out, nonce, box, key []byte) ([]byte, error) {
8997
ret, out := sliceForAppend(out, len(ciphertext))
9098

9199
firstMessageBlock := ciphertext
92-
if len(firstMessageBlock) > 32 {
93-
firstMessageBlock = firstMessageBlock[:32]
100+
if len(firstMessageBlock) > (BlockSize - KeySize) {
101+
firstMessageBlock = firstMessageBlock[:(BlockSize - KeySize)]
94102
}
95103
for i, x := range firstMessageBlock {
96-
out[i] = firstBlock[32+i] ^ x
104+
out[i] = firstBlock[(BlockSize - KeySize)+i] ^ x
97105
}
98106
ciphertext = ciphertext[len(firstMessageBlock):]
99107
out = out[len(firstMessageBlock):]

0 commit comments

Comments
 (0)