--- The Poly1305 one-time authenticator. -- -- @module poly1305 -- local expect = require "cc.expect".expect local random = require "ccryptolib.random" local packing = require "ccryptolib.internal.packing" local u4x4, fmt4x4 = packing.compileUnpack("= 0xfffb then c7, c6, c5, c4, c3, c2, c1, c0 = 0, 0, 0, 0, 0, 0, 0, c0 - 0xfffb end -- Decode s. local s0, s1, s2, s3 = u4x4(fmt4x4, key, 17) -- Add. local t0 = s0 + c0 + c1 local u0 = t0 % 2 ^ 32 local t1 = t0 - u0 + s1 * 2 ^ 32 + c2 + c3 local u1 = t1 % 2 ^ 64 local t2 = t1 - u1 + s2 * 2 ^ 64 + c4 + c5 local u2 = t2 % 2 ^ 96 local t3 = t2 - u2 + s3 * 2 ^ 96 + c6 + c7 local u3 = t3 % 2 ^ 128 -- Encode. return p4x4(fmt4x4, u0, u1 / 2 ^ 32, u2 / 2 ^ 64, u3 / 2 ^ 96) end local mac = mod.mac --- Verifies a Poly1305 tag. -- -- @tparam string key The key used to generate the tag. -- @tparam string message The message to authenticate. -- @tparam string tag The tag to check. -- @treturn boolean Whether the tag is valid or not. -- function mod.verify(key, message, tag) local kaux = random.random(32) return mac(kaux, tag) == mac(kaux, mac(key, message)) end return mod