Skip to content

Switch to using ChaCha20Poly1305Reusable for encryption#413

Merged
ikalchev merged 4 commits intoikalchev:devfrom
bdraco:reusable_chacha
Jun 27, 2022
Merged

Switch to using ChaCha20Poly1305Reusable for encryption#413
ikalchev merged 4 commits intoikalchev:devfrom
bdraco:reusable_chacha

Conversation

@bdraco
Copy link
Copy Markdown
Contributor

@bdraco bdraco commented Jun 18, 2022

The ChaCha20Poly1305 that comes with cryptography recreates the
ctx every time encrypt is called. Since we call encrypt in
a loop, we can avoid this overhead by using ChaCha20Poly1305Reusable
instead as we are doing everything in the same thread.

This makes quite a difference for latency of large payloads such as camera images. It also seems to fix the Home app stalling out on iOS 16 betas when using them with Home Assistant's HomeKit integration.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 18, 2022

Codecov Report

Merging #413 (1d32a2f) into dev (6c2b95c) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##              dev     #413   +/-   ##
=======================================
  Coverage   91.77%   91.77%           
=======================================
  Files          20       20           
  Lines        2116     2116           
  Branches      287      287           
=======================================
  Hits         1942     1942           
  Misses        132      132           
  Partials       42       42           
Impacted Files Coverage Δ
pyhap/accessory.py 96.64% <ø> (ø)
pyhap/camera.py 77.21% <100.00%> (ø)
pyhap/hap_crypto.py 100.00% <100.00%> (ø)
pyhap/hap_handler.py 81.52% <100.00%> (ø)
pyhap/hap_protocol.py 99.38% <100.00%> (ø)

The ChaCha20Poly1305 that comes with cryptography recreates the
ctx every time encrypt is called.  Since we call encrypt in
 a loop, we can avoid this overhead by using ChaCha20Poly1305Reusable
instead as we are doing everything in the same thread and not concerned
about thread safety
@bdraco bdraco force-pushed the reusable_chacha branch from 6f5d693 to 4f3b6a2 Compare June 18, 2022 17:29
@bdraco bdraco marked this pull request as ready for review June 18, 2022 17:44
@ikalchev ikalchev merged commit 519f22f into ikalchev:dev Jun 27, 2022
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

Successfully merging this pull request may close these issues.

2 participants