From 85fb0356413938db93b26bcb599d3cad09049fb9 Mon Sep 17 00:00:00 2001 From: Miguel Oliveira Date: Thu, 24 Mar 2022 10:03:19 -0300 Subject: [PATCH] Update Poly1305 Update Poly1305 to match the DJB approach for Fp arithmetic. This improves performance and correctness. Also fix wrong output when passing an empty string. --- poly1305.lua | 166 +++++++++++++++++++++++++-------------------------- 1 file changed, 81 insertions(+), 85 deletions(-) diff --git a/poly1305.lua b/poly1305.lua index c849afb..642c1fe 100644 --- a/poly1305.lua +++ b/poly1305.lua @@ -6,8 +6,6 @@ local expect = require "cc.expect".expect local random = require "ccryptolib.random" -local band = bit32.band - local mod = {} --- Computes a Poly1305 message authentication code. @@ -23,116 +21,114 @@ function mod.mac(key, message) -- Pad message. local pbplen = #message - 15 - if #message % 16 ~= 0 then + if #message % 16 ~= 0 or #message == 0 then message = message .. "\1" message = message .. ("\0"):rep(-#message % 16) end -- Decode r. - local r0, t1, r2, r3, t4, r5 = ("= 2 ^ 22 - 5 + if c7 == 0xffff * 2 ^ 112 + and c6 == 0xffff * 2 ^ 96 + and c5 == 0xffff * 2 ^ 80 + and c4 == 0xffff * 2 ^ 64 + and c3 == 0xffff * 2 ^ 48 + and c2 == 0xffff * 2 ^ 32 + and c1 == 0xffff * 2 ^ 16 + and c0 >= 0xfffa then - h5 = 0 - h4 = 0 - h3 = 0 - h2 = 0 - h1 = 0 - h0 = h0 - (2 ^ 22 - 5) + c7, c6, c5, c4, c3, c2, c1, c0 = 0, 0, 0, 0, 0, 0, 0, c0 - 0xfffa end -- Decode s. - local s0, s1, s2, s3, s4, s5 = ("