Skip to content

Commit

Permalink
aead: extend buffer for aead only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
iwasaki-kenta committed Feb 15, 2020
1 parent 611a041 commit b89a566
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@ import (
"math/rand"
)

func extendFront(buf []byte, n int) []byte {
if len(buf) < n {
clone := make([]byte, n+len(buf))
copy(clone[n:], buf)

return clone
}

return append(buf[:n], buf...)
}

func extendBack(buf []byte, n int) []byte {
n += len(buf)
if nn := n - cap(buf); nn > 0 {
buf = append(buf[:cap(buf)], make([]byte, nn)...)
}
return buf[:n]
}

func encryptAEAD(suite cipher.AEAD, buf []byte) ([]byte, error) {
a, b := suite.NonceSize(), len(buf)

buf = append(make([]byte, a), append(buf, make([]byte, suite.Overhead())...)...)
buf = extendFront(buf, a)
buf = extendBack(buf, b)

if _, err := rand.Read(buf[:a]); err != nil {
return nil, err
Expand Down

0 comments on commit b89a566

Please sign in to comment.