This commit is contained in:
gonzalef 2020-03-31 18:28:12 +02:00
parent a469108dc5
commit 7db155e671
5 changed files with 100 additions and 9 deletions

View File

@ -1 +1,4 @@
# Notes et recherches sur le projet
Telecharger la librarie OpenSSl avec 'sudo apt-get install libssl-dev'
Utiliser 'gcc -o {exec} {fichier.c} -lssl -lcrypto' pour utiliser la librarie OpenSSL

60
src/hash.c Normal file
View File

@ -0,0 +1,60 @@
#include "hash.h"
// Hash a single data
void hash_data(pub_data *data, unsigned char *buf) {
// 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];
SHA256(concat, totlen, hash);
// Put truncated hash into buf
hash_trunc(hash, buf);
}
// Hash every data contained in data_list then return a network hash
void hash_network(list *data_list, unsigned char *buf) {
unsigned char *concat = (unsigned char*) malloc(0);
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
SHA256(concat, totlen, hash);
// Put truncated hash into buf
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
memcpy(buf, hash32oct, 16);
}
// Concat all fields of data and put them in buf
void concat_data(pub_data *data, unsigned char *buf) {
memcpy(buf, &(data->id), 8);
memcpy(buf+8, &(data->seqno), 2);
memcpy(buf+10, data->data, data->length);
}
// Concat hash2 to hash1 (hash1 is modified)
void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size) {
hash1 = (unsigned char*) realloc(hash1, size + 16);
memcpy(hash1+size, hash2, 16);
}

23
src/hash.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef HASH_H
#define HASH_H
#include <openssl/sha.h>
#include "node.h"
#include "tlv.h"
// Hash a single data
void hash_data(pub_data *data, unsigned char *buf);
// Hash every data contained in data_list then return a network hash
void hash_network(list *data_list, unsigned char *buf);
// Truncate 32 octet hash to 16 octets
void hash_trunc(unsigned char *hash256bit, unsigned char *buf);
// Concat all fields of data and put them in buf
void concat_data(pub_data *data, unsigned char *buf);
// Concat hash2 to hash1 (hash1 is modified)
void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size);
#endif

View File

@ -1,13 +1,5 @@
// This is the main file of the Dazibao project. It represents the node, and
// handles all of the main logic, including the network connexions.
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include "tlv.h"
#include "node.h"
// For every packet recivied,

View File

@ -3,8 +3,15 @@
#ifndef NODE_H
#define NODE_H
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include "tlv.h"
#include "misc.h"
#include "hash.h"
// On which port do we listen to
#define LISTEN_PORT 1212
@ -42,6 +49,12 @@ typedef struct pub_data {
char *data;
} pub_data;
// General list
typedef struct list {
void *data;
void *next;
} list;
// static lists
static list *data_list;
static list *neighbour_list;