Skip to content

Commit

Permalink
Merge pull request #43 from cyberbit/feature/ccryptolib-1.2.1
Browse files Browse the repository at this point in the history
📦 update ccryptolib to 1.2.1
  • Loading branch information
cyberbit authored May 20, 2024
2 parents 195b32b + e1588f8 commit b861218
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/telem/vendor/ccryptolib/random.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ local lassert = util.lassert
local ctx = {
"ccryptolib 2023-04-11T19:43Z random.lua initialization context",
os.epoch("utc"),
os.epoch("ingame"),
os.day(),
os.time(),
math.random(0, 2 ^ 24 - 1),
math.random(0, 2 ^ 24 - 1),
tostring({}),
Expand All @@ -27,6 +28,55 @@ local function init(seed)
initialized = true
end

--- Returns whether the generator has been initialized or not.
--- @return boolean
local function isInit()
return initialized
end

--- Initializes the generator using VM instruction timing noise.
---
--- This function counts how many instructions the VM can execute within a single
--- millisecond, and mixes the lower bits of these values into the generator state.
--- The current implementation collects data for 512 ms and takes the lower 8 bits from
--- each count.
---
--- Compared to fetching entropy from a trusted web source, this approach is riskier but
--- more convenient. The factors that influence instruction timing suggest that this
--- seed is unpredictable for other players, but this assumption might turn out to be
--- untrue.
local function initWithTiming()
assert(os.epoch("utc") ~= 0)

local f = assert(load("local e=os.epoch return{" .. ("e'utc',"):rep(256) .. "}"))

do -- Warmup.
local t = f()
while t[256] - t[1] > 1 do t = f() end
end

-- Fill up the buffer.
local buf = {}
for i = 1, 512 do
local t = f()
while t[256] == t[1] do t = f() end
for j = 1, 256 do
if t[j] ~= t[1] then
buf[i] = j - 1
break
end
end
end

-- Perform a histogram check to catch faulty os.epoch implementations.
local hist = {}
for i = 0, 255 do hist[i] = 0 end
for i = 1, #buf do hist[buf[i]] = hist[buf[i]] + 1 end
for i = 0, 255 do assert(hist[i] < 20) end

init(string.char(table.unpack(buf)))
end

--- Mixes extra entropy into the generator state.
--- @param data string The additional entropy to mix.
local function mix(data)
Expand All @@ -49,6 +99,8 @@ end

return {
init = init,
isInit = isInit,
initWithTiming = initWithTiming,
mix = mix,
random = random,
}

0 comments on commit b861218

Please sign in to comment.