Skip to content

Commit

Permalink
Merge pull request #343 from nasa/332-pack-structures-if-not-multiple…
Browse files Browse the repository at this point in the history
…-of-32-bits

332 pack structures if not multiple of 32 bits
  • Loading branch information
Donnie-Ice authored Oct 30, 2024
2 parents 2af8229 + e96dcba commit da65f0d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 43 deletions.
26 changes: 13 additions & 13 deletions include/crypto_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef struct
uint16_t scid : 16; // Spacecraft ID
uint16_t vcid : 6; // Virtual Channel ID
uint8_t mapid : 6; // Multiplexer Access Point ID
} crypto_gvcid_t;
} __attribute__((packed)) crypto_gvcid_t;
#define CRYPTO_GVCID_SIZE (sizeof(crypto_gvcid_t))

/*
Expand Down Expand Up @@ -88,7 +88,7 @@ typedef struct
uint8_t arsnw_len : 8; // Anti-Replay Seq Num Window Length
uint16_t arsnw; // Anti-Replay Seq Num Window

} SecurityAssociation_t;
} __attribute__((packed)) SecurityAssociation_t;
#define SA_SIZE (sizeof(SecurityAssociation_t))

/*
Expand All @@ -104,7 +104,7 @@ typedef struct
uint8_t ispif : 1; // Invalid SPI Flag
uint16_t lspiu : 16; // Last SPI Used
uint8_t snval : 8; // SN Value (LSB)
} SDLS_FSR_t;
} __attribute__((packed)) SDLS_FSR_t;
#define SDLS_FSR_SIZE (sizeof(SDLS_FSR_t))

typedef struct
Expand Down Expand Up @@ -275,7 +275,7 @@ typedef struct
uint8_t vcid : 6; // Virtual Channel ID
uint16_t fl : 10; // The whole transfer frame length (max 1024)
uint8_t fsn : 8; // Frame sequence number, also N(S), zeroed on Type-B frames
} TC_FramePrimaryHeader_t;
} __attribute__((packed)) TC_FramePrimaryHeader_t;
#define TC_FRAME_PRIMARYHEADER_STRUCT_SIZE (sizeof(TC_FramePrimaryHeader_t))
#define TC_FRAME_HEADER_SIZE 5

Expand All @@ -289,15 +289,15 @@ typedef struct
uint8_t sn_field_len;
uint8_t pad[PAD_SIZE]; // Count of the used fill Bytes
uint8_t pad_field_len;
} TC_FrameSecurityHeader_t;
} __attribute__((packed)) TC_FrameSecurityHeader_t;
#define TC_FRAME_SECHEADER_SIZE (sizeof(TC_FrameSecurityHeader_t))

typedef struct
{
uint8_t mac[MAC_SIZE]; // Message Authentication Code
uint8_t mac_field_len;
uint16_t fecf; // Frame Error Control Field
} TC_FrameSecurityTrailer_t;
} __attribute__((packed)) TC_FrameSecurityTrailer_t;
#define TC_FRAME_SECTRAILER_SIZE (sizeof(TC_FrameSecurityTrailer_t))

typedef struct
Expand All @@ -307,7 +307,7 @@ typedef struct
uint8_t tc_pdu[TC_FRAME_DATA_SIZE];
uint16_t tc_pdu_len;
TC_FrameSecurityTrailer_t tc_sec_trailer;
} TC_t;
} __attribute__((packed)) TC_t;
#define TC_SIZE (sizeof(TC_t))

/*
Expand Down Expand Up @@ -342,7 +342,7 @@ typedef struct
CCSDS_HDR_t hdr;
CCSDS_PUS_t pus;
SDLS_TLV_t pdu;
} CCSDS_t;
} __attribute__((packed)) CCSDS_t;
#define CCSDS_SIZE (sizeof(CCSDS_t))

/*
Expand Down Expand Up @@ -370,7 +370,7 @@ typedef struct
uint8_t fbc : 2; // FARM-B Counter
uint8_t spare1 : 1; // Reserved Spare "0"
uint8_t rv : 8; // Report Value
} Telemetry_Frame_Ocf_Clcw_t;
} __attribute__((packed)) Telemetry_Frame_Ocf_Clcw_t;

#define TELEMETRY_FRAME_OCF_CLCW_SIZE (sizeof(Telemetry_Frame_Ocf_Clcw_t))

Expand All @@ -386,7 +386,7 @@ typedef struct
uint8_t bsaf : 1; // Bad Security Association Flag
uint16_t lspi : 16; // Last SPI Used
uint8_t snval : 8; // Sequence Number Value (LSB)
} Telemetry_Frame_Ocf_Fsr_t;
} __attribute__((packed)) Telemetry_Frame_Ocf_Fsr_t;

#define TELEMETRY_FRAME_OCF_FSR_SIZE (sizeof(Telemetry_Frame_Ocf_Fsr_t))

Expand Down Expand Up @@ -463,10 +463,10 @@ typedef struct
uint8_t spare : 2; // Reserved Spare
// 0 = Shall be set to 0
// Sync Flag 1 = Undefined
uint8_t vfcc : 2; // VC Frame Count cycle
uint8_t vfcc : 4; // VC Frame Count cycle
// Sync Flag 0 = Shall be 11
// Sync Flag 1 = Undefined
uint16_t fhp : 11; // First Header Pointer
uint16_t fhp : 16; // First Header Pointer
// Sync Flag 0 = Contains position of the first byte of the first packet in the data field
// Sync Flag 1 = undefined
} AOS_FramePrimaryHeader_t;
Expand All @@ -493,7 +493,7 @@ typedef struct
AOS_FrameSecurityHeader_t tm_sec_header;
uint8_t aos_pdu[AOS_FRAME_DATA_SIZE];
AOS_FrameSecurityTrailer_t aos_sec_trailer;
} AOS_t;
} __attribute__((packed)) AOS_t;
#define AOS_SIZE (sizeof(AOS_t))

#define AOS_MIN_SIZE \
Expand Down
1 change: 1 addition & 0 deletions src/core/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ uint8_t Crypto_Prep_Reply(uint8_t *reply, uint8_t appID)
reply[count++] = (sdls_frame.pdu.hdr.pdu_len & 0xFF00) >> 8;
reply[count++] = (sdls_frame.pdu.hdr.pdu_len & 0x00FF);

sdls_frame.pdu.hdr.type = 0;
return count;
}

Expand Down
95 changes: 65 additions & 30 deletions test/unit/ut_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ UTEST(CRYPTO_C, PDU_SWITCH)

Crypto_Init();

sdls_frame.hdr.type = 0;
sdls_frame.pdu.hdr.uf = 0;
sdls_frame.pdu.hdr.type = PDU_TYPE_COMMAND;
sdls_frame.pdu.hdr.uf = PDU_USER_FLAG_FALSE;
printf("SG_KEY_MGMT\n");
sdls_frame.pdu.hdr.sg = SG_KEY_MGMT;
sdls_frame.pdu.hdr.pid = PID_OTAR;
Expand All @@ -131,7 +131,6 @@ UTEST(CRYPTO_C, PDU_SWITCH)
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pdu_len = 0;
sdls_frame.pdu.hdr.pid = PID_KEY_VERIFICATION;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
Expand All @@ -142,7 +141,7 @@ UTEST(CRYPTO_C, PDU_SWITCH)

sdls_frame.pdu.hdr.pid = PID_KEY_INVENTORY;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTOGRAPHY_UNSUPPORTED_OPERATION_FOR_KEY_RING);

sdls_frame.pdu.hdr.pid = SG_KEY_MGMT;
status = Crypto_PDU(ingest, &tc_frame);
Expand Down Expand Up @@ -184,11 +183,11 @@ UTEST(CRYPTO_C, PDU_SWITCH)

sdls_frame.pdu.hdr.pid = PID_READ_ARSN;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = PID_SA_STATUS;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = 0b111;
status = Crypto_PDU(ingest, &tc_frame);
Expand All @@ -198,19 +197,19 @@ UTEST(CRYPTO_C, PDU_SWITCH)
sdls_frame.pdu.hdr.sg = SG_SEC_MON_CTRL;
sdls_frame.pdu.hdr.pid = PID_LOG_STATUS;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = PID_DUMP_LOG;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = PID_ERASE_LOG;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = PID_SELF_TEST;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = PID_ALARM_FLAG;
status = Crypto_PDU(ingest, &tc_frame);
Expand All @@ -220,53 +219,45 @@ UTEST(CRYPTO_C, PDU_SWITCH)
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

printf("PID_LOG_STATUS\n");
sdls_frame.pdu.hdr.sg = PID_LOG_STATUS;
sdls_frame.pdu.hdr.pid = PID_LOG_STATUS;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

printf("UF_TRUE\n");
sdls_frame.pdu.hdr.uf = 1;
sdls_frame.pdu.hdr.pid = 0;
sdls_frame.pdu.hdr.uf = PDU_USER_FLAG_TRUE;
sdls_frame.pdu.hdr.pid = PID_IDLE_FRAME_TRIGGER;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pid = 1;
sdls_frame.pdu.hdr.pid = PID_TOGGLE_BAD_SPI;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pid = 2;
sdls_frame.pdu.hdr.pid = PID_TOGGLE_BAD_IV;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pid = 3;
sdls_frame.pdu.hdr.pid = PID_TOGGLE_BAD_MAC;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pid = 4;
sdls_frame.pdu.hdr.pid = PID_TOGGLE_BAD_FECF;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pid = 5;
sdls_frame.pdu.hdr.pid = PID_MODIFY_KEY;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.pdu.hdr.pid = 6;
sdls_frame.pdu.hdr.pid = PID_MODIFY_ACTIVE_TM;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

// TODO: (RB) Disabled for now. Key Inventory needs to be re-worked. - Not currently using EP

// sdls_frame.pdu.hdr.pid = 7;
// status = Crypto_PDU(ingest, &tc_frame);
// ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
sdls_frame.pdu.hdr.pid = PID_MODIFY_VCID;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_ERROR);

sdls_frame.pdu.hdr.pid = 8;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);

sdls_frame.hdr.type = 1;
sdls_frame.hdr.type = PDU_TYPE_REPLY;
sdls_frame.pdu.hdr.pid = 8;
status = Crypto_PDU(ingest, &tc_frame);
ASSERT_EQ(status, CRYPTO_LIB_SUCCESS);
Expand Down Expand Up @@ -360,4 +351,48 @@ UTEST(CRYPTO_C, GET_ECS_ALGO_KEY_LEN)
ASSERT_EQ(algo_keylen, 32);
}

UTEST(CRYPTO_C, STRUCT_SIZE_VERIFICATION)
{
ASSERT_EQ(CRYPTO_GVCID_SIZE, (size_t)4);
ASSERT_EQ(SA_SIZE, (size_t)2348);
ASSERT_EQ(SDLS_FSR_SIZE, (size_t)4);
ASSERT_EQ(SDLS_TLV_HDR_SIZE, (size_t)3);
ASSERT_EQ(SDLS_TLV_SIZE, (size_t)497);
ASSERT_EQ(SDLS_EKB_SIZE, (size_t)514);
ASSERT_EQ(SDLS_OTAR_SIZE, (size_t)(2 + 16 + 30*514 + 16));
ASSERT_EQ(SDLS_KEY_SIZE, (size_t)2);
ASSERT_EQ(SDLS_KEY_INVENTORY_CMD_SIZE, (size_t)4);
ASSERT_EQ(SDLS_KEY_INVENTORY_RPLY_SIZE, (size_t)3);
ASSERT_EQ(SDLS_KEYV_CMD_BLK_SIZE, (size_t)18);
ASSERT_EQ(SDLS_KEYV_CMD_SIZE, (size_t)(29*18));
ASSERT_EQ(SDLS_KEYV_RPLY_BLK_SIZE, (size_t)50);
ASSERT_EQ(SDLS_KEYV_RPLY_SIZE, (size_t)(50*29));
ASSERT_EQ(SDLS_KEYDB_CMD_SIZE, (size_t)12);
ASSERT_EQ(SDLS_KEYDB_RPLY_SIZE, (size_t)38);
ASSERT_EQ(SDLS_SA_STATUS_RPLY_SIZE, (size_t)3);
ASSERT_EQ(SDLS_SA_READ_ARSN_RPLY_SIZE, (size_t)22);
ASSERT_EQ(SDLS_MC_LOG_RPLY_SIZE, (size_t)4);
ASSERT_EQ(SDLS_MC_DUMP_RPLY_SIZE, (size_t)7);
ASSERT_EQ(SDLS_MC_DUMP_BLK_RPLY_SIZE, (size_t)(7*50));
ASSERT_EQ(SDLS_MC_ST_RPLY_SIZE, (size_t)1);
ASSERT_EQ(SDLS_MC_SN_RPLY_SIZE, (size_t)16);
ASSERT_EQ(TC_FRAME_PRIMARYHEADER_STRUCT_SIZE, (size_t)5);
ASSERT_EQ(TC_FRAME_SECHEADER_SIZE, (size_t)70);
ASSERT_EQ(TC_FRAME_SECTRAILER_SIZE, (size_t)19);
ASSERT_EQ(TC_SIZE, (size_t)(5 + 70 + 19 + 2 + 1019));
ASSERT_EQ(CCSDS_HDR_SIZE, (size_t)6);
ASSERT_EQ(CCSDS_PUS_SIZE, (size_t)4);
ASSERT_EQ(CCSDS_SIZE, (size_t)(6 + 4 + 497));
ASSERT_EQ(TELEMETRY_FRAME_OCF_CLCW_SIZE, (size_t)4);
ASSERT_EQ(TELEMETRY_FRAME_OCF_FSR_SIZE, (size_t)4);
ASSERT_EQ(TM_FRAME_PRIMARYHEADER_SIZE, (size_t)6);
ASSERT_EQ(TM_FRAME_SECHEADER_SIZE, (size_t)18);
ASSERT_EQ(TM_FRAME_SECTRAILER_SIZE, (size_t)22);
ASSERT_EQ(TM_SIZE, (size_t)(6 + 18 + 1786 + 22));
ASSERT_EQ(AOS_FRAME_PRIMARYHEADER_SIZE, (size_t)8);
ASSERT_EQ(AOS_FRAME_SECHEADER_SIZE, (size_t)18);
ASSERT_EQ(AOS_FRAME_SECTRAILER_SIZE, (size_t)22);
ASSERT_EQ(AOS_SIZE, (size_t)(8 + 18 + 1786 + 22));
}

UTEST_MAIN();

0 comments on commit da65f0d

Please sign in to comment.