Add Lib Init

This commit is contained in:
Florian Märkl 2019-08-09 13:38:13 +02:00
parent 4c216db5d2
commit 0a3378bba9
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
7 changed files with 47 additions and 6 deletions

View file

@ -24,6 +24,13 @@ int main(int argc, char *argv[])
{ {
qRegisterMetaType<ChiakiQuitReason>(); qRegisterMetaType<ChiakiQuitReason>();
ChiakiErrorCode err = chiaki_lib_init();
if(err != CHIAKI_ERR_SUCCESS)
{
fprintf(stderr, "Chiaki lib init failed: %s\n", chiaki_error_string(err));
return 1;
}
QApplication app(argc, argv); QApplication app(argc, argv);
QApplication::setApplicationName("Chiaki"); QApplication::setApplicationName("Chiaki");

View file

@ -51,7 +51,12 @@ typedef enum
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code); CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code);
void *chiaki_aligned_alloc(size_t alignment, size_t size); CHIAKI_EXPORT void *chiaki_aligned_alloc(size_t alignment, size_t size);
/**
* Perform initialization of global state needed for using the Chiaki lib
*/
CHIAKI_EXPORT ChiakiErrorCode chiaki_lib_init();
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -27,7 +27,12 @@
extern "C" { extern "C" {
#endif #endif
CHIAKI_EXPORT ChiakiErrorCode chiaki_random_bytes(uint8_t *buf, size_t buf_size); /**
* Random for cryptography
*/
CHIAKI_EXPORT ChiakiErrorCode chiaki_random_bytes_crypt(uint8_t *buf, size_t buf_size);
CHIAKI_EXPORT uint32_t chiaki_random_32();
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -16,9 +16,14 @@
*/ */
#include <chiaki/common.h> #include <chiaki/common.h>
#include <chiaki/random.h>
#include <chiaki/fec.h>
#include <galois.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <errno.h>
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code) CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code)
{ {
@ -63,3 +68,16 @@ void *chiaki_aligned_alloc(size_t alignment, size_t size)
{ {
return aligned_alloc(alignment, size); return aligned_alloc(alignment, size);
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_lib_init()
{
unsigned int seed;
chiaki_random_bytes_crypt((uint8_t *)&seed, sizeof(seed));
srand(seed); // doesn't necessarily need to be secure for crypto
int galois_r = galois_init_default_field(CHIAKI_FEC_WORDSIZE);
if(galois_r != 0)
return galois_r == ENOMEM ? CHIAKI_ERR_MEMORY : CHIAKI_ERR_UNKNOWN;
return CHIAKI_ERR_SUCCESS;
}

View file

@ -19,10 +19,15 @@
#include <openssl/rand.h> #include <openssl/rand.h>
CHIAKI_EXPORT ChiakiErrorCode chiaki_random_bytes(uint8_t *buf, size_t buf_size) CHIAKI_EXPORT ChiakiErrorCode chiaki_random_bytes_crypt(uint8_t *buf, size_t buf_size)
{ {
int r = RAND_bytes(buf, (int)buf_size); int r = RAND_bytes(buf, (int)buf_size);
if(!r) if(!r)
return CHIAKI_ERR_UNKNOWN; return CHIAKI_ERR_UNKNOWN;
return CHIAKI_ERR_SUCCESS; return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT uint32_t chiaki_random_32()
{
return rand() % UINT32_MAX;
} }

View file

@ -333,7 +333,7 @@ static void *session_thread_func(void *arg)
session->mtu = 1454; session->mtu = 1454;
session->rtt = 12; session->rtt = 12;
err = chiaki_random_bytes(session->handshake_key, sizeof(session->handshake_key)); err = chiaki_random_bytes_crypt(session->handshake_key, sizeof(session->handshake_key));
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
{ {
CHIAKI_LOGE(session->log, "Session failed to generate handshake key"); CHIAKI_LOGE(session->log, "Session failed to generate handshake key");

View file

@ -17,6 +17,7 @@
#include <chiaki/takion.h> #include <chiaki/takion.h>
#include <chiaki/congestioncontrol.h> #include <chiaki/congestioncontrol.h>
#include <chiaki/random.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -186,7 +187,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
takion->cb_user = info->cb_user; takion->cb_user = info->cb_user;
takion->a_rwnd = TAKION_A_RWND; takion->a_rwnd = TAKION_A_RWND;
takion->tag_local = 0x4823; // "random" tag TODO: use actual random tag takion->tag_local = chiaki_random_32(); // 0x4823
takion->seq_num_local = takion->tag_local; takion->seq_num_local = takion->tag_local;
ret = chiaki_mutex_init(&takion->seq_num_local_mutex, false); ret = chiaki_mutex_init(&takion->seq_num_local_mutex, false);
if(ret != CHIAKI_ERR_SUCCESS) if(ret != CHIAKI_ERR_SUCCESS)