summaryrefslogtreecommitdiff
path: root/src/cryptography/hazmat/primitives
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2022-09-11 22:27:54 -0500
committerGitHub <noreply@github.com>2022-09-12 03:27:54 +0000
commitd6382bb0ceac8927efbbf2878d72b8554c144ef6 (patch)
treecca81d337906fe8b8ee991a684be0d85c96b6432 /src/cryptography/hazmat/primitives
parent30114c6ea9b8fca98d88bfe90aa2f39b6182820d (diff)
downloadcryptography-d6382bb0ceac8927efbbf2878d72b8554c144ef6.tar.gz
use fixed pool to improve perf of aead ChaCha20Poly1305 (#7601)
* use fixed pool to improve perf of aead ChaCha20Poly1305 ~35-45% speedup on benchmarks when reusing the same key for multiple operations * remove unneeded call
Diffstat (limited to 'src/cryptography/hazmat/primitives')
-rw-r--r--src/cryptography/hazmat/primitives/ciphers/aead.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/cryptography/hazmat/primitives/ciphers/aead.py b/src/cryptography/hazmat/primitives/ciphers/aead.py
index 3cdb3ebe4..b4564cfcc 100644
--- a/src/cryptography/hazmat/primitives/ciphers/aead.py
+++ b/src/cryptography/hazmat/primitives/ciphers/aead.py
@@ -9,6 +9,7 @@ import typing
from cryptography import exceptions, utils
from cryptography.hazmat.backends.openssl import aead
from cryptography.hazmat.backends.openssl.backend import backend
+from cryptography.hazmat.bindings._rust import FixedPool
class ChaCha20Poly1305:
@@ -26,11 +27,15 @@ class ChaCha20Poly1305:
raise ValueError("ChaCha20Poly1305 key must be 32 bytes.")
self._key = key
+ self._pool = FixedPool(self._create_fn)
@classmethod
def generate_key(cls) -> bytes:
return os.urandom(32)
+ def _create_fn(self):
+ return aead._aead_create_ctx(backend, self, self._key)
+
def encrypt(
self,
nonce: bytes,
@@ -47,7 +52,10 @@ class ChaCha20Poly1305:
)
self._check_params(nonce, data, associated_data)
- return aead._encrypt(backend, self, nonce, data, [associated_data], 16)
+ with self._pool.acquire() as ctx:
+ return aead._encrypt(
+ backend, self, nonce, data, [associated_data], 16, ctx
+ )
def decrypt(
self,
@@ -59,7 +67,10 @@ class ChaCha20Poly1305:
associated_data = b""
self._check_params(nonce, data, associated_data)
- return aead._decrypt(backend, self, nonce, data, [associated_data], 16)
+ with self._pool.acquire() as ctx:
+ return aead._decrypt(
+ backend, self, nonce, data, [associated_data], 16, ctx
+ )
def _check_params(
self,