TestCrypt.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import pytest
  2. from CryptMessage import CryptMessage
  3. @pytest.mark.usefixtures("resetSettings")
  4. class TestCrypt:
  5. def testPublickey(self, ui_websocket):
  6. pub = ui_websocket.testAction("UserPublickey", 0)
  7. assert len(pub) == 44 # Compressed, b64 encoded publickey
  8. # Different pubkey for specificed index
  9. assert ui_websocket.testAction("UserPublickey", 1) != ui_websocket.testAction("UserPublickey", 0)
  10. # Same publickey for same index
  11. assert ui_websocket.testAction("UserPublickey", 2) == ui_websocket.testAction("UserPublickey", 2)
  12. # Different publickey for different cert
  13. pub1 = ui_websocket.testAction("UserPublickey", 0)
  14. site_data = ui_websocket.user.getSiteData(ui_websocket.site.address)
  15. site_data["cert"] = "zeroid.bit"
  16. pub2 = ui_websocket.testAction("UserPublickey", 0)
  17. assert pub1 != pub2
  18. def testEcies(self, ui_websocket):
  19. ui_websocket.actionUserPublickey(0, 0)
  20. pub = ui_websocket.ws.result
  21. ui_websocket.actionEciesEncrypt(0, "hello", pub)
  22. encrypted = ui_websocket.ws.result
  23. assert len(encrypted) == 180
  24. # Don't allow decrypt using other privatekey index
  25. ui_websocket.actionEciesDecrypt(0, encrypted, 123)
  26. decrypted = ui_websocket.ws.result
  27. assert decrypted != "hello"
  28. # Decrypt using correct privatekey
  29. ui_websocket.actionEciesDecrypt(0, encrypted)
  30. decrypted = ui_websocket.ws.result
  31. assert decrypted == "hello"
  32. # Decrypt batch
  33. ui_websocket.actionEciesDecrypt(0, [encrypted, "baad", encrypted])
  34. decrypted = ui_websocket.ws.result
  35. assert decrypted == ["hello", None, "hello"]
  36. def testEciesUtf8(self, ui_websocket):
  37. # Utf8 test
  38. utf8_text = u'\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9p'
  39. ui_websocket.actionEciesEncrypt(0, utf8_text)
  40. encrypted = ui_websocket.ws.result
  41. ui_websocket.actionEciesDecrypt(0, encrypted)
  42. assert ui_websocket.ws.result == utf8_text
  43. def testEciesAes(self, ui_websocket):
  44. ui_websocket.actionEciesEncrypt(0, "hello", return_aes_key=True)
  45. ecies_encrypted, aes_key = ui_websocket.ws.result
  46. # Decrypt using Ecies
  47. ui_websocket.actionEciesDecrypt(0, ecies_encrypted)
  48. assert ui_websocket.ws.result == "hello"
  49. # Decrypt using AES
  50. aes_iv, aes_encrypted = CryptMessage.split(ecies_encrypted.decode("base64"))
  51. ui_websocket.actionAesDecrypt(0, aes_iv.encode("base64"), aes_encrypted.encode("base64"), aes_key)
  52. assert ui_websocket.ws.result == "hello"
  53. def testAes(self, ui_websocket):
  54. ui_websocket.actionAesEncrypt(0, "hello")
  55. key, iv, encrypted = ui_websocket.ws.result
  56. assert len(key) == 44
  57. assert len(iv) == 24
  58. assert len(encrypted) == 24
  59. # Single decrypt
  60. ui_websocket.actionAesDecrypt(0, iv, encrypted, key)
  61. assert ui_websocket.ws.result == "hello"
  62. # Batch decrypt
  63. ui_websocket.actionAesEncrypt(0, "hello")
  64. key2, iv2, encrypted2 = ui_websocket.ws.result
  65. assert [key, iv, encrypted] != [key2, iv2, encrypted2]
  66. # 2 correct key
  67. ui_websocket.actionAesDecrypt(0, [[iv, encrypted], [iv, encrypted], [iv, "baad"], [iv2, encrypted2]], [key])
  68. assert ui_websocket.ws.result == ["hello", "hello", None, None]
  69. # 3 key
  70. ui_websocket.actionAesDecrypt(0, [[iv, encrypted], [iv, encrypted], [iv, "baad"], [iv2, encrypted2]], [key, key2])
  71. assert ui_websocket.ws.result == ["hello", "hello", None, "hello"]
  72. def testAesUtf8(self, ui_websocket):
  73. utf8_text = u'\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9'
  74. ui_websocket.actionAesEncrypt(0, utf8_text)
  75. key, iv, encrypted = ui_websocket.ws.result
  76. ui_websocket.actionAesDecrypt(0, iv, encrypted, key)
  77. assert ui_websocket.ws.result == utf8_text