Skip to content

Commit

Permalink
Use Chacha20 implementation from official library.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgadams authored and tobischo committed Jan 9, 2024
1 parent c2b9286 commit f7dbbf8
Show file tree
Hide file tree
Showing 31 changed files with 1,567 additions and 2,752 deletions.
6 changes: 3 additions & 3 deletions crypto/chacha.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/sha512"
"encoding/base64"

"github.com/aead/chacha20"
"golang.org/x/crypto/chacha20"
)

// ChaChaStream is a ChaCha20 cipher that implements Stream and Encrypter interface
Expand All @@ -15,7 +15,7 @@ type ChaChaStream struct {

// NewChaChaEncrypter initialize a new ChaChaStream interfaced with Encrypter
func NewChaChaEncrypter(key []byte, iv []byte) (*ChaChaStream, error) {
cipher, err := chacha20.NewCipher(iv, key)
cipher, err := chacha20.NewUnauthenticatedCipher(key, iv)
if err != nil {
return nil, err
}
Expand All @@ -30,7 +30,7 @@ func NewChaChaEncrypter(key []byte, iv []byte) (*ChaChaStream, error) {
func NewChaChaStream(key []byte) (*ChaChaStream, error) {
hash := sha512.Sum512(key)

cipher, err := chacha20.NewCipher(hash[32:44], hash[:32])
cipher, err := chacha20.NewUnauthenticatedCipher(hash[:32], hash[32:44])
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func TestChaCha(t *testing.T) {
payload := []byte("test message")

// Encrypt
c, _ := NewStreamManager(ChaChaStreamID, key)
c, err := NewStreamManager(ChaChaStreamID, key)
if err != nil {
t.Fatalf("Failed to create stream manager: %v", err)
}
crypted := c.Pack(payload)

// Decrypt
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ go 1.20

require (
github.com/aead/argon2 v0.0.0-20180111183520-a87724528b07
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
github.com/google/go-cmp v0.6.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/aead/argon2 v0.0.0-20180111183520-a87724528b07 h1:i9/M2RadeVsPBMNwXFiaYkXQi9lY9VuZeI4Onavd3pA=
github.com/aead/argon2 v0.0.0-20180111183520-a87724528b07/go.mod h1:Tnm/osX+XXr9R+S71o5/F0E60sRkPVALdhWw25qPImQ=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand All @@ -10,14 +8,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3 h1:fJwx88sMf5RXwDwziL0/Mn9Wqs+efMSo/RYcL+37W9c=
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
25 changes: 0 additions & 25 deletions vendor/github.com/aead/chacha20/.gitignore

This file was deleted.

25 changes: 0 additions & 25 deletions vendor/github.com/aead/chacha20/.travis.yml

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/aead/chacha20/LICENSE

This file was deleted.

82 changes: 0 additions & 82 deletions vendor/github.com/aead/chacha20/README.md

This file was deleted.

Loading

0 comments on commit f7dbbf8

Please sign in to comment.