summaryrefslogtreecommitdiff
path: root/internal/stupidgcm/stupidgcm.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2017-06-29 18:52:33 +0200
committerJakob Unterwurzacher2017-06-29 18:52:33 +0200
commit0cc6f53496d71ff3b8150f32116741147c1cab27 (patch)
tree21a4dc8b826102a0f8ef608158da77305a431af5 /internal/stupidgcm/stupidgcm.go
parentc12a50d4c8c24ff55d682b2fed1923be4f87f182 (diff)
stupidgcm: use "dst" as the output buffer it is big enough
This saves an allocation of the ciphertext block.
Diffstat (limited to 'internal/stupidgcm/stupidgcm.go')
-rw-r--r--internal/stupidgcm/stupidgcm.go15
1 files changed, 14 insertions, 1 deletions
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...)
}