Skip to content

Commit

Permalink
[#344] Removed function with collision checks, now checks gvcid to co…
Browse files Browse the repository at this point in the history
…nfigure sql table selection
  • Loading branch information
Donnie-Ice committed Nov 13, 2024
1 parent 9bb8f50 commit c550037
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 70 deletions.
1 change: 0 additions & 1 deletion include/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ uint16_t Crypto_Calc_CRC16(uint8_t *data, int size);
int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t *sa_ptr, uint8_t *arsn, uint8_t *iv);
int32_t Crypto_Get_ECS_Algo_Keylen(uint8_t algo);
int32_t Crypto_Get_ACS_Algo_Keylen(uint8_t algo);
char* Crypto_Get_MariaDB_Table_Prefix(uint8_t table);

int32_t Crypto_Check_Anti_Replay_Verify_Pointers(SecurityAssociation_t *sa_ptr, uint8_t *arsn, uint8_t *iv);
int32_t Crypto_Check_Anti_Replay_ARSNW(SecurityAssociation_t *sa_ptr, uint8_t *arsn, int8_t *arsn_valid);
Expand Down
8 changes: 4 additions & 4 deletions include/crypto_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
#endif

// KMC Defines
#define MARIADB_TC_TABLE_PREFIX "TC_security_associations"
#define MARIADB_TM_TABLE_PREFIX "TM_security_associations"
#define MARIADB_AOS_TABLE_PREFIX "AOS_security_associations"
#define MARIADB_LEGACY_TABLE_PREFIX "security_associations"
#define MARIADB_TC_TABLE_NAME "TC_security_associations"
#define MARIADB_TM_TABLE_NAME "TM_security_associations"
#define MARIADB_AOS_TABLE_NAME "AOS_security_associations"
#define MARIADB_LEGACY_TABLE_NAME "security_associations"

// Managed Parameters Size
#define GVCID_MAN_PARAM_SIZE 250
Expand Down
30 changes: 0 additions & 30 deletions src/core/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,33 +1210,3 @@ uint32_t Crypto_Get_FSR()
return fsr;
}

/**
* @brief: Function: Crypto_Get_MariaDB_Table_Prefix
* For a given format (TC/TM/AOS), return the MariaDB table prefix
* @param algo: uint8_t
* @return int32: Key Length
**/
// char* Crypto_Get_MariaDB_Table_Prefix(uint8_t table)
// {
// char retval[25];

// switch (table)
// {
// case MARIADB_TC_TABLE:
// retval = MARIADB_TC_TABLE_PREFIX;
// break;
// case MARIADB_TM_TABLE:
// retval = MARIADB_TM_TABLE_PREFIX;
// break;
// case MARIADB_AOS_TABLE:
// retval = MARIADB_AOS_TABLE_PREFIX;
// break;
// case MARIADB_LEGACY_TABLE:
// retval = MARIADB_LEGACY_TABLE_PREFIX;
// break;
// default:
// break;
// }

// return retval;
// }
57 changes: 22 additions & 35 deletions src/sa/mariadb/sa_interface_mariadb.template.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static const char *SQL_SADB_UPDATE_IV_ARC_BY_SPI_NULL_IV =
static int32_t parse_sa_from_mysql_query(char *query, SecurityAssociation_t **security_association);
static int32_t convert_hexstring_to_byte_array(char *hexstr, uint8_t *byte_array);
static void convert_byte_array_to_hexstring(void *src_buffer, size_t buffer_length, char *dest_str);
static int32_t parse_table_from_gvcid(char* table);

/*
** Global Variables
Expand Down Expand Up @@ -188,7 +189,8 @@ static int32_t sa_get_from_spi(uint16_t spi, SecurityAssociation_t **security_as

char spi_query[2048];
char table[25];
status = query_all_tables(&table);

status = parse_table_from_gvcid(&table);
if (status == CRYPTO_LIB_SUCCESS)
{
snprintf(spi_query, sizeof(spi_query), SQL_SADB_GET_SA_BY_SPI, table, spi);
Expand All @@ -202,9 +204,9 @@ static int32_t sa_get_operational_sa_from_gvcid(uint8_t tfvn, uint16_t scid, uin
int32_t status = CRYPTO_LIB_SUCCESS;

char gvcid_query[2048];

char table[25];
status = query_all_tables(&table);

status = parse_table_from_gvcid(&table);
if (status == CRYPTO_LIB_SUCCESS)
{
snprintf(gvcid_query, sizeof(gvcid_query), SQL_SADB_GET_SA_BY_GVCID, table, tfvn, scid, vcid, mapid,
Expand Down Expand Up @@ -232,9 +234,9 @@ static int32_t sa_save_sa(SecurityAssociation_t *sa)

char *arsn_h = malloc(sa->arsn_len * 2 + 1);
convert_byte_array_to_hexstring(sa->arsn, sa->arsn_len, arsn_h);
// insert table queries here, store in variable = table that returned correct response

char table[25];
status = query_all_tables(&table);
status = parse_table_from_gvcid(&table);
if (status == CRYPTO_LIB_SUCCESS)
{
if (sa->iv != NULL)
Expand Down Expand Up @@ -599,39 +601,24 @@ static int32_t finish_with_error(MYSQL **con_loc, int err)
return err;
}

static int32_t query_all_tables(char* table)
static int32_t parse_table_from_gvcid(char* table)
{
int32_t status = 0;
char gvcid_query[2048];

char *tables[] = {MARIADB_TC_TABLE_PREFIX, MARIADB_TM_TABLE_PREFIX, MARIADB_AOS_TABLE_PREFIX};
char *mapid[] = {TYPE_TC , TYPE_TM , TYPE_AOS};
for (int i = 0; i <= 2; i++)
if (current_managed_parameters->has_fecf == TC_HAS_FECF || current_managed_parameters->has_fecf == TC_NO_FECF)
{
snprintf(gvcid_query, sizeof(gvcid_query), SQL_SADB_GET_SA_BY_GVCID, tables[i], current_managed_parameters_struct.tfvn, current_managed_parameters_struct.scid, current_managed_parameters_struct.vcid, mapid[i],
SA_OPERATIONAL);

MYSQL_RES *result = mysql_store_result(con);

int num_rows = mysql_num_rows(result);
if (num_rows == 0)
{
continue;
}
else
{
if (status == CRYPTO_LIB_SUCCESS)
{
//Collision
return CRYPTO_LIB_ERROR;
}
else
{
status = CRYPTO_LIB_SUCCESS;
table = tables[i];
}
}
table = MARIADB_TC_TABLE_NAME;
}
else if (current_managed_parameters->has_fecf == TM_HAS_FECF || current_managed_parameters->has_fecf == TM_NO_FECF)
{
table = MARIADB_TM_TABLE_NAME;
}
else if (current_managed_parameters->has_fecf == AOS_HAS_FECF || current_managed_parameters->has_fecf == AOS_NO_FECF)
{
table = MARIADB_AOS_TABLE_NAME;
}
else
{
status = CRYPTO_LIB_ERROR;
}

return status;
}

0 comments on commit c550037

Please sign in to comment.