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 secret key decryption #59

Closed
wants to merge 1 commit into from
Closed

Conversation

sug0
Copy link

@sug0 sug0 commented Jul 20, 2023

Fixes decryption of secret keys

@sug0 sug0 marked this pull request as draft July 20, 2023 03:45
@sug0 sug0 marked this pull request as ready for review July 20, 2023 03:59
@badboy
Copy link
Owner

badboy commented Jul 20, 2023

Can you expand on why you think this is broken? (I haven't looked at the code in a while so I might be missing context)

Copy link
Author

@sug0 sug0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @badboy, sure!

Comment on lines +144 to +146
if kdf_rounds == 0 {
PrivateKey::verify_keypair(&complete_key)?;
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I think before this change, this check would have never passed for encrypted keys. the reason is the public key derived from the encrypted secret key would never match the decrypted public key. I was hitting this issue when decoding encrypted secret keys.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this seems like a bug in hindsight of writing it. I even seem to have ignored the second part of my own comment :D It makes no sense to try validating a ciphertext blob.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a regression test of PrivateKey::from_bytes that checks both cases of this branch? (doesn't need to test a failed decryption, just that it is only ran when kdf_rounds is set appropriately)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRed this as its own fix in #61

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I totally forgot about this, as life got in the way 😅 my bad!

@@ -119,7 +119,7 @@ impl PrivateKey {
} = &derivation_info
{
let kdf_rounds = *kdf_rounds;
Self::inner_kdf_mix(&mut complete_key.0[..32], kdf_rounds, &salt, passphrase)?;
Self::inner_kdf_mix(&mut complete_key.0[..], kdf_rounds, &salt, passphrase)?;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this change and...

@@ -146,7 +146,7 @@ impl PrivateKey {
let mut encrypted_key = self.complete_key.clone(); // Cheap :)

match Self::inner_kdf_mix(
&mut encrypted_key[..32],
&mut encrypted_key[..],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...this change, to be honest I'm still not 100% sold on the reason why it fixes the issues I was having during decryption. what it essentially fixes is XOR-ing the complete key with the derived key from kdf vs. only XOR-ing the secret key bytes, I believe. before I ran into Error:WrongKey I think.

Copy link
Collaborator

@BlackHoleFox BlackHoleFox Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if I've missed a detail or if there is something amiss elsewhere if that's the case. From my knowledge it should just be the secret key part that gets passed into XOR.

What error are you getting back, WrongKey or BadPassword? whoops can't read today.

@sug0 sug0 closed this Oct 7, 2023
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