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 CryptoJS argument passing in DeriveEVPKey #1767

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/operations/DeriveEVPKey.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ class DeriveEVPKey extends Operation {
* @returns {string}
*/
run(input, args) {
const passphrase = Utils.convertToByteString(args[0].string, args[0].option),
const passphrase = CryptoJS.enc.Latin1.parse(
Copy link
Member

Choose a reason for hiding this comment

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

This feels a little contrived at the moment. Using Latin1 as the encoding scheme for the string does solve the issue, but doesn't feel optimal. Could we instead replace this with a conversion to a byte array and then create a crypto word array from that? E.g:

// Note: Entirely untested.
CryptoJS.lib.WordArray.create(new UInt8Array(Utils.convertToByteArray(args[0].string, args[0].option)))

Copy link
Member

Choose a reason for hiding this comment

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

That being said, this is a clear improvement over the current broken implementation, so if this isn't a two minute replacement I'm happy to approve and merge in this pull request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't WordArray expect an array of 32bit integers? I'm not sure whether new Uint32Array(new Uint8Array(byteArray).buffer) would have the correct endianness

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like WordArray expects BigEndian version of the buffer.. The Latin1 parse code - at least to me - looks like something as direct as possible given we need an array of 32bit integers represending BE view of the string

Copy link
Member

Choose a reason for hiding this comment

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

That's fair, I'll approve this then! 👍

Utils.convertToByteString(args[0].string, args[0].option)),
keySize = args[1] / 32,
iterations = args[2],
hasher = args[3],
salt = Utils.convertToByteString(args[4].string, args[4].option),
salt = CryptoJS.enc.Latin1.parse(
Utils.convertToByteString(args[4].string, args[4].option)),
key = CryptoJS.EvpKDF(passphrase, salt, { // lgtm [js/insufficient-password-hash]
keySize: keySize,
hasher: CryptoJS.algo[hasher],
Expand Down
Loading