2019-06-03 16:58:59 +01:00
|
|
|
/// @file utils_hash.c
|
|
|
|
/// @brief the adapter for SHA2 families.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
|
2019-06-03 20:51:05 +01:00
|
|
|
#include "rainbow_config.h"
|
2019-06-03 16:58:59 +01:00
|
|
|
#include "sha2.h"
|
|
|
|
#include "utils_hash.h"
|
|
|
|
|
|
|
|
static inline
|
2019-06-03 21:12:37 +01:00
|
|
|
int _hash( unsigned char *digest, const unsigned char *m, size_t mlen ) {
|
2019-06-16 11:28:32 +01:00
|
|
|
#if 32 == _HASH_LEN
|
2019-06-03 16:58:59 +01:00
|
|
|
sha256(digest, m, mlen);
|
2019-06-16 11:28:32 +01:00
|
|
|
#elif 48 == _HASH_LEN
|
|
|
|
sha384(digest, m, mlen);
|
|
|
|
#elif 64 == _HASH_LEN
|
|
|
|
sha512(digest, m, mlen);
|
|
|
|
#else
|
|
|
|
#error "unsupported _HASH_LEN"
|
|
|
|
#endif
|
2019-06-03 16:58:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
2019-06-03 21:12:37 +01:00
|
|
|
int expand_hash(unsigned char *digest, size_t n_digest, const unsigned char *hash) {
|
2019-06-03 16:58:59 +01:00
|
|
|
if ( _HASH_LEN >= n_digest ) {
|
2019-06-03 21:12:37 +01:00
|
|
|
for (size_t i = 0; i < n_digest; i++) {
|
2019-06-03 16:58:59 +01:00
|
|
|
digest[i] = hash[i];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-03 21:12:37 +01:00
|
|
|
for (size_t i = 0; i < _HASH_LEN; i++) {
|
2019-06-03 16:58:59 +01:00
|
|
|
digest[i] = hash[i];
|
|
|
|
}
|
|
|
|
n_digest -= _HASH_LEN;
|
|
|
|
|
|
|
|
|
2019-06-03 21:12:37 +01:00
|
|
|
while (_HASH_LEN <= n_digest ) {
|
2019-06-03 16:58:59 +01:00
|
|
|
_hash( digest + _HASH_LEN, digest, _HASH_LEN );
|
|
|
|
|
|
|
|
n_digest -= _HASH_LEN;
|
|
|
|
digest += _HASH_LEN;
|
|
|
|
}
|
|
|
|
unsigned char temp[_HASH_LEN];
|
|
|
|
if ( n_digest ) {
|
|
|
|
_hash( temp, digest, _HASH_LEN );
|
2019-06-03 21:12:37 +01:00
|
|
|
for (size_t i = 0; i < n_digest; i++) {
|
2019-06-03 16:58:59 +01:00
|
|
|
digest[_HASH_LEN + i] = temp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-03 21:12:37 +01:00
|
|
|
int PQCLEAN_RAINBOWIACLASSIC_CLEAN_hash_msg(unsigned char *digest,
|
|
|
|
size_t len_digest,
|
|
|
|
const unsigned char *m,
|
|
|
|
size_t mlen) {
|
2019-06-03 16:58:59 +01:00
|
|
|
unsigned char buf[_HASH_LEN];
|
2019-06-03 21:12:37 +01:00
|
|
|
_hash(buf, m, mlen);
|
|
|
|
return expand_hash(digest, len_digest, buf);
|
2019-06-03 16:58:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|