{
assert(s);
- // The hash state cannot be updated after the result has been fetched via
- // hash_result/hash_result_as_bytes.
- assert(!hash->md.finalized);
-
mdfour_update(&hash->md, (const unsigned char *)s, len);
if (len > 0 && hash->debug_binary) {
(void) fwrite(s, 1, len, hash->debug_binary);
size_t
hash_input_size(struct hash *hash)
{
- return hash->md.totalN;
+ return hash->md.totalN + hash->md.tail_len;
}
void
unsigned char sum[16];
hash_result_as_bytes(hash, sum);
- return format_hash_as_string(sum, (unsigned) hash->md.totalN);
+ return format_hash_as_string(sum, hash_input_size(hash));
}
void
hash_result_as_bytes(struct hash *hash, unsigned char *out)
{
- mdfour_update(&hash->md, NULL, 0);
mdfour_result(&hash->md, out);
}
// Copyright (C) 1997-1998 Andrew Tridgell
-// Copyright (C) 2009-2018 Joel Rosdahl
+// Copyright (C) 2009-2019 Joel Rosdahl
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
md->D = 0x10325476;
md->totalN = 0;
md->tail_len = 0;
- md->finalized = 0;
}
static
void
mdfour_update(struct mdfour *md, const unsigned char *in, size_t n)
{
- if (!in) {
- if (!md->finalized) {
- mdfour_tail(md, md->tail, md->tail_len);
- md->finalized = 1;
- }
- return;
- }
+ assert(in);
uint32_t M[16];
if (md->tail_len) {
void
mdfour_result(struct mdfour *md, unsigned char *out)
{
- copy4(out, md->A);
- copy4(out+4, md->B);
- copy4(out+8, md->C);
- copy4(out+12, md->D);
+ struct mdfour result;
+ result.A = md->A;
+ result.B = md->B;
+ result.C = md->C;
+ result.D = md->D;
+ result.totalN = md->totalN;
+ result.tail_len = md->tail_len;
+ memcpy(result.tail, md->tail, result.tail_len);
+
+ mdfour_tail(&result, result.tail, result.tail_len);
+ copy4(out, result.A);
+ copy4(out+4, result.B);
+ copy4(out+8, result.C);
+ copy4(out+12, result.D);
}
struct mdfour {
uint32_t A, B, C, D;
size_t totalN;
- unsigned char tail[64];
size_t tail_len;
- int finalized;
+ unsigned char tail[64];
};
void mdfour_begin(struct mdfour *md);
-// Copyright (C) 2010-2018 Joel Rosdahl
+// Copyright (C) 2010-2019 Joel Rosdahl
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
}
}
+TEST(hash_result_should_not_alter_state)
+{
+ struct hash *h = hash_init();
+ hash_string(h, "message");
+ free(hash_result(h));
+ hash_string(h, " digest");
+ CHECK_STR_EQ_FREE2("d9130a8164549fe818874806e1c7014b-14", hash_result(h));
+ hash_free(h);
+}
+
TEST(hash_result_should_be_idempotent)
{
struct hash *h = hash_init();