-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
argon2id support #11524
Draft
reaperhulk
wants to merge
1
commit into
pyca:main
Choose a base branch
from
reaperhulk:argon2id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
argon2id support #11524
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,9 @@ iOS | |
iterable | ||
Kerberos | ||
Keychain | ||
KiB | ||
kibibyte | ||
kibibytes | ||
Koblitz | ||
Lange | ||
logins | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# This file is dual licensed under the terms of the Apache License, Version | ||
# 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
# for complete details. | ||
|
||
from __future__ import annotations | ||
|
||
from cryptography import utils | ||
from cryptography.exceptions import ( | ||
AlreadyFinalized, | ||
InvalidKey, | ||
UnsupportedAlgorithm, | ||
) | ||
from cryptography.hazmat.bindings._rust import openssl as rust_openssl | ||
from cryptography.hazmat.primitives import constant_time | ||
from cryptography.hazmat.primitives.kdf import KeyDerivationFunction | ||
|
||
|
||
class Argon2id(KeyDerivationFunction): | ||
def __init__( | ||
self, | ||
*, | ||
salt: bytes, | ||
length: int, | ||
iterations: int, | ||
lanes: int, | ||
memory_cost: int, | ||
ad: bytes | None = None, | ||
secret: bytes | None = None, | ||
): | ||
from cryptography.hazmat.backends.openssl.backend import ( | ||
backend as ossl, | ||
) | ||
|
||
if not ossl.argon2_supported(): | ||
raise UnsupportedAlgorithm( | ||
"This version of OpenSSL does not support argon2id" | ||
) | ||
|
||
utils._check_bytes("salt", salt) | ||
# OpenSSL requires a salt of at least 8 bytes | ||
if len(salt) < 8: | ||
raise ValueError("salt must be at least 8 bytes") | ||
# Minimum length is 4 bytes as specified in RFC 9106 | ||
if not isinstance(length, int) or length < 4: | ||
raise ValueError("length must be an integer greater >= 4") | ||
if not isinstance(iterations, int) or iterations < 1: | ||
raise ValueError("iterations must be an integer greater than 0") | ||
if not isinstance(lanes, int) or lanes < 1: | ||
raise ValueError("lanes must be an integer greater than 0") | ||
# Memory cost must be at least 8 * lanes | ||
if not isinstance(memory_cost, int) or memory_cost < 8 * lanes: | ||
raise ValueError("memory_cost must be an integer >= 8 * lanes") | ||
if ad is not None: | ||
utils._check_bytes("ad", ad) | ||
if secret is not None: | ||
utils._check_bytes("secret", secret) | ||
|
||
self._used = False | ||
self._salt = salt | ||
self._length = length | ||
self._iterations = iterations | ||
self._lanes = lanes | ||
self._memory_cost = memory_cost | ||
self._ad = ad | ||
self._secret = secret | ||
|
||
def derive(self, key_material: bytes) -> bytes: | ||
if self._used: | ||
raise AlreadyFinalized("argon2id instances can only be used once.") | ||
self._used = True | ||
|
||
utils._check_byteslike("key_material", key_material) | ||
|
||
return rust_openssl.kdf.derive_argon2id( | ||
key_material, | ||
self._salt, | ||
self._length, | ||
self._iterations, | ||
self._lanes, | ||
self._memory_cost, | ||
self._ad, | ||
self._secret, | ||
) | ||
|
||
def verify(self, key_material: bytes, expected_key: bytes) -> None: | ||
if not constant_time.bytes_eq(self.derive(key_material), expected_key): | ||
raise InvalidKey |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we shouldn't just make this whole class be rust?