Skip to content

x.crypto.chacha20: makes 64-bit counter cipher works as an expected #25363

Merged
spytheman merged 7 commits into
vlang:masterfrom
blackshirt:chacha20
Sep 21, 2025
Merged

x.crypto.chacha20: makes 64-bit counter cipher works as an expected #25363
spytheman merged 7 commits into
vlang:masterfrom
blackshirt:chacha20

Conversation

@blackshirt

@blackshirt blackshirt commented Sep 21, 2025

Copy link
Copy Markdown
Contributor

This PR was motivated by discord disccussion here, especially, for firebird client testing by @einar-hjortdal with 64-bit counter chacha20 cipher that does not works as an expected previously. This patch was inspired from nakagami chacha20 used by golang firebird client in golang firebirdsql. Its adapted into existing x.crypto.chacha20 implementation.

This PR contains:

  • Rewrites of xor_key_stream building block with the new one. Its splitted out keystream generation and xoring bytes into separated logic. Previously, its falls on the same keystream_with_blocksize routine. With this patch, key stream generation separated into Stream.keystream, focussing on keystream generation and updating internal counter, where xor-ing messages happens on xor_key_stream and or keystream_full helper.
  • After this applied, the benchmark for xor_key_stream show a nice improvement, from ±53.430ms down into ±43.668ms. The benchmark also added into bench/bench.v file. It also make it more simpler and readable.
  • Some bits of clean up: split out increasing internal counter into own routine in Stream.inc_ctr
  • Marked a Cipher.reset method for deprecation. It intended for internal use, See some possibles culprits of this misbehaved calls tested by @tankf33der at here. Thanks to @tankf33der 👍
  • Removes unnecessary codes

After this patch applied, in my testing for v firebird client with 64-bit counter activated, the tests was passed successfully. @einar-hjortdal: can you give it a try ?

Cheers,
thanks

@vlang vlang deleted a comment from huly-for-github Bot Sep 21, 2025
@spytheman spytheman merged commit bf41714 into vlang:master Sep 21, 2025
72 checks passed
blackshirt added a commit to blackshirt/v that referenced this pull request Sep 21, 2025
@einar-hjortdal

Copy link
Copy Markdown
Contributor

It seems to work well, good work!

einar-hjortdal added a commit to einar-hjortdal/firebird that referenced this pull request Sep 23, 2025
@blackshirt

Copy link
Copy Markdown
Contributor Author

It seems to work well, good work!

Glad to know its working for you. Thank you for testing it

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.

3 participants