Skip to content

Fix ChaCha20Poly1305 to be singleshot#84

Open
embhorn wants to merge 3 commits intowolfSSL:masterfrom
embhorn:gh83
Open

Fix ChaCha20Poly1305 to be singleshot#84
embhorn wants to merge 3 commits intowolfSSL:masterfrom
embhorn:gh83

Conversation

@embhorn
Copy link
Member

@embhorn embhorn commented Mar 2, 2026

Changes Made

wolfcrypt/ciphers.py — ChaCha20Poly1305 class rewritten

Removed (streaming-style interface):

  • set_aad(), get_aad() — AAD is now passed directly to encrypt/decrypt
  • checkTag() — standalone tag checking
  • final() — had bugs (self._native_type instead of self._native_object) and was unreachable anyway
  • Mode tracking (_mode, _ENCRYPTION/_DECRYPTION state)
  • _native_object / _native_type — no longer needed since we use the one-shot C functions
  • _IV, _aad instance state

New interface (matches AesSiv pattern):

  • init(self, key) — only takes the key
  • encrypt(self, aad, iv, plaintext) → (ciphertext, authTag) — one-shot encryption
  • decrypt(self, aad, iv, authTag, ciphertext) → plaintext — one-shot decryption
  • _key_sizes fixed to [32] (ChaCha20-Poly1305 requires 256-bit keys per RFC 7539)

tests/test_chacha20poly1305.py — updated tests

  • Updated test_encrypt_decrypt to use the new one-shot interface
  • Same instance can now be reused for both encrypt and decrypt
  • Added test_invalid_key_size and test_decrypt_bad_tag tests
  • Removed unused imports (namedtuple)

Fixes #83

@embhorn embhorn self-assigned this Mar 2, 2026
Copilot AI review requested due to automatic review settings March 2, 2026 17:43
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR rewrites the ChaCha20Poly1305 wrapper to use a one-shot (non-streaming) AEAD interface aligned with the existing AesSiv pattern.

Changes:

  • Replaced the streaming-style API with one-shot encrypt(aad, iv, plaintext) and decrypt(aad, iv, authTag, ciphertext).
  • Enforced RFC 7539 key size by restricting _key_sizes to 32 bytes.
  • Updated and expanded tests for the new interface, including invalid key-size and bad-tag cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
wolfcrypt/ciphers.py Replaces streaming state with one-shot calls to the underlying wolfCrypt ChaCha20-Poly1305 functions.
tests/test_chacha20poly1305.py Updates tests to use the new API and adds negative tests for key size and tag verification.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 2, 2026 20:16
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@embhorn embhorn assigned wolfSSL-Bot and unassigned embhorn Mar 2, 2026
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.

ChaCha20Poly1305 interface suggests stream usage, but doesn't support streaming

3 participants