This commit is contained in:
Anavrins 2020-03-30 02:07:20 -04:00
parent 369070e19c
commit 39522ee5b1
9 changed files with 78 additions and 71 deletions

View File

@ -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()

View File

@ -27,4 +27,4 @@ kernel.hook('kernel_focus', function(_, eventData)
end
end)
os.pullEventRaw('kernel_halt')
os.pullEventRaw('kernel_halt')

View File

@ -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)

View File

@ -21,4 +21,4 @@ deleteIfExists('sys/autorun/apps.lua')
deleteIfExists('sys/init/6.tl3.lua')
-- remove this file
deleteIfExists('sys/autorun/upgraded.lua')
deleteIfExists('sys/autorun/upgraded.lua')

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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 },
})

View File

@ -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<stop and 1 or -1 do
ret[i] = self[j]
i = i+1
end
return setmetatable(ret, byteArrayMT)
end
}
}
Util.byteArrayMT = byteArrayMT
function Util.hexToByteArray(str)
local r = {}
@ -16,12 +49,12 @@ function Util.hexToByteArray(str)
for b in str:gmatch("%x%x?") do
r[#r+1] = tonumber(b, 16)
end
return r
return setmetatable(r, byteArrayMT)
end
function Util.byteArrayToHex(tbl)
if not tbl then error('byteArrayToHex: invalid table', 2) end
return ("%02x"):rep(#tbl):format(table.unpack(tbl))
return ("%02x"):rep(#tbl):format(_unpack(tbl))
end
function Util.tryTimed(timeout, f, ...)
@ -39,10 +72,10 @@ function Util.tryTimes(attempts, f, ...)
for _ = 1, attempts do
result = { f(...) }
if result[1] then
return table.unpack(result)
return _unpack(result)
end
end
return table.unpack(result)
return _unpack(result)
end
function Util.timer()