@@ -561,8 +561,10 @@ int client_send_encrypted_(client_context_t *context,
561561 byte nonce[12 ];
562562 memset (nonce, 0 , sizeof (nonce));
563563
564- byte encrypted[1024 + 18 ];
565- uint payload_offset = 0 ;
564+ #define ENCRYPTED_BUFFER_SIZE 1024
565+ #define AAD_SIZE 2
566+ byte *encrypted = (byte*)malloc (ENCRYPTED_BUFFER_SIZE + 16 + AAD_SIZE);
567+ size_t payload_offset = 0 ;
566568
567569 while (payload_offset < size) {
568570 size_t chunk_size = size - payload_offset;
@@ -580,19 +582,21 @@ int client_send_encrypted_(client_context_t *context,
580582 x /= 256 ;
581583 }
582584
583- size_t available = sizeof (encrypted) - 2 ;
584- int r = crypto_chacha20poly1305_encrypt (context->read_key , nonce, aead, 2 ,
585- payload + payload_offset, chunk_size, encrypted + 2 , &available);
585+ size_t available = ENCRYPTED_BUFFER_SIZE + 16 ;
586+ int r = crypto_chacha20poly1305_encrypt (context->read_key , nonce, aead, AAD_SIZE ,
587+ payload + payload_offset, chunk_size, encrypted + AAD_SIZE , &available);
586588 if (r) {
587589 ERROR (" Failed to chacha encrypt payload (code %d)" , r);
590+ free (encrypted);
588591 return -1 ;
589592 }
590593
591594 payload_offset += chunk_size;
592595
593- write (context, encrypted, available + 2 );
596+ write (context, encrypted, available + AAD_SIZE );
594597 }
595598
599+ free (encrypted);
596600 return 0 ;
597601}
598602
@@ -616,8 +620,8 @@ int client_decrypt_(client_context_t *context,
616620 byte nonce[12 ];
617621 memset (nonce, 0 , sizeof (nonce));
618622
619- int payload_offset = 0 ;
620- int decrypted_offset = 0 ;
623+ size_t payload_offset = 0 ;
624+ size_t decrypted_offset = 0 ;
621625
622626 while (payload_offset < payload_size) {
623627 size_t chunk_size = payload[payload_offset] + payload[payload_offset + 1 ] * 256 ;
0 commit comments