aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuca Deri <lucaderi@users.noreply.github.com>2023-12-23 10:47:28 +0100
committerGitHub <noreply@github.com>2023-12-23 10:47:28 +0100
commit99d48383286fbb865ab58db5e5f768d8ed14f41e (patch)
treeb76de4e166329667b1db41896f2f28508c5d1be4 /src
parentf2be9722242880fb9bff3be31d59dae6a25a8690 (diff)
New ndpi_sha256() nDPI API call (#2230)
* Added ndpi_sha256.c to the Windows project * Added ndpi_sha256() nDPI API call
Diffstat (limited to 'src')
-rw-r--r--src/include/ndpi_api.h2
-rw-r--r--src/lib/ndpi_main.c11
-rw-r--r--src/lib/protocols/tls.c11
-rw-r--r--src/lib/third_party/include/ndpi_sha256.h11
-rw-r--r--src/lib/third_party/src/ndpi_sha256.c8
5 files changed, 26 insertions, 17 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h
index 080f8d924..168014c2e 100644
--- a/src/include/ndpi_api.h
+++ b/src/include/ndpi_api.h
@@ -1787,6 +1787,8 @@ extern "C" {
/* ******************************* */
void ndpi_md5(const u_char *data, size_t data_len, u_char hash[16]);
+ void ndpi_sha256(const u_char *data, size_t data_len, u_int8_t sha_hash[32]);
+
u_int16_t ndpi_crc16_ccit(const void* data, size_t n_bytes);
u_int16_t ndpi_crc16_ccit_false(const void *data, size_t n_bytes);
u_int16_t ndpi_crc16_xmodem(const void *data, size_t n_bytes);
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index e80cef78e..0b9b28864 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -124,6 +124,7 @@
/* Third party libraries */
#include "third_party/include/ndpi_patricia.h"
#include "third_party/include/ndpi_md5.h"
+#include "third_party/include/ndpi_sha256.h"
#ifdef HAVE_NBPF
#include "nbpf.h"
@@ -10092,6 +10093,16 @@ void ndpi_md5(const u_char *data, size_t data_len, u_char hash[16]) {
/* ******************************************************************** */
+void ndpi_sha256(const u_char *data, size_t data_len, u_int8_t sha_hash[32]) {
+ ndpi_SHA256_CTX sha_ctx;
+
+ ndpi_sha256_init(&sha_ctx);
+ ndpi_sha256_update(&sha_ctx, data, data_len);
+ ndpi_sha256_final(&sha_ctx, sha_hash);
+}
+
+/* ******************************************************************** */
+
static int enough(int a, int b) {
u_int8_t percentage = 20;
diff --git a/src/lib/protocols/tls.c b/src/lib/protocols/tls.c
index 9bc948f23..fb3bd7d2e 100644
--- a/src/lib/protocols/tls.c
+++ b/src/lib/protocols/tls.c
@@ -1605,8 +1605,7 @@ static void ndpi_compute_ja4(struct ndpi_detection_module_struct *ndpi_struct,
union ja_info *ja) {
u_int8_t tmp_str[JA_STR_LEN];
u_int tmp_str_len, num_extn;
- SHA256_CTX sha_ctx;
- u_int8_t sha_hash[SHA256_BLOCK_SIZE];
+ u_int8_t sha_hash[NDPI_SHA256_BLOCK_SIZE];
char ja_str[JA_STR_LEN];
u_int16_t ja_str_len, i;
int rc;
@@ -1690,9 +1689,7 @@ static void ndpi_compute_ja4(struct ndpi_detection_module_struct *ndpi_struct,
if((rc > 0) && (tmp_str_len + rc < JA_STR_LEN)) tmp_str_len += rc; else break;
}
- ndpi_sha256_init(&sha_ctx);
- ndpi_sha256_update(&sha_ctx, tmp_str, tmp_str_len);
- ndpi_sha256_final(&sha_ctx, sha_hash);
+ ndpi_sha256(tmp_str, tmp_str_len, sha_hash);
rc = ndpi_snprintf(&ja_str[ja_str_len], JA_STR_LEN-ja_str_len,
"%02x%02x%02x%02x%02x%02x_",
@@ -1724,9 +1721,7 @@ static void ndpi_compute_ja4(struct ndpi_detection_module_struct *ndpi_struct,
printf("[EXTN] %s [len: %u]\n", tmp_str, tmp_str_len);
#endif
- ndpi_sha256_init(&sha_ctx);
- ndpi_sha256_update(&sha_ctx, tmp_str, tmp_str_len);
- ndpi_sha256_final(&sha_ctx, sha_hash);
+ ndpi_sha256(tmp_str, tmp_str_len, sha_hash);
rc = ndpi_snprintf(&ja_str[ja_str_len], JA_STR_LEN-ja_str_len,
"%02x%02x%02x%02x%02x%02x",
diff --git a/src/lib/third_party/include/ndpi_sha256.h b/src/lib/third_party/include/ndpi_sha256.h
index 83a424faa..e04773efd 100644
--- a/src/lib/third_party/include/ndpi_sha256.h
+++ b/src/lib/third_party/include/ndpi_sha256.h
@@ -14,7 +14,8 @@
#include "ndpi_typedefs.h"
/****************************** MACROS ******************************/
-#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
+
+#define NDPI_SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
/**************************** DATA TYPES ****************************/
@@ -23,12 +24,12 @@ typedef struct {
u_int32_t datalen;
unsigned long long bitlen;
u_int32_t state[8];
-} SHA256_CTX;
+} ndpi_SHA256_CTX;
/*********************** FUNCTION DECLARATIONS **********************/
-void ndpi_sha256_init(SHA256_CTX *ctx);
-void ndpi_sha256_update(SHA256_CTX *ctx, const u_int8_t data[], size_t len);
-void ndpi_sha256_final(SHA256_CTX *ctx, u_int8_t hash[]);
+void ndpi_sha256_init(ndpi_SHA256_CTX *ctx);
+void ndpi_sha256_update(ndpi_SHA256_CTX *ctx, const u_int8_t data[], size_t len);
+void ndpi_sha256_final(ndpi_SHA256_CTX *ctx, u_int8_t hash[]);
#endif // SHA256_H
diff --git a/src/lib/third_party/src/ndpi_sha256.c b/src/lib/third_party/src/ndpi_sha256.c
index b40c796a3..a98f46622 100644
--- a/src/lib/third_party/src/ndpi_sha256.c
+++ b/src/lib/third_party/src/ndpi_sha256.c
@@ -43,7 +43,7 @@ static const u_int32_t k[64] = {
};
/*********************** FUNCTION DEFINITIONS ***********************/
-void sha256_transform(SHA256_CTX *ctx, const u_int8_t data[])
+void sha256_transform(ndpi_SHA256_CTX *ctx, const u_int8_t data[])
{
u_int32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
@@ -84,7 +84,7 @@ void sha256_transform(SHA256_CTX *ctx, const u_int8_t data[])
ctx->state[7] += h;
}
-void ndpi_sha256_init(SHA256_CTX *ctx)
+void ndpi_sha256_init(ndpi_SHA256_CTX *ctx)
{
ctx->datalen = 0;
ctx->bitlen = 0;
@@ -98,7 +98,7 @@ void ndpi_sha256_init(SHA256_CTX *ctx)
ctx->state[7] = 0x5be0cd19;
}
-void ndpi_sha256_update(SHA256_CTX *ctx, const u_int8_t data[], size_t len)
+void ndpi_sha256_update(ndpi_SHA256_CTX *ctx, const u_int8_t data[], size_t len)
{
u_int32_t i;
@@ -113,7 +113,7 @@ void ndpi_sha256_update(SHA256_CTX *ctx, const u_int8_t data[], size_t len)
}
}
-void ndpi_sha256_final(SHA256_CTX *ctx, u_int8_t hash[])
+void ndpi_sha256_final(ndpi_SHA256_CTX *ctx, u_int8_t hash[])
{
u_int32_t i;