ppad-poly1305 0.2.0 → 0.3.0
raw patch · 5 files changed
+29/−16 lines, 5 files
Files
- CHANGELOG +4/−0
- bench/Main.hs +14/−4
- lib/Crypto/MAC/Poly1305.hs +5/−6
- ppad-poly1305.cabal +1/−1
- test/Main.hs +5/−5
CHANGELOG view
@@ -1,5 +1,9 @@ # Changelog +- 0.3.0 (2025-06-21)+ * The 'mac' function is now total, returning 'Nothing' when supplied+ with an invalid-length one-time key.+ - 0.2.0 (2025-03-10) * Fix a bug in which small produced MAC's were not being padded to 128 bits.
bench/Main.hs view
@@ -18,13 +18,23 @@ msg = fromJust . B16.decode $ "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e" -key :: BS.ByteString-key = fromJust . B16.decode $- "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"+key_small :: BS.ByteString+key_small = fromJust . B16.decode $+ "0000000000000000000000000000000000000000000000000000000000000003" +key_mid :: BS.ByteString+key_mid = fromJust . B16.decode $+ "8888888888888888888888888888888888888888888888888888888888888883"++key_big :: BS.ByteString+key_big = fromJust . B16.decode $+ "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3"+ suite :: Benchmark suite = bgroup "ppad-poly1305" [- bench "mac" $ nf (Poly1305.mac key) msg+ bench "mac (small key)" $ nf (Poly1305.mac key_small) msg+ , bench "mac (mid key)" $ nf (Poly1305.mac key_mid) msg+ , bench "mac (big key)" $ nf (Poly1305.mac key_big) msg ]
lib/Crypto/MAC/Poly1305.hs view
@@ -64,20 +64,19 @@ -- Per RFC8439: the key, which is essentially a /one-time/ key, should -- be unique, and MUST be unpredictable for each invocation. ----- The key must be exactly 256 bits in length. Providing an invalid--- key will cause the function to throw an ErrorCall exception.+-- The key must be exactly 256 bits in length. -- -- >>> mac "i'll never use this key again!!!" "a message needing authentication"--- "O'\231Z\224\149\148\246\203[}\210\203\b\200\207"+-- Just "O'\231Z\224\149\148\246\203[}\210\203\b\200\207" mac :: BS.ByteString -- ^ 256-bit one-time key -> BS.ByteString -- ^ arbitrary-length message- -> BS.ByteString -- ^ 128-bit message authentication code+ -> Maybe BS.ByteString -- ^ 128-bit message authentication code mac key@(BI.PS _ _ kl) msg- | kl /= 32 = error "ppad-poly1305 (mac): invalid key"+ | kl /= 32 = Nothing | otherwise = let (clamp . _roll -> r, _roll -> s) = BS.splitAt 16 key- in _poly1305_loop r s msg+ in pure (_poly1305_loop r s msg) _poly1305_loop :: Integer -> Integer -> BS.ByteString -> BS.ByteString _poly1305_loop !r !s !msg =
ppad-poly1305.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: ppad-poly1305-version: 0.2.0+version: 0.3.0 synopsis: A pure Poly1305 MAC license: MIT license-file: LICENSE
test/Main.hs view
@@ -34,7 +34,7 @@ Just e = B16.decode "a8061dc1305136c6c22b8baf0c0127a9" - o = Poly1305.mac key msg+ Just o = Poly1305.mac key msg H.assertEqual mempty e o mac1 :: TestTree@@ -45,7 +45,7 @@ "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" Just tag = B16.decode "00000000000000000000000000000000"- out = Poly1305.mac key msg+ Just out = Poly1305.mac key msg H.assertEqual mempty tag out mac2 :: TestTree@@ -56,7 +56,7 @@ "416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f" Just tag = B16.decode "36e5f6b5c5e06070f0efca96227a863e"- out = Poly1305.mac key msg+ Just out = Poly1305.mac key msg H.assertEqual mempty tag out mac3 :: TestTree@@ -67,7 +67,7 @@ "416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f" Just tag = B16.decode "f3477e7cd95417af89a6b8794c310cf0"- out = Poly1305.mac key msg+ Just out = Poly1305.mac key msg H.assertEqual mempty tag out mac4 :: TestTree@@ -78,7 +78,7 @@ "2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e" Just tag = B16.decode "4541669a7eaaee61e708dc7cbcc5eb62"- out = Poly1305.mac key msg+ Just out = Poly1305.mac key msg H.assertEqual mempty tag out mac5 :: TestTree