Skip to content

Commit ee90b05

Browse files
Fix mutable arguments passed as default arguments.
Function defaults are evaluated once, when the function is defined. The same mutable object is then shared across all calls to the function. If the object is modified, those modifications will persist across calls, which can lead to unexpected behavior.
1 parent 6fbdf3d commit ee90b05

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

wolfcrypt/ciphers.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,12 @@ def verify_pss(self, plaintext, signature):
824824
class RsaPrivate(RsaPublic):
825825
if _lib.KEYGEN_ENABLED:
826826
@classmethod
827-
def make_key(cls, size, rng=Random(), hash_type=None):
827+
def make_key(cls, size, rng=None, hash_type=None):
828828
"""
829829
Generates a new key pair of desired length **size**.
830830
"""
831+
if rng is None:
832+
rng = Random()
831833
rsa = cls(hash_type=hash_type)
832834

833835
ret = _lib.wc_MakeRsaKey(rsa.native_object, size, 65537,
@@ -1183,10 +1185,12 @@ def verify_raw(self, R, S, data):
11831185

11841186
class EccPrivate(EccPublic):
11851187
@classmethod
1186-
def make_key(cls, size, rng=Random()):
1188+
def make_key(cls, size, rng=None):
11871189
"""
11881190
Generates a new key pair of desired length **size**.
11891191
"""
1192+
if rng is None:
1193+
rng = Random()
11901194
ecc = cls()
11911195

11921196
ret = _lib.wc_ecc_make_key(rng.native_object, size,
@@ -1289,12 +1293,14 @@ def shared_secret(self, peer):
12891293

12901294
return _ffi.buffer(shared_secret, secret_size[0])[:]
12911295

1292-
def sign(self, plaintext, rng=Random()):
1296+
def sign(self, plaintext, rng=None):
12931297
"""
12941298
Signs **plaintext**, using the private key data in the object.
12951299
12961300
Returns the signature.
12971301
"""
1302+
if rng is None:
1303+
rng = Random()
12981304
plaintext = t2b(plaintext)
12991305
signature = _ffi.new("byte[%d]" % self.max_signature_size)
13001306

@@ -1312,12 +1318,14 @@ def sign(self, plaintext, rng=Random()):
13121318
return _ffi.buffer(signature, signature_size[0])[:]
13131319

13141320
if _lib.MPAPI_ENABLED:
1315-
def sign_raw(self, plaintext, rng=Random()):
1321+
def sign_raw(self, plaintext, rng=None):
13161322
"""
13171323
Signs **plaintext**, using the private key data in the object.
13181324
13191325
Returns the signature in its two raw components r, s
13201326
"""
1327+
if rng is None:
1328+
rng = Random()
13211329
plaintext = t2b(plaintext)
13221330
R = _ffi.new("mp_int[1]")
13231331
S = _ffi.new("mp_int[1]")
@@ -1449,10 +1457,12 @@ def __init__(self, key=None, pub=None):
14491457
self.decode_key(key,pub)
14501458

14511459
@classmethod
1452-
def make_key(cls, size, rng=Random()):
1460+
def make_key(cls, size, rng=None):
14531461
"""
14541462
Generates a new key pair of desired length **size**.
14551463
"""
1464+
if rng is None:
1465+
rng = Random()
14561466
ed25519 = cls()
14571467

14581468
ret = _lib.wc_ed25519_make_key(rng.native_object, size,
@@ -1645,10 +1655,12 @@ def __init__(self, key=None, pub=None):
16451655
self.decode_key(key,pub)
16461656

16471657
@classmethod
1648-
def make_key(cls, size, rng=Random()):
1658+
def make_key(cls, size, rng=None):
16491659
"""
16501660
Generates a new key pair of desired length **size**.
16511661
"""
1662+
if rng is None:
1663+
rng = Random()
16521664
ed448 = cls()
16531665

16541666
ret = _lib.wc_ed448_make_key(rng.native_object, size,
@@ -1862,13 +1874,15 @@ def decode_key(self, pub_key):
18621874
if ret < 0: # pragma: no cover
18631875
raise WolfCryptError("wc_KyberKey_DecodePublicKey() error (%d)" % ret)
18641876

1865-
def encapsulate(self, rng=Random()):
1877+
def encapsulate(self, rng=None):
18661878
"""
18671879
:param rng: random number generator for an encupsulation
18681880
:type rng: Random
18691881
:return: tuple of a shared secret (first element) and the cipher text (second element)
18701882
:rtype: tuple[bytes, bytes]
18711883
"""
1884+
if rng is None:
1885+
rng = Random()
18721886
ct_size = self.ct_size
18731887
ss_size = self.ss_size
18741888
ct = _ffi.new(f"unsigned char[{ct_size}]")
@@ -1906,7 +1920,7 @@ def encapsulate_with_random(self, rand):
19061920

19071921
class MlKemPrivate(_MlKemBase):
19081922
@classmethod
1909-
def make_key(cls, mlkem_type, rng=Random()):
1923+
def make_key(cls, mlkem_type, rng=None):
19101924
"""
19111925
:param mlkem_type: ML-KEM type
19121926
:type mlkem_type: MlKemType
@@ -1915,6 +1929,8 @@ def make_key(cls, mlkem_type, rng=Random()):
19151929
:return: `MlKemPrivate` object
19161930
:rtype: MlKemPrivate
19171931
"""
1932+
if rng is None:
1933+
rng = Random()
19181934
mlkem_priv = cls(mlkem_type)
19191935
ret = _lib.wc_KyberKey_MakeKey(mlkem_priv.native_object, rng.native_object)
19201936

@@ -2150,7 +2166,7 @@ def verify(self, signature, message):
21502166

21512167
class MlDsaPrivate(_MlDsaBase):
21522168
@classmethod
2153-
def make_key(cls, mldsa_type, rng=Random()):
2169+
def make_key(cls, mldsa_type, rng=None):
21542170
"""
21552171
:param mldsa_type: ML-DSA type
21562172
:type mldsa_type: MlDsaType
@@ -2159,6 +2175,8 @@ def make_key(cls, mldsa_type, rng=Random()):
21592175
:return: `MlDsaPrivate` object
21602176
:rtype: MlDsaPrivate
21612177
"""
2178+
if rng is None:
2179+
rng = Random()
21622180
mldsa_priv = cls(mldsa_type)
21632181
ret = _lib.wc_dilithium_make_key(
21642182
mldsa_priv.native_object, rng.native_object
@@ -2243,7 +2261,7 @@ def decode_key(self, priv_key, pub_key=None):
22432261
if pub_key is not None:
22442262
self._decode_pub_key(pub_key)
22452263

2246-
def sign(self, message, rng=Random()):
2264+
def sign(self, message, rng=None):
22472265
"""
22482266
:param message: message to be signed
22492267
:type message: bytes or str
@@ -2252,6 +2270,8 @@ def sign(self, message, rng=Random()):
22522270
:return: signature
22532271
:rtype: bytes
22542272
"""
2273+
if rng is None:
2274+
rng = Random()
22552275
msg_bytestype = t2b(message)
22562276
in_size = self.sig_size
22572277
signature = _ffi.new(f"byte[{in_size}]")

0 commit comments

Comments
 (0)