Skip to content

Instantly share code, notes, and snippets.

@SquidDev

SquidDev/smt.lua Secret

Last active Aug 22, 2020
Embed
What would you like to do?
smt.lua
local e=
type(package)=="table"and type(package.preload)=="table"and package.preload or{}local t=require
if type(t)~="function"then local a={}local o={}
t=function(i)local n=o[i]
if n~=nil then if n==a then
error(
"loop or previous error loading module '"..i.."'",2)end;return n end;o[i]=a;local s=e[i]if s then n=s(i)else
error("cannot load '"..i.."'",2)end;if n==nil then n=true end;o[i]=n;return n end end
e["smt.util.pem"]=function(...)local a=t("smt.util.base64")
local o=t("smt.crypto.rlwe")local i="%-%-%-%-%-BEGIN ([%s%a]+)%-%-%-%-%-"
local n="%-%-%-%-%-END ([%s%a]+)%-%-%-%-%-"local s="%-%-%-%-%-BEGIN [%s%a]+%-%-%-%-%-(.+)%-%-%-%-%-END [%s%a]+%-%-%-%-%-"
local function h(d,l)
assert(
type(d)=="table","An unencoded polynomial (mu) must be passed to convertToPEM!")
l=type(l)=="string"and l:upper()or"RLWE PUBLIC KEY"local u="-----BEGIN "..l.."-----"
local c="-----END "..l.."-----"return u.."\n"..a.encode(d).."\n"..c end
local function r(d)
assert(type(d)=="string","PEM must be a string!")d=d:gsub("\n","")
assert(d:match(i)~=nil,"PEM header not found!")
assert(d:match(n)~=nil,"PEM footer not found!")return a.decode(d:match(s))end;return{convertToPEM=h,convertFromPEM=r}end
e["smt.util.file"]=function(...)local function a(i,n)local s=fs.open(i,n or"r")local h=s.readAll()
s.close()return h end;local function o(i,n,s)
local h=fs.open(i,s or"w")h.write(n)h.close()end
return{read=a,write=o}end
e["smt.util.base64"]=function(...)
local a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="local o,i={},{}
local n=bit32 and bit32.lshift or bit.blshift;local s=bit32 and bit32.rshift or bit.brshift;local h=bit32 and
bit32.band or bit.band;local r=bit32 and bit32.bor or
bit.bor
for u=1,#a do local c=a:sub(u,u)o[u-1]=c;i[c]=u-1 end
local function d(u)
local u=type(u)=="table"and u or{tostring(u):byte(1,-1)}local c={}local m
for f=1,#u,3 do m=s(h(u[f],0xFC),2)c[#c+1]=o[m]
m=n(h(u[f],0x03),4)
if f+0 <#u then m=r(m,s(h(u[f+1],0xF0),4))
c[#c+1]=o[m]m=n(h(u[f+1],0x0F),2)if f+1 <#u then
m=r(m,s(h(u[f+2],0xC0),6))c[#c+1]=o[m]m=h(u[f+2],0x3F)c[#c+1]=o[m]else
c[#c+1]=o[m].."="end else
c[#c+1]=o[m].."=="end end;return table.concat(c)end
local function l(u)
if#u%4 ~=0 then error("Invalid base64 data",2)end;local c={}local m={}for f in u:gmatch(".")do m[#m+1]=f end
for f=1,#m,4 do local w={i[m[f]],i[m[f+1]],i[m[
f+2]],i[m[f+3]]}c[#c+1]=
r(n(w[1],2),s(w[2],4))%256
if w[3]<64 then c[#c+1]=
r(n(w[2],4),s(w[3],2))%256;if w[4]<64 then
c[#c+1]=r(n(w[3],6),w[4])%256 end end end;return c end;return{encode=d,decode=l}end;e["smt.smt"]=function(...)end
e["smt.main.transit"]=function(...)
local a=t("smt.main.exchange")local o=t("smt.util.file")local i=t("smt.crypto.chacha")
local n=t("smt.crypto.sha")local s=t("smt.crypto.rlwe")local h=false;local r={}local d={}
local l=
fs.exists("/.rlwe_pkmap")and textutils.unserialize(o.read("/.rlwe_pkmap"))or{}local u={}local c=peripheral.find("modem")if not c then
error("smt.main.transit requires a modem! Use smt.main.exchange for your own non-modem implementation!",0)end
local function m(q)c.open(q)end;local function f(q)c.close(q)end;local function w(q)h=q end;local function y()
o.write("/.rlwe_pkmap",textutils.serialize(l))end
local function p(q)if h then
term.blit("[DEBUG] "..q,"88888888"..
string.rep("7",#q),"ffffffff"..string.rep("f",#q))print()end end;local function v(q)return string.char(unpack(q))end
local function b()
while true do
local q,j,x,z,_,E=os.pullEvent("modem_message")
if
type(_)=="table"and _.type and _.type:sub(1,4)=="RLWE"then
if
(_.type=="RLWERequestPK"and type(_.uuid)=="string"and
_.target==a.uuid)then p("Sent PK to ".._.uuid)
c.transmit(z,x,{type="RLWEHeresYourPK",PK=a.staticPub,target=_.uuid,uuid=a.uuid})elseif
(
_.type=="RLWEHeresYourPK"and type(_.target)=="string"and _.target==a.uuid and
type(_.PK)=="table"and
type(_.uuid)=="string"and
type(l[_.uuid])~="table")then l[_.uuid]=_.PK
p("Recieved PubKey from ".._.uuid)os.queueEvent("RLWE-ReceivePubKey",_.uuid)y()elseif
(
_.type==
"RLWEStage2"and type(_.rid)=="number"and not
a.isActualRID(_.rid)and type(_.C1)=="table"and type(_.UPK)=="table"and _.target==a.uuid)then
p("Stage 2 Server - "..a.uuid.." | "..tostring(_.rid))local T,A,O,I=a.HandshakeStage2(_.C1,_.UPK,_.rid)
local N=a.getCIDChannel(I)m(N)
p("Agreed on channel "..tostring(N))
c.transmit(z,x,{type="RLWEStage3",C2=T,MAC=tostring(A),target=O,uuid=a.uuid})elseif
(
_.type=="RLWEStage3"and type(_.target)=="number"and
a.isActualRID(_.target)and type(_.C2)=="table"and type(_.MAC)=="string"and type(_.uuid)=="string"and d[_.target]==_.uuid)then
p("Stage 2 Client - ".._.target.." | "..tostring(_.uuid))local T=a.HandshakeStage3(_.C2,_.MAC,_.target)
local A=a.getCIDChannel(T)m(A)
p("Agreed on channel "..tostring(A))c.transmit(A,A,{type="RLWEFinished",cid=T})
r[T]=true;os.queueEvent("RLWE-Finish",T)u[T]=0
p("Finished - "..T)elseif
(_.type=="RLWEFinished"and type(_.cid)=="string"and
a.isActualCID(_.cid)and not r[_.cid])then os.queueEvent("RLWE-Finish",_.cid)
p("Finished - ".._.cid)r[_.cid]=true;u[_.cid]=0 elseif
(
_.type=="RLWEData"and type(_.cid)=="string"and a.isActualCID(_.cid)and r[_.cid]and type(_.hmac)=="string"and type(_.ctx)=="string"and type(_.nonce)=="table")then local T=a.getCIDKeys(_.cid)
local A=tostring(n.hmac(tostring(_.ctx)..v(_.nonce)..
tostring(_.cid)..tostring(u[_.cid]),T.hmac))p("HMAC - "..A.." == ".._.hmac)
if A==
_.hmac then u[_.cid]=u[_.cid]+1
local O=i.crypt(_.ctx,T.enc,_.nonce)local I=O[#O]for q=1,I do
if table.remove(O)~=I then error("Padding error!")end end;local N=tostring(O)
os.queueEvent("RLWE-Receive",_.cid,N)p("Decrypted: "..N)end end end end end
local function g(q,j)
assert(a.isActualCID(q),"We do not have a connection with the CID '"..tostring(q).."'.")
assert(type(j)=="table"or type(j)=="string",
"Expected 'table' or 'string' for argument #2, got "..type(j))
local j=type(j)=="string"and{j:byte(1,-1)}or j;local x=s.randomBytes(nil,12)local z=a.getCIDKeys(q)
local _=math.random(1,64)for A=1,_ do table.insert(j,_)end;local E=i.crypt(j,z.enc,x)
local T=a.getCIDChannel(q)
c.transmit(T,T,{type="RLWEData",cid=q,nonce=x,ctx=tostring(E),hmac=tostring(n.hmac(tostring(E)..v(x)..
tostring(q)..tostring(u[q]),z.hmac))})u[q]=u[q]+1 end
local function k(q,j)m(j)local x=l[q]
if not x then
c.transmit(j,j,{type="RLWERequestPK",target=q,uuid=a.uuid})os.pullEvent("RLWE-ReceivePubKey")x=l[q]end;local z,_,E=a.HandshakeStage1(x)d[E]=q
c.transmit(j,j,{rid=E,target=q,UPK=_,C1=z,type="RLWEStage2"})end
return{listener=b,openTunnel=k,openChannel=m,closeChannel=f,setDebug=w,sendData=g,uuid=a.uuid}end
e["smt.main.exchange"]=function(...)local a=t("smt.crypto.rlwe")
local o=t("smt.crypto.sha")local i=t("smt.util.file")local n=t("smt.util.pem")local s,h;local r
if
not
fs.exists("/.rlwe_pub.pem")or not fs.exists("/.rlwe_priv.pem")then local q,j=a.keyPair()
i.write("/.rlwe_pub.pem",n.convertToPEM(j,"RLWE PUBLIC KEY"))
i.write("/.rlwe_priv.pem",n.convertToPEM(q,"RLWE PRIVATE KEY"))s,h=j,q else
local q,j=i.read("/.rlwe_pub.pem"),i.read("/.rlwe_priv.pem")s,h=n.convertFromPEM(q),n.convertFromPEM(j)end
if not fs.exists("/.rlwe_uuid")then
r=o.hmac(os.getComputerID(),h):toHex():sub(1,10)i.write("/.rlwe_uuid",r)else r=i.read("/.rlwe_uuid")end;local d={}local l={}local u={}local c={}local m={}local f={}
local function w(q)local j,x=a.encapsulate(q)
local z,_=a.keyPair()local E=math.random(1000000,9999999)d[E]={pub=_,priv=z}
u[E]=true;l[E]=j;return x,_,E end
local function y(q,j,x)local z=a.decapsulate(h,s,q)l[x]=z;local _,E=a.encapsulate(j)
local T=o.hmac("1cxid",_):toHex()c[T]=true
m[T]={enc=o.hmac("2enc",_),hmac=o.hmac("3hmac",_),chan=o.hmac("4chan",_)}local A=m[T].chan
f[T]=bit.bor(bit.blshift(A[1],8),A[2])return E,o.hmac(E,z),x,T end
local function p(q,j,x)u[x]=false
if tostring(j)==tostring(o.hmac(q,l[x]))then
local z=a.decapsulate(d[x].priv,d[x].pub,q)local _=o.hmac("1cxid",z):toHex()c[_]=true
m[_]={enc=o.hmac("2enc",z),hmac=o.hmac("3hmac",z),chan=o.hmac("4chan",z)}local E=m[_].chan
f[_]=bit.bor(bit.blshift(E[1],8),E[2])return _ else
error("Handshake Stage 3 failure: HMAC failed, someone is likely trying to pretend to be the server!",0)end end;local function v(q)return m[q]end;local function b(q)return f[q]end
local function g(q)return c[q]==true end;local function k(q)return u[q]==true end
return
{HandshakeStage1=w,HandshakeStage2=y,HandshakeStage3=p,getCIDKeys=v,getCIDChannel=b,isActualCID=g,isActualRID=k,uuid=r,staticPriv=h,staticPub=s}end
e["smt.crypto.sha"]=function(...)local a=2^32
local o=bit32 and bit32.band or bit.band;local i=bit32 and bit32.bnot or bit.bnot;local n=
bit32 and bit32.bxor or bit.bxor
local s=bit32 and bit32.lshift or bit.blshift;local h=unpack
local function r(k,q)local j=k/ (2^q)local x=j%1;return(j-x)+x*a end;local function d(k,q)local j=k/ (2^q)return j-j%1 end
local l={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}
local u={0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2}
local function c(k)local q,j=0,0;if 0xFFFFFFFF-q<k then j=j+1
q=k- (0xFFFFFFFF-q)-1 else q=q+k end;return j,q end
local function m(k,q)return
s((k[q]or 0),24)+s((k[q+1]or 0),16)+
s((k[q+2]or 0),8)+ (k[q+3]or 0)end
local function f(k)local q=#k;local j={}k[#k+1]=0x80;while#k%64 ~=56 do k[#k+1]=0 end;local x=math.ceil(#
k/64)
for z=1,x do j[z]={}for _=1,16 do j[z][_]=m(k,1+ ((z-1)*64)+
((_-1)*4))end end;j[x][15],j[x][16]=c(q*8)return j end
local function w(k,q)
for I=17,64 do local N=k[I-15]
local S=n(n(r(k[I-15],7),r(k[I-15],18)),d(k[I-15],3))
local H=n(n(r(k[I-2],17),r(k[I-2],19)),d(k[I-2],10))k[I]=(k[I-16]+S+k[I-7]+H)%a end;local j,x,z,_,E,T,A,O=h(q)
for I=1,64 do local N=n(n(r(E,6),r(E,11)),r(E,25))
local S=n(o(E,T),o(i(E),A))local H=(O+N+S+u[I]+k[I])%a
local R=n(n(r(j,2),r(j,13)),r(j,22))local D=n(n(o(j,x),o(j,z)),o(x,z))local L=(R+D)%a;O,A,T,E,_,z,x,j=A,T,E,(_+
H)%a,z,x,j,(H+L)%a end;q[1]=(q[1]+j)%a;q[2]=(q[2]+x)%a
q[3]=(q[3]+z)%a;q[4]=(q[4]+_)%a;q[5]=(q[5]+E)%a
q[6]=(q[6]+T)%a;q[7]=(q[7]+A)%a;q[8]=(q[8]+O)%a;return q end
local y={__tostring=function(k)return string.char(unpack(k))end,__index={toHex=function(k,q)return("%02x"):rep(
#k):format(unpack(k))end,isEqual=function(k,q)if
type(q)~="table"then return false end;if#k~=#q then return false end;local j=0;for x=1,
#k do j=bit32.bor(j,n(k[x],q[x]))end
return j==0 end}}
local function p(k,q)local j={}for x=1,q do j[(x-1)*4+1]=o(d(k[x],24),0xFF)
j[(x-1)*4+2]=o(d(k[x],16),0xFF)j[(x-1)*4+3]=o(d(k[x],8),0xFF)
j[(x-1)*4+4]=o(k[x],0xFF)end;return
setmetatable(j,y)end
local function v(k)k=k or""
k=type(k)=="string"and{k:byte(1,-1)}or k;k=f(k)local q={h(l)}for j=1,#k do q=w(k[j],q)end;return p(q,8)end
local function b(k,q)local k=type(k)=="table"and{h(k)}or
{tostring(k):byte(1,-1)}
local q=type(q)=="table"and
{h(q)}or{tostring(q):byte(1,-1)}local j=64;q=#q>j and v(q)or q;local x={}local z={}local _={}for E=1,j do
x[E]=n(0x36,q[E]or 0)z[E]=n(0x5C,q[E]or 0)end;for E=1,#k do
x[j+E]=k[E]end;x=v(x)for E=1,j do _[E]=z[E]_[j+E]=x[E]end;return v(_)end
local function g(k,q,j,x)
local q=type(q)=="table"and q or{tostring(q):byte(1,-1)}local z=32;local x=x or 32;local _=1;local E={}
while x>0 do local T={}local A={h(q)}
local O=x>z and z or x;A[#A+1]=o(d(_,24),0xFF)A[#A+1]=o(d(_,16),0xFF)
A[#A+1]=o(d(_,8),0xFF)A[#A+1]=o(_,0xFF)
for I=1,j do A=b(A,k)
for N=1,O do T[N]=n(A[N],T[N]or 0)end;if I%200 ==0 then os.queueEvent("PBKDF2",I)
coroutine.yield("PBKDF2")end end;x=x-O;_=_+1;for I=1,O do E[#E+1]=T[I]end end;return setmetatable(E,y)end;return{pbkdf2=g,digest=v,hmac=b}end
e["smt.crypto.rlwe"]=function(...)local o=t("smt.crypto.chacha")
local i=t("smt.crypto.sha")local n=os.pullEvent;local s=fs.open;local h=tostring(math.random())
local r=1024;local d="/.urandom"
local l=bit32 and bit32.band or bit.band;local u=bit32 and bit32.bnot or bit.bnot;local c=
bit32 and bit32.bxor or bit.bxor
local m=bit32 and bit32.lshift or bit.blshift;local function f(a,w)local Z=a/ (2^w)return Z-Z%1 end;if fs.exists(d)then
local a=fs.open(d,"rb")h=h..a.readAll()a.close()end
local y=i.digest(h)
if not os.urandom then
os.pullEvent=function(a)
while true do local w={n()}local Z=#h
h={h,w[1],tostring(w[2]),tostring(w[3]),tostring(w[4]),tostring(os.epoch("utc")),tostring({})}h=table.concat(h,",")
if#h>r then y=i.digest(h)h=tostring(y)end;if not a or a==w[1]then return unpack(w)end end end
fs.open=function(a,w)
local Z=fs.exists(a)and fs.getSize(a)or 0;h={h,a,w,tostring(Z)}h=table.concat(h)
os.queueEvent("file")os.pullEvent()return s(a,w)end
os.urandom=function()os.queueEvent("random")os.pullEvent()
y=i.digest(y)return i.hmac("out",y)end end;local p=512;local v=25601
local b={1,114,12996,22287,6219,17739,25368,24640,18451,4132,10230,14175,3087,19105,1885,10082,22904,25355,23158,3109,21613,6186,13977,6116,5997,18032,7568,17919,20287,8628,10754,22709,3125,23437,9314,12155,3216,8210,14304,17793,5923,9596,18702,7145,20899,1593,2395,17020,20205,24881,20324,12846,5187,2495,2819,14154,693,2199,20277,7488,8799,4647,17738,25254,11644,21765,23514,18092,14408,4048,654,23354,25453,8729,22268,4053,1224,11531,8883,14223,8559,2888,22020,1382,3942,14171,2631,18323,15141,10807,3150,686,1401,6108,5085,16468,8479,19369,6380,10492,18442,3106,21271,18400,23919,13060,3982,18731,10451,13768,7891,3539,19431,13448,22613,17782,4669,20246,3954,15539,4977,4156,12966,18867,354,14755,18005,4490,25441,7361,19922,18220,3399,3471,11679,154,17556,4506,1664,10489,18100,15320,5612,25344,21904,13759,6865,14580,23656,8679,16568,19879,13318,7793,17968,272,5407,1974,20228,1902,12020,13427,20219,876,23061,17652,15450,20432,25158,700,2997,8845,9891,1130,815,16107,18527,12796,25088,18321,14913,10416,9778,13849,17125,6574,7007,5167,215,24510,3631,4318,5833,24937,1107,23794,24411,17946,23365,1106,23680,11415,21260,17146,8968,23913,12376,2809,13014,24339,9738,9289,9305,11129,14257,12435,9535,11748,8020,18245,6249,21159,5632,2023,213,24282,3240,10946,18996,15060,1573,115,13110,9682,2905,23958,17506,24407,17490,22583,14362,24405,17262,22192,20990,11967,7385,22658,22912,666,24722,2198,20163,20093,12113,24029,25600,25487,12605,3314,19382,7862,233,961,7150,21469,15371,11426,22514,6496,23716,15519,2697,246,2443,22492,3988,19415,11624,19485,19604,7569,18033,7682,5314,16973,14847,2892,22476,2164,16287,13446,22385,17391,11297,7808,19678,16005,6899,18456,4702,24008,23206,8581,5396,720,5277,12755,20414,23106,22782,11447,24908,23402,5324,18113,16802,20954,7863,347,13957,3836,2087,7509,11193,21553,24947,2247,148,16872,3333,21548,24377,14070,16718,11378,17042,22713,3581,24219,21659,11430,22970,7278,10460,14794,22451,24915,24200,19493,20516,9133,17122,6232,19221,15109,7159,22495,4330,7201,1682,12541,21619,6870,15150,11833,17710,22062,6170,12153,2988,7819,20932,5355,21647,10062,20624,21445,12635,6734,25247,10846,7596,21111,160,18240,5679,7381,22202,22130,13922,25447,8045,21095,23937,15112,7501,10281,19989,257,3697,11842,18736,11021,1945,16922,9033,5722,12283,17808,7633,25329,20194,23627,5373,23699,13581,12174,5382,24725,2540,7949,10151,5169,443,24901,22604,16756,15710,24471,24786,9494,7074,12805,513,7280,10688,15185,15823,11752,8476,19027,18594,20434,25386,1091,21970,21283,19768,664,24494,1807,1190,7655,2236,24495,1921,14186,4341,8455,16633,1688,13225,22792,12587,1262,15863,16312,16296,14472,11344,13166,16066,13853,17581,7356,19352,4442,19969,23578,25388,1319,22361,14655,6605,10541,24028,25486,12491,15919,22696,1643,8095,1194,8111,3018,11239,1196,8339,3409,4611,13634,18216,2943,2689,24935,879,23403,5438,5508,13488,1572}
local g={1,1572,13488,5508,5438,23403,879,24935,2689,2943,18216,13634,4611,3409,8339,1196,11239,3018,8111,1194,8095,1643,22696,15919,12491,25486,24028,10541,6605,14655,22361,1319,25388,23578,19969,4442,19352,7356,17581,13853,16066,13166,11344,14472,16296,16312,15863,1262,12587,22792,13225,1688,16633,8455,4341,14186,1921,24495,2236,7655,1190,1807,24494,664,19768,21283,21970,1091,25386,20434,18594,19027,8476,11752,15823,15185,10688,7280,513,12805,7074,9494,24786,24471,15710,16756,22604,24901,443,5169,10151,7949,2540,24725,5382,12174,13581,23699,5373,23627,20194,25329,7633,17808,12283,5722,9033,16922,1945,11021,18736,11842,3697,257,19989,10281,7501,15112,23937,21095,8045,25447,13922,22130,22202,7381,5679,18240,160,21111,7596,10846,25247,6734,12635,21445,20624,10062,21647,5355,20932,7819,2988,12153,6170,22062,17710,11833,15150,6870,21619,12541,1682,7201,4330,22495,7159,15109,19221,6232,17122,9133,20516,19493,24200,24915,22451,14794,10460,7278,22970,11430,21659,24219,3581,22713,17042,11378,16718,14070,24377,21548,3333,16872,148,2247,24947,21553,11193,7509,2087,3836,13957,347,7863,20954,16802,18113,5324,23402,24908,11447,22782,23106,20414,12755,5277,720,5396,8581,23206,24008,4702,18456,6899,16005,19678,7808,11297,17391,22385,13446,16287,2164,22476,2892,14847,16973,5314,7682,18033,7569,19604,19485,11624,19415,3988,22492,2443,246,2697,15519,23716,6496,22514,11426,15371,21469,7150,961,233,7862,19382,3314,12605,25487,25600,24029,12113,20093,20163,2198,24722,666,22912,22658,7385,11967,20990,22192,17262,24405,14362,22583,17490,24407,17506,23958,2905,9682,13110,115,1573,15060,18996,10946,3240,24282,213,2023,5632,21159,6249,18245,8020,11748,9535,12435,14257,11129,9305,9289,9738,24339,13014,2809,12376,23913,8968,17146,21260,11415,23680,1106,23365,17946,24411,23794,1107,24937,5833,4318,3631,24510,215,5167,7007,6574,17125,13849,9778,10416,14913,18321,25088,12796,18527,16107,815,1130,9891,8845,2997,700,25158,20432,15450,17652,23061,876,20219,13427,12020,1902,20228,1974,5407,272,17968,7793,13318,19879,16568,8679,23656,14580,6865,13759,21904,25344,5612,15320,18100,10489,1664,4506,17556,154,11679,3471,3399,18220,19922,7361,25441,4490,18005,14755,354,18867,12966,4156,4977,15539,3954,20246,4669,17782,22613,13448,19431,3539,7891,13768,10451,18731,3982,13060,23919,18400,21271,3106,18442,10492,6380,19369,8479,16468,5085,6108,1401,686,3150,10807,15141,18323,2631,14171,3942,1382,22020,2888,8559,14223,8883,11531,1224,4053,22268,8729,25453,23354,654,4048,14408,18092,23514,21765,11644,25254,17738,4647,8799,7488,20277,2199,693,14154,2819,2495,5187,12846,20324,24881,20205,17020,2395,1593,20899,7145,18702,9596,5923,17793,14304,8210,3216,12155,9314,23437,3125,22709,10754,8628,20287,17919,7568,18032,5997,6116,13977,6186,21613,3109,23158,25355,22904,10082,1885,19105,3087,14175,10230,4132,18451,24640,25368,17739,6219,22287,12996,114}
local k={1,22188,114,20534,12996,11185,22287,20641,6219,23383,17739,3158,25368,1598,24640,2965,18451,5197,4132,3635,10230,4774,14175,6615,3087,11681,19105,382,1885,17947,10082,23479,22904,14102,25355,20366,23158,17634,3109,13398,21613,16913,6186,8007,13977,16763,6116,16508,5997,13039,18032,1588,7568,1825,17919,3242,20287,11174,8628,19387,10754,8432,22709,14011,3125,9992,23437,12644,9314,7760,12155,14206,3216,6621,8210,12365,14304,1555,17793,23664,5923,9591,9596,18132,18702,18968,7145,11868,20899,21700,1593,16104,2395,18185,17020,25010,20205,9429,24881,25265,20324,12898,12846,11115,5187,12661,2495,9698,2819,4729,14154,1485,693,15684,2199,21507,20277,19703,7488,18855,8799,24587,4647,12409,17738,6571,25254,6665,11644,17381,21765,10157,23514,5853,18092,1616,14408,5017,4048,8716,654,20786,23354,14312,25453,18705,8729,7487,22268,8685,4053,17252,1224,21052,11531,19035,8883,19506,14223,21998,8559,24475,2888,25242,22020,10276,1382,19419,3942,12080,14171,20267,2631,6348,18323,6844,15141,12186,10807,6750,3150,1470,686,13974,1401,5774,6108,18211,5085,2373,16468,14512,8479,15904,19369,20986,6380,11511,10492,6603,18442,10313,3106,23637,21271,6513,18400,53,23919,6042,13060,23162,3982,3565,18731,22395,10451,18531,13768,13252,7891,269,3539,5065,19431,14188,13448,4569,22613,8846,17782,10005,4669,14126,20246,23102,3954,22326,15539,10665,4977,12563,4156,24127,12966,11171,18867,19045,354,20646,14755,23953,18005,16936,4490,10629,25441,8459,7361,17089,19922,2470,18220,25570,3399,22067,3471,6740,11679,330,154,12019,17556,13313,4506,7223,1664,4190,10489,16842,18100,25514,15320,15683,5612,21393,25344,6707,21904,22169,13759,18368,6865,20271,14580,6804,23656,7626,8679,24531,16568,6025,19879,21224,13318,13042,7793,1930,17968,15212,272,18901,5407,4230,1974,21402,20228,7733,1902,11128,12020,14143,13427,25040,20219,12849,876,5529,23061,15882,17652,18478,15450,7210,20432,2708,25158,1500,700,17394,2997,11639,8845,21195,9891,9736,1130,9061,815,8914,16107,17757,18527,1819,12796,2558,25088,10001,18321,13670,14913,22320,10416,9981,9778,11390,13849,18410,17125,25059,6574,15015,7007,22044,5167,4118,215,8634,24510,11438,3631,23882,4318,8842,5833,9549,24937,13344,1107,10757,23794,23051,24411,16512,17946,13495,23365,2370,1106,14170,23680,2517,11415,5327,21260,18455,17146,4588,8968,11012,23913,919,12376,2362,2809,13258,13014,953,24339,6238,9738,19905,9289,16282,9305,12876,11129,8607,14257,8360,12435,5803,9535,21517,11748,20843,8020,20810,18245,17048,6249,23397,21159,4754,5632,4335,2023,7771,213,15460,24282,21572,3240,1512,10946,18762,18996,13985,15060,7028,1573,7561,115,17121,13110,6118,9682,6225,2905,18423,23958,940,17506,4756,24407,4563,17490,8162,22583,8832,14362,8409,24405,11389,17262,18296,22192,12063,20990,18329,11967,15825,7385,11980,22658,8867,22912,12399,666,5431,24722,4710,2198,24920,20163,24770,20093,7670,12113,3946,24029,14627}
local q={25551,14522,23804,18093,16827,25086,6211,9652,9711,17152,7496,5091,7252,15540,7699,5526,19156,8133,6456,10177,10836,23220,9527,20415,25460,14327,8757,18765,18267,6228,17003,10834,1272,6383,2706,24085,4066,23342,17103,7391,4866,21399,20254,25115,17245,4038,23282,24289,15475,11217,5750,19636,1847,18587,10571,8023,2563,16464,9679,24398,8394,3358,10853,4970,10650,4535,24347,11942,25590,7291,8309,17805,5238,7567,16215,16460,16985,18110,24178,608,15932,8539,7326,8384,21623,20734,18829,3775,4432,20469,3632,22412,481,4688,13703,22049,10675,22875,12445,15696,4376,20349,18004,12979,13183,24592,12467,1114,13359,10340,7528,23446,6354,17273,4098,16096,16205,9124,1265,6368,17303,505,12054,229,4148,1574,18002,16632,10039,6883,11092,16454,2343,8678,22253,22084,10750,1092,2340,1357,17537,8321,21488,24102,11417,24465,1223,6278,2481,12631,8780,15157,3221,17874,20015,13631,25552,25496,25376,14147,4714,17416,11719,10483,15149,17833,5298,381,8131,10109,7033,18728,21845,24867,9399,23798,3451,7395,23161,2086,4470,2264,12166,469,1005,20440,18199,2425,12511,23152,5724,15923,12177,18779,18297,2635,12961,20459,21897,6692,14340,23414,13600,18171,2365,19697,5635,12075,274,11559,21112,19639,9168,23303,24334,22886,5154,7387,12172,15111,10437,22365,22324,7607,19958,2537,12751,20009,24590,16120,23571,21251,8965,22868,12430,4692,6397,2736,20492,24,7366,12127,7700,16500,20728,4187,19944,2507,16344,24051,14965,21096,23262,9617,9636,13334,17601,19430,19692,1967,4215,20004,20922,8260,17700,5013,21714,20929,8275,3103,2992,13726,18441,21230,8920,15457,18493,3055,13861,15073,3041,13831,18666,7083,4206,23642,6774,18173,24313,22841,23344,13450,10535,22575,22774,4914,10530,18907,14914,24644,19893,6055,12975,20489,18304,2650,23965,18438,13909,4204,1694,3630,464,22938,12580,12328,11788,25260,21213,1569,14334,8772,4168,16246,23841,14515,23789,7089,18848,7473,8699,22298,3894,4687,2729,20477,14621,9387,20115,10188,3545,14911,17323,15177,17893,23713,17898,1780,157,7651,16395,20503,18334,24658,19923,2462,8933,4513,13328,2959,9998,17767,23443,24634,12557,15936,1233,13614,18201,24373,15655,15260,7099,583,23193,20441,3572,3997,8565,11039,23655,21431,13008,24217,18978,437,8251,21338,16466,6026,1941,502,4733,21114,15986,12312,15411,108,7546,16170,9049,23048,16473,6041,12945,24082,22346,18626,3340,18129,2275,4875,17761,8801,15202,10632,11811,21652,6167,13215,17346,11569,2847,9758,20910,4577,24437,1163,13464,10565,18982,18732,14539,5554,19216,947,23973,3826,884,23838,7194,19073,18927,3985,4882,17776,19805,13181,2644,9323,9006,11984,79,22113,21784,21079,15911,8494,25516,14447,19986,2597,5565,11925,18239,6168,24189,18918,7623,16335,2088,817,5408,4274,1844,11266,5855,19861,13301,13873,18756,21905,17681,1315,17447,19100,8013,20828,744,23538,17523,8291,25081,2543,1792,3840,914,20245,3152,3097,13951,4294,16516,17105,3738,8010,13507,21629,9775,2660,5700,8557}local j
local x=math.ceil(math.log(v)/math.log(2))
local z=math.ceil(math.log(v)/math.log(256))local _=math.ceil(p*x/8)local E=math.ceil(p/8)local T=os.urandom
local A={__tostring=function(j)return
string.char(unpack(j))end,__index={toHex=function(a,w)return
("%02x"):rep(#a):format(unpack(a))end,isEqual=function(a,w)
if type(w)~="table"then return false end;if#a~=#w then return false end;local Z=0;for ee=1,#a do
Z=bit32.bor(Z,c(a[ee],w[ee]))end;return Z==0 end}}
local function O(a,w,Z)Z=Z or 32;w=w or 0;return
o.crypt(string.rep(string.char(0),Z),a,{0,0,0,0,0,0,0,0,0,0,0,w})end
local function I(a,w)local Z={}for ee=1,math.max(#a,#w)do
Z[ee]=c((a[ee]or 0),(w[ee]or 0))end;return Z end;local function N(...)local a={...}local w=a[1]
for Z=2,#a do for ee=1,#a[Z]do w[#w+1]=a[Z][ee]end end;return w end
local function S(a)local w={}for Z=0,#a-1,8 do
local ee=0
for et=8,1,-1 do ee=m(ee,1)ee=ee+ (a[Z+et]or 0)end;w[#w+1]=ee end;return
setmetatable(w,A)end
local function H(a)local w={}
for Z=1,#a do local ee=a[Z]for et=1,8 do w[#w+1]=l(ee,1)ee=f(ee,1)end end;return setmetatable(w,A)end
local function R(a)local a={unpack(a)}local w={}
for ee=1,p do local et=a[ee]for ea=1,x do local eo=et%2;w[#w+1]=et%2;et=
(et-eo)/2 end end;local Z=S(w)return Z end
local function D(a)local w=H(a)local Z={}
for ee=0,#w-1,x do local et=0;for ea=x,1,-1 do et=et*2
et=et+ (w[ee+ea]or 0)end;Z[#Z+1]=et end;return Z end;local function L(a,w)return O(T(),a,w)end
local function U(a)local w=45;local Z=13;local ee={}local et={}
while#et<p do
if#ee<w then
local eo=math.ceil(
w*math.ceil((p-#et)/Z)/8)a=O(a)ee=H(O(a,0x01,eo))end;local ea=0
for eo=1,w do ea=ea*2;ea=ea+table.remove(ee)end
if ea<34522712143931 then local eo=1;while#et<p and eo<=Z do local ei=(ea%11)
et[#et+1]=ei-5;ea=(ea-ei)/11;eo=eo+1 end end end;return et end
local function C(a)local w=(256^z-v)%v;local L={}local Z={}
while#Z<p do if#L==0 then local et=(p-#Z)*z
a=O(a)L=O(a,0x02,et)end;local ee=0;for et=1,z do ee=ee*256;ee=ee+
table.remove(L)end
if ee>=w then Z[#Z+1]=(ee%v)end end;return Z end;j=C(i.digest(""))
local function M(a)local w,Z;local ee,et,ea;local eo,ei;for ee=1,p do
a[ee]=(a[ee]*k[ee])%v end;Z=1;ea=f(p,1)
while ea>=1 do w=0
for et=0,ea-1 do
for ee=et,p-1,m(ea,1)do eo=
(a[ee+1]+a[ee+ea+1])%v
ei=(a[ee+1]-a[ee+ea+1])%v;a[ee+ea+1]=(ei*b[w+1])%v;a[ee+1]=eo end;w=(w-Z)%p end;Z=m(Z,1)ea=f(ea,1)end;return a end
local function F(a)local w,Z;local ee,et,ea;local eo,ei;Z=f(p,1)ea=1
while ea<p do w=0
for et=0,ea-1 do
for ee=et,p-1,m(ea,1)do eo=a[ee+1]ei=(
a[ee+ea+1]*g[w+1])%v
a[ee+1]=(eo+ei)%v;a[ee+ea+1]=(eo-ei)%v end;w=(w-Z)%p end;Z=f(Z,1)ea=m(ea,1)end;for ee=1,p do a[ee]=(a[ee]*q[ee])%v end;return a end
local function W(j,a)local w={}for Z=1,p do w[Z]=(j[Z]+a[Z])%v end;return w end
local function Y(j,a)local w={}for Z=1,p do w[Z]=(j[Z]*a[Z])%v end;return w end
local function P(a,w)local Z={}
for ee=1,p do
if a[ee]==0 then w=O(w)local et=w[1]%2
Z[ee]=et==1 and v-1 or a[ee]elseif a[ee]== (v-1)/4 then w=O(w)local et=w[1]%2;Z[ee]=coinflip==1 and(v+3/4)or
a[ee]else Z[ee]=a[ee]end end;return Z end
local function V(a)local w={}for Z=1,#a do
if a[Z]>1/4*v and a[Z]<3/4*v then w[Z]=1 else w[Z]=0 end end;return i.digest(S(w))end
local function B(a)local w={}
for Z=1,#a do if a[Z]>=1/4*v and a[Z]<1/2*v then w[Z]=1 elseif a[Z]>=3/4*v and
a[Z]<v then w[Z]=1 else w[Z]=0 end end;return S(w)end
local function G(b,a)a=H(a)local w={}
for Z=1,#b do
if a[Z]==0 and b[Z]>=7/8*v or
a[Z]==0 and b[Z]<3/8*v then w[Z]=0 elseif a[Z]==1 and b[Z]>=5/8*v or a[Z]==1 and b[Z]<
1/8*v then w[Z]=0 else w[Z]=1 end end;return i.digest(S(w))end;local function K()local a=T()local w=U(a)local Z=U(a)w=M(w)Z=M(Z)local ee=W(Y(Z,j),w)local et=R(Z)
local ea=R(ee)return et,ea end
local function Q(a,w,Z)assert(
#w<=32)local Z={unpack(Z)}local ee=D(a)local et=U(Z)local ea=U(Z)
local eo=U(Z)et=M(et)ea=M(ea)eo=M(eo)local ei=W(Y(et,j),ea)
local en=W(Y(et,ee),eo)en=F(en)en=P(en,Z)mu=V(en)local es=N(R(ei),B(en),I(mu,w))
return es end;local function J(a)local w=T()local Z=Q(a,w,i.digest({unpack(w)}))
return w,Z end
local function X(a,w,Z)local ee=D(a)
local et={unpack(Z,1,_)}local ea={unpack(Z,1+_,E+_)}
local eo={unpack(Z,1+_+E,32+_+E)}local b=Y(D(et),ee)b=F(b)local eo=I(G(b,ea),eo)
local ei=Q(w,eo,i.digest({unpack(eo)}))
if ei:isEqual(Z)then return setmetatable(eo,A)else return T()end end;return{encapsulate=J,decapsulate=X,keyPair=K,KEM1Encrypt=Q,randomBytes=L}end
e["smt.crypto.chacha"]=function(...)local a=bit32.bxor;local o=bit32.band;local i=bit32.lshift
local n=bit32.arshift;local s=2^32
local h={("expand 16-byte k"):byte(1,-1)}
local r={("expand 32-byte k"):byte(1,-1)}
local function d(p,v)local b=p/ (2^ (32-v))local g=b%1;return(b-g)+g*s end
local function l(p,v,g,k,q)p[v]=(p[v]+p[g])%s;p[q]=d(a(p[q],p[v]),16)p[k]=(
p[k]+p[q])%s;p[g]=d(a(p[g],p[k]),12)p[v]=(p[v]+
p[g])%s;p[q]=d(a(p[q],p[v]),8)p[k]=
(p[k]+p[q])%s;p[g]=d(a(p[g],p[k]),7)return p end
local function u(p,v)local b={unpack(p)}
for g=1,v do local k=g%2 ==1
b=k and l(b,1,5,9,13)or l(b,1,6,11,16)b=k and l(b,2,6,10,14)or l(b,2,7,12,13)b=k and
l(b,3,7,11,15)or l(b,3,8,9,14)b=
k and l(b,4,8,12,16)or l(b,4,5,10,15)end;for g=1,16 do b[g]=(b[g]+p[g])%s end;return b end
local function c(p,v)return
(p[v+1]or 0)+i((p[v+2]or 0),8)+
i((p[v+3]or 0),16)+i((p[v+4]or 0),24)end
local function m(p,v,b)local g=#p==32;local k=g and r or h;local q={}q[1]=c(k,0)q[2]=c(k,4)
q[3]=c(k,8)q[4]=c(k,12)q[5]=c(p,0)q[6]=c(p,4)q[7]=c(p,8)q[8]=c(p,12)q[9]=c(p,
g and 16 or 0)q[10]=c(p,g and 20 or 4)q[11]=c(p,
g and 24 or 8)q[12]=c(p,g and 28 or 12)
q[13]=b;q[14]=c(v,0)q[15]=c(v,4)q[16]=c(v,8)return q end
local function f(p)local v={}for b=1,16 do v[#v+1]=o(p[b],0xFF)
v[#v+1]=o(n(p[b],8),0xFF)v[#v+1]=o(n(p[b],16),0xFF)
v[#v+1]=o(n(p[b],24),0xFF)end;return v end
local w={__tostring=function(p)return string.char(unpack(p))end,__index={toHex=function(p,v)return("%02x"):rep(
#p):format(unpack(p))end,isEqual=function(p,v)if
type(v)~="table"then return false end;if#p~=#v then return false end;local b=0;for g=1,
#p do b=bit32.bor(b,a(p[g],v[g]))end
return b==0 end}}
local function y(p,v,b,g,k)
assert(type(v)=="table","ChaCha20: Invalid key format ("..type(v).."), must be table")
assert(type(b)=="table","ChaCha20: Invalid nonce format ("..type(b).."), must be table")
assert(#v==16 or#v==32,"ChaCha20: Invalid key length ("..#v.."), must be 16 or 32")
assert(#b==12,"ChaCha20: Invalid nonce length ("..#b.."), must be 12")local p=type(p)=="table"and{unpack(p)}or
{tostring(p):byte(1,-1)}
g=tonumber(g)or 1;k=tonumber(k)or 20;local q={}local j=m(v,b,g)
local x=math.floor(#p/64)
for z=0,x do local _=f(u(j,k))j[13]=(j[13]+1)%s;local E={}for T=1,64 do
E[T]=p[((z)*64)+T]end;for T=1,#E do q[#q+1]=a(E[T],_[T])end
if
z%1000 ==0 then os.queueEvent("")os.pullEvent("")end end;return setmetatable(q,w)end;return{crypt=y}end;return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.