This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionHandler.py
More file actions
69 lines (58 loc) · 2.95 KB
/
Copy pathEncryptionHandler.py
File metadata and controls
69 lines (58 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import time
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import dh
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import BetterLog
from CryptWrapper import CryptWrapper
from typing import Tuple
class AESGCMKeyHasNotBeenGenerated(Exception):
pass
class EncryptionHandler:
def __init__(self, dh_parameters: bytes | None):
self.self_prepared = False
self.other_prepared = False
self.aes_gcm_shared_key: AESGCM | None = None
self.dh_public_key = None
self.dh_other_key = None
self.dh_private_key = None
if dh_parameters is None:
self.dh_parameters = dh.generate_parameters(generator=2, key_size=2048, backend=default_backend())
self.dh_parameters_bytes = self.dh_parameters.parameter_bytes(encoding=serialization.Encoding.PEM, format=serialization.ParameterFormat.PKCS3)
else:
self.dh_parameters_bytes = dh_parameters
self.dh_parameters = serialization.load_pem_parameters(dh_parameters, backend=default_backend())
def generate_dh_keys(self) -> Tuple[bytes, bool]:
BetterLog.log_text('GENERATING DH KEYS')
self.dh_private_key, self.dh_public_key = CryptWrapper.generate_dh_keys(self.dh_parameters)
BetterLog.log_text('DH KEYS GENERATED')
return self.dh_public_key, self.__attempt_aes_gcm_generation__()
def __attempt_aes_gcm_generation__(self) -> bool:
if self.aes_gcm_shared_key is None:
if self.dh_private_key is not None:
if self.dh_other_key is not None:
BetterLog.log_text('GENERATING AES GCM KEYS')
self.aes_gcm_shared_key = CryptWrapper.generate_aes_gcm_key(self.dh_private_key, self.dh_other_key)
BetterLog.log_text('AES GCM KEYS GENERATED')
self.dh_other_key = None
self.dh_private_key = None
self.dh_public_key = None
self.dh_parameters_bytes = None
self.dh_parameters = None
self.self_prepared = True
return True
return False
def is_prepared(self):
return self.self_prepared and self.other_prepared
def received_other_public_key(self, other_public_key: bytes) -> bool:
BetterLog.log_text('OTHER DH KEY RECEIVED')
self.dh_other_key = other_public_key
return self.__attempt_aes_gcm_generation__()
def encrypt(self, plaintext: bytes) -> bytes:
if self.aes_gcm_shared_key is None:
raise AESGCMKeyHasNotBeenGenerated
return CryptWrapper.encrypt(self.aes_gcm_shared_key, plaintext)
def decrypt(self, ciphertext: bytes) -> bytes:
if self.aes_gcm_shared_key is None:
raise AESGCMKeyHasNotBeenGenerated
return CryptWrapper.decrypt(self.aes_gcm_shared_key, ciphertext)