Fix Windows file mode issue for key file updates #31
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
fopencall indemo.cline 150 uses text mode ("r+") when opening key files for updates. This causes issues on Windows due to automatic line ending translation:\r\n(CRLF) is translated to\n(LF)\n(LF) is expanded to\r\n(CRLF)This breaks the LMS algorithm when updating the signature counter in key files. For example, when the counter reaches
0x0A, it gets written as0x0D0Ainstead of just0x0A, corrupting the key format and causing signature failures.Solution
Changed the file mode from
"r+"(text mode) to"rb+"(binary mode) to prevent automatic line ending translation and ensure byte-for-byte accuracy.Changes
demo.cfopen(filename, "r+")→fopen(filename, "rb+")Testing
make CC=C:\msys64\ucrt64\bin\gcc.exe AR=C:\msys64\ucrt64\bin\ar.exe CFLAGS="-Wall -O3 -static"Reproduction Steps (Before Fix)
0x0A(or any byte containing\n), the key file gets corruptedImpact
This fix ensures the LMS implementation works correctly on Windows by maintaining binary file integrity for key updates.