2019-09-09 12:40:40 +01:00
|
|
|
/// @file utils_hash.c
|
|
|
|
/// @brief the adapter for SHA2 families.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
|
|
|
|
#include "utils_hash.h"
|
|
|
|
#include "rainbow_config.h"
|
|
|
|
#include "sha2.h"
|
|
|
|
|
2020-11-27 17:55:15 +00:00
|
|
|
static inline int h(unsigned char *digest, const unsigned char *m, size_t mlen) {
|
2019-09-09 12:40:40 +01:00
|
|
|
sha256(digest, m, mlen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int expand_hash(unsigned char *digest, size_t n_digest, const unsigned char *hash) {
|
|
|
|
if (_HASH_LEN >= n_digest) {
|
|
|
|
for (size_t i = 0; i < n_digest; i++) {
|
|
|
|
digest[i] = hash[i];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < _HASH_LEN; i++) {
|
|
|
|
digest[i] = hash[i];
|
|
|
|
}
|
|
|
|
n_digest -= _HASH_LEN;
|
|
|
|
|
|
|
|
while (_HASH_LEN <= n_digest) {
|
2020-11-27 17:55:15 +00:00
|
|
|
h(digest + _HASH_LEN, digest, _HASH_LEN);
|
2019-09-09 12:40:40 +01:00
|
|
|
|
|
|
|
n_digest -= _HASH_LEN;
|
|
|
|
digest += _HASH_LEN;
|
|
|
|
}
|
|
|
|
unsigned char temp[_HASH_LEN];
|
|
|
|
if (n_digest) {
|
2020-11-27 17:55:15 +00:00
|
|
|
h(temp, digest, _HASH_LEN);
|
2019-09-09 12:40:40 +01:00
|
|
|
for (size_t i = 0; i < n_digest; i++) {
|
|
|
|
digest[_HASH_LEN + i] = temp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:55:15 +00:00
|
|
|
int PQCLEAN_RAINBOWICLASSIC_CLEAN_hash_msg(unsigned char *digest,
|
2019-09-09 12:40:40 +01:00
|
|
|
size_t len_digest,
|
|
|
|
const unsigned char *m,
|
|
|
|
size_t mlen) {
|
|
|
|
unsigned char buf[_HASH_LEN];
|
2020-11-27 17:55:15 +00:00
|
|
|
h(buf, m, mlen);
|
2019-09-09 12:40:40 +01:00
|
|
|
return expand_hash(digest, len_digest, buf);
|
|
|
|
}
|