1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-26 17:31:38 +00:00
pqcrypto/crypto_sign/rainbowVc-cyclic-compressed/clean/utils_hash.c

51 lines
1.2 KiB
C
Raw Normal View History

2019-06-22 17:17:07 +01:00
/// @file utils_hash.c
/// @brief the adapter for SHA2 families.
///
///
2019-07-24 09:15:48 +01:00
#include "utils_hash.h"
2019-06-22 17:17:07 +01:00
#include "rainbow_config.h"
#include "sha2.h"
2019-07-24 09:15:48 +01:00
static inline int _hash(unsigned char *digest, const unsigned char *m, size_t mlen) {
2019-06-22 17:17:07 +01:00
sha512(digest, m, mlen);
return 0;
}
2019-07-24 09:15:48 +01:00
static inline int expand_hash(unsigned char *digest, size_t n_digest, const unsigned char *hash) {
if (_HASH_LEN >= n_digest) {
2019-06-22 17:17:07 +01:00
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;
2019-07-24 09:15:48 +01:00
while (_HASH_LEN <= n_digest) {
_hash(digest + _HASH_LEN, digest, _HASH_LEN);
2019-06-22 17:17:07 +01:00
n_digest -= _HASH_LEN;
digest += _HASH_LEN;
}
unsigned char temp[_HASH_LEN];
2019-07-24 09:15:48 +01:00
if (n_digest) {
_hash(temp, digest, _HASH_LEN);
2019-06-22 17:17:07 +01:00
for (size_t i = 0; i < n_digest; i++) {
digest[_HASH_LEN + i] = temp[i];
}
}
return 0;
}
int PQCLEAN_RAINBOWVCCYCLICCOMPRESSED_CLEAN_hash_msg(unsigned char *digest,
size_t len_digest,
const unsigned char *m,
size_t mlen) {
unsigned char buf[_HASH_LEN];
_hash(buf, m, mlen);
return expand_hash(digest, len_digest, buf);
}