diff --git a/.vscode/settings.json b/.vscode/settings.json index eb1af71..38f16d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,42 +9,10 @@ "redefined-local": "Warning" }, "Lua.diagnostics.globals": [ - "printError", - "sleep", - "read", - "write", "print", - "colours", - "colors", - "commands", - "disk", - "fs", - "gps", - "help", - "http", - "paintutils", - "parallel", - "peripheral", - "rednet", - "redstone", - "keys", - "settings", - "shell", - "multishell", - "term", - "textutils", - "turtle", - "pocket", - "vector", - "bit32", - "window", - "_CC_DEFAULT_SETTINGS", - "_HOST", "_VERSION", "_", - "_ENV", - "ccemux", - "wordlib" + "_ENV" ], - "Lua.runtime.version": "Lua 5.1" + "Lua.runtime.version": "Lua 5.2" } diff --git a/README.md b/README.md index c29fbc9..963ca46 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # CCryptoLib -An integrated collection of cryptographic primitives written in Lua using the ComputerCraft system API. +An integrated collection of cryptographic primitives written in Lua using the ComputerCraft system API. This is a fork for [Phoenix](https://phoenix.madefor.cc). Requires libsystem. ## Initializing the Random Number Generator All functions that take secret input may query the library's random generator, @@ -11,17 +11,27 @@ If you trust the tmpim Krist node, you can fetch a socket token and use it for initialization: ```lua local random = require "ccryptolib.random" +local network = require "system.network" -- Fetch a WebSocket token. -local postHandle = assert(http.post("https://krist.dev/ws/start", "")) -local data = textutils.unserializeJSON(postHandle.readAll()) -postHandle.close() +local postHandle = assert(network.post("https://krist.dev/ws/start", "")) +local data = textutils.unserializeJSON(postHandle:read("*a")) +postHandle:close() -- Initialize the generator using the given URL. random.init(data.url) -- Be polite and actually open the socket too. -http.websocket(data.url).close() +network.connect(data.url):close() +``` + +On CraftOS-PC or CCEmuX, you can use the built-in `nano` clock for relatively +decent entropy (this will not work in-game!): +```lua +local random = require "ccryptolib.random" + +-- Initialize the generator using nanoseconds. +random.init(os.time("nano")) ``` Otherwise, you will need to find another high-quality random entropy source to diff --git a/ccryptolib/aead.lua b/ccryptolib/aead.lua index 7fce3c1..3e04fe8 100644 --- a/ccryptolib/aead.lua +++ b/ccryptolib/aead.lua @@ -1,6 +1,6 @@ --- The ChaCha20Poly1305AEAD authenticated encryption with associated data (AEAD) construction. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local packing = require "ccryptolib.internal.packing" local chacha20 = require "ccryptolib.chacha20" diff --git a/ccryptolib/blake3.lua b/ccryptolib/blake3.lua index 54a3e84..59c7ac2 100644 --- a/ccryptolib/blake3.lua +++ b/ccryptolib/blake3.lua @@ -1,6 +1,6 @@ --- The BLAKE3 cryptographic hash function. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local packing = require "ccryptolib.internal.packing" diff --git a/ccryptolib/chacha20.lua b/ccryptolib/chacha20.lua index fdc51d0..d0defb1 100644 --- a/ccryptolib/chacha20.lua +++ b/ccryptolib/chacha20.lua @@ -1,6 +1,6 @@ --- The ChaCha20 stream cipher. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local packing = require "ccryptolib.internal.packing" diff --git a/ccryptolib/ed25519.lua b/ccryptolib/ed25519.lua index 3ca677c..171e50b 100644 --- a/ccryptolib/ed25519.lua +++ b/ccryptolib/ed25519.lua @@ -1,6 +1,6 @@ --- The Ed25519 digital signature scheme. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local fq = require "ccryptolib.internal.fq" local sha512 = require "ccryptolib.internal.sha512" diff --git a/ccryptolib/internal/sha512.lua b/ccryptolib/internal/sha512.lua index 9a4eb33..7f44ae4 100644 --- a/ccryptolib/internal/sha512.lua +++ b/ccryptolib/internal/sha512.lua @@ -1,6 +1,6 @@ --- The SHA512 cryptographic hash function. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local packing = require "ccryptolib.internal.packing" local shl = bit32.lshift diff --git a/ccryptolib/poly1305.lua b/ccryptolib/poly1305.lua index 123aad4..d5372b4 100644 --- a/ccryptolib/poly1305.lua +++ b/ccryptolib/poly1305.lua @@ -1,6 +1,6 @@ --- The Poly1305 one-time authenticator. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local packing = require "ccryptolib.internal.packing" diff --git a/ccryptolib/random.lua b/ccryptolib/random.lua index 797a152..3d1659d 100644 --- a/ccryptolib/random.lua +++ b/ccryptolib/random.lua @@ -1,4 +1,4 @@ -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local blake3 = require "ccryptolib.blake3" local chacha20 = require "ccryptolib.chacha20" local util = require "ccryptolib.internal.util" @@ -8,8 +8,8 @@ local lassert = util.lassert -- Extract local context. local ctx = { "ccryptolib 2023-04-11T19:43Z random.lua initialization context", - os.epoch("utc"), - os.epoch("ingame"), + os.time() * 1000, + os.time("ingame") * 1000, math.random(0, 2 ^ 24 - 1), math.random(0, 2 ^ 24 - 1), tostring({}), diff --git a/ccryptolib/sha256.lua b/ccryptolib/sha256.lua index eca0f97..7ab95e9 100644 --- a/ccryptolib/sha256.lua +++ b/ccryptolib/sha256.lua @@ -1,6 +1,6 @@ --- The SHA256 cryptographic hash function. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local packing = require "ccryptolib.internal.packing" diff --git a/ccryptolib/util.lua b/ccryptolib/util.lua index 0f2df8e..407e059 100644 --- a/ccryptolib/util.lua +++ b/ccryptolib/util.lua @@ -1,6 +1,6 @@ --- General utilities for handling byte strings. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local random = require "cryptolib.random" local poly1305 = require "ccryptolib.poly1305" diff --git a/ccryptolib/x25519.lua b/ccryptolib/x25519.lua index 46212f0..97c6351 100644 --- a/ccryptolib/x25519.lua +++ b/ccryptolib/x25519.lua @@ -1,6 +1,6 @@ --- The X25519 key exchange scheme. -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local util = require "ccryptolib.internal.util" local c25 = require "ccryptolib.internal.curve25519" diff --git a/ccryptolib/x25519c.lua b/ccryptolib/x25519c.lua index 214bdbc..f2009cd 100644 --- a/ccryptolib/x25519c.lua +++ b/ccryptolib/x25519c.lua @@ -1,4 +1,4 @@ -local expect = require "cc.expect".expect +local expect = require "system.expect".expect local lassert = require "ccryptolib.internal.util".lassert local fq = require "ccryptolib.internal.fq" local fp = require "ccryptolib.internal.fp" diff --git a/profile.lua b/profile.lua index b893a25..c929336 100644 --- a/profile.lua +++ b/profile.lua @@ -1,23 +1,20 @@ local PROFILE_TIME_MS = 2000 local function profile(fmt, fun, coeff) - local tStart = os.epoch("utc") + local tStart = os.time() local sum = 0 local count = 0 repeat - local t0 = os.epoch("utc") + local t0 = os.time() fun() - local t1 = os.epoch("utc") + local t1 = os.time() sum = sum + t1 - t0 count = count + 1 if count ~= 1 then - local x, y = term.getCursorPos() - term.setCursorPos(1, y - 1) - term.clearLine() + io.write("\x1b[F\x1b[K") end print(fmt:format(coeff * count / sum)) until t1 - tStart > PROFILE_TIME_MS - sleep() end local random = require "ccryptolib.random" diff --git a/spec/sha256_spec.lua b/spec/sha256_spec.lua index 1a70799..8683f03 100644 --- a/spec/sha256_spec.lua +++ b/spec/sha256_spec.lua @@ -20,7 +20,6 @@ describe("sha256.digest", function() local msg = util.hexcat { shortMsg[i].msg } local md = util.hexcat { shortMsg[i].md } expect(sha256.digest(msg)):eq(md) - sleep() end end) @@ -29,7 +28,6 @@ describe("sha256.digest", function() local msg = util.hexcat { longMsg[i].msg } local md = util.hexcat { longMsg[i].md } expect(sha256.digest(msg)):eq(md) - sleep() end end) @@ -44,7 +42,6 @@ describe("sha256.digest", function() md0, md1, md2 = md1, md2, sha256.digest(md0 .. md1 .. md2) end seed = md2 - sleep() end local out = util.hexcat { diff --git a/spec/sha512_spec.lua b/spec/sha512_spec.lua index acb37fe..adb56e6 100644 --- a/spec/sha512_spec.lua +++ b/spec/sha512_spec.lua @@ -20,7 +20,6 @@ describe("sha512.digest", function() local msg = util.hexcat { shortMsg[i].msg } local md = util.hexcat { shortMsg[i].md } expect(sha512.digest(msg)):eq(md) - sleep() end end) @@ -29,7 +28,6 @@ describe("sha512.digest", function() local msg = util.hexcat { longMsg[i].msg } local md = util.hexcat { longMsg[i].md } expect(sha512.digest(msg)):eq(md) - sleep() end end) @@ -45,7 +43,6 @@ describe("sha512.digest", function() md0, md1, md2 = md1, md2, sha512.digest(md0 .. md1 .. md2) end seed = md2 - sleep() end local out = util.hexcat { diff --git a/spec/x25519_spec.lua b/spec/x25519_spec.lua index c3909a1..0c48e06 100644 --- a/spec/x25519_spec.lua +++ b/spec/x25519_spec.lua @@ -55,7 +55,6 @@ describe("x25519.exchange", function() for _ = 1, 1000 do k, u = x25519.exchange(k, u), k - sleep() end local k1000 = util.hexcat { diff --git a/spec/x25519c_spec.lua b/spec/x25519c_spec.lua index 308e349..362c0f5 100644 --- a/spec/x25519c_spec.lua +++ b/spec/x25519c_spec.lua @@ -61,7 +61,6 @@ describe("x25519c._EXPERIMENTAL_exchangeX", function() for _ = 1, 1000 do k, u = exchange(k, u), k - sleep() end local k1000 = util.hexcat {