Skip to content

Commit

Permalink
aead: remove opt in encryptAEAD to prevent data race
Browse files Browse the repository at this point in the history
  • Loading branch information
iwasaki-kenta committed Feb 5, 2020
1 parent a074e02 commit 274f07b
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package noise

import (
"crypto/cipher"
"github.com/valyala/bytebufferpool"
"io"
"math/rand"
)

func encryptAEAD(suite cipher.AEAD, buf []byte) ([]byte, error) {
dst := bytebufferpool.Get()
defer bytebufferpool.Put(dst)
nonce := make([]byte, suite.NonceSize())

dst.B = append(dst.B[:0], make([]byte, suite.NonceSize())...)
dst.B = dst.B[:suite.NonceSize()]

if _, err := rand.Read(dst.B[:suite.NonceSize()]); err != nil {
if _, err := rand.Read(nonce[:]); err != nil {
return nil, err
}

return append(dst.B, suite.Seal(buf[:0], dst.B[:suite.NonceSize()], buf, nil)...), nil
return append(nonce, suite.Seal(buf[:0], nonce[:suite.NonceSize()], buf, nil)...), nil
}

func decryptAEAD(suite cipher.AEAD, buf []byte) ([]byte, error) {
Expand Down

0 comments on commit 274f07b

Please sign in to comment.