BSL v1.1.1 - 39.g9f43410
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
Data.c File Reference

Implementation of the data containers for handling variable-sized buffers and ownership. More...

#include "Data.h"
#include "bsl/BPSecLib_Private.h"
#include <string.h>
+ Include dependency graph for Data.c:

Functions

static void bsl_data_int_reset (BSL_Data_t *data)
 
static void bsl_data_int_free (BSL_Data_t *data)
 
void BSL_Data_Init (BSL_Data_t *data)
 Initialize an empty data struct.
 
int BSL_Data_InitBuffer (BSL_Data_t *data, size_t bytelen)
 Initialize with a dynamically-allocated owned buffer of size bytelen *.
 
void BSL_Data_InitView (BSL_Data_t *data, size_t len, const BSL_DataPtr_t src)
 Initialize a data struct as an overlay on optional external data.
 
void BSL_Data_InitMove (BSL_Data_t *data, BSL_Data_t *src)
 Initialize a data struct with move semantics from an existing struct.
 
void BSL_Data_Deinit (BSL_Data_t *data)
 De-initialize a data struct, freeing if necessary.
 
int BSL_Data_CopyFrom (BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src)
 Set an initialized data struct to a given size.
 
int BSL_Data_Resize (BSL_Data_t *data, size_t len)
 Resize the data, copying if necessary.
 
int BSL_Data_AppendFrom (BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src)
 Append an initialized data struct with a given size.
 
int BSL_Data_SetViewCstr (BSL_Data_t *data, const char *cstr)
 Set a data struct to be a view onto an existing null-terminated string.
 
int BSL_Data_Cmp (const BSL_Data_t *lhs, const BSL_Data_t *rhs)
 Compare two data values returning 0 if equal, or -1 or 1 if not equal.
 

Detailed Description

Implementation of the data containers for handling variable-sized buffers and ownership.

Function Documentation

◆ BSL_Data_AppendFrom()

int BSL_Data_AppendFrom ( BSL_Data_t data,
size_t  len,
BSL_DataConstPtr_t  src 
)

Append an initialized data struct with a given size.

Parameters
[in,out]dataThe data to copy into, which must not be NULL.
[in]lenThe total length to allocate, which may be non-zero.
[in]srcAn optional source buffer to copy from, from which len bytes will be copied.
Returns
Zero upon success.

References ASSERT_ARG_NONNULL, BSL_Data_Resize(), BSL_SUCCESS, CHK_ARG_EXPR, CHK_ARG_NONNULL, BSL_Data_t::len, and BSL_Data_t::ptr.

Referenced by BSL_Crypto_UnwrapKey(), BSL_Crypto_WrapKey(), and MockBPA_Agent_Exec().

◆ BSL_Data_Cmp()

int BSL_Data_Cmp ( const BSL_Data_t lhs,
const BSL_Data_t rhs 
)

Compare two data values returning 0 if equal, or -1 or 1 if not equal.

Warning
This comparison is not constant time, see BSL_Crypto_compare() for that need.
Parameters
[in]lhsThe left side to compare.
[in]rhsThe right side to compare.
Returns
Zero if equal, -1 if lhs is shorter or 1 if longer, and the output of memcmp() if same length.

References BSL_Data_t::len, and BSL_Data_t::ptr.

Referenced by BSL_TestUtils_IsB16StrEqualTo(), and BSLX_CoseSc_GetAndValidateKey().

◆ BSL_Data_CopyFrom()

int BSL_Data_CopyFrom ( BSL_Data_t data,
size_t  len,
BSL_DataConstPtr_t  src 
)

Set an initialized data struct to a given size.

Parameters
[in,out]dataThe data to copy into, which must not be NULL.
[in]lenThe total length to allocate, which may be non-zero.
[in]srcAn optional source buffer to copy from, from which len bytes will be copied.
Returns
Zero upon success.

References ASSERT_ARG_NONNULL, bsl_data_int_reset(), BSL_Data_Resize(), BSL_LOG_ERR, BSL_SUCCESS, BSL_Data_t::owned, and BSL_Data_t::ptr.

Referenced by BSL_Crypto_LoadKey(), bsl_mock_decode_eid(), BSLX_CoseMsg_Headers_Decode(), BSLX_CoseMsg_Mac0_Decode(), BSLX_CoseMsg_Mac_Decode(), BSLX_CoseMsg_Recipient_Decode(), BSLX_CoseSc_GenerateNonce(), BSLX_CoseSc_GetAndValidateAddlHeaders(), BSLX_CoseSc_GetAndValidateKey(), BSLX_CoseSc_GetOptions(), TEST_CASE(), and test_mock_bpa_ctr_decode_invalid().

◆ BSL_Data_Deinit()

void BSL_Data_Deinit ( BSL_Data_t data)

De-initialize a data struct, freeing if necessary.

