Skip to content

Commit 6a90952

Browse files
committed
[#487] remove table parsing function, add MULTI_TABLE build flag, fix UTs, remove always true checks
1 parent c53a43a commit 6a90952

File tree

14 files changed

+115
-86
lines changed

14 files changed

+115
-86
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ option(SA_CUSTOM "Security Association - Custom" OFF)
5656
option(SA_CUSTOM_PATH "Custom Security Association Path" OFF)
5757
option(SA_INTERNAL "Security Association - Internal" OFF)
5858
option(SA_MARIADB "Security Association - MariaDB" OFF)
59+
option(MARIADB_MULTI_TABLE "Specifies Unique SADB tables for TC, TM, and AOS" OFF)
5960
option(SUPPORT "Support" OFF)
6061
option(SYSTEM_INSTALL "SystemInstall" OFF)
6162
option(TEST "Test" OFF)

include/crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ extern uint8_t parity[4]; // Used in FHECF calc
324324
// Global configuration structs
325325
extern CryptoConfig_t crypto_config;
326326
extern SadbMariaDBConfig_t *sa_mariadb_config;
327+
extern char* mariadb_table_name;
327328
extern CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config;
328329
extern CamConfig_t *cam_config;
329330
extern GvcidManagedParameters_t *gvcid_managed_parameters;

include/crypto_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,11 @@
269269
*/
270270
#define TC_BLOCK_SIZE 16
271271

272+
/*
273+
** MariaDB Defines
274+
*/
275+
#define MARIADB_TC_TABLE_NAME "security_associations"
276+
#define MARIADB_TM_TABLE_NAME "security_associations_tm"
277+
#define MARIADB_AOS_TABLE_NAME "security_associations_aos"
278+
272279
#endif // CRYPTO_CONFIG_H

src/core/crypto.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ uint8_t parity[RS_PARITY];
7272
// CRC
7373
uint32_t crc32Table[CRC32TBL_SIZE];
7474
uint16_t crc16Table[CRC16TBL_SIZE];
75+
// Mariadb
76+
char* mariadb_table_name = "security_associations";
7577

7678
/*
7779
** Assisting Functions

src/core/crypto_aos.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer, uint16_t len_ingest)
106106
printf("\n");
107107
#endif
108108

109+
#ifdef MARIADB_MULTI_TABLE
110+
mariadb_table_name = MARIADB_AOS_TABLE_NAME;
111+
#endif
109112
status = sa_if->sa_get_operational_sa_from_gvcid(tfvn, scid, vcid, 0, &sa_ptr);
110113

111114
// No operational/valid SA found
@@ -940,11 +943,13 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
940943
* Reference CCSDS SDLP 3550b1 4.1.1.1.3
941944
**/
942945
// Get SPI
943-
printf("byte_idx: %d\n", byte_idx);
944946
spi = (uint8_t)p_ingest[byte_idx] << 8 | (uint8_t)p_ingest[byte_idx + 1];
945947
// Move index to past the SPI
946948
byte_idx += 2;
947949

950+
#ifdef MARIADB_MULTI_TABLE
951+
mariadb_table_name = MARIADB_AOS_TABLE_NAME;
952+
#endif
948953
status = sa_if->sa_get_from_spi(spi, &sa_ptr);
949954
// If no valid SPI, return
950955
if (status != CRYPTO_LIB_SUCCESS)
@@ -1417,7 +1422,7 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
14171422
pp_processed_frame->aos_header.sf = (p_new_dec_frame[5] & 0x40) >> 6;
14181423
pp_processed_frame->aos_header.spare = (p_new_dec_frame[5] & 0x30) >> 4;
14191424
pp_processed_frame->aos_header.vfcc = (p_new_dec_frame[5] & 0x0F);
1420-
if (current_managed_parameters_struct.aos_has_fhec == AOS_HAS_FHEC)
1425+
if (aos_current_managed_parameters_struct.aos_has_fhec == AOS_HAS_FHEC)
14211426
{
14221427
pp_processed_frame->aos_header.fhecf = (p_new_dec_frame[6] << 8) | p_new_dec_frame[7];
14231428
byte_idx += 8;
@@ -1428,13 +1433,13 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
14281433
}
14291434

14301435
// Security Header
1431-
if (current_managed_parameters_struct.aos_has_iz == AOS_HAS_IZ)
1436+
if (aos_current_managed_parameters_struct.aos_has_iz == AOS_HAS_IZ)
14321437
{
1433-
for (int i = 0; i < current_managed_parameters_struct.aos_iz_len; i++)
1438+
for (int i = 0; i < aos_current_managed_parameters_struct.aos_iz_len; i++)
14341439
{
14351440
memcpy(pp_processed_frame->aos_sec_header.iz + i, &p_new_dec_frame[byte_idx + i], 1);
14361441
}
1437-
byte_idx += current_managed_parameters_struct.aos_iz_len;
1442+
byte_idx += aos_current_managed_parameters_struct.aos_iz_len;
14381443
}
14391444

14401445
pp_processed_frame->aos_sec_header.spi =
@@ -1475,7 +1480,7 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
14751480
byte_idx += sa_ptr->stmacf_len;
14761481
pp_processed_frame->aos_sec_trailer.mac_field_len = sa_ptr->stmacf_len;
14771482

1478-
if (current_managed_parameters_struct.has_ocf == AOS_HAS_OCF)
1483+
if (aos_current_managed_parameters_struct.has_ocf == AOS_HAS_OCF)
14791484
{
14801485
for (int i = 0; i < OCF_SIZE; i++)
14811486
{
@@ -1489,7 +1494,7 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
14891494
pp_processed_frame->aos_sec_trailer.ocf_field_len = 0;
14901495
}
14911496

1492-
if (current_managed_parameters_struct.has_fecf == AOS_HAS_FECF)
1497+
if (aos_current_managed_parameters_struct.has_fecf == AOS_HAS_FECF)
14931498
{
14941499
pp_processed_frame->aos_sec_trailer.fecf =
14951500
(uint16_t)(p_new_dec_frame[byte_idx] << 8) | p_new_dec_frame[byte_idx + 1];

src/core/crypto_config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ int32_t Crypto_SC_Init(void)
9797
status = Crypto_Init();
9898

9999
SecurityAssociation_t *sa_ptr = NULL;
100+
#ifdef MARIADB_MULTI_TABLE
101+
mariadb_table_name = MARIADB_TC_TABLE_NAME;
102+
#endif
100103
sa_if->sa_get_from_spi(1, &sa_ptr);
101104
sa_ptr->gvcid_blk.vcid = 0;
102105
sa_if->sa_get_from_spi(2, &sa_ptr);
@@ -108,6 +111,9 @@ int32_t Crypto_SC_Init(void)
108111
sa_ptr->abm_len = ABM_SIZE;
109112
sa_ptr->shivf_len = 0;
110113
sa_ptr->iv_len = 0;
114+
#ifdef MARIADB_MULTI_TABLE
115+
mariadb_table_name = MARIADB_TM_TABLE_NAME;
116+
#endif
111117
sa_if->sa_get_from_spi(5, &sa_ptr);
112118
sa_ptr->sa_state = SA_OPERATIONAL;
113119
sa_ptr->shsnf_len = 0;

src/core/crypto_mc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ int32_t Crypto_SA_readARSN(uint8_t *ingest)
291291
// Read ingest
292292
spi = ((uint8_t)sdls_frame.tlv_pdu.data[0] << BYTE_LEN) | (uint8_t)sdls_frame.tlv_pdu.data[1];
293293

294+
// TODO: This is not correct
295+
#ifdef MARIADB_MULTI_TABLE
296+
mariadb_table_name = MARIADB_TC_TABLE_NAME;
297+
#endif
294298
status = sa_if->sa_get_from_spi(spi, &sa_ptr);
295299

296300
if (status != CRYPTO_LIB_SUCCESS)

src/core/crypto_tc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,9 @@ int32_t Crytpo_TC_Validate_TC_Temp_Header(const uint16_t in_frame_length, TC_Fra
838838
mc_if->mc_log(status);
839839
return status;
840840
}
841+
#ifdef MARIADB_MULTI_TABLE
842+
mariadb_table_name = MARIADB_TC_TABLE_NAME;
843+
#endif
841844
status = sa_if->sa_get_operational_sa_from_gvcid(temp_tc_header.tfvn, temp_tc_header.scid, temp_tc_header.vcid,
842845
*map_id, sa_ptr);
843846
// If unable to get operational SA, can return
@@ -1818,6 +1821,9 @@ uint32_t Crypto_TC_Sanity_Validations(TC_t *tc_sdls_processed_frame, SecurityAss
18181821
{
18191822
uint32_t status = CRYPTO_LIB_SUCCESS;
18201823

1824+
#ifdef MARIADB_MULTI_TABLE
1825+
mariadb_table_name = MARIADB_TC_TABLE_NAME;
1826+
#endif
18211827
status = sa_if->sa_get_from_spi(tc_sdls_processed_frame->tc_sec_header.spi, sa_ptr);
18221828
// If no valid SPI, return
18231829
if (status == CRYPTO_LIB_SUCCESS)
@@ -2208,6 +2214,10 @@ static int32_t validate_sa_index(SecurityAssociation_t *sa)
22082214
{
22092215
int32_t returnval = 0;
22102216
SecurityAssociation_t *temp_sa;
2217+
2218+
#ifdef MARIADB_MULTI_TABLE
2219+
mariadb_table_name = MARIADB_TC_TABLE_NAME;
2220+
#endif
22112221
sa_if->sa_get_from_spi(sa->spi, &temp_sa);
22122222

22132223
// Do not validate sa index on KMC

src/core/crypto_tm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ int32_t Crypto_TM_ApplySecurity(uint8_t *pTfBuffer, uint16_t len_ingest)
821821
printf("\n");
822822
#endif
823823

824+
#ifdef MARIADB_MULTI_TABLE
825+
mariadb_table_name = MARIADB_TM_TABLE_NAME;
826+
#endif
824827
status = sa_if->sa_get_operational_sa_from_gvcid(tfvn, scid, vcid, 0, &sa_ptr);
825828

826829
// No operational/valid SA found
@@ -1637,7 +1640,7 @@ int32_t Crypto_TM_Do_Decrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_
16371640
}
16381641
byte_idx += sa_ptr->stmacf_len;
16391642
pp_processed_frame->tm_sec_trailer.mac_field_len = sa_ptr->stmacf_len;
1640-
if (current_managed_parameters_struct.has_ocf == TM_HAS_OCF)
1643+
if (tm_current_managed_parameters_struct.has_ocf == TM_HAS_OCF)
16411644
{
16421645
for (int i = 0; i < OCF_SIZE; i++)
16431646
{
@@ -1650,7 +1653,7 @@ int32_t Crypto_TM_Do_Decrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_
16501653
{
16511654
pp_processed_frame->tm_sec_trailer.ocf_field_len = 0;
16521655
}
1653-
if (current_managed_parameters_struct.has_fecf == TM_HAS_FECF)
1656+
if (tm_current_managed_parameters_struct.has_fecf == TM_HAS_FECF)
16541657
{
16551658
pp_processed_frame->tm_sec_trailer.fecf =
16561659
((uint16_t)p_new_dec_frame[byte_idx] << 8) | p_new_dec_frame[byte_idx + 1];
@@ -1744,6 +1747,9 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
17441747
// Move index to past the SPI
17451748
byte_idx += 2;
17461749

1750+
#ifdef MARIADB_MULTI_TABLE
1751+
mariadb_table_name = MARIADB_TM_TABLE_NAME;
1752+
#endif
17471753
status = sa_if->sa_get_from_spi(spi, &sa_ptr);
17481754
}
17491755

src/core/crypto_user.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ int32_t Crypto_User_ModifyVCID(void)
174174
int i;
175175
int j;
176176

177+
// TODO: This is not correct
178+
#ifdef MARIADB_MULTI_TABLE
179+
mariadb_table_name = MARIADB_TC_TABLE_NAME;
180+
#endif
181+
177182
for (i = 0; i < NUM_GVCID; i++)
178183
{
179184
if (sa_if->sa_get_from_spi(i, &sa_ptr) != CRYPTO_LIB_SUCCESS)

0 commit comments

Comments
 (0)