Repository navigation
Basically a first implementation of the chacha20 Poly1305 algorithm - #90
Conversation
Still not working.
* Fixed bugs to get stream functions working - not yet tested * shrinking base classes to get the extended aead functions working (ccm, gcm, poly1305) - not yet tested
Minor: removed debug idx variable.
|
Added code to implement XChaCha20 as well |
|
I need to find the time to look at this over the next few weeks. Please be a bit patient! I value your contribution. Thanks for the warning about the changes in the base class. Yes, they need to be evaluated carefully. Does your code contain unit tests? |
|
Yes.. at least the two vectors from the original RFC. I found a bunch of test vectors here: This commit would also tackle the problems with the encodings - I changed the base class so it could be used in such a way. The chacha and GCM modules are already converted. Let me know what you think of that. |
|
We should try to have unit tests for as many areas of the library as possible. |
|
Refering to this one: "do you think it is a good idea to implement them here?" |
|
They actually include the RFC test in their test suit. It is even the first test.... |
|
I try to find original test vectors. Ok, when I took the library over there was some "crude" automatic test program which worked on a text file which contained the test vectors. And there were about a dozend unit tests for some newer date helper functions (from DECUtil). I started to implement unit tests for those test vectors in the text file, without questioning where they came from. Later on somebody reported a few flaws and fixes for them and that made me start to look for original test vectors for all the hash algorithms. After all those years I couldn't find them for all the hash algorithms but for those where I found them I added them. For KDF it was like this: the original author had implemented KDF2. At least that's what he had claimed. I found there were no tests yet and found the KDF specification and the test vectors for KDF2 didn't match the results produced by DEC. So I started to investigate and found out, that the original author had implemented KDF1 and KDF2 and 3 are only small deviations from that. The only difference is the order of 2-3 lines of code in their main method. So I implemented these two as well and added tests from that specification. => I try to find the original specification of the algorithm, be it some PDF, an RFC whatever and check if that contains test vectors. Also NIST is sometimes a source for test vectors or other standard bodies. Does this answer your question? |
|
I see... thanks for the info. |
Removed dll reference Fixed a crucial bug in the poly1305 generation - the ConstTimeCarray32 had wrong brackets applied.
|
These two last commits include the found test vector - about 300 tests. |
…d also AES HW support).
* extended additional buffer size in AES to allow alignment * Moved align code to DECUtil.pas * Fixed an old assembler routine for 64 bit byte swapping * Fixed include file - it only worked for 32bits environments but not for 64 bits * Updated chacha defines so they work with the DEC internal defines (removed my original ifdefs) * Fixed bug appearing in the gcm testcases -> wrong blocksize should throw an exception
|
Just a few updates here:
And... There are now new routines that utilizes the Intel AES instructions + a class variable that allows to fall back to the purepascal implementation (the original). |
|
Any thoughts about the update? |
* Added tests
|
Just checking ;) any news if that implementation is useful? There is now also some AVX assembler code for the Poly1305 implementation which does some decent speed increase I guess... |
|
Thanks for reaching out and sorry that I still haven't found the time to look at it! I can also tell you that I only should have time for this starting in summer, as I'm still in the middle of my preparations for a lecture at the local university (my first one) which will start in March and that's not the only time bound thing at the moment. I do hope that after the international Pascal Congress, where I'm a speaker (yes, I still need to prepare that one as well), see here: https://pascalcongress.com/talks.html things should get a bit calmer and I do hope to find then a bit of time for DEC again. Can we agree on moving this development to its own branch? Is this ok for you? |
…1305 - the current available CPU is queried and the best mode is selected
|
I actually was working on the development branch (a fork of it) - I created a pull request which seems to commit to the master branch though - I hope that is ok... Congrats by the way to the IPC talk and your course in the university :) I hope these will rock 👍 I don't know if it is actually necessary to have a separate branch to work on - I can still use my fork and create pull requests right? (I'm not so fluent with git and try my best ;) |
|
I'm not so fluent with git either and I'd really like you to not work on the master branch. For this I ccreated a new branch for you from the current state of the development branch: |
|
Oh great :) thanks for that - I was able to change the branch target |
|
Great that you managed this! |
Record that AEAD architecture must be separated from ChaCha features, and that a full DUnit to DUnitX migration is the prerequisite before that work.
Record that AEAD architecture must be separated from ChaCha features, and that a full DUnit to DUnitX migration is the prerequisite before that work.
…m#99) Extract Concern A from the ChaCha PR MHumm#90 donor without ChaCha/Poly1305. - Polymorphic FAuthObj replaces dual FGCM/FCCM fields; public IDECAuthenticatedCipher unchanged - Keep EncodeGCM/DecodeGCM/EncodeCCM/DecodeCCM as protected wrappers (no rename break) - GCM multi-call: absorb PR MHumm#99 streaming (GHASH partial + CTR keystream remainder + Done/FFinalized) - Reject PR MHumm#90 fIsLastBlock CTR model (incorrect for unaligned multi-chunk) - Base Done virtual (no-op for CCM one-shot); TDECCipherModes.Done finalizes then verifies ExpectedTag - InitMode always FreeAndNil(FAuthObj) before recreate (leak fix) - Multi-chunk and Done-lifecycle unit tests; corrected large GCM rsp tag See Docs/plans/2026-07-25-aead-architecture.md.
…m#99) Extract Concern A from the ChaCha PR MHumm#90 donor without ChaCha/Poly1305. - Polymorphic FAuthObj replaces dual FGCM/FCCM fields; public IDECAuthenticatedCipher unchanged - Keep EncodeGCM/DecodeGCM/EncodeCCM/DecodeCCM as protected wrappers (no rename break) - GCM multi-call: absorb PR MHumm#99 streaming (GHASH partial + CTR keystream remainder + Done/FFinalized) - Reject PR MHumm#90 fIsLastBlock CTR model (incorrect for unaligned multi-chunk) - Base Done virtual (no-op for CCM one-shot); TDECCipherModes.Done finalizes then verifies ExpectedTag - InitMode always FreeAndNil(FAuthObj) before recreate (leak fix) - Multi-chunk and Done-lifecycle unit tests; corrected large GCM rsp tag See Docs/plans/2026-07-25-aead-architecture.md.
…m donor) Port ChaCha20, XChaCha20 and Poly1305 onto package A AEAD architecture (FAuthObj + Encode/Decode/Done lifecycle). Do not re-apply PR MHumm#90 base/GCM rewrite. - cmPoly1305 mode, IsAuthenticatedBlockMode, IV size exception for Poly1305 - TPoly1305 redesigned for package A API; PAS default; AVX limb bug fixed - TCipher_ChaCha20 / TCipher_XChaCha20 with opt-in cmPoly1305 AEAD - DECCPUSupport (CPUID) + AlignPtr32; no AES-NI port - Formats empty-buffer path generalized via IsAuthenticatedBlockMode - Unit tests + Wycheproof-style vectors; multi-chunk / lifecycle coverage
…m#99) Extract Concern A from the ChaCha PR MHumm#90 donor without ChaCha/Poly1305. - Polymorphic FAuthObj replaces dual FGCM/FCCM fields; public IDECAuthenticatedCipher unchanged - Keep EncodeGCM/DecodeGCM/EncodeCCM/DecodeCCM as protected wrappers (no rename break) - GCM multi-call: absorb PR MHumm#99 streaming (GHASH partial + CTR keystream remainder + Done/FFinalized) - Reject PR MHumm#90 fIsLastBlock CTR model (incorrect for unaligned multi-chunk) - Base Done virtual (no-op for CCM one-shot); TDECCipherModes.Done finalizes then verifies ExpectedTag - InitMode always FreeAndNil(FAuthObj) before recreate (leak fix) - Multi-chunk and Done-lifecycle unit tests; corrected large GCM rsp tag See Docs/plans/2026-07-25-aead-architecture.md.
…m donor) Port ChaCha20, XChaCha20 and Poly1305 onto package A AEAD architecture (FAuthObj + Encode/Decode/Done lifecycle). Do not re-apply PR MHumm#90 base/GCM rewrite. - cmPoly1305 mode, IsAuthenticatedBlockMode, IV size exception for Poly1305 - TPoly1305 redesigned for package A API; PAS default; AVX limb bug fixed - TCipher_ChaCha20 / TCipher_XChaCha20 with opt-in cmPoly1305 AEAD - DECCPUSupport (CPUID) + AlignPtr32; no AES-NI port - Formats empty-buffer path generalized via IsAuthenticatedBlockMode - Unit tests + Wycheproof-style vectors; multi-chunk / lifecycle coverage
Record stack order, fork-only policy, upstream sequence, and links to per-package plans so later MHumm PRs do not rely on branch memory alone. Includes copies of the A/B/C design notes for a self-contained docs branch.
Record that AEAD architecture must be separated from ChaCha features, and that a full DUnit to DUnitX migration is the prerequisite before that work.
…m donor) Port ChaCha20, XChaCha20 and Poly1305 onto package A AEAD architecture (FAuthObj + Encode/Decode/Done lifecycle). Do not re-apply PR MHumm#90 base/GCM rewrite. - cmPoly1305 mode, IsAuthenticatedBlockMode, IV size exception for Poly1305 - TPoly1305 redesigned for package A API; PAS default; AVX limb bug fixed - TCipher_ChaCha20 / TCipher_XChaCha20 with opt-in cmPoly1305 AEAD - DECCPUSupport (CPUID) + AlignPtr32; no AES-NI port - Formats empty-buffer path generalized via IsAuthenticatedBlockMode - Unit tests + Wycheproof-style vectors; multi-chunk / lifecycle coverage
AEAD architecture: FAuthObj + multi-call GCM (from PR #90 concern A)
|
Is the PR dead or unwanted? |
|
No, the PR is not really dead. I'm sorry for not finding the time up to now to integrate this. About yours: the algorithm provided was deemed really useful, it just might be that we need to change some structure things as some internal stuff has been changed lately in order to be able to fix those bugs and make handling more uniform. For instance some stream handling stuff (if I remember correctly) now also requires to call Done at the end. |
|
Hi Mike, definitely not dead, and sorry for the long silence on this. Your PR turned out to be really valuable. Because it touches both the AEAD architecture and adds a lot of new algorithm and SIMD code, we split it into smaller packages so each part can be reviewed and tested on its own. The architecture part (new authenticator base, multi-call GCM, CCM via Done) has already landed in development through #102, #105, #106 and #107, building directly on your work. The ChaCha20/Poly1305 part and your AES-NI routines are prepared on our side and currently being brought up to date and tested. Given the complexity, especially of the SSE/AVX code, this will still take some time. The AI comments you saw came from automated review tools we used during the split, sorry if that felt impersonal. Thanks a lot for your contribution, it was by no means in vain. Best regards, |
|
What I'd need to check is, whether for the AVX routines also pure pascal versions exist, as not all CPUs have AVX, especially not on other platforms. But we'll see this soon I guess. |
|
Hi Markus, good point, and we agree: every assembler routine needs a pure Pascal version anyway. In Delphi 13.x only the two classic x86 compilers accept assembler at all, and only in x86/x64 syntax. DCC32 (Win32) accepts both Hand written ARM/NEON code could only come in as an externally built object file or static library per target ( So our rule for the ChaCha20/Poly1305 and AES-NI packages is: every SSE/AVX/AES-NI routine has an equivalent pure Pascal implementation with identical results, the asm is only compiled for CPUX86/CPUX64 and only used after a runtime CPU feature check, and the tests compare both paths on the same vectors. Two pitfalls we noticed: Best regards, |
|
for every method I implemented here there actually is a pue pascal implementation - this was actually the starting point ;) The code also uses quite a few ifdefs to check if the platform is correct. For ARM the fallback should be automatically the pure pascal implementation - also the cpu flags class flags all the features to "false". |
|
Sorry, but I'm not too well with Git yet. I had created a branch for getting this new algorithm development stabilized (means: adapted to the changes we meanwhile made on our end) and due to what I made the pull request was closed. But the work is far from finished. I tried to resolve all merge conflicts but seem to have intorduced some bugs in that branch now. It will take some more time to fix all these bugs I introduced. Please be a bit more patient while I'm trying to understand what I broke and how to fix it. |
|
You might notice that it is officially in the development branch now. We still might do some further formatting, but it's in the next release for sure. Thanks for contributing this and with the CPUID class we should have a basis for further optimized ASM implementations of algorithms. |
Hope this works as intendent....
Basically I implemented the base chacha20 algorithm and a poly1305 mode following the GCM mode implementation.
I need to say I did quite some rewriting on the base class (don't be mad...) to have a more leaner interface in the
cipher classes.
In addtion I rewrote the GCM part such that it works on streams too - multiple encode/decode calls are now allowed.
This was a rewrite of bug #87 based on the new base authenticator class.
I did not rewrite teh CCM stuff - simply I didn't knew how...
There are also SSE, AVX version of the chacha algorithm so I hope I managed to put it correctly in there - unfortunately I was not
successfull with a SSE/AVX version of the Poly1305 algorithm, this one is way more complicated that chacha...
Let me know what you think of the code...