Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb_mbedtls::MbedTlsWrapper::AESStateMBEDTLS Class Reference
Inheritance diagram for duckdb_mbedtls::MbedTlsWrapper::AESStateMBEDTLS:
Collaboration diagram for duckdb_mbedtls::MbedTlsWrapper::AESStateMBEDTLS:

Public Member Functions

DUCKDB_API AESStateMBEDTLS (duckdb::unique_ptr< duckdb::EncryptionStateMetadata > metadata)
DUCKDB_API void InitializeEncryption (duckdb::EncryptionNonce &nonce, duckdb::const_data_ptr_t key, duckdb::const_data_ptr_t aad, duckdb::idx_t aad_len) override
DUCKDB_API void InitializeDecryption (duckdb::EncryptionNonce &nonce, duckdb::const_data_ptr_t key, duckdb::const_data_ptr_t aad, duckdb::idx_t aad_len) override
DUCKDB_API size_t Process (duckdb::const_data_ptr_t in, duckdb::idx_t in_len, duckdb::data_ptr_t out, duckdb::idx_t out_len) override
DUCKDB_API size_t Finalize (duckdb::data_ptr_t out, duckdb::idx_t out_len, duckdb::data_ptr_t tag, duckdb::idx_t tag_len) override
DUCKDB_API void GenerateRandomData (duckdb::data_ptr_t data, duckdb::idx_t len) override
DUCKDB_API void FinalizeGCM (duckdb::data_ptr_t tag, duckdb::idx_t tag_len)
DUCKDB_API const mbedtls_cipher_info_t * GetCipher ()
void ForceMbedTLSUnsafe ()
void UndoForceMbedTLSUnsafe ()
Public Member Functions inherited from duckdb::EncryptionState
DUCKDB_API EncryptionState (unique_ptr< EncryptionStateMetadata > metadata)
EncryptionTypes::CipherType GetCipher () const

Static Public Member Functions

static DUCKDB_API void GenerateRandomDataStatic (duckdb::data_ptr_t data, duckdb::idx_t len)
static DUCKDB_API void SecureClearData (duckdb::data_ptr_t data, duckdb::idx_t len)

Private Member Functions

DUCKDB_API void InitializeInternal (duckdb::EncryptionNonce &nonce, duckdb::const_data_ptr_t aad, duckdb::idx_t aad_len)
DUCKDB_API void GenerateRandomDataInsecure (duckdb::data_ptr_t data, duckdb::idx_t len)

Private Attributes

duckdb::EncryptionTypes::Mode mode
duckdb::unique_ptr< mbedtls_cipher_context_t > context
bool force_mbedtls = false

Additional Inherited Members

Public Attributes inherited from duckdb::EncryptionState
unique_ptr< EncryptionStateMetadata > metadata

Constructor & Destructor Documentation

◆ AESStateMBEDTLS()

MbedTlsWrapper::AESStateMBEDTLS::AESStateMBEDTLS ( duckdb::unique_ptr< duckdb::EncryptionStateMetadata > metadata)
explicit
597814 : EncryptionState(std::move(metadata_p)), context(duckdb::make_uniq<mbedtls_cipher_context_t>()) {
597815 mbedtls_cipher_init(context.get());
597816
597817 auto cipher_info = GetCipher();
597818
597819 if (!cipher_info) {
597820 throw runtime_error("Failed to get Cipher");
597821 }
597822
597823 if (mbedtls_cipher_setup(context.get(), cipher_info)) {
597824 throw runtime_error("Failed to initialize cipher context");
597825 }
597826
597827 if (metadata->GetCipher() == duckdb::EncryptionTypes::CBC && mbedtls_cipher_set_padding_mode(context.get(), MBEDTLS_PADDING_PKCS7)) {
597828 throw runtime_error("Failed to set CBC padding");
597829
597830 }
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
This function prepares a cipher context for use with the given cipher primitive.
Definition duckdb.cpp:570333
@ MBEDTLS_PADDING_PKCS7
Definition duckdb.cpp:567870
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
This function initializes a ctx as NONE.
Definition duckdb.cpp:570289
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode)
This function sets the padding mode, for cipher modes that use padding.
Definition duckdb.cpp:571215

◆ ~AESStateMBEDTLS()

