[Blake2] define the algorithm as a multiple algorithm so that the output digest size is explicit in the digest types.

This commit is contained in:
Vincent Hanquez 2015-11-19 12:10:14 +00:00
parent 69f9d225eb
commit fae5f084cf
15 changed files with 88 additions and 68 deletions

View File

@ -10,10 +10,10 @@
module Crypto.Hash.Algorithms
( HashAlgorithm
-- * hash algorithms
, BLAKE2s(..)
, BLAKE2sp(..)
, BLAKE2b(..)
, BLAKE2bp(..)
, BLAKE2s_256(..)
, BLAKE2sp_256(..)
, BLAKE2b_512(..)
, BLAKE2bp_512(..)
, MD2(..)
, MD4(..)
, MD5(..)

View File

@ -9,29 +9,33 @@
-- BLAKE2b cryptographic hash.
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.BLAKE2b ( BLAKE2b (..) ) where
module Crypto.Hash.BLAKE2b
( BLAKE2b_512 (..)
) where
import Crypto.Hash.Types
import Foreign.Ptr (Ptr)
import Data.Word (Word8, Word32)
-- | BLAKE2b cryptographic hash algorithm
data BLAKE2b = BLAKE2b
-- | BLAKE2b (512 bits) cryptographic hash algorithm
data BLAKE2b_512 = BLAKE2b_512
deriving (Show)
instance HashAlgorithm BLAKE2b where
instance HashAlgorithm BLAKE2b_512 where
hashBlockSize _ = 128
hashDigestSize _ = 64
hashInternalContextSize _ = 361
hashInternalInit = c_blake2b_init
hashInternalInit p = c_blake2b_init p 512
hashInternalUpdate = c_blake2b_update
hashInternalFinalize = c_blake2b_finalize
hashInternalFinalize p = c_blake2b_finalize p 512
foreign import ccall unsafe "cryptonite_blake2b_init"
c_blake2b_init :: Ptr (Context a)-> IO ()
c_blake2b_init :: Ptr (Context a) -> Word32 -> IO ()
foreign import ccall "cryptonite_blake2b_update"
c_blake2b_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_blake2b_finalize"
c_blake2b_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_blake2b_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -9,29 +9,33 @@
-- BLAKE2bp cryptographic hash.
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.BLAKE2bp ( BLAKE2bp (..) ) where
module Crypto.Hash.BLAKE2bp
( BLAKE2bp_512 (..)
) where
import Crypto.Hash.Types
import Foreign.Ptr (Ptr)
import Data.Word (Word8, Word32)
-- | BLAKE2bp cryptographic hash algorithm
data BLAKE2bp = BLAKE2bp
-- | BLAKE2bp (512 bits) cryptographic hash algorithm
data BLAKE2bp_512 = BLAKE2bp_512
deriving (Show)
instance HashAlgorithm BLAKE2bp where
instance HashAlgorithm BLAKE2bp_512 where
hashBlockSize _ = 128
hashDigestSize _ = 64
hashInternalContextSize _ = 2325
hashInternalInit = c_blake2sp_init
hashInternalInit p = c_blake2sp_init p 512
hashInternalUpdate = c_blake2sp_update
hashInternalFinalize = c_blake2sp_finalize
hashInternalFinalize p = c_blake2sp_finalize p 512
foreign import ccall unsafe "cryptonite_blake2sp_init"
c_blake2sp_init :: Ptr (Context a)-> IO ()
c_blake2sp_init :: Ptr (Context a) -> Word32 -> IO ()
foreign import ccall "cryptonite_blake2sp_update"
c_blake2sp_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_blake2sp_finalize"
c_blake2sp_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_blake2sp_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -9,29 +9,33 @@
-- BLAKE2s cryptographic hash.
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.BLAKE2s ( BLAKE2s (..) ) where
module Crypto.Hash.BLAKE2s
( BLAKE2s_256 (..)
) where
import Crypto.Hash.Types
import Foreign.Ptr (Ptr)
import Data.Word (Word8, Word32)
-- | BLAKE2s cryptographic hash algorithm
data BLAKE2s = BLAKE2s
-- | BLAKE2s (256 bits) cryptographic hash algorithm
data BLAKE2s_256 = BLAKE2s_256
deriving (Show)
instance HashAlgorithm BLAKE2s where
instance HashAlgorithm BLAKE2s_256 where
hashBlockSize _ = 64
hashDigestSize _ = 32
hashInternalContextSize _ = 185
hashInternalInit = c_blake2s_init
hashInternalInit p = c_blake2s_init p 256
hashInternalUpdate = c_blake2s_update
hashInternalFinalize = c_blake2s_finalize
hashInternalFinalize p = c_blake2s_finalize p 256
foreign import ccall unsafe "cryptonite_blake2s_init"
c_blake2s_init :: Ptr (Context a)-> IO ()
c_blake2s_init :: Ptr (Context a) -> Word32 -> IO ()
foreign import ccall "cryptonite_blake2s_update"
c_blake2s_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_blake2s_finalize"
c_blake2s_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_blake2s_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -9,29 +9,33 @@
-- BLAKE2sp cryptographic hash.
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.BLAKE2sp ( BLAKE2sp (..) ) where
module Crypto.Hash.BLAKE2sp
( BLAKE2sp_256 (..)
) where
import Crypto.Hash.Types
import Foreign.Ptr (Ptr)
import Data.Word (Word8, Word32)
-- | BLAKE2sp cryptographic hash algorithm
data BLAKE2sp = BLAKE2sp
-- | BLAKE2sp (256 bits) cryptographic hash algorithm
data BLAKE2sp_256 = BLAKE2sp_256
deriving (Show)
instance HashAlgorithm BLAKE2sp where
instance HashAlgorithm BLAKE2sp_256 where
hashBlockSize _ = 64
hashDigestSize _ = 32
hashInternalContextSize _ = 2185
hashInternalInit = c_blake2sp_init
hashInternalInit p = c_blake2sp_init p 256
hashInternalUpdate = c_blake2sp_update
hashInternalFinalize = c_blake2sp_finalize
hashInternalFinalize p = c_blake2sp_finalize p 256
foreign import ccall unsafe "cryptonite_blake2sp_init"
c_blake2sp_init :: Ptr (Context a)-> IO ()
c_blake2sp_init :: Ptr (Context a) -> Word32 -> IO ()
foreign import ccall "cryptonite_blake2sp_update"
c_blake2sp_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_blake2sp_finalize"
c_blake2sp_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_blake2sp_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -1,16 +1,16 @@
#include "cryptonite_blake2b.h"
void cryptonite_blake2b_init(blake2b_ctx *ctx)
void cryptonite_blake2b_init(blake2b_ctx *ctx, uint32_t hashlen)
{
blake2b_init(ctx, 64);
blake2b_init(ctx, hashlen / 8);
}
void cryptonite_blake2b_update(blake2b_ctx *ctx, const uint8_t *data, uint32_t len)
{
blake2b_update(ctx, data, len);
blake2b_update(ctx, data, len);
}
void cryptonite_blake2b_finalize(blake2b_ctx *ctx, uint8_t *out)
void cryptonite_blake2b_finalize(blake2b_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
blake2b_final(ctx, out, 64);
blake2b_final(ctx, out, hashlen / 8);
}

View File

@ -5,8 +5,8 @@
typedef blake2b_state blake2b_ctx;
void cryptonite_blake2b_init(blake2b_ctx *ctx);
void cryptonite_blake2b_init(blake2b_ctx *ctx, uint32_t hashlen);
void cryptonite_blake2b_update(blake2b_ctx *ctx, const uint8_t *data, uint32_t len);
void cryptonite_blake2b_finalize(blake2b_ctx *ctx, uint8_t *out);
void cryptonite_blake2b_finalize(blake2b_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -1,16 +1,16 @@
#include "cryptonite_blake2bp.h"
void cryptonite_blake2bp_init(blake2bp_ctx *ctx)
void cryptonite_blake2bp_init(blake2bp_ctx *ctx, uint32_t hashlen)
{
blake2bp_init(ctx, 64);
blake2bp_init(ctx, hashlen / 8);
}
void cryptonite_blake2bp_update(blake2bp_ctx *ctx, const uint8_t *data, uint32_t len)
{
blake2bp_update(ctx, data, len);
blake2bp_update(ctx, data, len);
}
void cryptonite_blake2bp_finalize(blake2bp_ctx *ctx, uint8_t *out)
void cryptonite_blake2bp_finalize(blake2bp_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
blake2bp_final(ctx, out, 64);
blake2bp_final(ctx, out, hashlen / 8);
}

View File

@ -5,8 +5,8 @@
typedef blake2bp_state blake2bp_ctx;
void cryptonite_blake2bp_init(blake2bp_ctx *ctx);
void cryptonite_blake2bp_init(blake2bp_ctx *ctx, uint32_t hashlen);
void cryptonite_blake2bp_update(blake2bp_ctx *ctx, const uint8_t *data, uint32_t len);
void cryptonite_blake2bp_finalize(blake2bp_ctx *ctx, uint8_t *out);
void cryptonite_blake2bp_finalize(blake2bp_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -1,16 +1,16 @@
#include "cryptonite_blake2s.h"
void cryptonite_blake2s_init(blake2s_ctx *ctx)
void cryptonite_blake2s_init(blake2s_ctx *ctx, uint32_t hashlen)
{
blake2s_init(ctx, 32);
blake2s_init(ctx, hashlen / 8);
}
void cryptonite_blake2s_update(blake2s_ctx *ctx, const uint8_t *data, uint32_t len)
{
blake2s_update(ctx, data, len);
blake2s_update(ctx, data, len);
}
void cryptonite_blake2s_finalize(blake2s_ctx *ctx, uint8_t *out)
void cryptonite_blake2s_finalize(blake2s_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
blake2s_final(ctx, out, 32);
blake2s_final(ctx, out, hashlen / 8);
}

View File

@ -5,8 +5,8 @@
typedef blake2s_state blake2s_ctx;
void cryptonite_blake2s_init(blake2s_ctx *ctx);
void cryptonite_blake2s_init(blake2s_ctx *ctx, uint32_t hashlen);
void cryptonite_blake2s_update(blake2s_ctx *ctx, const uint8_t *data, uint32_t len);
void cryptonite_blake2s_finalize(blake2s_ctx *ctx, uint8_t *out);
void cryptonite_blake2s_finalize(blake2s_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -1,16 +1,16 @@
#include "cryptonite_blake2sp.h"
void cryptonite_blake2sp_init(blake2sp_ctx *ctx)
void cryptonite_blake2sp_init(blake2sp_ctx *ctx, uint32_t hashlen)
{
blake2sp_init(ctx, 32);
blake2sp_init(ctx, hashlen / 8);
}
void cryptonite_blake2sp_update(blake2sp_ctx *ctx, const uint8_t *data, uint32_t len)
{
blake2sp_update(ctx, data, len);
blake2sp_update(ctx, data, len);
}
void cryptonite_blake2sp_finalize(blake2sp_ctx *ctx, uint8_t *out)
void cryptonite_blake2sp_finalize(blake2sp_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
blake2sp_final(ctx, out, 32);
blake2sp_final(ctx, out, hashlen / 8);
}

View File

@ -5,8 +5,8 @@
typedef blake2sp_state blake2sp_ctx;
void cryptonite_blake2sp_init(blake2sp_ctx *ctx);
void cryptonite_blake2sp_init(blake2sp_ctx *ctx, uint32_t hashlen);
void cryptonite_blake2sp_update(blake2sp_ctx *ctx, const uint8_t *data, uint32_t len);
void cryptonite_blake2sp_finalize(blake2sp_ctx *ctx, uint8_t *out);
void cryptonite_blake2sp_finalize(blake2sp_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -53,10 +53,10 @@ data HashCustom =
hashModules =
-- module header hash ctx dg blk
[ GenHashModule "BLAKE2s" "blake2.h" "blake2s" 185 (HashSimple 256 64)
, GenHashModule "BLAKE2sp" "blake2.h" "blake2sp" 2185 (HashSimple 256 64)
, GenHashModule "BLAKE2b" "blake2.h" "blake2b" 361 (HashSimple 512 128)
, GenHashModule "BLAKE2bp" "blake2.h" "blake2sp" 2325 (HashSimple 512 128)
[ GenHashModule "BLAKE2s" "blake2.h" "blake2s" 185 (HashMulti [(256,64)])
, GenHashModule "BLAKE2sp" "blake2.h" "blake2sp" 2185 (HashMulti [(256,64)])
, GenHashModule "BLAKE2b" "blake2.h" "blake2b" 361 (HashMulti [(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)
, GenHashModule "MD5" "md5.h" "md5" 96 (HashSimple 128 64)

View File

@ -138,6 +138,10 @@ expected = [
"a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26",
"01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450",
"28e361fe8c56e617caa56c28c7c36e5c13be552b77081be82b642f08bb7ef085b9a81910fe98269386b9aacfd2349076c9506126e198f6f6ad44c12017ca77b1" ])
, ("BLAKE2b-512", HashAlg BLAKE2b_512, [
"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce",
"a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918",
"af438eea5d8cdb209336a7e85bf58090dc21b49d823f89a7d064c119f127bd361af9c7d109edda0f0e91bdce078d1d86b8e6f25727c98f6d3bb6f50acb2dd376" ])
]
runhash :: HashAlg -> ByteString -> ByteString