crypto cleanup
This commit is contained in:
@@ -10,6 +10,7 @@ local bxor = bit32.bxor
|
||||
local band = bit32.band
|
||||
local blshift = bit32.lshift
|
||||
local brshift = bit32.arshift
|
||||
local os = _G.os
|
||||
local textutils = _G.textutils
|
||||
|
||||
local mod = 2^32
|
||||
@@ -91,7 +92,7 @@ end
|
||||
local mt = {
|
||||
__tostring = function(a) return string.char(unpack(a)) end,
|
||||
__index = {
|
||||
toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
|
||||
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
|
||||
@@ -110,7 +111,7 @@ local function crypt(data, key, nonce, cntr, round)
|
||||
assert(#key == 16 or #key == 32, "ChaCha20: Invalid key length ("..#key.."), must be 16 or 32")
|
||||
assert(#nonce == 12, "ChaCha20: Invalid nonce length ("..#nonce.."), must be 12")
|
||||
|
||||
local data = type(data) == "table" and {unpack(data)} or {tostring(data):byte(1,-1)}
|
||||
data = type(data) == "table" and {unpack(data)} or {tostring(data):byte(1,-1)}
|
||||
cntr = tonumber(cntr) or 1
|
||||
round = tonumber(round) or 20
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ local fq = require('crypto.ecc.fq')
|
||||
local elliptic = require('crypto.ecc.elliptic')
|
||||
local sha256 = require('crypto.sha2')
|
||||
|
||||
local os = _G.os
|
||||
|
||||
local q = {1372, 62520, 47765, 8105, 45059, 9616, 65535, 65535, 65535, 65535, 65535, 65532}
|
||||
|
||||
local sLen = 24
|
||||
@@ -35,9 +37,7 @@ local function exchange(sk, pk)
|
||||
Z = elliptic.pointScale(Z)
|
||||
|
||||
local ss = fq.bytes(Z[2])
|
||||
local ss = sha256.digest(ss)
|
||||
|
||||
return ss
|
||||
return sha256.digest(ss)
|
||||
end
|
||||
|
||||
local function sign(sk, message)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
-- SHA-256, HMAC and PBKDF2 functions in ComputerCraft
|
||||
-- By Anavrins
|
||||
|
||||
local mod32 = 2^32
|
||||
local bit = _G.bit
|
||||
local os = _G.os
|
||||
local mod32 = 2^32
|
||||
local band = bit32 and bit32.band or bit.band
|
||||
local bnot = bit32 and bit32.bnot or bit.bnot
|
||||
local bxor = bit32 and bit32.bxor or bit.bxor
|
||||
@@ -38,7 +40,7 @@ local function counter(incr)
|
||||
local t1, t2 = 0, 0
|
||||
if 0xFFFFFFFF - t1 < incr then
|
||||
t2 = t2 + 1
|
||||
t1 = incr - (0xFFFFFFFF - t1) - 1
|
||||
t1 = incr - (0xFFFFFFFF - t1) - 1
|
||||
else t1 = t1 + incr
|
||||
end
|
||||
return t2, t1
|
||||
@@ -66,7 +68,7 @@ end
|
||||
|
||||
local function digestblock(w, C)
|
||||
for j = 17, 64 do
|
||||
local v = w[j-15]
|
||||
-- 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))
|
||||
w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod32
|
||||
@@ -95,7 +97,7 @@ end
|
||||
local mt = {
|
||||
__tostring = function(a) return string.char(unpack(a)) end,
|
||||
__index = {
|
||||
toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
|
||||
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
|
||||
@@ -120,7 +122,7 @@ local function toBytes(t, n)
|
||||
end
|
||||
|
||||
local function digest(data)
|
||||
local data = data or ""
|
||||
data = data or ""
|
||||
data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
|
||||
|
||||
data = preprocess(data)
|
||||
@@ -130,8 +132,8 @@ local function digest(data)
|
||||
end
|
||||
|
||||
local function hmac(data, key)
|
||||
local data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
|
||||
local key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
|
||||
data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
|
||||
key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
|
||||
|
||||
local blocksize = 64
|
||||
|
||||
@@ -161,9 +163,9 @@ local function hmac(data, key)
|
||||
end
|
||||
|
||||
local function pbkdf2(pass, salt, iter, dklen)
|
||||
local salt = type(salt) == "table" and salt or {tostring(salt):byte(1,-1)}
|
||||
salt = type(salt) == "table" and salt or {tostring(salt):byte(1,-1)}
|
||||
local hashlen = 32
|
||||
local dklen = dklen or 32
|
||||
dklen = dklen or 32
|
||||
local block = 1
|
||||
local out = {}
|
||||
|
||||
@@ -190,8 +192,13 @@ local function pbkdf2(pass, salt, iter, dklen)
|
||||
return setmetatable(out, mt)
|
||||
end
|
||||
|
||||
local function compute(data)
|
||||
return digest(data):toHex()
|
||||
end
|
||||
|
||||
return {
|
||||
digest = digest,
|
||||
compute = compute,
|
||||
hmac = hmac,
|
||||
pbkdf2 = pbkdf2,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user