Skip to content

Commit

Permalink
buffer lenghtt is now returned by ndpi_quick_encrypt() and ndpi_quick…
Browse files Browse the repository at this point in the history
…_deecrypt()
  • Loading branch information
lucaderi committed Sep 24, 2024
1 parent 42cfd29 commit 9a8dc64
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -6207,13 +6207,15 @@ void cryptDecryptUnitTest() {
u_char enc_dec_key[64] = "9dedb817e5a8805c1de62eb8982665b9a2b4715174c34d23b9a46ffafacfb2a7" /* SHA256("nDPI") */;
const char *test_string = "The quick brown fox jumps over the lazy dog";
char *enc, *dec;
u_int16_t e_len, d_len, t_len = strlen(test_string);

enc = ndpi_quick_encrypt(test_string, strlen(test_string), enc_dec_key);
enc = ndpi_quick_encrypt(test_string, t_len, &e_len, enc_dec_key);
assert(enc != NULL);
dec = ndpi_quick_decrypt((const char*)enc, strlen(enc), enc_dec_key);
dec = ndpi_quick_decrypt((const char*)enc, e_len, &d_len, enc_dec_key);
assert(dec != NULL);
assert(t_len == d_len);

assert(strcmp(dec, test_string) == 0);
assert(strncmp(dec, test_string, e_len) == 0);

ndpi_free(enc);
ndpi_free(dec);
Expand Down Expand Up @@ -6387,7 +6389,7 @@ int main(int argc, char **argv) {
printf("nDPI Library version mismatch: please make sure this code and the nDPI library are in sync\n");
return(-1);
}

if(!skip_unit_tests) {
#ifndef DEBUG_TRACE
/* Skip tests when debugging */
Expand Down
2 changes: 2 additions & 0 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2314,10 +2314,12 @@ extern "C" {

char* ndpi_quick_encrypt(const char *cleartext_msg,
u_int16_t cleartext_msg_len,
u_int16_t *encrypted_msg_len,
u_char encrypt_key[64]);

char* ndpi_quick_decrypt(const char *encrypted_msg,
u_int16_t encrypted_msg_len,
u_int16_t *decrypted_msg_len,
u_char decrypt_key[64]);

/* ******************************* */
Expand Down
9 changes: 9 additions & 0 deletions src/lib/ndpi_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3734,6 +3734,7 @@ u_int ndpi_bin2hex(u_char *out, u_int out_len, u_char* in, u_int in_len) {
*/
char* ndpi_quick_encrypt(const char *cleartext_msg,
u_int16_t cleartext_msg_len,
u_int16_t *encrypted_msg_len,
u_char encrypt_key[64]) {
char *encoded = NULL, *encoded_buf;
struct AES_ctx ctx;
Expand All @@ -3745,6 +3746,7 @@ char* ndpi_quick_encrypt(const char *cleartext_msg,
* But AES, being a block cipher, requires the input to be multiple of block size (16 bytes). */
encoded_len = cleartext_msg_len + 16 - (cleartext_msg_len % 16);

*encrypted_msg_len = 0;
encoded_buf = (char *)ndpi_calloc(encoded_len, 1);

if (encoded_buf == NULL) {
Expand All @@ -3768,13 +3770,16 @@ char* ndpi_quick_encrypt(const char *cleartext_msg,
encoded = ndpi_base64_encode((const unsigned char *)encoded_buf, encoded_len);
ndpi_free(encoded_buf);

*encrypted_msg_len = strlen(encoded);

return(encoded);
}

/* ************************************************************** */

char* ndpi_quick_decrypt(const char *encrypted_msg,
u_int16_t encrypted_msg_len,
u_int16_t *decrypted_msg_len,
u_char decrypt_key[64]) {
u_char nonce[24] = { 0x0 };
u_char binary_decrypt_key[32];
Expand All @@ -3784,6 +3789,8 @@ char* ndpi_quick_decrypt(const char *encrypted_msg,
u_int n_padding;
struct AES_ctx ctx;

*decrypted_msg_len = 0;

if(decoded_string == NULL) {
/* Allocation failure */
return(NULL);
Expand Down Expand Up @@ -3818,6 +3825,8 @@ char* ndpi_quick_decrypt(const char *encrypted_msg,
decoded_string[content_len] = 0;
}

*decrypted_msg_len = content_len;

ndpi_free(content);

return(decoded_string);
Expand Down

0 comments on commit 9a8dc64

Please sign in to comment.