2020-03-31 18:28:12 +02:00
|
|
|
#include "hash.h"
|
2020-04-29 16:17:22 +02:00
|
|
|
#include "debug.h"
|
2020-03-31 18:28:12 +02:00
|
|
|
|
|
|
|
// Hash a single data
|
|
|
|
void hash_data(pub_data *data, unsigned char *buf) {
|
2020-05-03 21:38:04 +02:00
|
|
|
|
2020-03-31 18:28:12 +02:00
|
|
|
// All three fields are concatenated into a single buffer
|
|
|
|
int totlen = data->length + 10;
|
|
|
|
unsigned char concat[totlen];
|
|
|
|
concat_data(data, concat);
|
|
|
|
|
|
|
|
// The resulting buf is hashed and put into a buffer
|
|
|
|
unsigned char hash[SHA256_DIGEST_LENGTH];
|
2020-05-03 00:19:54 +02:00
|
|
|
|
|
|
|
SHA256_CTX sha256;
|
|
|
|
SHA256_Init(&sha256);
|
|
|
|
SHA256_Update(&sha256, concat, totlen);
|
|
|
|
SHA256_Final(hash, &sha256);
|
2020-03-31 18:28:12 +02:00
|
|
|
|
2020-04-14 19:32:15 +02:00
|
|
|
// Put truncated hash into buf
|
2020-03-31 18:28:12 +02:00
|
|
|
hash_trunc(hash, buf);
|
2020-05-03 21:38:04 +02:00
|
|
|
|
2020-03-31 18:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash every data contained in data_list then return a network hash
|
|
|
|
void hash_network(list *data_list, unsigned char *buf) {
|
2020-04-28 17:47:31 +02:00
|
|
|
// Get list length to initialize concat buffer
|
2020-04-28 18:07:06 +02:00
|
|
|
int concat_len = len_list(data_list) * 16;
|
2020-04-28 17:47:31 +02:00
|
|
|
unsigned char *concat = (unsigned char*) malloc(concat_len);
|
|
|
|
|
2020-03-31 18:28:12 +02:00
|
|
|
unsigned char hash[SHA256_DIGEST_LENGTH];
|
|
|
|
int totlen = 0;
|
|
|
|
list *tmp = data_list;
|
|
|
|
|
|
|
|
// Hash every known data and concatenate it to buffer concat
|
|
|
|
while(tmp != NULL) {
|
|
|
|
hash_data((pub_data*) tmp->data, hash);
|
|
|
|
concat_hash(concat, hash, totlen);
|
|
|
|
totlen += 16;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash all of concat to obtain the network hash
|
2020-05-03 00:19:54 +02:00
|
|
|
SHA256_CTX sha256;
|
|
|
|
SHA256_Init(&sha256);
|
|
|
|
SHA256_Update(&sha256, concat, concat_len);
|
|
|
|
SHA256_Final(hash, &sha256);
|
2020-03-31 18:28:12 +02:00
|
|
|
|
2020-04-14 19:32:15 +02:00
|
|
|
// Put truncated hash into buf
|
2020-03-31 18:28:12 +02:00
|
|
|
hash_trunc(hash, buf);
|
|
|
|
|
|
|
|
// Get rid of concat
|
|
|
|
free(concat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate 32 octet hash to 16 octets
|
|
|
|
void hash_trunc(unsigned char *hash32oct, unsigned char *buf) {
|
|
|
|
// Copy the first 16 octets from hash32oct
|
2020-04-29 16:17:22 +02:00
|
|
|
if (memcpy(buf, hash32oct, 16) == NULL) {
|
|
|
|
print_debug(">> Truncating the hashs didn't work !");
|
|
|
|
}
|
2020-03-31 18:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Concat all fields of data and put them in buf
|
|
|
|
void concat_data(pub_data *data, unsigned char *buf) {
|
2020-05-03 21:38:04 +02:00
|
|
|
// Turn seqno to big endian
|
|
|
|
uint16_t seqno = htobe16(data->seqno);
|
2020-05-04 00:40:54 +02:00
|
|
|
uint64_t id = htobe64(data->id);
|
2020-05-03 21:38:04 +02:00
|
|
|
|
2020-05-04 00:40:54 +02:00
|
|
|
if (memcpy(buf, (char*) &id, 8) == NULL) {
|
2020-04-29 16:17:22 +02:00
|
|
|
print_debug(">> Concat the data (id) didn't work !");
|
2020-05-03 21:38:04 +02:00
|
|
|
}
|
|
|
|
if (memcpy(buf+8, (char*) &seqno, 2) == NULL) {
|
2020-04-29 16:17:22 +02:00
|
|
|
print_debug(">> concat the data (seqno) didn't work !");
|
|
|
|
}
|
|
|
|
if (memcpy(buf+10, data->data, data->length) == NULL) {
|
|
|
|
print_debug(">> Contact the data (data) didn't work !");
|
|
|
|
}
|
2020-03-31 18:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Concat hash2 to hash1 (hash1 is modified)
|
|
|
|
void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size) {
|
2020-04-29 16:17:22 +02:00
|
|
|
if(memcpy(hash1+size, hash2, 16) == NULL){
|
|
|
|
print_debug(">> Concat the hash didn't work !");
|
|
|
|
}
|
2020-04-14 19:32:15 +02:00
|
|
|
}
|