MbedTlsWrapper::AESStateMBEDTLS::~AESStateMBEDTLS ( )
override
597833 {
597834 if (context) {
597835 mbedtls_cipher_free(context.get());
597836 }
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
This function frees and clears the cipher-specific context of ctx. Freeing ctx itself remains the res...
Definition duckdb.cpp:570294

Member Function Documentation

◆ InitializeEncryption()

void MbedTlsWrapper::AESStateMBEDTLS::InitializeEncryption ( duckdb::EncryptionNonce & nonce,
duckdb::const_data_ptr_t key,
duckdb::const_data_ptr_t aad,
duckdb::idx_t aad_len )
overridevirtual

Reimplemented from duckdb::EncryptionState.

597875 {
597876 mode = duckdb::EncryptionTypes::ENCRYPT;
597877
597878 if (mbedtls_cipher_setkey(context.get(), key, metadata->GetKeyLen() * 8, MBEDTLS_ENCRYPT) != 0) {
597879 throw std::runtime_error("Failed to set AES key for encryption");
597880 }
597881
597882 InitializeInternal(nonce, aad, aad_len);
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, int key_bitlen, const mbedtls_operation_t operation)
This function sets the key to use with the given context.
Definition duckdb.cpp:570390

◆ InitializeDecryption()

void MbedTlsWrapper::AESStateMBEDTLS::InitializeDecryption ( duckdb::EncryptionNonce & nonce,
duckdb::const_data_ptr_t key,
duckdb::const_data_ptr_t aad,
duckdb::idx_t aad_len )
overridevirtual

Reimplemented from duckdb::EncryptionState.

597885 {
597886 mode = duckdb::EncryptionTypes::DECRYPT;
597887
597888 if (mbedtls_cipher_setkey(context.get(), key, metadata->GetKeyLen() * 8, MBEDTLS_DECRYPT)) {
597889 throw runtime_error("Failed to set AES key for encryption");
597890 }
597891
597892 InitializeInternal(nonce, aad, aad_len);

◆ Process()

size_t MbedTlsWrapper::AESStateMBEDTLS::Process ( duckdb::const_data_ptr_t in,
duckdb::idx_t in_len,
duckdb::data_ptr_t out,
duckdb::idx_t out_len )
overridevirtual

Reimplemented from duckdb::EncryptionState.

597896 {
597897
597898 // GCM works in-place, CTR and CBC don't
597899 auto use_out_copy = in == out && metadata->GetCipher() != CipherType::GCM;
597900
597901 auto out_ptr = out;
597902 std::unique_ptr<duckdb::data_t[]> out_copy;
597903 if (use_out_copy) {
597904 out_copy.reset(new duckdb::data_t[out_len]);
597905 out_ptr = out_copy.get();
597906 }
597907
597908 size_t out_len_res = duckdb::NumericCast<size_t>(out_len);
597909 if (mbedtls_cipher_update(context.get(), reinterpret_cast<const unsigned char *>(in), in_len, out_ptr,
597910 &out_len_res)) {
597911 throw runtime_error("Encryption or Decryption failed at Process");
597912 };
597913
597914 if (use_out_copy) {
597915 memcpy(out, out_ptr, out_len_res);
597916 }
597917 return out_len_res;
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
The generic cipher update function. It encrypts or decrypts using the given cipher context....
Definition duckdb.cpp:570667
uint8_t data_t
data pointers
Definition duckdb.hpp:246

◆ Finalize()

size_t MbedTlsWrapper::AESStateMBEDTLS::Finalize ( duckdb::data_ptr_t out,
duckdb::idx_t out_len,
duckdb::data_ptr_t tag,
duckdb::idx_t tag_len )
overridevirtual

Reimplemented from duckdb::EncryptionState.

597945 {
597946 size_t result = out_len;
597947 if (mbedtls_cipher_finish(context.get(), out, &result)) {
597948 throw runtime_error("Encryption or Decryption failed at Finalize");
597949 }
597950 if (metadata->GetCipher() == duckdb::EncryptionTypes::GCM) {
597951 FinalizeGCM(tag, tag_len);
597952 }
597953 return result;
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, unsigned char *output, size_t *olen)
The generic cipher finalization function. If data still needs to be flushed from an incomplete block,...
Definition duckdb.cpp:571107

◆ GenerateRandomData()

void MbedTlsWrapper::AESStateMBEDTLS::GenerateRandomData ( duckdb::data_ptr_t data,
duckdb::idx_t len )
overridevirtual

Reimplemented from duckdb::EncryptionState.

597858 {
597859 // generate insecure random data
597860 GenerateRandomDataInsecure(data, len);

◆ FinalizeGCM()

void MbedTlsWrapper::AESStateMBEDTLS::FinalizeGCM ( duckdb::data_ptr_t tag,
duckdb::idx_t tag_len )
597920 {
597921
597922 switch (mode) {
597923
597924 case duckdb::EncryptionTypes::ENCRYPT: {
597925 if (mbedtls_cipher_write_tag(context.get(), tag, tag_len)) {
597926 throw runtime_error("Writing tag failed");
597927 }
597928 break;
597929 }
597930
597931 case duckdb::EncryptionTypes::DECRYPT: {
597932 if (mbedtls_cipher_check_tag(context.get(), tag, tag_len)) {
597933 throw duckdb::InvalidInputException(
597934 "Computed AES tag differs from read AES tag, are you using the right key?");
597935 }
597936 break;
597937 }
597938
597939 default:
597940 throw duckdb::InternalException("Unhandled encryption mode %d", static_cast<int>(mode));
597941 }
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
This function writes a tag for AEAD ciphers. Currently supported with GCM and ChaCha20+Poly1305....
Definition duckdb.cpp:571275
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
This function checks the tag for AEAD ciphers. Currently supported with GCM and ChaCha20+Poly1305....
Definition duckdb.cpp:571321

◆ GetCipher()

const mbedtls_cipher_info_t * MbedTlsWrapper::AESStateMBEDTLS::GetCipher ( )
597770 {
597771 switch(metadata->GetCipher()) {
597772 case CipherType::GCM:
597773 switch (metadata->GetKeyLen()) {
597774 case 16:
597776 case 24:
597778 case 32:
597780 default:
597781 throw runtime_error("Invalid AES key length for GCM");
597782 }
597783 case CipherType::CTR:
597784 switch (metadata->GetKeyLen()) {
597785 case 16:
597787 case 24:
597789 case 32:
597791 default:
597792 throw runtime_error("Invalid AES key length for CTR");
597793 }
597794 case CipherType::CBC:
597795 switch (metadata->GetKeyLen()) {
597796 case 16:
597798 case 24:
597800 case 32:
597802 default:
597803 throw runtime_error("Invalid AES key length for CBC");
597804 }
597805 default:
597806 throw duckdb::InternalException("Invalid Encryption/Decryption Cipher: %s", duckdb::EncryptionTypes::CipherToString(metadata->GetCipher()));
597807 }
@ MBEDTLS_CIPHER_AES_128_CBC
Definition duckdb.cpp:567769
@ MBEDTLS_CIPHER_AES_192_GCM
Definition duckdb.cpp:567779
@ MBEDTLS_CIPHER_AES_256_CTR
Definition duckdb.cpp:567777
@ MBEDTLS_CIPHER_AES_128_CTR
Definition duckdb.cpp:567775
@ MBEDTLS_CIPHER_AES_256_GCM
Definition duckdb.cpp:567780
@ MBEDTLS_CIPHER_AES_128_GCM
Definition duckdb.cpp:567778
@ MBEDTLS_CIPHER_AES_192_CBC
Definition duckdb.cpp:567770
@ MBEDTLS_CIPHER_AES_192_CTR
Definition duckdb.cpp:567776
@ MBEDTLS_CIPHER_AES_256_CBC
Definition duckdb.cpp:567771
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
This function retrieves the cipher-information structure associated with the given cipher type.
Definition duckdb.cpp:570173

◆ SecureClearData()

void MbedTlsWrapper::AESStateMBEDTLS::SecureClearData ( duckdb::data_ptr_t data,
duckdb::idx_t len )
static
597810 {
597811 mbedtls_platform_zeroize(data, len);
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
Definition duckdb.cpp:592129

◆ ForceMbedTLSUnsafe()

void duckdb_mbedtls::MbedTlsWrapper::AESStateMBEDTLS::ForceMbedTLSUnsafe ( )
inline
50522 {
50523 force_mbedtls = true;
50524 }

◆ UndoForceMbedTLSUnsafe()

void duckdb_mbedtls::MbedTlsWrapper::AESStateMBEDTLS::UndoForceMbedTLSUnsafe ( )
inline
50526 {
50527 force_mbedtls = false;
50528 }

◆ InitializeInternal()

void MbedTlsWrapper::AESStateMBEDTLS::InitializeInternal ( duckdb::EncryptionNonce & nonce,
duckdb::const_data_ptr_t aad,
duckdb::idx_t aad_len )
private
597863 {
597864 if (mbedtls_cipher_set_iv(context.get(), nonce.data(), nonce.total_size())) {
597865 throw runtime_error("Failed to set IV for encryption");
597866 }
597867
597868 if (aad_len > 0) {
597869 if (mbedtls_cipher_update_ad(context.get(), aad, aad_len)) {
597870 throw std::runtime_error("Failed to set AAD");
597871 }
597872 }
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
This function sets the initialization vector (IV) or nonce.
Definition duckdb.cpp:570500
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
This function adds additional data for AEAD ciphers. Currently supported with GCM and ChaCha20+Poly13...
Definition duckdb.cpp:570619

◆ GenerateRandomDataInsecure()

void MbedTlsWrapper::AESStateMBEDTLS::GenerateRandomDataInsecure ( duckdb::data_ptr_t data,
duckdb::idx_t len )
private
597839 {
597840 if (!force_mbedtls) {
597841 // To use this insecure MbedTLS random number generator
597842 // we double check if force_mbedtls_unsafe is set
597843 // such that we do not accidentaly opt-in
597844 throw duckdb::InternalException("Insecure random generation called without setting 'force_mbedtls_unsafe' = true");
597845 }
597846
597847 duckdb::RandomEngine random_engine;
597848
597849 while (len != 0) {
597850 const auto random_integer = random_engine.NextRandomInteger();
597851 const auto next = duckdb::MinValue<duckdb::idx_t>(len, sizeof(random_integer));
597852 memcpy(data, duckdb::const_data_ptr_cast(&random_integer), next);
597853 data += next;
597854 len -= next;
597855 }

The documentation for this class was generated from the following file: