Skip to content

Commit

Permalink
Merge pull request #152 from #2-Standalone
Browse files Browse the repository at this point in the history
CryptoLib#2 - Standalone
  • Loading branch information
jlucas9 authored Apr 12, 2023
2 parents e445033 + b7d9d3f commit b938827
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 19 deletions.
15 changes: 9 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand All @@ -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()
2 changes: 2 additions & 0 deletions include/crypto_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -43,6 +44,7 @@
#define KCYN "\x1B[36m"
#define RESET "\033[0m"
#else
#define CRYPTO_DEBUG
#define KRED
#define RED
#define KGRN
Expand Down
20 changes: 14 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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++)
{
Expand Down
1 change: 1 addition & 0 deletions src/src_main/crypto_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/src_main/sadb_routine_inmemory.template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions util/include/standalone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* 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 <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#include "crypto.h"
#include "shared_util.h"


/*
** Configuration
*/
#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 3
#define CRYPTO_STANDALONE_FRAMING_VCID 0x00
#define CRYPTO_STANDALONE_FRAMING_TC_DATA_LEN 256


/*
** Defines
*/
#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
#define CRYPTO_CMD_VCID 4


/*
** Structures
*/
typedef struct
{
int sockfd;
int port;
} udp_info_t;


/*
** 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_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);


#ifdef __cplusplus
} /* Close scope of 'extern "C"' declaration which encloses file. */
#endif

#endif /* CRYPTOLIB_STANDALONE_H */
Loading

0 comments on commit b938827

Please sign in to comment.