packages feed

sha-validation 0.1.0.0 → 0.1.0.1

raw patch · 8 files changed

+170/−58 lines, 8 filesdep ~bytestring

Dependency ranges changed: bytestring

Files

CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for the sha-validation package +## 0.1.0.1 -- 2024-07-06++*   Provide test tools for monte test files for SHA (SHA1 and SHA2).+ ## 0.1.0.0 -- 2022-08-20 -* First version. Released on an unsuspecting world.+*   First version. Released on an unsuspecting world.
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2022, Lars Kuhtz <lakuhtz@gmail.com>+Copyright (c) 2022-2024, Lars Kuhtz <lakuhtz@gmail.com>  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
README.md view
@@ -1,1 +1,4 @@ Haskel support for NIST Secure Hash Algorithm Validation Tests++For details about the provided tests visit the site of the+[Cryptographic Algorithm Validation Program (CAVP)](https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing).
sha-validation.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: sha-validation-version: 0.1.0.0+version: 0.1.0.1 synopsis: Validation SHA Implementations Description: NIST Secure Hash Algorithm Validation Tests homepage: https://github.com/larskuhtz/hs-sha-validation@@ -9,15 +9,18 @@ license-file: LICENSE author: Lars Kuhtz maintainer: lakuhtz@gmail.com-copyright: Copyright (c) 2022 Lars Kuhtz <lakuhtz@gmail.com>+copyright: Copyright (c) 2022-2024 Lars Kuhtz <lakuhtz@gmail.com> category: Data tested-with:-    GHC==9.2.0-    GHC==9.0.1-    GHC==8.10.7-extra-source-files:+    GHC==9.10+    GHC==9.8+    GHC==9.6+    GHC==9.4+    GHC==9.2+extra-doc-files:     README.md     CHANGELOG.md+extra-source-files:     data/sha-3bytetestvectors/*.rsp     data/shabytetestvectors/*.rsp     data/shakebytetestvectors/*.rsp@@ -36,7 +39,7 @@         Test.Hash.SHA     build-depends:         , base >=4.11 && <5-        , bytestring >=0.10+        , bytestring >=0.11.1         , vector >=0.12         , attoparsec >=0.14         , text >=1.1
src/Test/Hash/Internal.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveLift #-} {-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE ImportQualifiedPost #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE OverloadedStrings #-}@@ -12,9 +13,9 @@  -- | -- Module: Test.Hash.Internal--- Copyright: Copyright © 2022 Kadena LLC.+-- Copyright: Copyright © 2022-2024 Lars Kuhtz <lakuhtz@gmail.com> -- License: MIT--- Maintainer: Lars Kuhtz <lars@kadena.io>+-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com> -- Stability: experimental -- Description: Internal Definitions --@@ -55,8 +56,8 @@ -- * Test Tools , msgTest , msgAssert-, monteTest-, monteAssert+, monteTestInternal+, monteAssertInternal  -- * Internal: Embedding Response Files in Haskell Code , embedIO@@ -67,20 +68,20 @@ import Control.Monad  import Data.Attoparsec.Text.Lazy-import qualified Data.ByteString as B-import qualified Data.ByteString.Base16 as B16-import qualified Data.ByteString.Char8 as B8-import qualified Data.ByteString.Internal as B (ByteString(..))-import qualified Data.ByteString.Unsafe as B+import Data.ByteString qualified as B+import Data.ByteString.Base16 qualified as B16+import Data.ByteString.Char8 qualified as B8+import Data.ByteString.Internal qualified as B (ByteString(..))+import Data.ByteString.Unsafe qualified as B import Data.Foldable import Data.Functor-import qualified Data.List as L+import Data.List qualified as L import Data.Maybe-import qualified Data.Text as T-import qualified Data.Text.Encoding as T-import qualified Data.Text.Lazy as TL-import qualified Data.Text.Lazy.IO as TL-import qualified Data.Vector as V+import Data.Text qualified as T+import Data.Text.Encoding qualified as T+import Data.Text.Lazy qualified as TL+import Data.Text.Lazy.IO qualified as TL+import Data.Vector qualified as V  import Language.Haskell.TH import Language.Haskell.TH.Syntax@@ -515,30 +516,11 @@         $ B.take (fromIntegral (_msgLen v) `quot` 8)         $ _msgMsg v --- | Check that all test vectors in a Monte Carlo File are satisfied by a given hash--- implementation.----monteTest :: (B.ByteString -> B.ByteString) -> MonteFile -> Bool-monteTest hash f = go (_monteSeed f) (toList $ _monteVectors f)-  where-    go :: B.ByteString -> [MonteVector] -> Bool-    go _ [] = True-    go s ((_monteMd -> h) : t) = hashI 1000 s == h && go h t--    -- Each Round consists of 1000 hash applications-    hashI :: Natural -> B.ByteString -> B.ByteString-    hashI 0 s = s-    hashI i s = let s' = hash s in hashI (i - 1) s'---- | For a given hash implementation, assert the correct result for each test--- vector in a 'MonteFile'.------ The function to assert equality is usually provided by some testing--- framework.----monteAssert+monteAssertInternal     :: Monad m-    => (String -> B.ByteString -> B.ByteString -> m ())+    => Natural+        -- ^ seed multiplyer+    -> (String -> B.ByteString -> B.ByteString -> m ())         -- ^ Function to assertion Equality. The first argument is a test label,         -- the second argument is the actual value, and the thrid value is the         -- expected value.@@ -546,19 +528,33 @@         -- ^ Hash function     -> MonteFile     -> m ()-monteAssert assert hash f = go (_monteSeed f) (toList $ _monteVectors f)+monteAssertInternal seedMultiplyer assert hash f = go (_monteSeed f) (toList $ _monteVectors f)   where+    size = fromIntegral (_monteL f)+    multiplySeed = mconcat . replicate (fromIntegral seedMultiplyer)+     go _ [] = return ()     go s (v : t) = do-        let r = hashI 1000 s+        let r = hashI 1000 (multiplySeed s)         let md = _monteMd v         assert (mkTestLabel (_monteCount v) md) md r         when (r == md) $ go md t      -- Each Round consists of 1000 hash applications     hashI :: Natural -> B.ByteString -> B.ByteString-    hashI 0 s = s-    hashI i s = let s' = hash s in hashI (i - 1) s'+    hashI 0 s = B.takeEnd size s+    hashI i s = let s' = hash s in hashI (i - 1) (B.drop size s <> s')++monteTestInternal+    :: Natural+    -> (B.ByteString -> B.ByteString)+    -> MonteFile+    -> Bool+monteTestInternal i h f = case monteAssertInternal i check h f of+    Nothing -> False+    Just () -> True+  where+    check _ a b = guard (a == b)  mkTestLabel :: Natural -> B.ByteString -> String mkTestLabel i input = show i <> "[" <> B8.unpack msg <> "]"
src/Test/Hash/SHA.hs view
@@ -1,11 +1,12 @@+{-# LANGUAGE ImportQualifiedPost #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-}  -- | -- Module: Test.Hash.SHA--- Copyright: Copyright © 2022 Kadena LLC.+-- Copyright: Copyright © 2022-2024 Lars Kuhtz <lakuhtz@gmail.com> -- License: MIT--- Maintainer: Lars Kuhtz <lars@kadena.io>+-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com> -- Stability: experimental -- Description: SHA Test Vectors for Hashing Byte-Oriented Messages --@@ -13,7 +14,7 @@ -- -- Details can be found here: ----- https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha3vs.pdf+-- https://csrc.nist.gov/csrc/media/projects/cryptographic-algorithm-validation-program/documents/shs/shavs.pdf. -- -- Response files are available here: --@@ -51,11 +52,66 @@ , sha512Monte , sha512_224Monte , sha512_256Monte++-- * Test Utils+, msgTest+, msgAssert+, monteTest+, monteAssert ) where +import Data.ByteString qualified as B+ -- internal modules  import Test.Hash.Internal++-- -------------------------------------------------------------------------- --+-- Tools++-- | Test a given SHA1 or SHA2 implementation for the test vectors in a monte+-- file. See 'monteAssert' for details.+--+monteTest :: (B.ByteString -> B.ByteString) -> MonteFile -> Bool+monteTest = monteTestInternal 3++-- | For a given SHA1 or SHA2 implementation, assert the correct result for each+-- test vector in a 'MonteFile'.+--+-- The function to assert equality is usually provided by some testing+-- framework.+--+-- NOTE that the test algorithms for SHA (SHA1 and SHA2) and SHA3 are different.+--+-- The test algorithm is describe in cf. https://csrc.nist.gov/csrc/media/projects/cryptographic-algorithm-validation-program/documents/shs/shavs.pdf.+-- The pseudo code is as follows:+--+-- @+-- INPUT: Seed - A random seed n bits long+-- {+--     for (j=0; j<100; j++) {+--         MD0 = MD1 = MD2 = Seed;+--         for (i=3; i<1003; i++) {+--             Mi = MDi-3 || MDi-2 || MDi-1;+--             MDi = SHA(Mi);+--         }+--         MDj = Seed = MD1002;+--         OUTPUT: MDj+--     }+-- }+-- @+--+monteAssert+    :: Monad m+    => (String -> B.ByteString -> B.ByteString -> m ())+        -- ^ Function to assertion Equality. The first argument is a test label,+        -- the second argument is the actual value, and the thrid value is the+        -- expected value.+    -> (B.ByteString -> B.ByteString)+        -- ^ Hash function+    -> MonteFile+    -> m ()+monteAssert = monteAssertInternal 3  -- -------------------------------------------------------------------------- -- --
src/Test/Hash/SHA3.hs view
@@ -1,11 +1,12 @@+{-# LANGUAGE ImportQualifiedPost #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-}  -- | -- Module: Test.Hash.SHA3--- Copyright: Copyright © 2022 Kadena LLC.+-- Copyright: Copyright © 2022-2024 Lars Kuhtz <lakuhtz@gmail.com> -- License: MIT--- Maintainer: Lars Kuhtz <lars@kadena.io>+-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com> -- Stability: experimental -- Description: SHA-3 Hash Function Tests for Hashing Byte-Oriented Messages --@@ -50,9 +51,58 @@ , monteAssert ) where +import Data.ByteString qualified as B+ -- internal modules  import Test.Hash.Internal++-- -------------------------------------------------------------------------- --+-- Tools++-- | Test a given SHA3 implementation for the test vectors in a monte file. See+-- 'monteAssert' for details.+--+monteTest :: (B.ByteString -> B.ByteString) -> MonteFile -> Bool+monteTest = monteTestInternal 1++-- | For a given SHA3 implementation, assert the correct result for each test+-- vector in a 'MonteFile'.+--+-- The function to assert equality is usually provided by some testing+-- framework.+--+-- The test algorithm is describe in cf. https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha3vs.pdf+-- The pseudo code is as follows:+--+-- @+-- INPUT: A random Seed n bits long+-- {+--     MD0 = Seed;+--     for (j=0; j<100; j++) {+--         for (i=1; i<1001; i++) {+--             Msgi = MDi-1;+--             MDi = SHA3(Msgi);+--         }+--         MD0 = MD1000;+--         OUTPUT: MD0+--     }+-- }+-- @+--+-- NOTE that the test algorithms for SHA (SHA1 and SHA2) and SHA3 are different.+--+monteAssert+    :: Monad m+    => (String -> B.ByteString -> B.ByteString -> m ())+        -- ^ Function to assertion Equality. The first argument is a test label,+        -- the second argument is the actual value, and the thrid value is the+        -- expected value.+    -> (B.ByteString -> B.ByteString)+        -- ^ Hash function+    -> MonteFile+    -> m ()+monteAssert = monteAssertInternal 1  -- -------------------------------------------------------------------------- -- -- Long Selected Message SHA-3 Hash Function Tests for Hashing Byte-Oriented Messages
src/Test/Hash/SHAKE.hs view
@@ -3,9 +3,9 @@  -- | -- Module: Test.Hash.SHAKE--- Copyright: Copyright © 2022 Kadena LLC.+-- Copyright: Copyright © 2022-2024 Lars Kuhtz <lakuhtz@gmail.com> -- License: MIT--- Maintainer: Lars Kuhtz <lars@kadena.io>+-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com> -- Stability: experimental -- Description: SHA-3 XOF Test Vectors for Byte-Oriented Output --