TestCrypt.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. site_data = ui_websocket.user.getSiteData(ui_websocket.site.address)
  14. site_data["cert"] = None
  15. pub1 = ui_websocket.testAction("UserPublickey", 0)
  16. site_data = ui_websocket.user.getSiteData(ui_websocket.site.address)
  17. site_data["cert"] = "zeroid.bit"
  18. pub2 = ui_websocket.testAction("UserPublickey", 0)
  19. assert pub1 != pub2
  20. def testEcies(self, ui_websocket):
  21. ui_websocket.actionUserPublickey(0, 0)
  22. pub = ui_websocket.ws.result
  23. ui_websocket.actionEciesEncrypt(0, "hello", pub)
  24. encrypted = ui_websocket.ws.result
  25. assert len(encrypted) == 180
  26. # Don't allow decrypt using other privatekey index
  27. ui_websocket.actionEciesDecrypt(0, encrypted, 123)
  28. decrypted = ui_websocket.ws.result
  29. assert decrypted != "hello"
  30. # Decrypt using correct privatekey
  31. ui_websocket.actionEciesDecrypt(0, encrypted)
  32. decrypted = ui_websocket.ws.result
  33. assert decrypted == "hello"
  34. # Decrypt batch
  35. ui_websocket.actionEciesDecrypt(0, [encrypted, "baad", encrypted])
  36. decrypted = ui_websocket.ws.result
  37. assert decrypted == ["hello", None, "hello"]
  38. def testEciesUtf8(self, ui_websocket):
  39. # Utf8 test
  40. utf8_text = u'\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9p'
  41. ui_websocket.actionEciesEncrypt(0, utf8_text)
  42. encrypted = ui_websocket.ws.result
  43. ui_websocket.actionEciesDecrypt(0, encrypted)
  44. assert ui_websocket.ws.result == utf8_text
  45. def testEciesAes(self, ui_websocket):
  46. ui_websocket.actionEciesEncrypt(0, "hello", return_aes_key=True)
  47. ecies_encrypted, aes_key = ui_websocket.ws.result
  48. # Decrypt using Ecies
  49. ui_websocket.actionEciesDecrypt(0, ecies_encrypted)
  50. assert ui_websocket.ws.result == "hello"
  51. # Decrypt using AES
  52. aes_iv, aes_encrypted = CryptMessage.split(ecies_encrypted.decode("base64"))
  53. ui_websocket.actionAesDecrypt(0, aes_iv.encode("base64"), aes_encrypted.encode("base64"), aes_key)
  54. assert ui_websocket.ws.result == "hello"
  55. def testAes(self, ui_websocket):
  56. ui_websocket.actionAesEncrypt(0, "hello")
  57. key, iv, encrypted = ui_websocket.ws.result
  58. assert len(key) == 44
  59. assert len(iv) == 24
  60. assert len(encrypted) == 24
  61. # Single decrypt
  62. ui_websocket.actionAesDecrypt(0, iv, encrypted, key)
  63. assert ui_websocket.ws.result == "hello"
  64. # Batch decrypt
  65. ui_websocket.actionAesEncrypt(0, "hello")
  66. key2, iv2, encrypted2 = ui_websocket.ws.result
  67. assert [key, iv, encrypted] != [key2, iv2, encrypted2]
  68. # 2 correct key
  69. ui_websocket.actionAesDecrypt(0, [[iv, encrypted], [iv, encrypted], [iv, "baad"], [iv2, encrypted2]], [key])
  70. assert ui_websocket.ws.result == ["hello", "hello", None, None]
  71. # 3 key
  72. ui_websocket.actionAesDecrypt(0, [[iv, encrypted], [iv, encrypted], [iv, "baad"], [iv2, encrypted2]], [key, key2])
  73. assert ui_websocket.ws.result == ["hello", "hello", None, "hello"]
  74. def testAesUtf8(self, ui_websocket):
  75. utf8_text = u'\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9'
  76. ui_websocket.actionAesEncrypt(0, utf8_text)
  77. key, iv, encrypted = ui_websocket.ws.result
  78. ui_websocket.actionAesDecrypt(0, iv, encrypted, key)
  79. assert ui_websocket.ws.result == utf8_text