Make BLAKE3 stateful
This commit is contained in:
parent
db4c272aea
commit
b9457e9dd5
181
blake3.lua
181
blake3.lua
|
@ -118,90 +118,164 @@ local function merge(cvl, cvr)
|
||||||
return cvl
|
return cvl
|
||||||
end
|
end
|
||||||
|
|
||||||
local function blake3(iv, flags, msg, len)
|
local function update(state, message)
|
||||||
-- Set up the state.
|
expect(1, state, "table")
|
||||||
local stateCvs = {}
|
expect(1, message, "string")
|
||||||
local stateCv = iv
|
|
||||||
local stateT = 0
|
-- Append to buffer.
|
||||||
local stateN = 0
|
state.m = state.m .. message
|
||||||
local stateStart = CHUNK_START
|
|
||||||
local stateEnd = 0
|
-- Split off complete blocks.
|
||||||
|
local blockslen = #state.m - (#state.m - 1) % 64 - 1
|
||||||
|
local blocks = state.m:sub(1, blockslen)
|
||||||
|
state.m = state.m:sub(1 + blockslen)
|
||||||
|
|
||||||
-- Digest complete blocks.
|
-- Digest complete blocks.
|
||||||
for i = 1, #msg - 64, 64 do
|
for i = 1, #blocks, 64 do
|
||||||
-- Compress the block.
|
-- Compress the block.
|
||||||
local block = {("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4"):unpack(msg, i)}
|
local block = {("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4"):unpack(blocks, i)}
|
||||||
local stateFlags = flags + stateStart + stateEnd
|
local stateFlags = state.f + state.s + state.e
|
||||||
stateCv = compress(stateCv, block, stateT, 64, stateFlags)
|
state.cv = compress(state.cv, block, state.t, 64, stateFlags)
|
||||||
stateStart = 0
|
state.s = 0
|
||||||
stateN = stateN + 1
|
state.n = state.n + 1
|
||||||
|
|
||||||
if stateN == 15 then
|
if state.n == 15 then
|
||||||
-- Last block in chunk.
|
-- Last block in chunk.
|
||||||
stateEnd = CHUNK_END
|
state.e = CHUNK_END
|
||||||
elseif stateN == 16 then
|
elseif state.n == 16 then
|
||||||
-- Chunk complete, merge.
|
-- Chunk complete, merge.
|
||||||
local mergeCv = stateCv
|
local mergeCv = state.cv
|
||||||
local mergeAmt = stateT + 1
|
local mergeAmt = state.t + 1
|
||||||
while mergeAmt % 2 == 0 do
|
while mergeAmt % 2 == 0 do
|
||||||
local block = merge(table.remove(stateCvs), mergeCv)
|
local block = merge(table.remove(state.cvs), mergeCv)
|
||||||
mergeCv = compress(iv, block, 0, 64, flags + PARENT)
|
mergeCv = compress(state.iv, block, 0, 64, state.f + PARENT)
|
||||||
mergeAmt = mergeAmt / 2
|
mergeAmt = mergeAmt / 2
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Push back.
|
-- Push back.
|
||||||
table.insert(stateCvs, mergeCv)
|
table.insert(state.cvs, mergeCv)
|
||||||
|
|
||||||
-- Update state back to next chunk.
|
-- Update state back to next chunk.
|
||||||
stateCv = iv
|
state.cv = state.iv
|
||||||
stateT = stateT + 1
|
state.t = state.t + 1
|
||||||
stateN = 0
|
state.n = 0
|
||||||
stateStart = CHUNK_START
|
state.s = CHUNK_START
|
||||||
stateEnd = 0
|
state.e = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return state
|
||||||
|
end
|
||||||
|
|
||||||
|
local function finalize(state)
|
||||||
-- Pad the last message block.
|
-- Pad the last message block.
|
||||||
local lastLen = #msg == 0 and 0 or (#msg - 1) % 64 + 1
|
local lastLen = #state.m
|
||||||
local padded = msg:sub(-lastLen) .. ("\0"):rep(64)
|
local padded = state.m .. ("\0"):rep(64)
|
||||||
local last = {("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4"):unpack(padded)}
|
local last = {("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4"):unpack(padded)}
|
||||||
|
|
||||||
-- Prepare output expansion state.
|
-- Prepare output expansion state.
|
||||||
local outCv, outBlock, outLen, outFlags
|
if state.t > 0 then
|
||||||
if stateT > 0 then
|
|
||||||
-- Root is a parent, digest last block now and merge parents.
|
-- Root is a parent, digest last block now and merge parents.
|
||||||
local stateFlags = flags + stateStart + CHUNK_END
|
local stateFlags = state.f + state.s + CHUNK_END
|
||||||
local mergeCv = compress(stateCv, last, stateT, lastLen, stateFlags)
|
local mergeCv = compress(state.cv, last, state.t, lastLen, stateFlags)
|
||||||
for i = #stateCvs, 2, -1 do
|
for i = #state.cvs, 2, -1 do
|
||||||
local block = merge(stateCvs[i], mergeCv)
|
local block = merge({unpack(state.cvs[i])}, mergeCv)
|
||||||
mergeCv = compress(iv, block, 0, 64, flags + PARENT)
|
mergeCv = compress(state.iv, block, 0, 64, state.f + PARENT)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Set output state.
|
-- Set output state.
|
||||||
outCv = iv
|
return {
|
||||||
outBlock = merge(stateCvs[1], mergeCv)
|
expand = expand,
|
||||||
outLen = 64
|
cv = {unpack(state.iv)},
|
||||||
outFlags = flags + ROOT + PARENT
|
m = merge({unpack(state.cvs[1])}, mergeCv),
|
||||||
|
n = 64,
|
||||||
|
f = state.f + ROOT + PARENT,
|
||||||
|
}
|
||||||
else
|
else
|
||||||
-- Root block is in the first chunk, set output state.
|
-- Root is in the first chunk, set output state.
|
||||||
outCv = stateCv
|
return {
|
||||||
outBlock = last
|
expand = expand,
|
||||||
outLen = lastLen
|
cv = {unpack(state.cv)},
|
||||||
outFlags = flags + stateStart + CHUNK_END + ROOT
|
m = last,
|
||||||
|
n = lastLen,
|
||||||
|
f = state.f + state.s + CHUNK_END + ROOT,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function expand(out, len, offset)
|
||||||
|
expect(1, out, "table")
|
||||||
|
expect(1, len, "number")
|
||||||
|
expect(2, offset, "nil", "number")
|
||||||
|
offset = offset or 0
|
||||||
|
|
||||||
-- Expand output.
|
-- Expand output.
|
||||||
local out = {}
|
local out = {}
|
||||||
for i = 0, len / 64 do
|
for i = 0, len / 64 do
|
||||||
local md = compress(outCv, outBlock, i, outLen, outFlags, true)
|
local n = offset + i
|
||||||
|
local md = compress(out.cv, out.m, n, out.n, out.f, true)
|
||||||
out[i + 1] = ("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4"):pack(unpack(md))
|
out[i + 1] = ("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4"):pack(unpack(md))
|
||||||
end
|
end
|
||||||
|
|
||||||
return table.concat(out):sub(1, len)
|
return table.concat(out):sub(1, len)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function copy(state)
|
||||||
|
-- Copy CV stack.
|
||||||
|
local cvs = {}
|
||||||
|
for i = 1, #state.cvs do cvs[i] = {unpack(state.cvs[i])} end
|
||||||
|
|
||||||
|
return {
|
||||||
|
update = update,
|
||||||
|
finalize = finalize,
|
||||||
|
copy = copy,
|
||||||
|
iv = {unpack(state.iv)},
|
||||||
|
cv = {unpack(state.cv)},
|
||||||
|
cvs = cvs,
|
||||||
|
m = state.m,
|
||||||
|
t = state.t,
|
||||||
|
n = state.n,
|
||||||
|
s = state.s,
|
||||||
|
e = state.e,
|
||||||
|
f = state.f,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local function new(iv, f)
|
||||||
|
return {
|
||||||
|
update = update,
|
||||||
|
finalize = finalize,
|
||||||
|
copy = copy,
|
||||||
|
iv = iv,
|
||||||
|
cv = iv,
|
||||||
|
cvs = {},
|
||||||
|
m = "",
|
||||||
|
t = 0,
|
||||||
|
n = 0,
|
||||||
|
s = CHUNK_START,
|
||||||
|
e = 0,
|
||||||
|
f = f,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
local mod = {}
|
local mod = {}
|
||||||
|
|
||||||
|
function mod.new()
|
||||||
|
return new({unpack(IV)}, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
function mod.newKeyed(key)
|
||||||
|
expect(1, key, "string")
|
||||||
|
assert(#key == 32, "key length must be 32")
|
||||||
|
return new({("<I4I4I4I4I4I4I4I4"):unpack(key)}, KEYED_HASH)
|
||||||
|
end
|
||||||
|
|
||||||
|
function mod.newDk(context)
|
||||||
|
expect(1, context, "string")
|
||||||
|
local iv = new(IV, DERIVE_KEY_CONTEXT):update(context):finalize():expand(32)
|
||||||
|
return new({("<I4I4I4I4I4I4I4I4"):unpack(iv)}, DERIVE_KEY_MATERIAL)
|
||||||
|
end
|
||||||
|
|
||||||
--- Hashes data using BLAKE3.
|
--- Hashes data using BLAKE3.
|
||||||
--
|
--
|
||||||
-- @tparam string message The input message.
|
-- @tparam string message The input message.
|
||||||
|
@ -213,8 +287,7 @@ function mod.digest(message, len)
|
||||||
expect(2, len, "number", "nil")
|
expect(2, len, "number", "nil")
|
||||||
len = len or 32
|
len = len or 32
|
||||||
assert(len % 1 == 0 and len >= 1, "length must be a positive integer")
|
assert(len % 1 == 0 and len >= 1, "length must be a positive integer")
|
||||||
|
return new(IV, 0):update(message):finalize():expand(len)
|
||||||
return blake3(IV, 0, message, len)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Performs a keyed hash.
|
--- Performs a keyed hash.
|
||||||
|
@ -231,8 +304,8 @@ function mod.digestKeyed(key, message, len)
|
||||||
expect(3, len, "number", "nil")
|
expect(3, len, "number", "nil")
|
||||||
len = len or 32
|
len = len or 32
|
||||||
assert(len % 1 == 0 and len >= 1, "length must be a positive integer")
|
assert(len % 1 == 0 and len >= 1, "length must be a positive integer")
|
||||||
|
local h = new({("<I4I4I4I4I4I4I4I4"):unpack(key)}, KEYED_HASH)
|
||||||
return blake3({("<I4I4I4I4I4I4I4I4"):unpack(key)}, KEYED_HASH, message, len)
|
return h:update(message):finalize():expand(len)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Makes a context-based key derivation function (KDF).
|
--- Makes a context-based key derivation function (KDF).
|
||||||
|
@ -242,17 +315,15 @@ end
|
||||||
--
|
--
|
||||||
function mod.deriveKey(context)
|
function mod.deriveKey(context)
|
||||||
expect(1, context, "string")
|
expect(1, context, "string")
|
||||||
|
local iv = new(IV, DERIVE_KEY_CONTEXT):update(context):finalize():expand(32)
|
||||||
local hash = blake3(IV, DERIVE_KEY_CONTEXT, context, 32)
|
|
||||||
local iv = {("<I4I4I4I4I4I4I4I4"):unpack(hash)}
|
|
||||||
|
|
||||||
return function(material, len)
|
return function(material, len)
|
||||||
expect(1, material, "string")
|
expect(1, material, "string")
|
||||||
expect(2, len, "number", "nil")
|
expect(2, len, "number", "nil")
|
||||||
len = len or 32
|
len = len or 32
|
||||||
assert(len % 1 == 0 and len >= 1, "length must be a positive integer")
|
assert(len % 1 == 0 and len >= 1, "length must be a positive integer")
|
||||||
|
local h = new({("<I4I4I4I4I4I4I4I4"):unpack(iv)}, DERIVE_KEY_MATERIAL)
|
||||||
return blake3(iv, DERIVE_KEY_MATERIAL, material, len)
|
return h:update(material):finalize():expand(len)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue