diff --git a/Crypto/Hash/Algorithms.hs b/Crypto/Hash/Algorithms.hs index d34fd34..094a595 100644 --- a/Crypto/Hash/Algorithms.hs +++ b/Crypto/Hash/Algorithms.hs @@ -11,10 +11,15 @@ module Crypto.Hash.Algorithms ( HashAlgorithm -- * hash algorithms + , Blake2s_160(..) , Blake2s_224(..) - , Blake2sp_224(..) , Blake2s_256(..) + , Blake2sp_224(..) , Blake2sp_256(..) + , Blake2b_160(..) + , Blake2b_224(..) + , Blake2b_256(..) + , Blake2b_384(..) , Blake2b_512(..) , Blake2bp_512(..) , MD2(..) diff --git a/Crypto/Hash/Blake2b.hs b/Crypto/Hash/Blake2b.hs index fdf1e9e..c22c284 100644 --- a/Crypto/Hash/Blake2b.hs +++ b/Crypto/Hash/Blake2b.hs @@ -11,7 +11,7 @@ {-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE DeriveDataTypeable #-} module Crypto.Hash.Blake2b - ( Blake2b_512 (..) + ( Blake2b_160 (..), Blake2b_224 (..), Blake2b_256 (..), Blake2b_384 (..), Blake2b_512 (..) ) where import Crypto.Hash.Types @@ -21,6 +21,54 @@ import Data.Typeable import Data.Word (Word8, Word32) +-- | Blake2b (160 bits) cryptographic hash algorithm +data Blake2b_160 = Blake2b_160 + deriving (Show,Data,Typeable) + +instance HashAlgorithm Blake2b_160 where + hashBlockSize _ = 128 + hashDigestSize _ = 20 + hashInternalContextSize _ = 361 + hashInternalInit p = c_blake2b_init p 160 + hashInternalUpdate = c_blake2b_update + hashInternalFinalize p = c_blake2b_finalize p 160 + +-- | Blake2b (224 bits) cryptographic hash algorithm +data Blake2b_224 = Blake2b_224 + deriving (Show,Data,Typeable) + +instance HashAlgorithm Blake2b_224 where + hashBlockSize _ = 128 + hashDigestSize _ = 28 + hashInternalContextSize _ = 361 + hashInternalInit p = c_blake2b_init p 224 + hashInternalUpdate = c_blake2b_update + hashInternalFinalize p = c_blake2b_finalize p 224 + +-- | Blake2b (256 bits) cryptographic hash algorithm +data Blake2b_256 = Blake2b_256 + deriving (Show,Data,Typeable) + +instance HashAlgorithm Blake2b_256 where + hashBlockSize _ = 128 + hashDigestSize _ = 32 + hashInternalContextSize _ = 361 + hashInternalInit p = c_blake2b_init p 256 + hashInternalUpdate = c_blake2b_update + hashInternalFinalize p = c_blake2b_finalize p 256 + +-- | Blake2b (384 bits) cryptographic hash algorithm +data Blake2b_384 = Blake2b_384 + deriving (Show,Data,Typeable) + +instance HashAlgorithm Blake2b_384 where + hashBlockSize _ = 128 + hashDigestSize _ = 48 + hashInternalContextSize _ = 361 + hashInternalInit p = c_blake2b_init p 384 + hashInternalUpdate = c_blake2b_update + hashInternalFinalize p = c_blake2b_finalize p 384 + -- | Blake2b (512 bits) cryptographic hash algorithm data Blake2b_512 = Blake2b_512 deriving (Show,Data,Typeable) diff --git a/Crypto/Hash/Blake2s.hs b/Crypto/Hash/Blake2s.hs index 845ebf7..076fe77 100644 --- a/Crypto/Hash/Blake2s.hs +++ b/Crypto/Hash/Blake2s.hs @@ -11,7 +11,7 @@ {-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE DeriveDataTypeable #-} module Crypto.Hash.Blake2s - ( Blake2s_224 (..), Blake2s_256 (..) + ( Blake2s_160 (..), Blake2s_224 (..), Blake2s_256 (..) ) where import Crypto.Hash.Types @@ -21,6 +21,18 @@ import Data.Typeable import Data.Word (Word8, Word32) +-- | Blake2s (160 bits) cryptographic hash algorithm +data Blake2s_160 = Blake2s_160 + deriving (Show,Data,Typeable) + +instance HashAlgorithm Blake2s_160 where + hashBlockSize _ = 64 + hashDigestSize _ = 20 + hashInternalContextSize _ = 185 + hashInternalInit p = c_blake2s_init p 160 + hashInternalUpdate = c_blake2s_update + hashInternalFinalize p = c_blake2s_finalize p 160 + -- | Blake2s (224 bits) cryptographic hash algorithm data Blake2s_224 = Blake2s_224 deriving (Show,Data,Typeable) diff --git a/gen/Gen.hs b/gen/Gen.hs index 6b5dd12..18ce39c 100644 --- a/gen/Gen.hs +++ b/gen/Gen.hs @@ -58,9 +58,9 @@ data HashCustom = hashModules = -- module header hash ctx dg blk - [ GenHashModule "Blake2s" "blake2.h" "blake2s" 185 (HashMulti [] [(224,64), (256,64)]) + [ GenHashModule "Blake2s" "blake2.h" "blake2s" 185 (HashMulti [] [(160, 64), (224,64), (256,64)]) , GenHashModule "Blake2sp" "blake2.h" "blake2sp" 2185 (HashMulti [] [(224,64), (256,64)]) - , GenHashModule "Blake2b" "blake2.h" "blake2b" 361 (HashMulti [] [(512,128)]) + , GenHashModule "Blake2b" "blake2.h" "blake2b" 361 (HashMulti [] [(160, 128), (224, 128), (256, 128), (384, 128), (512,128)]) , GenHashModule "Blake2bp" "blake2.h" "blake2sp" 2325 (HashMulti [] [(512,128)]) , GenHashModule "MD2" "md2.h" "md2" 96 (HashSimple 128 16) , GenHashModule "MD4" "md4.h" "md4" 96 (HashSimple 128 64) diff --git a/tests/Hash.hs b/tests/Hash.hs index f8fe8a5..19d6edf 100644 --- a/tests/Hash.hs +++ b/tests/Hash.hs @@ -138,10 +138,38 @@ expected = [ "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26", "01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450", "28e361fe8c56e617caa56c28c7c36e5c13be552b77081be82b642f08bb7ef085b9a81910fe98269386b9aacfd2349076c9506126e198f6f6ad44c12017ca77b1" ]) + , ("Blake2b-160", HashAlg Blake2b_160, [ + "3345524abf6bbe1809449224b5972c41790b6cf2", + "3c523ed102ab45a37d54f5610d5a983162fde84f", + "a3d365b5fba5d36fbb19c03b7fde496058969c5a" ]) + , ("Blake2b-224", HashAlg Blake2b_224, [ + "836cc68931c2e4e3e838602eca1902591d216837bafddfe6f0c8cb07", + "477c3985751dd4d1b8c93827ea5310b33bb02a26463a050dffd3e857", + "a4a1b6851be66891a3deff406c4d7556879ebf952407450755f90eb6" ]) + , ("Blake2b-256", HashAlg Blake2b_256, [ + "0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8", + "01718cec35cd3d796dd00020e0bfecb473ad23457d063b75eff29c0ffa2e58a9", + "036c13096926b3dfccfe3f233bd1b2f583b818b8b15c01be65af69238e900b2c" ]) + , ("Blake2b-384", HashAlg Blake2b_384, [ + "b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100", + "b7c81b228b6bd912930e8f0b5387989691c1cee1e65aade4da3b86a3c9f678fc8018f6ed9e2906720c8d2a3aeda9c03d", + "927a1f297873cbe887a93b2183c4e2eba53966ba92c6db8b87029a1d8c673471d09740676cced79c5016838973f630c3" ]) , ("Blake2b-512", HashAlg Blake2b_512, [ "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce", "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918", "af438eea5d8cdb209336a7e85bf58090dc21b49d823f89a7d064c119f127bd361af9c7d109edda0f0e91bdce078d1d86b8e6f25727c98f6d3bb6f50acb2dd376" ]) + , ("Blake2s-160", HashAlg Blake2s_160, [ + "354c9c33f735962418bdacb9479873429c34916f", + "5a604fec9713c369e84b0ed68daed7d7504ef240", + "759bef6d041bcbd861b8b51baaece6c8fffd0acf" ]) + , ("Blake2s-224", HashAlg Blake2s_224, [ + "1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4", + "e4e5cb6c7cae41982b397bf7b7d2d9d1949823ae78435326e8db4912", + "e220025fd46a9a635c3f7f60bb96a84c01019ac0817f5901e7eeaa2c" ]) + , ("Blake2s-256", HashAlg Blake2s_256, [ + "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9", + "606beeec743ccbeff6cbcdf5d5302aa855c256c29b88c8ed331ea1a6bf3c8812", + "94662583a600a12dff357c0a6f1b514a710ef0f587a38e8d2e4d7f67e9c81667" ]) ] runhash :: HashAlg -> ByteString -> ByteString