[Hash] tweak internal C API to have the hashlen

This commit is contained in:
Vincent Hanquez 2015-11-19 11:37:38 +00:00
parent f51fdf23ca
commit ad285be68c
16 changed files with 40 additions and 40 deletions

View File

@ -28,7 +28,7 @@ instance HashAlgorithm Keccak_224 where
hashInternalContextSize _ = 360
hashInternalInit p = c_keccak_init p 224
hashInternalUpdate = c_keccak_update
hashInternalFinalize = c_keccak_finalize
hashInternalFinalize p = c_keccak_finalize p 224
-- | Keccak (256 bits) cryptographic hash algorithm
data Keccak_256 = Keccak_256
@ -40,7 +40,7 @@ instance HashAlgorithm Keccak_256 where
hashInternalContextSize _ = 360
hashInternalInit p = c_keccak_init p 256
hashInternalUpdate = c_keccak_update
hashInternalFinalize = c_keccak_finalize
hashInternalFinalize p = c_keccak_finalize p 256
-- | Keccak (384 bits) cryptographic hash algorithm
data Keccak_384 = Keccak_384
@ -52,7 +52,7 @@ instance HashAlgorithm Keccak_384 where
hashInternalContextSize _ = 360
hashInternalInit p = c_keccak_init p 384
hashInternalUpdate = c_keccak_update
hashInternalFinalize = c_keccak_finalize
hashInternalFinalize p = c_keccak_finalize p 384
-- | Keccak (512 bits) cryptographic hash algorithm
data Keccak_512 = Keccak_512
@ -64,7 +64,7 @@ instance HashAlgorithm Keccak_512 where
hashInternalContextSize _ = 360
hashInternalInit p = c_keccak_init p 512
hashInternalUpdate = c_keccak_update
hashInternalFinalize = c_keccak_finalize
hashInternalFinalize p = c_keccak_finalize p 512
foreign import ccall unsafe "cryptonite_keccak_init"
@ -74,4 +74,4 @@ foreign import ccall "cryptonite_keccak_update"
c_keccak_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_keccak_finalize"
c_keccak_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_keccak_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -28,7 +28,7 @@ instance HashAlgorithm SHA3_224 where
hashInternalContextSize _ = 360
hashInternalInit p = c_sha3_init p 224
hashInternalUpdate = c_sha3_update
hashInternalFinalize = c_sha3_finalize
hashInternalFinalize p = c_sha3_finalize p 224
-- | SHA3 (256 bits) cryptographic hash algorithm
data SHA3_256 = SHA3_256
@ -40,7 +40,7 @@ instance HashAlgorithm SHA3_256 where
hashInternalContextSize _ = 360
hashInternalInit p = c_sha3_init p 256
hashInternalUpdate = c_sha3_update
hashInternalFinalize = c_sha3_finalize
hashInternalFinalize p = c_sha3_finalize p 256
-- | SHA3 (384 bits) cryptographic hash algorithm
data SHA3_384 = SHA3_384
@ -52,7 +52,7 @@ instance HashAlgorithm SHA3_384 where
hashInternalContextSize _ = 360
hashInternalInit p = c_sha3_init p 384
hashInternalUpdate = c_sha3_update
hashInternalFinalize = c_sha3_finalize
hashInternalFinalize p = c_sha3_finalize p 384
-- | SHA3 (512 bits) cryptographic hash algorithm
data SHA3_512 = SHA3_512
@ -64,7 +64,7 @@ instance HashAlgorithm SHA3_512 where
hashInternalContextSize _ = 360
hashInternalInit p = c_sha3_init p 512
hashInternalUpdate = c_sha3_update
hashInternalFinalize = c_sha3_finalize
hashInternalFinalize p = c_sha3_finalize p 512
foreign import ccall unsafe "cryptonite_sha3_init"
@ -74,4 +74,4 @@ foreign import ccall "cryptonite_sha3_update"
c_sha3_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha3_finalize"
c_sha3_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_sha3_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -28,7 +28,7 @@ instance HashAlgorithm SHA512t_224 where
hashInternalContextSize _ = 264
hashInternalInit p = c_sha512t_init p 224
hashInternalUpdate = c_sha512t_update
hashInternalFinalize = c_sha512t_finalize
hashInternalFinalize p = c_sha512t_finalize p 224
-- | SHA512t (256 bits) cryptographic hash algorithm
data SHA512t_256 = SHA512t_256
@ -40,7 +40,7 @@ instance HashAlgorithm SHA512t_256 where
hashInternalContextSize _ = 264
hashInternalInit p = c_sha512t_init p 256
hashInternalUpdate = c_sha512t_update
hashInternalFinalize = c_sha512t_finalize
hashInternalFinalize p = c_sha512t_finalize p 256
foreign import ccall unsafe "cryptonite_sha512t_init"
@ -50,4 +50,4 @@ foreign import ccall "cryptonite_sha512t_update"
c_sha512t_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha512t_finalize"
c_sha512t_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_sha512t_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -28,7 +28,7 @@ instance HashAlgorithm Skein256_224 where
hashInternalContextSize _ = 96
hashInternalInit p = c_skein256_init p 224
hashInternalUpdate = c_skein256_update
hashInternalFinalize = c_skein256_finalize
hashInternalFinalize p = c_skein256_finalize p 224
-- | Skein256 (256 bits) cryptographic hash algorithm
data Skein256_256 = Skein256_256
@ -40,7 +40,7 @@ instance HashAlgorithm Skein256_256 where
hashInternalContextSize _ = 96
hashInternalInit p = c_skein256_init p 256
hashInternalUpdate = c_skein256_update
hashInternalFinalize = c_skein256_finalize
hashInternalFinalize p = c_skein256_finalize p 256
foreign import ccall unsafe "cryptonite_skein256_init"
@ -50,4 +50,4 @@ foreign import ccall "cryptonite_skein256_update"
c_skein256_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_skein256_finalize"
c_skein256_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_skein256_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -28,7 +28,7 @@ instance HashAlgorithm Skein512_224 where
hashInternalContextSize _ = 160
hashInternalInit p = c_skein512_init p 224
hashInternalUpdate = c_skein512_update
hashInternalFinalize = c_skein512_finalize
hashInternalFinalize p = c_skein512_finalize p 224
-- | Skein512 (256 bits) cryptographic hash algorithm
data Skein512_256 = Skein512_256
@ -40,7 +40,7 @@ instance HashAlgorithm Skein512_256 where
hashInternalContextSize _ = 160
hashInternalInit p = c_skein512_init p 256
hashInternalUpdate = c_skein512_update
hashInternalFinalize = c_skein512_finalize
hashInternalFinalize p = c_skein512_finalize p 256
-- | Skein512 (384 bits) cryptographic hash algorithm
data Skein512_384 = Skein512_384
@ -52,7 +52,7 @@ instance HashAlgorithm Skein512_384 where
hashInternalContextSize _ = 160
hashInternalInit p = c_skein512_init p 384
hashInternalUpdate = c_skein512_update
hashInternalFinalize = c_skein512_finalize
hashInternalFinalize p = c_skein512_finalize p 384
-- | Skein512 (512 bits) cryptographic hash algorithm
data Skein512_512 = Skein512_512
@ -64,7 +64,7 @@ instance HashAlgorithm Skein512_512 where
hashInternalContextSize _ = 160
hashInternalInit p = c_skein512_init p 512
hashInternalUpdate = c_skein512_update
hashInternalFinalize = c_skein512_finalize
hashInternalFinalize p = c_skein512_finalize p 512
foreign import ccall unsafe "cryptonite_skein512_init"
@ -74,4 +74,4 @@ foreign import ccall "cryptonite_skein512_update"
c_skein512_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_skein512_finalize"
c_skein512_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_skein512_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()

