Skip to content

Commit 2f1e514

Browse files
author
Jeremy T
committed
Merge branch 'feat/decrypt_padding' into 'master'
feat(decrypt): trim buffer after calling decrypt See merge request TankerHQ/sdk-python!235
2 parents 79b24f2 + f1418ef commit 2f1e514

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

tankersdk/ffi_helpers.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from asyncio import Future
33
from typing import Any, Callable, List, Optional, cast
44

5-
from .error import Error, ErrorCode, make_error
5+
from .error import Error, ErrorCode, InvalidArgument, make_error
66

77
# mypy does not know ffi types, this is for documentation purposes only
88
CData = Any
@@ -28,8 +28,14 @@ def str_to_c_string(self, text: Optional[str]) -> CData:
2828
def c_string_to_bytes(self, c_data: CData) -> bytes:
2929
return cast(bytes, self.ffi.string(c_data))
3030

31-
def c_buffer_to_bytes(self, c_data: CData) -> bytes:
32-
res = self.ffi.buffer(c_data, len(c_data))
31+
def c_buffer_to_bytes(self, c_data: CData, length: Optional[int] = None) -> bytes:
32+
if length is not None:
33+
if length < 0:
34+
raise InvalidArgument("Negative buffer size")
35+
buffer_size = length
36+
else:
37+
buffer_size = len(c_data)
38+
res = self.ffi.buffer(c_data, buffer_size)
3339
# Make a copy of the self.ffi.buffer as a simple `bytes`
3440
# object so that it can be used without worrying
3541
# about the self.ffi buffer being garbage collected.

tankersdk/tanker.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,11 @@ async def decrypt(self, encrypted_data: bytes) -> bytes:
778778
c_future = tankerlib.tanker_decrypt(
779779
self.c_tanker, c_clear_buffer, c_encrypted_buffer, len(c_encrypted_buffer)
780780
)
781-
await ffihelpers.handle_tanker_future(c_future)
782-
return ffihelpers.c_buffer_to_bytes(c_clear_buffer)
781+
c_voidp = await ffihelpers.handle_tanker_future(c_future)
782+
c_clear_size = ffi.cast("uint64_t", c_voidp)
783+
clear_size = int(c_clear_size)
784+
785+
return ffihelpers.c_buffer_to_bytes(c_clear_buffer, clear_size)
783786

784787
async def encrypt_stream(
785788
self, clear_stream: InputStream, options: Optional[EncryptionOptions] = None

test/test_tanker.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,16 @@ async def test_encrypt_decrypt(tmp_path: Path, app: Dict[str, str]) -> None:
414414
await alice.session.stop()
415415

416416

417+
@pytest.mark.asyncio
418+
async def test_encrypt_decrypt_empty(tmp_path: Path, app: Dict[str, str]) -> None:
419+
alice = await create_user_session(tmp_path, app)
420+
message = b""
421+
encrypted_data = await alice.session.encrypt(message)
422+
clear_data = await alice.session.decrypt(encrypted_data)
423+
assert clear_data == message
424+
await alice.session.stop()
425+
426+
417427
@pytest.mark.asyncio
418428
async def test_share_during_encrypt(tmp_path: Path, app: Dict[str, str]) -> None:
419429
alice = await create_user_session(tmp_path, app)

0 commit comments

Comments
 (0)