Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nonce byte array allocations #30

Open
nbrownus opened this issue Sep 4, 2018 · 0 comments
Open

nonce byte array allocations #30

nbrownus opened this issue Sep 4, 2018 · 0 comments

Comments

@nbrownus
Copy link
Contributor

nbrownus commented Sep 4, 2018

nonce preparation in Encrypt and Decrypt causes an allocation. In an application encrypting/decrypting at 200k+ times a second this becomes rather noticeable in the garbage collector.

One way I've been thinking of working around this is to provide a function similar to this:

func (c aeadCipher) EncryptNB(out []byte, n uint64, ad, plaintext []byte, nb []byte) []byte {
	nb[0] = 0
	nb[1] = 0
	nb[2] = 0
	nb[3] = 0
	binary.BigEndian.PutUint64(nb[4:], n)
	return c.Seal(out, nb, plaintext, ad)
}

Obviously there are a handful of issues here, is the byte array big enough, too big, being used by another thread? Adding (some of) those checks would still likely be less overhead though.

I can bring crypto into my part of the program, but this feels bad:

nb[0] = 0
nb[1] = 0
nb[2] = 0
nb[3] = 0
binary.BigEndian.PutUint64(nb[4:], n)
return s.c.(cipher.AEAD).Open(out, nb, ciphertext, ad)

Other thoughts would be to internalize a heap for storing nonce byte arrays.

Wanted to get your thoughts on the matter before I sent any PRs your way, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant