Skip to content
Merged
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
32 changes: 25 additions & 7 deletions acquire/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import hashlib
import io
import os
from datetime import datetime, timezone
from typing import BinaryIO

from dissect.cstruct import cstruct

try:
import _pystandalone

HAS_PYSTANDALONE = True

Check warning on line 14 in acquire/crypt.py

View check run for this annotation

Codecov / codecov/patch

acquire/crypt.py#L14

Added line #L14 was not covered by tests
except ImportError:
HAS_PYSTANDALONE = False

try:
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
Expand Down Expand Up @@ -74,16 +82,26 @@
"""

def __init__(self, fh: BinaryIO, public_key: str):
if not HAS_PYCRYPTODOME:
raise ImportError("PyCryptodome is not available")
if not HAS_PYSTANDALONE and not HAS_PYCRYPTODOME:
raise ImportError("Neither _pystandalone nor PyCryptodome are available")

Check warning on line 86 in acquire/crypt.py

View check run for this annotation

Codecov / codecov/patch

acquire/crypt.py#L86

Added line #L86 was not covered by tests

self.fh = fh

key = get_random_bytes(32)
iv = get_random_bytes(12)
self.cipher = AES.new(key, AES.MODE_GCM, nonce=iv)

rsa = PKCS1_OAEP.new(RSA.import_key(public_key))
if HAS_PYSTANDALONE:
try:
key = _pystandalone.rand_bytes(32)
iv = _pystandalone.rand_bytes(12)
except Exception:

Check warning on line 94 in acquire/crypt.py

View check run for this annotation

Codecov / codecov/patch

acquire/crypt.py#L91-L94

Added lines #L91 - L94 were not covered by tests
# Fallback if pystandalone does not work
key = os.urandom(32)
iv = os.urandom(12)
self.cipher = _pystandalone.aes_256_gcm(key, iv)
rsa = _pystandalone.rsa(public_key)

Check warning on line 99 in acquire/crypt.py

View check run for this annotation

Codecov / codecov/patch

acquire/crypt.py#L96-L99

Added lines #L96 - L99 were not covered by tests
else:
key = get_random_bytes(32)
iv = get_random_bytes(12)
self.cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
rsa = PKCS1_OAEP.new(RSA.import_key(public_key))

plain_header = c_acquire.header(
magic=HEADER_MAGIC,
Expand Down
Loading