Skip to main content

MTE Base Code Examples

Purpose

The objective of an MTE Base "Examples" is to provide short examples of common MTE Base actions.

MTE Setup

To use the MTE in an application there are some headers and/or code snippets that must be included in the project to pull in the native library as well as any wrapper files that have been created.

//----------------------------------------------------------------------
// There are two different options when setting up the MTE in CSharp
//----------------------------------------------------------------------
// Both of the following options require that the native library
// is also included in the solution
//----------------------------------------------------------------------
//-----------
// Option 1
//-----------

//-------------------------------------------------------------
// Include the Eclypses.MTE.Core NuGet package in the solution
//-------------------------------------------------------------

// Ensure each page where the MTE is referenced has this
using Eclypses.MTE;
//-----------
// Option 2
//-----------

//----------------------------------------------------------------
// Include ALL files found inside the MTE archive folder "/src/cs/"
//----------------------------------------------------------------

// Ensure each page where the MTE is referenced has this
using Eclypses.MTE;

MTE Initialization

The MTE initialization must be called before any other MTE function. For many of the languages the wrapper will take care of this for you. The exception to this is C and JavaScript. JavaScript requires that the WASM be initialized.

// Initialize MTE
if (!mte_init(NULL, NULL))
{
fputs("MTE init error.", stderr);
return -1;
}

MTE Version

The MTE requires both sides to use compatible versions. To check the version that is running simply call the MTE version method. Additionally, the version of the Language Interface must also match the version of the library, making it very important to update all language interface files when upgrading the MTE library.

// Create encoder or decoder as shown above
// Get version of MTE in a string
string mteVersion = mteEncoder.GetVersion();
// Get Mte major version number as an integer
int mteMajorVersion = mteEncoder.GetVersionMajor();
// Get MTE minor version number as an integer
int mteMinorVersion = mteEncoder.GetVersionMinor();
// Get MTE patch version number as an integer
int mtePatchVersion = mteEncoder.GetVersionPatch();
// Log MTE Version
logger.LogDebug($"MTE version is {mteMajorVersion}.{mteMinorVersion}.{mtePatchVersion}");

Initializing MTE License

A licensed version of the MTE must be initialized with the company name and license code. A licensed version of the library will have “-LIC” before the version number in the name of the package downloaded from the Eclypses developer’s portal. Below is a code sample:

// Create mte encoder or decoder
MteEnc mteEncoder = new MteEnc();

// Initialize the MTE license
if (!mteEncoder.InitLicense(appSettings.LicenseCompanyName, appSettings.LicenseKey)) {

// MTE cannot continue so this should be dealt with appropriately
// Set encoder status to a license error
_encoderStatus = MteStatus.mte_status_license_error;

// License error, Mte cannot continue handle appropriately
// Below is an example
Log.LogError ("MTE license appears to be invalid. MTE cannot be initalized ({0}): {1}. Press any key to end.",
mteEncoder.GetStatusName(_encoderStatus),
mteEncoder.GetStatusDescription(_encoderStatus));
return;
}

Interpreting the MTE Status

Most MTE methods return an MTE status which is meant to reflect if the MTE method was successful or not. If there is an error the status will help the developer to understand what went wrong. For a detailed explanation of what each status means please see the MTE Developer Guides.


// MTE status description where "status" is the MteStatus
string statusDescription = mteEncoder.GetStatusDescription(status);

// MTE status name
string StatusName = mteEncoder.GetStatusName(status);

Algorithm Name to String and Vice Versa

If settings are being passed into a program from a database or settings file one may need to convert the string name of an algorithm into the actual algorithm parameter or the algorithm parameter name into a string.

// Create default encoder or decoder
MteEnc mteEncoder = new MteEnc();

// Convert from string to Algorithm
MteDrbgs drbg = mteEncoder.GetDrbgsAlgo("mte_drbgs_ctr_aes128_df");
MteVerifiers verifier = mteEncoder.GetVerifiersAlgo("mte_verifiers_t64_crc32");
MteHashes hash = mteEncoder.GetHashesAlgo("mte_hashes_sha256");
MteCiphers cipher = mteEncoder.GetCiphersAlgo("mte_ciphers_aes256_ctr");

// Get Algorithm name in a string
string drbgStringName = mteEncoder.GetDrbgsName(drbg);
string verifierStringName = mteEncoder.GetVerifiersName(verifier);
string hashStringName = mteEncoder.GetHashesName(hash);
string cipherStringName = mteEncoder.GetCiphersName(cipher);

Option Min and Max values

Depending on which DRBG algorithm is selected the size of the entropy, personalization string and nonce byte length requirements can change. To get the byte length of these options needed for a given DRBG the following code can be used for and MTE Encoder, the exact same methods are available for an MTE Decoder as well.

// Create the Encoder with default options
MteEnc mteEncoder = new MteEnc();

// Get the minimum and maximum entropy byte length
int minEntropyBytes = mteEncoder.GetDrbgsEntropyMinBytes(mteEncoder.GetDrbg());
int maxEntropyBytes = mteEncoder.GetDrbgsEntropyMaxBytes(mteEncoder.GetDrbg());

// Get min and max personalization string byte length
int minPersonalBytes = mteEncoder.GetDrbgsPersonalMinBytes(mteEncoder.GetDrbg());
int maxPersonalBytes = mteEncoder.GetDrbgsPersonalMaxBytes(mteEncoder.GetDrbg());

// Get min and max nonce byte length
int minNonceBytes = mteEncoder.GetDrbgsNonceMinBytes(mteEncoder.GetDrbg());
int maxNonceBytes = mteEncoder.GetDrbgsNonceMaxBytes(mteEncoder.GetDrbg());