forked from kaganisildak/bitchat-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
212 lines (177 loc) · 7.75 KB
/
Copy pathencryption.py
File metadata and controls
212 lines (177 loc) · 7.75 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import hashlib
from typing import Dict, Optional, Tuple
from dataclasses import dataclass
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
class EncryptionError(Exception):
"""Encryption-related errors"""
pass
class EncryptionService:
def __init__(self):
# Generate ephemeral keys
self.private_key = X25519PrivateKey.generate()
self.public_key = self.private_key.public_key()
self.signing_key = ed25519.Ed25519PrivateKey.generate()
self.verifying_key = self.signing_key.public_key()
# Generate persistent identity key
self._identity_key = ed25519.Ed25519PrivateKey.generate()
self.identity_public = self._identity_key.public_key()
# Storage
self.peer_public_keys: Dict[str, X25519PublicKey] = {}
self.peer_signing_keys: Dict[str, ed25519.Ed25519PublicKey] = {}
self.peer_identity_keys: Dict[str, ed25519.Ed25519PublicKey] = {}
self.shared_secrets: Dict[str, bytes] = {}
def get_combined_public_key_data(self) -> bytes:
"""Get 96-byte combined public key data"""
data = bytearray()
# X25519 public key (32 bytes)
data.extend(self.public_key.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
))
# Ed25519 signing key (32 bytes)
data.extend(self.verifying_key.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
))
# Identity key (32 bytes)
data.extend(self.identity_public.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
))
return bytes(data)
def store_peer_keys(self, peer_id: str, key_data: bytes):
"""Store peer's public keys from key exchange"""
if len(key_data) < 96:
raise EncryptionError(f"Invalid key data size: {len(key_data)} (expected 96)")
# Parse X25519 public key (first 32 bytes)
public_key_bytes = key_data[0:32]
signing_key_bytes = key_data[32:64]
identity_key_bytes = key_data[64:96]
# Parse X25519 key
try:
public_key = X25519PublicKey.from_public_bytes(public_key_bytes)
except Exception:
raise EncryptionError("Invalid X25519 public key")
# Parse Ed25519 signing key
try:
signing_key = ed25519.Ed25519PublicKey.from_public_bytes(signing_key_bytes)
except Exception:
raise EncryptionError("Invalid signing key")
# Parse identity key with Android compatibility
try:
identity_key = ed25519.Ed25519PublicKey.from_public_bytes(identity_key_bytes)
except Exception:
# Android bug compatibility
print(f"[CRYPTO] Note: Peer {peer_id} appears to be Android (invalid identity key format)")
identity_key = signing_key
# Store keys
self.peer_public_keys[peer_id] = public_key
self.peer_signing_keys[peer_id] = signing_key
self.peer_identity_keys[peer_id] = identity_key
# Generate shared secret
shared_secret = self.private_key.exchange(public_key)
# Derive symmetric key using HKDF
hkdf = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=b"bitchat-v1",
info=b"",
backend=default_backend()
)
symmetric_key = hkdf.derive(shared_secret)
self.shared_secrets[peer_id] = symmetric_key
print(f"[CRYPTO] Successfully established shared secret with {peer_id}")
def get_peer_identity_key(self, peer_id: str) -> Optional[bytes]:
"""Get peer's identity key bytes"""
if peer_id in self.peer_identity_keys:
return self.peer_identity_keys[peer_id].public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
)
return None
def get_peer_fingerprint(self, peer_id: str) -> Optional[str]:
"""Get SHA256 fingerprint of peer's identity key"""
key_bytes = self.get_peer_identity_key(peer_id)
if key_bytes:
digest = hashlib.sha256(key_bytes).digest()
return digest[:16].hex()
return None
def encrypt(self, data: bytes, peer_id: str) -> bytes:
"""Encrypt data for a peer"""
if peer_id not in self.shared_secrets:
raise EncryptionError("No shared secret")
aesgcm = AESGCM(self.shared_secrets[peer_id])
nonce = os.urandom(12)
ciphertext = aesgcm.encrypt(nonce, data, None)
return nonce + ciphertext
def decrypt(self, data: bytes, peer_id: str) -> bytes:
"""Decrypt data from a peer"""
if len(data) < 12:
raise EncryptionError("Data too short")
if peer_id not in self.shared_secrets:
raise EncryptionError("No shared secret")
aesgcm = AESGCM(self.shared_secrets[peer_id])
nonce = data[:12]
ciphertext = data[12:]
try:
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
return plaintext
except Exception:
raise EncryptionError("Decryption failed")
def sign(self, data: bytes) -> bytes:
"""Sign data"""
signature = self.signing_key.sign(data)
return signature
def verify(self, signature: bytes, data: bytes, peer_id: str) -> bool:
"""Verify signature"""
if peer_id not in self.peer_signing_keys:
raise EncryptionError("No signing key")
try:
self.peer_signing_keys[peer_id].verify(signature, data)
return True
except Exception:
return False
@staticmethod
def derive_channel_key(password: str, channel_name: str) -> bytes:
"""Derive channel key from password"""
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=channel_name.encode(),
iterations=100_000,
backend=default_backend()
)
return kdf.derive(password.encode())
def encrypt_with_key(self, data: bytes, key: bytes) -> bytes:
"""Encrypt with a specific key"""
aesgcm = AESGCM(key)
nonce = os.urandom(12)
ciphertext = aesgcm.encrypt(nonce, data, None)
return nonce + ciphertext
def decrypt_with_key(self, data: bytes, key: bytes) -> bytes:
"""Decrypt with a specific key"""
if len(data) < 12:
raise EncryptionError("Data too short")
aesgcm = AESGCM(key)
nonce = data[:12]
ciphertext = data[12:]
try:
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
return plaintext
except Exception:
raise EncryptionError("Decryption failed")
def has_peer_key(self, peer_id: str) -> bool:
"""Check if we have a peer's key"""
return peer_id in self.shared_secrets
def encrypt_for_peer(self, peer_id: str, data: bytes) -> bytes:
"""Encrypt specifically for a peer"""
return self.encrypt(data, peer_id)
# Export classes and errors
__all__ = ['EncryptionError', 'EncryptionService']