Avoid useless copies and protect keys
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
30
mud.c
30
mud.c
@@ -581,13 +581,22 @@ mud_get_key(struct mud *mud, unsigned char *key, size_t *size)
|
|||||||
int
|
int
|
||||||
mud_set_key(struct mud *mud, unsigned char *key, size_t size)
|
mud_set_key(struct mud *mud, unsigned char *key, size_t size)
|
||||||
{
|
{
|
||||||
if (!key || (size < MUD_KEY_SIZE)) {
|
if (key && (size < MUD_KEY_SIZE)) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(mud->crypto.private.encrypt.key, key, MUD_KEY_SIZE);
|
unsigned char *enc = mud->crypto.private.encrypt.key;
|
||||||
memcpy(mud->crypto.private.decrypt.key, key, MUD_KEY_SIZE);
|
unsigned char *dec = mud->crypto.private.decrypt.key;
|
||||||
|
|
||||||
|
if (key) {
|
||||||
|
memcpy(enc, key, MUD_KEY_SIZE);
|
||||||
|
sodium_memzero(key, size);
|
||||||
|
} else {
|
||||||
|
randombytes_buf(enc, MUD_KEY_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(dec, enc, MUD_KEY_SIZE);
|
||||||
|
|
||||||
mud->crypto.current = mud->crypto.private;
|
mud->crypto.current = mud->crypto.private;
|
||||||
mud->crypto.next = mud->crypto.private;
|
mud->crypto.next = mud->crypto.private;
|
||||||
@@ -596,15 +605,6 @@ mud_set_key(struct mud *mud, unsigned char *key, size_t size)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
mud_new_key(struct mud *mud)
|
|
||||||
{
|
|
||||||
unsigned char key[MUD_KEY_SIZE];
|
|
||||||
|
|
||||||
randombytes_buf(key, sizeof(key));
|
|
||||||
return mud_set_key(mud, key, sizeof(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
mud_set_tc(struct mud *mud, int tc)
|
mud_set_tc(struct mud *mud, int tc)
|
||||||
{
|
{
|
||||||
@@ -814,11 +814,13 @@ mud_create(int port, int v4, int v6)
|
|||||||
if (sodium_init() == -1)
|
if (sodium_init() == -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
struct mud *mud = calloc(1, sizeof(struct mud));
|
struct mud *mud = sodium_malloc(sizeof(struct mud));
|
||||||
|
|
||||||
if (!mud)
|
if (!mud)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
memset(mud, 0, sizeof(struct mud));
|
||||||
|
|
||||||
mud->fd = mud_create_socket(port, v4, v6);
|
mud->fd = mud_create_socket(port, v4, v6);
|
||||||
|
|
||||||
if (mud->fd == -1) {
|
if (mud->fd == -1) {
|
||||||
@@ -859,7 +861,7 @@ mud_delete(struct mud *mud)
|
|||||||
errno = err;
|
errno = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(mud);
|
sodium_free(mud);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|||||||
1
mud.h
1
mud.h
@@ -9,7 +9,6 @@ void mud_delete (struct mud *);
|
|||||||
|
|
||||||
int mud_get_fd (struct mud *);
|
int mud_get_fd (struct mud *);
|
||||||
|
|
||||||
int mud_new_key (struct mud *);
|
|
||||||
int mud_set_key (struct mud *, unsigned char *, size_t);
|
int mud_set_key (struct mud *, unsigned char *, size_t);
|
||||||
int mud_get_key (struct mud *, unsigned char *, size_t *);
|
int mud_get_key (struct mud *, unsigned char *, size_t *);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user