Parameters
[in,out]dataThe data to de-initialize, which must not be NULL.
Returns
Zero upon success.
Postcondition
The struct must be initialized before using again.

References ASSERT_ARG_NONNULL, and bsl_data_int_free().

Referenced by BSL_AbsSecBlock_Decode(), BSL_API_QuerySecurity(), BSL_AuthCtx_Deinit(), BSL_CBOR_EncodeEID(), BSL_Cipher_Deinit(), BSL_CryptoKey_Deinit(), BSL_ExecAnyVerifierAcceptor_Pre(), BSL_IdValPair_Print(), bsl_mock_decode_eid_from_ctx(), bsl_mock_eid_deinit(), BSL_TestUtils_IsB16StrEqualTo(), BSL_TestUtils_PrintHexToBuffer(), BSLP_PolicyOptions_SC3(), BSLX_BCB_ComputeAAD(), BSLX_BCB_Deinit(), BSLX_BCB_Execute(), BSLX_BCB_GetOptions(), BSLX_BIB_Deinit(), BSLX_BIB_Execute(), BSLX_BIB_GenIPPT(), BSLX_BIB_InitFromSecOper(), BSLX_CoseMsg_Headers_Deinit(), BSLX_CoseMsg_Mac0_Deinit(), BSLX_CoseMsg_Mac_Deinit(), BSLX_CoseMsg_Recipient_Deinit(), BSLX_CoseSc_AddAadScope(), BSLX_CoseSc_Deinit(), BSLX_CoseSc_Encrypt0_Source(), BSLX_CoseSc_Encrypt0_VerifyAccept(), BSLX_CoseSc_Encrypt_Compute(), BSLX_CoseSc_Encrypt_Source(), BSLX_CoseSc_Encrypt_VerifyAccept(), BSLX_CoseSc_ExternalAad_Chunked(), BSLX_CoseSc_GetOptions(), BSLX_CoseSc_HkdfContentKey(), BSLX_CoseSc_Mac0_Source(), BSLX_CoseSc_Mac0_VerifyAccept(), BSLX_CoseSc_Mac_Source(), BSLX_CoseSc_Mac_VerifyAccept(), ccsds_mac_rng(), cose_exA_5_rng(), cose_exA_6_rng(), Encode_ASB(), LLVMFuzzerTestOneInput(), mock_bpa_ctr_deinit(), mock_bpa_key_registry_init_jwk(), MockBPA_Bundle_Deinit(), setUp(), test_AppendixA_Example1_BIB_Source(), test_AppendixA_Example1_BIB_VerifyAccept(), test_AppendixA_Example4_BCB_Source(), test_AppendixA_Example4_BCB_VerifyAccept(), test_AppendixA_Example5_BCB_Source(), test_AppendixA_Example5_BCB_VerifyAccept(), test_AppendixA_Example6_BCB_Source(), test_bsl_loopback_eid(), test_bsl_mock_encode_bundle(), test_bsl_mock_encode_canonical(), test_bsl_mock_encode_primary(), test_BSL_TextUtil_Base16_Decode_invalid(), test_BSL_TextUtil_Base16_Decode_valid(), test_BSL_TextUtil_Base16_Encode(), test_BSL_TextUtil_Base64_Decode_invalid(), test_BSL_TextUtil_Base64_Decode_valid(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), test_CCSDS_Example_Mac_Source(), test_CCSDS_Example_Mac_VerifyAccept(), test_CoseSc_InvalidOptions_Verifier(), test_crypto_generate_iv(), test_decrypt(), test_encrypt(), test_mock_bpa_crc_crc16(), test_mock_bpa_crc_crc32c(), test_mock_bpa_ctr_decode_invalid(), test_qcbor_decode_array_nested(), test_qcbor_decode_map_sequential(), test_qcbor_decode_only_head(), test_qcbor_decode_with_head(), test_qcbor_decode_without_head(), test_RFC9173_AppendixA_Example1_BIB_Source(), test_RFC9173_AppendixA_Example2_BCB_Acceptor(), test_RFC9173_AppendixA_Example2_BCB_Source(), test_sec_accept_keyunwrap(), test_sec_source_keywrap(), and TestASBDecodeEncodeClosure().

◆ BSL_Data_Init()

void BSL_Data_Init ( BSL_Data_t data)

Initialize an empty data struct.

Parameters
[in,out]dataThe data to initialize, which must not be NULL.
See also
BSL_DATA_INIT_NULL

References ASSERT_ARG_NONNULL, and bsl_data_int_reset().

Referenced by BSL_CBOR_EncodeEID(), BSL_CryptoKey_Init(), bsl_mock_decode_eid(), BSL_TestUtils_IsB16StrEqualTo(), BSLP_PolicyOptions_SC3(), BSLX_BCB_Init(), BSLX_BIB_InitFromSecOper(), BSLX_CoseMsg_Headers_Init(), BSLX_CoseMsg_Mac0_Init(), BSLX_CoseMsg_Mac_Init(), BSLX_CoseMsg_Recipient_Init(), BSLX_CoseSc_AddAadScope(), BSLX_CoseSc_Encrypt0_Source(), BSLX_CoseSc_Encrypt0_VerifyAccept(), BSLX_CoseSc_Encrypt_Compute(), BSLX_CoseSc_Encrypt_Source(), BSLX_CoseSc_Encrypt_VerifyAccept(), BSLX_CoseSc_ExternalAad_Chunked(), BSLX_CoseSc_GenerateIV(), BSLX_CoseSc_GetOptions(), BSLX_CoseSc_HkdfContentKey(), BSLX_CoseSc_Init(), BSLX_CoseSc_Mac0_Source(), BSLX_CoseSc_Mac0_VerifyAccept(), BSLX_CoseSc_Mac_Source(), BSLX_CoseSc_Mac_VerifyAccept(), ccsds_mac_rng(), cose_exA_5_rng(), cose_exA_6_rng(), Encode_ASB(), LLVMFuzzerTestOneInput(), mock_bpa_ctr_init(), mock_bpa_key_registry_init_jwk(), MockBPA_Bundle_Init(), setUp(), test_AppendixA_Example1_BIB_Source(), test_AppendixA_Example1_BIB_VerifyAccept(), test_AppendixA_Example4_BCB_Source(), test_AppendixA_Example4_BCB_VerifyAccept(), test_AppendixA_Example5_BCB_Source(), test_AppendixA_Example5_BCB_VerifyAccept(), test_AppendixA_Example6_BCB_Source(), test_bsl_loopback_eid(), test_bsl_mock_encode_bundle(), test_bsl_mock_encode_canonical(), test_bsl_mock_encode_primary(), test_BSL_TextUtil_Base16_Decode_invalid(), test_BSL_TextUtil_Base16_Decode_valid(), test_BSL_TextUtil_Base16_Encode(), test_BSL_TextUtil_Base64_Decode_invalid(), test_BSL_TextUtil_Base64_Decode_valid(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), test_CCSDS_Example_Mac_Source(), test_CCSDS_Example_Mac_VerifyAccept(), test_CoseSc_InvalidOptions_Verifier(), test_encrypt(), test_mock_bpa_crc_crc16(), test_mock_bpa_crc_crc32c(), test_mock_bpa_ctr_decode_invalid(), test_qcbor_decode_array_nested(), test_qcbor_decode_map_sequential(), test_qcbor_decode_only_head(), test_qcbor_decode_with_head(), test_qcbor_decode_without_head(), test_sec_accept_keyunwrap(), and test_sec_source_keywrap().

◆ BSL_Data_InitBuffer()

int BSL_Data_InitBuffer ( BSL_Data_t data,
size_t  bytelen 
)

Initialize with a dynamically-allocated owned buffer of size bytelen *.

Parameters
[in,out]dataThe data to initialize.
[in]bytelenLength of buffer to dynamically allocate.
Returns
Zero upon success.

References ASSERT_ARG_NONNULL, bsl_data_int_reset(), BSL_malloc(), BSL_SUCCESS, CHK_ARG_EXPR, CHK_POSTCONDITION, BSL_Data_t::len, BSL_Data_t::owned, and BSL_Data_t::ptr.

Referenced by BSL_API_QuerySecurity(), BSL_AuthCtx_Init(), BSL_Cipher_Init(), BSL_ExecAnyVerifierAcceptor_Pre(), BSLX_BIB_Execute(), BSLX_BIB_GenIPPT(), TEST_CASE(), test_crypto_generate_iv(), test_decrypt(), test_encrypt(), and TestASBDecodeEncodeClosure().

◆ BSL_Data_InitMove()

void BSL_Data_InitMove ( BSL_Data_t data,
BSL_Data_t src 
)

Initialize a data struct with move semantics from an existing struct.

References ASSERT_ARG_NONNULL, and bsl_data_int_reset().

◆ BSL_Data_InitView()

void BSL_Data_InitView ( BSL_Data_t data,
size_t  len,
BSL_DataPtr_t  src 
)

◆ bsl_data_int_free()

static void bsl_data_int_free ( BSL_Data_t data)
static

◆ bsl_data_int_reset()

◆ BSL_Data_Resize()

◆ BSL_Data_SetViewCstr()

int BSL_Data_SetViewCstr ( BSL_Data_t data,
const char *  cstr 
)

Set a data struct to be a view onto an existing null-terminated string.

Parameters
[in,out]dataThe data to create a view from, which must not be NULL.
[in]cstrThe C string pointer to create a view on, which must not be NULL.
Returns
Zero upon success.

References ASSERT_ARG_NONNULL, BSL_DATA_INIT_VIEW_CSTR, bsl_data_int_free(), and BSL_SUCCESS.

Referenced by BSLX_BCB_GetOptions(), and BSLX_BIB_InitFromSecOper().