ppad-chacha 0.1.0 → 0.2.0
raw patch · 5 files changed
+85/−14 lines, 5 filesdep +deepseqPVP ok
version bump matches the API change (PVP)
Dependencies added: deepseq
API changes (from Hackage documentation)
+ Crypto.Cipher.ChaCha20: instance GHC.Classes.Eq Crypto.Cipher.ChaCha20.Error
+ Crypto.Cipher.ChaCha20: instance GHC.Show.Show Crypto.Cipher.ChaCha20.Error
- Crypto.Cipher.ChaCha20: block :: ByteString -> Word32 -> ByteString -> ByteString
+ Crypto.Cipher.ChaCha20: block :: ByteString -> Word32 -> ByteString -> Either Error ByteString
- Crypto.Cipher.ChaCha20: cipher :: ByteString -> Word32 -> ByteString -> ByteString -> ByteString
+ Crypto.Cipher.ChaCha20: cipher :: ByteString -> Word32 -> ByteString -> ByteString -> Either Error ByteString
Files
- CHANGELOG +5/−0
- bench/Main.hs +9/−0
- lib/Crypto/Cipher/ChaCha20.hs +20/−12
- ppad-chacha.cabal +2/−1
- test/Main.hs +49/−1
CHANGELOG view
@@ -1,5 +1,10 @@ # Changelog +- 0.2.0 (2025-06-21)+ * Both the ChaCha block function and stream cipher are now total,+ returning values of 'Left InvalidKey' or 'Left InvalidNonce' when+ supplied with the corresponding bad inputs.+ - 0.1.0 (2025-03-09) * Initial release, supporting the chacha20 stream cipher and block function.
bench/Main.hs view
@@ -1,13 +1,22 @@+{-# OPTIONS_GHC -fno-warn-orphans #-} {-# LANGUAGE BangPatterns #-}+{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE StandaloneDeriving #-} module Main where +import Control.DeepSeq import Criterion.Main import qualified Crypto.Cipher.ChaCha20 as ChaCha20 import qualified Data.ByteString as BS import qualified Data.ByteString.Base16 as B16 import Data.Maybe (fromJust)+import GHC.Generics++deriving instance Generic ChaCha20.Error++instance NFData ChaCha20.Error main :: IO () main = defaultMain [
lib/Crypto/Cipher/ChaCha20.hs view
@@ -20,6 +20,9 @@ -- * ChaCha20 block function , block + -- * Error information+ , Error(..)+ -- testing , ChaCha(..) , _chacha@@ -163,7 +166,7 @@ !(WSPair k7 t7) = unsafe_parseWsPair t6 in if BS.null t7 then Key {..}- else error "ppad-chacha (_parse_key): bytes remaining"+ else error "ppad-chacha (_parse_key): internal error, bytes remaining" data Nonce = Nonce { n0 :: {-# UNPACK #-} !Word32@@ -180,7 +183,7 @@ !(WSPair n2 t2) = unsafe_parseWsPair t1 in if BS.null t2 then Nonce {..}- else error "ppad-chacha (_parse_nonce): bytes remaining"+ else error "ppad-chacha (_parse_nonce): internal error, bytes remaining" -- chacha20 block function ---------------------------------------------------- @@ -266,6 +269,11 @@ PA.writePrimArray s idx (iv + sv) serialize state +data Error =+ InvalidKey+ | InvalidNonce+ deriving (Eq, Show)+ -- RFC8439 2.3 -- | The ChaCha20 block function. Useful for generating a keystream.@@ -276,11 +284,11 @@ :: BS.ByteString -- ^ 256-bit key -> Word32 -- ^ 32-bit counter -> BS.ByteString -- ^ 96-bit nonce- -> BS.ByteString -- ^ 512-bit keystream+ -> Either Error BS.ByteString -- ^ 512-bit keystream block key@(BI.PS _ _ kl) counter nonce@(BI.PS _ _ nl)- | kl /= 32 = error "ppad-chacha (block): invalid key"- | nl /= 12 = error "ppad-chacha (block): invalid nonce"- | otherwise = runST $ do+ | kl /= 32 = Left InvalidKey+ | nl /= 12 = Left InvalidNonce+ | otherwise = pure $ runST $ do let k = _parse_key key n = _parse_nonce nonce state@(ChaCha s) <- _chacha k counter n@@ -324,17 +332,17 @@ -- >>> cip -- "\192*c\248A\204\211n\130y8\197\146k\245\178Y\197=\180_\223\138\146:^\206\&0\v[\201" -- >>> cipher key 1 non cip--- "but you can share the plaintext"+-- Right "but you can share the plaintext" cipher :: BS.ByteString -- ^ 256-bit key -> Word32 -- ^ 32-bit counter -> BS.ByteString -- ^ 96-bit nonce -> BS.ByteString -- ^ arbitrary-length plaintext- -> BS.ByteString -- ^ ciphertext+ -> Either Error BS.ByteString -- ^ ciphertext cipher raw_key@(BI.PS _ _ kl) counter raw_nonce@(BI.PS _ _ nl) plaintext- | kl /= 32 = error "ppad-chacha (cipher): invalid key"- | nl /= 12 = error "ppad-chacha (cipher): invalid nonce"- | otherwise = runST $ do+ | kl /= 32 = Left InvalidKey+ | nl /= 12 = Left InvalidNonce+ | otherwise = pure $ runST $ do let key = _parse_key raw_key non = _parse_nonce raw_nonce _cipher key counter non plaintext@@ -352,7 +360,7 @@ let loop acc !j bs = case BS.splitAt 64 bs of (chunk@(BI.PS _ _ l), etc)- | l == 0 && BS.length etc == 0 -> pure $+ | l == 0 && BS.length etc == 0 -> pure $ -- XX BS.toStrict (BSB.toLazyByteString acc) | otherwise -> do PA.copyMutablePrimArray s 0 initial 0 16
ppad-chacha.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: ppad-chacha-version: 0.1.0+version: 0.2.0 synopsis: A pure ChaCha20 stream cipher license: MIT license-file: LICENSE@@ -60,6 +60,7 @@ base , bytestring , criterion+ , deepseq , ppad-base16 , ppad-chacha
test/Main.hs view
@@ -1,3 +1,4 @@+{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE OverloadedStrings #-}@@ -22,6 +23,9 @@ , chacha20_block_init , chacha20_rounds , encrypt+ , crypt1+ , crypt2+ , crypt3 ] quarter :: TestTree@@ -111,6 +115,50 @@ encrypt :: TestTree encrypt = H.testCase "chacha20 encrypt" $ do- let o = ChaCha.cipher block_key 1 crypt_non crypt_plain+ let Right o = ChaCha.cipher block_key 1 crypt_non crypt_plain H.assertEqual mempty crypt_cip o++-- additional vectors++crypt1 :: TestTree+crypt1 = H.testCase "chacha20 encrypt (A.2 #1)" $ do+ let key = fromJust . B16.decode $+ "0000000000000000000000000000000000000000000000000000000000000000"+ non = fromJust . B16.decode $+ "000000000000000000000000"+ con = 0+ plain = fromJust . B16.decode $+ "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"+ cip = fromJust . B16.decode $+ "76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"+ Right out = ChaCha.cipher key con non plain+ H.assertEqual mempty cip out++crypt2 :: TestTree+crypt2 = H.testCase "chacha20 encrypt (A.2 #2)" $ do+ let key = fromJust . B16.decode $+ "0000000000000000000000000000000000000000000000000000000000000001"+ non = fromJust . B16.decode $+ "000000000000000000000002"+ con = 1+ plain = fromJust . B16.decode $+ "416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f"+ cip = fromJust . B16.decode $+ "a3fbf07df3fa2fde4f376ca23e82737041605d9f4f4f57bd8cff2c1d4b7955ec2a97948bd3722915c8f3d337f7d370050e9e96d647b7c39f56e031ca5eb6250d4042e02785ececfa4b4bb5e8ead0440e20b6e8db09d881a7c6132f420e52795042bdfa7773d8a9051447b3291ce1411c680465552aa6c405b7764d5e87bea85ad00f8449ed8f72d0d662ab052691ca66424bc86d2df80ea41f43abf937d3259dc4b2d0dfb48a6c9139ddd7f76966e928e635553ba76c5c879d7b35d49eb2e62b0871cdac638939e25e8a1e0ef9d5280fa8ca328b351c3c765989cbcf3daa8b6ccc3aaf9f3979c92b3720fc88dc95ed84a1be059c6499b9fda236e7e818b04b0bc39c1e876b193bfe5569753f88128cc08aaa9b63d1a16f80ef2554d7189c411f5869ca52c5b83fa36ff216b9c1d30062bebcfd2dc5bce0911934fda79a86f6e698ced759c3ff9b6477338f3da4f9cd8514ea9982ccafb341b2384dd902f3d1ab7ac61dd29c6f21ba5b862f3730e37cfdc4fd806c22f221"+ Right out = ChaCha.cipher key con non plain+ H.assertEqual mempty cip out++crypt3 :: TestTree+crypt3 = H.testCase "chacha20 encrypt (A.2 #3)" $ do+ let key = fromJust . B16.decode $+ "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0"+ non = fromJust . B16.decode $+ "000000000000000000000002"+ con = 42+ plain = fromJust . B16.decode $+ "2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e"+ cip = fromJust . B16.decode $+ "62e6347f95ed87a45ffae7426f27a1df5fb69110044c0d73118effa95b01e5cf166d3df2d721caf9b21e5fb14c616871fd84c54f9d65b283196c7fe4f60553ebf39c6402c42234e32a356b3e764312a61a5532055716ead6962568f87d3f3f7704c6a8d1bcd1bf4d50d6154b6da731b187b58dfd728afa36757a797ac188d1"+ Right out = ChaCha.cipher key con non plain+ H.assertEqual mempty cip out