From 39522ee5b1f2c6fb875bbe1ea76bec8836e5dac8 Mon Sep 17 00:00:00 2001 From: Anavrins Date: Mon, 30 Mar 2020 02:07:20 -0400 Subject: [PATCH] Cleanup --- sys/apps/Overview.lua | 4 +++ sys/apps/ShellLauncher.lua | 2 +- sys/apps/network/keygen.lua | 34 +++++++++++------------ sys/autorun/upgraded.lua | 2 +- sys/modules/opus/crypto/chacha20.lua | 29 ++++---------------- sys/modules/opus/crypto/ecc/init.lua | 7 +++-- sys/modules/opus/crypto/sha2.lua | 26 ++++-------------- sys/modules/opus/socket.lua | 4 +-- sys/modules/opus/util.lua | 41 +++++++++++++++++++++++++--- 9 files changed, 78 insertions(+), 71 deletions(-) diff --git a/sys/apps/Overview.lua b/sys/apps/Overview.lua index b4b524f..c93f516 100644 --- a/sys/apps/Overview.lua +++ b/sys/apps/Overview.lua @@ -157,6 +157,7 @@ local page = UI.Page { f = 'files', s = 'shell', l = 'lua', + n = 'network', [ 'control-n' ] = 'new', delete = 'delete', }, @@ -435,6 +436,9 @@ function page:eventHandler(event) elseif event.type == 'files' then shell.switchTab(shell.openTab(Alt.get('files'))) + elseif event.type == 'network' then + shell.switchTab(shell.openTab('network')) + elseif event.type == 'focus_change' then if event.focused.parent.UIElement == 'Icon' then event.focused.parent:scrollIntoView() diff --git a/sys/apps/ShellLauncher.lua b/sys/apps/ShellLauncher.lua index f2eebb7..570234b 100644 --- a/sys/apps/ShellLauncher.lua +++ b/sys/apps/ShellLauncher.lua @@ -27,4 +27,4 @@ kernel.hook('kernel_focus', function(_, eventData) end end) -os.pullEventRaw('kernel_halt') \ No newline at end of file +os.pullEventRaw('kernel_halt') diff --git a/sys/apps/network/keygen.lua b/sys/apps/network/keygen.lua index 65f04b8..4a74670 100644 --- a/sys/apps/network/keygen.lua +++ b/sys/apps/network/keygen.lua @@ -10,30 +10,30 @@ local keyPairs = { } local function generateKeyPair() local key = { } for _ = 1, 32 do - table.insert(key, ("%02x"):format(math.random(0, 0xFF))) + table.insert(key, math.random(0, 0xFF)) end - local privateKey = Util.hexToByteArray(table.concat(key)) + local privateKey = setmetatable(key, Util.byteArrayMT) return privateKey, ECC.publicKey(privateKey) end getmetatable(network).__index.getKeyPair = function() - local keys = table.remove(keyPairs) - os.queueEvent('generate_keypair') - if not keys then - return generateKeyPair() - end - return table.unpack(keys) + local keys = table.remove(keyPairs) + os.queueEvent('generate_keypair') + if not keys then + return generateKeyPair() + end + return table.unpack(keys) end -- Generate key pairs in the background as this is a time-consuming process Event.on('generate_keypair', function() - while true do - os.sleep(5) - local timer = Util.timer() - table.insert(keyPairs, { generateKeyPair() }) - _G._syslog('Generated keypair in ' .. timer()) - if #keyPairs >= 3 then - break - end - end + while true do + os.sleep(5) + local timer = Util.timer() + table.insert(keyPairs, { generateKeyPair() }) + _G._syslog('Generated keypair in ' .. timer()) + if #keyPairs >= 3 then + break + end + end end) diff --git a/sys/autorun/upgraded.lua b/sys/autorun/upgraded.lua index 141bf1d..1cb0c1a 100644 --- a/sys/autorun/upgraded.lua +++ b/sys/autorun/upgraded.lua @@ -21,4 +21,4 @@ deleteIfExists('sys/autorun/apps.lua') deleteIfExists('sys/init/6.tl3.lua') -- remove this file -deleteIfExists('sys/autorun/upgraded.lua') \ No newline at end of file +deleteIfExists('sys/autorun/upgraded.lua') diff --git a/sys/modules/opus/crypto/chacha20.lua b/sys/modules/opus/crypto/chacha20.lua index 7c3b819..9db2d08 100644 --- a/sys/modules/opus/crypto/chacha20.lua +++ b/sys/modules/opus/crypto/chacha20.lua @@ -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 diff --git a/sys/modules/opus/crypto/ecc/init.lua b/sys/modules/opus/crypto/ecc/init.lua index 980f699..4712dca 100644 --- a/sys/modules/opus/crypto/ecc/init.lua +++ b/sys/modules/opus/crypto/ecc/init.lua @@ -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) diff --git a/sys/modules/opus/crypto/sha2.lua b/sys/modules/opus/crypto/sha2.lua index 4dcb98b..17df659 100644 --- a/sys/modules/opus/crypto/sha2.lua +++ b/sys/modules/opus/crypto/sha2.lua @@ -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 diff --git a/sys/modules/opus/socket.lua b/sys/modules/opus/socket.lua index b126643..89afa4b 100644 --- a/sys/modules/opus/socket.lua +++ b/sys/modules/opus/socket.lua @@ -139,7 +139,7 @@ function Socket.connect(host, port, options) dhost = socket.dhost, t = Crypto.encrypt({ -- this is not that much data... ts = os.epoch('utc'), - pk = Util.byteArrayToHex(socket.pubKey), + pk = socket.pubKey:toHex(), }, Util.hexToByteArray(identifier)), }) @@ -237,7 +237,7 @@ function Socket.server(port, options) type = 'CONN', dhost = socket.dhost, shost = socket.shost, - pk = Util.byteArrayToHex(socket.pubKey), + pk = socket.pubKey:toHex(), options = socket.options.ENCRYPT and { ENCRYPT = true }, }) diff --git a/sys/modules/opus/util.lua b/sys/modules/opus/util.lua index 564b9f8..383420e 100644 --- a/sys/modules/opus/util.lua +++ b/sys/modules/opus/util.lua @@ -9,6 +9,39 @@ local textutils = _G.textutils local _sformat = string.format local _srep = string.rep local _ssub = string.sub +local _unpack = table.unpack +local _bor = bit32.bor +local _bxor = bit32.bxor + +byteArrayMT = { + __tostring = function(a) return string.char(_unpack(a)) end, + __index = { + toHex = function(self) return ("%02x"):rep(#self):format(_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 = _bor(ret, _bxor(self[i], t[i])) + end + return ret == 0 + end, + sub = function(self, a, b) + local len = #self+1 + local start = a%len + local stop = (b or len-1)%len + local ret = {} + local i = 1 + for j = start, stop, start