From 22b190b55afcbbd2e014339ead081a552fea4287 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sat, 28 Apr 2018 15:26:04 -0700 Subject: Switch initialism style to follow the rest of the Haskell world --- btls.cabal | 12 +-- cbits/btls.c | 2 +- src/Data/Digest.hs | 16 +-- src/Data/Digest/Internal.hs | 4 +- src/Data/HMAC.hs | 82 +++++++++++++++ src/Data/Hmac.hs | 82 --------------- src/Internal/Base.chs | 12 +-- src/Internal/Digest.chs | 44 ++++---- src/Internal/HMAC.chs | 60 +++++++++++ src/Internal/Hmac.chs | 60 ----------- tests/Data/Digest/HashTests.hs | 6 +- tests/Data/Digest/MD5Tests.hs | 92 ++++++++++++++++ tests/Data/Digest/Md5Tests.hs | 92 ---------------- tests/Data/Digest/SHA1Tests.hs | 90 ++++++++++++++++ tests/Data/Digest/SHA2Tests.hs | 233 +++++++++++++++++++++++++++++++++++++++++ tests/Data/Digest/Sha1Tests.hs | 90 ---------------- tests/Data/Digest/Sha2Tests.hs | 233 ----------------------------------------- tests/Data/DigestTests.hs | 12 +-- tests/Data/HMACTests.hs | 192 +++++++++++++++++++++++++++++++++ tests/Data/HmacTests.hs | 192 --------------------------------- tests/Tests.hs | 4 +- 21 files changed, 805 insertions(+), 805 deletions(-) create mode 100644 src/Data/HMAC.hs delete mode 100644 src/Data/Hmac.hs create mode 100644 src/Internal/HMAC.chs delete mode 100644 src/Internal/Hmac.chs create mode 100644 tests/Data/Digest/MD5Tests.hs delete mode 100644 tests/Data/Digest/Md5Tests.hs create mode 100644 tests/Data/Digest/SHA1Tests.hs create mode 100644 tests/Data/Digest/SHA2Tests.hs delete mode 100644 tests/Data/Digest/Sha1Tests.hs delete mode 100644 tests/Data/Digest/Sha2Tests.hs create mode 100644 tests/Data/HMACTests.hs delete mode 100644 tests/Data/HmacTests.hs diff --git a/btls.cabal b/btls.cabal index 4246e43..d63a67a 100644 --- a/btls.cabal +++ b/btls.cabal @@ -48,13 +48,13 @@ library -Wno-safe -Wno-unsafe -optl-Wl,-z,relro -optl-Wl,-z,now -optl-Wl,-s exposed-modules: Data.Digest - , Data.Hmac + , Data.HMAC other-modules: Data.Digest.Internal , Foreign.Ptr.Cast , Foreign.Ptr.ConstantTimeEquals , Internal.Base , Internal.Digest - , Internal.Hmac + , Internal.HMAC , Result c-sources: cbits/btls.c -- Use special names for the BoringSSL libraries to avoid accidentally pulling @@ -79,10 +79,10 @@ test-suite tests main-is: Tests.hs other-modules: Data.DigestTests , Data.Digest.HashTests - , Data.Digest.Md5Tests - , Data.Digest.Sha1Tests - , Data.Digest.Sha2Tests - , Data.HmacTests + , Data.Digest.MD5Tests + , Data.Digest.SHA1Tests + , Data.Digest.SHA2Tests + , Data.HMACTests build-depends: base >=4.9 && <4.10 , btls , bytestring >=0.10 && <0.11 diff --git a/cbits/btls.c b/cbits/btls.c index e922f3a..d4bfd62 100644 --- a/cbits/btls.c +++ b/cbits/btls.c @@ -14,6 +14,6 @@ #include -void btlsFinalizeEvpMdCtx(EVP_MD_CTX* const ctx) { +void btlsFinalizeEVPMDCtx(EVP_MD_CTX* const ctx) { (void)EVP_MD_CTX_cleanup(ctx); } diff --git a/src/Data/Digest.hs b/src/Data/Digest.hs index 3bd042c..34279e5 100644 --- a/src/Data/Digest.hs +++ b/src/Data/Digest.hs @@ -35,23 +35,23 @@ import Internal.Digest type LazyByteString = ByteString.Lazy.ByteString md5, sha1, sha224, sha256, sha384, sha512 :: Algorithm -md5 = Algorithm evpMd5 -sha1 = Algorithm evpSha1 -sha224 = Algorithm evpSha224 -sha256 = Algorithm evpSha256 -sha384 = Algorithm evpSha384 -sha512 = Algorithm evpSha512 +md5 = Algorithm evpMD5 +sha1 = Algorithm evpSHA1 +sha224 = Algorithm evpSHA224 +sha256 = Algorithm evpSHA256 +sha384 = Algorithm evpSHA384 +sha512 = Algorithm evpSHA512 -- | Hashes according to the given 'Algorithm'. hash :: Algorithm -> LazyByteString -> Digest hash (Algorithm md) bytes = unsafeLocalState $ do - ctxFP <- mallocEvpMdCtx + ctxFP <- mallocEVPMDCtx withForeignPtr ctxFP $ \ctx -> do evpDigestInitEx ctx md noEngine mapM_ (updateBytes ctx) (ByteString.Lazy.toChunks bytes) d <- - allocaArray evpMaxMdSize $ \mdOut -> + allocaArray evpMaxMDSize $ \mdOut -> alloca $ \pOutSize -> do evpDigestFinalEx ctx mdOut pOutSize outSize <- fromIntegral <$> peek pOutSize diff --git a/src/Data/Digest/Internal.hs b/src/Data/Digest/Internal.hs index be88e68..859f765 100644 --- a/src/Data/Digest/Internal.hs +++ b/src/Data/Digest/Internal.hs @@ -21,10 +21,10 @@ import Data.Char (intToDigit) import Data.Word (Word8) import Foreign (Ptr) -import Internal.Base (EvpMd) +import Internal.Base (EVPMD) -- | A cryptographic hash function. -newtype Algorithm = Algorithm (Ptr EvpMd) +newtype Algorithm = Algorithm (Ptr EVPMD) -- | The result of a hash operation. newtype Digest = Digest ByteString diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs new file mode 100644 index 0000000..1ad1bfb --- /dev/null +++ b/src/Data/HMAC.hs @@ -0,0 +1,82 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +module Data.HMAC + ( SecretKey(SecretKey) + , HMAC + , hmac + ) where + +import Data.ByteString (ByteString) +import qualified Data.ByteString as ByteString +import qualified Data.ByteString.Lazy as ByteString.Lazy +import qualified Data.ByteString.Unsafe as ByteString +import Foreign (Storable(peek), alloca, allocaArray, withForeignPtr) +import Foreign.Marshal.Unsafe (unsafeLocalState) +import Unsafe.Coerce (unsafeCoerce) + +import Data.Digest.Internal (Algorithm(Algorithm), Digest(Digest)) +import Foreign.Ptr.ConstantTimeEquals (constantTimeEquals) +import Internal.Base +import Internal.Digest +import Internal.HMAC + +type LazyByteString = ByteString.Lazy.ByteString + +-- | A secret key used as input to a cipher or HMAC. Equality comparisons on +-- this type are variable-time. +newtype SecretKey = SecretKey ByteString + deriving (Eq, Ord, Show) + +-- | A hash-based message authentication code. Equality comparisons on this type +-- are constant-time. +newtype HMAC = HMAC ByteString + +instance Eq HMAC where + (HMAC a) == (HMAC b) = + unsafeLocalState $ + ByteString.unsafeUseAsCStringLen a $ \(a', size) -> + ByteString.unsafeUseAsCStringLen b $ \(b', _) -> + constantTimeEquals a' b' size + +instance Show HMAC where + show (HMAC m) = show (Digest m) + +-- | Creates an HMAC according to the given 'Algorithm'. +hmac :: Algorithm -> SecretKey -> LazyByteString -> HMAC +hmac (Algorithm md) (SecretKey key) bytes = + unsafeLocalState $ do + ctxFP <- mallocHMACCtx + withForeignPtr ctxFP $ \ctx -> do + ByteString.unsafeUseAsCStringLen key $ \(keyBytes, keySize) -> + hmacInitEx ctx keyBytes (fromIntegral keySize) md noEngine + mapM_ (updateBytes ctx) (ByteString.Lazy.toChunks bytes) + m <- + allocaArray evpMaxMDSize $ \hmacOut -> + alloca $ \pOutSize -> do + hmacFinal ctx hmacOut pOutSize + outSize <- fromIntegral <$> peek pOutSize + -- As in 'Data.Digest.Internal', 'hmacOut' is a 'Ptr CUChar'. Have + -- GHC reinterpret it as a 'Ptr CChar' so that it can be ingested + -- into a 'ByteString'. + ByteString.packCStringLen (unsafeCoerce hmacOut, outSize) + return (HMAC m) + where + updateBytes ctx chunk = + -- 'hmacUpdate' treats its @bytes@ argument as @const@, so the sharing + -- inherent in 'ByteString.unsafeUseAsCStringLen' is fine. + ByteString.unsafeUseAsCStringLen chunk $ \(buf, len) -> + -- 'buf' is a 'Ptr CChar', but 'hmacUpdate' takes a 'Ptr CUChar', so we + -- do the 'unsafeCoerce' dance yet again. + hmacUpdate ctx (unsafeCoerce buf) (fromIntegral len) diff --git a/src/Data/Hmac.hs b/src/Data/Hmac.hs deleted file mode 100644 index 2211248..0000000 --- a/src/Data/Hmac.hs +++ /dev/null @@ -1,82 +0,0 @@ --- Copyright 2018 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -module Data.Hmac - ( SecretKey(SecretKey) - , Hmac - , hmac - ) where - -import Data.ByteString (ByteString) -import qualified Data.ByteString as ByteString -import qualified Data.ByteString.Lazy as ByteString.Lazy -import qualified Data.ByteString.Unsafe as ByteString -import Foreign (Storable(peek), alloca, allocaArray, withForeignPtr) -import Foreign.Marshal.Unsafe (unsafeLocalState) -import Unsafe.Coerce (unsafeCoerce) - -import Data.Digest.Internal (Algorithm(Algorithm), Digest(Digest)) -import Foreign.Ptr.ConstantTimeEquals (constantTimeEquals) -import Internal.Base -import Internal.Digest -import Internal.Hmac - -type LazyByteString = ByteString.Lazy.ByteString - --- | A secret key used as input to a cipher or HMAC. Equality comparisons on --- this type are variable-time. -newtype SecretKey = SecretKey ByteString - deriving (Eq, Ord, Show) - --- | A hash-based message authentication code. Equality comparisons on this type --- are constant-time. -newtype Hmac = Hmac ByteString - -instance Eq Hmac where - (Hmac a) == (Hmac b) = - unsafeLocalState $ - ByteString.unsafeUseAsCStringLen a $ \(a', size) -> - ByteString.unsafeUseAsCStringLen b $ \(b', _) -> - constantTimeEquals a' b' size - -instance Show Hmac where - show (Hmac m) = show (Digest m) - --- | Creates an HMAC according to the given 'Algorithm'. -hmac :: Algorithm -> SecretKey -> LazyByteString -> Hmac -hmac (Algorithm md) (SecretKey key) bytes = - unsafeLocalState $ do - ctxFP <- mallocHmacCtx - withForeignPtr ctxFP $ \ctx -> do - ByteString.unsafeUseAsCStringLen key $ \(keyBytes, keySize) -> - hmacInitEx ctx keyBytes (fromIntegral keySize) md noEngine - mapM_ (updateBytes ctx) (ByteString.Lazy.toChunks bytes) - m <- - allocaArray evpMaxMdSize $ \hmacOut -> - alloca $ \pOutSize -> do - hmacFinal ctx hmacOut pOutSize - outSize <- fromIntegral <$> peek pOutSize - -- As in 'Data.Digest.Internal', 'hmacOut' is a 'Ptr CUChar'. Have - -- GHC reinterpret it as a 'Ptr CChar' so that it can be ingested - -- into a 'ByteString'. - ByteString.packCStringLen (unsafeCoerce hmacOut, outSize) - return (Hmac m) - where - updateBytes ctx chunk = - -- 'hmacUpdate' treats its @bytes@ argument as @const@, so the sharing - -- inherent in 'ByteString.unsafeUseAsCStringLen' is fine. - ByteString.unsafeUseAsCStringLen chunk $ \(buf, len) -> - -- 'buf' is a 'Ptr CChar', but 'hmacUpdate' takes a 'Ptr CUChar', so we - -- do the 'unsafeCoerce' dance yet again. - hmacUpdate ctx (unsafeCoerce buf) (fromIntegral len) diff --git a/src/Internal/Base.chs b/src/Internal/Base.chs index 427cf6b..c2e615a 100644 --- a/src/Internal/Base.chs +++ b/src/Internal/Base.chs @@ -30,14 +30,14 @@ noEngine = nullPtr -- | The BoringSSL @EVP_MD_CTX@ type, representing the state of a pending -- hashing operation. -data EvpMdCtx -{#pointer *EVP_MD_CTX as 'Ptr EvpMdCtx' -> EvpMdCtx nocode#} +data EVPMDCtx +{#pointer *EVP_MD_CTX as 'Ptr EVPMDCtx' -> EVPMDCtx nocode#} -- | The BoringSSL @EVP_MD@ type, representing a hash algorithm. -data EvpMd -{#pointer *EVP_MD as 'Ptr EvpMd' -> EvpMd nocode#} +data EVPMD +{#pointer *EVP_MD as 'Ptr EVPMD' -> EVPMD nocode#} -- | The BoringSSL @HMAC_CTX@ type, representing the state of a pending HMAC -- operation. -data HmacCtx -{#pointer *HMAC_CTX as 'Ptr HmacCtx' -> HmacCtx nocode#} +data HMACCtx +{#pointer *HMAC_CTX as 'Ptr HMACCtx' -> HMACCtx nocode#} diff --git a/src/Internal/Digest.chs b/src/Internal/Digest.chs index 021de22..d451f1c 100644 --- a/src/Internal/Digest.chs +++ b/src/Internal/Digest.chs @@ -16,10 +16,10 @@ {-# OPTIONS_GHC -Wno-orphans #-} module Internal.Digest - ( evpMd5, evpSha1, evpSha224, evpSha256, evpSha384, evpSha512 - , mallocEvpMdCtx + ( evpMD5, evpSHA1, evpSHA224, evpSHA256, evpSHA384, evpSHA512 + , mallocEVPMDCtx , evpDigestInitEx, evpDigestUpdate, evpDigestFinalEx - , evpMaxMdSize + , evpMaxMDSize ) where import Foreign @@ -33,40 +33,40 @@ import Result #include -evpMd5, evpSha1, evpSha224, evpSha256, evpSha384, evpSha512 :: Ptr EvpMd -evpMd5 = {#call pure EVP_md5 as ^#} -evpSha1 = {#call pure EVP_sha1 as ^#} -evpSha224 = {#call pure EVP_sha224 as ^#} -evpSha256 = {#call pure EVP_sha256 as ^#} -evpSha384 = {#call pure EVP_sha384 as ^#} -evpSha512 = {#call pure EVP_sha512 as ^#} +evpMD5, evpSHA1, evpSHA224, evpSHA256, evpSHA384, evpSHA512 :: Ptr EVPMD +evpMD5 = {#call pure EVP_md5 as ^#} +evpSHA1 = {#call pure EVP_sha1 as ^#} +evpSHA224 = {#call pure EVP_sha224 as ^#} +evpSHA256 = {#call pure EVP_sha256 as ^#} +evpSHA384 = {#call pure EVP_sha384 as ^#} +evpSHA512 = {#call pure EVP_sha512 as ^#} --- | Memory-safe allocator for 'EvpMdCtx'. -mallocEvpMdCtx :: IO (ForeignPtr EvpMdCtx) -mallocEvpMdCtx = do +-- | Memory-safe allocator for 'EVPMDCtx'. +mallocEVPMDCtx :: IO (ForeignPtr EVPMDCtx) +mallocEVPMDCtx = do fp <- mallocForeignPtr withForeignPtr fp {#call EVP_MD_CTX_init as ^#} - addForeignPtrFinalizer btlsFinalizeEvpMdCtxPtr fp + addForeignPtrFinalizer btlsFinalizeEVPMDCtxPtr fp return fp -foreign import ccall "&btlsFinalizeEvpMdCtx" - btlsFinalizeEvpMdCtxPtr :: FinalizerPtr EvpMdCtx +foreign import ccall "&btlsFinalizeEVPMDCtx" + btlsFinalizeEVPMDCtxPtr :: FinalizerPtr EVPMDCtx -evpDigestInitEx :: Ptr EvpMdCtx -> Ptr EvpMd -> Ptr Engine -> IO () +evpDigestInitEx :: Ptr EVPMDCtx -> Ptr EVPMD -> Ptr Engine -> IO () evpDigestInitEx ctx md engine = requireSuccess $ {#call EVP_DigestInit_ex as ^#} ctx md engine -evpDigestUpdate :: Ptr EvpMdCtx -> Ptr a -> CULong -> IO () +evpDigestUpdate :: Ptr EVPMDCtx -> Ptr a -> CULong -> IO () evpDigestUpdate ctx md bytes = alwaysSucceeds $ {#call EVP_DigestUpdate as ^#} ctx (asVoidPtr md) bytes -evpDigestFinalEx :: Ptr EvpMdCtx -> Ptr CUChar -> Ptr CUInt -> IO () +evpDigestFinalEx :: Ptr EVPMDCtx -> Ptr CUChar -> Ptr CUInt -> IO () evpDigestFinalEx ctx mdOut outSize = alwaysSucceeds $ {#call EVP_DigestFinal_ex as ^#} ctx mdOut outSize -evpMaxMdSize :: Int -evpMaxMdSize = {#const EVP_MAX_MD_SIZE#} +evpMaxMDSize :: Int +evpMaxMDSize = {#const EVP_MAX_MD_SIZE#} -instance Storable EvpMdCtx where +instance Storable EVPMDCtx where sizeOf _ = {#sizeof EVP_MD_CTX#} alignment _ = {#alignof EVP_MD_CTX#} diff --git a/src/Internal/HMAC.chs b/src/Internal/HMAC.chs new file mode 100644 index 0000000..7e64edf --- /dev/null +++ b/src/Internal/HMAC.chs @@ -0,0 +1,60 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +{-# OPTIONS_GHC -Wno-missing-methods #-} +{-# OPTIONS_GHC -Wno-orphans #-} + +module Internal.HMAC + ( mallocHMACCtx + , hmacInitEx, hmacUpdate, hmacFinal + ) where + +import Foreign + (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, sizeOf), + addForeignPtrFinalizer, mallocForeignPtr, withForeignPtr) +import Foreign.C.Types + +import Foreign.Ptr.Cast (asVoidPtr) +{#import Internal.Base#} +import Result + +#include + +-- | Memory-safe allocator for 'HMACCtx'. +mallocHMACCtx :: IO (ForeignPtr HMACCtx) +mallocHMACCtx = do + fp <- mallocForeignPtr + withForeignPtr fp {#call HMAC_CTX_init as ^#} + addForeignPtrFinalizer hmacCtxCleanup fp + return fp + +foreign import ccall "&HMAC_CTX_cleanup" + hmacCtxCleanup :: FinalizerPtr HMACCtx + +hmacInitEx :: Ptr HMACCtx -> Ptr a -> CULong -> Ptr EVPMD -> Ptr Engine -> IO () +hmacInitEx ctx bytes size md engine = + requireSuccess $ + {#call HMAC_Init_ex as ^#} ctx (asVoidPtr bytes) size md engine + +hmacUpdate :: Ptr HMACCtx -> Ptr CUChar -> CULong -> IO () +hmacUpdate ctx bytes size = + alwaysSucceeds $ {#call HMAC_Update as ^#} ctx bytes size + +hmacFinal :: Ptr HMACCtx -> Ptr CUChar -> Ptr CUInt -> IO () +hmacFinal ctx out outSize = + requireSuccess $ {#call HMAC_Final as ^#} ctx out outSize + +instance Storable HMACCtx where + sizeOf _ = {#sizeof HMAC_CTX#} + alignment _ = {#alignof HMAC_CTX#} diff --git a/src/Internal/Hmac.chs b/src/Internal/Hmac.chs deleted file mode 100644 index 0ef1e2e..0000000 --- a/src/Internal/Hmac.chs +++ /dev/null @@ -1,60 +0,0 @@ --- Copyright 2018 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -{-# OPTIONS_GHC -Wno-missing-methods #-} -{-# OPTIONS_GHC -Wno-orphans #-} - -module Internal.Hmac - ( mallocHmacCtx - , hmacInitEx, hmacUpdate, hmacFinal - ) where - -import Foreign - (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, sizeOf), - addForeignPtrFinalizer, mallocForeignPtr, withForeignPtr) -import Foreign.C.Types - -import Foreign.Ptr.Cast (asVoidPtr) -{#import Internal.Base#} -import Result - -#include - --- | Memory-safe allocator for 'HmacCtx'. -mallocHmacCtx :: IO (ForeignPtr HmacCtx) -mallocHmacCtx = do - fp <- mallocForeignPtr - withForeignPtr fp {#call HMAC_CTX_init as ^#} - addForeignPtrFinalizer hmacCtxCleanup fp - return fp - -foreign import ccall "&HMAC_CTX_cleanup" - hmacCtxCleanup :: FinalizerPtr HmacCtx - -hmacInitEx :: Ptr HmacCtx -> Ptr a -> CULong -> Ptr EvpMd -> Ptr Engine -> IO () -hmacInitEx ctx bytes size md engine = - requireSuccess $ - {#call HMAC_Init_ex as ^#} ctx (asVoidPtr bytes) size md engine - -hmacUpdate :: Ptr HmacCtx -> Ptr CUChar -> CULong -> IO () -hmacUpdate ctx bytes size = - alwaysSucceeds $ {#call HMAC_Update as ^#} ctx bytes size - -hmacFinal :: Ptr HmacCtx -> Ptr CUChar -> Ptr CUInt -> IO () -hmacFinal ctx out outSize = - requireSuccess $ {#call HMAC_Final as ^#} ctx out outSize - -instance Storable HmacCtx where - sizeOf _ = {#sizeof HMAC_CTX#} - alignment _ = {#alignof HMAC_CTX#} diff --git a/tests/Data/Digest/HashTests.hs b/tests/Data/Digest/HashTests.hs index 16b1aa8..45b31e2 100644 --- a/tests/Data/Digest/HashTests.hs +++ b/tests/Data/Digest/HashTests.hs @@ -17,7 +17,7 @@ module Data.Digest.HashTests ( tableTestCase , testAgainstCoreutils - , testAgainstOpenssl + , testAgainstOpenSSL ) where import Data.ByteString (ByteString) @@ -55,8 +55,8 @@ runExternal p s = do hClose stdin -- causes process to exit hGetContents stdout -testAgainstOpenssl :: (ByteString -> String) -> String -> Property IO -testAgainstOpenssl f flag = +testAgainstOpenSSL :: (ByteString -> String) -> String -> Property IO +testAgainstOpenSSL f flag = over ByteString.Series.enumW8s $ \s -> monadic $ do theirs <- runExternal (proc "openssl" ["dgst", '-' : flag]) s diff --git a/tests/Data/Digest/MD5Tests.hs b/tests/Data/Digest/MD5Tests.hs new file mode 100644 index 0000000..0db7bcb --- /dev/null +++ b/tests/Data/Digest/MD5Tests.hs @@ -0,0 +1,92 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +{-# LANGUAGE OverloadedStrings #-} + +module Data.Digest.MD5Tests (tests) where + +import qualified Data.ByteString.Lazy as ByteString.Lazy +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.SmallCheck (testProperty) + +import Data.Digest.HashTests + (tableTestCase, testAgainstCoreutils, testAgainstOpenSSL) +import Data.Digest (hash, md5) + +tests :: TestTree +tests = testGroup "MD5" + [ testRFCExamples + , testGoExamples + , testCoreutilsConformance + , testOpenSSLConformance + ] + +-- | MD5 example vectors from RFC 1321. +testRFCExamples = testGroup "RFC 1321 examples" $ + map (tableTestCase md5sum) + [ ("d41d8cd98f00b204e9800998ecf8427e", "") + , ("0cc175b9c0f1b6a831c399e269772661", "a") + , ("900150983cd24fb0d6963f7d28e17f72", "abc") + , ("f96b697d7cb7938d525a2f31aaf161d0", "message digest") + , ("c3fcd3d76192e4007dfb496cca67e13b", "abcdefghijklmnopqrstuvwxyz") + , ("d174ab98d277d9f5a5611c2c9f419d9f", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") + , ("57edf4a22be3c955ac49da2e2107b67a", "12345678901234567890123456789012345678901234567890123456789012345678901234567890") + ] + +-- | Test vectors used to test the Go MD5 implementation. +testGoExamples = testGroup "Go tests" $ + map (tableTestCase md5sum) + [ ("d41d8cd98f00b204e9800998ecf8427e", "") + , ("0cc175b9c0f1b6a831c399e269772661", "a") + , ("187ef4436122d1cc2f40dc2b92f0eba0", "ab") + , ("900150983cd24fb0d6963f7d28e17f72", "abc") + , ("e2fc714c4727ee9395f324cd2e7f331f", "abcd") + , ("ab56b4d92b40713acc5af89985d4b786", "abcde") + , ("e80b5017098950fc58aad83c8c14978e", "abcdef") + , ("7ac66c0f148de9519b8bd264312c4d64", "abcdefg") + , ("e8dc4081b13434b45189a720b77b6818", "abcdefgh") + , ("8aa99b1f439ff71293e95357bac6fd94", "abcdefghi") + , ("a925576942e94b2ef57a066101b48876", "abcdefghij") + , ("d747fc1719c7eacb84058196cfe56d57", "Discard medicine more than two years old.") + , ("bff2dcb37ef3a44ba43ab144768ca837", "He who has a shady past knows that nice guys finish last.") + , ("0441015ecb54a7342d017ed1bcfdbea5", "I wouldn't marry him with a ten foot pole.") + , ("9e3cac8e9e9757a60c3ea391130d3689", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") + , ("a0f04459b031f916a59a35cc482dc039", "The days of the digital watch are numbered. -Tom Stoppard") + , ("e7a48e0fe884faf31475d2a04b1362cc", "Nepal premier won't resign.") + , ("637d2fe925c07c113800509964fb0e06", "For every action there is an equal and opposite government program.") + , ("834a8d18d5c6562119cf4c7f5086cb71", "His money is twice tainted: 'taint yours and 'taint mine.") + , ("de3a4d2fd6c73ec2db2abad23b444281", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") + , ("acf203f997e2cf74ea3aff86985aefaf", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") + , ("e1c1384cb4d2221dfdd7c795a4222c9a", "size: a.out: bad magic") + , ("c90f3ddecc54f34228c063d7525bf644", "The major problem is with sendmail. -Mark Horton") + , ("cdf7ab6c1fd49bd9933c43f3ea5af185", "Give me a rock, paper and scissors and I will move the world. CCFestoon") + , ("83bc85234942fc883c063cbd7f0ad5d0", "If the enemy is within range, then so are you.") + , ("277cbe255686b48dd7e8f389394d9299", "It's well we cannot hear the screams/That we create in others' dreams.") + , ("fd3fb0a7ffb8af16603f3d3af98f8e1f", "You remind me of a TV show, but that's all right: I watch it anyway.") + , ("469b13a78ebf297ecda64d4723655154", "C is as portable as Stonehedge!!") + , ("63eb3a2f466410104731c4b037600110", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") + , ("72c2ed7592debca1c90fc0100f931a2f", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") + , ("132f7619d33b523b1d9e5bd8e0928355", "How can you write a big system without C++? -Paul Glick") + ] + +-- | Tests our MD5 implementation against coreutils'. +testCoreutilsConformance = testProperty "conformance with coreutils" $ + testAgainstCoreutils md5sum "md5sum" + +-- | Tests our MD5 implementation against openssl(1)'s. +testOpenSSLConformance = testProperty "conformance with OpenSSL" $ + testAgainstOpenSSL md5sum "md5" + +-- Convenience function. +md5sum = show . hash md5 . ByteString.Lazy.fromStrict diff --git a/tests/Data/Digest/Md5Tests.hs b/tests/Data/Digest/Md5Tests.hs deleted file mode 100644 index 79f8757..0000000 --- a/tests/Data/Digest/Md5Tests.hs +++ /dev/null @@ -1,92 +0,0 @@ --- Copyright 2018 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -{-# LANGUAGE OverloadedStrings #-} - -module Data.Digest.Md5Tests (tests) where - -import qualified Data.ByteString.Lazy as ByteString.Lazy -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.SmallCheck (testProperty) - -import Data.Digest.HashTests - (tableTestCase, testAgainstCoreutils, testAgainstOpenssl) -import Data.Digest (hash, md5) - -tests :: TestTree -tests = testGroup "MD5" - [ testRfcExamples - , testGoExamples - , testCoreutilsConformance - , testOpensslConformance - ] - --- | MD5 example vectors from RFC 1321. -testRfcExamples = testGroup "RFC 1321 examples" $ - map (tableTestCase md5sum) - [ ("d41d8cd98f00b204e9800998ecf8427e", "") - , ("0cc175b9c0f1b6a831c399e269772661", "a") - , ("900150983cd24fb0d6963f7d28e17f72", "abc") - , ("f96b697d7cb7938d525a2f31aaf161d0", "message digest") - , ("c3fcd3d76192e4007dfb496cca67e13b", "abcdefghijklmnopqrstuvwxyz") - , ("d174ab98d277d9f5a5611c2c9f419d9f", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") - , ("57edf4a22be3c955ac49da2e2107b67a", "12345678901234567890123456789012345678901234567890123456789012345678901234567890") - ] - --- | Test vectors used to test the Go MD5 implementation. -testGoExamples = testGroup "Go tests" $ - map (tableTestCase md5sum) - [ ("d41d8cd98f00b204e9800998ecf8427e", "") - , ("0cc175b9c0f1b6a831c399e269772661", "a") - , ("187ef4436122d1cc2f40dc2b92f0eba0", "ab") - , ("900150983cd24fb0d6963f7d28e17f72", "abc") - , ("e2fc714c4727ee9395f324cd2e7f331f", "abcd") - , ("ab56b4d92b40713acc5af89985d4b786", "abcde") - , ("e80b5017098950fc58aad83c8c14978e", "abcdef") - , ("7ac66c0f148de9519b8bd264312c4d64", "abcdefg") - , ("e8dc4081b13434b45189a720b77b6818", "abcdefgh") - , ("8aa99b1f439ff71293e95357bac6fd94", "abcdefghi") - , ("a925576942e94b2ef57a066101b48876", "abcdefghij") - , ("d747fc1719c7eacb84058196cfe56d57", "Discard medicine more than two years old.") - , ("bff2dcb37ef3a44ba43ab144768ca837", "He who has a shady past knows that nice guys finish last.") - , ("0441015ecb54a7342d017ed1bcfdbea5", "I wouldn't marry him with a ten foot pole.") - , ("9e3cac8e9e9757a60c3ea391130d3689", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") - , ("a0f04459b031f916a59a35cc482dc039", "The days of the digital watch are numbered. -Tom Stoppard") - , ("e7a48e0fe884faf31475d2a04b1362cc", "Nepal premier won't resign.") - , ("637d2fe925c07c113800509964fb0e06", "For every action there is an equal and opposite government program.") - , ("834a8d18d5c6562119cf4c7f5086cb71", "His money is twice tainted: 'taint yours and 'taint mine.") - , ("de3a4d2fd6c73ec2db2abad23b444281", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") - , ("acf203f997e2cf74ea3aff86985aefaf", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") - , ("e1c1384cb4d2221dfdd7c795a4222c9a", "size: a.out: bad magic") - , ("c90f3ddecc54f34228c063d7525bf644", "The major problem is with sendmail. -Mark Horton") - , ("cdf7ab6c1fd49bd9933c43f3ea5af185", "Give me a rock, paper and scissors and I will move the world. CCFestoon") - , ("83bc85234942fc883c063cbd7f0ad5d0", "If the enemy is within range, then so are you.") - , ("277cbe255686b48dd7e8f389394d9299", "It's well we cannot hear the screams/That we create in others' dreams.") - , ("fd3fb0a7ffb8af16603f3d3af98f8e1f", "You remind me of a TV show, but that's all right: I watch it anyway.") - , ("469b13a78ebf297ecda64d4723655154", "C is as portable as Stonehedge!!") - , ("63eb3a2f466410104731c4b037600110", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") - , ("72c2ed7592debca1c90fc0100f931a2f", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") - , ("132f7619d33b523b1d9e5bd8e0928355", "How can you write a big system without C++? -Paul Glick") - ] - --- | Tests our MD5 implementation against coreutils'. -testCoreutilsConformance = testProperty "conformance with coreutils" $ - testAgainstCoreutils md5sum "md5sum" - --- | Tests our MD5 implementation against openssl(1)'s. -testOpensslConformance = testProperty "conformance with OpenSSL" $ - testAgainstOpenssl md5sum "md5" - --- Convenience function. -md5sum = show . hash md5 . ByteString.Lazy.fromStrict diff --git a/tests/Data/Digest/SHA1Tests.hs b/tests/Data/Digest/SHA1Tests.hs new file mode 100644 index 0000000..362660f --- /dev/null +++ b/tests/Data/Digest/SHA1Tests.hs @@ -0,0 +1,90 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +{-# LANGUAGE OverloadedStrings #-} + +module Data.Digest.SHA1Tests (tests) where + +import qualified Data.ByteString.Lazy as ByteString.Lazy +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit ((@?=), testCase) +import Test.Tasty.SmallCheck (testProperty) + +import Data.Digest.HashTests + (tableTestCase, testAgainstCoreutils, testAgainstOpenSSL) +import Data.Digest (hash, sha1) + +tests :: TestTree +tests = testGroup "SHA-1" + [ testNistExamples + , testGoExamples + , testCoreutilsConformance + , testOpenSSLConformance + ] + + +-- | SHA-1 example vectors from +-- https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values. +testNistExamples = testGroup "NIST examples" + [ testCase "one-block" $ sha1sum "abc" @?= "a9993e364706816aba3e25717850c26c9cd0d89d" + , testCase "two-block" $ sha1sum "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" @?= "84983e441c3bd26ebaae4aa1f95129e5e54670f1" + ] + +-- | Test vectors used to test the Go SHA-1 implementation. +testGoExamples = testGroup "Go tests" $ + map (tableTestCase sha1sum) + [ ("76245dbf96f661bd221046197ab8b9f063f11bad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n") + , ("da39a3ee5e6b4b0d3255bfef95601890afd80709", "") + , ("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a") + , ("da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab") + , ("a9993e364706816aba3e25717850c26c9cd0d89d", "abc") + , ("81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd") + , ("03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde") + , ("1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef") + , ("2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg") + , ("425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh") + , ("c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi") + , ("d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij") + , ("ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old.") + , ("e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last.") + , ("45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole.") + , ("55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") + , ("b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered. -Tom Stoppard") + , ("c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign.") + , ("6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program.") + , ("597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine.") + , ("6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") + , ("514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") + , ("c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size: a.out: bad magic") + , ("74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail. -Mark Horton") + , ("0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world. CCFestoon") + , ("3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you.") + , ("410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams.") + , ("841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway.") + , ("163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!") + , ("32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") + , ("0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") + , ("6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++? -Paul Glick") + ] + +-- | Tests our SHA-1 implementation against coreutils'. +testCoreutilsConformance = testProperty "conformance with coreutils" $ + testAgainstCoreutils sha1sum "sha1sum" + +-- | Tests our SHA-1 implementation against openssl(1)'s. +testOpenSSLConformance = testProperty "conformance with OpenSSL" $ + testAgainstOpenSSL sha1sum "sha1" + +-- Convenience function. +sha1sum = show . hash sha1 . ByteString.Lazy.fromStrict diff --git a/tests/Data/Digest/SHA2Tests.hs b/tests/Data/Digest/SHA2Tests.hs new file mode 100644 index 0000000..eecf1d8 --- /dev/null +++ b/tests/Data/Digest/SHA2Tests.hs @@ -0,0 +1,233 @@ +-- Copyright 2017 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +{-# LANGUAGE OverloadedStrings #-} + +module Data.Digest.SHA2Tests (tests) where + +import qualified Data.ByteString.Lazy as ByteString.Lazy +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit ((@?=), testCase) +import Test.Tasty.SmallCheck (testProperty) + +import Data.Digest.HashTests + (tableTestCase, testAgainstCoreutils, testAgainstOpenSSL) +import Data.Digest (hash, sha224, sha256, sha384, sha512) + +tests :: TestTree +tests = testGroup "SHA-2" + [ testNistExamples + , testGoExamples + , testCoreutilsConformance + , testOpenSSLConformance + ] + +-- | SHA-2 example vectors from +-- https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values. +testNistExamples = testGroup "NIST examples" + [ testNistSHA224 + , testNistSHA256 + , testNistSHA384 + , testNistSHA512 + ] + +testNistSHA224 = testGroup "SHA-224" + [ testCase "one-block" $ sha224sum "abc" @?= "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" + , testCase "two-block" $ sha224sum "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" @?= "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" + ] + +testNistSHA256 = testGroup "SHA-256" + [ testCase "one-block" $ sha256sum "abc" @?= "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + , testCase "two-block" $ sha256sum "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" @?= "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" + ] + +testNistSHA384 = testGroup "SHA-384" + [ testCase "one-block" $ sha384sum "abc" @?= "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7" + , testCase "two-block" $ sha384sum "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" @?= "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039" + ] + +testNistSHA512 = testGroup "SHA-512" + [ testCase "one-block" $ sha512sum "abc" @?= "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" + , testCase "two-block" $ sha512sum "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" @?= "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" + ] + +-- | Test vectors used to test the Go SHA-2 implementations. +testGoExamples = testGroup "Go tests" + [ testGoSHA224 + , testGoSHA256 + , testGoSHA384 + , testGoSHA512 + ] + +testGoSHA224 = testGroup "SHA-224" $ + map (tableTestCase sha224sum) + [ ("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "") + , ("abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5", "a") + , ("db3cda86d4429a1d39c148989566b38f7bda0156296bd364ba2f878b", "ab") + , ("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", "abc") + , ("a76654d8e3550e9a2d67a0eeb6c67b220e5885eddd3fde135806e601", "abcd") + , ("bdd03d560993e675516ba5a50638b6531ac2ac3d5847c61916cfced6", "abcde") + , ("7043631cb415556a275a4ebecb802c74ee9f6153908e1792a90b6a98", "abcdef") + , ("d1884e711701ad81abe0c77a3b0ea12e19ba9af64077286c72fc602d", "abcdefg") + , ("17eb7d40f0356f8598e89eafad5f6c759b1f822975d9c9b737c8a517", "abcdefgh") + , ("aeb35915346c584db820d2de7af3929ffafef9222a9bcb26516c7334", "abcdefghi") + , ("d35e1e5af29ddb0d7e154357df4ad9842afee527c689ee547f753188", "abcdefghij") + , ("19297f1cef7ddc8a7e947f5c5a341e10f7245045e425db67043988d7", "Discard medicine more than two years old.") + , ("0f10c2eb436251f777fbbd125e260d36aecf180411726c7c885f599a", "He who has a shady past knows that nice guys finish last.") + , ("4d1842104919f314cad8a3cd20b3cba7e8ed3e7abed62b57441358f6", "I wouldn't marry him with a ten foot pole.") + , ("a8ba85c6fe0c48fbffc72bbb2f03fcdbc87ae2dc7a56804d1590fb3b", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") + , ("5543fbab26e67e8885b1a852d567d1cb8b9bfe42e0899584c50449a9", "The days of the digital watch are numbered. -Tom Stoppard") + , ("65ca107390f5da9efa05d28e57b221657edc7e43a9a18fb15b053ddb", "Nepal premier won't resign.") + , ("84953962be366305a9cc9b5cd16ed019edc37ac96c0deb3e12cca116", "For every action there is an equal and opposite government program.") + , ("35a189ce987151dfd00b3577583cc6a74b9869eecf894459cb52038d", "His money is twice tainted: 'taint yours and 'taint mine.") + , ("2fc333713983edfd4ef2c0da6fb6d6415afb94987c91e4069eb063e6", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") + , ("cbe32d38d577a1b355960a4bc3c659c2dc4670859a19777a875842c4", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") + , ("a2dc118ce959e027576413a7b440c875cdc8d40df9141d6ef78a57e1", "size: a.out: bad magic") + , ("d10787e24052bcff26dc484787a54ed819e4e4511c54890ee977bf81", "The major problem is with sendmail. -Mark Horton") + , ("62efcf16ab8a893acdf2f348aaf06b63039ff1bf55508c830532c9fb", "Give me a rock, paper and scissors and I will move the world. CCFestoon") + , ("3e9b7e4613c59f58665104c5fa86c272db5d3a2ff30df5bb194a5c99", "If the enemy is within range, then so are you.") + , ("5999c208b8bdf6d471bb7c359ac5b829e73a8211dff686143a4e7f18", "It's well we cannot hear the screams/That we create in others' dreams.") + , ("3b2d67ff54eabc4ef737b14edf87c64280ef582bcdf2a6d56908b405", "You remind me of a TV show, but that's all right: I watch it anyway.") + , ("d0733595d20e4d3d6b5c565a445814d1bbb2fd08b9a3b8ffb97930c6", "C is as portable as Stonehedge!!") + , ("43fb8aeed8a833175c9295c1165415f98c866ef08a4922959d673507", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") + , ("ec18e66e93afc4fb1604bc2baedbfd20b44c43d76e65c0996d7851c6", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") + , ("86ed2eaa9c75ba98396e5c9fb2f679ecf0ea2ed1e0ee9ceecb4a9332", "How can you write a big system without C++? -Paul Glick") + ] + +testGoSHA256 = testGroup "SHA-256" $ + map (tableTestCase sha256sum) + [ ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "") + , ("ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", "a") + , ("fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603", "ab") + , ("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "abc") + , ("88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", "abcd") + , ("36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c", "abcde") + , ("bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721", "abcdef") + , ("7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a", "abcdefg") + , ("9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab", "abcdefgh") + , ("19cc02f26df43cc571bc9ed7b0c4d29224a3ec229529221725ef76d021c8326f", "abcdefghi") + , ("72399361da6a7754fec986dca5b7cbaf1c810a28ded4abaf56b2106d06cb78b0", "abcdefghij") + , ("a144061c271f152da4d151034508fed1c138b8c976339de229c3bb6d4bbb4fce", "Discard medicine more than two years old.") + , ("6dae5caa713a10ad04b46028bf6dad68837c581616a1589a265a11288d4bb5c4", "He who has a shady past knows that nice guys finish last.") + , ("ae7a702a9509039ddbf29f0765e70d0001177914b86459284dab8b348c2dce3f", "I wouldn't marry him with a ten foot pole.") + , ("6748450b01c568586715291dfa3ee018da07d36bb7ea6f180c1af6270215c64f", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") + , ("14b82014ad2b11f661b5ae6a99b75105c2ffac278cd071cd6c05832793635774", "The days of the digital watch are numbered. -Tom Stoppard") + , ("7102cfd76e2e324889eece5d6c41921b1e142a4ac5a2692be78803097f6a48d8", "Nepal premier won't resign.") + , ("23b1018cd81db1d67983c5f7417c44da9deb582459e378d7a068552ea649dc9f", "For every action there is an equal and opposite government program.") + , ("8001f190dfb527261c4cfcab70c98e8097a7a1922129bc4096950e57c7999a5a", "His money is twice tainted: 'taint yours and 'taint mine.") + , ("8c87deb65505c3993eb24b7a150c4155e82eee6960cf0c3a8114ff736d69cad5", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") + , ("bfb0a67a19cdec3646498b2e0f751bddc41bba4b7f30081b0b932aad214d16d7", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") + , ("7f9a0b9bf56332e19f5a0ec1ad9c1425a153da1c624868fda44561d6b74daf36", "size: a.out: bad magic") + , ("b13f81b8aad9e3666879af19886140904f7f429ef083286195982a7588858cfc", "The major problem is with sendmail. -Mark Horton") + , ("b26c38d61519e894480c70c8374ea35aa0ad05b2ae3d6674eec5f52a69305ed4", "Give me a rock, paper and scissors and I will move the world. CCFestoon") + , ("049d5e26d4f10222cd841a119e38bd8d2e0d1129728688449575d4ff42b842c1", "If the enemy is within range, then so are you.") + , ("0e116838e3cc1c1a14cd045397e29b4d087aa11b0853fc69ec82e90330d60949", "It's well we cannot hear the screams/That we create in others' dreams.") + , ("4f7d8eb5bcf11de2a56b971021a444aa4eafd6ecd0f307b5109e4e776cd0fe46", "You remind me of a TV show, but that's all right: I watch it anyway.") + , ("61c0cc4c4bd8406d5120b3fb4ebc31ce87667c162f29468b3c779675a85aebce", "C is as portable as Stonehedge!!") + , ("1fb2eb3688093c4a3f80cd87a5547e2ce940a4f923243a79a2a1e242220693ac", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") + , ("395585ce30617b62c80b93e8208ce866d4edc811a177fdb4b82d3911d8696423", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") + , ("4f9b189a13d030838269dce846b16a1ce9ce81fe63e65de2f636863336a98fe6", "How can you write a big system without C++? -Paul Glick") + ] + +testGoSHA384 = testGroup "SHA-384" $ + map (tableTestCase sha384sum) + [ ("38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "") + , ("54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31", "a") + , ("c7be03ba5bcaa384727076db0018e99248e1a6e8bd1b9ef58a9ec9dd4eeebb3f48b836201221175befa74ddc3d35afdd", "ab") + , ("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", "abc") + , ("1165b3406ff0b52a3d24721f785462ca2276c9f454a116c2b2ba20171a7905ea5a026682eb659c4d5f115c363aa3c79b", "abcd") + , ("4c525cbeac729eaf4b4665815bc5db0c84fe6300068a727cf74e2813521565abc0ec57a37ee4d8be89d097c0d2ad52f0", "abcde") + , ("c6a4c65b227e7387b9c3e839d44869c4cfca3ef583dea64117859b808c1e3d8ae689e1e314eeef52a6ffe22681aa11f5", "abcdef") + , ("9f11fc131123f844c1226f429b6a0a6af0525d9f40f056c7fc16cdf1b06bda08e302554417a59fa7dcf6247421959d22", "abcdefg") + , ("9000cd7cada59d1d2eb82912f7f24e5e69cc5517f68283b005fa27c285b61e05edf1ad1a8a9bded6fd29eb87d75ad806", "abcdefgh") + , ("ef54915b60cf062b8dd0c29ae3cad69abe6310de63ac081f46ef019c5c90897caefd79b796cfa81139788a260ded52df", "abcdefghi") + , ("a12070030a02d86b0ddacd0d3a5b598344513d0a051e7355053e556a0055489c1555399b03342845c4adde2dc44ff66c", "abcdefghij") + , ("86f58ec2d74d1b7f8eb0c2ff0967316699639e8d4eb129de54bdf34c96cdbabe200d052149f2dd787f43571ba74670d4", "Discard medicine more than two years old.") + , ("ae4a2b639ca9bfa04b1855d5a05fe7f230994f790891c6979103e2605f660c4c1262a48142dcbeb57a1914ba5f7c3fa7", "He who has a shady past knows that nice guys finish last.") + , ("40ae213df6436eca952aa6841886fcdb82908ef1576a99c8f49bb9dd5023169f7c53035abdda0b54c302f4974e2105e7", "I wouldn't marry him with a ten foot pole.") + , ("e7cf8b873c9bc950f06259aa54309f349cefa72c00d597aebf903e6519a50011dfe355afff064a10701c705693848df9", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") + , ("c3d4f0f4047181c7d39d34703365f7bf70207183caf2c2f6145f04da895ef69124d9cdeb635da636c3a474e61024e29b", "The days of the digital watch are numbered. -Tom Stoppard") + , ("a097aab567e167d5cf93676ed73252a69f9687cb3179bb2d27c9878119e94bf7b7c4b58dc90582edfaf66e11388ed714", "Nepal premier won't resign.") + , ("5026ca45c41fc64712eb65065da92f6467541c78f8966d3fe2c8e3fb769a3ec14215f819654b47bd64f7f0eac17184f3", "For every action there is an equal and opposite government program.") + , ("ac1cc0f5ac8d5f5514a7b738ac322b7fb52a161b449c3672e9b6a6ad1a5e4b26b001cf3bad24c56598676ca17d4b445a", "His money is twice tainted: 'taint yours and 'taint mine.") + , ("722d10c5de371ec0c8c4b5247ac8a5f1d240d68c73f8da13d8b25f0166d6f309bf9561979a111a0049405771d201941a", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") + , ("dc2d3ea18bfa10549c63bf2b75b39b5167a80c12aff0e05443168ea87ff149fb0eda5e0bd234eb5d48c7d02ffc5807f1", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") + , ("1d67c969e2a945ae5346d2139760261504d4ba164c522443afe19ef3e29b152a4c52445489cfc9d7215e5a450e8e1e4e", "size: a.out: bad magic") + , ("5ff8e075e465646e7b73ef36d812c6e9f7d60fa6ea0e533e5569b4f73cde53cdd2cc787f33540af57cca3fe467d32fe0", "The major problem is with sendmail. -Mark Horton") + , ("5bd0a997a67c9ae1979a894eb0cde403dde003c9b6f2c03cf21925c42ff4e1176e6df1ca005381612ef18457b9b7ec3b", "Give me a rock, paper and scissors and I will move the world. CCFestoon") + , ("1eee6da33e7e54fc5be52ae23b94b16ba4d2a947ae4505c6a3edfc7401151ea5205ac01b669b56f27d8ef7f175ed7762", "If the enemy is within range, then so are you.") + , ("76b06e9dea66bfbb1a96029426dc0dfd7830bd297eb447ff5358d94a87cd00c88b59df2493fef56ecbb5231073892ea9", "It's well we cannot hear the screams/That we create in others' dreams.") + , ("12acaf21452cff586143e3f5db0bfdf7802c057e1adf2a619031c4e1b0ccc4208cf6cef8fe722bbaa2fb46a30d9135d8", "You remind me of a TV show, but that's all right: I watch it anyway.") + , ("0fc23d7f4183efd186f0bc4fc5db867e026e2146b06cb3d52f4bdbd57d1740122caa853b41868b197b2ac759db39df88", "C is as portable as Stonehedge!!") + , ("bc805578a7f85d34a86a32976e1c34fe65cf815186fbef76f46ef99cda10723f971f3f1464d488243f5e29db7488598d", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") + , ("b23918399a12ebf4431559eec3813eaf7412e875fd7464f16d581e473330842d2e96c6be49a7ce3f9bb0b8bc0fcbe0fe", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") + , ("1764b700eb1ead52a2fc33cc28975c2180f1b8faa5038d94cffa8d78154aab16e91dd787e7b0303948ebed62561542c8", "How can you write a big system without C++? -Paul Glick") + ] + +testGoSHA512 = testGroup "SHA-512" $ + map (tableTestCase sha512sum) + [ ("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "") + , ("1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75", "a") + , ("2d408a0717ec188158278a796c689044361dc6fdde28d6f04973b80896e1823975cdbf12eb63f9e0591328ee235d80e9b5bf1aa6a44f4617ff3caf6400eb172d", "ab") + , ("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", "abc") + , ("d8022f2060ad6efd297ab73dcc5355c9b214054b0d1776a136a669d26a7d3b14f73aa0d0ebff19ee333368f0164b6419a96da49e3e481753e7e96b716bdccb6f", "abcd") + , ("878ae65a92e86cac011a570d4c30a7eaec442b85ce8eca0c2952b5e3cc0628c2e79d889ad4d5c7c626986d452dd86374b6ffaa7cd8b67665bef2289a5c70b0a1", "abcde") + , ("e32ef19623e8ed9d267f657a81944b3d07adbb768518068e88435745564e8d4150a0a703be2a7d88b61e3d390c2bb97e2d4c311fdc69d6b1267f05f59aa920e7", "abcdef") + , ("d716a4188569b68ab1b6dfac178e570114cdf0ea3a1cc0e31486c3e41241bc6a76424e8c37ab26f096fc85ef9886c8cb634187f4fddff645fb099f1ff54c6b8c", "abcdefg") + , ("a3a8c81bc97c2560010d7389bc88aac974a104e0e2381220c6e084c4dccd1d2d17d4f86db31c2a851dc80e6681d74733c55dcd03dd96f6062cdda12a291ae6ce", "abcdefgh") + , ("f22d51d25292ca1d0f68f69aedc7897019308cc9db46efb75a03dd494fc7f126c010e8ade6a00a0c1a5f1b75d81e0ed5a93ce98dc9b833db7839247b1d9c24fe", "abcdefghi") + , ("ef6b97321f34b1fea2169a7db9e1960b471aa13302a988087357c520be957ca119c3ba68e6b4982c019ec89de3865ccf6a3cda1fe11e59f98d99f1502c8b9745", "abcdefghij") + , ("2210d99af9c8bdecda1b4beff822136753d8342505ddce37f1314e2cdbb488c6016bdaa9bd2ffa513dd5de2e4b50f031393d8ab61f773b0e0130d7381e0f8a1d", "Discard medicine more than two years old.") + , ("a687a8985b4d8d0a24f115fe272255c6afaf3909225838546159c1ed685c211a203796ae8ecc4c81a5b6315919b3a64f10713da07e341fcdbb08541bf03066ce", "He who has a shady past knows that nice guys finish last.") + , ("8ddb0392e818b7d585ab22769a50df660d9f6d559cca3afc5691b8ca91b8451374e42bcdabd64589ed7c91d85f626596228a5c8572677eb98bc6b624befb7af8", "I wouldn't marry him with a ten foot pole.") + , ("26ed8f6ca7f8d44b6a8a54ae39640fa8ad5c673f70ee9ce074ba4ef0d483eea00bab2f61d8695d6b34df9c6c48ae36246362200ed820448bdc03a720366a87c6", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") + , ("e5a14bf044be69615aade89afcf1ab0389d5fc302a884d403579d1386a2400c089b0dbb387ed0f463f9ee342f8244d5a38cfbc0e819da9529fbff78368c9a982", "The days of the digital watch are numbered. -Tom Stoppard") + , ("420a1faa48919e14651bed45725abe0f7a58e0f099424c4e5a49194946e38b46c1f8034b18ef169b2e31050d1648e0b982386595f7df47da4b6fd18e55333015", "Nepal premier won't resign.") + , ("d926a863beadb20134db07683535c72007b0e695045876254f341ddcccde132a908c5af57baa6a6a9c63e6649bba0c213dc05fadcf9abccea09f23dcfb637fbe", "For every action there is an equal and opposite government program.") + , ("9a98dd9bb67d0da7bf83da5313dff4fd60a4bac0094f1b05633690ffa7f6d61de9a1d4f8617937d560833a9aaa9ccafe3fd24db418d0e728833545cadd3ad92d", "His money is twice tainted: 'taint yours and 'taint mine.") + , ("d7fde2d2351efade52f4211d3746a0780a26eec3df9b2ed575368a8a1c09ec452402293a8ea4eceb5a4f60064ea29b13cdd86918cd7a4faf366160b009804107", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") + , ("b0f35ffa2697359c33a56f5c0cf715c7aeed96da9905ca2698acadb08fbc9e669bf566b6bd5d61a3e86dc22999bcc9f2224e33d1d4f32a228cf9d0349e2db518", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") + , ("3d2e5f91778c9e66f7e061293aaa8a8fc742dd3b2e4f483772464b1144189b49273e610e5cccd7a81a19ca1fa70f16b10f1a100a4d8c1372336be8484c64b311", "size: a.out: bad magic") + , ("b2f68ff58ac015efb1c94c908b0d8c2bf06f491e4de8e6302c49016f7f8a33eac3e959856c7fddbc464de618701338a4b46f76dbfaf9a1e5262b5f40639771c7", "The major problem is with sendmail. -Mark Horton") + , ("d8c92db5fdf52cf8215e4df3b4909d29203ff4d00e9ad0b64a6a4e04dec5e74f62e7c35c7fb881bd5de95442123df8f57a489b0ae616bd326f84d10021121c57", "Give me a rock, paper and scissors and I will move the world. CCFestoon") + , ("19a9f8dc0a233e464e8566ad3ca9b91e459a7b8c4780985b015776e1bf239a19bc233d0556343e2b0a9bc220900b4ebf4f8bdf89ff8efeaf79602d6849e6f72e", "If the enemy is within range, then so are you.") + , ("00b4c41f307bde87301cdc5b5ab1ae9a592e8ecbb2021dd7bc4b34e2ace60741cc362560bec566ba35178595a91932b8d5357e2c9cec92d393b0fa7831852476", "It's well we cannot hear the screams/That we create in others' dreams.") + , ("91eccc3d5375fd026e4d6787874b1dce201cecd8a27dbded5065728cb2d09c58a3d467bb1faf353bf7ba567e005245d5321b55bc344f7c07b91cb6f26c959be7", "You remind me of a TV show, but that's all right: I watch it anyway.") + , ("fabbbe22180f1f137cfdc9556d2570e775d1ae02a597ded43a72a40f9b485d500043b7be128fb9fcd982b83159a0d99aa855a9e7cc4240c00dc01a9bdf8218d7", "C is as portable as Stonehedge!!") + , ("2ecdec235c1fa4fc2a154d8fba1dddb8a72a1ad73838b51d792331d143f8b96a9f6fcb0f34d7caa351fe6d88771c4f105040e0392f06e0621689d33b2f3ba92e", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") + , ("7ad681f6f96f82f7abfa7ecc0334e8fa16d3dc1cdc45b60b7af43fe4075d2357c0c1d60e98350f1afb1f2fe7a4d7cd2ad55b88e458e06b73c40b437331f5dab4", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") + , ("833f9248ab4a3b9e5131f745fda1ffd2dd435b30e965957e78291c7ab73605fd1912b0794e5c233ab0a12d205a39778d19b83515d6a47003f19cdee51d98c7e0", "How can you write a big system without C++? -Paul Glick") + ] + +-- | Tests our SHA-2 implementations against coreutils'. +testCoreutilsConformance = testGroup "conformance with coreutils" + [ testProperty "SHA-224" $ testAgainstCoreutils sha224sum "sha224sum" + , testProperty "SHA-256" $ testAgainstCoreutils sha256sum "sha256sum" + , testProperty "SHA-384" $ testAgainstCoreutils sha384sum "sha384sum" + , testProperty "SHA-512" $ testAgainstCoreutils sha512sum "sha512sum" + ] + +-- | Tests our SHA-2 implementations against openssl(1)'s. +testOpenSSLConformance = testGroup "conformance with OpenSSL" + [ testProperty "SHA-224" $ testAgainstOpenSSL sha224sum "sha224" + , testProperty "SHA-256" $ testAgainstOpenSSL sha256sum "sha256" + , testProperty "SHA-384" $ testAgainstOpenSSL sha384sum "sha384" + , testProperty "SHA-512" $ testAgainstOpenSSL sha512sum "sha512" + ] + +-- Convenience functions. +sha224sum = show . hash sha224 . ByteString.Lazy.fromStrict +sha256sum = show . hash sha256 . ByteString.Lazy.fromStrict +sha384sum = show . hash sha384 . ByteString.Lazy.fromStrict +sha512sum = show . hash sha512 . ByteString.Lazy.fromStrict diff --git a/tests/Data/Digest/Sha1Tests.hs b/tests/Data/Digest/Sha1Tests.hs deleted file mode 100644 index ea84553..0000000 --- a/tests/Data/Digest/Sha1Tests.hs +++ /dev/null @@ -1,90 +0,0 @@ --- Copyright 2018 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -{-# LANGUAGE OverloadedStrings #-} - -module Data.Digest.Sha1Tests (tests) where - -import qualified Data.ByteString.Lazy as ByteString.Lazy -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit ((@?=), testCase) -import Test.Tasty.SmallCheck (testProperty) - -import Data.Digest.HashTests - (tableTestCase, testAgainstCoreutils, testAgainstOpenssl) -import Data.Digest (hash, sha1) - -tests :: TestTree -tests = testGroup "SHA-1" - [ testNistExamples - , testGoExamples - , testCoreutilsConformance - , testOpensslConformance - ] - - --- | SHA-1 example vectors from --- https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values. -testNistExamples = testGroup "NIST examples" - [ testCase "one-block" $ sha1sum "abc" @?= "a9993e364706816aba3e25717850c26c9cd0d89d" - , testCase "two-block" $ sha1sum "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" @?= "84983e441c3bd26ebaae4aa1f95129e5e54670f1" - ] - --- | Test vectors used to test the Go SHA-1 implementation. -testGoExamples = testGroup "Go tests" $ - map (tableTestCase sha1sum) - [ ("76245dbf96f661bd221046197ab8b9f063f11bad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n") - , ("da39a3ee5e6b4b0d3255bfef95601890afd80709", "") - , ("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a") - , ("da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab") - , ("a9993e364706816aba3e25717850c26c9cd0d89d", "abc") - , ("81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd") - , ("03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde") - , ("1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef") - , ("2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg") - , ("425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh") - , ("c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi") - , ("d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij") - , ("ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old.") - , ("e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last.") - , ("45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole.") - , ("55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") - , ("b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered. -Tom Stoppard") - , ("c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign.") - , ("6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program.") - , ("597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine.") - , ("6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") - , ("514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") - , ("c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size: a.out: bad magic") - , ("74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail. -Mark Horton") - , ("0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world. CCFestoon") - , ("3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you.") - , ("410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams.") - , ("841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway.") - , ("163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!") - , ("32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") - , ("0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") - , ("6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++? -Paul Glick") - ] - --- | Tests our SHA-1 implementation against coreutils'. -testCoreutilsConformance = testProperty "conformance with coreutils" $ - testAgainstCoreutils sha1sum "sha1sum" - --- | Tests our SHA-1 implementation against openssl(1)'s. -testOpensslConformance = testProperty "conformance with OpenSSL" $ - testAgainstOpenssl sha1sum "sha1" - --- Convenience function. -sha1sum = show . hash sha1 . ByteString.Lazy.fromStrict diff --git a/tests/Data/Digest/Sha2Tests.hs b/tests/Data/Digest/Sha2Tests.hs deleted file mode 100644 index 374157c..0000000 --- a/tests/Data/Digest/Sha2Tests.hs +++ /dev/null @@ -1,233 +0,0 @@ --- Copyright 2017 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -{-# LANGUAGE OverloadedStrings #-} - -module Data.Digest.Sha2Tests (tests) where - -import qualified Data.ByteString.Lazy as ByteString.Lazy -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit ((@?=), testCase) -import Test.Tasty.SmallCheck (testProperty) - -import Data.Digest.HashTests - (tableTestCase, testAgainstCoreutils, testAgainstOpenssl) -import Data.Digest (hash, sha224, sha256, sha384, sha512) - -tests :: TestTree -tests = testGroup "SHA-2" - [ testNistExamples - , testGoExamples - , testCoreutilsConformance - , testOpensslConformance - ] - --- | SHA-2 example vectors from --- https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values. -testNistExamples = testGroup "NIST examples" - [ testNistSha224 - , testNistSha256 - , testNistSha384 - , testNistSha512 - ] - -testNistSha224 = testGroup "SHA-224" - [ testCase "one-block" $ sha224sum "abc" @?= "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" - , testCase "two-block" $ sha224sum "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" @?= "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" - ] - -testNistSha256 = testGroup "SHA-256" - [ testCase "one-block" $ sha256sum "abc" @?= "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" - , testCase "two-block" $ sha256sum "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" @?= "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" - ] - -testNistSha384 = testGroup "SHA-384" - [ testCase "one-block" $ sha384sum "abc" @?= "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7" - , testCase "two-block" $ sha384sum "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" @?= "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039" - ] - -testNistSha512 = testGroup "SHA-512" - [ testCase "one-block" $ sha512sum "abc" @?= "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" - , testCase "two-block" $ sha512sum "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" @?= "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" - ] - --- | Test vectors used to test the Go SHA-2 implementations. -testGoExamples = testGroup "Go tests" - [ testGoSha224 - , testGoSha256 - , testGoSha384 - , testGoSha512 - ] - -testGoSha224 = testGroup "SHA-224" $ - map (tableTestCase sha224sum) - [ ("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "") - , ("abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5", "a") - , ("db3cda86d4429a1d39c148989566b38f7bda0156296bd364ba2f878b", "ab") - , ("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", "abc") - , ("a76654d8e3550e9a2d67a0eeb6c67b220e5885eddd3fde135806e601", "abcd") - , ("bdd03d560993e675516ba5a50638b6531ac2ac3d5847c61916cfced6", "abcde") - , ("7043631cb415556a275a4ebecb802c74ee9f6153908e1792a90b6a98", "abcdef") - , ("d1884e711701ad81abe0c77a3b0ea12e19ba9af64077286c72fc602d", "abcdefg") - , ("17eb7d40f0356f8598e89eafad5f6c759b1f822975d9c9b737c8a517", "abcdefgh") - , ("aeb35915346c584db820d2de7af3929ffafef9222a9bcb26516c7334", "abcdefghi") - , ("d35e1e5af29ddb0d7e154357df4ad9842afee527c689ee547f753188", "abcdefghij") - , ("19297f1cef7ddc8a7e947f5c5a341e10f7245045e425db67043988d7", "Discard medicine more than two years old.") - , ("0f10c2eb436251f777fbbd125e260d36aecf180411726c7c885f599a", "He who has a shady past knows that nice guys finish last.") - , ("4d1842104919f314cad8a3cd20b3cba7e8ed3e7abed62b57441358f6", "I wouldn't marry him with a ten foot pole.") - , ("a8ba85c6fe0c48fbffc72bbb2f03fcdbc87ae2dc7a56804d1590fb3b", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") - , ("5543fbab26e67e8885b1a852d567d1cb8b9bfe42e0899584c50449a9", "The days of the digital watch are numbered. -Tom Stoppard") - , ("65ca107390f5da9efa05d28e57b221657edc7e43a9a18fb15b053ddb", "Nepal premier won't resign.") - , ("84953962be366305a9cc9b5cd16ed019edc37ac96c0deb3e12cca116", "For every action there is an equal and opposite government program.") - , ("35a189ce987151dfd00b3577583cc6a74b9869eecf894459cb52038d", "His money is twice tainted: 'taint yours and 'taint mine.") - , ("2fc333713983edfd4ef2c0da6fb6d6415afb94987c91e4069eb063e6", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") - , ("cbe32d38d577a1b355960a4bc3c659c2dc4670859a19777a875842c4", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") - , ("a2dc118ce959e027576413a7b440c875cdc8d40df9141d6ef78a57e1", "size: a.out: bad magic") - , ("d10787e24052bcff26dc484787a54ed819e4e4511c54890ee977bf81", "The major problem is with sendmail. -Mark Horton") - , ("62efcf16ab8a893acdf2f348aaf06b63039ff1bf55508c830532c9fb", "Give me a rock, paper and scissors and I will move the world. CCFestoon") - , ("3e9b7e4613c59f58665104c5fa86c272db5d3a2ff30df5bb194a5c99", "If the enemy is within range, then so are you.") - , ("5999c208b8bdf6d471bb7c359ac5b829e73a8211dff686143a4e7f18", "It's well we cannot hear the screams/That we create in others' dreams.") - , ("3b2d67ff54eabc4ef737b14edf87c64280ef582bcdf2a6d56908b405", "You remind me of a TV show, but that's all right: I watch it anyway.") - , ("d0733595d20e4d3d6b5c565a445814d1bbb2fd08b9a3b8ffb97930c6", "C is as portable as Stonehedge!!") - , ("43fb8aeed8a833175c9295c1165415f98c866ef08a4922959d673507", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") - , ("ec18e66e93afc4fb1604bc2baedbfd20b44c43d76e65c0996d7851c6", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") - , ("86ed2eaa9c75ba98396e5c9fb2f679ecf0ea2ed1e0ee9ceecb4a9332", "How can you write a big system without C++? -Paul Glick") - ] - -testGoSha256 = testGroup "SHA-256" $ - map (tableTestCase sha256sum) - [ ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "") - , ("ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", "a") - , ("fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603", "ab") - , ("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "abc") - , ("88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", "abcd") - , ("36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c", "abcde") - , ("bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721", "abcdef") - , ("7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a", "abcdefg") - , ("9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab", "abcdefgh") - , ("19cc02f26df43cc571bc9ed7b0c4d29224a3ec229529221725ef76d021c8326f", "abcdefghi") - , ("72399361da6a7754fec986dca5b7cbaf1c810a28ded4abaf56b2106d06cb78b0", "abcdefghij") - , ("a144061c271f152da4d151034508fed1c138b8c976339de229c3bb6d4bbb4fce", "Discard medicine more than two years old.") - , ("6dae5caa713a10ad04b46028bf6dad68837c581616a1589a265a11288d4bb5c4", "He who has a shady past knows that nice guys finish last.") - , ("ae7a702a9509039ddbf29f0765e70d0001177914b86459284dab8b348c2dce3f", "I wouldn't marry him with a ten foot pole.") - , ("6748450b01c568586715291dfa3ee018da07d36bb7ea6f180c1af6270215c64f", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") - , ("14b82014ad2b11f661b5ae6a99b75105c2ffac278cd071cd6c05832793635774", "The days of the digital watch are numbered. -Tom Stoppard") - , ("7102cfd76e2e324889eece5d6c41921b1e142a4ac5a2692be78803097f6a48d8", "Nepal premier won't resign.") - , ("23b1018cd81db1d67983c5f7417c44da9deb582459e378d7a068552ea649dc9f", "For every action there is an equal and opposite government program.") - , ("8001f190dfb527261c4cfcab70c98e8097a7a1922129bc4096950e57c7999a5a", "His money is twice tainted: 'taint yours and 'taint mine.") - , ("8c87deb65505c3993eb24b7a150c4155e82eee6960cf0c3a8114ff736d69cad5", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") - , ("bfb0a67a19cdec3646498b2e0f751bddc41bba4b7f30081b0b932aad214d16d7", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") - , ("7f9a0b9bf56332e19f5a0ec1ad9c1425a153da1c624868fda44561d6b74daf36", "size: a.out: bad magic") - , ("b13f81b8aad9e3666879af19886140904f7f429ef083286195982a7588858cfc", "The major problem is with sendmail. -Mark Horton") - , ("b26c38d61519e894480c70c8374ea35aa0ad05b2ae3d6674eec5f52a69305ed4", "Give me a rock, paper and scissors and I will move the world. CCFestoon") - , ("049d5e26d4f10222cd841a119e38bd8d2e0d1129728688449575d4ff42b842c1", "If the enemy is within range, then so are you.") - , ("0e116838e3cc1c1a14cd045397e29b4d087aa11b0853fc69ec82e90330d60949", "It's well we cannot hear the screams/That we create in others' dreams.") - , ("4f7d8eb5bcf11de2a56b971021a444aa4eafd6ecd0f307b5109e4e776cd0fe46", "You remind me of a TV show, but that's all right: I watch it anyway.") - , ("61c0cc4c4bd8406d5120b3fb4ebc31ce87667c162f29468b3c779675a85aebce", "C is as portable as Stonehedge!!") - , ("1fb2eb3688093c4a3f80cd87a5547e2ce940a4f923243a79a2a1e242220693ac", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") - , ("395585ce30617b62c80b93e8208ce866d4edc811a177fdb4b82d3911d8696423", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") - , ("4f9b189a13d030838269dce846b16a1ce9ce81fe63e65de2f636863336a98fe6", "How can you write a big system without C++? -Paul Glick") - ] - -testGoSha384 = testGroup "SHA-384" $ - map (tableTestCase sha384sum) - [ ("38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "") - , ("54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31", "a") - , ("c7be03ba5bcaa384727076db0018e99248e1a6e8bd1b9ef58a9ec9dd4eeebb3f48b836201221175befa74ddc3d35afdd", "ab") - , ("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", "abc") - , ("1165b3406ff0b52a3d24721f785462ca2276c9f454a116c2b2ba20171a7905ea5a026682eb659c4d5f115c363aa3c79b", "abcd") - , ("4c525cbeac729eaf4b4665815bc5db0c84fe6300068a727cf74e2813521565abc0ec57a37ee4d8be89d097c0d2ad52f0", "abcde") - , ("c6a4c65b227e7387b9c3e839d44869c4cfca3ef583dea64117859b808c1e3d8ae689e1e314eeef52a6ffe22681aa11f5", "abcdef") - , ("9f11fc131123f844c1226f429b6a0a6af0525d9f40f056c7fc16cdf1b06bda08e302554417a59fa7dcf6247421959d22", "abcdefg") - , ("9000cd7cada59d1d2eb82912f7f24e5e69cc5517f68283b005fa27c285b61e05edf1ad1a8a9bded6fd29eb87d75ad806", "abcdefgh") - , ("ef54915b60cf062b8dd0c29ae3cad69abe6310de63ac081f46ef019c5c90897caefd79b796cfa81139788a260ded52df", "abcdefghi") - , ("a12070030a02d86b0ddacd0d3a5b598344513d0a051e7355053e556a0055489c1555399b03342845c4adde2dc44ff66c", "abcdefghij") - , ("86f58ec2d74d1b7f8eb0c2ff0967316699639e8d4eb129de54bdf34c96cdbabe200d052149f2dd787f43571ba74670d4", "Discard medicine more than two years old.") - , ("ae4a2b639ca9bfa04b1855d5a05fe7f230994f790891c6979103e2605f660c4c1262a48142dcbeb57a1914ba5f7c3fa7", "He who has a shady past knows that nice guys finish last.") - , ("40ae213df6436eca952aa6841886fcdb82908ef1576a99c8f49bb9dd5023169f7c53035abdda0b54c302f4974e2105e7", "I wouldn't marry him with a ten foot pole.") - , ("e7cf8b873c9bc950f06259aa54309f349cefa72c00d597aebf903e6519a50011dfe355afff064a10701c705693848df9", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") - , ("c3d4f0f4047181c7d39d34703365f7bf70207183caf2c2f6145f04da895ef69124d9cdeb635da636c3a474e61024e29b", "The days of the digital watch are numbered. -Tom Stoppard") - , ("a097aab567e167d5cf93676ed73252a69f9687cb3179bb2d27c9878119e94bf7b7c4b58dc90582edfaf66e11388ed714", "Nepal premier won't resign.") - , ("5026ca45c41fc64712eb65065da92f6467541c78f8966d3fe2c8e3fb769a3ec14215f819654b47bd64f7f0eac17184f3", "For every action there is an equal and opposite government program.") - , ("ac1cc0f5ac8d5f5514a7b738ac322b7fb52a161b449c3672e9b6a6ad1a5e4b26b001cf3bad24c56598676ca17d4b445a", "His money is twice tainted: 'taint yours and 'taint mine.") - , ("722d10c5de371ec0c8c4b5247ac8a5f1d240d68c73f8da13d8b25f0166d6f309bf9561979a111a0049405771d201941a", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") - , ("dc2d3ea18bfa10549c63bf2b75b39b5167a80c12aff0e05443168ea87ff149fb0eda5e0bd234eb5d48c7d02ffc5807f1", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") - , ("1d67c969e2a945ae5346d2139760261504d4ba164c522443afe19ef3e29b152a4c52445489cfc9d7215e5a450e8e1e4e", "size: a.out: bad magic") - , ("5ff8e075e465646e7b73ef36d812c6e9f7d60fa6ea0e533e5569b4f73cde53cdd2cc787f33540af57cca3fe467d32fe0", "The major problem is with sendmail. -Mark Horton") - , ("5bd0a997a67c9ae1979a894eb0cde403dde003c9b6f2c03cf21925c42ff4e1176e6df1ca005381612ef18457b9b7ec3b", "Give me a rock, paper and scissors and I will move the world. CCFestoon") - , ("1eee6da33e7e54fc5be52ae23b94b16ba4d2a947ae4505c6a3edfc7401151ea5205ac01b669b56f27d8ef7f175ed7762", "If the enemy is within range, then so are you.") - , ("76b06e9dea66bfbb1a96029426dc0dfd7830bd297eb447ff5358d94a87cd00c88b59df2493fef56ecbb5231073892ea9", "It's well we cannot hear the screams/That we create in others' dreams.") - , ("12acaf21452cff586143e3f5db0bfdf7802c057e1adf2a619031c4e1b0ccc4208cf6cef8fe722bbaa2fb46a30d9135d8", "You remind me of a TV show, but that's all right: I watch it anyway.") - , ("0fc23d7f4183efd186f0bc4fc5db867e026e2146b06cb3d52f4bdbd57d1740122caa853b41868b197b2ac759db39df88", "C is as portable as Stonehedge!!") - , ("bc805578a7f85d34a86a32976e1c34fe65cf815186fbef76f46ef99cda10723f971f3f1464d488243f5e29db7488598d", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") - , ("b23918399a12ebf4431559eec3813eaf7412e875fd7464f16d581e473330842d2e96c6be49a7ce3f9bb0b8bc0fcbe0fe", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") - , ("1764b700eb1ead52a2fc33cc28975c2180f1b8faa5038d94cffa8d78154aab16e91dd787e7b0303948ebed62561542c8", "How can you write a big system without C++? -Paul Glick") - ] - -testGoSha512 = testGroup "SHA-512" $ - map (tableTestCase sha512sum) - [ ("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "") - , ("1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75", "a") - , ("2d408a0717ec188158278a796c689044361dc6fdde28d6f04973b80896e1823975cdbf12eb63f9e0591328ee235d80e9b5bf1aa6a44f4617ff3caf6400eb172d", "ab") - , ("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", "abc") - , ("d8022f2060ad6efd297ab73dcc5355c9b214054b0d1776a136a669d26a7d3b14f73aa0d0ebff19ee333368f0164b6419a96da49e3e481753e7e96b716bdccb6f", "abcd") - , ("878ae65a92e86cac011a570d4c30a7eaec442b85ce8eca0c2952b5e3cc0628c2e79d889ad4d5c7c626986d452dd86374b6ffaa7cd8b67665bef2289a5c70b0a1", "abcde") - , ("e32ef19623e8ed9d267f657a81944b3d07adbb768518068e88435745564e8d4150a0a703be2a7d88b61e3d390c2bb97e2d4c311fdc69d6b1267f05f59aa920e7", "abcdef") - , ("d716a4188569b68ab1b6dfac178e570114cdf0ea3a1cc0e31486c3e41241bc6a76424e8c37ab26f096fc85ef9886c8cb634187f4fddff645fb099f1ff54c6b8c", "abcdefg") - , ("a3a8c81bc97c2560010d7389bc88aac974a104e0e2381220c6e084c4dccd1d2d17d4f86db31c2a851dc80e6681d74733c55dcd03dd96f6062cdda12a291ae6ce", "abcdefgh") - , ("f22d51d25292ca1d0f68f69aedc7897019308cc9db46efb75a03dd494fc7f126c010e8ade6a00a0c1a5f1b75d81e0ed5a93ce98dc9b833db7839247b1d9c24fe", "abcdefghi") - , ("ef6b97321f34b1fea2169a7db9e1960b471aa13302a988087357c520be957ca119c3ba68e6b4982c019ec89de3865ccf6a3cda1fe11e59f98d99f1502c8b9745", "abcdefghij") - , ("2210d99af9c8bdecda1b4beff822136753d8342505ddce37f1314e2cdbb488c6016bdaa9bd2ffa513dd5de2e4b50f031393d8ab61f773b0e0130d7381e0f8a1d", "Discard medicine more than two years old.") - , ("a687a8985b4d8d0a24f115fe272255c6afaf3909225838546159c1ed685c211a203796ae8ecc4c81a5b6315919b3a64f10713da07e341fcdbb08541bf03066ce", "He who has a shady past knows that nice guys finish last.") - , ("8ddb0392e818b7d585ab22769a50df660d9f6d559cca3afc5691b8ca91b8451374e42bcdabd64589ed7c91d85f626596228a5c8572677eb98bc6b624befb7af8", "I wouldn't marry him with a ten foot pole.") - , ("26ed8f6ca7f8d44b6a8a54ae39640fa8ad5c673f70ee9ce074ba4ef0d483eea00bab2f61d8695d6b34df9c6c48ae36246362200ed820448bdc03a720366a87c6", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave") - , ("e5a14bf044be69615aade89afcf1ab0389d5fc302a884d403579d1386a2400c089b0dbb387ed0f463f9ee342f8244d5a38cfbc0e819da9529fbff78368c9a982", "The days of the digital watch are numbered. -Tom Stoppard") - , ("420a1faa48919e14651bed45725abe0f7a58e0f099424c4e5a49194946e38b46c1f8034b18ef169b2e31050d1648e0b982386595f7df47da4b6fd18e55333015", "Nepal premier won't resign.") - , ("d926a863beadb20134db07683535c72007b0e695045876254f341ddcccde132a908c5af57baa6a6a9c63e6649bba0c213dc05fadcf9abccea09f23dcfb637fbe", "For every action there is an equal and opposite government program.") - , ("9a98dd9bb67d0da7bf83da5313dff4fd60a4bac0094f1b05633690ffa7f6d61de9a1d4f8617937d560833a9aaa9ccafe3fd24db418d0e728833545cadd3ad92d", "His money is twice tainted: 'taint yours and 'taint mine.") - , ("d7fde2d2351efade52f4211d3746a0780a26eec3df9b2ed575368a8a1c09ec452402293a8ea4eceb5a4f60064ea29b13cdd86918cd7a4faf366160b009804107", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977") - , ("b0f35ffa2697359c33a56f5c0cf715c7aeed96da9905ca2698acadb08fbc9e669bf566b6bd5d61a3e86dc22999bcc9f2224e33d1d4f32a228cf9d0349e2db518", "It's a tiny change to the code and not completely disgusting. - Bob Manchek") - , ("3d2e5f91778c9e66f7e061293aaa8a8fc742dd3b2e4f483772464b1144189b49273e610e5cccd7a81a19ca1fa70f16b10f1a100a4d8c1372336be8484c64b311", "size: a.out: bad magic") - , ("b2f68ff58ac015efb1c94c908b0d8c2bf06f491e4de8e6302c49016f7f8a33eac3e959856c7fddbc464de618701338a4b46f76dbfaf9a1e5262b5f40639771c7", "The major problem is with sendmail. -Mark Horton") - , ("d8c92db5fdf52cf8215e4df3b4909d29203ff4d00e9ad0b64a6a4e04dec5e74f62e7c35c7fb881bd5de95442123df8f57a489b0ae616bd326f84d10021121c57", "Give me a rock, paper and scissors and I will move the world. CCFestoon") - , ("19a9f8dc0a233e464e8566ad3ca9b91e459a7b8c4780985b015776e1bf239a19bc233d0556343e2b0a9bc220900b4ebf4f8bdf89ff8efeaf79602d6849e6f72e", "If the enemy is within range, then so are you.") - , ("00b4c41f307bde87301cdc5b5ab1ae9a592e8ecbb2021dd7bc4b34e2ace60741cc362560bec566ba35178595a91932b8d5357e2c9cec92d393b0fa7831852476", "It's well we cannot hear the screams/That we create in others' dreams.") - , ("91eccc3d5375fd026e4d6787874b1dce201cecd8a27dbded5065728cb2d09c58a3d467bb1faf353bf7ba567e005245d5321b55bc344f7c07b91cb6f26c959be7", "You remind me of a TV show, but that's all right: I watch it anyway.") - , ("fabbbe22180f1f137cfdc9556d2570e775d1ae02a597ded43a72a40f9b485d500043b7be128fb9fcd982b83159a0d99aa855a9e7cc4240c00dc01a9bdf8218d7", "C is as portable as Stonehedge!!") - , ("2ecdec235c1fa4fc2a154d8fba1dddb8a72a1ad73838b51d792331d143f8b96a9f6fcb0f34d7caa351fe6d88771c4f105040e0392f06e0621689d33b2f3ba92e", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley") - , ("7ad681f6f96f82f7abfa7ecc0334e8fa16d3dc1cdc45b60b7af43fe4075d2357c0c1d60e98350f1afb1f2fe7a4d7cd2ad55b88e458e06b73c40b437331f5dab4", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule") - , ("833f9248ab4a3b9e5131f745fda1ffd2dd435b30e965957e78291c7ab73605fd1912b0794e5c233ab0a12d205a39778d19b83515d6a47003f19cdee51d98c7e0", "How can you write a big system without C++? -Paul Glick") - ] - --- | Tests our SHA-2 implementations against coreutils'. -testCoreutilsConformance = testGroup "conformance with coreutils" - [ testProperty "SHA-224" $ testAgainstCoreutils sha224sum "sha224sum" - , testProperty "SHA-256" $ testAgainstCoreutils sha256sum "sha256sum" - , testProperty "SHA-384" $ testAgainstCoreutils sha384sum "sha384sum" - , testProperty "SHA-512" $ testAgainstCoreutils sha512sum "sha512sum" - ] - --- | Tests our SHA-2 implementations against openssl(1)'s. -testOpensslConformance = testGroup "conformance with OpenSSL" - [ testProperty "SHA-224" $ testAgainstOpenssl sha224sum "sha224" - , testProperty "SHA-256" $ testAgainstOpenssl sha256sum "sha256" - , testProperty "SHA-384" $ testAgainstOpenssl sha384sum "sha384" - , testProperty "SHA-512" $ testAgainstOpenssl sha512sum "sha512" - ] - --- Convenience functions. -sha224sum = show . hash sha224 . ByteString.Lazy.fromStrict -sha256sum = show . hash sha256 . ByteString.Lazy.fromStrict -sha384sum = show . hash sha384 . ByteString.Lazy.fromStrict -sha512sum = show . hash sha512 . ByteString.Lazy.fromStrict diff --git a/tests/Data/DigestTests.hs b/tests/Data/DigestTests.hs index daef282..5bcf3b1 100644 --- a/tests/Data/DigestTests.hs +++ b/tests/Data/DigestTests.hs @@ -16,13 +16,13 @@ module Data.DigestTests (tests) where import Test.Tasty (TestTree, testGroup) -import qualified Data.Digest.Md5Tests -import qualified Data.Digest.Sha1Tests -import qualified Data.Digest.Sha2Tests +import qualified Data.Digest.MD5Tests +import qualified Data.Digest.SHA1Tests +import qualified Data.Digest.SHA2Tests tests :: TestTree tests = testGroup "Data.Digest" - [ Data.Digest.Md5Tests.tests - , Data.Digest.Sha1Tests.tests - , Data.Digest.Sha2Tests.tests + [ Data.Digest.MD5Tests.tests + , Data.Digest.SHA1Tests.tests + , Data.Digest.SHA2Tests.tests ] diff --git a/tests/Data/HMACTests.hs b/tests/Data/HMACTests.hs new file mode 100644 index 0000000..06ee8eb --- /dev/null +++ b/tests/Data/HMACTests.hs @@ -0,0 +1,192 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +{-# LANGUAGE OverloadedStrings #-} + +module Data.HMACTests (tests) where + +import qualified Data.ByteString as ByteString +import qualified Data.ByteString.Lazy as ByteString.Lazy +import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy.Char8 +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit ((@?=), testCase) + +import Data.Digest (md5, sha1, sha224, sha256, sha384, sha512) +import Data.HMAC (SecretKey(SecretKey), hmac) + +type LazyByteString = ByteString.Lazy.ByteString + +tests :: TestTree +tests = testGroup "Data.HMAC" + [ testRFC2202 + , testFips198 + , testRFC4231 + ] + +tableTestCase :: + (SecretKey -> LazyByteString -> String) + -> (SecretKey, LazyByteString, String) + -> TestTree +tableTestCase f (key, input, output) = + testCase (abbreviate input) (f key input @?= output) + +abbreviate :: LazyByteString -> String +abbreviate input = + let (x, y) = splitAt 12 (show input) in + x ++ (if null y then "" else "...") ++ "\"" + +-- | Tests from RFC 2202. +testRFC2202 = testGroup "RFC 2202" [testMd5, testSha1] + where + testMd5 = testGroup "MD5" $ + map (tableTestCase hmacMd5) + [ ( SecretKey (ByteString.replicate 16 0x0b) + , "Hi There" + , "9294727a3638bb1c13f48ef8158bfc9d") + , ( SecretKey "Jefe" + , "what do ya want for nothing?" + , "750c783e6ab0b503eaa86e310a5db738") + , ( SecretKey (ByteString.replicate 16 0xaa) + , ByteString.Lazy.replicate 50 0xdd + , "56be34521d144c88dbb8c733f0e8b3f6") + , ( SecretKey (ByteString.pack [0x01 .. 0x19]) + , ByteString.Lazy.replicate 50 0xcd + , "697eaf0aca3a3aea3a75164746ffaa79") + , ( SecretKey (ByteString.replicate 16 0x0c) + , "Test With Truncation" + , "56461ef2342edc00f9bab995690efd4c") + , ( SecretKey (ByteString.replicate 80 0xaa) + , "Test Using Larger Than Block-Size Key - Hash Key First" + , "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") + , ( SecretKey (ByteString.replicate 80 0xaa) + , "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" + , "6f630fad67cda0ee1fb1f562db3aa53e") + ] + testSha1 = testGroup "SHA-1" $ + map (tableTestCase hmacSha1) + [ ( SecretKey (ByteString.replicate 20 0x0b) + , "Hi There" + , "b617318655057264e28bc0b6fb378c8ef146be00") + , ( SecretKey "Jefe" + , "what do ya want for nothing?" + , "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") + , ( SecretKey (ByteString.replicate 20 0xaa) + , ByteString.Lazy.replicate 50 0xdd + , "125d7342b9ac11cd91a39af48aa17b4f63f175d3") + , ( SecretKey (ByteString.pack [0x01 .. 0x19]) + , ByteString.Lazy.replicate 50 0xcd + , "4c9007f4026250c6bc8414f9bf50c86c2d7235da") + , ( SecretKey (ByteString.replicate 20 0x0c) + , "Test With Truncation" + , "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") + , ( SecretKey (ByteString.replicate 80 0xaa) + , "Test Using Larger Than Block-Size Key - Hash Key First" + , "aa4ae5e15272d00e95705637ce8a3b55ed402112") + , ( SecretKey (ByteString.replicate 80 0xaa) + , "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" + , "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") + ] + +-- | Tests from FIPS 198. +testFips198 = testGroup "FIPS 198" $ + map (tableTestCase hmacSha1) + [ ( SecretKey (ByteString.pack [0 .. 0x3f]) + , "Sample #1" + , "4f4ca3d5d68ba7cc0a1208c9c61e9c5da0403c0a") + , ( SecretKey (ByteString.pack [0x30 .. 0x43]) + , "Sample #2" + , "0922d3405faa3d194f82a45830737d5cc6c75d24") + , ( SecretKey (ByteString.pack [0x50 .. 0xb3]) + , "Sample #3" + , "bcf41eab8bb2d802f3d05caf7cb092ecf8d1a3aa") + ] + ++ [truncatedFips198Test] + +truncatedFips198Test = + let input = "Sample #4" :: String + key = SecretKey (ByteString.pack [0x70 .. 0xa0]) + output = "9ea886efe268dbecce420c75" :: String + in testCase + (show input) + (take 24 (hmacSha1 key (ByteString.Lazy.Char8.pack input)) @?= output) + +rfc4231TestCase :: + (SecretKey, LazyByteString, String, String, String, String) -> TestTree +rfc4231TestCase (key, input, sha224Output, sha256Output, sha384Output, sha512Output) = + testGroup (abbreviate input) + [ testCase "SHA-224" (hmacSha224 key input @?= sha224Output) + , testCase "SHA-256" (hmacSha256 key input @?= sha256Output) + , testCase "SHA-384" (hmacSha384 key input @?= sha384Output) + , testCase "SHA-512" (hmacSha512 key input @?= sha512Output) + ] + +-- | Tests from RFC 4231. +testRFC4231 = testGroup "RFC 4231" $ + map rfc4231TestCase + [ ( SecretKey (ByteString.replicate 20 0x0b) + , "Hi There" + , "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22" + , "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" + , "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6" + , "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854") + , ( SecretKey "Jefe" + , "what do ya want for nothing?" + , "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44" + , "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" + , "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649" + , "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737") + , ( SecretKey (ByteString.replicate 20 0xaa) + , ByteString.Lazy.replicate 50 0xdd + , "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea" + , "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe" + , "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27" + , "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb") + , ( SecretKey (ByteString.pack [0x01 .. 0x19]) + , ByteString.Lazy.replicate 50 0xcd + , "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a" + , "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b" + , "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffb" + , "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd") + , ( SecretKey (ByteString.replicate 131 0xaa) + , "Test Using Larger Than Block-Size Key - Hash Key First" + , "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e" + , "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54" + , "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952" + , "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598") + , ( SecretKey (ByteString.replicate 131 0xaa) + , "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." + , "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1" + , "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2" + , "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523e" + , "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58") + ] + ++ [truncatedRFC4231Test] + +truncatedRFC4231Test = + let key = SecretKey (ByteString.replicate 20 0x0c) + input = "Test With Truncation" :: LazyByteString + t f = take 32 (f key input) :: String + in testGroup (abbreviate input) + [ testCase "SHA-224" (t hmacSha224 @?= "0e2aea68a90c8d37c988bcdb9fca6fa8") + , testCase "SHA-256" (t hmacSha256 @?= "a3b6167473100ee06e0c796c2955552b") + , testCase "SHA-384" (t hmacSha384 @?= "3abf34c3503b2a23a46efc619baef897") + , testCase "SHA-512" (t hmacSha512 @?= "415fad6271580a531d4179bc891d87a6") + ] + +hmacMd5 key bytes = show $ hmac md5 key bytes +hmacSha1 key bytes = show $ hmac sha1 key bytes +hmacSha224 key bytes = show $ hmac sha224 key bytes +hmacSha256 key bytes = show $ hmac sha256 key bytes +hmacSha384 key bytes = show $ hmac sha384 key bytes +hmacSha512 key bytes = show $ hmac sha512 key bytes diff --git a/tests/Data/HmacTests.hs b/tests/Data/HmacTests.hs deleted file mode 100644 index c1abc38..0000000 --- a/tests/Data/HmacTests.hs +++ /dev/null @@ -1,192 +0,0 @@ --- Copyright 2018 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -{-# LANGUAGE OverloadedStrings #-} - -module Data.HmacTests (tests) where - -import qualified Data.ByteString as ByteString -import qualified Data.ByteString.Lazy as ByteString.Lazy -import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy.Char8 -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit ((@?=), testCase) - -import Data.Digest (md5, sha1, sha224, sha256, sha384, sha512) -import Data.Hmac (SecretKey(SecretKey), hmac) - -type LazyByteString = ByteString.Lazy.ByteString - -tests :: TestTree -tests = testGroup "Data.Hmac" - [ testRfc2202 - , testFips198 - , testRfc4231 - ] - -tableTestCase :: - (SecretKey -> LazyByteString -> String) - -> (SecretKey, LazyByteString, String) - -> TestTree -tableTestCase f (key, input, output) = - testCase (abbreviate input) (f key input @?= output) - -abbreviate :: LazyByteString -> String -abbreviate input = - let (x, y) = splitAt 12 (show input) in - x ++ (if null y then "" else "...") ++ "\"" - --- | Tests from RFC 2202. -testRfc2202 = testGroup "RFC 2202" [testMd5, testSha1] - where - testMd5 = testGroup "MD5" $ - map (tableTestCase hmacMd5) - [ ( SecretKey (ByteString.replicate 16 0x0b) - , "Hi There" - , "9294727a3638bb1c13f48ef8158bfc9d") - , ( SecretKey "Jefe" - , "what do ya want for nothing?" - , "750c783e6ab0b503eaa86e310a5db738") - , ( SecretKey (ByteString.replicate 16 0xaa) - , ByteString.Lazy.replicate 50 0xdd - , "56be34521d144c88dbb8c733f0e8b3f6") - , ( SecretKey (ByteString.pack [0x01 .. 0x19]) - , ByteString.Lazy.replicate 50 0xcd - , "697eaf0aca3a3aea3a75164746ffaa79") - , ( SecretKey (ByteString.replicate 16 0x0c) - , "Test With Truncation" - , "56461ef2342edc00f9bab995690efd4c") - , ( SecretKey (ByteString.replicate 80 0xaa) - , "Test Using Larger Than Block-Size Key - Hash Key First" - , "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") - , ( SecretKey (ByteString.replicate 80 0xaa) - , "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" - , "6f630fad67cda0ee1fb1f562db3aa53e") - ] - testSha1 = testGroup "SHA-1" $ - map (tableTestCase hmacSha1) - [ ( SecretKey (ByteString.replicate 20 0x0b) - , "Hi There" - , "b617318655057264e28bc0b6fb378c8ef146be00") - , ( SecretKey "Jefe" - , "what do ya want for nothing?" - , "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") - , ( SecretKey (ByteString.replicate 20 0xaa) - , ByteString.Lazy.replicate 50 0xdd - , "125d7342b9ac11cd91a39af48aa17b4f63f175d3") - , ( SecretKey (ByteString.pack [0x01 .. 0x19]) - , ByteString.Lazy.replicate 50 0xcd - , "4c9007f4026250c6bc8414f9bf50c86c2d7235da") - , ( SecretKey (ByteString.replicate 20 0x0c) - , "Test With Truncation" - , "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") - , ( SecretKey (ByteString.replicate 80 0xaa) - , "Test Using Larger Than Block-Size Key - Hash Key First" - , "aa4ae5e15272d00e95705637ce8a3b55ed402112") - , ( SecretKey (ByteString.replicate 80 0xaa) - , "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" - , "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") - ] - --- | Tests from FIPS 198. -testFips198 = testGroup "FIPS 198" $ - map (tableTestCase hmacSha1) - [ ( SecretKey (ByteString.pack [0 .. 0x3f]) - , "Sample #1" - , "4f4ca3d5d68ba7cc0a1208c9c61e9c5da0403c0a") - , ( SecretKey (ByteString.pack [0x30 .. 0x43]) - , "Sample #2" - , "0922d3405faa3d194f82a45830737d5cc6c75d24") - , ( SecretKey (ByteString.pack [0x50 .. 0xb3]) - , "Sample #3" - , "bcf41eab8bb2d802f3d05caf7cb092ecf8d1a3aa") - ] - ++ [truncatedFips198Test] - -truncatedFips198Test = - let input = "Sample #4" :: String - key = SecretKey (ByteString.pack [0x70 .. 0xa0]) - output = "9ea886efe268dbecce420c75" :: String - in testCase - (show input) - (take 24 (hmacSha1 key (ByteString.Lazy.Char8.pack input)) @?= output) - -rfc4231TestCase :: - (SecretKey, LazyByteString, String, String, String, String) -> TestTree -rfc4231TestCase (key, input, sha224Output, sha256Output, sha384Output, sha512Output) = - testGroup (abbreviate input) - [ testCase "SHA-224" (hmacSha224 key input @?= sha224Output) - , testCase "SHA-256" (hmacSha256 key input @?= sha256Output) - , testCase "SHA-384" (hmacSha384 key input @?= sha384Output) - , testCase "SHA-512" (hmacSha512 key input @?= sha512Output) - ] - --- | Tests from RFC 4231. -testRfc4231 = testGroup "RFC 4231" $ - map rfc4231TestCase - [ ( SecretKey (ByteString.replicate 20 0x0b) - , "Hi There" - , "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22" - , "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" - , "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6" - , "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854") - , ( SecretKey "Jefe" - , "what do ya want for nothing?" - , "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44" - , "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" - , "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649" - , "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737") - , ( SecretKey (ByteString.replicate 20 0xaa) - , ByteString.Lazy.replicate 50 0xdd - , "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea" - , "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe" - , "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27" - , "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb") - , ( SecretKey (ByteString.pack [0x01 .. 0x19]) - , ByteString.Lazy.replicate 50 0xcd - , "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a" - , "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b" - , "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffb" - , "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd") - , ( SecretKey (ByteString.replicate 131 0xaa) - , "Test Using Larger Than Block-Size Key - Hash Key First" - , "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e" - , "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54" - , "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952" - , "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598") - , ( SecretKey (ByteString.replicate 131 0xaa) - , "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." - , "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1" - , "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2" - , "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523e" - , "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58") - ] - ++ [truncatedRfc4231Test] - -truncatedRfc4231Test = - let key = SecretKey (ByteString.replicate 20 0x0c) - input = "Test With Truncation" :: LazyByteString - t f = take 32 (f key input) :: String - in testGroup (abbreviate input) - [ testCase "SHA-224" (t hmacSha224 @?= "0e2aea68a90c8d37c988bcdb9fca6fa8") - , testCase "SHA-256" (t hmacSha256 @?= "a3b6167473100ee06e0c796c2955552b") - , testCase "SHA-384" (t hmacSha384 @?= "3abf34c3503b2a23a46efc619baef897") - , testCase "SHA-512" (t hmacSha512 @?= "415fad6271580a531d4179bc891d87a6") - ] - -hmacMd5 key bytes = show $ hmac md5 key bytes -hmacSha1 key bytes = show $ hmac sha1 key bytes -hmacSha224 key bytes = show $ hmac sha224 key bytes -hmacSha256 key bytes = show $ hmac sha256 key bytes -hmacSha384 key bytes = show $ hmac sha384 key bytes -hmacSha512 key bytes = show $ hmac sha512 key bytes diff --git a/tests/Tests.hs b/tests/Tests.hs index 58e9be6..ee38f2a 100644 --- a/tests/Tests.hs +++ b/tests/Tests.hs @@ -19,10 +19,10 @@ module Main import Test.Tasty (defaultMain, testGroup) import qualified Data.DigestTests -import qualified Data.HmacTests +import qualified Data.HMACTests main :: IO () main = defaultMain $ testGroup "btls" [ Data.DigestTests.tests - , Data.HmacTests.tests + , Data.HMACTests.tests ] -- cgit v1.2.3