From 61a26d7c55bdbf706a2dd4b2e79dde90f0950132 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sun, 30 Jun 2019 19:53:26 -0400 Subject: [PATCH] encryption perf --- sys/apps/network/vnc.lua | 20 +++ sys/apps/vnc.lua | 7 +- sys/modules/opus/{crypto => }/cbor.lua | 0 sys/modules/opus/crypto/chacha20.lua | 9 +- sys/modules/opus/crypto/lualzw.lua | 164 ------------------------- sys/modules/opus/crypto/serializer.lua | 50 -------- 6 files changed, 28 insertions(+), 222 deletions(-) rename sys/modules/opus/{crypto => }/cbor.lua (100%) delete mode 100644 sys/modules/opus/crypto/lualzw.lua delete mode 100644 sys/modules/opus/crypto/serializer.lua diff --git a/sys/apps/network/vnc.lua b/sys/apps/network/vnc.lua index 54ea8fc..c833ead 100644 --- a/sys/apps/network/vnc.lua +++ b/sys/apps/network/vnc.lua @@ -71,3 +71,23 @@ Event.addRoutine(function() end end end) + +Event.addRoutine(function() + + print('svnc: listening on port 5901') + + while true do + local socket = Socket.server(5901, { ENCRYPT = true }) + + print('svnc: connection from ' .. socket.dhost) + + -- no new process - only 1 connection allowed + -- due to term size issues + local s, m = pcall(vncHost, socket) + socket:close() + if not s and m then + print('vnc error') + _G.printError(m) + end + end +end) diff --git a/sys/apps/vnc.lua b/sys/apps/vnc.lua index cb2d945..0265254 100644 --- a/sys/apps/vnc.lua +++ b/sys/apps/vnc.lua @@ -10,7 +10,7 @@ local shell = _ENV.shell local term = _G.term local remoteId -local args = { ... } +local args, options = Util.parse(...) if #args == 1 then remoteId = tonumber(args[1]) else @@ -23,11 +23,12 @@ if not remoteId then end if multishell then - multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId) + multishell.setTitle(multishell.getCurrent(), + (options.s and 'SVNC-' or 'VNC-') .. remoteId) end local function connect() - local socket, msg, reason = Socket.connect(remoteId, 5900) + local socket, msg, reason = Socket.connect(remoteId, options.s and 5901 or 5900) if reason == 'NOTRUST' then local s, m = shell.run('trust ' .. remoteId) diff --git a/sys/modules/opus/crypto/cbor.lua b/sys/modules/opus/cbor.lua similarity index 100% rename from sys/modules/opus/crypto/cbor.lua rename to sys/modules/opus/cbor.lua diff --git a/sys/modules/opus/crypto/chacha20.lua b/sys/modules/opus/crypto/chacha20.lua index ba1afa7..89cf0ac 100644 --- a/sys/modules/opus/crypto/chacha20.lua +++ b/sys/modules/opus/crypto/chacha20.lua @@ -1,9 +1,8 @@ -- Chacha20 cipher in ComputerCraft -- By Anavrins -local LZW = require('opus.crypto.lualzw') +local cbor = require('opus.cbor') local sha2 = require('opus.crypto.sha2') -local Serializer = require('opus.crypto.serializer') local Util = require('opus.util') local ROUNDS = 8 -- Adjust this for speed tradeoff @@ -153,10 +152,10 @@ end local function encrypt(data, key) local nonce = genNonce(12) - data = Serializer.serialize(data) - data = LZW.compress(data) + data = cbor.encode(data) key = sha2.digest(key) local ctx = crypt(data, key, nonce, 1, ROUNDS) + return { nonce:toHex(), ctx:toHex() } end @@ -165,7 +164,7 @@ local function decrypt(data, key) data = Util.hexToByteArray(data[2]) key = sha2.digest(key) local ptx = crypt(data, key, nonce, 1, ROUNDS) - return textutils.unserialise(LZW.decompress(tostring(ptx))) + return cbor.decode(tostring(ptx)) end local obj = {} diff --git a/sys/modules/opus/crypto/lualzw.lua b/sys/modules/opus/crypto/lualzw.lua deleted file mode 100644 index 7bdfa21..0000000 --- a/sys/modules/opus/crypto/lualzw.lua +++ /dev/null @@ -1,164 +0,0 @@ --- see: https://github.com/Rochet2/lualzw ---[[ -MIT License - -Copyright (c) 2016 Rochet2 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -]] - -local char = string.char -local type = type -local sub = string.sub -local tconcat = table.concat - -local basedictcompress = {} -local basedictdecompress = {} -for i = 0, 255 do - local ic, iic = char(i), char(i, 0) - basedictcompress[ic] = iic - basedictdecompress[iic] = ic -end - -local function dictAddA(str, dict, a, b) - if a >= 256 then - a, b = 0, b+1 - if b >= 256 then - dict = {} - b = 1 - end - end - dict[str] = char(a,b) - a = a+1 - return dict, a, b -end - -local function compress(input) - if type(input) ~= "string" then - error ("string expected, got "..type(input)) - end - local len = #input - if len <= 1 then - return "u"..input - end - - local dict = {} - local a, b = 0, 1 - - local result = {"c"} - local resultlen = 1 - local n = 2 - local word = "" - for i = 1, len do - local c = sub(input, i, i) - local wc = word..c - if not (basedictcompress[wc] or dict[wc]) then - local write = basedictcompress[word] or dict[word] - if not write then - error "algorithm error, could not fetch word" - end - result[n] = write - resultlen = resultlen + #write - n = n+1 - if len <= resultlen then - return "u"..input - end - dict, a, b = dictAddA(wc, dict, a, b) - word = c - else - word = wc - end - end - result[n] = basedictcompress[word] or dict[word] - resultlen = resultlen+#result[n] - if len <= resultlen then - return "u"..input - end - return tconcat(result) -end - -local function dictAddB(str, dict, a, b) - if a >= 256 then - a, b = 0, b+1 - if b >= 256 then - dict = {} - b = 1 - end - end - dict[char(a,b)] = str - a = a+1 - return dict, a, b -end - -local function decompress(input) - if type(input) ~= "string" then - error( "string expected, got "..type(input)) - end - - if #input < 1 then - error("invalid input - not a compressed string") - end - - local control = sub(input, 1, 1) - if control == "u" then - return sub(input, 2) - elseif control ~= "c" then - error( "invalid input - not a compressed string") - end - input = sub(input, 2) - local len = #input - - if len < 2 then - error( "invalid input - not a compressed string") - end - - local dict = {} - local a, b = 0, 1 - - local result = {} - local n = 1 - local last = sub(input, 1, 2) - result[n] = basedictdecompress[last] or dict[last] - n = n+1 - for i = 3, len, 2 do - local code = sub(input, i, i+1) - local lastStr = basedictdecompress[last] or dict[last] - if not lastStr then - error( "could not find last from dict. Invalid input?") - end - local toAdd = basedictdecompress[code] or dict[code] - if toAdd then - result[n] = toAdd - n = n+1 - dict, a, b = dictAddB(lastStr..sub(toAdd, 1, 1), dict, a, b) - else - local tmp = lastStr..sub(lastStr, 1, 1) - result[n] = tmp - n = n+1 - dict, a, b = dictAddB(tmp, dict, a, b) - end - last = code - end - return tconcat(result) -end - -return { - compress = compress, - decompress = decompress, -} \ No newline at end of file diff --git a/sys/modules/opus/crypto/serializer.lua b/sys/modules/opus/crypto/serializer.lua deleted file mode 100644 index db19336..0000000 --- a/sys/modules/opus/crypto/serializer.lua +++ /dev/null @@ -1,50 +0,0 @@ -local Serializer = { } - -local insert = table.insert -local format = string.format - -function Serializer.serialize(tbl) - local output = { } - - local function recurse(t) - local sType = type(t) - if sType == 'table' then - if next(t) == nil then - insert(output, '{}') - else - insert(output, '{') - local tSeen = {} - for k, v in ipairs(t) do - tSeen[k] = true - recurse(v) - insert(output, ',') - end - for k, v in pairs(t) do - if not tSeen[k] then - if type(k) == 'string' and string.match(k, '^[%a_][%a%d_]*$') then - insert(output, k .. '=') - recurse(v) - insert(output, ',') - else - insert(output, '[') - recurse(k) - insert(output, ']=') - recurse(v) - insert(output, ',') - end - end - end - insert(output, '}') - end - elseif sType == 'string' then - insert(output, format('%q', t)) - else - insert(output, tostring(t)) - end - end - - recurse(tbl) - return table.concat(output) -end - -return Serializer