2424_PLAINTEXT_STRING_BUFFER_LEN_MAX = 4096
2525"""Maximum length of plaintext string values that can be encrypted."""
2626
27- _Hash = hashlib .sha512
27+ _HASH = hashlib .sha512
2828"""Hash function used for HKDF and matching."""
2929
30-
3130def _hkdf_extract (salt : bytes , input_key : bytes ) -> bytes :
3231 """
3332 Extracts a pseudorandom key (PRK) using HMAC with the given salt and input key material.
34- If the salt is empty, a zero-filled byte string of the same length as the hash function's digest size is used.
33+ If the salt is empty, a zero-filled byte string of the same length as the hash function's
34+ digest size is used.
3535 """
3636 if len (salt ) == 0 :
37- salt = bytes ([0 ] * _Hash ().digest_size )
38- return hmac .new (salt , input_key , _Hash ).digest ()
37+ salt = bytes ([0 ] * _HASH ().digest_size )
38+ return hmac .new (salt , input_key , _HASH ).digest ()
3939
4040def _hkdf_expand (pseudo_random_key : bytes , info : bytes , length : int ) -> bytes :
4141 """
42- Expands the pseudo_random_key into an output key material (OKM) of the desired length using HMAC-based expansion.
42+ Expands the pseudo_random_key into an output key material (OKM) of the desired length using
43+ HMAC-based expansion.
4344 """
44- t = b""
45- okm = b""
45+ t = b''
46+ okm = b''
4647 i = 0
4748 while len (okm ) < length :
4849 i += 1
49- t = hmac .new (pseudo_random_key , t + info + bytes ([i ]), _Hash ).digest ()
50+ t = hmac .new (pseudo_random_key , t + info + bytes ([i ]), _HASH ).digest ()
5051 okm += t
5152 return okm [:length ]
5253
53- def _hkdf (length : int , input_key : bytes , salt : bytes = b"" , info : bytes = b"" ) -> bytes :
54+ def _hkdf (length : int , input_key : bytes , salt : bytes = b'' , info : bytes = b'' ) -> bytes :
5455 """
5556 Extract a pseudorandom key of `length` from `input_key` and optionally `salt` and `info`.
5657 """
@@ -63,7 +64,7 @@ def _random_bytes(length: int, seed: Optional[bytes] = None, salt: Optional[byte
6364 the seed if one is supplied).
6465 """
6566 if seed is not None :
66- return _hkdf (length , seed , b"" if salt is None else salt )
67+ return _hkdf (length , seed , b'' if salt is None else salt )
6768
6869 return secrets .token_bytes (length )
6970
@@ -93,7 +94,7 @@ def _random_int(
9394 integer = None
9495 index = 0
9596 while integer is None or integer > range_ :
96- bytes_ = bytearray (_random_bytes (8 , seed , index .to_bytes (8 , 'little' )))
97+ bytes_ = bytearray (_random_bytes (8 , seed , index .to_bytes (64 , 'little' )))
9798 index += 1
9899 bytes_ [4 ] &= 1
99100 bytes_ [5 ] &= 0
@@ -260,14 +261,18 @@ def generate(
260261 'seed-based derivation of summation-compatible keys ' +
261262 'is not supported for single-node clusters'
262263 )
263- secret_key ['material' ] = pailliers .secret (2048 )
264+ secret_key ['material' ] = pailliers .secret (256 )
264265 else :
265266 # Multiplicative masks to be used on the shares for each node.
266267 secret_key ['material' ] = [
267268 _random_int (
268269 1 ,
269270 _SECRET_SHARED_SIGNED_INTEGER_MODULUS - 1 ,
270- _seeds (seed , i ) if seed is not None else None
271+ (
272+ _random_bytes (64 , seed , i .to_bytes (64 , 'little' ))
273+ if seed is not None else
274+ None
275+ )
271276 )
272277 for i in range (len (secret_key ['cluster' ]['nodes' ]))
273278 ]
@@ -560,7 +565,7 @@ def encrypt(
560565
561566 # Encrypt (i.e., hash) a value for matching.
562567 if key ['operations' ].get ('match' ):
563- ciphertext = _pack (_Hash (key ['material' ] + buffer ).digest ())
568+ ciphertext = _pack (_HASH (key ['material' ] + buffer ).digest ())
564569
565570 # For multiple-node clusters, prepare the same ciphertext for each.
566571 if len (key ['cluster' ]['nodes' ]) > 1 :
0 commit comments