Use builtin :crypto functions (when available) for PBKDF2 and secure constant-time comparison. #37
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.
This changes
Plug.Crypto.KeyGenerator
to use the builtin:crypto.pbkdf2_hmac/5
when available, but fallback to the legacy (existing) method for performing PBKDF2. I modified the test for now to check that the new and old outputs match each other.This also changes
Plug.Crypto.masked_compare/3
andPlug.Crypto.secure_compare/2
to use the builtin:crypto.hash_equals/2
when available, but fallback to the legacy (existing) method for constant-time comparison otherwise.I've added some notes in the comments about which legacy implementations may be dropped once the minimum OTP version is OTP 24.2 and later OTP 25.0.
Performance:
:crypto.pbkdf2_hmac/5
is roughly 3-4x faster and creates significantly less garbage on the heap (in some cases 22,000x less garbage). Available on OTP 24.2+:crypto.hash_equals/2
is roughly 3-30x faster and creates significantly less garbage on the heap (in some cases 6,800x less garbage). Available on OTP 25.0+PBKDF2 Benchmark Results
masked_compare/3 and secure_compare/2 Benchmark Results