-
Notifications
You must be signed in to change notification settings - Fork 29
Make the random generator of _Rsa and RsaPublic configurable. #93
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -650,13 +650,13 @@ class _Rsa(object): # pylint: disable=too-few-public-methods | |||||||||||
| _mgf = None | ||||||||||||
| _hash_type = None | ||||||||||||
|
|
||||||||||||
| def __init__(self): | ||||||||||||
| def __init__(self, rng=Random()): | ||||||||||||
| self.native_object = _ffi.new("RsaKey *") | ||||||||||||
| ret = _lib.wc_InitRsaKey(self.native_object, _ffi.NULL) | ||||||||||||
| if ret < 0: # pragma: no cover | ||||||||||||
| raise WolfCryptError("Invalid key error (%d)" % ret) | ||||||||||||
|
|
||||||||||||
| self._random = Random() | ||||||||||||
| self._random = rng | ||||||||||||
| if _lib.RSA_BLINDING_ENABLED: | ||||||||||||
| ret = _lib.wc_RsaSetRNG(self.native_object, | ||||||||||||
| self._random.native_object) | ||||||||||||
|
|
@@ -690,12 +690,14 @@ def _get_mgf(self): | |||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class RsaPublic(_Rsa): | ||||||||||||
| def __init__(self, key=None, hash_type=None): | ||||||||||||
| def __init__(self, key=None, hash_type=None, rng=None): | ||||||||||||
| if key is not None: | ||||||||||||
| key = t2b(key) | ||||||||||||
| self._hash_type = hash_type | ||||||||||||
| if rng is None: | ||||||||||||
| rng = Random() | ||||||||||||
|
|
||||||||||||
| _Rsa.__init__(self) | ||||||||||||
| _Rsa.__init__(self, rng) | ||||||||||||
|
|
||||||||||||
| idx = _ffi.new("word32*") | ||||||||||||
| idx[0] = 0 | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 [Medium] The PR's stated goal is to make the random generator of Suggestion:
Suggested change
|
||||||||||||
|
|
||||||||||||
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.
🟠 [High] Mutable default argument
rng=Random()in_Rsa.__init__🚫 BLOCK
bugThe default argument
rng=Random()is a classic Python mutable-default-argument bug.Random()is evaluated once at class definition time, and the same object is reused for every call that omitsrng. This means allRsaPrivateinstances (whose__init__calls_Rsa.__init__(self)without providingrngon line 851) will share the exact sameRandomobject. If one instance is garbage-collected and itsRandomobject cleaned up, every other instance sharing that default will hold a dangling reference. Even without cleanup issues, sharing a single RNG across independent key objects is a correctness and isolation problem. Ironically, theRsaPublicsubclass does this correctly with therng=None/if rng is None: rng = Random()pattern — the base class should use the same approach.Suggestion: