Allow different values of AD size

This commit is contained in:
angt
2016-02-04 19:21:16 +01:00
parent 48419d0e5c
commit 3d947d872e

32
mud.c
View File

@@ -350,11 +350,15 @@ void mud_delete (struct mud *mud)
static static
int mud_encrypt (struct mud *mud, uint32_t nonce, int mud_encrypt (struct mud *mud, uint32_t nonce,
unsigned char *dst, size_t dst_size, unsigned char *dst, size_t dst_size,
const unsigned char *src, size_t src_size) const unsigned char *src, size_t src_size,
size_t ad_size)
{ {
if (!src_size) if (!src_size)
return 0; return 0;
if (ad_size > src_size)
return 0;
size_t size = src_size+4+crypto_aead_aes256gcm_ABYTES; size_t size = src_size+4+crypto_aead_aes256gcm_ABYTES;
if (size > dst_size) if (size > dst_size)
@@ -365,15 +369,15 @@ int mud_encrypt (struct mud *mud, uint32_t nonce,
mud_write32(npub, nonce); mud_write32(npub, nonce);
crypto_aead_aes256gcm_encrypt_afternm( crypto_aead_aes256gcm_encrypt_afternm(
dst+8, NULL, dst+ad_size+4, NULL,
src+4, src_size-4, src+ad_size, src_size-ad_size,
src, 4, src, ad_size,
NULL, NULL,
npub, npub,
(const crypto_aead_aes256gcm_state *)&mud->crypto.key); (const crypto_aead_aes256gcm_state *)&mud->crypto.key);
memcpy(dst, npub, 4); memcpy(dst, npub, 4);
memcpy(dst+4, src, 4); memcpy(dst+4, src, ad_size);
return size; return size;
} }
@@ -381,11 +385,15 @@ int mud_encrypt (struct mud *mud, uint32_t nonce,
static static
int mud_decrypt (struct mud *mud, uint32_t *nonce, int mud_decrypt (struct mud *mud, uint32_t *nonce,
unsigned char *dst, size_t dst_size, unsigned char *dst, size_t dst_size,
const unsigned char *src, size_t src_size) const unsigned char *src, size_t src_size,
size_t ad_size)
{ {
if (!src_size) if (!src_size)
return 0; return 0;
if (ad_size > src_size)
return 0;
size_t size = src_size-4-crypto_aead_aes256gcm_ABYTES; size_t size = src_size-4-crypto_aead_aes256gcm_ABYTES;
if (size > dst_size) if (size > dst_size)
@@ -394,13 +402,13 @@ int mud_decrypt (struct mud *mud, uint32_t *nonce,
unsigned char npub[crypto_aead_aes256gcm_NPUBBYTES] = {0}; unsigned char npub[crypto_aead_aes256gcm_NPUBBYTES] = {0};
memcpy(npub, src, 4); memcpy(npub, src, 4);
memcpy(dst, src+4, 4); memcpy(dst, src+4, ad_size);
if (crypto_aead_aes256gcm_decrypt_afternm( if (crypto_aead_aes256gcm_decrypt_afternm(
dst+4, NULL, dst+ad_size, NULL,
NULL, NULL,
src+8, src_size-8, src+ad_size+4, src_size-ad_size-4,
src+4, 4, src+ad_size, ad_size,
npub, npub,
(const crypto_aead_aes256gcm_state *)&mud->crypto.key)) (const crypto_aead_aes256gcm_state *)&mud->crypto.key))
return -1; return -1;
@@ -488,7 +496,7 @@ int mud_recv (struct mud *mud, void *data, size_t size)
struct packet *packet = &mud->rx.packet[mud->rx.start]; struct packet *packet = &mud->rx.packet[mud->rx.start];
int ret = mud_decrypt(mud, NULL, data, size, int ret = mud_decrypt(mud, NULL, data, size,
packet->data, packet->size); packet->data, packet->size, 4);
mud->rx.start++; mud->rx.start++;
@@ -561,7 +569,7 @@ int mud_send (struct mud *mud, const void *data, size_t size)
int ret = mud_encrypt(mud, now, int ret = mud_encrypt(mud, now,
packet->data, sizeof(packet->data), packet->data, sizeof(packet->data),
data, size); data, size, 4);
if (!ret) { if (!ret) {
errno = EMSGSIZE; errno = EMSGSIZE;