Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b99d4bd
[nasa/cryptolib#463] WIP: TC apply UT memory leak fix
Donnie-Ice May 6, 2025
78b6b09
[nasa/cryptolib#463] TC process UT memory leak fix
Donnie-Ice May 6, 2025
f34d2d8
[nasa/cryptolib#463] aos apply UT memory leaks
Donnie-Ice May 6, 2025
1d6ab9b
[nasa/cryptolib#463] aos process UT memory leaks, all HMAC failing
Donnie-Ice May 6, 2025
9bfd2e5
[nasa/cryptolib#463] tm apply ut memory leaks
Donnie-Ice May 6, 2025
7446fbf
[nasa/cryptolib#463] tm process ut memory leaks
Donnie-Ice May 6, 2025
22b8b81
[nasa/cryptolib#463] tm process ut memory leaks, HMAC failing
Donnie-Ice May 6, 2025
4ffc920
[nasa/cryptolib#463] gcm_siv ut memory leaks fix
Donnie-Ice May 6, 2025
e6d790a
[nasa/cryptolib#463] tc_apply ut memory leaks fix
Donnie-Ice May 6, 2025
69b3dda
[nasa/cryptolib#463] remove HMAC function with memory leak, fix accio…
Donnie-Ice May 6, 2025
dd77c02
style: auto-format via clang-format
Donnie-Ice May 6, 2025
39fa08d
[nasa/cryptolib#463] Resolve rest of memory leaks
Donnie-Ice May 8, 2025
ef26f7e
style: auto-format via clang-format
Donnie-Ice May 8, 2025
1e86718
[nasa/cryptolib#463] remove merge conflict markers
Donnie-Ice May 8, 2025
e81dcf9
[nasa/cryptolib#463] merge conflicts
Donnie-Ice May 8, 2025
d29f0a8
style: auto-format via clang-format
Donnie-Ice May 8, 2025
44aba85
[nasa/cryptolib#463] EP memory leaks fixed
Donnie-Ice May 9, 2025
f9a96dd
Merge branch '463-resolve-memory-leaks-in-uts' of github.com:nasa/Cry…
Donnie-Ice May 9, 2025
9d5ff8e
style: auto-format via clang-format
Donnie-Ice May 9, 2025
93ae9c6
[nasa/cryptolib#463] Double-free fix
Donnie-Ice May 9, 2025
1f18d2b
Merge branch '463-resolve-memory-leaks-in-uts' of github.com:nasa/Cry…
Donnie-Ice May 9, 2025
c830a67
style: auto-format via clang-format
Donnie-Ice May 9, 2025
89b3158
[nasa/cryptolib#463] Update rocky build to check memory errors
Donnie-Ice May 9, 2025
2536e58
[nasa/cryptolib#463] Update rocky build to check memory errors (2)
Donnie-Ice May 9, 2025
9b55328
[nasa/cryptolib#463] update codeql docker tag
Donnie-Ice May 9, 2025
bd1db0e
[nasa/cryptolib#463] remove -fsanitize=address build flag, codeql fai…
Donnie-Ice May 9, 2025
97427af
[nasa/cryptolib#463] Added fhecf back in
Donnie-Ice May 9, 2025
9ab484c
style: auto-format via clang-format
Donnie-Ice May 9, 2025
b64e5c7
[nasa/cryptolib#463] Fix length miscalculation
Donnie-Ice Jun 3, 2025
7b4cfeb
Merge branch '463-resolve-memory-leaks-in-uts' of github.com:nasa/Cry…
Donnie-Ice Jun 3, 2025
0af10b0
[nasa/cryptolib#463] Fix uts after length calc fix
Donnie-Ice Jun 3, 2025
6d5d6a1
style: auto-format via clang-format
Donnie-Ice Jun 3, 2025
869fae0
Merge branch 'dev' into 463-resolve-memory-leaks-in-uts
Donnie-Ice Jun 23, 2025
478c31f
[nasa/cryptolib#463] Fix unused frees after dev merge
Donnie-Ice Jun 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ jobs:
- name: Update
run: yum update -y
- name: Install Dependencies
run: yum install -y --enablerepo=devel python3-pip python3-devel epel-release libcurl-devel git cmake gcc java-11-openjdk-devel openssl wget bzip2 ldconfig mariadb-devel mariadb-common mariadb-connector-c mariadb-connector-c-config mariadb-errmsg mariadb-gssapi-server
run: yum install -y --enablerepo=devel python3-pip python3-devel epel-release libcurl-devel git cmake gcc java-11-openjdk-devel openssl wget bzip2 ldconfig mariadb-devel mariadb-common mariadb-connector-c mariadb-connector-c-config mariadb-errmsg mariadb-gssapi-server libasan
# Might want to trim this down, but these dependencies should work for KMC
- name: install lcov
run: yum install -y --enablerepo=epel lcov
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
name: Analyze Build_Internal
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
container:
image: ivvitc/cryptolib:20250102
image: ivvitc/cryptolib:20250108
permissions:
# required for all workflows
security-events: write
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
name: Analyze Build_Minimal
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
container:
image: ivvitc/cryptolib:20250102
image: ivvitc/cryptolib:20250108
permissions:
# required for all workflows
security-events: write
Expand Down Expand Up @@ -126,7 +126,7 @@ jobs:
name: Analyze Build_Wolf
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
container:
image: ivvitc/cryptolib:20250102
image: ivvitc/cryptolib:20250108
permissions:
# required for all workflows
security-events: write
Expand Down Expand Up @@ -204,7 +204,7 @@ jobs:
name: Analyze Build_RHEL
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
container:
image: ivvitc/cryptolib:20250102
image: ivvitc/cryptolib:20250108
permissions:
# required for all workflows
security-events: write
Expand Down Expand Up @@ -256,7 +256,7 @@ jobs:
name: Analyze Build_EP
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
container:
image: ivvitc/cryptolib:20250102
image: ivvitc/cryptolib:20250108
permissions:
# required for all workflows
security-events: write
Expand Down
3 changes: 1 addition & 2 deletions include/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ extern uint32_t crc32Table[CRC32TBL_SIZE];
extern uint16_t crc16Table[CRC16TBL_SIZE];

// GF(2^4) field and logarithm tables
static const uint8_t gf_exp[30] = {1, 2, 4, 8, 3, 6, 12, 11, 5, 10, 7, 14, 15, 13, 9,
1, 2, 4, 8, 3, 6, 12, 11, 5, 10, 7, 14, 15, 13, 9};
static const uint8_t gf_exp[15] = {1, 2, 4, 8, 3, 6, 12, 11, 5, 10, 7, 14, 15, 13, 9};

static const uint8_t gf_log[GF_SIZE] = {0, 0, 1, 4, 2, 8, 5, 10, 3, 14, 9, 7, 6, 13, 11, 12};

Expand Down
7 changes: 5 additions & 2 deletions src/core/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,11 @@ uint16_t Crypto_Calc_FHECF(uint8_t *data)
memset(parity, 0, RS_PARITY);
for (i = 0; i < RS_DATA; i++)
{
feedback = data[i] ^ parity[0];
memcpy(&parity[0], &parity[1], RS_PARITY - 1);
feedback = (data[i] ^ parity[0]) % (GF_SIZE - 1);
for (j = 0; j < RS_PARITY - 1; j++)
{
parity[j] = parity[j + 1];
}
parity[RS_PARITY - 1] = 0;

for (j = 0; j < RS_PARITY; j++)
Expand Down
4 changes: 4 additions & 0 deletions src/core/crypto_aos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,14 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8
{
status = CRYPTO_LIB_ERR_KEY_ID_ERROR;
mc_if->mc_log(status);
free(p_new_dec_frame);
return status;
}
if (ekp->key_state != KEY_ACTIVE)
{
status = CRYPTO_LIB_ERR_KEY_STATE_INVALID;
mc_if->mc_log(status);
free(p_new_dec_frame);
return status;
}
}
Expand All @@ -1214,12 +1216,14 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8
{
status = CRYPTO_LIB_ERR_KEY_ID_ERROR;
mc_if->mc_log(status);
free(p_new_dec_frame);
return status;
}
if (akp->key_state != KEY_ACTIVE)
{
status = CRYPTO_LIB_ERR_KEY_STATE_INVALID;
mc_if->mc_log(status);
free(p_new_dec_frame);
return status;
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/core/crypto_tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ int32_t Crypto_TC_Frame_Validation(uint16_t *p_enc_frame_len)
printf(KRED "Error: New frame would violate maximum tc frame managed parameter! \n" RESET);
status = CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_MANAGED_PARAM_MAX_LIMIT;
mc_if->mc_log(status);
printf("STATUS=%d\n", status);
return status;
}
// Ensure the frame to be created will not violate spec max length
Expand All @@ -261,6 +260,7 @@ int32_t Crypto_TC_Frame_Validation(uint16_t *p_enc_frame_len)
printf(KRED "Error: New frame would violate specification max TC frame size! \n" RESET);
status = CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT;
mc_if->mc_log(status);
return status;
}
return status;
}
Expand Down Expand Up @@ -399,12 +399,14 @@ int32_t Crypto_TC_Encrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_ptr
{
status = CRYPTO_LIB_ERR_KEY_ID_ERROR;
mc_if->mc_log(status);
free(p_new_enc_frame);
return status;
}
if (ekp->key_state != KEY_ACTIVE)
{
status = CRYPTO_LIB_ERR_KEY_STATE_INVALID;
mc_if->mc_log(status);
free(p_new_enc_frame);
return status;
}
}
Expand All @@ -418,12 +420,14 @@ int32_t Crypto_TC_Encrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_ptr
{
status = CRYPTO_LIB_ERR_KEY_ID_ERROR;
mc_if->mc_log(status);
free(p_new_enc_frame);
return status;
}
if (akp->key_state != KEY_ACTIVE)
{
status = CRYPTO_LIB_ERR_KEY_STATE_INVALID;
mc_if->mc_log(status);
free(p_new_enc_frame);
return status;
}
}
Expand Down Expand Up @@ -681,7 +685,6 @@ int32_t Crypto_TC_Do_Encrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_
aad, ecs_is_aead_algorithm, index_p, p_in_frame, cam_cookies, pkcs_padding);
if (status != CRYPTO_LIB_SUCCESS)
{
Crypto_TC_Safe_Free_Ptr(*aad);
mc_if->mc_log(status);
return status;
}
Expand Down Expand Up @@ -1110,11 +1113,11 @@ int32_t Crypto_TC_ApplySecurity_Cam(const uint8_t *p_in_frame, const uint16_t in
// Determine if segment header exists and FECF exists
uint8_t segment_hdr_len = TC_SEGMENT_HDR_SIZE;
uint8_t fecf_len = FECF_SIZE;
uint8_t ocf_len = TELEMETRY_FRAME_OCF_CLCW_SIZE;
uint8_t ocf_len = OCF_SIZE;
Crypto_TC_Calc_Lengths(&fecf_len, &segment_hdr_len, &ocf_len);

// Calculate tf_payload length here to be used in other logic
int16_t payload_calc = temp_tc_header.fl - TC_FRAME_HEADER_SIZE - segment_hdr_len - fecf_len + 1;
int16_t payload_calc = (temp_tc_header.fl + 1) - TC_FRAME_HEADER_SIZE - segment_hdr_len - ocf_len - fecf_len;
// check if payload length underflows
if (payload_calc < 0)
{
Expand All @@ -1140,8 +1143,8 @@ int32_t Crypto_TC_ApplySecurity_Cam(const uint8_t *p_in_frame, const uint16_t in
*/

// Calculate frame lengths based on SA fields
*p_enc_frame_len =
temp_tc_header.fl + 1 + 2 + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + sa_ptr->stmacf_len;
*p_enc_frame_len = temp_tc_header.fl + 1 + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len +
sa_ptr->stmacf_len + ocf_len;
new_enc_frame_header_field_length = (*p_enc_frame_len) - 1;

// Finalize frame setup
Expand Down Expand Up @@ -1330,13 +1333,13 @@ int32_t Crypto_TC_Parse_Check_FECF(uint8_t *ingest, int *len_ingest, TC_t *tc_sd
// Calculate our own
uint16_t calculated_fecf = Crypto_Calc_FECF(ingest, *len_ingest - 2);
// Compare
if (received_fecf != calculated_fecf)
{
#ifdef DEBUG
printf("Received FECF is 0x%04X\n", received_fecf);
printf("Calculated FECF is 0x%04X\n", calculated_fecf);
printf("FECF was Calced over %d bytes\n", *len_ingest - 2);
printf("Received FECF is 0x%04X\n", received_fecf);
printf("Calculated FECF is 0x%04X\n", calculated_fecf);
printf("FECF was Calced over %d bytes\n", *len_ingest - 2);
#endif
if (received_fecf != calculated_fecf)
{
status = CRYPTO_LIB_ERR_INVALID_FECF;
mc_if->mc_log(status);
}
Expand Down
15 changes: 11 additions & 4 deletions src/core/crypto_tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ int32_t Crypto_TM_ApplySecurity(uint8_t *pTfBuffer, uint16_t len_ingest)
printf("Actual secondary header length: %d\n", secondary_hdr_len);
#endif
// Only validate SHVN if secondary header is present
if (idx > secondary_hdr_start && shvn > 3) // SHVN is 2 bits, so max value is 3
if (idx > secondary_hdr_start && shvn > 0) // SHVN is 2 bits, 0b00 is the only allowed value
{
status = CRYPTO_LIB_ERR_TM_SECONDARY_HDR_VN;
mc_if->mc_log(status);
Expand Down Expand Up @@ -957,9 +957,10 @@ int32_t Crypto_TM_ApplySecurity(uint8_t *pTfBuffer, uint16_t len_ingest)
**/

// Set SPI
pTfBuffer[idx] = ((sa_ptr->spi & 0xFF00) >> 8);
pTfBuffer[idx + 1] = (sa_ptr->spi & 0x00FF);
idx += 2;
pTfBuffer[idx] = (uint8_t)(sa_ptr->spi >> 8);
idx++;
pTfBuffer[idx] = (sa_ptr->spi & 0xFF);
idx++;

// Set initialization vector if specified
status = Crypto_TM_IV_Sanity_Check(&sa_service_type, sa_ptr);
Expand Down Expand Up @@ -1784,6 +1785,11 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8_

// Get Key
status = Crypto_TM_Get_Keys(&ekp, &akp, sa_ptr);
if (status != CRYPTO_LIB_SUCCESS)
{
free(p_new_dec_frame);
return status;
}
}

if (status == CRYPTO_LIB_SUCCESS)
Expand All @@ -1800,6 +1806,7 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8_
#ifdef DEBUG
printf(KRED "Error: SA Not Operational \n" RESET);
#endif
free(p_new_dec_frame);
return CRYPTO_LIB_ERR_SA_NOT_OPERATIONAL;
}

Expand Down
53 changes: 26 additions & 27 deletions src/crypto/libgcrypt/cryptography_interface_libgcrypt.template.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,33 +304,32 @@ static int32_t cryptography_validate_authentication(uint8_t *data_out, size_t le
}

#ifdef MAC_DEBUG
uint32_t *tmac_size = &mac_size;
uint8_t *tmac = calloc(1, *tmac_size);
gcry_error = gcry_mac_read(tmp_mac_hd,
tmac, // tag output
(size_t *)tmac_size // tag size
);
if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR)
{
printf(KRED "ERROR: gcry_mac_read error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK);
status = CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR;
return status;
}

printf("Calculated Mac Size: %d\n", *tmac_size);
printf("Calculated MAC (full length):\n\t");
for (uint32_t i = 0; i < *tmac_size; i++)
{
printf("%02X", tmac[i]);
}
printf("\nCalculated MAC (truncated to sa_ptr->stmacf_len):\n\t");
for (uint32_t i = 0; i < mac_size; i++)
{
printf("%02X", tmac[i]);
}
printf("\n");
if (!tmac)
free(tmac);
// Commented out due to memory leaks with HMAC
// uint32_t *tmac_size = &mac_size;
// uint8_t tmac[*tmac_size];
// gcry_error = gcry_mac_read(tmp_mac_hd,
// &tmac, // tag output
// (size_t*)&mac_size // tag size
// );
// if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR)
// {
// printf(KRED "ERROR: gcry_mac_read error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK);
// status = CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR;
// return status;
// }

// printf("Calculated Mac Size: %d\n", *tmac_size);
// printf("Calculated MAC (full length):\n\t");
// for (uint32_t i = 0; i < *tmac_size; i++)
// {
// printf("%02X", tmac[i]);
// }
// printf("\nCalculated MAC (truncated to sa_ptr->stmacf_len):\n\t");
// for (uint32_t i = 0; i < mac_size; i++)
// {
// printf("%02X", tmac[i]);
// }
// printf("\n");

printf("Received MAC:\n\t");
for (uint32_t i = 0; i < mac_size; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/sa/internal/sa_interface_inmemory.template.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ void sa_populate(void)
sa[9].ast = 0;
sa[9].shivf_len = 12;
sa[9].iv_len = 12;
sa[9].shsnf_len = 2;
sa[9].shsnf_len = 0;
sa[9].arsnw = 5;
sa[9].arsnw_len = 1;
sa[9].arsn_len = 2;
sa[9].arsn_len = 0;
sa[9].gvcid_blk.tfvn = 0x01;
sa[9].gvcid_blk.scid = SCID & 0x3FF;
sa[9].gvcid_blk.vcid = 0;
Expand Down
12 changes: 8 additions & 4 deletions support/fuzz/scripts/build-fuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ fi
echo "🔨 Compiling CryptoLib without ASan..."
rm -rf build
mkdir -p build/fuzz && cd build/fuzz
cmake $PROJECT_ROOT -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
cmake $PROJECT_ROOT -B $PROJECT_ROOT/build/fuzz \
-DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
-DCMAKE_C_FLAGS="$OPT_FLAGS" \
-DCMAKE_CXX_FLAGS="$OPT_FLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="-flto" \
Expand All @@ -77,7 +78,8 @@ cd ..
echo "🔨 Compiling CryptoLib with ASan..."
rm -rf fuzz-asan
mkdir fuzz-asan && cd fuzz-asan
cmake $PROJECT_ROOT -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
cmake $PROJECT_ROOT -B $PROJECT_ROOT/build/fuzz-asan \
-DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
-DCMAKE_C_FLAGS="-fsanitize=address $OPT_FLAGS" \
-DCMAKE_CXX_FLAGS="-fsanitize=address $OPT_FLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address -flto" \
Expand All @@ -95,7 +97,8 @@ echo "🔨 Compiling CryptoLib with CmpLog instrumentation..."
rm -rf fuzz-cmplog
mkdir fuzz-cmplog && cd fuzz-cmplog
export AFL_LLVM_CMPLOG=1 # Enable CmpLog instrumentation
cmake $PROJECT_ROOT -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
cmake $PROJECT_ROOT -B $PROJECT_ROOT/build/fuzz-cmplog \
-DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
-DCMAKE_C_FLAGS="$OPT_FLAGS" \
-DCMAKE_CXX_FLAGS="$OPT_FLAGS" \
-DCRYPTO_LIBGCRYPT=ON \
Expand All @@ -113,7 +116,8 @@ echo "🔨 Compiling CryptoLib with CompCov (laf-intel) instrumentation..."
rm -rf fuzz-compcov
mkdir fuzz-compcov && cd fuzz-compcov
export AFL_LLVM_LAF_ALL=1 # Enable CompCov instrumentation
cmake $PROJECT_ROOT -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
cmake $PROJECT_ROOT -B $PROJECT_ROOT/build/fuzz-compcov \
-DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX \
-DCMAKE_C_FLAGS="$OPT_FLAGS" \
-DCMAKE_CXX_FLAGS="$OPT_FLAGS" \
-DCRYPTO_LIBGCRYPT=ON \
Expand Down
9 changes: 9 additions & 0 deletions support/fuzz/scripts/fuzz-auto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd $SCRIPT_DIR
./build-fuzz.sh
cd ..
python3 generate_corpus.py
cd ./scripts
./run-fuzz-multithreaded.sh
Loading