Cleanup
This commit is contained in:
@@ -12,12 +12,11 @@ local band = bit32.band
|
||||
local blshift = bit32.lshift
|
||||
local brshift = bit32.arshift
|
||||
local textutils = _G.textutils
|
||||
local mt = Util.byteArrayMT
|
||||
|
||||
local mod = 2^32
|
||||
local tau = {("expand 16-byte k"):byte(1,-1)}
|
||||
local sigma = {("expand 32-byte k"):byte(1,-1)}
|
||||
local null32 = {("A"):rep(32):byte(1,-1)}
|
||||
local null12 = {("A"):rep(12):byte(1,-1)}
|
||||
|
||||
local function rotl(n, b)
|
||||
local s = n/(2^(32-b))
|
||||
@@ -91,22 +90,6 @@ local function serialize(state)
|
||||
return r
|
||||
end
|
||||
|
||||
local mt = {
|
||||
__tostring = function(a) return string.char(table.unpack(a)) end,
|
||||
__index = {
|
||||
toHex = function(self) return ("%02x"):rep(#self):format(table.unpack(self)) end,
|
||||
isEqual = function(self, t)
|
||||
if type(t) ~= "table" then return false end
|
||||
if #self ~= #t then return false end
|
||||
local ret = 0
|
||||
for i = 1, #self do
|
||||
ret = bit32.bor(ret, bxor(self[i], t[i]))
|
||||
end
|
||||
return ret == 0
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
local function crypt(data, key, nonce, cntr, round)
|
||||
assert(type(key) == "table", "ChaCha20: Invalid key format ("..type(key).."), must be table")
|
||||
assert(type(nonce) == "table", "ChaCha20: Invalid nonce format ("..type(nonce).."), must be table")
|
||||
@@ -133,15 +116,12 @@ local function crypt(data, key, nonce, cntr, round)
|
||||
out[#out+1] = bxor(block[j], ks[j])
|
||||
end
|
||||
|
||||
--if i % 1000 == 0 then
|
||||
throttle()
|
||||
--os.queueEvent("")
|
||||
--os.pullEvent("")
|
||||
--end
|
||||
throttle()
|
||||
end
|
||||
return setmetatable(out, mt)
|
||||
end
|
||||
|
||||
-- Helper functions
|
||||
local function genNonce(len)
|
||||
local nonce = {}
|
||||
for i = 1, len do
|
||||
@@ -170,6 +150,9 @@ end
|
||||
local obj = {}
|
||||
local rng_mt = {['__index'] = obj}
|
||||
|
||||
-- PRNG object
|
||||
local null32 = {("A"):rep(32):byte(1,-1)}
|
||||
local null12 = {("A"):rep(12):byte(1,-1)}
|
||||
function obj:nextInt(byte)
|
||||
if not byte or byte < 1 or byte > 6 then error("Can only return 1-6 bytes", 2) end
|
||||
local output = 0
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
local fq = require('opus.crypto.ecc.fq')
|
||||
local elliptic = require('opus.crypto.ecc.elliptic')
|
||||
local sha256 = require('opus.crypto.sha2')
|
||||
local Util = require('opus.util')
|
||||
|
||||
|
||||
local os = _G.os
|
||||
local unpack = table.unpack
|
||||
local mt = Util.byteArrayMT
|
||||
|
||||
local q = {1372, 62520, 47765, 8105, 45059, 9616, 65535, 65535, 65535, 65535, 65535, 65532}
|
||||
|
||||
@@ -27,7 +30,7 @@ local function publicKey(sk)
|
||||
local Y = elliptic.scalarMulG(x)
|
||||
local pk = elliptic.pointEncode(Y)
|
||||
|
||||
return pk
|
||||
return setmetatable(pk, mt)
|
||||
end
|
||||
|
||||
local function exchange(sk, pk)
|
||||
@@ -62,7 +65,7 @@ local function sign(sk, message)
|
||||
sig[#sig + 1] = s[i]
|
||||
end
|
||||
|
||||
return sig
|
||||
return setmetatable(sig, mt)
|
||||
end
|
||||
|
||||
local function verify(pk, message, sig)
|
||||
|
||||
@@ -9,6 +9,7 @@ local bnot = bit32 and bit32.bnot or bit.bnot
|
||||
local bxor = bit32 and bit32.bxor or bit.bxor
|
||||
local blshift = bit32 and bit32.lshift or bit.blshift
|
||||
local upack = unpack or table.unpack
|
||||
local mt = Util.byteArrayMT
|
||||
|
||||
local function rrotate(n, b)
|
||||
local s = n/(2^b)
|
||||
@@ -68,17 +69,16 @@ end
|
||||
|
||||
local function digestblock(w, C)
|
||||
for j = 17, 64 do
|
||||
-- local v = w[j-15]
|
||||
local s0 = bxor(bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18)), brshift(w[j-15], 3))
|
||||
local s1 = bxor(bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19)), brshift(w[j-2], 10))
|
||||
local s0 = bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18), brshift(w[j-15], 3))
|
||||
local s1 = bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19), brshift(w[j-2], 10))
|
||||
w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod32
|
||||
end
|
||||
local a, b, c, d, e, f, g, h = upack(C)
|
||||
for j = 1, 64 do
|
||||
local S1 = bxor(bxor(rrotate(e, 6), rrotate(e, 11)), rrotate(e, 25))
|
||||
local S1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
|
||||
local ch = bxor(band(e, f), band(bnot(e), g))
|
||||
local temp1 = (h + S1 + ch + K[j] + w[j])%mod32
|
||||
local S0 = bxor(bxor(rrotate(a, 2), rrotate(a, 13)), rrotate(a, 22))
|
||||
local S0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
|
||||
local maj = bxor(bxor(band(a, b), band(a, c)), band(b, c))
|
||||
local temp2 = (S0 + maj)%mod32
|
||||
h, g, f, e, d, c, b, a = g, f, e, (d+temp1)%mod32, c, b, a, (temp1+temp2)%mod32
|
||||
@@ -94,22 +94,6 @@ local function digestblock(w, C)
|
||||
return C
|
||||
end
|
||||
|
||||
local mt = {
|
||||
__tostring = function(a) return string.char(upack(a)) end,
|
||||
__index = {
|
||||
toHex = function(self) return ("%02x"):rep(#self):format(upack(self)) end,
|
||||
isEqual = function(self, t)
|
||||
if type(t) ~= "table" then return false end
|
||||
if #self ~= #t then return false end
|
||||
local ret = 0
|
||||
for i = 1, #self do
|
||||
ret = bit32.bor(ret, bxor(self[i], t[i]))
|
||||
end
|
||||
return ret == 0
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
local function toBytes(t, n)
|
||||
local b = {}
|
||||
for i = 1, n do
|
||||
|
||||
Reference in New Issue
Block a user