iterate array correctly so that random number is actually used in signing #309
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.
The recently refactored signSync function was not working correctly, making signatures insecure. If you signed two different transactions with the same private key, it was possible to rederive your private key. The problem was the "array" being passed to nextBytes had a length set to some value, but all of the values were undefined, so it couldn't be iterated. This meant that after we generated the random bytes, the random bytes were not being copied to the array. The solution is to iterate over
a
instead, which has all the values defined and thus can be iterated.Here's the faulty code:
Here's the corrected code:
I investigated the BigInteger code and found this:
BigInteger was generating a blank array, and then setting the length to (a>>3)+1, which is why all the values were undefined when it called nextBytes.
In order to make sure this particular issue doesn't occur again, I've added two new tests: One to make sure that several signatures generated in a row are all unique, and another to make sure that many random byte arrays generated in a row are all unique.
The error was introduced in this PR: #289 Ironically, that PR was trying to increase the security of signatures in the browser by using the SecureRandom class, but actually decreased security instead by not actually using the secure random numbers.