Skip to content

Commit 3c74445

Browse files
authored
Merge commit from fork
add additional AOS Process length checks
2 parents e10e04b + 5bade74 commit 3c74445

6 files changed

Lines changed: 166 additions & 36 deletions

File tree

include/crypto_error.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@
156156
#define CRYPTO_LIB_ERR_TC_FRAME_LENGTH_MISMATCH (-82)
157157
#define CRYPTO_LIB_ERR_SHPLF_LEN_LESS_THAN_MIN_PAD_SIZE (-83)
158158
#define CRYPTO_LIB_ERR_INVALID_AOS_IZ_LENGTH (-84)
159+
#define CRYPTO_LIB_ERR_INVALID_AOS_FRAME_LENGTH (-85)
159160

160-
#define CRYPTO_CORE_ERROR_CODES_MAX -84
161+
#define CRYPTO_CORE_ERROR_CODES_MAX -85
161162

162163
// Define codes for returning MDB Strings, and determining error based on strings
163164
#define CAM_ERROR_CODES 600

include/crypto_structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,6 @@ typedef struct
637637
} __attribute__((packed)) AOS_t;
638638
#define AOS_SIZE (sizeof(AOS_t))
639639

640-
#define AOS_MIN_SIZE 7
640+
#define AOS_MIN_SIZE 6
641641

642642
#endif // CRYPTO_STRUCTS_H

src/core/crypto_aos.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,20 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer, uint16_t len_ingest)
804804
return status;
805805
}
806806

807+
int32_t Crypto_AOS_Verify_Frame_Lengths(uint16_t len_ingest)
808+
{
809+
uint8_t fhec_len = aos_current_managed_parameters_struct.aos_has_fhec == AOS_HAS_FHEC ? FHECF_SIZE : 0;
810+
uint16_t iz_len = aos_current_managed_parameters_struct.aos_has_iz == AOS_HAS_IZ ? aos_current_managed_parameters_struct.aos_iz_len : 0;
811+
uint8_t ocf_len = aos_current_managed_parameters_struct.has_ocf == AOS_HAS_OCF ? OCF_SIZE : 0;
812+
uint8_t fecf_len = aos_current_managed_parameters_struct.has_fecf == AOS_HAS_FECF ? FECF_SIZE : 0;
813+
uint16_t expected_frame_length = AOS_MIN_SIZE + fhec_len + SPI_LEN + iz_len + ocf_len + fecf_len;
814+
if (len_ingest < expected_frame_length)
815+
{
816+
return CRYPTO_LIB_ERR_INVALID_AOS_FRAME_LENGTH;
817+
}
818+
return CRYPTO_LIB_SUCCESS;
819+
}
820+
807821
/**
808822
* @brief Function: Crypto_AOS_ProcessSecurity
809823
* @param ingest: uint8_t*
@@ -889,6 +903,12 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
889903
return status;
890904
} // Unable to get necessary Managed Parameters for AOS TF -- return with error.
891905

906+
status = Crypto_AOS_Verify_Frame_Lengths(len_ingest);
907+
if (status != CRYPTO_LIB_SUCCESS)
908+
{
909+
return status;
910+
}
911+
892912
// Increment to end of Primary Header start, depends on FHECF presence
893913
byte_idx = 6;
894914
if (aos_current_managed_parameters_struct.aos_has_fhec == AOS_HAS_FHEC)

src/core/crypto_error.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ char *crypto_enum_errlist_core[] = {(char *)"CRYPTO_LIB_SUCCESS",
114114
(char *)"CRYPTO_LIB_ERR_TM_SECONDARY_HDR_VN",
115115
(char *)"CRYPTO_LIB_ERR_TC_FRAME_LENGTH_MISMATCH",
116116
(char *)"CRYPTO_LIB_ERR_SHPLF_LEN_LESS_THAN_MIN_PAD_SIZE",
117-
(char *)"CRYPTO_LIB_ERR_INVALID_AOS_IZ_LENGTH"};
117+
(char *)"CRYPTO_LIB_ERR_INVALID_AOS_IZ_LENGTH",
118+
(char *)"CRYPTO_LIB_ERR_INVALID_AOS_FRAME_LENGTH"};
118119

119120
char *crypto_enum_errlist_config[] = {
120121
(char *)"CRYPTO_CONFIGURATION_NOT_COMPLETE",

src/crypto/libgcrypt/cryptography_interface_libgcrypt.template.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -305,38 +305,38 @@ static int32_t cryptography_validate_authentication(uint8_t *data_out, size_t le
305305

306306
#ifdef MAC_DEBUG
307307
// Commented out due to memory leaks with HMAC
308-
uint32_t *tmac_size = &mac_size;
309-
uint8_t tmac[*tmac_size];
310-
gcry_error = gcry_mac_read(tmp_mac_hd,
311-
&tmac, // tag output
312-
(size_t *)&mac_size // tag size
313-
);
314-
if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR)
315-
{
316-
printf(KRED "ERROR: gcry_mac_read error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK);
317-
status = CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR;
318-
return status;
319-
}
320-
321-
printf("Calculated Mac Size: %d\n", *tmac_size);
322-
printf("Calculated MAC (full length):\n\t");
323-
for (uint32_t i = 0; i < *tmac_size; i++)
324-
{
325-
printf("%02X", tmac[i]);
326-
}
327-
printf("\nCalculated MAC (truncated to sa_ptr->stmacf_len):\n\t");
328-
for (uint32_t i = 0; i < mac_size; i++)
329-
{
330-
printf("%02X", tmac[i]);
331-
}
332-
printf("\n");
333-
334-
printf("Received MAC:\n\t");
335-
for (uint32_t i = 0; i < mac_size; i++)
336-
{
337-
printf("%02X", mac[i]);
338-
}
339-
printf("\n");
308+
// uint32_t *tmac_size = &mac_size;
309+
// uint8_t tmac[*tmac_size];
310+
// gcry_error = gcry_mac_read(tmp_mac_hd,
311+
// &tmac, // tag output
312+
// (size_t *)&mac_size // tag size
313+
// );
314+
// if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR)
315+
// {
316+
// printf(KRED "ERROR: gcry_mac_read error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK);
317+
// status = CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR;
318+
// return status;
319+
// }
320+
321+
// printf("Calculated Mac Size: %d\n", *tmac_size);
322+
// printf("Calculated MAC (full length):\n\t");
323+
// for (uint32_t i = 0; i < *tmac_size; i++)
324+
// {
325+
// printf("%02X", tmac[i]);
326+
// }
327+
// printf("\nCalculated MAC (truncated to sa_ptr->stmacf_len):\n\t");
328+
// for (uint32_t i = 0; i < mac_size; i++)
329+
// {
330+
// printf("%02X", tmac[i]);
331+
// }
332+
// printf("\n");
333+
334+
// printf("Received MAC:\n\t");
335+
// for (uint32_t i = 0; i < mac_size; i++)
336+
// {
337+
// printf("%02X", mac[i]);
338+
// }
339+
// printf("\n");
340340
#endif
341341

342342
// Compare computed mac with MAC in frame

test/unit/ut_aos_process.c

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ UTEST(AOS_PROCESS, AOS_SA_SEGFAULT_TEST)
18871887
memset(aos_frame, 0, (sizeof(uint8_t) * AOS_SIZE));
18881888

18891889
status = Crypto_AOS_ProcessSecurity((uint8_t *)framed_aos_b, framed_aos_len, aos_frame, &processed_aos_len);
1890-
ASSERT_EQ(CRYPTO_LIB_ERR_SPI_INDEX_OOB, status);
1890+
ASSERT_EQ(CRYPTO_LIB_ERR_INVALID_AOS_FRAME_LENGTH, status);
18911891

18921892
Crypto_Shutdown();
18931893
free(framed_aos_b);
@@ -2166,4 +2166,112 @@ UTEST(AOS_PROCESS, AOS_FHECF_TEST)
21662166
free(aos_frame);
21672167
}
21682168

2169+
UTEST(AOS_PROCESS, AOS_6BYTE_TEST)
2170+
{
2171+
remove("sa_save_file.bin");
2172+
// Local Variables
2173+
int32_t status = CRYPTO_LIB_SUCCESS;
2174+
2175+
uint16_t processed_aos_len;
2176+
2177+
// Configure Parameters
2178+
Crypto_Config_CryptoLib(KEY_TYPE_INTERNAL, MC_TYPE_INTERNAL, SA_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT,
2179+
IV_INTERNAL, CRYPTO_AOS_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR,
2180+
TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE,
2181+
AOS_CHECK_FECF_FALSE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE);
2182+
// AOS Test
2183+
GvcidManagedParameters_t AOS_UT_Managed_Parameters = {
2184+
1, 0x0003, 0, AOS_HAS_FECF, AOS_NO_FHEC, AOS_IZ_NA, 0, AOS_SEGMENT_HDRS_NA, 6, AOS_NO_OCF, 1};
2185+
Crypto_Config_Add_Gvcid_Managed_Parameters(AOS_UT_Managed_Parameters);
2186+
status = Crypto_Init();
2187+
2188+
// Test frame setup
2189+
char *framed_aos_h = "40C0FEDCBA98";
2190+
char *framed_aos_b = NULL;
2191+
int framed_aos_len = 0;
2192+
hex_conversion(framed_aos_h, &framed_aos_b, &framed_aos_len);
2193+
2194+
SecurityAssociation_t *sa_ptr = NULL;
2195+
SaInterface sa_if = get_sa_interface_inmemory();
2196+
sa_if->sa_get_from_spi(10, &sa_ptr); // Disable SPI 10
2197+
sa_ptr->sa_state = SA_KEYED;
2198+
sa_if->sa_get_from_spi(5, &sa_ptr); // Enable and setup 5
2199+
sa_ptr->sa_state = SA_OPERATIONAL;
2200+
sa_ptr->est = 1;
2201+
sa_ptr->ecs = CRYPTO_CIPHER_AES256_GCM;
2202+
sa_ptr->arsn_len = 0;
2203+
sa_ptr->shsnf_len = 0;
2204+
2205+
crypto_key_t *ekp = NULL;
2206+
ekp = key_if->get_key(sa_ptr->ekid);
2207+
ekp->key_state = KEY_ACTIVE;
2208+
2209+
AOS_t *aos_frame;
2210+
aos_frame = malloc(sizeof(uint8_t) * AOS_SIZE);
2211+
memset(aos_frame, 0, (sizeof(uint8_t) * AOS_SIZE));
2212+
2213+
status = Crypto_AOS_ProcessSecurity((uint8_t *)framed_aos_b, framed_aos_len, aos_frame, &processed_aos_len);
2214+
ASSERT_EQ(CRYPTO_LIB_ERR_INVALID_AOS_FRAME_LENGTH, status);
2215+
2216+
Crypto_aosPrint(aos_frame);
2217+
2218+
Crypto_Shutdown();
2219+
free(framed_aos_b);
2220+
free(aos_frame);
2221+
}
2222+
2223+
UTEST(AOS_PROCESS, AOS_8BYTE_TEST)
2224+
{
2225+
remove("sa_save_file.bin");
2226+
// Local Variables
2227+
int32_t status = CRYPTO_LIB_SUCCESS;
2228+
2229+
uint16_t processed_aos_len;
2230+
2231+
// Configure Parameters
2232+
Crypto_Config_CryptoLib(KEY_TYPE_INTERNAL, MC_TYPE_INTERNAL, SA_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT,
2233+
IV_INTERNAL, CRYPTO_AOS_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR,
2234+
TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE,
2235+
AOS_CHECK_FECF_FALSE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE);
2236+
// AOS Test
2237+
GvcidManagedParameters_t AOS_UT_Managed_Parameters = {
2238+
1, 0x0003, 0, AOS_HAS_FECF, AOS_NO_FHEC, AOS_IZ_NA, 0, AOS_SEGMENT_HDRS_NA, 8, AOS_NO_OCF, 1};
2239+
Crypto_Config_Add_Gvcid_Managed_Parameters(AOS_UT_Managed_Parameters);
2240+
status = Crypto_Init();
2241+
2242+
// Test frame setup
2243+
char *framed_aos_h = "40C0FEDCBA987605";
2244+
char *framed_aos_b = NULL;
2245+
int framed_aos_len = 0;
2246+
hex_conversion(framed_aos_h, &framed_aos_b, &framed_aos_len);
2247+
2248+
SecurityAssociation_t *sa_ptr = NULL;
2249+
SaInterface sa_if = get_sa_interface_inmemory();
2250+
sa_if->sa_get_from_spi(10, &sa_ptr); // Disable SPI 10
2251+
sa_ptr->sa_state = SA_KEYED;
2252+
sa_if->sa_get_from_spi(5, &sa_ptr); // Enable and setup 5
2253+
sa_ptr->sa_state = SA_OPERATIONAL;
2254+
sa_ptr->est = 1;
2255+
sa_ptr->ecs = CRYPTO_CIPHER_AES256_GCM;
2256+
sa_ptr->arsn_len = 0;
2257+
sa_ptr->shsnf_len = 0;
2258+
2259+
crypto_key_t *ekp = NULL;
2260+
ekp = key_if->get_key(sa_ptr->ekid);
2261+
ekp->key_state = KEY_ACTIVE;
2262+
2263+
AOS_t *aos_frame;
2264+
aos_frame = malloc(sizeof(uint8_t) * AOS_SIZE);
2265+
memset(aos_frame, 0, (sizeof(uint8_t) * AOS_SIZE));
2266+
2267+
status = Crypto_AOS_ProcessSecurity((uint8_t *)framed_aos_b, framed_aos_len, aos_frame, &processed_aos_len);
2268+
ASSERT_EQ(CRYPTO_LIB_ERR_INVALID_AOS_FRAME_LENGTH, status);
2269+
2270+
Crypto_aosPrint(aos_frame);
2271+
2272+
Crypto_Shutdown();
2273+
free(framed_aos_b);
2274+
free(aos_frame);
2275+
}
2276+
21692277
UTEST_MAIN();

0 commit comments

Comments
 (0)