Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Fix KEY_LENGTH for PBKDF2 by specifying it in bits rather than bytes #127

Merged
merged 1 commit into from
Jan 20, 2025

Conversation

kjetilkl
Copy link
Collaborator

The KEY_LENGTH constant defined in the KDF.java file is set to 32, in order to stretch the user-supplied password into a 32 byte hash (256 bits) that can be used as the secret key for the ChaCha20 encryption algorithm.

However, with the current code, the PBKDF2_HMAC_SHA256 function only returns a 4 byte hash on line 43.
So, it seems that this function (but not SCRYPT and BCRYPT) requires the key length to be specified in bits rather than bytes, although this fact is not documented anywhere.

I don't think the "case PBKDF2_HMAC_SHA256" block has actually ever been tested before in context, because the ChaCha20 algorithm immediately returns an error if it is not provided with a 256 bits key.

By multiplying the KEY_LENGTH with 8, the PBKDF2_HMAC_SHA256 function returns a 256 bit key, as required, and the hash value is also identical to the one produced by the Python-implementation of Crypt4GH.

@kjetilkl kjetilkl merged commit 8226b6d into master Jan 20, 2025
3 checks passed
@kjetilkl kjetilkl deleted the fix/pbkdf2_keylength branch January 20, 2025 10:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant