some fixes
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Tue, 8 Apr 2025 15:14:04 +0000 (17:14 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Tue, 8 Apr 2025 15:14:33 +0000 (17:14 +0200)
morep_simulator.c
parse.c
parse.h
pdu_clear_data.c
pdu_encrypted_data.c
script-cross_crypto.eth
script-local_crypto.eth
simulator.h [new file with mode: 0644]

index a538c18f8c64130c830e4b31a1d1d8618b5f4632..30c059299be32fe411757fb996e65e8f2d5c7562 100644 (file)
@@ -27,8 +27,6 @@
 
 #include "morep.h"
 #include "parse.h"
-#include "verbose.h"
-
 #include "pdu_channel.h"
 #include "pdu_encrypted_data.h"
 #include "pdu_prng_param.h"
@@ -37,6 +35,8 @@
 #include "pdu_key.h"
 #include "pdu_raw_data.h"
 #include "pdu_raw_data.h"
+#include "simulator.h"
+#include "verbose.h"
 
 char *progname = NULL;
 
@@ -46,42 +46,7 @@ DECLARE_VERBOSE_LEVEL (morep, INFO);
 
 #define BUFMAX 4096
 
-typedef enum {
-    nomod_e = 0,
-    red_e,
-    cryp_e,
-    black_e
-} module_t;
-
-typedef enum {
-    noserv_e = 0,
-    cross_crypto_e,
-    local_crypto_e,
-    provisioning_e,
-    prng_e,
-    bypass_e,
-    control_e
-} service_id_t;
-
-typedef enum {
-    undef_pdu_e = 0,
-    channel_e,
-    clear_data_e,
-    encrypted_data_e,
-    key_e,
-    prng_param_e,
-    raw_data_e,
-    status_e,
-    nopdu_e
-} pdu_t;
-
-typedef struct {
-    char *name;
-    service_id_t service_id;
-    uint8_t msgtype;
-    pdu_t pdu;
-} message_t;
-
+/* definition of all messages */
 message_t message_list[] = {
 
     /* Cross cryptographic service */
@@ -128,23 +93,11 @@ message_t message_list[] = {
     {"", noserv_e, 0x00, undef_pdu_e}
 };
 
-typedef struct {
-    int morep;
-    int mode;
-    char *etype;
-} comm_t;
-
+/* list of communcation chanels */
 #define MAXCOMMS 32
-
 comm_t comm_list[MAXCOMMS] = {0};
 
-typedef struct {
-    char *name;
-    service_id_t service_id;
-    comm_t tx;
-    comm_t rx;
-} service_t;
-
+/* definition of all services */
 service_t service_list[] = {
     {"CROSS_CRYPTO", cross_crypto_e, {-1, 0, "0809"}, {-1, 1, "0809"}},
     {"LOCAL_CRYPTO", local_crypto_e, {-1, 0, "080a"}, {-1, 1, "080a"}},
@@ -155,6 +108,7 @@ service_t service_list[] = {
     {"", noserv_e, {-1, 0, "0000"}, {-1, 1, "0000"}}
 };
 
+/* signal handler */
 void sig_handler (int sig)
 {
     switch (sig) {
@@ -168,6 +122,7 @@ void sig_handler (int sig)
     }
 }
 
+/* read a file */
 char *read_stream (FILE *sd, int *plen)
 {
     VERBOSE (morep, TRACE, PRINTF ("read_stream\n"));
@@ -199,19 +154,7 @@ char *read_stream (FILE *sd, int *plen)
     return buffer;
 }
 
-void print_message (FILE *fd, char *etype, int mode, uint8_t msg, int seqnum, uint8_t *payload, int len)
-{
-    fprintf (fd ? fd : stdout, "%c%s SEG=%d MSG=%d LEN=%d", mode ? 'T' : 'R', etype, seqnum, msg, len);
-    if (len > 0) {
-        int i;
-        fprintf (fd ? fd : stdout, " PAYLOAD=");
-        for (i = 0; i < len; i++) {
-            fprintf (fd, "%02x", payload[i]);
-        }
-    }
-    fprintf (fd ? fd : stdout, "\n");
-}
-
+/* main function */
 int main (int argc, char **argv)
 {
     char *filename = NULL;
@@ -597,7 +540,8 @@ int main (int argc, char **argv)
 
         /* log message */
         if (log) {
-            fprintf (log, "%c%s[%s] SEG=%d MSG=%d LEN=%d %s ", mode ? 'T' : 'R', serv->name, comm->etype, seqnum, msg->msgtype, len, msg->name);
+            fprintf (log, "%c%s[%s] SEG=%d MSG=%d LEN=%d %s ", mode ? 'T' : 'R', serv->name,
+                     comm->etype, seqnum, msg->msgtype, len, msg->name);
             char buffer[1496 * 3 + 256] = {0};
             switch (pdu) {
             case channel_e:
diff --git a/parse.c b/parse.c
index a8f8876d0fbc322e9cced4c8c83c9d7df32d2f59..8f6e4439eb9d6435bf2acab45da16dd4fad20c08 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -47,6 +47,7 @@ int parse_array (char *str, uint8_t *buffer, int maxlen)
 
     /* string payload: "..." (space must be protected by '\') */
     if ((*str == '"') && (slen > 1)) {
+        VERBOSE (morep, TRACE, PRINTF ("string payload: \"...\"\n"));
         if (maxlen < slen - 2) slen = maxlen + 2;
         if (str[slen - 1] == '"') {
             len = slen - 2;
@@ -62,6 +63,7 @@ int parse_array (char *str, uint8_t *buffer, int maxlen)
 
     /* file payload: @filename */
     else if (*str == '@') {
+        VERBOSE (morep, TRACE, PRINTF ("file payload: @filename\n"));
         FILE *fid = fopen (str + 1, "r");
         if (fid != NULL) {
             while ((len < maxlen) && (!feof (fid))) {
@@ -81,8 +83,11 @@ int parse_array (char *str, uint8_t *buffer, int maxlen)
 
     /* hexa payload: xxxxxx [0-9a-fA-F] */
     else {
-        if (maxlen * 2 < slen) slen = maxlen * 2;
+        if (maxlen * 2 < slen) {
+            slen = maxlen * 2;
+        }
         if (slen % 2 == 0) {
+            VERBOSE (morep, TRACE, PRINTF ("hexa payload: xxxxxx\n"));
             len = slen / 2;
             if (len > maxlen) {
                 VERBOSE (morep, WARNING, PRINTF ("string too large (%d > %d) for '%s'\n", len, maxlen, str));
@@ -91,8 +96,8 @@ int parse_array (char *str, uint8_t *buffer, int maxlen)
             for (int i = 0; i < len; i++) {
                 char digit[3] = {0};
                 char *ptr = NULL;
-                digit[0] = str[3 * i];
-                digit[1] = str[3 * i + 1];
+                digit[0] = str[2 * i];
+                digit[1] = str[2 * i + 1];
                 buffer[i] = strtol (digit, &ptr, 16);
                 if ((*ptr != '\0') && (*ptr != ' ') && (*ptr != '\t')) {
                     VERBOSE (morep, ERROR, PRINTF ("unrecognize hexa-string (%d) '%s'|%s\n", 2 * i, str, ptr));
@@ -109,12 +114,12 @@ int parse_array (char *str, uint8_t *buffer, int maxlen)
     return len;
 }
 
-int snprint_array (char *buffer, int size, uint8_t *data, int nb)
+int snprint_array (char *buffer, int size, char *name, uint8_t *data, int nb)
 {
     int i;
-    int len = 0;
+    int len = snprintf (buffer, size, " %s[%d]=", name, nb);
     for (i = 0; (i < nb) && (len < size); i++) {
-        len += snprintf (buffer + 2 * i, size - len, "%02x", data[i]);
+        len += snprintf (buffer + len, size - len, "%02x", data[i]);
     }
     return len;
 }
diff --git a/parse.h b/parse.h
index 3c9514007b2e8ced34ace0d3ea5897fc85784efb..e09d9f6c2d4654998c031ff3444a70b5ff80c65b 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -115,10 +115,11 @@ __BEGIN_DECLS
 
    @param name field name
    @param buf preallocated storage
+   @param optinal parameter to use defined length
 */
-#define PARSE_ARRAY(name, buf)                                                               \
+#define PARSE_ARRAY(name, buf, ...)                                                               \
         else if (strcmp (var, name) == 0) {                                                  \
-            buf##_len = parse_array (val, buf, (buf##_len) ? buf##_len : (int)sizeof (buf)); \
+            buf##_len = parse_array (val, buf, (1 == __VA_ARGS__ + 0) ? buf##_len : (int)sizeof (buf)); \
         }
 
 /**
@@ -171,7 +172,7 @@ __BEGIN_DECLS
    @param field data from structure
 */
 #define FORMAT_INT(name, field)                                              \
-    len += snprintf (_buffer, _maxlen - len, "%d", field);                   \
+    len += snprintf (_buffer + len, _maxlen - len, " %s=%d", name, field);   \
     if (len == _maxlen) {                                                    \
         VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field)); \
     }
@@ -185,7 +186,7 @@ __BEGIN_DECLS
    @param field data from structure
 */
 #define FORMAT_DOUBLE(name, field)                                           \
-    len += snprintf (_buffer, _maxlen - len, "%g", field);                   \
+    len += snprintf (_buffer + len, _maxlen - len, " %s=%g", name, field);   \
     if (len == _maxlen) {                                                    \
         VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field)); \
     }
@@ -198,10 +199,12 @@ __BEGIN_DECLS
    @param name field name
    @param field data from structure
 */
-#define FORMAT_ARRAY(name, field)                                            \
-    len += snprint_array (_buffer, _maxlen - len, field, field##_len);       \
-    if (len == _maxlen) {                                                    \
-        VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field)); \
+#define FORMAT_ARRAY(name, field)                                                      \
+    if (field##_len) {                                                                 \
+        len += snprint_array (_buffer + len, _maxlen - len, name, field, field##_len); \
+        if (len == _maxlen) {                                                          \
+            VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field));       \
+        }                                                                              \
     }
 
 /**
@@ -212,10 +215,10 @@ __BEGIN_DECLS
    @param name field name
    @param field data from structure
 */
-#define FORMAT_TAB(name, field)                                              \
-    len += snprint_array (_buffer, _maxlen - len, field, sizeof (field));    \
-    if (len == _maxlen) {                                                    \
-        VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field)); \
+#define FORMAT_TAB(name, field)                                                       \
+    len += snprint_array (_buffer + len, _maxlen - len, name, field, sizeof (field)); \
+    if (len == _maxlen) {                                                             \
+        VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field));          \
     }
 
 /**
@@ -383,7 +386,7 @@ __BEGIN_DECLS
     case 2:                                                                      \
         if (ptr + 1 < end) {                                                     \
             field = ntohs (*((uint16_t *)ptr));                                  \
-            ptr -= 2;                                                            \
+            ptr += 2;                                                            \
         } else  {                                                                \
             VERBOSE (morep, WARNING, PRINTF ("no data for field '%s'\n", name)); \
             ptr = end + 1;                                                       \
@@ -392,7 +395,7 @@ __BEGIN_DECLS
     case 4:                                                                      \
         if (ptr + 3 < end) {                                                     \
             field = ntohl (*((uint32_t *)ptr));                                  \
-            ptr -= 4;                                                            \
+            ptr += 4;                                                            \
         } else  {                                                                \
             VERBOSE (morep, WARNING, PRINTF ("no data for field '%s'\n", name)); \
             ptr = end + 1;                                                       \
@@ -413,7 +416,7 @@ __BEGIN_DECLS
     case 4:                                                                      \
         if (ptr + 3 < end) {                                                     \
             field = *(float *)ptr;                                               \
-            ptr -= 4;                                                            \
+            ptr += 4;                                                            \
         } else  {                                                                \
             VERBOSE (morep, WARNING, PRINTF ("no data for field '%s'\n", name)); \
             ptr = end + 1;                                                       \
@@ -422,7 +425,7 @@ __BEGIN_DECLS
     case 8:                                                                      \
         if (ptr + 7 < end) {                                                     \
             field = *(double *)ptr;                                              \
-            ptr -= 8;                                                            \
+            ptr += 8;                                                            \
         } else  {                                                                \
             VERBOSE (morep, WARNING, PRINTF ("no data for field '%s'\n", name)); \
             ptr = end + 1;                                                       \
@@ -455,9 +458,10 @@ __BEGIN_DECLS
 
    @param name field name
    @param field structure storage
+   @param optinal parameter to use defined length
 */
-#define DESERIAL_ARRAY(name, field)                                          \
-    if (field##_len == 0) {                                                  \
+#define DESERIAL_ARRAY(name, field, ...)                                     \
+    if ((0 == __VA_ARGS__ + 0) && (ptr < end)) {                             \
         memcpy (field, ptr, end - ptr);                                      \
         field##_len = end - ptr;                                             \
         ptr = end;                                                           \
@@ -485,7 +489,7 @@ double parse_double (char *val);
 
 int parse_array (char *val, uint8_t *buffer, int maxlen);
 
-int snprint_array (char *buffer, int size, uint8_t *data, int nb);
+int snprint_array (char *buffer, int size, char *name, uint8_t *data, int nb);
 
 #endif /* __PARSE_H__ */
 
index edc81922abe44126ebe28a8b8ee7e1b972246eda..04f81b17ae2b3b1447ab8fab5d680e1e35952cbf 100644 (file)
@@ -23,7 +23,7 @@ int parse_clear_data (char *line, CLEAR_DATA_t *out)
     BEGIN_PARSE (line)
     PARSE_INT ("CHANNELID", out->channel_id)
     PARSE_INT ("BYPASSLEN", out->bypass_len)
-    PARSE_ARRAY ("BYPASS", out->bypass)
+    PARSE_ARRAY ("BYPASS", out->bypass, 1)
     PARSE_ARRAY ("DATA", out->data)
     END_PARSE ()
 }
@@ -53,7 +53,7 @@ int deserial_clear_data (uint8_t *buffer, int len, CLEAR_DATA_t *out)
     BEGIN_DESERIAL (buffer, len)
     DESERIAL_INT ("CHANNELID", out->channel_id)
     DESERIAL_INT ("BYPASSLEN", out->bypass_len)
-    DESERIAL_ARRAY ("BYPASS", out->bypass)
+    DESERIAL_ARRAY ("BYPASS", out->bypass, 1)
     DESERIAL_ARRAY ("DATA", out->data)
     END_DESERIAL ()
 }
index 194dcadc492e46cf0ff00694b2aaaa7697755f4a..6220d03895459ba48e295c2e79566e12cab79004 100644 (file)
@@ -24,7 +24,7 @@ int parse_encrypted_data (char *line, ENCRYPTED_DATA_t *out)
     PARSE_INT ("CHANNELID", out->channel_id)
     PARSE_TAB ("IV", out->iv)
     PARSE_INT ("BYPASSLEN", out->bypass_len)
-    PARSE_ARRAY ("BYPASS", out->bypass)
+    PARSE_ARRAY ("BYPASS", out->bypass, 1)
     PARSE_ARRAY ("DATA", out->data)
     END_PARSE ()
 }
@@ -57,7 +57,7 @@ int deserial_encrypted_data (uint8_t *buffer, int len, ENCRYPTED_DATA_t *out)
     DESERIAL_INT ("CHANNELID", out->channel_id)
     DESERIAL_TAB ("IV", out->iv)
     DESERIAL_INT ("BYPASSLEN", out->bypass_len)
-    DESERIAL_ARRAY ("BYPASS", out->bypass)
+    DESERIAL_ARRAY ("BYPASS", out->bypass, 1)
     DESERIAL_ARRAY ("DATA", out->data)
     END_DESERIAL ()
 }
index f2fa7ba7a4427dfb36ce4af8c8314db679a28dc6..1efb78b03c562d35b6c1420abd48a25fc21e253a 100644 (file)
@@ -1,13 +1,13 @@
-# Test script
+# Cross crypto test script
 
-TCROSS_CRYPTO ENCRYPT_CROSS_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth
-RCROSS_CRYPTO ENCRYPT_CROSS_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth
+T:CROSS_CRYPTO ENCRYPT_CROSS_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11223344 DATA=@script-local_crypto.eth
+R:CROSS_CRYPTO ENCRYPT_CROSS_ASYNC
 
-TCROSS_CRYPTO ENCRYPTED_CROSS_ASYNC CHANNELID=9 BYPASSLEN=0 DATA=@script-local_crypto.eth
-RCROSS_CRYPTO ENCRYPTED_CROSS_ASYNC CHANNELID=9 BYPASSLEN=0 DATA=@script-local_crypto.eth
+T:CROSS_CRYPTO ENCRYPTED_CROSS_ASYNC CHANNELID=9 BYPASSLEN=1 BYPASS=55 DATA=@script-local_crypto.eth
+RCROSS_CRYPTO ENCRYPTED_CROSS_ASYNC
 
-TCROSS_CRYPTO DECRYPT_CROSS_ASYNC CHANNELID=5 BYPASSLEN=0 DATA=@script-local_crypto.eth
+T:CROSS_CRYPTO DECRYPT_CROSS_ASYNC CHANNELID=5 BYPASSLEN=0 DATA=@script-local_crypto.eth
 RCROSS_CRYPTO DECRYPT_CROSS_ASYNC CHANNELID=5 BYPASSLEN=0 DATA=@script-local_crypto.eth
 
-TCROSS_CRYPTO DECRYPTED_CROSS_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth
-RCROSS_CRYPTO DECRYPTED_CROSS_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth
+T:CROSS_CRYPTO DECRYPTED_CROSS_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11223344 DATA=@script-cross_crypto.eth
+R:CROSS_CRYPTO DECRYPTED_CROSS_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11223344 DATA=@script-cross_crypto.eth
index e9e43122b0a4cd734e425204756332bcb4718ced..b1fd5d14386df8f1bf82f0099dcf64ec9d6d16e4 100644 (file)
@@ -1,4 +1,4 @@
-# Test script
+# Local crypto test script
 
 T:LOCAL_CRYPTO ENCRYPT_LOCAL_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth
 R:LOCAL_CRYPTO ENCRYPT_LOCAL_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth
diff --git a/simulator.h b/simulator.h
new file mode 100644 (file)
index 0000000..379c29a
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+  File name        : simulator.10
+  Projet           : MERLIN
+  Date of creation : 2025/04/10
+  Version          : 1.0
+  Copyright        : Thales SIX
+  Author           : Laurent Mazet <laurent.mazet@thalesgroup.com>
+
+  Description      : This file defines simulator types
+
+  History          :
+  - initial version
+*/
+
+#ifndef __SIMULATOR_H__
+#define __SIMULATOR_H__
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+/**
+   @ingroup SIMULATOR
+
+   Module enumarate values
+*/
+typedef enum {
+    nomod_e = 0, /**< no module defined*/
+    red_e, /**< red radio module */
+    cryp_e, /**< cryptographic module */
+    black_e /**< black radio module */
+} module_t;
+
+/**
+   @ingroup SIMULATOR
+
+   Service enumarate values
+*/
+typedef enum {
+    noserv_e = 0, /**< no service defined */
+    cross_crypto_e, /**< cross cryptographic service */
+    local_crypto_e, /**< local cryptographic service */
+    provisioning_e, /**< provisioning service */
+    prng_e, /**< PRNG service */
+    bypass_e, /**< bypass service */
+    control_e /**< controlservice */
+} service_id_t;
+
+/**
+   @ingroup SIMULATOR
+
+   PDU enumarate values
+*/
+typedef enum {
+    undef_pdu_e = 0, /**< undefined PDU */
+    channel_e, /**< CHANNEL_t PDU */
+    clear_data_e, /**< CLEAR_DATA_t PDU */
+    encrypted_data_e, /**< ENCRYPTED_DATA_t PDU */
+    key_e, /**< KEY_t PDU */
+    prng_param_e, /**< PRNG_PARAM_t PDU */
+    raw_data_e, /**< RAW_DATA_t PDU */
+    status_e, /**< STATUS_t PDU */
+    nopdu_e /**< no PDU */
+} pdu_t;
+
+/**
+   @ingroup SIMULATOR
+
+   Message type
+*/
+typedef struct {
+    char *name; /**< message name */
+    service_id_t service_id; /**< associated service id */
+    uint8_t msgtype; /**< message id */
+    pdu_t pdu; /**< associated PDU type */
+} message_t;
+
+/**
+   @ingroup SIMULATOR
+
+   Communition channel type
+*/
+typedef struct {
+    int morep; /**< referent MOREP index */
+    int mode; /**< transmission mode: 0 for TX, 1 for RX */
+    char *etype; /**< associated Ethertype in a string format */
+} comm_t;
+
+/**
+   @ingroup SIMULATOR
+
+   Service type
+*/
+typedef struct {
+    char *name; /**< service name */
+    service_id_t service_id; /**< service id */
+    comm_t tx; /**< associated TX communication channel */
+    comm_t rx; /**< assiciated RX communication channel */
+} service_t;
+
+#endif /* __SIMULATOR_H__ */
+
+/* vim: set ts=4 sw=4 si et: */