Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Advanced Encryption Standard in LuaU (minified/bundled)
- Author: RobloxGamerPro200007
- Source: https://devforum.roblox.com/t/advanced-encryption-standard-in-luau/2009120?page=2
- Minified using darklua.com - as long as it works I'm happy.
- ]]
- local a=error
- local b=newproxy
- local c=getmetatable
- local d=typeof
- local e=bit32.bxor
- local f=bit32.rrotate
- local g=buffer.copy
- local h=buffer.create
- local i=buffer.fromstring
- local j=buffer.len
- local k=buffer.readu8
- local l=buffer.readu16
- local m=buffer.readu32
- local n=buffer.tostring
- local o=buffer.writestring
- local p=buffer.writeu8
- local q=buffer.writeu16
- local r=buffer.writeu32
- local s=math.floor
- local t=string.sub
- local u=h(131072)
- local v=h(65536)
- local w=h(65536)
- local x=h(65536)
- local y=h(65536)
- local z=h(65536)
- local function keySchedule(A:buffer|string,B:number,C:buffer,D:boolean):buffer
- if D then
- g(C,0,A::buffer,0,B)
- else
- o(C,0,A::string,B)
- end
- local E=f(m(C,B-4),8)
- local F=0.5
- if B==32 then
- for G=32,192,32 do
- F=F*2%229
- E=e(m(C,G-32),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2),F)
- r(C,G,E)
- E=e(m(C,G-28),E)
- r(C,G+4,E)
- E=e(m(C,G-24),E)
- r(C,G+8,E)
- E=e(m(C,G-20),E)
- r(C,G+12,E)
- E=e(m(C,G-16),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2))
- r(C,G+16,E)
- E=e(m(C,G-12),E)
- r(C,G+20,E)
- E=e(m(C,G-8),E)
- r(C,G+24,E)
- E=e(m(C,G-4),E)
- r(C,G+28,E)
- E=f(E,8)
- end
- E=e(m(C,192),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2),64)
- r(C,224,E)
- E=e(m(C,196),E)
- r(C,228,E)
- E=e(m(C,200),E)
- r(C,232,E)
- r(C,236,e(m(C,204),E))
- elseif B==24 then
- for G=24,168,24 do
- F=F*2%229
- E=e(m(C,G-24),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2),F)
- r(C,G,E)
- E=e(m(C,G-20),E)
- r(C,G+4,E)
- E=e(m(C,G-16),E)
- r(C,G+8,E)
- E=e(m(C,G-12),E)
- r(C,G+12,E)
- E=e(m(C,G-8),E)
- r(C,G+16,E)
- E=e(m(C,G-4),E)
- r(C,G+20,E)
- E=f(E,8)
- end
- E=e(m(C,168),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2),128)
- r(C,192,E)
- E=e(m(C,172),E)
- r(C,196,E)
- E=e(m(C,176),E)
- r(C,200,E)
- r(C,204,e(m(C,180),E))
- else
- for G=16,144,16 do
- F=F*2%229
- E=e(m(C,G-16),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2),F)
- r(C,G,E)
- E=e(m(C,G-12),E)
- r(C,G+4,E)
- E=e(m(C,G-8),E)
- r(C,G+8,E)
- E=e(m(C,G-4),E)
- r(C,G+12,E)
- E=f(E,8)
- end
- E=e(m(C,144),l(u,s(E/65536)*2)*65536+
- l(u,E%65536*2),54)
- r(C,160,E)
- E=e(m(C,148),E)
- r(C,164,E)
- E=e(m(C,152),E)
- r(C,168,E)
- r(C,172,e(m(C,156),E))
- end
- return C
- end
- local function encryptBlock(A:buffer,B:number,C:buffer,D:number,E:buffer,F:number):()
- local G=e(k(C,D),k(A,0))
- local H=e(k(C,D+1),k(A,1))
- local I=e(k(C,D+2),k(A,2))
- local J=e(k(C,D+3),k(A,3))
- local K=e(k(C,D+4),k(A,4))
- local L=e(k(C,D+5),k(A,5))
- local M=e(k(C,D+6),k(A,6))
- local N=e(k(C,D+7),k(A,7))
- local O=e(k(C,D+8),k(A,8))
- local P=e(k(C,D+9),k(A,9))
- local Q=e(k(C,D+10),k(A,10))
- local R=e(k(C,D+11),k(A,11))
- local S=e(k(C,D+12),k(A,12))
- local T=e(k(C,D+13),k(A,13))
- local U=e(k(C,D+14),k(A,14))
- local V=e(k(C,D+15),k(A,15))
- local W=G*256+L;local X=L*256+Q;local Y=Q*256+V;local Z=V*256+G
- local _=K*256+P;local aa=P*256+U;local ab=U*256+J;local ac=J*256+K
- local ad=O*256+T;local ae=T*256+I;local af=I*256+N;local ag=N*256+O
- local ah=S*256+H;local ai=H*256+M;local aj=M*256+R;local ak=R*256+S
- for al=16,B,16 do
- G=e(k(v,W),k(w,Y),k(A,al))
- H=e(k(v,X),k(w,Z),k(A,al+1))
- I=e(k(v,Y),k(w,W),k(A,al+2))
- J=e(k(v,Z),k(w,X),k(A,al+3))
- K=e(k(v,_),k(w,ab),k(A,al+4))
- L=e(k(v,aa),k(w,ac),k(A,al+5))
- M=e(k(v,ab),k(w,_),k(A,al+6))
- N=e(k(v,ac),k(w,aa),k(A,al+7))
- O=e(k(v,ad),k(w,af),k(A,al+8))
- P=e(k(v,ae),k(w,ag),k(A,al+9))
- Q=e(k(v,af),k(w,ad),k(A,al+10))
- R=e(k(v,ag),k(w,ae),k(A,al+11))
- S=e(k(v,ah),k(w,aj),k(A,al+12))
- T=e(k(v,ai),k(w,ak),k(A,al+13))
- U=e(k(v,aj),k(w,ah),k(A,al+14))
- V=e(k(v,ak),k(w,ai),k(A,al+15))
- W,X,Y,Z,_,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak=
- G*256+L,L*256+Q,Q*256+V,V*256+G,K*256+P,P*256+U,U*256+J,J*256+K,
- O*256+T,T*256+I,I*256+N,N*256+O,S*256+H,H*256+M,M*256+R,R*256+S
- end
- r(E,F,e(l(u,e(k(v,ak),k(w,ai),
- k(A,B+31))*512+e(k(v,af),k(w,ad),k(A,B+26))*2)
- *65536+l(u,e(k(v,aa),k(w,ac),k(A,B+21))*512+
- e(k(v,W),k(w,Y),k(A,B+16))*2),m(A,B+32)))
- r(E,F+4,e(l(u,e(k(v,Z),k(w,X),
- k(A,B+19))*512+e(k(v,aj),k(w,ah),k(A,B+30))*2)
- *65536+l(u,e(k(v,ae),k(w,ag),k(A,B+25))*512+
- e(k(v,_),k(w,ab),k(A,B+20))*2),m(A,B+36)))
- r(E,F+8,e(l(u,e(k(v,ac),k(w,aa),
- k(A,B+23))*512+e(k(v,Y),k(w,W),k(A,B+18))*2)
- *65536+l(u,e(k(v,ai),k(w,ak),k(A,B+29))*512+
- e(k(v,ad),k(w,af),k(A,B+24))*2),m(A,B+40)))
- r(E,F+12,e(l(u,e(k(v,ag),k(w,ae),
- k(A,B+27))*512+e(k(v,ab),k(w,_),k(A,B+22))*2)
- *65536+l(u,e(k(v,X),k(w,Z),k(A,B+17))*512+
- e(k(v,ah),k(w,aj),k(A,B+28))*2),m(A,B+44)))
- end
- local function decryptBlock(aa:buffer,ab:number,ac:buffer,ad:number,ae:buffer,af:number):()
- local ag=e(k(x,k(ac,ad)*256+k(aa,ab+32)),k(aa,ab+16))
- local ah=e(k(x,k(ac,ad+13)*256+k(aa,ab+45)),k(aa,ab+17))
- local ai=e(k(x,k(ac,ad+10)*256+k(aa,ab+42)),k(aa,ab+18))
- local aj=e(k(x,k(ac,ad+7)*256+k(aa,ab+39)),k(aa,ab+19))
- local ak=e(k(x,k(ac,ad+4)*256+k(aa,ab+36)),k(aa,ab+20))
- local al=e(k(x,k(ac,ad+1)*256+k(aa,ab+33)),k(aa,ab+21))
- local A=e(k(x,k(ac,ad+14)*256+k(aa,ab+46)),k(aa,ab+22))
- local B=e(k(x,k(ac,ad+11)*256+k(aa,ab+43)),k(aa,ab+23))
- local C=e(k(x,k(ac,ad+8)*256+k(aa,ab+40)),k(aa,ab+24))
- local D=e(k(x,k(ac,ad+5)*256+k(aa,ab+37)),k(aa,ab+25))
- local E=e(k(x,k(ac,ad+2)*256+k(aa,ab+34)),k(aa,ab+26))
- local F=e(k(x,k(ac,ad+15)*256+k(aa,ab+47)),k(aa,ab+27))
- local G=e(k(x,k(ac,ad+12)*256+k(aa,ab+44)),k(aa,ab+28))
- local H=e(k(x,k(ac,ad+9)*256+k(aa,ab+41)),k(aa,ab+29))
- local I=e(k(x,k(ac,ad+6)*256+k(aa,ab+38)),k(aa,ab+30))
- local J=e(k(x,k(ac,ad+3)*256+k(aa,ab+35)),k(aa,ab+31))
- local K=ag*256+ah;local L=ah*256+ai;local M=ai*256+aj;local N=aj*256+ag
- local O=ak*256+al;local P=al*256+A;local Q=A*256+B;local R=B*256+ak
- local S=C*256+D;local T=D*256+E;local U=E*256+F;local V=F*256+C
- local W=G*256+H;local X=H*256+I;local Y=I*256+J;local Z=J*256+G
- for _=ab,16,-16 do
- ag=e(k(x,k(y,K)*256+k(z,M)),k(aa,_))
- ah=e(k(x,k(y,X)*256+k(z,Z)),k(aa,_+1))
- ai=e(k(x,k(y,U)*256+k(z,S)),k(aa,_+2))
- aj=e(k(x,k(y,R)*256+k(z,P)),k(aa,_+3))
- ak=e(k(x,k(y,O)*256+k(z,Q)),k(aa,_+4))
- al=e(k(x,k(y,L)*256+k(z,N)),k(aa,_+5))
- A=e(k(x,k(y,Y)*256+k(z,W)),k(aa,_+6))
- B=e(k(x,k(y,V)*256+k(z,T)),k(aa,_+7))
- C=e(k(x,k(y,S)*256+k(z,U)),k(aa,_+8))
- D=e(k(x,k(y,P)*256+k(z,R)),k(aa,_+9))
- E=e(k(x,k(y,M)*256+k(z,K)),k(aa,_+10))
- F=e(k(x,k(y,Z)*256+k(z,X)),k(aa,_+11))
- G=e(k(x,k(y,W)*256+k(z,Y)),k(aa,_+12))
- H=e(k(x,k(y,T)*256+k(z,V)),k(aa,_+13))
- I=e(k(x,k(y,Q)*256+k(z,O)),k(aa,_+14))
- J=e(k(x,k(y,N)*256+k(z,L)),k(aa,_+15))
- K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z=
- ag*256+ah,ah*256+ai,ai*256+aj,aj*256+ag,ak*256+al,al*256+A,A*256+B,B*256+ak,
- C*256+D,D*256+E,E*256+F,F*256+C,G*256+H,H*256+I,I*256+J,J*256+G
- end
- r(ae,af,e(k(x,k(y,R)*256+k(z,P)),
- k(aa,3))*16777216+e(k(x,k(y,U)*256+k(z,S)),
- k(aa,2))*65536+e(k(x,k(y,X)*256+k(z,Z)),
- k(aa,1))*256+e(k(x,k(y,K)*256+k(z,M)),
- k(aa,0)))
- r(ae,af+4,e(k(x,k(y,V)*256+k(z,T)),
- k(aa,7))*16777216+e(k(x,k(y,Y)*256+k(z,W)),
- k(aa,6))*65536+e(k(x,k(y,L)*256+k(z,N)),
- k(aa,5))*256+e(k(x,k(y,O)*256+k(z,Q)),
- k(aa,4)))
- r(ae,af+8,e(k(x,k(y,Z)*256+k(z,X)),
- k(aa,11))*16777216+e(k(x,k(y,M)*256+k(z,K)),
- k(aa,10))*65536+e(k(x,k(y,P)*256+k(z,R)),
- k(aa,9))*256+e(k(x,k(y,S)*256+k(z,U)),
- k(aa,8)))
- r(ae,af+12,e(k(x,k(y,N)*256+k(z,L)),
- k(aa,15))*16777216+e(k(x,k(y,Q)*256+k(z,O)),
- k(aa,14))*65536+e(k(x,k(y,T)*256+k(z,V)),
- k(aa,13))*256+e(k(x,k(y,W)*256+k(z,Y)),
- k(aa,12)))
- end
- do
- local aa=h(256)
- local ab=h(256)
- local ac=h(256)
- local ad=h(256)
- local ae=h(256)
- local af=1;local ag=1;local ah
- local function gfmul(ai:number,aj:number):number
- local ak=0
- for al=0,7 do
- if aj%2==1 then
- ak=e(ak,ai)
- end
- if ai>=128 then
- ai=e(ai*2%256,27)
- else
- ai=ai*2%256
- end
- aj=s(aj/2)
- end
- return ak
- end
- p(aa,0,99)
- for ai=1,255 do
- af=e(af,af*2,if af<128 then 0 else 27)%256
- ag=e(ag,ag*2)
- ag=e(ag,ag*4)
- ag=e(ag,ag*16)%256
- if ag>=128 then
- ag=e(ag,9)
- end
- ah=e(ag,ag%128*2+ag/128,ag%64*4+ag/64,ag%32*8+ag/32,ag%16*16+ag/16,99)
- p(aa,af,ah)
- p(ab,ah,af)
- p(ac,af,gfmul(3,af))
- p(ad,af,gfmul(9,af))
- p(ae,af,gfmul(11,af))
- end
- local ai,aj,ak,al;ah=0
- for A=0,255 do
- af=k(aa,A);ai=af*256
- aj,al,ak=gfmul(2,af),gfmul(13,A),gfmul(14,A)
- for B=0,255 do
- ag=k(aa,B)
- q(u,ah*2,ai+ag)
- p(x,ah,k(ab,e(A,B)))
- p(v,ah,e(aj,k(ac,ag)))
- p(w,ah,e(af,ag))
- p(y,ah,e(ak,k(ae,B)))
- p(z,ah,e(al,k(ad,B)))
- ah+=1
- end
- end
- end
- export type AesCipher=typeof(setmetatable({}::{
- Key:string,Length:number,Mode:{FwdMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
- (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{any},...any)->(),
- InvMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
- (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{any},...any)->()},
- Padding:{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:boolean?},RoundKeys:string,
- Encrypt:(self:AesCipher,plaintext:buffer|string,output:buffer?,...any)->buffer,
- Decrypt:(self:AesCipher,ciphertext:buffer|string,output:buffer?,...any)->buffer,
- EncryptBlock:(self:AesCipher,plaintext:buffer,offset:number,output:buffer?,offsetOut:number?)->(),
- DecryptBlock:(self:AesCipher,ciphertext:buffer,offset:number,output:buffer?,offsetOut:number?)->(),
- Destroy:(self:AesCipher)->()
- },{}))
- local function newidx(aa,ab)
- return a(`{ab} cannot be assigned to`)
- end
- local function tostr()
- return"AesCipher"
- end
- local aa=require(loadstring[[local a=assert
- local b=bit32.bxor
- local c=buffer.copy
- local d=buffer.create
- local e=buffer.len
- local f=buffer.readu8
- local g=buffer.readu32 local h=
- buffer.tostring
- local i=buffer.writestring
- local j=buffer.writeu8
- local k=buffer.writeu32 local l=
- math.floor
- local m=math.random
- local n=table.freeze
- local o=string.pack
- local p=d(16)
- local function addByteCtr(q:buffer,r:number,s:number,t:number,u:boolean):()
- local v
- if u then
- v=f(q,s)+r
- j(q,s,v)
- if v>=256 then
- for w=s+1,t do
- v=f(q,w)+1
- j(q,w,v)
- if v<256 then
- break
- end
- end
- end
- else
- v=f(q,t)+r
- j(q,t,v)
- if v>=256 then
- for w=t-1,s,-1 do
- v=f(q,w)+1
- j(q,w,v)
- if v<256 then
- break
- end
- end
- end
- end
- end
- local q={}
- q.ECB=n{
- FwdMode=function(r,s,t,u)
- local v=e(t)-16
- a(v%16==0,"Input length must be a multiple of 16 bytes")
- for w=0,v,16 do
- r(t,w,u,w)
- end
- end,
- InvMode=function(r,s,t,u)
- local v=e(t)-16
- a(v%16==0,"Input length must be a multiple of 16 bytes")
- for w=0,v,16 do
- s(t,w,u,w)
- end
- end
- }
- q.CBC=n{
- FwdMode=function(r,s,t,u,v,w)
- local x=e(t)-16
- a(x%16==0,"Input length must be a multiple of 16 bytes")
- w=w or p
- a(e(w)==16,"Initialization vector must be 16 bytes long")
- k(u,0,b(g(t,0),g(w,0)))
- k(u,4,b(g(t,4),g(w,4)))
- k(u,8,b(g(t,8),g(w,8)))
- k(u,12,b(g(t,12),g(w,12)))
- r(u,0,u,0)
- for y=16,x,16 do
- k(u,y,b(g(t,y),g(u,y-16)))
- k(u,y+4,b(g(t,y+4),g(u,y-12)))
- k(u,y+8,b(g(t,y+8),g(u,y-8)))
- k(u,y+12,b(g(t,y+12),g(u,y-4)))
- r(u,y,u,y)
- end
- end,
- InvMode=function(r,t,u,v,w,x)
- local y=e(u)-16
- a(y%16==0,"Input length must be a multiple of 16 bytes")
- x=x or p
- a(e(x)==16,"Initialization vector must be 16 bytes long")
- local z=g(u,0);local A=g(u,4)
- local B=g(u,8);local C=g(u,12)
- local D,E,F,G
- t(u,0,v,0)
- k(v,0,b(g(v,0),g(x,0)))
- k(v,4,b(g(v,4),g(x,4)))
- k(v,8,b(g(v,8),g(x,8)))
- k(v,12,b(g(v,12),g(x,12)))
- for H=16,y,16 do
- D=g(u,H)
- E=g(u,H+4)
- F=g(u,H+8)
- G=g(u,H+12)
- t(u,H,v,H)
- k(v,H,b(g(v,H),z))
- k(v,H+4,b(g(v,H+4),A))
- k(v,H+8,b(g(v,H+8),B))
- k(v,H+12,b(g(v,H+12),C))
- z,A,B,C=D,E,F,G
- end
- end
- }
- q.PCBC=n{
- FwdMode=function(t,u,v,w,x,y)
- local z=e(v)-16
- a(z%16==0,"Input length must be a multiple of 16 bytes")
- y=y or p
- a(e(y)==16,"Initialization vector must be 16 bytes long")
- local A=g(v,0);local B=g(v,4)
- local C=g(v,8);local D=g(v,12)
- local E,F,G,H
- k(w,0,b(A,g(y,0)))
- k(w,4,b(B,g(y,4)))
- k(w,8,b(C,g(y,8)))
- k(w,12,b(D,g(y,12)))
- t(w,0,w,0)
- for I=16,z,16 do
- E=g(v,I)
- F=g(v,I+4)
- G=g(v,I+8)
- H=g(v,I+12)
- k(w,I,b(A,E,g(w,I-16)))
- k(w,I+4,b(B,F,g(w,I-12)))
- k(w,I+8,b(C,G,g(w,I-8)))
- k(w,I+12,b(D,H,g(w,I-4)))
- t(w,I,w,I)
- A,B,C,D=E,F,G,H
- end
- end,
- InvMode=function(t,v,w,x,y,z)
- local A=e(w)-16
- a(A%16==0,"Input length must be a multiple of 16 bytes")
- z=z or p
- a(e(z)==16,"Initialization vector must be 16 bytes long")
- local B=g(w,0);local C=g(w,4)
- local D=g(w,8);local E=g(w,12)
- v(w,0,x,0)
- local F=b(g(x,0),g(z,0))
- local G=b(g(x,4),g(z,4))
- local H=b(g(x,8),g(z,8))
- local I=b(g(x,12),g(z,12))
- local J,K,L,M
- k(x,0,F)
- k(x,4,G)
- k(x,8,H)
- k(x,12,I)
- local N,O,P,Q=0,4,8,12
- for R=16,A,16 do
- N+=16;O+=16;P+=16;Q+=16
- J=g(w,N)
- K=g(w,O)
- L=g(w,P)
- M=g(w,Q)
- v(w,R,x,R)
- F=b(B,F,g(x,N))
- G=b(C,G,g(x,O))
- H=b(D,H,g(x,P))
- I=b(E,I,g(x,Q))
- B,C,D,E=J,K,L,M
- k(x,N,F)
- k(x,O,G)
- k(x,P,H)
- k(x,Q,I)
- end
- end
- }
- local function cfbFwd(v,w,x,y,z:{CommonTemp:buffer,SegmentSize:number},A)
- local B=z.SegmentSize
- local C=e(x)
- a(C%B==0,"Input length must be a multiple of segment size")
- A=A or p
- a(e(A)==16,"Initialization vector must be 16 bytes long")
- local D=z.CommonTemp or d(31)
- if C==B then
- v(A,0,D,0)
- for E=0,B-1 do
- j(y,E,b(f(x,E),f(D,E)))
- end
- else
- local E=C-B;local F=16-B
- local G
- v(A,0,D,0)
- for H=0,B-1 do
- j(y,H,b(f(x,H),f(D,H)))
- end
- c(D,0,A,B,F)
- c(D,F,y,0,B)
- for H=B,E-B,B do
- G=0
- c(D,16,D,B,F)
- v(D,0,D,0)
- for I=H,H+B-1 do
- j(y,I,b(f(x,I),f(D,G)))
- G+=1
- end
- c(D,0,D,16,F)
- c(D,F,y,H,B)
- end
- v(D,0,D,0)
- G=0
- for H=E,C-1 do
- j(y,H,b(f(x,H),f(D,G)))
- G+=1
- end
- end
- end
- local function cfbInv(v,w,x,y,z:{CommonTemp:buffer,SegmentSize:number},A)
- local B=e(x)
- local C=z.SegmentSize
- a(B%C==0,"Input length must be a multiple of segment size")
- A=A or p
- a(e(A)==16,"Initialization vector must be 16 bytes long")
- local D=z.CommonTemp or d(31)
- if B==C then
- v(A,0,D,0)
- for E=0,C-1 do
- j(y,E,b(f(x,E),f(D,E)))
- end
- else
- local E=B-C;local F=16-C
- local G
- v(A,0,D,0)
- for H=0,C-1 do
- j(y,H,b(f(x,H),f(D,H)))
- end
- c(D,0,A,C,F)
- c(D,F,x,0,C)
- for H=C,E-C,C do
- G=0
- c(D,16,D,C,F)
- v(D,0,D,0)
- for I=H,H+C-1 do
- j(y,I,b(f(x,I),f(D,G)))
- G+=1
- end
- c(D,0,D,16,F)
- c(D,F,x,H,C)
- end
- v(D,0,D,0)
- G=0
- for H=E,B-1 do
- j(y,H,b(f(x,H),f(D,G)))
- G+=1
- end
- end
- end
- local function ofbMode(v,w,x:buffer,y:buffer,z,A:buffer)
- local B=e(x)-16
- a(B%16==0,"Input length must be a multiple of 16 bytes")
- A=A or p
- a(e(A)==16,"Initialization vector must be 16 bytes long")
- local C=g(x,0);local D=g(x,4)
- local E=g(x,8);local F=g(x,12)
- v(A,0,y,0)
- C=b(C,g(y,0))
- D=b(D,g(y,4))
- E=b(E,g(y,8))
- F=b(F,g(y,12))
- local G,H,I,J
- for K=16,B,16 do
- G=g(x,K)
- H=g(x,K+4)
- I=g(x,K+8)
- J=g(x,K+12)
- v(y,K-16,y,K)
- k(y,K-16,C)
- k(y,K-12,D)
- k(y,K-8,E)
- k(y,K-4,F)
- C=b(G,g(y,K))
- D=b(H,g(y,K+4))
- E=b(I,g(y,K+8))
- F=b(J,g(y,K+12))
- end
- k(y,B,C)
- k(y,B+4,D)
- k(y,B+8,E)
- k(y,B+12,F)
- end
- q.OFB=n{
- FwdMode=ofbMode,InvMode=ofbMode
- }
- local function ctrMode(v,x,y,z,A:{CommonTemp:buffer,InitValue:string,Prefix:string,Suffix:string,
- Step:number,LittleEndian:boolean})
- local B=e(y)-16
- a(B%16==0,"Input length must be a multiple of 16 bytes")
- local C=A.CommonTemp
- local D=A.InitValue;local E=A.Prefix
- local F=A.Suffix;local G=A.Step;local H=A.LittleEndian
- local I=#E;local J=I+#D-1
- i(C,0,E)
- i(C,I,D)
- i(C,J+1,F)
- local K=g(y,0);local L=g(y,4)
- local M=g(y,8);local N=g(y,12)
- v(C,0,z,0)
- k(z,0,b(g(z,0),K))
- k(z,4,b(g(z,4),L))
- k(z,8,b(g(z,8),M))
- k(z,12,b(g(z,12),N))
- for O=16,B,16 do
- K=g(y,O)
- L=g(y,O+4)
- M=g(y,O+8)
- N=g(y,O+12)
- addByteCtr(C,G,I,J,H)
- v(C,0,z,O)
- k(z,O,b(K,g(z,O)))
- k(z,O+4,b(L,g(z,O+4)))
- k(z,O+8,b(M,g(z,O+8)))
- k(z,O+12,b(N,g(z,O+12)))
- end
- end
- local v={
- __index=function(v,x:"CFB"|"CTR"):{}?
- return if x=="CFB"then{
- FwdMode=cfbFwd,InvMode=cfbInv,
- SegmentSize=16,
- CommonTemp=d(31)
- }elseif x=="CTR"then{
- FwdMode=ctrMode,InvMode=ctrMode,
- InitValue=o("I2I2I2I2I2I2I2I2",m(0,65535),m(0,65535),m(0,65535),
- m(0,65535),m(0,65535),m(0,65535),m(0,65535),m(0,65535)),
- Prefix="",Suffix="",Step=1,LittleEndian=false,
- CommonTemp=d(16)
- }else nil
- end,
- __newindex=function()
- end
- }
- setmetatable(q,v)
- q.CFB={}::{FwdMode:typeof(cfbFwd),InvMode:typeof(cfbInv),CommonTemp:buffer,SegmentSize:number}
- q.CTR={}::{FwdMode:typeof(ctrMode),InvMode:typeof(ctrMode),CommonTemp:buffer,InitValue:string,Prefix:string,Suffix:string,
- Step:number,LittleEndian:boolean}
- n(q)
- v.__metatable="This metatable is locked"
- return q]]())
- local ab=require(loadstring[[local a=assert
- local b=error
- local c=buffer.copy
- local d=buffer.create
- local e=buffer.fill
- local f=buffer.len
- local g=buffer.readu8
- local h=buffer.writeu8
- local i=math.random
- local j=table.freeze
- local k={}
- local function nonPad(l)
- return l
- end
- k.None=j{
- Pad=nonPad,Unpad=nonPad,
- Overwrite=false
- }
- local function anxPad(l,m,n)
- local o=f(l)
- local p=o-o%n
- if m then
- a(f(m)>=o+n,"Output buffer out of bounds")
- local q=n-o%n
- c(m,0,l,0,o)
- e(m,o,0,q-1)
- h(m,p+n-1,q)
- else
- p+=n
- m=d(p)
- c(m,0,l,0,o)
- h(m,p-1,n-o%n)
- end
- return m
- end
- local function i10Pad(l,m,n)
- local o=f(l)
- local p=o-o%n
- if m then
- a(f(m)>=o+n,"Output buffer out of bounds")
- else
- m=d(p+n)
- end
- c(m,0,l,0,o)
- for q=o,p+n-2 do
- h(m,q,i(0,255))
- end
- h(m,p+n-1,n-o%n)
- return m
- end
- local function i10Unpad(l,m,n)
- local o=f(l)
- local p=g(l,o-1)
- local q=o-p
- a(0<p and p<=n,"Got unexpected padding")
- if m then
- a(f(m)>=q,"Output buffer out of bounds")
- else
- m=d(q)
- end
- c(m,0,l,0,q)
- return m
- end
- local function pksPad(l,m,n)
- local o=f(l)
- local p=o-o%n
- if m then
- a(f(m)>=o+n,"Output buffer out of bounds")
- else
- m=d(p+n)
- end
- local q=n-o%n
- c(m,0,l,0,o)
- e(m,o,q,q)
- return m
- end
- local function pksUnpad(l,m,n)
- local o=f(l)
- local p=g(l,o-1)
- local q=o-p
- a(0<p and p<=n,"Got unexpected padding")
- for r=q,o-2 do
- if g(l,r)~=p then
- b"Got unexpected padding"
- end
- end
- if m then
- a(f(m)>=q,"Output buffer out of bounds")
- else
- m=d(q)
- end
- c(m,0,l,0,q)
- return m
- end
- local function ii7Pad(l,m,n)
- local o=f(l)
- if m then
- a(f(m)>=o+n,"Output buffer out of bounds")
- e(m,o+1,0,n-o%n-1)
- else
- m=d(o+n-o%n)
- end
- c(m,0,l,0,o)
- h(m,o,128)
- return m
- end
- local function ii7Unpad(l,m,n)
- local o=f(l)-1
- local p
- for q=o,o-n,-1 do
- p=g(l,q)
- if p==128 then
- if m then
- a(f(m)>=q,"Output buffer out of bounds")
- else
- m=d(q)
- end
- c(m,0,l,0,q)
- return m
- else
- a(p==0,"Got unexpected padding")
- end
- end
- b"Got unexpected padding"
- return d(0)
- end
- local function zroPad(l,m,n)
- local o=f(l)
- if m then
- a(f(m)>=o+n,"Output buffer out of bounds")
- e(m,o,0,n-o%n)
- else
- m=d(o+n-o%n)
- end
- c(m,0,l,0,o)
- return m
- end
- local function zroUnpad(l,m,n)
- local o=f(l)-1
- local p
- for q=o,o-n,-1 do
- p=g(l,q)
- if p==0 then
- q+=1
- if m then
- a(f(m)>=q,"Output buffer out of bounds")
- else
- m=d(q)
- end
- c(m,0,l,0,q)
- return m
- end
- end
- c(m,0,l,0,o-n-1)
- return m
- end
- local l={
- __index=function(l,m:"AnsiX923"|"Iso10126"|"Pkcs7"|"Iso7816_4"|"Zero"):{}?
- return if m=="AnsiX923"then{
- Pad=anxPad,Unpad=anxPad,
- Overwrite=nil
- }elseif m=="Iso10126"then{
- Pad=i10Pad,Unpad=i10Unpad,
- Overwrite=nil
- }elseif m=="Pkcs7"then{
- Pad=pksPad,Unpad=pksUnpad,
- Overwrite=nil
- }elseif m=="Iso7816_4"then{
- Pad=ii7Pad,Unpad=ii7Unpad,
- Overwrite=nil
- }elseif m=="Zero"then{
- Pad=zroPad,Unpad=zroUnpad,
- Overwrite=nil
- }else nil
- end,
- __newindex=function()end
- }
- setmetatable(k,l)
- k.AnsiX923={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
- k.Iso10126={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
- k.Pkcs7={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
- k.Iso7816_4={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
- k.Zero={}::{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:nil|boolean}
- j(k)
- l.__metatable="This metatable is locked"
- return k
- ]]())
- local function expandKey(ac:buffer|string,ad:buffer?):buffer
- local ae=d(ac)=="buffer"
- local af=if ae then j(ac::buffer)else#(ac::string)
- 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"
- return keySchedule(ac,af,ad or h(ag::number),ae)
- end
- local function fromKey(ac:buffer,ad:{FwdMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
- (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{[string]:any},...any)->(),
- InvMode:((input:buffer,offset:number,output:buffer,offsetOut:number)->(),
- (input:buffer,offset:number,output:buffer,offsetOut:number)->(),buffer,buffer,{[string]:any},...any)->()}?,
- ae:{Pad:(buffer,buffer?,number)->buffer,Unpad:(buffer,buffer?,number)->buffer,Overwrite:boolean?}?):AesCipher
- local af:number?=j(ac)
- local ag:number?;local ah:string?
- local ai:string?=n(ac)
- if af==240 then
- ag=192
- ah=t(ai::string,1,32)
- elseif af==208 then
- ag=160
- ah=t(ai::string,1,24)
- elseif af==176 then
- ag=128
- ah=t(ai::string,1,16)
- else
- a"Round keys must be either 240, 208 or 128 bytes long"
- end
- local aj:buffer?=ac
- local ak:any=ad or aa.ECB
- local al=ak.FwdMode;local A=ak.InvMode;local B=ak.SegmentSize or 16
- local C:any=ae or ab.Pkcs7
- local D=C.Pad;local E=C.Unpad
- local F=b(true)::AesCipher
- local G=c(F)
- local function encp(H,I,J,K)
- encryptBlock(aj::buffer,ag::number,H,I,J,K)
- end
- local function decp(H,I,J,K)
- decryptBlock(aj::buffer,ag::number,H,I,J,K)
- end
- local function enc(H:AesCipher,I,J,...)
- local K=d(I)
- local L=if K=="buffer"then I::buffer elseif K=="string"then i(I)
- else a(`Unable to cast {K} to buffer`)
- J=d(J)=="buffer"and J
- if H~=F then
- return H:Encrypt(L,J::buffer,...)
- elseif ag then
- local M=D(L,J,B)
- al(encp,decp,if C.Overwrite==false then L else M,M,ak,...)
- return M
- else
- a"AesCipher object's already destroyed"
- return h(0)
- end
- end
- local function encb(H:AesCipher,I,J,K,L)
- if H~=F then
- H:EncryptBlock(I,J,K,L)
- elseif ag then
- encryptBlock(aj::buffer,ag::number,I,J,K or I,L or J)
- else
- a"AesCipher object's already destroyed"
- end
- end
- local function dec(H:AesCipher,I,J,...)
- local K=d(I)
- local L=if K=="buffer"then I::buffer elseif K=="string"then i(I)
- else a(`Unable to cast {K} to buffer`)
- J=d(J)=="buffer"and J
- if H~=F then
- return H:Decrypt(L,J::buffer,...)
- elseif ag then
- local M=C.Overwrite
- local N=if M==nil then h(j(L))elseif M then L else J or h(j(L))
- A(encp,decp,L,N,ak,...)
- return E(N,J,B)
- else
- a"AesCipher object's already destroyed"
- return h(0)
- end
- end
- local function decb(H:AesCipher,I,J,K,L)
- if H~=F then
- H:DecryptBlock(I,J,K,L)
- elseif ag then
- decryptBlock(aj::buffer,ag::number,I,J,K or I,L or J)
- else
- a"AesCipher object's already destroyed"
- end
- end
- local function destroy(H:AesCipher):()
- if H~=F then
- H:Destroy()
- elseif ag then
- ai,aj,ag,al,A,ak,C,ah,af=nil,nil,nil,nil,nil,nil,nil,nil,nil
- else
- a"AesCipher object's already destroyed"
- end
- end
- G.__index=function(H,I)
- return if I=="Encrypt"then enc elseif I=="Decrypt"then dec elseif I=="EncryptBlock"then encb elseif I=="DecryptBlock"then decb
- elseif I=="Destroy"then destroy elseif ag then(if I=="Key"then ah elseif I=="RoundKeys"then ai
- elseif I=="Mode"then ak elseif I=="Padding"then C elseif I=="Length"then af
- else a(`{I} is not a valid member of AesCipher`))else a"AesCipher object's already destroyed"
- end
- G.__newindex=newidx
- G.__tostring=tostr
- G.__len=function():number
- return af or a"AesCipher object's destroyed"
- end
- G.__metatable="AesCipher object: Metatable's locked"
- return F
- end
- return table.freeze{
- new=function(ac,ad,ae)
- return fromKey(expandKey(ac),ad,ae)
- end,
- expandKey=expandKey,
- fromKey=fromKey,
- modes=aa,
- pads=ab
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement