Advertisement
1lann

Diffie Hellman Key-Exchange in Lua

Oct 5th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. --    Handshake
  2.  
  3.  
  4. local Handshake = {}
  5.  
  6. Handshake.prime = 625210769
  7. Handshake.channel = 54569
  8. Handshake.base = -1
  9. Handshake.secret = -1
  10. Handshake.sharedSecret = -1
  11. Handshake.packetHeader = "["..protocolName.."-Handshake-Packet-Header]"
  12. Handshake.packetMatch = "%["..protocolName.."%-Handshake%-Packet%-Header%](.+)"
  13.  
  14.  
  15. function Handshake.exponentWithModulo(base, exponent, modulo)
  16.         local remainder = base
  17.  
  18.         for i = 1, exponent-1 do
  19.                 remainder = remainder * remainder
  20.                 if remainder >= modulo then
  21.                         remainder = remainder % modulo
  22.                 end
  23.         end
  24.  
  25.         return remainder
  26. end
  27.  
  28.  
  29. function Handshake.clear()
  30.         Handshake.base = -1
  31.         Handshake.secret = -1
  32.         Handshake.sharedSecret = -1
  33. end
  34.  
  35.  
  36. function Handshake.generateInitiatorData()
  37.         Handshake.base = math.random(10,99999)
  38.         Handshake.secret = math.random(10,99999)
  39.         return {
  40.                 type = "initiate",
  41.                 prime = Handshake.prime,
  42.                 base = Handshake.base,
  43.                 moddedSecret = Handshake.exponentWithModulo(Handshake.base, Handshake.secret, Handshake.prime)
  44.         }
  45. end
  46.  
  47.  
  48. function Handshake.generateResponseData(initiatorData)
  49.         local isPrimeANumber = type(initiatorData.prime) == "number"
  50.         local isPrimeMatching = initiatorData.prime == Handshake.prime
  51.         local isBaseANumber = type(initiatorData.base) == "number"
  52.         local isInitiator = initiatorData.type == "initiate"
  53.         local isModdedSecretANumber = type(initiatorData.moddedSecret) == "number"
  54.         local areAllNumbersNumbers = isPrimeANumber and isBaseANumber and isModdedSecretANumber
  55.  
  56.         if areAllNumbersNumbers and isPrimeMatching then
  57.                 if isInitiator then
  58.                         Handshake.base = initiatorData.base
  59.                         Handshake.secret = math.random(10,99999)
  60.                         Handshake.sharedSecret = Handshake.exponentWithModulo(initiatorData.moddedSecret, Handshake.secret, Handshake.prime)
  61.                         return {
  62.                                 type = "response",
  63.                                 prime = Handshake.prime,
  64.                                 base = Handshake.base,
  65.                                 moddedSecret = Handshake.exponentWithModulo(Handshake.base, Handshake.secret, Handshake.prime)
  66.                         }, Handshake.sharedSecret
  67.                 elseif initiatorData.type == "response" and Handshake.base > 0 and Handshake.secret > 0 then
  68.                         Handshake.sharedSecret = Handshake.exponentWithModulo(initiatorData.moddedSecret, Handshake.secret, Handshake.prime)
  69.                         return Handshake.sharedSecret
  70.                 else
  71.                         return false
  72.                 end
  73.         else
  74.                 return false
  75.         end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement