Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that assumes toByteArrayUnsigned() always returns 32 bytes. #5

Merged
merged 1 commit into from
Oct 30, 2014
Merged

Conversation

anaoum
Copy link
Contributor

@anaoum anaoum commented Oct 23, 2014

To reproduce this bug, try entering the following XPRIV:
xprv9s21ZrQH143K3GMYVMZBZCGProVPNqGXRXz3ktUYt1rXG813HhysPLeNj8AfahpW2d4Tc678FMYndMrw9sqUFuLD2mug9pwojMuNusRbsv9
and use the following derivation path: m/44'

The correct derived private key should be:
xprv9tzN9JZpuT1wiz7y2qsBnjttAxvUQcdjCCEVXdMQmp32FeX4vGve5PA7yFan15Jo71HrxsA6VAnpqWW2JNoaXaLe3NeqWKoYS76AEBfioN7
but the code was previously outputting:
DeaWiSNc9gKmPJgBaytskvDH3Cz4tebKSjxu9t2hSyGVx1gt2xdGPVXjPHoUQd9LBRpcJQDbFdQdEAzqWq2eVeErNLsGwEEWGEGgArZjHap4aP

This was as a result of line 185 in bip32.js calling
this.eckey.priv.toByteArrayUnsigned() without verifying that the
returned byte array had the correct length required for serialization
(32 bytes).

Furthermore, errors would cascade down the path dangerously unnoticed.
For example, using the path m/44'/0' with the XPRIV mentioned above,
the correct derived private key should be:
xprv9xUGaEZ5EBM1tGE9Ujp2EaPC85Pt8PRwMSmk53rvy3YmJbg6jBZM3mybraxFBWFgzrfhEhiiugDMArUqiGDSQF3zHL8wY6AZBXE77Jq2utJ
however the original code would give:
xprv9xUGaEZ5EBM1ueG3H1yNEgQuk9ivpzaHhVbp58hcCJSyrinvZC3k6uvzobs6Av5UmLvjfRQK6AWq2fL9ej27wjr3BURJFddYzvovFYngqLS

This was quite dangerous as there is nothing obviously wrong with the
output. This cascading bug was due to line 254 in bip32.js which also
incorrectly assumed that toByteArrayUnsigned() always returns a 32 byte
long array.

To reproduce this bug, try entering the following XPRIV:
xprv9s21ZrQH143K3GMYVMZBZCGProVPNqGXRXz3ktUYt1rXG813HhysPLeNj8AfahpW2d4Tc678FMYndMrw9sqUFuLD2mug9pwojMuNusRbsv9
and use the following derivation path: m/44'

The correct derived private key should be:
xprv9tzN9JZpuT1wiz7y2qsBnjttAxvUQcdjCCEVXdMQmp32FeX4vGve5PA7yFan15Jo71HrxsA6VAnpqWW2JNoaXaLe3NeqWKoYS76AEBfioN7
but the code was previously outputting:
DeaWiSNc9gKmPJgBaytskvDH3Cz4tebKSjxu9t2hSyGVx1gt2xdGPVXjPHoUQd9LBRpcJQDbFdQdEAzqWq2eVeErNLsGwEEWGEGgArZjHap4aP

This was as a result of line 185 in bip32.js calling
this.eckey.priv.toByteArrayUnsigned() without verifying that the
returned byte array had the correct length required for serialization
(32 bytes).

Furthermore, errors would cascade down the path dangerously unnoticed.
For example, using the path m/44'/0' with the XPRIV mentioned above,
the correct derived private key should be:
xprv9xUGaEZ5EBM1tGE9Ujp2EaPC85Pt8PRwMSmk53rvy3YmJbg6jBZM3mybraxFBWFgzrfhEhiiugDMArUqiGDSQF3zHL8wY6AZBXE77Jq2utJ
however the original code would give:
xprv9xUGaEZ5EBM1ueG3H1yNEgQuk9ivpzaHhVbp58hcCJSyrinvZC3k6uvzobs6Av5UmLvjfRQK6AWq2fL9ej27wjr3BURJFddYzvovFYngqLS

This was quite dangerous as there is nothing obviously wrong with the
output. This cascading bug was due to line 254 in bip32.js which also
incorrectly assumed that toByteArrayUnsigned() always returns a 32 byte
long array.
@anaoum
Copy link
Contributor Author

anaoum commented Oct 24, 2014

This solves issue #4

sarchar added a commit that referenced this pull request Oct 30, 2014
Fix bug that assumes toByteArrayUnsigned() always returns 32 bytes.
@sarchar sarchar merged commit c061b77 into bip32:master Oct 30, 2014
@sarchar
Copy link
Collaborator

sarchar commented Oct 30, 2014

Merged. Thanks for the commit.

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.

None yet

2 participants