Skip to content

Commit c312253

Browse files
committed
refactor: consolidate pystandalone crypt
Add fallbacl when openssl crypto rng fails
1 parent 53d5944 commit c312253

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

acquire/crypt.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
import hashlib
44
import io
5+
import os
56
from datetime import datetime, timezone
67
from typing import BinaryIO
78

89
from dissect.cstruct import cstruct
910

11+
try:
12+
import _pystandalone
13+
14+
HAS_PYSTANDALONE = True
15+
except ImportError:
16+
HAS_PYSTANDALONE = False
17+
1018
try:
1119
from Crypto.Cipher import AES, PKCS1_OAEP
1220
from Crypto.PublicKey import RSA
@@ -74,16 +82,26 @@ class EncryptedStream(io.RawIOBase):
7482
"""
7583

7684
def __init__(self, fh: BinaryIO, public_key: str):
77-
if not HAS_PYCRYPTODOME:
78-
raise ImportError("PyCryptodome is not available")
85+
if not HAS_PYSTANDALONE and not HAS_PYCRYPTODOME:
86+
raise ImportError("Neither _pystandalone nor PyCryptodome are available")
7987

8088
self.fh = fh
8189

82-
key = get_random_bytes(32)
83-
iv = get_random_bytes(12)
84-
self.cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
85-
86-
rsa = PKCS1_OAEP.new(RSA.import_key(public_key))
90+
if HAS_PYSTANDALONE:
91+
try:
92+
key = _pystandalone.rand_bytes(32)
93+
iv = _pystandalone.rand_bytes(12)
94+
except Exception:
95+
# Fallback if pystandalone does not work
96+
key = os.urandom(32)
97+
iv = os.urandom(12)
98+
self.cipher = _pystandalone.aes_256_gcm(key, iv)
99+
rsa = _pystandalone.rsa(public_key)
100+
else:
101+
key = get_random_bytes(32)
102+
iv = get_random_bytes(12)
103+
self.cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
104+
rsa = PKCS1_OAEP.new(RSA.import_key(public_key))
87105

88106
plain_header = c_acquire.header(
89107
magic=HEADER_MAGIC,

0 commit comments

Comments
 (0)