forked from biemster/FindMy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptic.py
72 lines (50 loc) · 1.97 KB
/
cryptic.py
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
"""
Cryptography functions
"""
# pylint: disable=missing-function-docstring
from base64 import b64encode
import hashlib
from codecs import encode
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.padding import PKCS7
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
def int_to_bytes(n, length, endianess="big"):
return int.to_bytes(n, length, endianess)
def bytes_to_int(b):
return int(encode(b, "hex"), 16)
def sha256(data):
digest = hashlib.new("sha256")
digest.update(data)
return digest.digest()
def decrypt(enc_data, algorithm_dkey, mode):
decryptor = Cipher(algorithm_dkey, mode, default_backend()).decryptor()
return decryptor.update(enc_data) + decryptor.finalize()
def unpad(padded_binary, blocksize):
unpadder = PKCS7(blocksize).unpadder()
return unpadder.update(padded_binary) + unpadder.finalize()
def get_public_key(priv):
return (
ec.derive_private_key(priv, ec.SECP224R1(), default_backend())
.public_key()
.public_numbers()
.x
)
def get_public_from_private(private_key):
return int_to_bytes(get_public_key(bytes_to_int(private_key)), 28)
def get_hashed_public_key(private_key):
return sha256(get_public_from_private(private_key))
def b64_ascii(encodable):
return b64encode(encodable).decode("ascii")
# @see https://github.com/hatomist/openhaystack-python
def get_result(priv, data):
eph_key = ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP224R1(), data[5:62])
shared_key = ec.derive_private_key(
priv, ec.SECP224R1(), default_backend()
).exchange(ec.ECDH(), eph_key)
symmetric_key = sha256(shared_key + b"\x00\x00\x00\x01" + data[5:62])
decryption_key = symmetric_key[:16]
iv = symmetric_key[16:]
enc_data = data[62:72]
tag = data[72:]
return decrypt(enc_data, algorithms.AES(decryption_key), modes.GCM(iv, tag))