From 0cc6f53496d71ff3b8150f32116741147c1cab27 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 29 Jun 2017 18:52:33 +0200 Subject: stupidgcm: use "dst" as the output buffer it is big enough This saves an allocation of the ciphertext block. --- internal/stupidgcm/stupidgcm.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'internal/stupidgcm/stupidgcm.go') diff --git a/internal/stupidgcm/stupidgcm.go b/internal/stupidgcm/stupidgcm.go index 5cc6c3c..54915df 100644 --- a/internal/stupidgcm/stupidgcm.go +++ b/internal/stupidgcm/stupidgcm.go @@ -55,7 +55,17 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte { if len(in) == 0 { log.Panic("Zero-length input data is not supported") } - buf := make([]byte, len(in)+tagLen) + + // If the "dst" slice is large enough we can use it as our output buffer + outLen := len(in) + tagLen + var buf []byte + inplace := false + if cap(dst)-len(dst) >= outLen { + inplace = true + buf = dst[len(dst) : len(dst)+outLen] + } else { + buf = make([]byte, outLen) + } // https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode @@ -115,6 +125,9 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte { // Free scratch space C.EVP_CIPHER_CTX_free(ctx) + if inplace { + return dst[:len(dst)+outLen] + } return append(dst, buf...) } -- cgit v1.2.3