commit a1f7f23d728804d317b693f1aa9a460753847a8d parent 6cb8fb530c5f4780113b12798626908f01811954 Author: Jared Tobin <jared@jtobin.io> Date: Mon, 10 Mar 2025 09:51:10 +0400 lib: poly1305_key_gen Diffstat:
M | lib/Crypto/AEAD/ChaCha20Poly1305.hs | | | 12 | ++++++++++++ |
M | test/Main.hs | | | 27 | ++++++++++++++++++++++++++- |
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/lib/Crypto/AEAD/ChaCha20Poly1305.hs b/lib/Crypto/AEAD/ChaCha20Poly1305.hs @@ -1,4 +1,16 @@ +{-# LANGUAGE OverloadedStrings #-} module Crypto.AEAD.ChaCha20Poly1305 where +import qualified Data.ByteString as BS +import qualified Data.ByteString.Internal as BI +import qualified Crypto.Cipher.ChaCha20 as ChaCha20 +import qualified Crypto.MAC.Poly1305 as Poly1305 +poly1305_key_gen + :: BS.ByteString -- ^ 256-bit initial keying material + -> BS.ByteString -- ^ 96-bit nonce + -> BS.ByteString -- ^ 256-bit key (suitable for poly1305) +poly1305_key_gen key@(BI.PS _ _ l) nonce + | l /= 32 = error "ppad-aead (poly1305_key_gen): invalid key" + | otherwise = BS.take 32 (ChaCha20.block key 0 nonce) diff --git a/test/Main.hs b/test/Main.hs @@ -1,4 +1,29 @@ +{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE OverloadedStrings #-} + module Main where +import qualified Crypto.AEAD.ChaCha20Poly1305 as AEAD +import qualified Data.ByteString.Base16 as B16 +import Test.Tasty +import qualified Test.Tasty.HUnit as H + main :: IO () -main = pure () +main = defaultMain $ testGroup "ppad-aead" [ + poly1305_key_gen + ] + +poly1305_key_gen :: TestTree +poly1305_key_gen = H.testCase "poly1305_key_gen" $ do + let Just key = B16.decode + "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f" + Just non = B16.decode + "000000000001020304050607" + + Just e = B16.decode + "8ad5a08b905f81cc815040274ab29471a833b637e3fd0da508dbb8e2fdd1a646" + + o = AEAD.poly1305_key_gen key non + H.assertEqual mempty e o +