cryptor.py 5.0 KB

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