sha3: actually support both FIPS SHA3 and Keccak

This commit is contained in:
Alex Beregszaszi 2016-10-06 14:15:36 +01:00
parent 53cbece3a6
commit f77a4585ec

View File

@ -49,12 +49,19 @@ namespace keccak
#define decsha3(bits) \ #define decsha3(bits) \
int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t); int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
#define deckeccak(bits) \
int keccak##bits(uint8_t*, size_t, const uint8_t*, size_t);
decshake(128) decshake(128)
decshake(256) decshake(256)
decsha3(224) decsha3(224)
decsha3(256) decsha3(256)
decsha3(384) decsha3(384)
decsha3(512) decsha3(512)
deckeccak(224)
deckeccak(256)
deckeccak(384)
deckeccak(512)
/******** The Keccak-f[1600] permutation ********/ /******** The Keccak-f[1600] permutation ********/
@ -192,6 +199,14 @@ static inline int hash(uint8_t* out, size_t outlen,
if (outlen > (bits/8)) { \ if (outlen > (bits/8)) { \
return -1; \ return -1; \
} \ } \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \
}
#define defkeccak(bits) \
int keccak##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \
} }
@ -205,6 +220,12 @@ defsha3(256)
defsha3(384) defsha3(384)
defsha3(512) defsha3(512)
/*** KECCAK FOFs ***/
defkeccak(224)
defkeccak(256)
defkeccak(384)
defkeccak(512)
} }
bool keccak256(bytesConstRef _input, bytesRef o_output) bool keccak256(bytesConstRef _input, bytesRef o_output)
@ -212,7 +233,7 @@ bool keccak256(bytesConstRef _input, bytesRef o_output)
// FIXME: What with unaligned memory? // FIXME: What with unaligned memory?
if (o_output.size() != 32) if (o_output.size() != 32)
return false; return false;
keccak::sha3_256(o_output.data(), 32, _input.data(), _input.size()); keccak::keccak256(o_output.data(), 32, _input.data(), _input.size());
// keccak::keccak(ret.data(), 32, (uint64_t const*)_input.data(), _input.size()); // keccak::keccak(ret.data(), 32, (uint64_t const*)_input.data(), _input.size());
return true; return true;
} }