@@ -561,8 +561,10 @@ int client_send_encrypted_(client_context_t *context,
561
561
byte nonce[12 ];
562
562
memset (nonce, 0 , sizeof (nonce));
563
563
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 ;
566
568
567
569
while (payload_offset < size) {
568
570
size_t chunk_size = size - payload_offset;
@@ -580,19 +582,21 @@ int client_send_encrypted_(client_context_t *context,
580
582
x /= 256 ;
581
583
}
582
584
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);
586
588
if (r) {
587
589
ERROR (" Failed to chacha encrypt payload (code %d)" , r);
590
+ free (encrypted);
588
591
return -1 ;
589
592
}
590
593
591
594
payload_offset += chunk_size;
592
595
593
- write (context, encrypted, available + 2 );
596
+ write (context, encrypted, available + AAD_SIZE );
594
597
}
595
598
599
+ free (encrypted);
596
600
return 0 ;
597
601
}
598
602
@@ -616,8 +620,8 @@ int client_decrypt_(client_context_t *context,
616
620
byte nonce[12 ];
617
621
memset (nonce, 0 , sizeof (nonce));
618
622
619
- int payload_offset = 0 ;
620
- int decrypted_offset = 0 ;
623
+ size_t payload_offset = 0 ;
624
+ size_t decrypted_offset = 0 ;
621
625
622
626
while (payload_offset < payload_size) {
623
627
size_t chunk_size = payload[payload_offset] + payload[payload_offset + 1 ] * 256 ;
0 commit comments