diff options
-rw-r--r-- | example/ndpiReader.c | 10 | ||||
-rw-r--r-- | src/include/ndpi_api.h | 2 | ||||
-rw-r--r-- | src/lib/ndpi_utils.c | 9 |
3 files changed, 17 insertions, 4 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 1a1a34073..207056785 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -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); @@ -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 */ diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 7e695cc32..ae1540dc6 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -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]); /* ******************************* */ diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c index ebe057c56..14a716298 100644 --- a/src/lib/ndpi_utils.c +++ b/src/lib/ndpi_utils.c @@ -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; @@ -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) { @@ -3768,6 +3770,8 @@ 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); } @@ -3775,6 +3779,7 @@ char* ndpi_quick_encrypt(const char *cleartext_msg, 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]; @@ -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); @@ -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); |