Extract SharedKey struct and use it as var instead of indexing

This commit is contained in:
Diadlo 2017-06-04 23:47:18 +03:00
parent c12ef22138
commit b3174bb6c4
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 25 additions and 20 deletions

View File

@ -114,21 +114,22 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t
for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) { for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) {
int index = public_key[30] * MAX_KEYS_PER_SLOT + i; int index = public_key[30] * MAX_KEYS_PER_SLOT + i;
Shared_Key *key = &shared_keys->keys[index];
if (shared_keys->keys[index].stored) { if (key->stored) {
if (public_key_cmp(public_key, shared_keys->keys[index].public_key) == 0) { if (public_key_cmp(public_key, key->public_key) == 0) {
memcpy(shared_key, shared_keys->keys[index].shared_key, CRYPTO_SHARED_KEY_SIZE); memcpy(shared_key, key->shared_key, CRYPTO_SHARED_KEY_SIZE);
++shared_keys->keys[index].times_requested; ++key->times_requested;
shared_keys->keys[index].time_last_requested = unix_time(); key->time_last_requested = unix_time();
return; return;
} }
if (num != 0) { if (num != 0) {
if (is_timeout(shared_keys->keys[index].time_last_requested, KEYS_TIMEOUT)) { if (is_timeout(key->time_last_requested, KEYS_TIMEOUT)) {
num = 0; num = 0;
curr = index; curr = index;
} else if (num > shared_keys->keys[index].times_requested) { } else if (num > key->times_requested) {
num = shared_keys->keys[index].times_requested; num = key->times_requested;
curr = index; curr = index;
} }
} }
@ -143,11 +144,12 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t
encrypt_precompute(public_key, secret_key, shared_key); encrypt_precompute(public_key, secret_key, shared_key);
if (num != (uint32_t)~0) { if (num != (uint32_t)~0) {
shared_keys->keys[curr].stored = 1; Shared_Key *key = &shared_keys->keys[curr];
shared_keys->keys[curr].times_requested = 1; key->stored = 1;
memcpy(shared_keys->keys[curr].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); key->times_requested = 1;
memcpy(shared_keys->keys[curr].shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE); memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
shared_keys->keys[curr].time_last_requested = unix_time(); memcpy(key->shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE);
key->time_last_requested = unix_time();
} }
} }

View File

@ -214,14 +214,17 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
/* struct to store some shared keys so we don't have to regenerate them for each request. */ /* struct to store some shared keys so we don't have to regenerate them for each request. */
#define MAX_KEYS_PER_SLOT 4 #define MAX_KEYS_PER_SLOT 4
#define KEYS_TIMEOUT 600 #define KEYS_TIMEOUT 600
typedef struct { typedef struct {
struct { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; uint32_t times_requested;
uint32_t times_requested; uint8_t stored; /* 0 if not, 1 if is */
uint8_t stored; /* 0 if not, 1 if is */ uint64_t time_last_requested;
uint64_t time_last_requested; } Shared_Key;
} keys[256 * MAX_KEYS_PER_SLOT];
typedef struct {
Shared_Key keys[256 * MAX_KEYS_PER_SLOT];
} Shared_Keys; } Shared_Keys;
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/