Skip to content

Commit

Permalink
call update method directly
Browse files Browse the repository at this point in the history
backport of 4fe080e
  • Loading branch information
totaam committed Jul 28, 2024
1 parent 00f7380 commit c91372a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions xpra/net/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def validate_backend() -> None:
ev = enc.encrypt(m)
evs = hexstr(ev)
log(" encrypted(%s)=%s", m, evs)
dv = dec.decrypt(ev)
dv = dec.update(ev)
log(" decrypted(%s)=%s", evs, dv)
if dv!=m:
raise RuntimeError(f"expected {m!r} but got {dv!r}")
Expand Down Expand Up @@ -251,9 +251,7 @@ def get_encryptor(ciphername : str, iv:str, password, key_salt, key_hash : str,
return get_cipher_encryptor(key, iv, mode), get_block_size(mode)

def get_cipher_encryptor(key, iv:str, mode:str):
encryptor = _get_cipher(key, iv, mode).encryptor()
encryptor.encrypt = encryptor.update
return encryptor
return _get_cipher(key, iv, mode).encryptor()

def get_decryptor(ciphername : str, iv:str, password, key_salt, key_hash : str, key_size : int, iterations : int):
log("get_decryptor%s", (ciphername, iv, password, hexstr(key_salt), key_hash, key_size, iterations))
Expand All @@ -279,18 +277,17 @@ def i(s):
supports_memoryviews = tuple(i(s) for s in version.split("."))>=(2, 5)
log("get_decryptor(..) python-cryptography supports_memoryviews(%s)=%s",
version, supports_memoryviews)
if supports_memoryviews:
decryptor.decrypt = decryptor.update
else:
if not supports_memoryviews:
_patch_decryptor(decryptor)
return decryptor

def _patch_decryptor(decryptor):
#with older versions of python-cryptography,
#we have to copy the memoryview to a bytearray:
def decrypt(v):
return decryptor.update(memoryview_to_bytes(v))
decryptor.decrypt = decrypt
_update = decryptor.update
def update(v):
return _update(memoryview_to_bytes(v))
decryptor.update = update

def get_block_size(mode:str) -> int:
if mode=="CBC":
Expand Down
4 changes: 2 additions & 2 deletions xpra/net/protocol/socket_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def _add_chunks_to_queue(self, packet_type:str, chunks,
actual_size += padding_size
if len(padded)!=actual_size:
raise RuntimeError(f"expected padded size to be {actual_size}, but got {len(padded)}")
data = self.cipher_out.encrypt(padded)
data = self.cipher_out.update(padded)
if len(data)!=actual_size:
raise RuntimeError(f"expected encrypted size to be {actual_size}, but got {len(data)}")
cryptolog("sending %s bytes %s encrypted with %s bytes of padding",
Expand Down Expand Up @@ -1004,7 +1004,7 @@ def check_packet_size(size_to_check, packet_header):
return
cryptolog("received %i %s encrypted bytes with %i padding",
payload_size, self.cipher_in_name, padding_size)
data = self.cipher_in.decrypt(data)
data = self.cipher_in.update(data)
if padding_size > 0:
def debug_str(s):
try:
Expand Down

0 comments on commit c91372a

Please sign in to comment.