Ensure encryption buffer doesn't move between nonce and cypher text allocation

This commit is contained in:
Jeremy Lakeman 2013-02-22 16:27:37 +10:30
parent 48f7cd7d5d
commit af584994a2
2 changed files with 8 additions and 9 deletions

View File

@ -147,7 +147,7 @@ int ob_makespace(struct overlay_buffer *b,int bytes)
}
// already enough space?
if (b->position + bytes < b->allocSize)
if (b->position + bytes <= b->allocSize)
return 0;
if (b->bytes && !b->allocated)

View File

@ -327,8 +327,13 @@ int overlay_mdp_decrypt(struct overlay_frame *f, overlay_mdp_frame *mdp)
}
unsigned char *nonce=ob_get_bytes_ptr(f->payload, nb);
if (!nonce)
RETURN(WHYF("Expected %d bytes of nonce", nb));
int cipher_len=ob_remaining(f->payload);
unsigned char *cipher_text=ob_get_bytes_ptr(f->payload, cipher_len);
if (!cipher_text)
RETURN(WHYF("Expected %d bytes of cipher text", cipher_len));
unsigned char plain_block[cipher_len+cz];
@ -680,9 +685,9 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int userGeneratedFrameP,
ob_free(plaintext);
frame->payload = ob_new();
ob_makespace(frame->payload, nb+cipher_len);
unsigned char *nonce = ob_append_space(frame->payload, nb);
unsigned char *nonce = ob_append_space(frame->payload, nb+cipher_len);
unsigned char *cipher_text = nonce + nb;
if (!nonce)
RETURN(-1);
if (urandombytes(nonce,nb)) {
@ -699,12 +704,6 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int userGeneratedFrameP,
op_free(frame);
RETURN(WHY("could not compute Curve25519(NxM)"));
}
/* Get pointer to place in frame where the ciphered text needs to go */
unsigned char *cipher_text=ob_append_space(frame->payload,cipher_len);
if ((!cipher_text)){
op_free(frame);
RETURN(WHY("could not make space for ciphered text"));
}
/* Actually authcrypt the payload */
if (crypto_box_curve25519xsalsa20poly1305_afternm
(cipher_text,plain,cipher_len,nonce,k)){