Skip to content

Commit

Permalink
Modified to work on Phoenix
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 authored Jun 13, 2023
1 parent 0a23090 commit 5b3ec96
Show file tree
Hide file tree
Showing 18 changed files with 34 additions and 67 deletions.
36 changes: 2 additions & 34 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/aead.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/blake3.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/chacha20.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/ed25519.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/internal/sha512.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/poly1305.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
6 changes: 3 additions & 3 deletions ccryptolib/random.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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({}),
Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/sha256.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/util.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/x25519.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion ccryptolib/x25519c.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 4 additions & 7 deletions profile.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 0 additions & 3 deletions spec/sha256_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand All @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions spec/sha512_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion spec/x25519_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion spec/x25519c_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5b3ec96

Please sign in to comment.