Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1import pytest
2import base64
3from CryptMessage import CryptMessage
4
5
6@pytest.mark.usefixtures("resetSettings")
7class TestCrypt:
8 publickey = "A3HatibU4S6eZfIQhVs2u7GLN5G9wXa9WwlkyYIfwYaj"
9 privatekey = "5JBiKFYBm94EUdbxtnuLi6cvNcPzcKymCUHBDf2B6aq19vvG3rL"
10 utf8_text = '\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9'
11 ecies_encrypted_text = "R5J1RFIDOzE5bnWopvccmALKACCk/CRcd/KSE9OgExJKASyMbZ57JVSUenL2TpABMmcT+wAgr2UrOqClxpOWvIUwvwwupXnMbRTzthhIJJrTRW3sCJVaYlGEMn9DAcvbflgEkQX/MVVdLV3tWKySs1Vk8sJC/y+4pGYCrZz7vwDNEEERaqU="
12
13 @pytest.mark.parametrize("text", [b"hello", '\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9'.encode("utf8")])
14 @pytest.mark.parametrize("text_repeat", [1, 10, 128, 1024])
15 def testEncryptEcies(self, text, text_repeat):
16 text_repeated = text * text_repeat
17 aes_key, encrypted = CryptMessage.eciesEncrypt(text_repeated, self.publickey)
18 assert len(aes_key) == 32
19 # assert len(encrypted) == 134 + int(len(text) / 16) * 16 # Not always true
20
21 assert CryptMessage.eciesDecrypt(base64.b64encode(encrypted), self.privatekey) == text_repeated
22
23 def testDecryptEcies(self, user):
24 assert CryptMessage.eciesDecrypt(self.ecies_encrypted_text, self.privatekey) == b"hello"
25
26 def testPublickey(self, ui_websocket):
27 pub = ui_websocket.testAction("UserPublickey", 0)
28 assert len(pub) == 44 # Compressed, b64 encoded publickey
29
30 # Different pubkey for specificed index
31 assert ui_websocket.testAction("UserPublickey", 1) != ui_websocket.testAction("UserPublickey", 0)
32
33 # Same publickey for same index
34 assert ui_websocket.testAction("UserPublickey", 2) == ui_websocket.testAction("UserPublickey", 2)
35
36 # Different publickey for different cert
37 site_data = ui_websocket.user.getSiteData(ui_websocket.site.address)
38 site_data["cert"] = None
39 pub1 = ui_websocket.testAction("UserPublickey", 0)
40
41 site_data = ui_websocket.user.getSiteData(ui_websocket.site.address)
42 site_data["cert"] = "zeroid.bit"
43 pub2 = ui_websocket.testAction("UserPublickey", 0)
44 assert pub1 != pub2
45
46 def testEcies(self, ui_websocket):
47 pub = ui_websocket.testAction("UserPublickey")
48
49 encrypted = ui_websocket.testAction("EciesEncrypt", "hello", pub)
50 assert len(encrypted) == 180
51
52 # Don't allow decrypt using other privatekey index
53 decrypted = ui_websocket.testAction("EciesDecrypt", encrypted, 123)
54 assert decrypted != "hello"
55
56 # Decrypt using correct privatekey
57 decrypted = ui_websocket.testAction("EciesDecrypt", encrypted)
58 assert decrypted == "hello"
59
60 # Decrypt incorrect text
61 decrypted = ui_websocket.testAction("EciesDecrypt", "baad")
62 assert decrypted is None
63
64 # Decrypt batch
65 decrypted = ui_websocket.testAction("EciesDecrypt", [encrypted, "baad", encrypted])
66 assert decrypted == ["hello", None, "hello"]
67
68 def testEciesUtf8(self, ui_websocket):
69 # Utf8 test
70 ui_websocket.actionEciesEncrypt(0, self.utf8_text)
71 encrypted = ui_websocket.ws.getResult()
72
73 ui_websocket.actionEciesDecrypt(0, encrypted)
74 assert ui_websocket.ws.getResult() == self.utf8_text
75
76 def testEciesAes(self, ui_websocket):
77 ui_websocket.actionEciesEncrypt(0, "hello", return_aes_key=True)
78 ecies_encrypted, aes_key = ui_websocket.ws.getResult()
79
80 # Decrypt using Ecies
81 ui_websocket.actionEciesDecrypt(0, ecies_encrypted)
82 assert ui_websocket.ws.getResult() == "hello"
83
84 # Decrypt using AES
85 aes_iv, aes_encrypted = CryptMessage.split(base64.b64decode(ecies_encrypted))
86
87 ui_websocket.actionAesDecrypt(0, base64.b64encode(aes_iv), base64.b64encode(aes_encrypted), aes_key)
88 assert ui_websocket.ws.getResult() == "hello"
89
90 def testEciesAesLongpubkey(self, ui_websocket):
91 privatekey = "5HwVS1bTFnveNk9EeGaRenWS1QFzLFb5kuncNbiY3RiHZrVR6ok"
92
93 ecies_encrypted, aes_key = ["lWiXfEikIjw1ac3J/RaY/gLKACALRUfksc9rXYRFyKDSaxhwcSFBYCgAdIyYlY294g/6VgAf/68PYBVMD3xKH1n7Zbo+ge8b4i/XTKmCZRJvy0eutMKWckYCMVcxgIYNa/ZL1BY1kvvH7omgzg1wBraoLfdbNmVtQgdAZ9XS8PwRy6OB2Q==", "Rvlf7zsMuBFHZIGHcbT1rb4If+YTmsWDv6kGwcvSeMM="]
94
95 # Decrypt using Ecies
96 ui_websocket.actionEciesDecrypt(0, ecies_encrypted, privatekey)
97 assert ui_websocket.ws.getResult() == "hello"
98
99 # Decrypt using AES
100 aes_iv, aes_encrypted = CryptMessage.split(base64.b64decode(ecies_encrypted))
101
102 ui_websocket.actionAesDecrypt(0, base64.b64encode(aes_iv), base64.b64encode(aes_encrypted), aes_key)
103 assert ui_websocket.ws.getResult() == "hello"
104
105 def testAes(self, ui_websocket):
106 ui_websocket.actionAesEncrypt(0, "hello")
107 key, iv, encrypted = ui_websocket.ws.getResult()
108
109 assert len(key) == 44
110 assert len(iv) == 24
111 assert len(encrypted) == 24
112
113 # Single decrypt
114 ui_websocket.actionAesDecrypt(0, iv, encrypted, key)
115 assert ui_websocket.ws.getResult() == "hello"
116
117 # Batch decrypt
118 ui_websocket.actionAesEncrypt(0, "hello")
119 key2, iv2, encrypted2 = ui_websocket.ws.getResult()
120
121 assert [key, iv, encrypted] != [key2, iv2, encrypted2]
122
123 # 2 correct key
124 ui_websocket.actionAesDecrypt(0, [[iv, encrypted], [iv, encrypted], [iv, "baad"], [iv2, encrypted2]], [key])
125 assert ui_websocket.ws.getResult() == ["hello", "hello", None, None]
126
127 # 3 key
128 ui_websocket.actionAesDecrypt(0, [[iv, encrypted], [iv, encrypted], [iv, "baad"], [iv2, encrypted2]], [key, key2])
129 assert ui_websocket.ws.getResult() == ["hello", "hello", None, "hello"]
130
131 def testAesUtf8(self, ui_websocket):
132 ui_websocket.actionAesEncrypt(0, self.utf8_text)
133 key, iv, encrypted = ui_websocket.ws.getResult()
134
135 ui_websocket.actionAesDecrypt(0, iv, encrypted, key)
136 assert ui_websocket.ws.getResult() == self.utf8_text