Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions core/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import struct
from pathlib import Path

from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from Crypto.Cipher import AES

from core.errors import PortabaseError

Expand Down Expand Up @@ -81,7 +80,6 @@ def _read_header(handle) -> tuple[bytes, int]:


def decrypt_enc_file(enc_path: Path, out_path: Path, key: bytes) -> None:
aesgcm = AESGCM(key)
tmp_path = out_path.with_name(out_path.name + ".part")

try:
Expand Down Expand Up @@ -119,9 +117,12 @@ def decrypt_enc_file(enc_path: Path, out_path: Path, key: bytes) -> None:
)

nonce = base_nonce + struct.pack(">I", chunk_index)
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
try:
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
except InvalidTag as exc:
plaintext = cipher.decrypt_and_verify(
ciphertext[:-_TAG_LEN], ciphertext[-_TAG_LEN:]
)
except ValueError as exc:
raise DecryptionError(
f"Authentication failed on chunk {chunk_index} "
"(wrong key or corrupt data)."
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ dependencies = [
"questionary>=2.1.0",
"requests>=2.32.5",
"pyyaml>=6.0.3",
"cryptography>=44.0.0; sys_platform != 'darwin' or platform_machine != 'x86_64'",
"cryptography>=44.0.0,<49; sys_platform == 'darwin' and platform_machine == 'x86_64'",
"pycryptodome>=3.23.0",
"jinja2>=3.1",
]

Expand Down
8 changes: 4 additions & 4 deletions tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from typing import Any

from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from Crypto.Cipher import AES

from core.errors import NetworkError
from core.fields import Field
Expand Down Expand Up @@ -113,11 +113,11 @@ def encrypt(plain: bytes, key: bytes, chunk_size: int = 4) -> bytes:
"chunk_size": chunk_size,
}
out = json.dumps(header).encode() + b"\n"
aes = AESGCM(key)
for index, start in enumerate(range(0, len(plain), chunk_size)):
nonce = base_nonce + struct.pack(">I", index)
ciphertext = aes.encrypt(nonce, plain[start : start + chunk_size], None)
out += struct.pack(">I", len(ciphertext)) + ciphertext
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
ciphertext, tag = cipher.encrypt_and_digest(plain[start : start + chunk_size])
out += struct.pack(">I", len(ciphertext) + len(tag)) + ciphertext + tag
return out


Expand Down
Loading
Loading