Advertisement
Anavrins

ChaCha20-Lua

Dec 24th, 2015
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. -- Chacha20 cipher in ComputerCraft
  2. -- By Anavrins
  3. -- MIT License
  4. -- Pastebin: https://pastebin.com/GPzf9JSa
  5. -- Last updated: March 27 2020
  6.  
  7. local mod32 = 2^32
  8. local bor = bit32.bor
  9. local bxor = bit32.bxor
  10. local band = bit32.band
  11. local blshift = bit32.lshift
  12. local brshift = bit32.arshift
  13.  
  14. local tau = {("expand 16-byte k"):byte(1,-1)}
  15. local sigma = {("expand 32-byte k"):byte(1,-1)}
  16. local null32 = {("A"):rep(32):byte(1,-1)}
  17. local null12 = {("A"):rep(12):byte(1,-1)}
  18.  
  19. local function rotl(n, b)
  20.     local s = n/(2^(32-b))
  21.     local f = s%1
  22.     return (s-f) + f*mod32
  23. end
  24.  
  25. local function quarterRound(s, a, b, c, d)
  26.     s[a] = (s[a]+s[b])%mod32; s[d] = rotl(bxor(s[d], s[a]), 16)
  27.     s[c] = (s[c]+s[d])%mod32; s[b] = rotl(bxor(s[b], s[c]), 12)
  28.     s[a] = (s[a]+s[b])%mod32; s[d] = rotl(bxor(s[d], s[a]), 8)
  29.     s[c] = (s[c]+s[d])%mod32; s[b] = rotl(bxor(s[b], s[c]), 7)
  30.     return s
  31. end
  32.  
  33. local function hashBlock(state, rnd)
  34.     local s = {unpack(state)}
  35.     for i = 1, rnd do
  36.         local r = i%2==1
  37.         s = r and quarterRound(s, 1, 5,  9, 13) or quarterRound(s, 1, 6, 11, 16)
  38.         s = r and quarterRound(s, 2, 6, 10, 14) or quarterRound(s, 2, 7, 12, 13)
  39.         s = r and quarterRound(s, 3, 7, 11, 15) or quarterRound(s, 3, 8,  9, 14)
  40.         s = r and quarterRound(s, 4, 8, 12, 16) or quarterRound(s, 4, 5, 10, 15)
  41.     end
  42.     for i = 1, 16 do s[i] = (s[i]+state[i])%mod32 end
  43.     return s
  44. end
  45.  
  46. local function LE_toInt(bs, i)
  47.     return (bs[i+1] or 0)+
  48.     blshift((bs[i+2] or 0), 8)+
  49.     blshift((bs[i+3] or 0), 16)+
  50.     blshift((bs[i+4] or 0), 24)
  51. end
  52.  
  53. local function initState(key, nonce, counter)
  54.     local isKey256 = #key == 32
  55.     local const = isKey256 and sigma or tau
  56.     local state = {}
  57.  
  58.     state[ 1] = LE_toInt(const, 0)
  59.     state[ 2] = LE_toInt(const, 4)
  60.     state[ 3] = LE_toInt(const, 8)
  61.     state[ 4] = LE_toInt(const, 12)
  62.  
  63.     state[ 5] = LE_toInt(key, 0)
  64.     state[ 6] = LE_toInt(key, 4)
  65.     state[ 7] = LE_toInt(key, 8)
  66.     state[ 8] = LE_toInt(key, 12)
  67.     state[ 9] = LE_toInt(key, isKey256 and 16 or 0)
  68.     state[10] = LE_toInt(key, isKey256 and 20 or 4)
  69.     state[11] = LE_toInt(key, isKey256 and 24 or 8)
  70.     state[12] = LE_toInt(key, isKey256 and 28 or 12)
  71.  
  72.     state[13] = counter
  73.     state[14] = LE_toInt(nonce, 0)
  74.     state[15] = LE_toInt(nonce, 4)
  75.     state[16] = LE_toInt(nonce, 8)
  76.  
  77.     return state
  78. end
  79.  
  80. local function serialize(state)
  81.     local r = {}
  82.     for i = 1, 16 do
  83.         r[#r+1] = band(state[i], 0xFF)
  84.         r[#r+1] = band(brshift(state[i], 8), 0xFF)
  85.         r[#r+1] = band(brshift(state[i], 16), 0xFF)
  86.         r[#r+1] = band(brshift(state[i], 24), 0xFF)
  87.     end
  88.     return r
  89. end
  90.  
  91. local mt = {
  92.     __tostring = function(a) return string.char(unpack(a)) end,
  93.     __index = {
  94.         toHex = function(self) return ("%02x"):rep(#self):format(unpack(self)) end,
  95.         isEqual = function(self, t)
  96.             if type(t) ~= "table" then return false end
  97.             if #self ~= #t then return false end
  98.             local ret = 0
  99.             for i = 1, #self do
  100.                 ret = bor(ret, bxor(self[i], t[i]))
  101.             end
  102.             return ret == 0
  103.         end
  104.     }
  105. }
  106.  
  107. local function crypt(data, key, nonce, cntr, round)
  108.     assert(type(key) == "table", "ChaCha20: Invalid key format ("..type(key).."), must be table")
  109.     assert(type(nonce) == "table", "ChaCha20: Invalid nonce format ("..type(nonce).."), must be table")
  110.     assert(#key == 16 or #key == 32, "ChaCha20: Invalid key length ("..#key.."), must be 16 or 32")
  111.     assert(#nonce == 12, "ChaCha20: Invalid nonce length ("..#nonce.."), must be 12")
  112.  
  113.     local data = type(data) == "table" and {unpack(data)} or {tostring(data):byte(1,-1)}
  114.     cntr = tonumber(cntr) or 1
  115.     round = tonumber(round) or 20
  116.  
  117.     local out = {}
  118.     local state = initState(key, nonce, cntr)
  119.     local blockAmt = math.floor(#data/64)
  120.     for i = 0, blockAmt do
  121.         local ks = serialize(hashBlock(state, round))
  122.         state[13] = (state[13]+1) % mod32
  123.  
  124.         local block = {}
  125.         for j = 1, 64 do
  126.             block[j] = data[((i)*64)+j]
  127.         end
  128.         for j = 1, #block do
  129.             out[#out+1] = bxor(block[j], ks[j])
  130.         end
  131.  
  132.         if i % 1000 == 0 then
  133.             os.queueEvent("")
  134.             os.pullEvent("")
  135.         end
  136.     end
  137.     return setmetatable(out, mt)
  138. end
  139.  
  140. local function genNonce(len)
  141.     local nonce = {}
  142.     for i = 1, len do
  143.         nonce[i] = math.random(0, 0xFF)
  144.     end
  145.     return setmetatable(nonce, mt)
  146. end
  147.  
  148. local obj = {}
  149. local mtrng = {['__index'] = obj}
  150. local function newRNG(seed)
  151.   local objVars = {}
  152.   objVars.seed = seed
  153.   objVars.cnt = 0
  154.   objVars.block = {}
  155.   return setmetatable(objVars, mtrng)
  156. end
  157.  
  158. -- Specify how many bytes to return
  159. -- 1 Byte is 8 bits, returns int between 0 and 255
  160. -- 2 Bytes is 16 bits, returns int up to 65535
  161. -- 4 Bytes is 32 bits, returns int up to 4294967295
  162. -- Max of 6 Bytes is 48 bits, returns int up to 281474976710655
  163. function obj:nextInt(byte)
  164.   if not byte or byte < 1 or byte > 6 then error("Can only return 1-6 bytes", 2) end
  165.   local output = 0
  166.   for i = 0, byte-1 do
  167.     if #self.block == 0 then
  168.       self.cnt = self.cnt + 1
  169.       self.block = crypt(null32, self.seed, null12, self.cnt)
  170.     end
  171.     local newByte = table.remove(self.block)
  172.     output = output + (newByte * (2^(8*i)))
  173.   end
  174.   return output
  175. end
  176.  
  177. return {
  178.     crypt = crypt,
  179.     genNonce = genNonce,
  180.     newRNG = newRNG
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement