TestCrypt.py 5.9 KB

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