~rootmos/lua-hack

575e73f0cbdad2e5dfef1c21cbbb678b6feb11c0 — Gustav Behm 1 year, 2 months ago 67380af lamport-ext
Dump and load signatures
2 files changed, 145 insertions(+), 0 deletions(-)

M lamport/init.lua
M lamport/test.lua
M lamport/init.lua => lamport/init.lua +81 -0
@@ 57,6 57,7 @@ local function mk_key(iter, hash)

    key.id = key.pub.id
    key.dump = M.dump_key
    key.sign = M.sign

    return key
end


@@ 101,6 102,83 @@ function M.generate_key(hash, rng)
    return mk_key(f, hash)
end

function M.dump_sig(sig, f)
    if io.type(f) == "file" then
        for _, k in ipairs(sig) do
            f:write(k)
        end

        if sig.unused then
            for _, k in ipairs(sig.unused) do
                f:write(k)
            end
        end
    else
        local s = ""
        for _, k in ipairs(sig) do
            s = s .. k
        end

        if sig.unused then
            for _, k in ipairs(sig.unused) do
                s = s .. k
            end
        end

        return s
    end
end

local function mk_sig(iter, hash)
    local n = M.N(hash)

    local sig = {}
    for i = 1,8*n do
        sig[i] = iter()
    end

    for i = 1,8*n do
        local x = iter()
        if i == 1 then
            if x == nil then
                break
            else
                sig.unused = {}
            end
        end
        sig.unused[i] = x
    end

    sig.dump = M.dump_sig
    sig.verify = M.verify

    return sig
end

function M.load_sig(f, hash)
    local hash = hash or M.hash or default_hash()
    local n = M.N(hash)

    if io.type(f) == "file" then
        local function g()
            return f:read(n)
        end
        return mk_sig(g, hash)
    else
        local o = 0
        local N = #f
        local function g()
            if o == N then
                return nil
            end
            local k = f:sub(o + 1, o + n)
            o = o + n
            return k
        end
        return mk_sig(g, hash)
    end
end

local function little_endian_bits(bs)
    local N = #bs
    local i = 0


@@ 144,6 222,9 @@ function M.sign_hash(key, h, include_unused)
        sig.unused = unused
    end

    sig.dump = M.dump_sig
    sig.verify = M.verify

    return sig
end


M lamport/test.lua => lamport/test.lua +64 -0
@@ 83,6 83,7 @@ function test_sign_verify()
    local msg = F.bytestring()
    local sig = L.sign(key, msg)
    lu.assertTrue(L.verify(sig, key.pub, msg))
    lu.assertTrue(sig:verify(key.pub, msg))
end

function test_sign_verify_modified()


@@ 91,6 92,7 @@ function test_sign_verify_modified()
    local sig = L.sign(key, msg0)
    local msg1 = F.bytestring()
    lu.assertError(L.verify(sig, key.pub, msg0))
    lu.assertError(sig:verify(key.pub, msg0))
end

function test_sign_verify_using_keyid()


@@ 98,6 100,7 @@ function test_sign_verify_using_keyid()
    local msg = F.bytestring()
    local sig = L.sign(key, msg, true)
    lu.assertTrue(L.verify(sig, key.id, msg))
    lu.assertTrue(sig:verify(key.id, msg))
end

function test_sign_verify_using_keyid_modified()


@@ 106,6 109,67 @@ function test_sign_verify_using_keyid_modified()
    local sig = L.sign(key, msg0, true)
    local msg1 = F.bytestring()
    lu.assertError(L.verify(sig, key.id, msg0))
    lu.assertError(sig:verify(key.id, msg0))
end

function test_dump_sig()
    local key = L.generate_key()
    local msg = F.bytestring()
    local sig = L.sign(key, msg)
    lu.assertEquals(#sig:dump(), 8*L.N()*L.N())
    lu.assertEquals(sig:dump(), L.dump_sig(sig))
end

function test_dump_sig_using_keyid()
    local key = L.generate_key()
    local msg = F.bytestring()
    local sig = L.sign(key, msg, true)
    lu.assertEquals(#sig:dump(), 2*8*L.N()*L.N())
    lu.assertEquals(sig:dump(), L.dump_sig(sig))
end

function test_load_sig()
    local key = L.generate_key()
    local msg = F.bytestring()
    local s0 = key:sign(msg)
    local s1 = L.load_sig(s0:dump())
    lu.assertTrue(D.eq(s0, s1))
end

function test_load_sig_using_keyid()
    local key = L.generate_key()
    local msg = F.bytestring()
    local s0 = key:sign(msg, true)
    local s1 = L.load_sig(s0:dump())
    lu.assertTrue(D.eq(s0, s1))
end

function test_load_sig_file()
    local key = L.generate_key()
    local msg = F.bytestring()
    local s0 = key:sign(msg)

    local f = io.tmpfile()
    s0:dump(f)
    f:flush()
    f:seek("set")

    local s1 = L.load_sig(f)
    lu.assertTrue(D.eq(s0, s1))
end

function test_load_sig_file_using_keyid()
    local key = L.generate_key()
    local msg = F.bytestring()
    local s0 = key:sign(msg, true)

    local f = io.tmpfile()
    s0:dump(f)
    f:flush()
    f:seek("set")

    local s1 = L.load_sig(f)
    lu.assertTrue(D.eq(s0, s1))
end

os.exit(lu.LuaUnit.run())