View File

@ -135,7 +135,7 @@ void cryptonite_keccak_update(struct keccak_ctx *ctx, uint8_t *data, uint32_t le
}
}
void cryptonite_keccak_finalize(struct keccak_ctx *ctx, uint8_t *out)
void cryptonite_keccak_finalize(struct keccak_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
uint64_t w[25];

View File

@ -40,6 +40,6 @@ struct keccak_ctx
void cryptonite_keccak_init(struct keccak_ctx *ctx, uint32_t hashlen);
void cryptonite_keccak_update(struct keccak_ctx *ctx, uint8_t *data, uint32_t len);
void cryptonite_keccak_finalize(struct keccak_ctx *ctx, uint8_t *out);
void cryptonite_keccak_finalize(struct keccak_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -135,7 +135,7 @@ void cryptonite_sha3_update(struct sha3_ctx *ctx, const uint8_t *data, uint32_t
}
}
void cryptonite_sha3_finalize(struct sha3_ctx *ctx, uint8_t *out)
void cryptonite_sha3_finalize(struct sha3_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
uint64_t w[25];
@ -155,5 +155,5 @@ void cryptonite_sha3_finalize(struct sha3_ctx *ctx, uint8_t *out)
/* output */
cpu_to_le64_array(w, ctx->state, 25);
memcpy(out, w, ctx->hashlen);
memcpy(out, w, hashlen / 8);
}

View File

@ -40,6 +40,6 @@ struct sha3_ctx
void cryptonite_sha3_init(struct sha3_ctx *ctx, uint32_t hashlen);
void cryptonite_sha3_update(struct sha3_ctx *ctx, const uint8_t *data, uint32_t len);
void cryptonite_sha3_finalize(struct sha3_ctx *ctx, uint8_t *out);
void cryptonite_sha3_finalize(struct sha3_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -196,15 +196,15 @@ void cryptonite_sha512_finalize(struct sha512_ctx *ctx, uint8_t *out)
#include <stdio.h>
void cryptonite_sha512t_init(struct sha512t_ctx *tctx, int t)
void cryptonite_sha512t_init(struct sha512t_ctx *tctx, uint32_t hashlen)
{
struct sha512_ctx *ctx = &tctx->ctx;
memset(ctx, 0, sizeof(*ctx));
if (t >= 512)
if (hashlen >= 512)
return;
tctx->t = t;
tctx->t = hashlen;
switch (t) {
switch (hashlen) {
case 224:
ctx->h[0] = 0x8c3d37c819544da2ULL;
ctx->h[1] = 0x73e1996689dcd4d6ULL;
@ -234,7 +234,7 @@ void cryptonite_sha512t_init(struct sha512t_ctx *tctx, int t)
for (i = 0; i < 8; i++)
ctx->h[i] ^= 0xa5a5a5a5a5a5a5a5ULL;
i = sprintf(buf, "SHA-512/%d", t);
i = sprintf(buf, "SHA-512/%d", hashlen);
cryptonite_sha512_update(ctx, (uint8_t *) buf, i);
cryptonite_sha512_finalize(ctx, out);
@ -251,7 +251,7 @@ void cryptonite_sha512t_update(struct sha512t_ctx *ctx, const uint8_t *data, uin
return cryptonite_sha512_update(&ctx->ctx, data, len);
}
void cryptonite_sha512t_finalize(struct sha512t_ctx *ctx, uint8_t *out)
void cryptonite_sha512t_finalize(struct sha512t_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
uint8_t intermediate[SHA512_DIGEST_SIZE];

View File

@ -58,8 +58,8 @@ void cryptonite_sha512_update(struct sha512_ctx *ctx, const uint8_t *data, uint3
void cryptonite_sha512_finalize(struct sha512_ctx *ctx, uint8_t *out);
/* only multiples of 8 are supported as valid t values */
void cryptonite_sha512t_init(struct sha512t_ctx *ctx, int t);
void cryptonite_sha512t_init(struct sha512t_ctx *ctx, uint32_t hashlen);
void cryptonite_sha512t_update(struct sha512t_ctx *ctx, const uint8_t *data, uint32_t len);
void cryptonite_sha512t_finalize(struct sha512t_ctx *ctx, uint8_t *out);
void cryptonite_sha512t_finalize(struct sha512t_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -155,7 +155,7 @@ void cryptonite_skein256_update(struct skein256_ctx *ctx, const uint8_t *data, u
}
}
void cryptonite_skein256_finalize(struct skein256_ctx *ctx, uint8_t *out)
void cryptonite_skein256_finalize(struct skein256_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
uint32_t outsize;
uint64_t *p = (uint64_t *) out;

View File

@ -40,6 +40,6 @@ struct skein256_ctx
void cryponite_skein256_init(struct skein256_ctx *ctx, uint32_t hashlen);
void cryponite_skein256_update(struct skein256_ctx *ctx, const uint8_t *data, uint32_t len);
void cryponite_skein256_finalize(struct skein256_ctx *ctx, uint8_t *out);
void cryponite_skein256_finalize(struct skein256_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -173,7 +173,7 @@ void cryptonite_skein512_update(struct skein512_ctx *ctx, const uint8_t *data, u
}
}
void cryptonite_skein512_finalize(struct skein512_ctx *ctx, uint8_t *out)
void cryptonite_skein512_finalize(struct skein512_ctx *ctx, uint32_t hashlen, uint8_t *out)
{
uint32_t outsize;
uint64_t *p = (uint64_t *) out;

View File

@ -40,6 +40,6 @@ struct skein512_ctx
void cryponite_skein512_init(struct skein512_ctx *ctx, uint32_t hashlen);
void cryponite_skein512_update(struct skein512_ctx *ctx, const uint8_t *data, uint32_t len);
void cryponite_skein512_finalize(struct skein512_ctx *ctx, uint8_t *out);
void cryponite_skein512_finalize(struct skein512_ctx *ctx, uint32_t hashlen, uint8_t *out);
#endif

View File

@ -28,7 +28,7 @@ instance HashAlgorithm %%MODULENAME%%_%%CUSTOM_BITSIZE%% where
hashInternalContextSize _ = %%CTX_SIZE_BYTES%%
hashInternalInit p = c_%%HASHNAME%%_init p %%CUSTOM_BITSIZE%%
hashInternalUpdate = c_%%HASHNAME%%_update
hashInternalFinalize = c_%%HASHNAME%%_finalize
hashInternalFinalize p = c_%%HASHNAME%%_finalize p %%CUSTOM_BITSIZE%%
%{CUSTOMIZABLE%}
foreign import ccall unsafe "cryptonite_%%HASHNAME%%_init"
@ -38,4 +38,4 @@ foreign import ccall "cryptonite_%%HASHNAME%%_update"
c_%%HASHNAME%%_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_%%HASHNAME%%_finalize"
c_%%HASHNAME%%_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_%%HASHNAME%%_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()