Advertisement
darraghd493

RobloxGamerPro200007's Advanced Encryption Standard in LuaU (minified/bundled)

Jan 3rd, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.93 KB | Source Code | 0 0
  1. --[[
  2.     Advanced Encryption Standard in LuaU (minified/bundled)
  3.     Author: RobloxGamerPro200007
  4.     Source: https://devforum.roblox.com/t/advanced-encryption-standard-in-luau/2009120?page=2
  5.  
  6.     Minified using darklua.com - as long as it works I'm happy.
  7. ]]
  8. local a=error
  9. local b=newproxy
  10. local c=getmetatable
  11. local d=typeof
  12.  
  13.  
  14. local e=bit32.bxor
  15. local f=bit32.rrotate
  16.  
  17.  
  18. local g=buffer.copy
  19. local h=buffer.create
  20. local i=buffer.fromstring
  21. local j=buffer.len
  22. local k=buffer.readu8
  23. local l=buffer.readu16
  24. local m=buffer.readu32
  25. local n=buffer.tostring
  26. local o=buffer.writestring
  27. local p=buffer.writeu8
  28. local q=buffer.writeu16
  29. local r=buffer.writeu32
  30.  
  31.  
  32. local s=math.floor
  33.  
  34.  
  35. local t=string.sub
  36.  
  37.  
  38.  
  39.  
  40. local u=h(131072)
  41. local v=h(65536)
  42. local w=h(65536)
  43. local x=h(65536)
  44. local y=h(65536)
  45. local z=h(65536)
  46.  
  47.  
  48.  
  49.  
  50.  
  51. local function keySchedule(A:buffer|string,B:number,C:buffer,D:boolean):buffer
  52.     if D then
  53.         g(C,0,A::buffer,0,B)
  54.     else
  55.         o(C,0,A::string,B)
  56.     end
  57.     local E=f(m(C,B-4),8)
  58.     local F=0.5
  59.  
  60.     if B==32 then
  61.         for G=32,192,32 do
  62.             F=F*2%229
  63.             E=e(m(C,G-32),l(u,s(E/65536)*2)*65536+
  64.                 l(u,E%65536*2),F)
  65.             r(C,G,E)
  66.  
  67.             E=e(m(C,G-28),E)
  68.             r(C,G+4,E)
  69.             E=e(m(C,G-24),E)
  70.             r(C,G+8,E)
  71.             E=e(m(C,G-20),E)
  72.             r(C,G+12,E)
  73.  
  74.             E=e(m(C,G-16),l(u,s(E/65536)*2)*65536+
  75.                 l(u,E%65536*2))
  76.             r(C,G+16,E)
  77.  
  78.             E=e(m(C,G-12),E)
  79.             r(C,G+20,E)
  80.             E=e(m(C,G-8),E)
  81.             r(C,G+24,E)
  82.             E=e(m(C,G-4),E)
  83.             r(C,G+28,E)
  84.             E=f(E,8)
  85.         end
  86.         E=e(m(C,192),l(u,s(E/65536)*2)*65536+
  87.             l(u,E%65536*2),64)
  88.         r(C,224,E)
  89.  
  90.         E=e(m(C,196),E)
  91.         r(C,228,E)
  92.         E=e(m(C,200),E)
  93.         r(C,232,E)
  94.         r(C,236,e(m(C,204),E))
  95.     elseif B==24 then
  96.         for G=24,168,24 do
  97.             F=F*2%229
  98.             E=e(m(C,G-24),l(u,s(E/65536)*2)*65536+
  99.                 l(u,E%65536*2),F)
  100.             r(C,G,E)
  101.  
  102.             E=e(m(C,G-20),E)
  103.             r(C,G+4,E)
  104.             E=e(m(C,G-16),E)
  105.             r(C,G+8,E)
  106.             E=e(m(C,G-12),E)
  107.             r(C,G+12,E)
  108.             E=e(m(C,G-8),E)
  109.             r(C,G+16,E)
  110.             E=e(m(C,G-4),E)
  111.             r(C,G+20,E)
  112.             E=f(E,8)
  113.         end
  114.         E=e(m(C,168),l(u,s(E/65536)*2)*65536+
  115.             l(u,E%65536*2),128)
  116.         r(C,192,E)
  117.  
  118.         E=e(m(C,172),E)
  119.         r(C,196,E)
  120.         E=e(m(C,176),E)
  121.         r(C,200,E)
  122.         r(C,204,e(m(C,180),E))
  123.     else
  124.         for G=16,144,16 do
  125.             F=F*2%229
  126.             E=e(m(C,G-16),l(u,s(E/65536)*2)*65536+
  127.                 l(u,E%65536*2),F)
  128.             r(C,G,E)
  129.  
  130.             E=e(m(C,G-12),E)
  131.             r(C,G+4,E)
  132.             E=e(m(C,G-8),E)
  133.             r(C,G+8,E)
  134.             E=e(m(C,G-4),E)
  135.             r(C,G+12,E)
  136.             E=f(E,8)
  137.         end
  138.         E=e(m(C,144),l(u,s(E/65536)*2)*65536+
  139.             l(u,E%65536*2),54)
  140.         r(C,160,E)
  141.  
  142.         E=e(m(C,148),E)
  143.         r(C,164,E)
  144.         E=e(m(C,152),E)
  145.         r(C,168,E)
  146.         r(C,172,e(m(C,156),E))
  147.     end
  148.     return C
  149. end
  150.  
  151.  
  152. local function encryptBlock(A:buffer,B:number,C:buffer,D:number,E:buffer,F:number):()
  153.  
  154.     local G=e(k(C,D),k(A,0))
  155.     local H=e(k(C,D+1),k(A,1))
  156.     local I=e(k(C,D+2),k(A,2))
  157.     local J=e(k(C,D+3),k(A,3))
  158.     local K=e(k(C,D+4),k(A,4))
  159.     local L=e(k(C,D+5),k(A,5))
  160.     local M=e(k(C,D+6),k(A,6))
  161.     local N=e(k(C,D+7),k(A,7))
  162.     local O=e(k(C,D+8),k(A,8))
  163.     local P=e(k(C,D+9),k(A,9))
  164.     local Q=e(k(C,D+10),k(A,10))
  165.     local R=e(k(C,D+11),k(A,11))
  166.     local S=e(k(C,D+12),k(A,12))
  167.     local T=e(k(C,D+13),k(A,13))
  168.     local U=e(k(C,D+14),k(A,14))
  169.     local V=e(k(C,D+15),k(A,15))
  170.  
  171.     local W=G*256+L;local X=L*256+Q;local Y=Q*256+V;local Z=V*256+G
  172.     local _=K*256+P;local aa=P*256+U;local ab=U*256+J;local ac=J*256+K
  173.     local ad=O*256+T;local ae=T*256+I;local af=I*256+N;local ag=N*256+O
  174.     local ah=S*256+H;local ai=H*256+M;local aj=M*256+R;local ak=R*256+S
  175.  
  176.     for al=16,B,16 do
  177.  
  178.         G=e(k(v,W),k(w,Y),k(A,al))
  179.         H=e(k(v,X),k(w,Z),k(A,al+1))
  180.         I=e(k(v,Y),k(w,W),k(A,al+2))
  181.         J=e(k(v,Z),k(w,X),k(A,al+3))
  182.         K=e(k(v,_),k(w,ab),k(A,al+4))
  183.         L=e(k(v,aa),k(w,ac),k(A,al+5))
  184.         M=e(k(v,ab),k(w,_),k(A,al+6))
  185.         N=e(k(v,ac),k(w,aa),k(A,al+7))
  186.         O=e(k(v,ad),k(w,af),k(A,al+8))
  187.         P=e(k(v,ae),k(w,ag),k(A,al+9))
  188.         Q=e(k(v,af),k(w,ad),k(A,al+10))
  189.         R=e(k(v,ag),k(w,ae),k(A,al+11))
  190.         S=e(k(v,ah),k(w,aj),k(A,al+12))
  191.         T=e(k(v,ai),k(w,ak),k(A,al+13))
  192.         U=e(k(v,aj),k(w,ah),k(A,al+14))
  193.         V=e(k(v,ak),k(w,ai),k(A,al+15))
  194.  
  195.         W,X,Y,Z,_,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak=
  196.             G*256+L,L*256+Q,Q*256+V,V*256+G,K*256+P,P*256+U,U*256+J,J*256+K,
  197.         O*256+T,T*256+I,I*256+N,N*256+O,S*256+H,H*256+M,M*256+R,R*256+S
  198.     end
  199.  
  200.  
  201.     r(E,F,e(l(u,e(k(v,ak),k(w,ai),
  202.         k(A,B+31))*512+e(k(v,af),k(w,ad),k(A,B+26))*2)
  203.             *65536+l(u,e(k(v,aa),k(w,ac),k(A,B+21))*512+
  204.                 e(k(v,W),k(w,Y),k(A,B+16))*2),m(A,B+32)))
  205.     r(E,F+4,e(l(u,e(k(v,Z),k(w,X),
  206.         k(A,B+19))*512+e(k(v,aj),k(w,ah),k(A,B+30))*2)
  207.             *65536+l(u,e(k(v,ae),k(w,ag),k(A,B+25))*512+
  208.                 e(k(v,_),k(w,ab),k(A,B+20))*2),m(A,B+36)))
  209.     r(E,F+8,e(l(u,e(k(v,ac),k(w,aa),
  210.         k(A,B+23))*512+e(k(v,Y),k(w,W),k(A,B+18))*2)
  211.             *65536+l(u,e(k(v,ai),k(w,ak),k(A,B+29))*512+
  212.                 e(k(v,ad),k(w,af),k(A,B+24))*2),m(A,B+40)))
  213.     r(E,F+12,e(l(u,e(k(v,ag),k(w,ae),
  214.         k(A,B+27))*512+e(k(v,ab),k(w,_),k(A,B+22))*2)
  215.             *65536+l(u,e(k(v,X),k(w,Z),k(A,B+17))*512+
  216.                 e(k(v,ah),k(w,aj),k(A,B+28))*2),m(A,B+44)))
  217. end
  218.  
  219. local function decryptBlock(aa:buffer,ab:number,ac:buffer,ad:number,ae:buffer,af:number):()
  220.  
  221.     local ag=e(k(x,k(ac,ad)*256+k(aa,ab+32)),k(aa,ab+16))
  222.     local ah=e(k(x,k(ac,ad+13)*256+k(aa,ab+45)),k(aa,ab+17))
  223.     local ai=e(k(x,k(ac,ad+10)*256+k(aa,ab+42)),k(aa,ab+18))
  224.     local aj=e(k(x,k(ac,ad+7)*256+k(aa,ab+39)),k(aa,ab+19))
  225.     local ak=e(k(x,k(ac,ad+4)*256+k(aa,ab+36)),k(aa,ab+20))
  226.     local al=e(k(x,k(ac,ad+1)*256+k(aa,ab+33)),k(aa,ab+21))
  227.     local A=e(k(x,k(ac,ad+14)*256+k(aa,ab+46)),k(aa,ab+22))
  228.     local B=e(k(x,k(ac,ad+11)*256+k(aa,ab+43)),k(aa,ab+23))
  229.     local C=e(k(x,k(ac,ad+8)*256+k(aa,ab+40)),k(aa,ab+24))
  230.     local D=e(k(x,k(ac,ad+5)*256+k(aa,ab+37)),k(aa,ab+25))
  231.     local E=e(k(x,k(ac,ad+2)*256+k(aa,ab+34)),k(aa,ab+26))
  232.     local F=e(k(x,k(ac,ad+15)*256+k(aa,ab+47)),k(aa,ab+27))
  233.     local G=e(k(x,k(ac,ad+12)*256+k(aa,ab+44)),k(aa,ab+28))
  234.     local H=e(k(x,k(ac,ad+9)*256+k(aa,ab+41)),k(aa,ab+29))
  235.     local I=e(k(x,k(ac,ad+6)*256+k(aa,ab+38)),k(aa,ab+30))
  236.     local J=e(k(x,k(ac,ad+3)*256+k(aa,ab+35)),k(aa,ab+31))
  237.  
  238.     local K=ag*256+ah;local L=ah*256+ai;local M=ai*256+aj;local N=aj*256+ag
  239.     local O=ak*256+al;local P=al*256+A;local Q=A*256+B;local R=B*256+ak
  240.     local S=C*256+D;local T=D*256+E;local U=E*256+F;local V=F*256+C
  241.     local W=G*256+H;local X=H*256+I;local Y=I*256+J;local Z=J*256+G
  242.  
  243.     for _=ab,16,-16 do
  244.  
  245.         ag=e(k(x,k(y,K)*256+k(z,M)),k(aa,_))
  246.         ah=e(k(x,k(y,X)*256+k(z,Z)),k(aa,_+1))
  247.         ai=e(k(x,k(y,U)*256+k(z,S)),k(aa,_+2))
  248.         aj=e(k(x,k(y,R)*256+k(z,P)),k(aa,_+3))
  249.         ak=e(k(x,k(y,O)*256+k(z,Q)),k(aa,_+4))
  250.         al=e(k(x,k(y,L)*256+k(z,N)),k(aa,_+5))
  251.         A=e(k(x,k(y,Y)*256+k(z,W)),k(aa,_+6))
  252.         B=e(k(x,k(y,V)*256+k(z,T)),k(aa,_+7))
  253.         C=e(k(x,k(y,S)*256+k(z,U)),k(aa,_+8))
  254.         D=e(k(x,k(y,P)*256+k(z,R)),k(aa,_+9))
  255.         E=e(k(x,k(y,M)*256+k(z,K)),k(aa,_+10))
  256.         F=e(k(x,k(y,Z)*256+k(z,X)),k(aa,_+11))
  257.         G=e(k(x,k(y,W)*256+k(z,Y)),k(aa,_+12))
  258.         H=e(k(x,k(y,T)*256+k(z,V)),k(aa,_+13))
  259.         I=e(k(x,k(y,Q)*256+k(z,O)),k(aa,_+14))
  260.         J=e(k(x,k(y,N)*256+k(z,L)),k(aa,_+15))
  261.  
  262.         K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z=
  263.             ag*256+ah,ah*256+ai,ai*256+aj,aj*256+ag,ak*256+al,al*256+A,A*256+B,B*256+ak,
  264.         C*256+D,D*256+E,E*256+F,F*256+C,G*256+H,H*256+I,I*256+J,J*256+G
  265.     end
  266.  
  267.  
  268.     r(ae,af,e(k(x,k(y,R)*256+k(z,P)),
  269.         k(aa,3))*16777216+e(k(x,k(y,U)*256+k(z,S)),
  270.             k(aa,2))*65536+e(k(x,k(y,X)*256+k(z,Z)),
  271.             k(aa,1))*256+e(k(x,k(y,K)*256+k(z,M)),
  272.             k(aa,0)))
  273.     r(ae,af+4,e(k(x,k(y,V)*256+k(z,T)),
  274.         k(aa,7))*16777216+e(k(x,k(y,Y)*256+k(z,W)),
  275.             k(aa,6))*65536+e(k(x,k(y,L)*256+k(z,N)),
  276.             k(aa,5))*256+e(k(x,k(y,O)*256+k(z,Q)),
  277.             k(aa,4)))
  278.     r(ae,af+8,e(k(x,k(y,Z)*256+k(z,X)),
  279.         k(aa,11))*16777216+e(k(x,k(y,M)*256+k(z,K)),
  280.             k(aa,10))*65536+e(k(x,k(y,P)*256+k(z,R)),
  281.             k(aa,9))*256+e(k(x,k(y,S)*256+k(z,U)),
  282.             k(aa,8)))
  283.     r(ae,af+12,e(k(x,k(y,N)*256+k(z,L)),
  284.         k(aa,15))*16777216+e(k(x,k(y,Q)*256+k(z,O)),
  285.             k(aa,14))*65536+e(k(x,k(y,T)*256+k(z,V)),
  286.             k(aa,13))*256+e(k(x,k(y,W)*256+k(z,Y)),
  287.             k(aa,12)))
  288. end
  289.  
  290.  
  291.  
  292.  
  293. do
  294.     local aa=h(256)
  295.     local ab=h(256)
  296.  
  297.     local ac=h(256)
  298.     local ad=h(256)
  299.     local ae=h(256)
  300.     local af=1;local ag=1;local ah
  301.  
  302.     local function gfmul(ai:number,aj:number):number
  303.         local ak=0
  304.  
  305.         for al=0,7 do
  306.             if aj%2==1 then
  307.                 ak=e(ak,ai)
  308.             end
  309.  
  310.             if ai>=128 then
  311.                 ai=e(ai*2%256,27)
  312.             else
  313.                 ai=ai*2%256
  314.             end
  315.             aj=s(aj/2)
  316.         end
  317.         return ak
  318.     end
  319.  
  320.  
  321.     p(aa,0,99)
  322.     for ai=1,255 do
  323.         af=e(af,af*2,if af<128 then 0 else 27)%256
  324.         ag=e(ag,ag*2)
  325.         ag=e(ag,ag*4)
  326.         ag=e(ag,ag*16)%256
  327.         if ag>=128 then
  328.             ag=e(ag,9)
  329.         end
  330.  
  331.         ah=e(ag,ag%128*2+ag/128,ag%64*4+ag/64,ag%32*8+ag/32,ag%16*16+ag/16,99)
  332.         p(aa,af,ah)
  333.         p(ab,ah,af)
  334.  
  335.         p(ac,af,gfmul(3,af))
  336.         p(ad,af,gfmul(9,af))
  337.         p(ae,af,gfmul(11,af))
  338.     end
  339.  
  340.  
  341.     local ai,aj,ak,al;ah=0
  342.     for A=0,255 do
  343.         af=k(aa,A);ai=af*256
  344.         aj,al,ak=gfmul(2,af),gfmul(13,A),gfmul(14,A)
  345.         for B=0,255 do
  346.             ag=k(aa,B)
  347.             q(u,ah*2,ai+ag)
  348.             p(x,ah,k(ab,e(A,B)))
  349.             p(v,ah,e(aj,k(ac,ag)))
  350.             p(w,ah,e(af,ag))
  351.             p(y,ah,e(ak,k(ae,B)))
  352.             p(z,ah,e(al,k(ad,B)))
  353.             ah+=1
  354.         end
  355.     end
  356. end
  357.  
  358.  
  359.  
  360.  
  361.  
  362. export type AesCipher=typeof(setmetatable({}::{
  363.     Key:string,Length:number,Mode:{FwdMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
  364.         (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{any},...any)->(),
  365.         InvMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
  366.             (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{any},...any)->()},
  367.     Padding:{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:boolean?},RoundKeys:string,
  368.     Encrypt:(self:AesCipher,plaintext:buffer|string,output:buffer?,...any)->buffer,
  369.     Decrypt:(self:AesCipher,ciphertext:buffer|string,output:buffer?,...any)->buffer,
  370.     EncryptBlock:(self:AesCipher,plaintext:buffer,offset:number,output:buffer?,offsetOut:number?)->(),
  371.     DecryptBlock:(self:AesCipher,ciphertext:buffer,offset:number,output:buffer?,offsetOut:number?)->(),
  372.     Destroy:(self:AesCipher)->()
  373. },{}))
  374. local function newidx(aa,ab)
  375.     return a(`{ab} cannot be assigned to`)
  376. end
  377. local function tostr()
  378.     return"AesCipher"
  379. end
  380.  
  381. local aa=require(loadstring[[local a=assert
  382.  
  383. local b=bit32.bxor
  384.  
  385. local c=buffer.copy
  386. local d=buffer.create
  387. local e=buffer.len
  388. local f=buffer.readu8
  389. local g=buffer.readu32 local h=
  390.     buffer.tostring
  391. local i=buffer.writestring
  392. local j=buffer.writeu8
  393. local k=buffer.writeu32 local l=
  394.  
  395.     math.floor
  396. local m=math.random
  397.  
  398. local n=table.freeze
  399.  
  400. local o=string.pack
  401.  
  402.  
  403. local p=d(16)
  404.  
  405. local function addByteCtr(q:buffer,r:number,s:number,t:number,u:boolean):()
  406.     local v
  407.     if u then
  408.         v=f(q,s)+r
  409.         j(q,s,v)
  410.         if v>=256 then
  411.             for w=s+1,t do
  412.                 v=f(q,w)+1
  413.                 j(q,w,v)
  414.                 if v<256 then
  415.                     break
  416.                 end
  417.             end
  418.         end
  419.     else
  420.         v=f(q,t)+r
  421.         j(q,t,v)
  422.         if v>=256 then
  423.             for w=t-1,s,-1 do
  424.                 v=f(q,w)+1
  425.                 j(q,w,v)
  426.                 if v<256 then
  427.                     break
  428.                 end
  429.             end
  430.         end
  431.     end
  432. end
  433.  
  434. local q={}
  435.  
  436.  
  437.  
  438.  
  439.  
  440. q.ECB=n{
  441.     FwdMode=function(r,s,t,u)
  442.         local v=e(t)-16
  443.         a(v%16==0,"Input length must be a multiple of 16 bytes")
  444.         for w=0,v,16 do
  445.             r(t,w,u,w)
  446.         end
  447.     end,
  448.     InvMode=function(r,s,t,u)
  449.         local v=e(t)-16
  450.         a(v%16==0,"Input length must be a multiple of 16 bytes")
  451.         for w=0,v,16 do
  452.             s(t,w,u,w)
  453.         end
  454.     end
  455. }
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462. q.CBC=n{
  463.     FwdMode=function(r,s,t,u,v,w)
  464.         local x=e(t)-16
  465.         a(x%16==0,"Input length must be a multiple of 16 bytes")
  466.         w=w or p
  467.         a(e(w)==16,"Initialization vector must be 16 bytes long")
  468.         k(u,0,b(g(t,0),g(w,0)))
  469.         k(u,4,b(g(t,4),g(w,4)))
  470.         k(u,8,b(g(t,8),g(w,8)))
  471.         k(u,12,b(g(t,12),g(w,12)))
  472.         r(u,0,u,0)
  473.         for y=16,x,16 do
  474.             k(u,y,b(g(t,y),g(u,y-16)))
  475.             k(u,y+4,b(g(t,y+4),g(u,y-12)))
  476.             k(u,y+8,b(g(t,y+8),g(u,y-8)))
  477.             k(u,y+12,b(g(t,y+12),g(u,y-4)))
  478.             r(u,y,u,y)
  479.         end
  480.     end,
  481.     InvMode=function(r,t,u,v,w,x)
  482.         local y=e(u)-16
  483.         a(y%16==0,"Input length must be a multiple of 16 bytes")
  484.         x=x or p
  485.         a(e(x)==16,"Initialization vector must be 16 bytes long")
  486.         local z=g(u,0);local A=g(u,4)
  487.         local B=g(u,8);local C=g(u,12)
  488.         local D,E,F,G
  489.         t(u,0,v,0)
  490.         k(v,0,b(g(v,0),g(x,0)))
  491.         k(v,4,b(g(v,4),g(x,4)))
  492.         k(v,8,b(g(v,8),g(x,8)))
  493.         k(v,12,b(g(v,12),g(x,12)))
  494.         for H=16,y,16 do
  495.             D=g(u,H)
  496.             E=g(u,H+4)
  497.             F=g(u,H+8)
  498.             G=g(u,H+12)
  499.             t(u,H,v,H)
  500.             k(v,H,b(g(v,H),z))
  501.             k(v,H+4,b(g(v,H+4),A))
  502.             k(v,H+8,b(g(v,H+8),B))
  503.             k(v,H+12,b(g(v,H+12),C))
  504.             z,A,B,C=D,E,F,G
  505.         end
  506.     end
  507. }
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514. q.PCBC=n{
  515.     FwdMode=function(t,u,v,w,x,y)
  516.         local z=e(v)-16
  517.         a(z%16==0,"Input length must be a multiple of 16 bytes")
  518.         y=y or p
  519.         a(e(y)==16,"Initialization vector must be 16 bytes long")
  520.         local A=g(v,0);local B=g(v,4)
  521.         local C=g(v,8);local D=g(v,12)
  522.         local E,F,G,H
  523.         k(w,0,b(A,g(y,0)))
  524.         k(w,4,b(B,g(y,4)))
  525.         k(w,8,b(C,g(y,8)))
  526.         k(w,12,b(D,g(y,12)))
  527.         t(w,0,w,0)
  528.         for I=16,z,16 do
  529.             E=g(v,I)
  530.             F=g(v,I+4)
  531.             G=g(v,I+8)
  532.             H=g(v,I+12)
  533.             k(w,I,b(A,E,g(w,I-16)))
  534.             k(w,I+4,b(B,F,g(w,I-12)))
  535.             k(w,I+8,b(C,G,g(w,I-8)))
  536.             k(w,I+12,b(D,H,g(w,I-4)))
  537.             t(w,I,w,I)
  538.             A,B,C,D=E,F,G,H
  539.         end
  540.     end,
  541.     InvMode=function(t,v,w,x,y,z)
  542.         local A=e(w)-16
  543.         a(A%16==0,"Input length must be a multiple of 16 bytes")
  544.         z=z or p
  545.         a(e(z)==16,"Initialization vector must be 16 bytes long")
  546.         local B=g(w,0);local C=g(w,4)
  547.         local D=g(w,8);local E=g(w,12)
  548.         v(w,0,x,0)
  549.         local F=b(g(x,0),g(z,0))
  550.         local G=b(g(x,4),g(z,4))
  551.         local H=b(g(x,8),g(z,8))
  552.         local I=b(g(x,12),g(z,12))
  553.         local J,K,L,M
  554.         k(x,0,F)
  555.         k(x,4,G)
  556.         k(x,8,H)
  557.         k(x,12,I)
  558.         local N,O,P,Q=0,4,8,12
  559.         for R=16,A,16 do
  560.             N+=16;O+=16;P+=16;Q+=16
  561.             J=g(w,N)
  562.             K=g(w,O)
  563.             L=g(w,P)
  564.             M=g(w,Q)
  565.             v(w,R,x,R)
  566.             F=b(B,F,g(x,N))
  567.             G=b(C,G,g(x,O))
  568.             H=b(D,H,g(x,P))
  569.             I=b(E,I,g(x,Q))
  570.             B,C,D,E=J,K,L,M
  571.             k(x,N,F)
  572.             k(x,O,G)
  573.             k(x,P,H)
  574.             k(x,Q,I)
  575.         end
  576.     end
  577. }
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588. local function cfbFwd(v,w,x,y,z:{CommonTemp:buffer,SegmentSize:number},A)
  589.     local B=z.SegmentSize
  590.     local C=e(x)
  591.     a(C%B==0,"Input length must be a multiple of segment size")
  592.     A=A or p
  593.     a(e(A)==16,"Initialization vector must be 16 bytes long")
  594.     local D=z.CommonTemp or d(31)
  595.     if C==B then
  596.         v(A,0,D,0)
  597.         for E=0,B-1 do
  598.             j(y,E,b(f(x,E),f(D,E)))
  599.         end
  600.     else
  601.         local E=C-B;local F=16-B
  602.         local G
  603.         v(A,0,D,0)
  604.         for H=0,B-1 do
  605.             j(y,H,b(f(x,H),f(D,H)))
  606.         end
  607.         c(D,0,A,B,F)
  608.         c(D,F,y,0,B)
  609.         for H=B,E-B,B do
  610.             G=0
  611.             c(D,16,D,B,F)
  612.             v(D,0,D,0)
  613.             for I=H,H+B-1 do
  614.                 j(y,I,b(f(x,I),f(D,G)))
  615.                 G+=1
  616.             end
  617.             c(D,0,D,16,F)
  618.             c(D,F,y,H,B)
  619.         end
  620.         v(D,0,D,0)
  621.         G=0
  622.         for H=E,C-1 do
  623.             j(y,H,b(f(x,H),f(D,G)))
  624.             G+=1
  625.         end
  626.     end
  627. end
  628. local function cfbInv(v,w,x,y,z:{CommonTemp:buffer,SegmentSize:number},A)
  629.     local B=e(x)
  630.     local C=z.SegmentSize
  631.     a(B%C==0,"Input length must be a multiple of segment size")
  632.     A=A or p
  633.     a(e(A)==16,"Initialization vector must be 16 bytes long")
  634.     local D=z.CommonTemp or d(31)
  635.     if B==C then
  636.         v(A,0,D,0)
  637.         for E=0,C-1 do
  638.             j(y,E,b(f(x,E),f(D,E)))
  639.         end
  640.     else
  641.         local E=B-C;local F=16-C
  642.         local G
  643.         v(A,0,D,0)
  644.         for H=0,C-1 do
  645.             j(y,H,b(f(x,H),f(D,H)))
  646.         end
  647.         c(D,0,A,C,F)
  648.         c(D,F,x,0,C)
  649.         for H=C,E-C,C do
  650.             G=0
  651.             c(D,16,D,C,F)
  652.             v(D,0,D,0)
  653.             for I=H,H+C-1 do
  654.                 j(y,I,b(f(x,I),f(D,G)))
  655.                 G+=1
  656.             end
  657.             c(D,0,D,16,F)
  658.             c(D,F,x,H,C)
  659.         end
  660.         v(D,0,D,0)
  661.         G=0
  662.         for H=E,B-1 do
  663.             j(y,H,b(f(x,H),f(D,G)))
  664.             G+=1
  665.         end
  666.     end
  667. end
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676. local function ofbMode(v,w,x:buffer,y:buffer,z,A:buffer)
  677.     local B=e(x)-16
  678.     a(B%16==0,"Input length must be a multiple of 16 bytes")
  679.     A=A or p
  680.     a(e(A)==16,"Initialization vector must be 16 bytes long")
  681.     local C=g(x,0);local D=g(x,4)
  682.     local E=g(x,8);local F=g(x,12)
  683.     v(A,0,y,0)
  684.     C=b(C,g(y,0))
  685.     D=b(D,g(y,4))
  686.     E=b(E,g(y,8))
  687.     F=b(F,g(y,12))
  688.     local G,H,I,J
  689.     for K=16,B,16 do
  690.         G=g(x,K)
  691.         H=g(x,K+4)
  692.         I=g(x,K+8)
  693.         J=g(x,K+12)
  694.         v(y,K-16,y,K)
  695.         k(y,K-16,C)
  696.         k(y,K-12,D)
  697.         k(y,K-8,E)
  698.         k(y,K-4,F)
  699.         C=b(G,g(y,K))
  700.         D=b(H,g(y,K+4))
  701.         E=b(I,g(y,K+8))
  702.         F=b(J,g(y,K+12))
  703.     end
  704.     k(y,B,C)
  705.     k(y,B+4,D)
  706.     k(y,B+8,E)
  707.     k(y,B+12,F)
  708. end
  709. q.OFB=n{
  710.     FwdMode=ofbMode,InvMode=ofbMode
  711. }
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728. local function ctrMode(v,x,y,z,A:{CommonTemp:buffer,InitValue:string,Prefix:string,Suffix:string,
  729.     Step:number,LittleEndian:boolean})
  730.     local B=e(y)-16
  731.     a(B%16==0,"Input length must be a multiple of 16 bytes")
  732.     local C=A.CommonTemp
  733.     local D=A.InitValue;local E=A.Prefix
  734.     local F=A.Suffix;local G=A.Step;local H=A.LittleEndian
  735.     local I=#E;local J=I+#D-1
  736.     i(C,0,E)
  737.     i(C,I,D)
  738.     i(C,J+1,F)
  739.     local K=g(y,0);local L=g(y,4)
  740.     local M=g(y,8);local N=g(y,12)
  741.     v(C,0,z,0)
  742.     k(z,0,b(g(z,0),K))
  743.     k(z,4,b(g(z,4),L))
  744.     k(z,8,b(g(z,8),M))
  745.     k(z,12,b(g(z,12),N))
  746.     for O=16,B,16 do
  747.         K=g(y,O)
  748.         L=g(y,O+4)
  749.         M=g(y,O+8)
  750.         N=g(y,O+12)
  751.         addByteCtr(C,G,I,J,H)
  752.         v(C,0,z,O)
  753.         k(z,O,b(K,g(z,O)))
  754.         k(z,O+4,b(L,g(z,O+4)))
  755.         k(z,O+8,b(M,g(z,O+8)))
  756.         k(z,O+12,b(N,g(z,O+12)))
  757.     end
  758. end
  759.  
  760. local v={
  761.     __index=function(v,x:"CFB"|"CTR"):{}?
  762.         return if x=="CFB"then{
  763.             FwdMode=cfbFwd,InvMode=cfbInv,
  764.             SegmentSize=16,
  765.             CommonTemp=d(31)
  766.         }elseif x=="CTR"then{
  767.                 FwdMode=ctrMode,InvMode=ctrMode,
  768.                 InitValue=o("I2I2I2I2I2I2I2I2",m(0,65535),m(0,65535),m(0,65535),
  769.                     m(0,65535),m(0,65535),m(0,65535),m(0,65535),m(0,65535)),
  770.                 Prefix="",Suffix="",Step=1,LittleEndian=false,
  771.                 CommonTemp=d(16)
  772.             }else nil
  773.     end,
  774.     __newindex=function()
  775.     end
  776. }
  777. setmetatable(q,v)
  778. q.CFB={}::{FwdMode:typeof(cfbFwd),InvMode:typeof(cfbInv),CommonTemp:buffer,SegmentSize:number}
  779. q.CTR={}::{FwdMode:typeof(ctrMode),InvMode:typeof(ctrMode),CommonTemp:buffer,InitValue:string,Prefix:string,Suffix:string,
  780.     Step:number,LittleEndian:boolean}
  781. n(q)
  782. v.__metatable="This metatable is locked"
  783. return q]]())
  784. local ab=require(loadstring[[local a=assert
  785. local b=error
  786.  
  787. local c=buffer.copy
  788. local d=buffer.create
  789. local e=buffer.fill
  790. local f=buffer.len
  791. local g=buffer.readu8
  792. local h=buffer.writeu8
  793.  
  794. local i=math.random
  795.  
  796. local j=table.freeze
  797.  
  798. local k={}
  799.  
  800.  
  801.  
  802.  
  803. local function nonPad(l)
  804.     return l
  805. end
  806. k.None=j{
  807.     Pad=nonPad,Unpad=nonPad,
  808.     Overwrite=false
  809. }
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816. local function anxPad(l,m,n)
  817.     local o=f(l)
  818.     local p=o-o%n
  819.     if m then
  820.         a(f(m)>=o+n,"Output buffer out of bounds")
  821.         local q=n-o%n
  822.         c(m,0,l,0,o)
  823.         e(m,o,0,q-1)
  824.         h(m,p+n-1,q)
  825.     else
  826.         p+=n
  827.         m=d(p)
  828.         c(m,0,l,0,o)
  829.         h(m,p-1,n-o%n)
  830.     end
  831.     return m
  832. end
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855. local function i10Pad(l,m,n)
  856.     local o=f(l)
  857.     local p=o-o%n
  858.     if m then
  859.         a(f(m)>=o+n,"Output buffer out of bounds")
  860.     else
  861.         m=d(p+n)
  862.     end
  863.     c(m,0,l,0,o)
  864.     for q=o,p+n-2 do
  865.         h(m,q,i(0,255))
  866.     end
  867.     h(m,p+n-1,n-o%n)
  868.     return m
  869. end
  870. local function i10Unpad(l,m,n)
  871.     local o=f(l)
  872.     local p=g(l,o-1)
  873.     local q=o-p
  874.     a(0<p and p<=n,"Got unexpected padding")
  875.     if m then
  876.         a(f(m)>=q,"Output buffer out of bounds")
  877.     else
  878.         m=d(q)
  879.     end
  880.     c(m,0,l,0,q)
  881.     return m
  882. end
  883.  
  884.  
  885.  
  886.  
  887. local function pksPad(l,m,n)
  888.     local o=f(l)
  889.     local p=o-o%n
  890.     if m then
  891.         a(f(m)>=o+n,"Output buffer out of bounds")
  892.     else
  893.         m=d(p+n)
  894.     end
  895.     local q=n-o%n
  896.     c(m,0,l,0,o)
  897.     e(m,o,q,q)
  898.     return m
  899. end
  900. local function pksUnpad(l,m,n)
  901.     local o=f(l)
  902.     local p=g(l,o-1)
  903.     local q=o-p
  904.     a(0<p and p<=n,"Got unexpected padding")
  905.     for r=q,o-2 do
  906.         if g(l,r)~=p then
  907.             b"Got unexpected padding"
  908.         end
  909.     end
  910.     if m then
  911.         a(f(m)>=q,"Output buffer out of bounds")
  912.     else
  913.         m=d(q)
  914.     end
  915.     c(m,0,l,0,q)
  916.     return m
  917. end
  918.  
  919.  
  920.  
  921.  
  922. local function ii7Pad(l,m,n)
  923.     local o=f(l)
  924.     if m then
  925.         a(f(m)>=o+n,"Output buffer out of bounds")
  926.         e(m,o+1,0,n-o%n-1)
  927.     else
  928.         m=d(o+n-o%n)
  929.     end
  930.     c(m,0,l,0,o)
  931.     h(m,o,128)
  932.     return m
  933. end
  934. local function ii7Unpad(l,m,n)
  935.     local o=f(l)-1
  936.     local p
  937.     for q=o,o-n,-1 do
  938.         p=g(l,q)
  939.         if p==128 then
  940.             if m then
  941.                 a(f(m)>=q,"Output buffer out of bounds")
  942.             else
  943.                 m=d(q)
  944.             end
  945.             c(m,0,l,0,q)
  946.             return m
  947.         else
  948.             a(p==0,"Got unexpected padding")
  949.         end
  950.     end
  951.     b"Got unexpected padding"
  952.     return d(0)
  953. end
  954.  
  955.  
  956.  
  957.  
  958. local function zroPad(l,m,n)
  959.     local o=f(l)
  960.     if m then
  961.         a(f(m)>=o+n,"Output buffer out of bounds")
  962.         e(m,o,0,n-o%n)
  963.     else
  964.         m=d(o+n-o%n)
  965.     end
  966.     c(m,0,l,0,o)
  967.     return m
  968. end
  969. local function zroUnpad(l,m,n)
  970.     local o=f(l)-1
  971.     local p
  972.     for q=o,o-n,-1 do
  973.         p=g(l,q)
  974.         if p==0 then
  975.             q+=1
  976.             if m then
  977.                 a(f(m)>=q,"Output buffer out of bounds")
  978.             else
  979.                 m=d(q)
  980.             end
  981.             c(m,0,l,0,q)
  982.             return m
  983.         end
  984.     end
  985.     c(m,0,l,0,o-n-1)
  986.     return m
  987. end
  988.  
  989. local l={
  990.     __index=function(l,m:"AnsiX923"|"Iso10126"|"Pkcs7"|"Iso7816_4"|"Zero"):{}?
  991.         return if m=="AnsiX923"then{
  992.             Pad=anxPad,Unpad=anxPad,
  993.             Overwrite=nil
  994.         }elseif m=="Iso10126"then{
  995.                 Pad=i10Pad,Unpad=i10Unpad,
  996.                 Overwrite=nil
  997.             }elseif m=="Pkcs7"then{
  998.                 Pad=pksPad,Unpad=pksUnpad,
  999.                 Overwrite=nil
  1000.             }elseif m=="Iso7816_4"then{
  1001.                 Pad=ii7Pad,Unpad=ii7Unpad,
  1002.                 Overwrite=nil
  1003.             }elseif m=="Zero"then{
  1004.                 Pad=zroPad,Unpad=zroUnpad,
  1005.                 Overwrite=nil
  1006.             }else nil
  1007.     end,
  1008.     __newindex=function()end
  1009. }
  1010. setmetatable(k,l)
  1011. k.AnsiX923={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
  1012. k.Iso10126={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
  1013. k.Pkcs7={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
  1014. k.Iso7816_4={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
  1015. k.Zero={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
  1016. j(k)
  1017. l.__metatable="This metatable is locked"
  1018. return k
  1019. ]]())
  1020. local function expandKey(ac:buffer|string,ad:buffer?):buffer
  1021.     local ae=d(ac)=="buffer"
  1022.     local af=if ae then j(ac::buffer)else#(ac::string)
  1023.     local ag=if af==32 then 240 elseif af==24 then 208 elseif af==16 then 176 else a"Key must be either 16, 24 or 32 bytes long"
  1024.     return keySchedule(ac,af,ad or h(ag::number),ae)
  1025. end
  1026. local function fromKey(ac:buffer,ad:{FwdMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
  1027.     (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{[string]:any},...any)->(),
  1028.     InvMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
  1029.         (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{[string]:any},...any)->()}?,
  1030.     ae:{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:boolean?}?):AesCipher
  1031.     local af:number?=j(ac)
  1032.     local ag:number?;local ah:string?
  1033.     local ai:string?=n(ac)
  1034.     if af==240 then
  1035.         ag=192
  1036.         ah=t(ai::string,1,32)
  1037.     elseif af==208 then
  1038.         ag=160
  1039.         ah=t(ai::string,1,24)
  1040.     elseif af==176 then
  1041.         ag=128
  1042.         ah=t(ai::string,1,16)
  1043.     else
  1044.         a"Round keys must be either 240, 208 or 128 bytes long"
  1045.     end
  1046.  
  1047.  
  1048.     local aj:buffer?=ac
  1049.     local ak:any=ad or aa.ECB
  1050.     local al=ak.FwdMode;local A=ak.InvMode;local B=ak.SegmentSize or 16
  1051.     local C:any=ae or ab.Pkcs7
  1052.     local D=C.Pad;local E=C.Unpad
  1053.     local F=b(true)::AesCipher
  1054.     local G=c(F)
  1055.  
  1056.     local function encp(H,I,J,K)
  1057.         encryptBlock(aj::buffer,ag::number,H,I,J,K)
  1058.     end
  1059.     local function decp(H,I,J,K)
  1060.         decryptBlock(aj::buffer,ag::number,H,I,J,K)
  1061.     end
  1062.     local function enc(H:AesCipher,I,J,...)
  1063.         local K=d(I)
  1064.         local L=if K=="buffer"then I::buffer elseif K=="string"then i(I)
  1065.             else a(`Unable to cast {K} to buffer`)
  1066.         J=d(J)=="buffer"and J
  1067.         if H~=F then
  1068.             return H:Encrypt(L,J::buffer,...)
  1069.         elseif ag then
  1070.             local M=D(L,J,B)
  1071.             al(encp,decp,if C.Overwrite==false then L else M,M,ak,...)
  1072.             return M
  1073.         else
  1074.             a"AesCipher object's already destroyed"
  1075.             return h(0)
  1076.         end
  1077.     end
  1078.     local function encb(H:AesCipher,I,J,K,L)
  1079.         if H~=F then
  1080.             H:EncryptBlock(I,J,K,L)
  1081.         elseif ag then
  1082.             encryptBlock(aj::buffer,ag::number,I,J,K or I,L or J)
  1083.         else
  1084.             a"AesCipher object's already destroyed"
  1085.         end
  1086.     end
  1087.     local function dec(H:AesCipher,I,J,...)
  1088.         local K=d(I)
  1089.         local L=if K=="buffer"then I::buffer elseif K=="string"then i(I)
  1090.             else a(`Unable to cast {K} to buffer`)
  1091.         J=d(J)=="buffer"and J
  1092.         if H~=F then
  1093.             return H:Decrypt(L,J::buffer,...)
  1094.         elseif ag then
  1095.             local M=C.Overwrite
  1096.             local N=if M==nil then h(j(L))elseif M then L else J or h(j(L))
  1097.             A(encp,decp,L,N,ak,...)
  1098.             return E(N,J,B)
  1099.         else
  1100.             a"AesCipher object's already destroyed"
  1101.             return h(0)
  1102.         end
  1103.     end
  1104.     local function decb(H:AesCipher,I,J,K,L)
  1105.         if H~=F then
  1106.             H:DecryptBlock(I,J,K,L)
  1107.         elseif ag then
  1108.             decryptBlock(aj::buffer,ag::number,I,J,K or I,L or J)
  1109.         else
  1110.             a"AesCipher object's already destroyed"
  1111.         end
  1112.     end
  1113.     local function destroy(H:AesCipher):()
  1114.         if H~=F then
  1115.             H:Destroy()
  1116.         elseif ag then
  1117.             ai,aj,ag,al,A,ak,C,ah,af=nil,nil,nil,nil,nil,nil,nil,nil,nil
  1118.         else
  1119.             a"AesCipher object's already destroyed"
  1120.         end
  1121.     end
  1122.  
  1123.  
  1124.     G.__index=function(H,I)
  1125.         return if I=="Encrypt"then enc elseif I=="Decrypt"then dec elseif I=="EncryptBlock"then encb elseif I=="DecryptBlock"then decb
  1126.             elseif I=="Destroy"then destroy elseif ag then(if I=="Key"then ah elseif I=="RoundKeys"then ai
  1127.                 elseif I=="Mode"then ak elseif I=="Padding"then C elseif I=="Length"then af
  1128.                 else a(`{I} is not a valid member of AesCipher`))else a"AesCipher object's already destroyed"
  1129.     end
  1130.     G.__newindex=newidx
  1131.     G.__tostring=tostr
  1132.     G.__len=function():number
  1133.         return af or a"AesCipher object's destroyed"
  1134.     end
  1135.     G.__metatable="AesCipher object: Metatable's locked"
  1136.     return F
  1137. end
  1138.  
  1139. return table.freeze{
  1140.     new=function(ac,ad,ae)
  1141.         return fromKey(expandKey(ac),ad,ae)
  1142.     end,
  1143.     expandKey=expandKey,
  1144.     fromKey=fromKey,
  1145.     modes=aa,
  1146.     pads=ab
  1147. }
Tags: minified
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement