"Fossies" - the Fresh Open Source Software Archive

Member "n2n-3.1.1/tools/tests-transform.c" (31 Mar 2022, 7361 Bytes) of package /linux/misc/n2n-3.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * (C) 2007-22 - ntop.org and contributors
    3  *
    4  * This program is free software; you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation; either version 3 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, see <http://www.gnu.org/licenses/>
   16  *
   17  */
   18 
   19 #include <inttypes.h>
   20 
   21 #include "n2n.h"
   22 #include "hexdump.h"
   23 
   24 #define DURATION                2.5   // test duration per algorithm
   25 #define PACKETS_BEFORE_GETTIME  2047  // do not check time after every packet but after (2 ^ n - 1)
   26 
   27 
   28 uint8_t PKT_CONTENT[]={
   29     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   30     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   31     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   32     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   33     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   34     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   35     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   36     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   37     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   38     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   39     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   40     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   41     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   42     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   43     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
   44     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
   45 };
   46 
   47 /* Prototypes */
   48 static ssize_t do_encode_packet ( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c );
   49 static void run_transop_benchmark (const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf);
   50 
   51 
   52 int main (int argc, char * argv[]) {
   53 
   54     uint8_t pktbuf[N2N_PKT_BUF_SIZE];
   55     n2n_trans_op_t transop_null, transop_tf;
   56     n2n_trans_op_t transop_aes;
   57     n2n_trans_op_t transop_cc20;
   58     n2n_trans_op_t transop_speck;
   59     n2n_trans_op_t transop_lzo;
   60 #ifdef HAVE_ZSTD
   61     n2n_trans_op_t transop_zstd;
   62 #endif
   63     n2n_edge_conf_t conf;
   64 
   65     /* Init configuration */
   66     edge_init_conf_defaults(&conf);
   67     strncpy((char *)conf.community_name, "abc123def456", sizeof(conf.community_name));
   68     conf.encrypt_key = "SoMEVer!S$cUREPassWORD";
   69 
   70     char *test_name = "environment";
   71     printf("%s: community_name = \"%s\"\n", test_name, conf.community_name);
   72     printf("%s: encrypt_key = \"%s\"\n", test_name, conf.encrypt_key);
   73     printf("%s: input size = 0x%" PRIx64 "\n", test_name, sizeof(PKT_CONTENT));
   74     fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout);
   75     printf("\n");
   76 
   77     /* Init transopts */
   78     n2n_transop_null_init(&conf, &transop_null);
   79     n2n_transop_tf_init(&conf, &transop_tf);
   80     n2n_transop_aes_init(&conf, &transop_aes);
   81     n2n_transop_cc20_init(&conf, &transop_cc20);
   82     n2n_transop_speck_init(&conf, &transop_speck);
   83     n2n_transop_lzo_init(&conf, &transop_lzo);
   84 #ifdef HAVE_ZSTD
   85     n2n_transop_zstd_init(&conf, &transop_zstd);
   86 #endif
   87 
   88     /* Run the tests */
   89     /* FIXME: interop tests are pretty useless without the expected encrypted buffer data */
   90     run_transop_benchmark("null", &transop_null, &conf, pktbuf);
   91     run_transop_benchmark("tf", &transop_tf, &conf, pktbuf);
   92     run_transop_benchmark("aes", &transop_aes, &conf, pktbuf);
   93     run_transop_benchmark("cc20", &transop_cc20, &conf, pktbuf);
   94     run_transop_benchmark("speck", &transop_speck, &conf, pktbuf);
   95     run_transop_benchmark("lzo", &transop_lzo, &conf, pktbuf);
   96 #ifdef HAVE_ZSTD
   97     run_transop_benchmark("zstd", &transop_zstd, &conf, pktbuf);
   98 #else
   99     // FIXME - output dummy data to the stdout for easy comparison
  100     printf("zstd: output size = 0x47\n");
  101     printf("000: 03 02 00 03 61 62 63 31  32 33 64 65 66 34 35 36   |    abc123def456|\n");
  102     printf("010: 00 00 00 00 00 00 00 00  00 01 02 03 04 05 00 01   |                |\n");
  103     printf("020: 02 03 04 05 00 00 28 b5  2f fd 60 00 01 bd 00 00   |      ( / `     |\n");
  104     printf("030: 80 00 01 02 03 04 05 06  07 08 09 0a 0b 0c 0d 0e   |                |\n");
  105     printf("040: 0f 01 00 da 47 9d 4b                               |    G K|\n");
  106 
  107     fprintf(stderr, "%s: not compiled - dummy data output\n", "zstd");
  108     printf("\n");
  109 #endif
  110 
  111     /* Cleanup */
  112     transop_null.deinit(&transop_null);
  113     transop_tf.deinit(&transop_tf);
  114     transop_aes.deinit(&transop_aes);
  115     transop_cc20.deinit(&transop_cc20);
  116     transop_speck.deinit(&transop_speck);
  117     transop_lzo.deinit(&transop_lzo);
  118 #ifdef HAVE_ZSTD
  119     transop_zstd.deinit(&transop_zstd);
  120 #endif
  121 
  122     return 0;
  123 }
  124 
  125 // --- transop benchmark ------------------------------------------------------------------
  126 
  127 static void run_transop_benchmark (const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf) {
  128     n2n_common_t cmn;
  129     n2n_PACKET_t pkt;
  130     n2n_mac_t mac_buf;
  131     uint8_t decodebuf[N2N_PKT_BUF_SIZE];
  132     size_t idx;
  133     size_t rem;
  134     size_t nw;
  135 
  136     // encryption
  137     memset(mac_buf, 0, sizeof(mac_buf));
  138 
  139     nw = do_encode_packet( pktbuf, N2N_PKT_BUF_SIZE, conf->community_name);
  140     nw += op_fn->fwd(op_fn,
  141                      pktbuf+nw, N2N_PKT_BUF_SIZE-nw,
  142                      PKT_CONTENT, sizeof(PKT_CONTENT), mac_buf);
  143 
  144     printf("%s: output size = 0x%" PRIx64 "\n", op_name, nw);
  145     fhexdump(0, pktbuf, nw, stdout);
  146 
  147     // decrpytion
  148     idx=0;
  149     rem=nw;
  150     decode_common( &cmn, pktbuf, &rem, &idx);
  151     decode_PACKET( &pkt, &cmn, pktbuf, &rem, &idx );
  152     op_fn->rev(op_fn, decodebuf, sizeof(decodebuf), pktbuf+idx, rem, 0);
  153 
  154     if(memcmp(decodebuf, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) {
  155         fprintf(stderr, "%s: round-trip buffer mismatch\n", op_name);
  156         exit(1);
  157     }
  158 
  159     fprintf(stderr, "%s: tested\n", op_name);
  160     printf("\n");
  161 }
  162 
  163 
  164 static ssize_t do_encode_packet ( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c )
  165 {
  166     // FIXME: this is a parameter of the test environment
  167     n2n_mac_t destMac={0,1,2,3,4,5};
  168 
  169     n2n_common_t cmn;
  170     n2n_PACKET_t pkt;
  171     size_t idx;
  172 
  173 
  174     memset( &cmn, 0, sizeof(cmn) );
  175     cmn.ttl = N2N_DEFAULT_TTL;
  176     cmn.pc = n2n_packet;
  177     cmn.flags=0; /* no options, not from supernode, no socket */
  178     memcpy( cmn.community, c, N2N_COMMUNITY_SIZE );
  179 
  180     memset( &pkt, 0, sizeof(pkt) );
  181     memcpy( pkt.srcMac, destMac, N2N_MAC_SIZE);
  182     memcpy( pkt.dstMac, destMac, N2N_MAC_SIZE);
  183 
  184     pkt.sock.family=0; /* do not encode sock */
  185 
  186     idx=0;
  187     encode_PACKET( pktbuf, &idx, &cmn, &pkt );
  188     traceEvent( TRACE_DEBUG, "encoded PACKET header of size=%u", (unsigned int)idx );
  189 
  190     return idx;
  191 }