From fae5f084cf41cfcf6ad8822ef71fa02f266848bc Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Thu, 19 Nov 2015 12:10:14 +0000 Subject: [PATCH] [Blake2] define the algorithm as a multiple algorithm so that the output digest size is explicit in the digest types. --- Crypto/Hash/Algorithms.hs | 8 ++++---- Crypto/Hash/BLAKE2b.hs | 20 ++++++++++++-------- Crypto/Hash/BLAKE2bp.hs | 20 ++++++++++++-------- Crypto/Hash/BLAKE2s.hs | 20 ++++++++++++-------- Crypto/Hash/BLAKE2sp.hs | 20 ++++++++++++-------- cbits/cryptonite_blake2b.c | 10 +++++----- cbits/cryptonite_blake2b.h | 4 ++-- cbits/cryptonite_blake2bp.c | 10 +++++----- cbits/cryptonite_blake2bp.h | 4 ++-- cbits/cryptonite_blake2s.c | 10 +++++----- cbits/cryptonite_blake2s.h | 4 ++-- cbits/cryptonite_blake2sp.c | 10 +++++----- cbits/cryptonite_blake2sp.h | 4 ++-- gen/Gen.hs | 8 ++++---- tests/Hash.hs | 4 ++++ 15 files changed, 88 insertions(+), 68 deletions(-) diff --git a/Crypto/Hash/Algorithms.hs b/Crypto/Hash/Algorithms.hs index a18b0ec..216110d 100644 --- a/Crypto/Hash/Algorithms.hs +++ b/Crypto/Hash/Algorithms.hs @@ -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(..) diff --git a/Crypto/Hash/BLAKE2b.hs b/Crypto/Hash/BLAKE2b.hs index 9521bfa..02e16ca 100644 --- a/Crypto/Hash/BLAKE2b.hs +++ b/Crypto/Hash/BLAKE2b.hs @@ -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 () diff --git a/Crypto/Hash/BLAKE2bp.hs b/Crypto/Hash/BLAKE2bp.hs index 1e71ba2..44fbd66 100644 --- a/Crypto/Hash/BLAKE2bp.hs +++ b/Crypto/Hash/BLAKE2bp.hs @@ -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 () diff --git a/Crypto/Hash/BLAKE2s.hs b/Crypto/Hash/BLAKE2s.hs index ab7ea67..8430016 100644 --- a/Crypto/Hash/BLAKE2s.hs +++ b/Crypto/Hash/BLAKE2s.hs @@ -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 () diff --git a/Crypto/Hash/BLAKE2sp.hs b/Crypto/Hash/BLAKE2sp.hs index 5061722..a903b3d 100644 --- a/Crypto/Hash/BLAKE2sp.hs +++ b/Crypto/Hash/BLAKE2sp.hs @@ -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 () diff --git a/cbits/cryptonite_blake2b.c b/cbits/cryptonite_blake2b.c index 1f2dd8d..dbaff93 100644 --- a/cbits/cryptonite_blake2b.c +++ b/cbits/cryptonite_blake2b.c @@ -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); } diff --git a/cbits/cryptonite_blake2b.h b/cbits/cryptonite_blake2b.h index 8edc8ac..bd8cf0b 100644 --- a/cbits/cryptonite_blake2b.h +++ b/cbits/cryptonite_blake2b.h @@ -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 diff --git a/cbits/cryptonite_blake2bp.c b/cbits/cryptonite_blake2bp.c index 318833d..ce565f5 100644 --- a/cbits/cryptonite_blake2bp.c +++ b/cbits/cryptonite_blake2bp.c @@ -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); } diff --git a/cbits/cryptonite_blake2bp.h b/cbits/cryptonite_blake2bp.h index 4ccf54c..eb9d1c6 100644 --- a/cbits/cryptonite_blake2bp.h +++ b/cbits/cryptonite_blake2bp.h @@ -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 diff --git a/cbits/cryptonite_blake2s.c b/cbits/cryptonite_blake2s.c index 07d9dac..0f85a57 100644 --- a/cbits/cryptonite_blake2s.c +++ b/cbits/cryptonite_blake2s.c @@ -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); } diff --git a/cbits/cryptonite_blake2s.h b/cbits/cryptonite_blake2s.h index d847a17..3ed85fa 100644 --- a/cbits/cryptonite_blake2s.h +++ b/cbits/cryptonite_blake2s.h @@ -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 diff --git a/cbits/cryptonite_blake2sp.c b/cbits/cryptonite_blake2sp.c index 9a8580a..8791473 100644 --- a/cbits/cryptonite_blake2sp.c +++ b/cbits/cryptonite_blake2sp.c @@ -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); } diff --git a/cbits/cryptonite_blake2sp.h b/cbits/cryptonite_blake2sp.h index 7d040a4..c2d58f7 100644 --- a/cbits/cryptonite_blake2sp.h +++ b/cbits/cryptonite_blake2sp.h @@ -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 diff --git a/gen/Gen.hs b/gen/Gen.hs index cc4449f..6a32ed5 100644 --- a/gen/Gen.hs +++ b/gen/Gen.hs @@ -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) diff --git a/tests/Hash.hs b/tests/Hash.hs index d261759..f72c406 100644 --- a/tests/Hash.hs +++ b/tests/Hash.hs @@ -138,6 +138,10 @@ expected = [ "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26", "01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450", "28e361fe8c56e617caa56c28c7c36e5c13be552b77081be82b642f08bb7ef085b9a81910fe98269386b9aacfd2349076c9506126e198f6f6ad44c12017ca77b1" ]) + , ("BLAKE2b-512", HashAlg BLAKE2b_512, [ + "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce", + "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918", + "af438eea5d8cdb209336a7e85bf58090dc21b49d823f89a7d064c119f127bd361af9c7d109edda0f0e91bdce078d1d86b8e6f25727c98f6d3bb6f50acb2dd376" ]) ] runhash :: HashAlg -> ByteString -> ByteString