@@ -19,6 +19,8 @@ def SHA256(x: bytes) -> bytes:
19
19
20
20
21
21
def scrypt (* , password : str , salt : str , n : int , r : int , p : int , dklen : int ) -> bytes :
22
+ if n * r * p < 2 ** 20 : # 128 MB memory usage
23
+ raise ValueError ("The Scrypt parameters chosen are not secure." )
22
24
if n >= 2 ** (128 * r / 8 ):
23
25
raise ValueError ("The given `n` should be less than `2**(128 * r / 8)`."
24
26
f"\t Got `n={ n } `, r={ r } , 2**(128 * r / 8)={ 2 ** (128 * r / 8 )} " )
@@ -29,6 +31,14 @@ def scrypt(*, password: str, salt: str, n: int, r: int, p: int, dklen: int) -> b
29
31
def PBKDF2 (* , password : bytes , salt : bytes , dklen : int , c : int , prf : str ) -> bytes :
30
32
if 'sha' not in prf :
31
33
raise ValueError (f"String 'sha' is not in `prf`({ prf } )" )
34
+ if 'sha256' in prf and c < 2 ** 18 :
35
+ '''
36
+ Verify the number of rounds of SHA256-PBKDF2. SHA512 not checked as use in BIP39
37
+ does not require, and therefore doesn't use, safe parameters (c=2048).
38
+
39
+ Ref: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#from-mnemonic-to-seed
40
+ '''
41
+ raise ValueError ("The PBKDF2 parameters chosen are not secure." )
32
42
_hash = _sha256 if 'sha256' in prf else _sha512
33
43
res = _PBKDF2 (password = password , salt = salt , dkLen = dklen , count = c , hmac_hash_module = _hash ) # type: ignore
34
44
return res if isinstance (res , bytes ) else res [0 ] # PyCryptodome can return Tuple[bytes]
0 commit comments