cryptor.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. import socket
  3. import threading
  4. import config as cfg
  5. from nacl.public import PrivateKey
  6. from connection.socket import SocketClient
  7. from tls import TLSClientSession
  8. import time
  9. from Crypto.Util.number import long_to_bytes, bytes_to_long
  10. from tls.ghash import GhashCry
  11. from Utils import *
  12. def new_x25519():
  13. private_key = PrivateKey.generate()
  14. key_exchange = bytes(private_key.public_key)
  15. return bytes(private_key).hex(), key_exchange.hex()
  16. class Cryptor:
  17. def __init__(self, parser):
  18. self.privatekey, self.key_exchange = new_x25519()
  19. self.server = None
  20. self.parser = parser
  21. self.connector = None
  22. self.key = None
  23. gBackPrint("CRYPTOR", "")
  24. gPrint("privatekey", self.privatekey)
  25. gPrint("key_exchange", self.key_exchange)
  26. def oracleInitial(self, data):
  27. try:
  28. self.server = data["server"]
  29. self.connector = SocketClient(
  30. data["connector"][0], data["connector"][1], self.parser, "Cryptor"
  31. )
  32. self.key = data["key"]
  33. dataLen = data["dataLen"]
  34. if 0 == dataLen % 16:
  35. datalen = dataLen // 16
  36. else:
  37. datalen = (dataLen + (16 - dataLen % 16)) // 16
  38. self.dataLen = datalen
  39. time.sleep(1)
  40. self.keyExchange()
  41. except Exception as e:
  42. rBackPrint("An exception occurred oracleInitial", e)
  43. def keyExchange(self):
  44. try:
  45. d = {}
  46. self.parser.add(d, "type", "keyExchange")
  47. self.parser.add(d, "keyExchange", self.key_exchange)
  48. self.connector.send(self.parser.toSEND(d))
  49. except Exception as e:
  50. rBackPrint("An exception occurred keyExchange", e)
  51. def fromContoCryHelloCLient(self, data):
  52. try:
  53. self.tls = TLSClientSession(
  54. bytes.fromhex(self.privatekey),
  55. bytes.fromhex(self.key_exchange),
  56. bytearray(data["data"], "utf-8"),
  57. self.fromCrytoCon,
  58. self.fromCrytoConDone,
  59. )
  60. except Exception as e:
  61. rBackPrint("An exception occurred fromContoCryHelloCLient", e)
  62. def fromCrytoCon(self, data):
  63. try:
  64. self.parser.parserSend(data, self.connector, "fromCrytoCon")
  65. except Exception as e:
  66. rBackPrint("An exception occurred fromCrytoCon", e)
  67. def fromCrytoConDone(self, change, finish):
  68. try:
  69. bPrint("Start to send Done to Connection ", self.tls.cipher.key)
  70. n = self.tls.cipher.get_nonce()
  71. k = self.tls.cipher.key
  72. bPrint("Key", k)
  73. bPrint("Nonce", n)
  74. g = GhashCry(bytes_to_long(k), bytes_to_long(n))
  75. f, l = g.get()
  76. d = {}
  77. self.parser.add(d, "type", "fromCrytoConDone")
  78. self.parser.add(d, "change", change)
  79. self.parser.add(d, "finish", finish)
  80. self.parser.add(d, "auth_key", f)
  81. self.parser.add(d, "last_key", l)
  82. self.counter = g.encIV(self.dataLen)
  83. t = b""
  84. for x in self.counter:
  85. t = t + x
  86. self.counter2 = t
  87. self.connector.send(self.parser.toSEND(d))
  88. except Exception as e:
  89. rBackPrint("An exception occurred fromCrytoConDone", e)
  90. def MPC(self, json):
  91. try:
  92. n = int(json["data"])
  93. bPrint("MPC Input", self.counter2[4 * n : 4 * (1 + n)].hex())
  94. bPrint(
  95. "MPC Input int",
  96. str(bytes_to_long((self.counter2[4 * n : 4 * (1 + n)]))),
  97. )
  98. s = (
  99. "echo "
  100. + str(bytes_to_long((self.counter2[4 * n : 4 * (1 + n)])))
  101. + " > ./Player-Data/Input-P2-0"
  102. )
  103. os.system(s)
  104. os.system(
  105. "./MP-SPDZ/mascot-party.x 2 xor -pn 12023 -h 192.168.178.105 -N 3"
  106. )
  107. except Exception as e:
  108. rBackPrint("An exception occurred MPC", e)
  109. def fromContoCry(self, data):
  110. try:
  111. self.tls.tls_response(memoryview(bytes.fromhex(data["data"])))
  112. except Exception as e:
  113. rBackPrint("An exception occurred fromContoCry", e)