Guest User

poly1305 lua 5.1

a guest
Mar 1st, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.56 KB | Cybersecurity | 0 0
  1. bit32_lshift = nil
  2. bit32_rshift = nil
  3. bit32_or = nil
  4. bit32_and = nil
  5. bit32_xor = nil
  6. bit32_negate = nil
  7.  
  8. if not bit then -- lua >= 5.4
  9.     load([[
  10.         bit32_lshift = function(a,b) return a << b  end;
  11.         bit32_rshift = function(a,b) return a >> b  end;
  12.         bit32_or = function(a,b) return a | b  end;
  13.         bit32_and = function(a,b) return a & b  end;
  14.         bit32_xor = function(a,b) return a ^ b  end;
  15.         bit32_negate = function(a) return ~a  end;
  16.     ]])()
  17. else -- bit library is global on my restricted env
  18.     bit32_lshift = bit.lshift
  19.     bit32_rshift = bit.rshift
  20.     bit32_or = bit.bor
  21.     bit32_and = bit.band
  22.     bit32_xor = bit.bxor
  23.     bit32_negate = bit.bnot
  24. end
  25.  
  26. function my_unpack(tbl, i, j)
  27.     i = i or 1
  28.     j = j or #tbl
  29.  
  30.     if i <= j then
  31.         return tbl[i], my_unpack(tbl, i + 1, j)
  32.     end
  33. end
  34. local table_unpack = my_unpack or table.unpack or unpack
  35.  
  36.  
  37. --equivalent of string.unpack with <I4 formats
  38. local function lendian_string_unpack_to_ints(num_ints, str, start_index)
  39.     start_index = start_index or 1
  40.     num_ints = num_ints or 100000
  41.  
  42.  
  43.     local count = 0
  44.     if type(num_ints == string) then
  45.         local _, endIndex = string.find(num_ints, "I4")
  46.         while endIndex do
  47.             count = count + 1
  48.             _, endIndex = string.find(num_ints, "I4", endIndex + 1)
  49.         end
  50.     end
  51.  
  52.     local results = {}
  53.     local end_index = start_index + (count * 4) - 1
  54.     for i = start_index, end_index, 4 do
  55.         if i <= #str then
  56.             local bytes = {string.byte(str, i, i + 3)}
  57.             local value = 0
  58.             for j = #bytes, 1, -1 do
  59.                 value = value * 256 + bytes[j]
  60.             end
  61.             table.insert(results, value)
  62.         end
  63.     end
  64.     return table_unpack(results)
  65. end
  66.  
  67. --equivalent of string.pack with <I4 formats
  68. local function lendian_string_pack_to_ints(num_ints, ...)
  69.     local result = {}
  70.     local args = { ... }
  71.     local argIndex = 1
  72.  
  73.     local count = 0
  74.     if type(num_ints == string) then
  75.         local _, endIndex = string.find(num_ints, "I4")
  76.         while endIndex do
  77.             count = count + 1
  78.             _, endIndex = string.find(num_ints, "I4", endIndex + 1)
  79.         end
  80.     end
  81.  
  82.     for i = 1, count do
  83.         local num = args[argIndex]
  84.         for j = 1, 4 do
  85.             table.insert(result, string.char(bit32_and(num, 0xFF)))
  86.             num = bit32_rshift(num, 8)
  87.         end
  88.         argIndex = argIndex + 1
  89.     end
  90.  
  91.     return table.concat(result)
  92. end
  93.  
  94. local sunp = lendian_string_unpack_to_ints;
  95.  
  96.  
  97. local function poly_init(k)
  98.     local st = {r={bit32_and((sunp("<I4", k, 1)), 67108863),bit32_and(bit32_rshift(sunp("<I4", k, 4), 2), 67108611),bit32_and(bit32_rshift(sunp("<I4", k, 7), 4), 67092735),bit32_and(bit32_rshift(sunp("<I4", k, 10), 6), 66076671),bit32_and(bit32_rshift(sunp("<I4", k, 13), 8), 1048575)},h={0,0,0,0,0},pad={sunp("<I4", k, 17),sunp("<I4", k, 21),sunp("<I4", k, 25),sunp("<I4", k, 29)},buffer="",leftover=0,final=false};
  99.     return st;
  100. end
  101. local function poly_blocks(st, m)
  102.     local bytes = #m;
  103.     local midx = 1;
  104.     local hibit = (st.final and 0) or 16777216;
  105.     local r0 = st.r[1];
  106.     local r1 = st.r[2];
  107.     local r2 = st.r[3];
  108.     local r3 = st.r[4];
  109.     local r4 = st.r[5];
  110.     local s1 = r1 * 5;
  111.     local s2 = r2 * 5;
  112.     local s3 = r3 * 5;
  113.     local s4 = r4 * 5;
  114.     local h0 = st.h[1];
  115.     local h1 = st.h[2];
  116.     local h2 = st.h[3];
  117.     local h3 = st.h[4];
  118.     local h4 = st.h[5];
  119.     local d0, d1, d2, d3, d4, c;
  120.     while bytes >= 16 do
  121.         h0 = h0 + bit32_and((sunp("<I4", m, midx)), 67108863);
  122.         h1 = h1 + bit32_and(bit32_rshift(sunp("<I4", m, midx + 3), 2), 67108863);
  123.         h2 = h2 + bit32_and(bit32_rshift(sunp("<I4", m, midx + 6), 4), 67108863);
  124.         h3 = h3 + bit32_and(bit32_rshift(sunp("<I4", m, midx + 9), 6), 67108863);
  125.         h4 = h4 + bit32_or(bit32_rshift(sunp("<I4", m, midx + 12), 8), hibit);
  126.         d0 = (h0 * r0) + (h1 * s4) + (h2 * s3) + (h3 * s2) + (h4 * s1);
  127.         d1 = (h0 * r1) + (h1 * r0) + (h2 * s4) + (h3 * s3) + (h4 * s2);
  128.         d2 = (h0 * r2) + (h1 * r1) + (h2 * r0) + (h3 * s4) + (h4 * s3);
  129.         d3 = (h0 * r3) + (h1 * r2) + (h2 * r1) + (h3 * r0) + (h4 * s4);
  130.         d4 = (h0 * r4) + (h1 * r3) + (h2 * r2) + (h3 * r1) + (h4 * r0);
  131.         c = bit32_and(bit32_rshift(d0, 26), 4294967295);
  132.         h0 = bit32_and(d0, 67108863);
  133.         d1 = d1 + c;
  134.         c = bit32_and(bit32_rshift(d1, 26), 4294967295);
  135.         h1 = bit32_and(d1, 67108863);
  136.         d2 = d2 + c;
  137.         c = bit32_and(bit32_rshift(d2, 26), 4294967295);
  138.         h2 = bit32_and(d2, 67108863);
  139.         d3 = d3 + c;
  140.         c = bit32_and(bit32_rshift(d3, 26), 4294967295);
  141.         h3 = bit32_and(d3, 67108863);
  142.         d4 = d4 + c;
  143.         c = bit32_and(bit32_rshift(d4, 26), 4294967295);
  144.         h4 = bit32_and(d4, 67108863);
  145.         h0 = h0 + (c * 5);
  146.         c = bit32_rshift(h0, 26);
  147.         h0 = bit32_and(h0, 67108863);
  148.         h1 = h1 + c;
  149.         midx = midx + 16;
  150.         bytes = bytes - 16;
  151.     end
  152.     st.h[1] = h0;
  153.     st.h[2] = h1;
  154.     st.h[3] = h2;
  155.     st.h[4] = h3;
  156.     st.h[5] = h4;
  157.     st.bytes = bytes;
  158.     st.midx = midx;
  159.     return st;
  160. end
  161. local function poly_update(st, m)
  162.     st.bytes, st.midx = #m, 1;
  163.     if (st.bytes >= 16) then
  164.         poly_blocks(st, m);
  165.     end
  166.     if (st.bytes == 0) then
  167.     else
  168.         local buffer = string.sub(m, st.midx) .. "\x01" .. string.rep("\0", (16 - st.bytes) - 1);
  169.         assert(#buffer == 16);
  170.         st.final = true;
  171.         poly_blocks(st, buffer);
  172.     end
  173.     return st;
  174. end
  175. local function poly_finish(st)
  176.     local c, mask;
  177.     local f;
  178.     local h0 = st.h[1];
  179.     local h1 = st.h[2];
  180.     local h2 = st.h[3];
  181.     local h3 = st.h[4];
  182.     local h4 = st.h[5];
  183.     c = bit32_rshift(h1, 26);
  184.     h1 = bit32_and(h1, 67108863);
  185.     h2 = h2 + c;
  186.     c = bit32_rshift(h2, 26);
  187.     h2 = bit32_and(h2, 67108863);
  188.     h3 = h3 + c;
  189.     c = bit32_rshift(h3, 26);
  190.     h3 = bit32_and(h3, 67108863);
  191.     h4 = h4 + c;
  192.     c = bit32_rshift(h4, 26);
  193.     h4 = bit32_and(h4, 67108863);
  194.     h0 = h0 + (c * 5);
  195.     c = bit32_rshift(h0, 26);
  196.     h0 = bit32_and(h0, 67108863);
  197.     h1 = h1 + c;
  198.     local g0 = h0 + 5;
  199.     c = bit32_rshift(g0, 26);
  200.     g0 = bit32_and(g0, 67108863);
  201.     local g1 = h1 + c;
  202.     c = bit32_rshift(g1, 26);
  203.     g1 = bit32_and(g1, 67108863);
  204.     local g2 = h2 + c;
  205.     c = bit32_rshift(g2, 26);
  206.     g2 = bit32_and(g2, 67108863);
  207.     local g3 = h3 + c;
  208.     c = bit32_rshift(g3, 26);
  209.     g3 = bit32_and(g3, 67108863);
  210.     local g4 = bit32_and((h4 + c) - 67108864, 4294967295);
  211.     mask = bit32_and(bit32_rshift(g4, 31) - 1, 4294967295);
  212.     g0 = bit32_and(g0, mask);
  213.     g1 = bit32_and(g1, mask);
  214.     g2 = bit32_and(g2, mask);
  215.     g3 = bit32_and(g3, mask);
  216.     g4 = bit32_and(g4, mask);
  217.     mask = bit32_and(bit32_negate(mask), 4294967295);
  218.     h0 = bit32_or(bit32_and(h0, mask), g0);
  219.     h1 = bit32_or(bit32_and(h1, mask), g1);
  220.     h2 = bit32_or(bit32_and(h2, mask), g2);
  221.     h3 = bit32_or(bit32_and(h3, mask), g3);
  222.     h4 = bit32_or(bit32_and(h4, mask), g4);
  223.     h0 = bit32_and(bit32_or(h0, bit32_lshift(h1, 26)), 4294967295);
  224.     h1 = bit32_and(bit32_or(bit32_rshift(h1, 6), bit32_lshift(h2, 20)), 4294967295);
  225.     h2 = bit32_and(bit32_or(bit32_rshift(h2, 12), bit32_lshift(h3, 14)), 4294967295);
  226.     h3 = bit32_and(bit32_or(bit32_rshift(h3, 18), bit32_lshift(h4, 8)), 4294967295);
  227.     f = h0 + st.pad[1];
  228.     h0 = bit32_and(f, 4294967295);
  229.     f = h1 + st.pad[2] + bit32_rshift(f, 32);
  230.     h1 = bit32_and(f, 4294967295);
  231.     f = h2 + st.pad[3] + bit32_rshift(f, 32);
  232.     h2 = bit32_and(f, 4294967295);
  233.     f = h3 + st.pad[4] + bit32_rshift(f, 32);
  234.     h3 = bit32_and(f, 4294967295);
  235.     local mac = lendian_string_pack_to_ints("<I4I4I4I4", h0, h1, h2, h3);
  236.     return mac;
  237. end
  238. local function poly_auth(m, k)
  239.     assert(#k == 32);
  240.     local st = poly_init(k);
  241.     poly_update(st, m);
  242.     local mac = poly_finish(st);
  243.     return mac;
  244. end
  245. local function poly_verify(m, k, mac)
  246.     local macm = poly_auth(m, k);
  247.     return macm == mac;
  248. end
  249.  
  250. local t = {
  251.     init = poly_init,
  252.     update = poly_update,
  253.     finish = poly_finish,
  254.     auth = poly_auth,
  255.     verify = poly_verify,
  256. }
  257.  
  258. function stringToASCII(str)
  259.     local ascii = {}
  260.     for i = 1, #str do
  261.         table.insert(ascii, string.byte(str, i))
  262.     end
  263.     return ascii
  264. end
  265.  
  266. function printStringAsASCII(str)
  267.     local ascii = stringToASCII(str)
  268.     for i = 1, #ascii do
  269.         print(ascii[i])
  270.     end
  271. end
  272.  
  273. printStringAsASCII(t.auth("1234123412341234","12345678123456781234567812345678"))
Advertisement
Add Comment
Please, Sign In to add comment