From a2f6bbb90fc5e5a1c69095b92eb4886097691a6c Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Fri, 24 Mar 2023 13:16:29 -0400 Subject: [PATCH 01/10] [nasa-itc/cryptolib#2] Initial standalone utility files; --- util/include/standalone.h | 71 +++++++++++++++++++++ util/src_util/standalone.c | 122 +++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 util/include/standalone.h create mode 100644 util/src_util/standalone.c diff --git a/util/include/standalone.h b/util/include/standalone.h new file mode 100644 index 00000000..a4933a15 --- /dev/null +++ b/util/include/standalone.h @@ -0,0 +1,71 @@ +/* Copyright (C) 2009 - 2022 National Aeronautics and Space Administration. + All Foreign Rights are Reserved to the U.S. Government. + + This software is provided "as is" without any warranty of any kind, either expressed, implied, or statutory, + including, but not limited to, any warranty that the software will conform to specifications, any implied warranties + of merchantability, fitness for a particular purpose, and freedom from infringement, and any warranty that the + documentation will conform to the program, or any warranty that the software will be error free. + + In no event shall NASA be liable for any damages, including, but not limited to direct, indirect, special or + consequential damages, arising out of, resulting from, or in any way connected with the software or its + documentation, whether or not based upon warranty, contract, tort or otherwise, and whether or not loss was sustained + from, or arose out of the results of, or use of, the software, documentation or services provided hereunder. + + ITC Team + NASA IV&V + jstar-development-team@mail.nasa.gov +*/ + +#ifndef CRYPTOLIB_STANDALONE_H +#define CRYPTOLIB_STANDALONE_H + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/* +** Includes +*/ +#include +#include +#include +#include +#include +#include + +#include "crypto.h" +#include "shared_util.h" + + +/* +** Defines +*/ +#define TC_APPLY_PORT 76540 +#define TC_PROCESS_PORT 76541 +#define TM_APPLY_PORT 76542 +#define TM_PROCESS_PORT 76543 + + +/* +** Structures +*/ +typedef struct +{ + int sockfd; + int port; +} udp_info_t; + + +/* +** Prototypes +*/ +int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port); + + +#ifdef __cplusplus +} /* Close scope of 'extern "C"' declaration which encloses file. */ +#endif + +#endif /* CRYPTOLIB_STANDALONE_H */ diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c new file mode 100644 index 00000000..ad7e288f --- /dev/null +++ b/util/src_util/standalone.c @@ -0,0 +1,122 @@ +/* Copyright (C) 2009 - 2022 National Aeronautics and Space Administration. + All Foreign Rights are Reserved to the U.S. Government. + + This software is provided "as is" without any warranty of any kind, either expressed, implied, or statutory, + including, but not limited to, any warranty that the software will conform to specifications, any implied warranties + of merchantability, fitness for a particular purpose, and freedom from infringement, and any warranty that the + documentation will conform to the program, or any warranty that the software will be error free. + + In no event shall NASA be liable for any damages, including, but not limited to direct, indirect, special or + consequential damages, arising out of, resulting from, or in any way connected with the software or its + documentation, whether or not based upon warranty, contract, tort or otherwise, and whether or not loss was sustained + from, or arose out of the results of, or use of, the software, documentation or services provided hereunder. + + ITC Team + NASA IV&V + jstar-development-team@mail.nasa.gov +*/ + + +/******************************************************************************* +** Standalone CryptoLib Implementation +** UDP interfaces to apply / process each frame type and return the result. +*******************************************************************************/ + +#include "standalone.h" + +/* +** Global Variables +*/ + + +/* +** Functions +*/ +int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) +{ + int status = CRYPTO_LIB_SUCCESS; + int optval; + socklen_t optlen; + + sock->port = port; + + /* Create */ + sock->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + if(sock->sockfd == -1) + { + printf("udp_init: Socket create error port %d", sock->port); + } + + /* Bind */ + struct sockaddr_in saddr; + saddr.sin_family = AF_INET; + saddr.sin_addr.s_addr = inet_addr("0.0.0.0"); + saddr.sin_port = htons(sock->port); + status = bind(sock->sockfd, (struct sockaddr *) &saddr, sizeof(saddr)); + if (status != 0) + { + printf(" udp_init: Socker bind error with port %d", sock->port); + } + else + { + status = CRYPTO_LIB_ERROR; + } + + /* Keep Alive */ + optval = 1; + optlen = sizeof(optval); + setsockopt(sock->sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen); + + return status; +} + +int main(int argc, char* argv[]) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + int32_t run_status = CRYPTO_LIB_SUCCESS; + + //uint8_t tc_apply_in[TC_MAX_FRAME_SIZE] = {0}; + //uint8_t tc_apply_out[TC_MAX_FRAME_SIZE] = {0}; + + udp_info_t tc_apply; + + + /* Initialize */ + printf("Starting CryptoLib in standalone mode! \n"); + printf(" TC Apply - UDP %d \n", TC_APPLY_PORT); + //printf(" TC Process - UDP 76541 \n"); + //printf(" TM Apply - UDP 76542 \n"); + //printf(" TM Process - UDP 76543 \n"); + printf("\n"); + if (argc != 1) + { + printf("Invalid number of arguments! \n"); + printf(" Expected zero but received: %s \n", argv[1]); + } + + status = Crypto_Init(); + if(status != CRYPTO_LIB_SUCCESS) + { + printf("Crypto_Init failed with error %d \n", status); + run_status = CRYPTO_LIB_ERROR; + } + + status = crypto_standalone_udp_init(&tc_apply, TC_APPLY_PORT); + + + /* Loop for testing */ + while(run_status == CRYPTO_LIB_SUCCESS) + { + /* Initialize test data as proof of concept */ + + /* Only run once for testing */ + run_status = CRYPTO_LIB_ERROR; + } + + + /* Cleanup */ + close(tc_apply.port); + Crypto_Shutdown(); + printf("\n"); + return 1; +} \ No newline at end of file From ac78e10950a813d521b0cc31faa334d2b5e2f9f8 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Mon, 27 Mar 2023 16:45:39 -0400 Subject: [PATCH 02/10] [nasa-itc/cryptolib#2] Added menu to standalone; --- util/include/standalone.h | 21 ++++ util/src_util/standalone.c | 227 +++++++++++++++++++++++++++++++++---- 2 files changed, 227 insertions(+), 21 deletions(-) diff --git a/util/include/standalone.h b/util/include/standalone.h index a4933a15..0546149b 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -31,7 +31,10 @@ extern "C" #include #include #include +#include #include +#include +#include #include #include @@ -47,6 +50,17 @@ extern "C" #define TM_APPLY_PORT 76542 #define TM_PROCESS_PORT 76543 +#define CRYPTO_PROMPT "cryptolib> " +#define CRYPTO_MAX_INPUT_BUF 512 +#define CRYPTO_MAX_INPUT_TOKENS 32 +#define CRYPTO_MAX_INPUT_TOKEN_SIZE 64 + +#define CRYPTO_CMD_UNKNOWN -1 +#define CRYPTO_CMD_HELP 0 +#define CRYPTO_CMD_EXIT 1 +#define CRYPTO_CMD_NOOP 2 +#define CRYPTO_CMD_RESET 3 + /* ** Structures @@ -61,7 +75,14 @@ typedef struct /* ** Prototypes */ +int32_t crypto_standalone_check_number_arguments(int actual, int expected); +void crypto_standalone_to_lower(char* str); +void crypto_standalone_print_help(void); +int32_t crypto_standalone_get_command(const char* str); +int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens); int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port); +int32_t crypto_standalone_tc_apply(udp_info_t* tc_sock); +void crypto_standalone_cleanup(const int signal); #ifdef __cplusplus diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index ad7e288f..e58f388b 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -27,11 +27,121 @@ /* ** Global Variables */ +static volatile uint8_t keepRunning = CRYPTO_LIB_SUCCESS; /* ** Functions */ +int32_t crypto_standalone_check_number_arguments(int actual, int expected) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + if (actual != expected) + { + status = CRYPTO_LIB_ERROR; + printf("Invalid command format or number of arguments, type 'help' for more info\n"); + } + return status; +} + +void crypto_standalone_to_lower(char* str) +{ + char* ptr = str; + while(*ptr) + { + *ptr = tolower((unsigned char) *ptr); + ptr++; + } + return; +} + +void crypto_standalone_print_help(void) +{ + printf(CRYPTO_PROMPT "command [args]\n" + "---------------------------------------------------------------------\n" + "help - Display help \n" + "exit - Exit app \n" + "noop - No operation command to device \n" + "reset cryptolib - Reset CryptoLib configuration \n" + "\n" + ); +} + +int32_t crypto_standalone_get_command(const char* str) +{ + int32_t status = CRYPTO_CMD_UNKNOWN; + char lcmd[CRYPTO_MAX_INPUT_TOKEN_SIZE]; + + strncpy(lcmd, str, CRYPTO_MAX_INPUT_TOKEN_SIZE); + crypto_standalone_to_lower(lcmd); + + if(strcmp(lcmd, "help") == 0) + { + status = CRYPTO_CMD_HELP; + } + else if(strcmp(lcmd, "exit") == 0) + { + status = CRYPTO_CMD_EXIT; + } + else if(strcmp(lcmd, "noop") == 0) + { + status = CRYPTO_CMD_NOOP; + } + else if(strcmp(lcmd, "reset") == 0) + { + status = CRYPTO_CMD_RESET; + } + return status; +} + +int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + char lcmd[CRYPTO_MAX_INPUT_TOKEN_SIZE]; + + /* Process command */ + switch(cc) + { + case CRYPTO_CMD_HELP: + crypto_standalone_print_help(); + break; + + case CRYPTO_CMD_EXIT: + keepRunning = CRYPTO_LIB_ERROR; + break; + + case CRYPTO_CMD_NOOP: + if (crypto_standalone_check_number_arguments(num_tokens, 0) == CRYPTO_LIB_SUCCESS) + { + printf("NOOP command success\n"); + } + break; + + case CRYPTO_CMD_RESET: + if (crypto_standalone_check_number_arguments(num_tokens, 1) == CRYPTO_LIB_SUCCESS) + { + strncpy(lcmd, tokens, CRYPTO_MAX_INPUT_TOKEN_SIZE); + crypto_standalone_to_lower(lcmd); + if (strcmp(&tokens[0], "cryptolib") == 0) + { + printf("Reset requested and confirmed!\n"); + } + else + { + printf("Need to provide additional argument \"confirm\" to reset!\n"); + } + } + break; + + default: + printf("Invalid command format, type 'help' for more info\n"); + status = CRYPTO_LIB_ERROR; + break; + } + + return status; +} + int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) { int status = CRYPTO_LIB_SUCCESS; @@ -56,9 +166,6 @@ int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) if (status != 0) { printf(" udp_init: Socker bind error with port %d", sock->port); - } - else - { status = CRYPTO_LIB_ERROR; } @@ -70,23 +177,67 @@ int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) return status; } -int main(int argc, char* argv[]) +int32_t crypto_standalone_tc_apply(udp_info_t* tc_sock) { int32_t status = CRYPTO_LIB_SUCCESS; - int32_t run_status = CRYPTO_LIB_SUCCESS; - - //uint8_t tc_apply_in[TC_MAX_FRAME_SIZE] = {0}; + + uint8_t tc_apply_in[TC_MAX_FRAME_SIZE] = {0}; //uint8_t tc_apply_out[TC_MAX_FRAME_SIZE] = {0}; - udp_info_t tc_apply; + struct sockaddr_in rcv_addr; + int sockaddr_size = sizeof(struct sockaddr_in); + while(keepRunning == CRYPTO_LIB_SUCCESS) + { + /* Receive */ + status = recvfrom(tc_sock->sockfd, tc_apply_in, sizeof(tc_apply_in), 0, (struct sockaddr*) &rcv_addr, (socklen_t*) &sockaddr_size); + if (status != -1) + { + //bytes_recvd = status; + + /* Process */ + + /* Reply */ + } + + /* Delay */ + usleep(100); + } + return status; +} + +void crypto_standalone_cleanup(const int signal) +{ + if (signal == SIGINT) + { + printf("\n"); + printf("Received CTRL+C, cleaning up... \n"); + } + /* Signal threads to stop */ + keepRunning = CRYPTO_LIB_ERROR; + exit(signal); + return; +} + +int main(int argc, char* argv[]) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + + char input_buf[CRYPTO_MAX_INPUT_BUF]; + char input_tokens[CRYPTO_MAX_INPUT_TOKENS][CRYPTO_MAX_INPUT_TOKEN_SIZE]; + int num_input_tokens; + int cmd; + char* token_ptr; + + udp_info_t tc_apply; + //pthread_t tc_apply_thread; /* Initialize */ printf("Starting CryptoLib in standalone mode! \n"); printf(" TC Apply - UDP %d \n", TC_APPLY_PORT); - //printf(" TC Process - UDP 76541 \n"); - //printf(" TM Apply - UDP 76542 \n"); - //printf(" TM Process - UDP 76543 \n"); + //printf(" TC Process - UDP %d \n", TC_PROCESS_PORT); + //printf(" TM Apply - UDP %d \n", TM_APPLY_PORT); + //printf(" TM Process - UDP %d \n", TM_PROCESS_PORT); printf("\n"); if (argc != 1) { @@ -94,29 +245,63 @@ int main(int argc, char* argv[]) printf(" Expected zero but received: %s \n", argv[1]); } + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); status = Crypto_Init(); if(status != CRYPTO_LIB_SUCCESS) { printf("Crypto_Init failed with error %d \n", status); - run_status = CRYPTO_LIB_ERROR; + keepRunning = CRYPTO_LIB_ERROR; + } + else + { + keepRunning = crypto_standalone_udp_init(&tc_apply, TC_APPLY_PORT); + printf("crypto_standalone_udp_init returned %d \n", keepRunning); } - status = crypto_standalone_udp_init(&tc_apply, TC_APPLY_PORT); - + /* Catch CTRL+C */ + signal(SIGINT, crypto_standalone_cleanup); - /* Loop for testing */ - while(run_status == CRYPTO_LIB_SUCCESS) + /* Main loop */ + while(keepRunning == CRYPTO_LIB_SUCCESS) { - /* Initialize test data as proof of concept */ + num_input_tokens = -1; + cmd = CRYPTO_CMD_UNKNOWN; - /* Only run once for testing */ - run_status = CRYPTO_LIB_ERROR; - } + /* Read user input */ + printf(CRYPTO_PROMPT); + fgets(input_buf, CRYPTO_MAX_INPUT_BUF, stdin); + /* Tokenize line buffer */ + token_ptr = strtok(input_buf, " \t\n"); + while((num_input_tokens < CRYPTO_MAX_INPUT_TOKENS) && (token_ptr != NULL)) + { + if(num_input_tokens == -1) + { + /* First token is command */ + cmd = crypto_standalone_get_command(token_ptr); + } + else + { + strncpy(input_tokens[num_input_tokens], token_ptr, CRYPTO_MAX_INPUT_TOKEN_SIZE); + } + token_ptr = strtok(NULL, " \t\n"); + num_input_tokens++; + } + + /* Process command if valid */ + if(num_input_tokens >= 0) + { + crypto_standalone_process_command(cmd, num_input_tokens, token_ptr); + } + } /* Cleanup */ close(tc_apply.port); + Crypto_Shutdown(); + printf("\n"); - return 1; + exit(status); } \ No newline at end of file From d2ef3aa8b4fe49029ddf405e01547338745f747c Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Tue, 28 Mar 2023 11:48:19 -0400 Subject: [PATCH 03/10] [nasa-itc/cryptolib#2] Added tc_apply thread for testing; --- util/CMakeLists.txt | 4 ++- util/include/standalone.h | 4 ++- util/src_util/standalone.c | 51 ++++++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index e757ffba..09f6bdb3 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -29,6 +29,8 @@ endif(${ENCTEST}) aux_source_directory(core UTIL_SRC_FILES) aux_source_directory(src_util APP_SRC_FILES) +find_package(Threads REQUIRED) + file( GLOB SOURCE_FILES src_util/*.c ) foreach(SOURCE_PATH ${SOURCE_FILES}) get_filename_component(EXECUTABLE_NAME ${SOURCE_PATH} NAME_WE) @@ -38,7 +40,7 @@ foreach(SOURCE_PATH ${SOURCE_FILES}) else() add_executable(${EXECUTABLE_NAME} ${SOURCE_PATH}) target_sources(${EXECUTABLE_NAME} PRIVATE core/shared_util.c) - target_link_libraries(${EXECUTABLE_NAME} LINK_PUBLIC Crypto) + target_link_libraries(${EXECUTABLE_NAME} LINK_PUBLIC Crypto pthread) endif() if(${ENCTEST} AND ${EXECUTABLE_NAME} STREQUAL et_dt_validation) diff --git a/util/include/standalone.h b/util/include/standalone.h index 0546149b..835d04ae 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -61,6 +61,8 @@ extern "C" #define CRYPTO_CMD_NOOP 2 #define CRYPTO_CMD_RESET 3 +#define CRYPTO_STANDALONE_TC_APPLY_DEBUG + /* ** Structures @@ -81,7 +83,7 @@ void crypto_standalone_print_help(void); int32_t crypto_standalone_get_command(const char* str); int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens); int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port); -int32_t crypto_standalone_tc_apply(udp_info_t* tc_sock); +void* crypto_standalone_tc_apply(void* sock); void crypto_standalone_cleanup(const int signal); diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index e58f388b..dbfb6eaa 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -177,12 +177,16 @@ int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) return status; } -int32_t crypto_standalone_tc_apply(udp_info_t* tc_sock) +void* crypto_standalone_tc_apply(void* sock) { int32_t status = CRYPTO_LIB_SUCCESS; + udp_info_t* tc_sock = (udp_info_t*) sock; uint8_t tc_apply_in[TC_MAX_FRAME_SIZE] = {0}; - //uint8_t tc_apply_out[TC_MAX_FRAME_SIZE] = {0}; + int tc_in_len; + uint8_t tc_apply_out[TC_MAX_FRAME_SIZE] = {0}; + uint8_t* tc_out_ptr = tc_apply_out; + uint16_t tc_out_len; struct sockaddr_in rcv_addr; int sockaddr_size = sizeof(struct sockaddr_in); @@ -193,17 +197,46 @@ int32_t crypto_standalone_tc_apply(udp_info_t* tc_sock) status = recvfrom(tc_sock->sockfd, tc_apply_in, sizeof(tc_apply_in), 0, (struct sockaddr*) &rcv_addr, (socklen_t*) &sockaddr_size); if (status != -1) { - //bytes_recvd = status; + tc_in_len = status; + #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG + printf("crypto_standalone_tc_apply - received[%d]: 0x", tc_in_len); + for(int i = 0; i < status; i++) + { + printf("%02x", tc_apply_in[i]); + } + printf("\n"); + #endif /* Process */ + status = Crypto_TC_ApplySecurity(tc_apply_in, tc_in_len, &tc_out_ptr, &tc_out_len); + #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG + printf("crypto_standalone_tc_apply - encrypted[%d]: 0x", tc_out_len); + for(int i = 0; i < status; i++) + { + printf("%02x", tc_apply_out[i]); + } + printf("\n"); + #endif /* Reply */ + status = sendto(tc_sock->sockfd, tc_out_ptr, tc_out_len, 0, (struct sockaddr*) &rcv_addr, sizeof(rcv_addr)); + if ((status == -1) || (status != tc_out_len)) + { + printf("crypto_standalone_tc_apply - Reply error %d \n", status); + } + + /* Reset */ + memset(tc_apply_in, 0x00, sizeof(tc_apply_in)); + tc_in_len = 0; + memset(tc_apply_out, 0x00, sizeof(tc_apply_in)); + tc_out_len = 0; } /* Delay */ usleep(100); } - return status; + close(tc_sock->port); + return tc_sock; } void crypto_standalone_cleanup(const int signal) @@ -230,7 +263,7 @@ int main(int argc, char* argv[]) char* token_ptr; udp_info_t tc_apply; - //pthread_t tc_apply_thread; + pthread_t tc_apply_thread; /* Initialize */ printf("Starting CryptoLib in standalone mode! \n"); @@ -263,6 +296,14 @@ int main(int argc, char* argv[]) /* Catch CTRL+C */ signal(SIGINT, crypto_standalone_cleanup); + /* Start threads */ + status = pthread_create(&tc_apply_thread, NULL, *crypto_standalone_tc_apply, &tc_apply); + if (status < 0) + { + perror("Failed to create read thread"); + return status; + } + /* Main loop */ while(keepRunning == CRYPTO_LIB_SUCCESS) { From 23ae73172f9cf02dd1f14b034b1fcc29355ec754 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Thu, 30 Mar 2023 09:09:49 -0400 Subject: [PATCH 04/10] [nasa-itc/cryptolib#2] Setup forwarding addresses and prepare TM process thread; --- util/include/standalone.h | 22 ++++++--- util/src_util/standalone.c | 96 ++++++++++++++++++++++++++------------ 2 files changed, 82 insertions(+), 36 deletions(-) diff --git a/util/include/standalone.h b/util/include/standalone.h index 835d04ae..cf026fbc 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -43,13 +43,22 @@ extern "C" /* -** Defines +** Configuration */ #define TC_APPLY_PORT 76540 -#define TC_PROCESS_PORT 76541 -#define TM_APPLY_PORT 76542 -#define TM_PROCESS_PORT 76543 +#define TC_APPLY_FWD_PORT 5010 +#define TM_PROCESS_PORT 5011 +#define TM_PROCESS_FWD_PORT 76541 + +#define CRYPTO_STANDALONE_TC_APPLY_DEBUG +#define CRYPTO_STANDALONE_TM_PROCESS_DEBUG + +#define CRYPTO_STANDALONE_HANDLE_FRAMING + +/* +** Defines +*/ #define CRYPTO_PROMPT "cryptolib> " #define CRYPTO_MAX_INPUT_BUF 512 #define CRYPTO_MAX_INPUT_TOKENS 32 @@ -61,8 +70,6 @@ extern "C" #define CRYPTO_CMD_NOOP 2 #define CRYPTO_CMD_RESET 3 -#define CRYPTO_STANDALONE_TC_APPLY_DEBUG - /* ** Structures @@ -81,9 +88,10 @@ int32_t crypto_standalone_check_number_arguments(int actual, int expected); void crypto_standalone_to_lower(char* str); void crypto_standalone_print_help(void); int32_t crypto_standalone_get_command(const char* str); -int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens); +int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens); //, char* tokens); int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port); void* crypto_standalone_tc_apply(void* sock); +void* crypto_standalone_tm_process(void* sock); void crypto_standalone_cleanup(const int signal); diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index dbfb6eaa..173786cc 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -62,7 +62,7 @@ void crypto_standalone_print_help(void) "help - Display help \n" "exit - Exit app \n" "noop - No operation command to device \n" - "reset cryptolib - Reset CryptoLib configuration \n" + "reset - Reset CryptoLib \n" "\n" ); } @@ -94,10 +94,9 @@ int32_t crypto_standalone_get_command(const char* str) return status; } -int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens) +int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens) //, char* tokens) { int32_t status = CRYPTO_LIB_SUCCESS; - char lcmd[CRYPTO_MAX_INPUT_TOKEN_SIZE]; /* Process command */ switch(cc) @@ -120,16 +119,7 @@ int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* case CRYPTO_CMD_RESET: if (crypto_standalone_check_number_arguments(num_tokens, 1) == CRYPTO_LIB_SUCCESS) { - strncpy(lcmd, tokens, CRYPTO_MAX_INPUT_TOKEN_SIZE); - crypto_standalone_to_lower(lcmd); - if (strcmp(&tokens[0], "cryptolib") == 0) - { - printf("Reset requested and confirmed!\n"); - } - else - { - printf("Need to provide additional argument \"confirm\" to reset!\n"); - } + printf("Reset command received\n"); } break; @@ -189,8 +179,13 @@ void* crypto_standalone_tc_apply(void* sock) uint16_t tc_out_len; struct sockaddr_in rcv_addr; + struct sockaddr_in fwd_addr; int sockaddr_size = sizeof(struct sockaddr_in); + fwd_addr.sin_family = AF_INET; + fwd_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); + fwd_addr.sin_port = htons(TC_APPLY_FWD_PORT); + while(keepRunning == CRYPTO_LIB_SUCCESS) { /* Receive */ @@ -219,7 +214,7 @@ void* crypto_standalone_tc_apply(void* sock) #endif /* Reply */ - status = sendto(tc_sock->sockfd, tc_out_ptr, tc_out_len, 0, (struct sockaddr*) &rcv_addr, sizeof(rcv_addr)); + status = sendto(tc_sock->sockfd, tc_out_ptr, tc_out_len, 0, (struct sockaddr*) &fwd_addr, sizeof(fwd_addr)); if ((status == -1) || (status != tc_out_len)) { printf("crypto_standalone_tc_apply - Reply error %d \n", status); @@ -239,6 +234,21 @@ void* crypto_standalone_tc_apply(void* sock) return tc_sock; } +void* crypto_standalone_tm_process(void* sock) +{ + udp_info_t* tm_sock = (udp_info_t*) sock; + + while(keepRunning == CRYPTO_LIB_SUCCESS) + { + /* Do nothing for now */ + + /* Delay */ + usleep(100); + } + close(tm_sock->port); + return tm_sock; +} + void crypto_standalone_cleanup(const int signal) { if (signal == SIGINT) @@ -263,14 +273,14 @@ int main(int argc, char* argv[]) char* token_ptr; udp_info_t tc_apply; + udp_info_t tm_process; pthread_t tc_apply_thread; + pthread_t tm_process_thread; - /* Initialize */ + printf("Starting CryptoLib in standalone mode! \n"); printf(" TC Apply - UDP %d \n", TC_APPLY_PORT); - //printf(" TC Process - UDP %d \n", TC_PROCESS_PORT); - //printf(" TM Apply - UDP %d \n", TM_APPLY_PORT); - //printf(" TM Process - UDP %d \n", TM_PROCESS_PORT); + printf(" TM Process - UDP %d \n", TM_PROCESS_PORT); printf("\n"); if (argc != 1) { @@ -278,6 +288,7 @@ int main(int argc, char* argv[]) printf(" Expected zero but received: %s \n", argv[1]); } + /* Initialize CryptoLib */ Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); @@ -287,25 +298,52 @@ int main(int argc, char* argv[]) printf("Crypto_Init failed with error %d \n", status); keepRunning = CRYPTO_LIB_ERROR; } - else + + /* Initialize sockets */ + if (keepRunning == CRYPTO_LIB_SUCCESS) { - keepRunning = crypto_standalone_udp_init(&tc_apply, TC_APPLY_PORT); - printf("crypto_standalone_udp_init returned %d \n", keepRunning); + status = crypto_standalone_udp_init(&tc_apply, TC_APPLY_PORT); + if (status != CRYPTO_LIB_SUCCESS) + { + printf("crypto_standalone_udp_init tc_apply failed with status %d \n", status); + keepRunning = CRYPTO_LIB_ERROR; + } + else + { + status = crypto_standalone_udp_init(&tm_process, TM_PROCESS_PORT); + if (status != CRYPTO_LIB_SUCCESS) + { + printf("crypto_standalone_udp_init tm_process failed with status %d \n", status); + keepRunning = CRYPTO_LIB_ERROR; + } + } } /* Catch CTRL+C */ signal(SIGINT, crypto_standalone_cleanup); /* Start threads */ - status = pthread_create(&tc_apply_thread, NULL, *crypto_standalone_tc_apply, &tc_apply); - if (status < 0) + if (keepRunning == CRYPTO_LIB_SUCCESS) { - perror("Failed to create read thread"); - return status; + status = pthread_create(&tc_apply_thread, NULL, *crypto_standalone_tc_apply, &tc_apply); + if (status < 0) + { + perror("Failed to create tc_apply_thread thread"); + keepRunning = CRYPTO_LIB_ERROR; + } + else + { + status = pthread_create(&tm_process_thread, NULL, *crypto_standalone_tm_process, &tm_process); + if (status < 0) + { + perror("Failed to create tm_process_thread thread"); + keepRunning = CRYPTO_LIB_ERROR; + } + } } /* Main loop */ - while(keepRunning == CRYPTO_LIB_SUCCESS) + while (keepRunning == CRYPTO_LIB_SUCCESS) { num_input_tokens = -1; cmd = CRYPTO_CMD_UNKNOWN; @@ -316,9 +354,9 @@ int main(int argc, char* argv[]) /* Tokenize line buffer */ token_ptr = strtok(input_buf, " \t\n"); - while((num_input_tokens < CRYPTO_MAX_INPUT_TOKENS) && (token_ptr != NULL)) + while ((num_input_tokens < CRYPTO_MAX_INPUT_TOKENS) && (token_ptr != NULL)) { - if(num_input_tokens == -1) + if (num_input_tokens == -1) { /* First token is command */ cmd = crypto_standalone_get_command(token_ptr); @@ -334,7 +372,7 @@ int main(int argc, char* argv[]) /* Process command if valid */ if(num_input_tokens >= 0) { - crypto_standalone_process_command(cmd, num_input_tokens, token_ptr); + crypto_standalone_process_command(cmd, num_input_tokens); } } From 86a3f8e2e6d168c9a5c9411cacade54325f01297 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Thu, 30 Mar 2023 14:33:21 -0400 Subject: [PATCH 05/10] [nasa-itc/cryptolib#2] Update Cmake CFE checks; --- CMakeLists.txt | 15 +++++++++------ src/CMakeLists.txt | 20 ++++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb60312f..777fc9c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,8 +29,11 @@ OPTION(ENCTEST "Encryption-Tests" OFF) # Disabled by default, enable with: -DENC OPTION(CODECOV "Code-Coverage" OFF) # Disabled by default, enable with: -DCODECOV=ON OPTION(SYSTEM_INSTALL "SystemInstall" OFF) #Disabled by default, enable with: -DSYSTEM_INSTALL=ON -set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") -set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install) +IF(NOT DEFINED CFE_SYSTEM_PSPNAME) + # Not cFE / cFS + set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") + set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install) +ENDIF() IF(CRYPTO_SUBMODULE_INSTALL) #If building CryptoLib as a submodule of another build system (EG, JPL KMC, Nasa NOS3, etc...) set(CMAKE_INSTALL_PREFIX ${CRYPTO_SUBMODULE_INSTALL}) @@ -66,12 +69,10 @@ include_directories(include) # The shared OSAL and cFE include directories should always be used # Note that this intentionally does NOT include PSP-specific includes, just the generic # Only include cFS/NOS3 directories if env var is defined -if(DEFINED ENV{CFECORE_SOURCE_DIR}) #if ${CFECORE_SOURCE_DIR} is set, expect cFS build infrastructure to be in place. +IF(DEFINED CFE_SYSTEM_PSPNAME) include_directories(${CFECORE_SOURCE_DIR}/src/inc) include_directories(${CFEPSP_SOURCE_DIR}/fsw/inc) ADD_DEFINITIONS(-DNOS3) -else() - #pass endif() if(NOT DEFINED ${PROJECT_BINARY_DIR}) @@ -84,4 +85,6 @@ if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MYPROJECT_BUILD_TESTING) AND BUI add_subdirectory(test) endif() -add_subdirectory(util) +IF(NOT DEFINED CFE_SYSTEM_PSPNAME) + add_subdirectory(util) +ENDIF() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b8b77fc4..f1044340 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,11 +48,13 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) endif() # Create the app module -if(DEFINED ENV{CFECORE_SOURCE_DIR}) #if ${CFECORE_SOURCE_DIR} is set, expect cFS build infrastructure to be in place. +IF(DEFINED CFE_SYSTEM_PSPNAME) + set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/cpu${TGTSYS_${SYSVAR}}/${INSTALL_SUBDIR}") add_cfe_app(Crypto ${LIB_SRC_FILES}) -else() #standalone build +ELSE() + # Standalone build add_library(Crypto SHARED ${LIB_SRC_FILES}) -endif() +ENDIF() if(LIBGCRYPT) target_link_libraries(Crypto gcrypt) @@ -79,9 +81,15 @@ add_custom_command(TARGET Crypto POST_BUILD COMMENT "Created ${PROJECT_BINARY_DIR}/lib/libCrypto.so" ) -install(TARGETS Crypto - DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include) + +IF(DEFINED CFE_SYSTEM_PSPNAME) + install(TARGETS Crypto + DESTINATION ${CMAKE_INSTALL_PREFIX}/cpu${TGTSYS_${SYSVAR}}/${INSTALL_SUBDIR}) +ELSE() + install(TARGETS Crypto + DESTINATION ${CMAKE_INSTALL_PREFIX}/lib + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include) +ENDIF() IF(MYSQL) file(GLOB MYSQL_SCRIPTS crypto_sadb/sadb_mariadb_sql/*.sql) From 1442f098a73d33811a296574623578b7cc24e043 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Wed, 5 Apr 2023 13:25:59 -0400 Subject: [PATCH 06/10] [nasa-itc/cryptolib#2] Added initial tc_frame function; --- util/include/standalone.h | 11 ++++++---- util/src_util/standalone.c | 44 +++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/util/include/standalone.h b/util/include/standalone.h index cf026fbc..ae2da71b 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -45,15 +45,18 @@ extern "C" /* ** Configuration */ -#define TC_APPLY_PORT 76540 -#define TC_APPLY_FWD_PORT 5010 -#define TM_PROCESS_PORT 5011 -#define TM_PROCESS_FWD_PORT 76541 +#define TC_APPLY_PORT 6010 +#define TC_APPLY_FWD_PORT 8010 +#define TM_PROCESS_PORT 8011 +#define TM_PROCESS_FWD_PORT 6011 #define CRYPTO_STANDALONE_TC_APPLY_DEBUG #define CRYPTO_STANDALONE_TM_PROCESS_DEBUG #define CRYPTO_STANDALONE_HANDLE_FRAMING +#define CRYPTO_STANDALONE_FRAMING_SCID 0x42 +#define CRYPTO_STANDALONE_FRAMING_VCID 0x00 +#define CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN 256 /* diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index 173786cc..a65c3418 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -28,6 +28,7 @@ ** Global Variables */ static volatile uint8_t keepRunning = CRYPTO_LIB_SUCCESS; +static volatile uint8_t tc_seq_num = 0; /* @@ -167,6 +168,25 @@ int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) return status; } +void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length) +{ + /* Zero Frame */ + memset(out_data, 0x00, *out_length); + + /* TC Length */ + *out_length = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN + 5; + + /* TC Header */ + out_data[0] = 0x20; + out_data[1] = CRYPTO_STANDALONE_FRAMING_SCID; + out_data[2] = (CRYPTO_STANDALONE_FRAMING_VCID && 0xFC) || (((uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN >> 8) && 0x03); + out_data[3] = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN && 0x00FF; + out_data[4] = tc_seq_num++; + + /* TC Data */ + memcpy(&out_data[5], in_data, in_length); +} + void* crypto_standalone_tc_apply(void* sock) { int32_t status = CRYPTO_LIB_SUCCESS; @@ -176,7 +196,7 @@ void* crypto_standalone_tc_apply(void* sock) int tc_in_len; uint8_t tc_apply_out[TC_MAX_FRAME_SIZE] = {0}; uint8_t* tc_out_ptr = tc_apply_out; - uint16_t tc_out_len; + uint16_t tc_out_len = TC_MAX_FRAME_SIZE; struct sockaddr_in rcv_addr; struct sockaddr_in fwd_addr; @@ -202,11 +222,24 @@ void* crypto_standalone_tc_apply(void* sock) printf("\n"); #endif + /* Frame */ + #ifdef CRYPTO_STANDALONE_HANDLE_FRAMING + crypto_standalone_tc_frame(tc_apply_in, tc_in_len, tc_apply_out, &tc_out_len); + memcpy(tc_apply_in, tc_apply_out, tc_out_len); + tc_in_len = tc_out_len; + printf("crypto_standalone_tc_apply - framed[%d]: 0x", tc_in_len); + for(int i = 0; i < tc_in_len; i++) + { + printf("%02x", tc_apply_in[i]); + } + printf("\n"); + #endif + /* Process */ status = Crypto_TC_ApplySecurity(tc_apply_in, tc_in_len, &tc_out_ptr, &tc_out_len); #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG - printf("crypto_standalone_tc_apply - encrypted[%d]: 0x", tc_out_len); - for(int i = 0; i < status; i++) + printf("crypto_standalone_tc_apply - status = %d, encrypted[%d]: 0x", status, tc_out_len); + for(int i = 0; i < tc_out_len; i++) { printf("%02x", tc_apply_out[i]); } @@ -290,8 +323,9 @@ int main(int argc, char* argv[]) /* Initialize CryptoLib */ Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); - Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); - Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0042, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0042, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + // TODO: CryptoLib appears to be looking at the second byte and not specficially the SCID bits status = Crypto_Init(); if(status != CRYPTO_LIB_SUCCESS) { From 30e9e99f167ca567d96466ecb7334d01f4ba2f88 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Thu, 6 Apr 2023 16:44:46 -0400 Subject: [PATCH 07/10] [nasa-itc/cryptolib#2] TC standalone clear mode functional; --- include/crypto_config.h | 2 + ...ryptography_interface_libgcrypt.template.c | 8 +-- util/include/standalone.h | 2 +- util/src_util/standalone.c | 50 ++++++++++++------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/include/crypto_config.h b/include/crypto_config.h index 2eb46c4f..8c6a8dd8 100644 --- a/include/crypto_config.h +++ b/include/crypto_config.h @@ -35,6 +35,7 @@ // Debug Colors #ifdef DEBUG +#define CRYPTO_DEBUG printf("%s:%s: %d", __FILE__, __FUNCTION__, __LINE__); #define KRED "\x1B[31m" #define KGRN "\x1B[32m" #define KYEL "\x1B[33m" @@ -43,6 +44,7 @@ #define KCYN "\x1B[36m" #define RESET "\033[0m" #else +#define CRYPTO_DEBUG #define KRED #define RED #define KGRN diff --git a/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c b/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c index c5205b96..2c87b4ce 100644 --- a/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c +++ b/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c @@ -923,7 +923,7 @@ static int32_t cryptography_encrypt(uint8_t* data_out, size_t len_data_out, #ifdef TC_DEBUG size_t j; - printf("Input payload length is %ld\n", len_data_in); + printf("Input payload length is %ld\n", (long int) len_data_in); printf(KYEL "Printing Frame Data prior to encryption:\n\t"); for (j = 0; j < len_data_in; j++) { @@ -954,7 +954,7 @@ static int32_t cryptography_encrypt(uint8_t* data_out, size_t len_data_out, } #ifdef TC_DEBUG - printf("Output payload length is %ld\n", len_data_out); + printf("Output payload length is %ld\n", (long int) len_data_out); printf(KYEL "Printing TC Frame Data after encryption:\n\t"); for (j = 0; j < len_data_out; j++) { @@ -1056,7 +1056,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, #ifdef TC_DEBUG size_t j; - printf("Input payload length is %ld\n", len_data_in); + printf("Input payload length is %ld\n", (long int) len_data_in); printf(KYEL "Printing Frame Data prior to encryption:\n\t"); for (j = 0; j < len_data_in; j++) { @@ -1113,7 +1113,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, } #ifdef TC_DEBUG - printf("Output payload length is %ld\n", len_data_out); + printf("Output payload length is %ld\n", (long int) len_data_out); printf(KYEL "Printing TC Frame Data after encryption:\n\t"); for (j = 0; j < len_data_out; j++) { diff --git a/util/include/standalone.h b/util/include/standalone.h index ae2da71b..0b0c618f 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -54,7 +54,7 @@ extern "C" #define CRYPTO_STANDALONE_TM_PROCESS_DEBUG #define CRYPTO_STANDALONE_HANDLE_FRAMING -#define CRYPTO_STANDALONE_FRAMING_SCID 0x42 +#define CRYPTO_STANDALONE_FRAMING_SCID 3 #define CRYPTO_STANDALONE_FRAMING_VCID 0x00 #define CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN 256 diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index a65c3418..cf3315f1 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -174,7 +174,7 @@ void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* o memset(out_data, 0x00, *out_length); /* TC Length */ - *out_length = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN + 5; + *out_length = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN + 6; /* TC Header */ out_data[0] = 0x20; @@ -183,8 +183,17 @@ void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* o out_data[3] = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN && 0x00FF; out_data[4] = tc_seq_num++; + /* Segement Header */ + out_data[5] = 0x00; + + /* SDLS Header */ + + /* TC Data */ - memcpy(&out_data[5], in_data, in_length); + memcpy(&out_data[6], in_data, in_length); + + /* SDLS Trailer */ + } void* crypto_standalone_tc_apply(void* sock) @@ -237,20 +246,27 @@ void* crypto_standalone_tc_apply(void* sock) /* Process */ status = Crypto_TC_ApplySecurity(tc_apply_in, tc_in_len, &tc_out_ptr, &tc_out_len); - #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG - printf("crypto_standalone_tc_apply - status = %d, encrypted[%d]: 0x", status, tc_out_len); - for(int i = 0; i < tc_out_len; i++) + if (status == CRYPTO_LIB_SUCCESS) + { + #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG + printf("crypto_standalone_tc_apply - status = %d, encrypted[%d]: 0x", status, tc_out_len); + for(int i = 0; i < tc_out_len; i++) + { + printf("%02x", tc_apply_out[i]); + } + printf("\n"); + #endif + + /* Reply */ + status = sendto(tc_sock->sockfd, tc_out_ptr, tc_out_len, 0, (struct sockaddr*) &fwd_addr, sizeof(fwd_addr)); + if ((status == -1) || (status != tc_out_len)) { - printf("%02x", tc_apply_out[i]); + printf("crypto_standalone_tc_apply - Reply error %d \n", status); } - printf("\n"); - #endif - - /* Reply */ - status = sendto(tc_sock->sockfd, tc_out_ptr, tc_out_len, 0, (struct sockaddr*) &fwd_addr, sizeof(fwd_addr)); - if ((status == -1) || (status != tc_out_len)) + } + else { - printf("crypto_standalone_tc_apply - Reply error %d \n", status); + printf("crypto_standalone_tc_apply - AppySecurity error %d \n", status); } /* Reset */ @@ -310,7 +326,6 @@ int main(int argc, char* argv[]) pthread_t tc_apply_thread; pthread_t tm_process_thread; - printf("Starting CryptoLib in standalone mode! \n"); printf(" TC Apply - UDP %d \n", TC_APPLY_PORT); printf(" TM Process - UDP %d \n", TM_PROCESS_PORT); @@ -322,11 +337,8 @@ int main(int argc, char* argv[]) } /* Initialize CryptoLib */ - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); - Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0042, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); - Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0042, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); - // TODO: CryptoLib appears to be looking at the second byte and not specficially the SCID bits - status = Crypto_Init(); + status = Crypto_Init_Unit_Test(); + // TODO: CryptoLib appears to be looking at the second byte and not specficially the SCID bits if(status != CRYPTO_LIB_SUCCESS) { printf("Crypto_Init failed with error %d \n", status); From e905a39b949f0eb52875d399b352c9cc9863ba12 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Tue, 11 Apr 2023 07:32:44 -0400 Subject: [PATCH 08/10] [nasa-itc/cryptolib#2] Updated VCID 4 to be operational and AES-GCM-256, this does break a unit test that will need to be fixed; --- src/src_main/crypto_config.c | 1 + src/src_main/sadb_routine_inmemory.template.c | 4 +- util/include/standalone.h | 5 +- util/src_util/standalone.c | 92 ++++++++++++++----- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/src/src_main/crypto_config.c b/src/src_main/crypto_config.c index 0eea91a3..54bc15f8 100644 --- a/src/src_main/crypto_config.c +++ b/src/src_main/crypto_config.c @@ -53,6 +53,7 @@ int32_t Crypto_Init_Unit_Test(void) TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 4, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); status = Crypto_Init(); return status; } diff --git a/src/src_main/sadb_routine_inmemory.template.c b/src/src_main/sadb_routine_inmemory.template.c index 59bc86cd..eb87688a 100644 --- a/src/src_main/sadb_routine_inmemory.template.c +++ b/src/src_main/sadb_routine_inmemory.template.c @@ -132,7 +132,7 @@ int32_t sadb_config(void) // SA 4 VC0/1 is now 4-VC0, 7-VC1 sa[4].spi = 4; sa[4].ekid = 130; - sa[4].sa_state = SA_KEYED; + sa[4].sa_state = SA_OPERATIONAL; sa[4].est = 1; sa[4].ast = 1; sa[4].ecs_len = 1; @@ -150,7 +150,7 @@ int32_t sadb_config(void) sa[4].arsn_len = 0; sa[4].gvcid_tc_blk.tfvn = 0; sa[4].gvcid_tc_blk.scid = SCID & 0x3FF; - sa[4].gvcid_tc_blk.vcid = 0; + sa[4].gvcid_tc_blk.vcid = 4; sa[4].gvcid_tc_blk.mapid = TYPE_TC; // SA 5 - KEYED; ARSNW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 131 diff --git a/util/include/standalone.h b/util/include/standalone.h index 0b0c618f..b0bcb3d9 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -72,6 +72,7 @@ extern "C" #define CRYPTO_CMD_EXIT 1 #define CRYPTO_CMD_NOOP 2 #define CRYPTO_CMD_RESET 3 +#define CRYPTO_CMD_VCID 4 /* @@ -91,8 +92,10 @@ int32_t crypto_standalone_check_number_arguments(int actual, int expected); void crypto_standalone_to_lower(char* str); void crypto_standalone_print_help(void); int32_t crypto_standalone_get_command(const char* str); -int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens); //, char* tokens); +int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens); int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port); +int32_t crypto_reset(void); +void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length); void* crypto_standalone_tc_apply(void* sock); void* crypto_standalone_tm_process(void* sock); void crypto_standalone_cleanup(const int signal); diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index cf3315f1..39cdc7bd 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -29,6 +29,7 @@ */ static volatile uint8_t keepRunning = CRYPTO_LIB_SUCCESS; static volatile uint8_t tc_seq_num = 0; +static volatile uint8_t tc_vcid = CRYPTO_STANDALONE_FRAMING_VCID; /* @@ -59,11 +60,12 @@ void crypto_standalone_to_lower(char* str) void crypto_standalone_print_help(void) { printf(CRYPTO_PROMPT "command [args]\n" - "---------------------------------------------------------------------\n" - "help - Display help \n" - "exit - Exit app \n" - "noop - No operation command to device \n" - "reset - Reset CryptoLib \n" + "----------------------------------------------------------------------\n" + "help - Display help \n" + "exit - Exit app \n" + "noop - No operation command to device \n" + "reset - Reset CryptoLib \n" + "vcid # - Change active TC virtual channel \n" "\n" ); } @@ -92,10 +94,14 @@ int32_t crypto_standalone_get_command(const char* str) { status = CRYPTO_CMD_RESET; } + else if(strcmp(lcmd, "vcid") == 0) + { + status = CRYPTO_CMD_VCID; + } return status; } -int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens) //, char* tokens) +int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens, char* tokens) { int32_t status = CRYPTO_LIB_SUCCESS; @@ -118,11 +124,20 @@ int32_t crypto_standalone_process_command(int32_t cc, int32_t num_tokens) //, ch break; case CRYPTO_CMD_RESET: - if (crypto_standalone_check_number_arguments(num_tokens, 1) == CRYPTO_LIB_SUCCESS) + if (crypto_standalone_check_number_arguments(num_tokens, 0) == CRYPTO_LIB_SUCCESS) { + status = crypto_reset(); printf("Reset command received\n"); } break; + + case CRYPTO_CMD_VCID: + if (crypto_standalone_check_number_arguments(num_tokens, 1) == CRYPTO_LIB_SUCCESS) + { + tc_vcid = (uint8_t) atoi(&tokens[0]); + printf("Changed active virtual channel (VCID) to %d \n", tc_vcid); + } + break; default: printf("Invalid command format, type 'help' for more info\n"); @@ -168,6 +183,26 @@ int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port) return status; } +int32_t crypto_reset(void) +{ + int32_t status; + + status = Crypto_Shutdown(); + if(status != CRYPTO_LIB_SUCCESS) + { + printf("CryptoLib initialization failed with error %d \n", status); + } + + status = Crypto_Init_Unit_Test(); + // TODO: CryptoLib appears to be looking at the second byte and not specficially the SCID bits + if(status != CRYPTO_LIB_SUCCESS) + { + printf("CryptoLib initialization failed with error %d \n", status); + } + + return status; +} + void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length) { /* Zero Frame */ @@ -179,8 +214,8 @@ void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* o /* TC Header */ out_data[0] = 0x20; out_data[1] = CRYPTO_STANDALONE_FRAMING_SCID; - out_data[2] = (CRYPTO_STANDALONE_FRAMING_VCID && 0xFC) || (((uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN >> 8) && 0x03); - out_data[3] = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN && 0x00FF; + out_data[2] = ((tc_vcid << 2) & 0xFC) | (((uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN >> 8) & 0x03); + out_data[3] = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN & 0x00FF; out_data[4] = tc_seq_num++; /* Segement Header */ @@ -201,11 +236,14 @@ void* crypto_standalone_tc_apply(void* sock) int32_t status = CRYPTO_LIB_SUCCESS; udp_info_t* tc_sock = (udp_info_t*) sock; - uint8_t tc_apply_in[TC_MAX_FRAME_SIZE] = {0}; - int tc_in_len; - uint8_t tc_apply_out[TC_MAX_FRAME_SIZE] = {0}; - uint8_t* tc_out_ptr = tc_apply_out; - uint16_t tc_out_len = TC_MAX_FRAME_SIZE; + uint8_t tc_apply_in[TC_MAX_FRAME_SIZE]; + uint16_t tc_in_len = 0; + uint8_t* tc_out_ptr; + uint16_t tc_out_len = 0; + + #ifdef CRYPTO_STANDALONE_HANDLE_FRAMING + uint8_t tc_framed[TC_MAX_FRAME_SIZE]; + #endif struct sockaddr_in rcv_addr; struct sockaddr_in fwd_addr; @@ -215,6 +253,9 @@ void* crypto_standalone_tc_apply(void* sock) fwd_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); fwd_addr.sin_port = htons(TC_APPLY_FWD_PORT); + /* Prepare */ + memset(tc_apply_in, 0x00, sizeof(tc_apply_in)); + while(keepRunning == CRYPTO_LIB_SUCCESS) { /* Receive */ @@ -233,9 +274,10 @@ void* crypto_standalone_tc_apply(void* sock) /* Frame */ #ifdef CRYPTO_STANDALONE_HANDLE_FRAMING - crypto_standalone_tc_frame(tc_apply_in, tc_in_len, tc_apply_out, &tc_out_len); - memcpy(tc_apply_in, tc_apply_out, tc_out_len); + crypto_standalone_tc_frame(tc_apply_in, tc_in_len, tc_framed, &tc_out_len); + memcpy(tc_apply_in, tc_framed, tc_out_len); tc_in_len = tc_out_len; + tc_out_len = 0; printf("crypto_standalone_tc_apply - framed[%d]: 0x", tc_in_len); for(int i = 0; i < tc_in_len; i++) { @@ -252,7 +294,7 @@ void* crypto_standalone_tc_apply(void* sock) printf("crypto_standalone_tc_apply - status = %d, encrypted[%d]: 0x", status, tc_out_len); for(int i = 0; i < tc_out_len; i++) { - printf("%02x", tc_apply_out[i]); + printf("%02x", tc_out_ptr[i]); } printf("\n"); #endif @@ -269,11 +311,11 @@ void* crypto_standalone_tc_apply(void* sock) printf("crypto_standalone_tc_apply - AppySecurity error %d \n", status); } - /* Reset */ - memset(tc_apply_in, 0x00, sizeof(tc_apply_in)); + /* Reset */ + memset(tc_apply_in, 0x00, sizeof(tc_apply_in)); tc_in_len = 0; - memset(tc_apply_out, 0x00, sizeof(tc_apply_in)); tc_out_len = 0; + free(tc_out_ptr); } /* Delay */ @@ -337,11 +379,10 @@ int main(int argc, char* argv[]) } /* Initialize CryptoLib */ - status = Crypto_Init_Unit_Test(); - // TODO: CryptoLib appears to be looking at the second byte and not specficially the SCID bits + status = crypto_reset(); if(status != CRYPTO_LIB_SUCCESS) { - printf("Crypto_Init failed with error %d \n", status); + printf("CryptoLib initialization failed with error %d \n", status); keepRunning = CRYPTO_LIB_ERROR; } @@ -406,10 +447,12 @@ int main(int argc, char* argv[]) { /* First token is command */ cmd = crypto_standalone_get_command(token_ptr); + //printf("CMD = %s %d\n",token_ptr,cmd); } else { strncpy(input_tokens[num_input_tokens], token_ptr, CRYPTO_MAX_INPUT_TOKEN_SIZE); + //printf("Token[%d] = %s\n",num_input_tokens,token_ptr); } token_ptr = strtok(NULL, " \t\n"); num_input_tokens++; @@ -418,12 +461,13 @@ int main(int argc, char* argv[]) /* Process command if valid */ if(num_input_tokens >= 0) { - crypto_standalone_process_command(cmd, num_input_tokens); + crypto_standalone_process_command(cmd, num_input_tokens, &input_tokens[0][0]); } } /* Cleanup */ close(tc_apply.port); + close(tm_process.port); Crypto_Shutdown(); From 22328236209be91e782efbba535e0d6002f74bd9 Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Tue, 11 Apr 2023 16:11:37 -0400 Subject: [PATCH 09/10] [nasa-itc/cryptolib#2] TM support added, although TM_ProcessSecurity is currently a pass through; --- util/include/standalone.h | 5 +- util/src_util/standalone.c | 140 +++++++++++++++++++++++++++++++++---- 2 files changed, 131 insertions(+), 14 deletions(-) diff --git a/util/include/standalone.h b/util/include/standalone.h index b0bcb3d9..9bc7924e 100644 --- a/util/include/standalone.h +++ b/util/include/standalone.h @@ -50,8 +50,8 @@ extern "C" #define TM_PROCESS_PORT 8011 #define TM_PROCESS_FWD_PORT 6011 -#define CRYPTO_STANDALONE_TC_APPLY_DEBUG -#define CRYPTO_STANDALONE_TM_PROCESS_DEBUG +//#define CRYPTO_STANDALONE_TC_APPLY_DEBUG +//#define CRYPTO_STANDALONE_TM_PROCESS_DEBUG #define CRYPTO_STANDALONE_HANDLE_FRAMING #define CRYPTO_STANDALONE_FRAMING_SCID 3 @@ -97,6 +97,7 @@ int32_t crypto_standalone_udp_init(udp_info_t* sock, int32_t port); int32_t crypto_reset(void); void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length); void* crypto_standalone_tc_apply(void* sock); +void crypto_standalone_tm_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length); void* crypto_standalone_tm_process(void* sock); void crypto_standalone_cleanup(const int signal); diff --git a/util/src_util/standalone.c b/util/src_util/standalone.c index 39cdc7bd..b04ed34c 100644 --- a/util/src_util/standalone.c +++ b/util/src_util/standalone.c @@ -205,9 +205,6 @@ int32_t crypto_reset(void) void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length) { - /* Zero Frame */ - memset(out_data, 0x00, *out_length); - /* TC Length */ *out_length = (uint16_t) CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN + 6; @@ -223,12 +220,10 @@ void crypto_standalone_tc_frame(uint8_t* in_data, uint16_t in_length, uint8_t* o /* SDLS Header */ - /* TC Data */ memcpy(&out_data[6], in_data, in_length); /* SDLS Trailer */ - } void* crypto_standalone_tc_apply(void* sock) @@ -278,12 +273,14 @@ void* crypto_standalone_tc_apply(void* sock) memcpy(tc_apply_in, tc_framed, tc_out_len); tc_in_len = tc_out_len; tc_out_len = 0; - printf("crypto_standalone_tc_apply - framed[%d]: 0x", tc_in_len); - for(int i = 0; i < tc_in_len; i++) - { - printf("%02x", tc_apply_in[i]); - } - printf("\n"); + #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG + printf("crypto_standalone_tc_apply - framed[%d]: 0x", tc_in_len); + for(int i = 0; i < tc_in_len; i++) + { + printf("%02x", tc_apply_in[i]); + } + printf("\n"); + #endif #endif /* Process */ @@ -316,6 +313,9 @@ void* crypto_standalone_tc_apply(void* sock) tc_in_len = 0; tc_out_len = 0; free(tc_out_ptr); + #ifdef CRYPTO_STANDALONE_TC_APPLY_DEBUG + printf("\n"); + #endif } /* Delay */ @@ -325,13 +325,129 @@ void* crypto_standalone_tc_apply(void* sock) return tc_sock; } +void crypto_standalone_tm_frame(uint8_t* in_data, uint16_t in_length, uint8_t* out_data, uint16_t* out_length) +{ + /* TM Length */ + *out_length = (uint16_t) in_length - 10; + + /* TM Header */ + memcpy(out_data, &in_data[10], in_length - 10); +} + void* crypto_standalone_tm_process(void* sock) { + int32_t status = CRYPTO_LIB_SUCCESS; udp_info_t* tm_sock = (udp_info_t*) sock; + uint8_t tm_process_in[TM_FRAME_DATA_SIZE]; + int tm_process_len = 0; + uint16_t spp_len = 0; + uint8_t* tm_ptr; + + #ifdef CRYPTO_STANDALONE_HANDLE_FRAMING + uint8_t tm_framed[TM_FRAME_DATA_SIZE]; + uint16_t tm_framed_len = 0; + #endif + + struct sockaddr_in rcv_addr; + struct sockaddr_in fwd_addr; + int sockaddr_size = sizeof(struct sockaddr_in); + + fwd_addr.sin_family = AF_INET; + fwd_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); + fwd_addr.sin_port = htons(TM_PROCESS_FWD_PORT); + while(keepRunning == CRYPTO_LIB_SUCCESS) { - /* Do nothing for now */ + /* Receive */ + status = recvfrom(tm_sock->sockfd, tm_process_in, sizeof(tm_process_in), 0, (struct sockaddr*) &rcv_addr, (socklen_t*) &sockaddr_size); + if (status != -1) + { + tm_process_len = status; + #ifdef CRYPTO_STANDALONE_TM_PROCESS_DEBUG + printf("crypto_standalone_tm_process - received[%d]: 0x", tm_process_len); + for(int i = 0; i < status; i++) + { + printf("%02x", tm_process_in[i]); + } + printf("\n"); + #endif + + /* Process */ + status = Crypto_TM_ProcessSecurity(tm_process_in, &tm_process_len); + if (status == CRYPTO_LIB_SUCCESS) + { + #ifdef CRYPTO_STANDALONE_TM_PROCESS_DEBUG + printf("crypto_standalone_tm_process - status = %d, decrypted[%d]: 0x", status, tm_process_len); + for(int i = 0; i < tm_process_len; i++) + { + printf("%02x", tm_process_in[i]); + } + printf("\n"); + #endif + + /* Frame */ + #ifdef CRYPTO_STANDALONE_HANDLE_FRAMING + crypto_standalone_tm_frame(tm_process_in, tm_process_len, tm_framed, &tm_framed_len); + memcpy(tm_process_in, tm_framed, tm_framed_len); + tm_process_len = tm_framed_len; + tm_framed_len = 0; + #ifdef CRYPTO_STANDALONE_TM_PROCESS_DEBUG + printf("crypto_standalone_tm_process - deframed[%d]: 0x", tm_process_len); + for(int i = 0; i < tm_process_len; i++) + { + printf("%02x", tm_process_in[i]); + } + printf("\n"); + #endif + #endif + + /* Space Packet Protocol Loop */ + tm_ptr = &tm_process_in[0]; + + while (tm_process_len > 5) + { + if ((tm_ptr[0] == 0x08) || (tm_ptr[0] == 0x09)) + { + spp_len = ((tm_ptr[4] << 8) | tm_ptr[5]) + 7; + #ifdef CRYPTO_STANDALONE_TM_PROCESS_DEBUG + printf("crypto_standalone_tm_process - SPP[%d]: 0x", spp_len); + for(int i = 0; i < spp_len; i++) + { + printf("%02x", tm_ptr[i]); + } + printf("\n"); + #endif + status = sendto(tm_sock->sockfd, tm_ptr, spp_len, 0, (struct sockaddr*) &fwd_addr, sizeof(fwd_addr)); + if ((status == -1) || (status != spp_len)) + { + printf("crypto_standalone_tm_process - Reply error %d \n", status); + } + tm_ptr = &tm_ptr[spp_len]; + tm_process_len = tm_process_len - spp_len; + } + else + { + if ( ((tm_ptr[0] != 0x03) && (tm_ptr[1] != 0xFF)) && ((tm_ptr[0] != 0xFF) && (tm_ptr[1] != 0x48)) ) + { + printf("crypto_standalone_tm_process - SPP loop error, expected idle packet or frame! \n"); + } + tm_process_len = 0; + } + } + } + else + { + printf("crypto_standalone_tm_process - ProcessSecurity error %d \n", status); + } + + /* Reset */ + memset(tm_process_in, 0x00, sizeof(tm_process_in)); + tm_process_len = 0; + #ifdef CRYPTO_STANDALONE_TM_PROCESS_DEBUG + printf("\n"); + #endif + } /* Delay */ usleep(100); From b7d9d3f76ebf8149d2628f19898315c93d84c6ac Mon Sep 17 00:00:00 2001 From: "Lucas, John P" Date: Tue, 11 Apr 2023 16:22:32 -0400 Subject: [PATCH 10/10] [nasa-itc/cryptolib#2] Updated ut_tc_apply to change spi[4] back to vcid 0 for tests; --- util/src_util/ut_tc_apply.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util/src_util/ut_tc_apply.c b/util/src_util/ut_tc_apply.c index dc66c29d..a626d9fa 100644 --- a/util/src_util/ut_tc_apply.c +++ b/util/src_util/ut_tc_apply.c @@ -133,6 +133,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; sadb_routine->sadb_get_sa_from_spi(4, &test_association); + test_association->gvcid_tc_blk.vcid = 0; test_association->sa_state = SA_OPERATIONAL; test_association->ast = 0; test_association->arsn_len = 0; @@ -206,6 +207,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_AUTH_ENC) sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; sadb_routine->sadb_get_sa_from_spi(4, &test_association); + test_association->gvcid_tc_blk.vcid = 0; test_association->sa_state = SA_OPERATIONAL; test_association->arsn_len = 0; @@ -253,6 +255,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVE sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; sadb_routine->sadb_get_sa_from_spi(4, &test_association); + test_association->gvcid_tc_blk.vcid = 0; test_association->sa_state = SA_OPERATIONAL; test_association->shivf_len = 6; test_association->iv_len = 12; @@ -331,6 +334,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; sadb_routine->sadb_get_sa_from_spi(4, &test_association); + test_association->gvcid_tc_blk.vcid = 0; test_association->sa_state = SA_OPERATIONAL; test_association->shivf_len = 6; test_association->iv_len = 12; @@ -408,6 +412,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLO sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; sadb_routine->sadb_get_sa_from_spi(4, &test_association); + test_association->gvcid_tc_blk.vcid = 0; test_association->sa_state = SA_OPERATIONAL; test_association->shivf_len = 0; test_association->iv_len = 0;