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.
- C
- C++
- CSharp
- Java
- JavaScript
- Swift
- Python
- Go
- PHP
//--------------------------------------------------------
// Add the path to the "include" directory of the SDK in the
// header search path.
// Each header file needed will neet to be referenced.
//--------------------------------------------------------
#include "mte_base.h"
#include "mte_status.h"
#include "mte_enc.h"
#include "mte_dec.h"
// Include the following relevant headers if using the MKE add-on.
#include "mte_mke_enc.h"
#include "mte_mke_dec.h"
// Include the following relevant header if using the FLEN add-on.
#include "mte_flen_enc.h"
#include "MteBase.h"
#include "mte_status.h"
#include "MteEnc.h"
#include "MteDec.h"
// Include the following relevant headers if using the MKE add-on.
#include "MteMkeEnc.h"
#include "MteMkeDec.h"
// Include the following relevant header if using the FLEN add-on.
#include "MteFlenEnc.h"
//----------------------------------------------------------------------
// 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;
//--------------------------------------------------------
// Include the "com.eclypses.mte" package in the solution
// This can be found in the MTE archive folder "src/java"
//--------------------------------------------------------
// The solution must also include a path to the native
// library itself in the Build Path configuration
//--------------------------------------------------------
// Each page that references the MTE must have the following
//--------------------------------------------------------
import com.eclypses.mte.*;
// import the entire MTE library
import MTELib from "mte-wasm-browser";
// OR, import individual modules
import { MteWasm, MteBase, MteEnc, MteDec } from "mte-wasm-browser";
/*
This is a template bridging header that contains all necessary header
includes. If your project has an existing bridging header, you can copy
the relevant includes to that header. If your project does not have a
bridging header, you can uncomment the relevant lines and use this file
directly.
Note that the headers listed here include other headers, so the entire SDK
include directory should be kept in tact. Directly including these is enough
to get the associated Swift classes to build. You should uncomment only the
includes associated with the classes you need, and only include the classes
you really need in your project. There is a derivation chain, so you need to
include all base classes in your project as well.
*/
// Base (MteBase) includes. These are required to use any part of MTE.
#include "mte_init.h"
#include "mte_license.h"
#include "mte_version.h"
#include "mte_base.h"
#include "mte_wrap_base.h"
// Core decoder (MteDec).
#include "mte_dec.h"
#include "mte_wrap_dec.h"
// Core encoder (MteEnc).
#include "mte_enc.h"
#include "mte_wrap_enc.h"
// Fixed-Length Add-On encoder (MteFlenEnc).
#include "mte_flen_enc.h"
#include "mte_wrap_flen_enc.h"
// Managed-Key Encryption Add-On decoder (MteMkeDec).
#include "mte_mke_dec.h"
#include "mte_wrap_mke_dec.h"
// Managed-Key Encryption Add-On encoder (MteMkeEnc).
#include "mte_mke_enc.h"
#include "mte_wrap_mke_enc.h"
# Initialize MTE Core libraries.
from MteBase import MteBase
from MteStatus import MteStatus
from MteEnc import MteEnc
from MteDec import MteDec
from MteDrbgs import MteDrbgs
from MteVerifiers import MteVerifiers
from MteHashes import MteHashes
from MteCiphers import MteCiphers
# Import the following relevant libraries if you are using the MKE or FLEN add-ons.
from MteMkeEnc import MteMkeEnc
from MteMkeDec import MteMkeDec
from MteFlenEnc import MteFlenEnc
/*
Create a directory named "mte" in the main directory.
Add the contents of the “src/go” directory from the
mte-Windows package or mte-Linux package into the newly
created mte directory.
Add the corresponding "lib" directory and contents
to the mte directory as well.
Add the following to the import statement at the top of the file
where you need to use the MTE
*"moduleName" is the name of your go module
*/
import (
"moduleName/mte"
)
//--------------------------------------------------------
// The mtephp Extension must be installed on your version
// of PHP. PHP7 and PHP8 are supported.
//
// For instructions on how to install the mtephp
// extension please see PHP Extension ReadMe File
//--------------------------------------------------------
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.
- C
- JavaScript
// Initialize MTE
if (!mte_init(NULL, NULL))
{
fputs("MTE init error.", stderr);
return -1;
}
import { MteWasm } from "mte-wasm-browser";
const mteWasm = new MteWasm();
await mteWasm.instantiate();
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.
- C
- C++
- CSharp
- Java
- JavaScript
- Swift
- Python
- Go
- PHP
// Get version of MTE we are using.
const char* mte_version = mte_base_version();
printf("%s\n", mte_version);
// There are macros available when including
// the header "mte_version.h"
// Get version of MTE in a char pointer.
const char* version = MTE_VERSION;
printf("%s\n", version);
// Get MTE major version number as an integer.
int mte_major_version = MTE_VERSION_MAJOR;
printf("%d\n", mte_major_version);
// Get MTE minor version number as an integer
int mte_minor_version = MTE_VERSION_MINOR;
printf("%d\n", mte_minor_version);
// Get MTE patch version number as an integer
int mte_patch_version = MTE_VERSION_PATCH;
printf("%d\n", mte_patch_version);
// Get version of MTE we are using.
const char* mteVersion = MteBase::getVersion();
std::cout << mteVersion << std::endl;
// Get MTE major version number as size_t.
const size_t mteMajorVersion = MteBase::getVersionMajor();
std::cout << mteMajorVersion << std::endl;
// Get MTE minor version number as size_t.
const size_t mteMinorVersion = MteBase::getVersionMinor();
std::cout << mteMinorVersion << std::endl;
// Get MTE patch version number as size_t.
const size_t mtePatchVersion = MteBase::getVersionPatch();
std::cout << mtePatchVersion << std::endl;
// 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}");
// Get version of MTE we are using
String mteVersion = MteBase.getVersion();
// Get Mte major version number as an integer
int mteMajorVersion = MteBase.getVersionMajor();
// Get MTE minor version number as an integer
int mteMinorVersion = MteBase.getVersionMinor();
// Get MTE patch version number as an integer
int mtePatchVersion = MteBase.getVersionPatch();
// Log MTE Major version
System.out.print("MTE version is " + mteMajorVersion + "." + mteMinorVersion + "." + mtePatchVersion);
import { MteWasm, MteBase, MteDrbgs } from "mte-wasm-browser";
// instantiate wasm
const mteWasm = new MteWasm();
await mteWasm.instantiate();
// create instance of MteBase
const mteBase = new MteBase(mteWasm, MteDrbgs.mte_drbgs_none);
// log MTE version
const version = mteBase.getVersion();
console.log(`MTE Version is: ${version}`);
// Log MTE major version
const major = mteBase.getVersionMajor();
console.log(major);
// Log MTE minor version
const minor = mteBase.getVersionMinor();
console.log(minor);
// Log MTE patch version
const patch = mteBase.getVersionPatch();
console.log(patch);
// Get version of MTE we are using
let mteVersion: String = MteBase.getVersion()
print(mteVersion)
// Get Mte major version number as an integer
let mteMajorVersion: Int = MteBase.getVersionMajor()
print(mteMajorVersion)
// Get MTE minor version number as an integer
let mteMinorVersion: Int = MteBase.getVersionMinor()
print(mteMinorVersion)
// Get MTE patch version number as an integer
let mtePatchVersion: Int = MteBase.getVersionPatch()
print(mtePatchVersion)
# Get version of MTE in a string.
mte_version = MteBase.get_version()
# Get Mte major version number as an integer.
mte_major_version = MteBase.get_version_major()
# Get Mte minor version number as an integer.
mte_minor_version = MteBase.get_version_minor()
# Get Mte patch version number as an integer.
mte_patch_version = MteBase.get_version_patch()
// Get MTE version
mteVersion := mte.GetVersion()
// Get MTE major version number
mteMajorVersion := mte.GetVersionMajor()
// Get MTE minor version number
mteMinorVersion := mte.GetVersionMinor()
// Get MTE patch version number
mtePatchVersion := mte.GetVersionPatch()
// print out MTE version
fmt.Printf("Using MTE Version %s\n", mteVersion)
<?php
$version = mte_get_version();
$major = mte_get_version_major();
$minor = mte_get_version_minor();
$patch = mte_get_version_patch();
echo "MTE Version: " .$version. " ";
echo "MTE Major: " .$major. " ";
echo "MTE Minor: " .$minor. " ";
echo "MTE Patch: " .$patch. " ";
?>
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:
- C
- C++
- CSharp
- Java
- JavaScript
- Python
- Swift
- Go
- PHP
/* Initialize MTE license. If a license code is not required (e.g., trial
mode), this can be skipped. This demo attempts to load the license info
from the environment if required. */
if (!mte_license_init("YOUR_COMPANY", "YOUR_LICENSE"))
{
company = getenv("MTE_COMPANY");
license = getenv("MTE_LICENSE");
if (company == NULL || license == NULL ||
!mte_license_init(company, license))
{
fprintf(stderr, "License init error (%s): %s\n",
mte_base_status_name(mte_status_license_error),
mte_base_status_description(mte_status_license_error));
return mte_status_license_error;
}
}
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license info
// from the environment if required.
if (!MteBase::initLicense("YOUR_COMPANY", "YOUR_LICENSE"))
{
const char *company = getenv("MTE_COMPANY");
const char *license = getenv("MTE_LICENSE");
if (company == NULL || license == NULL ||
!MteBase::initLicense(company, license))
{
std::cerr << "License init error ("
<< MteBase::getStatusName(mte_status_license_error)
<< "): "
<< MteBase::getStatusDescription(mte_status_license_error)
<< std::endl;
return mte_status_license_error;
}
}
// 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;
}
// Initialize the MTE license
if (!MteBase.initLicense(LicenseCompanyName, LicenseKey)) {
// MTE cannot continue so this should be dealt with appropriately
// Set encoder status to a license error
_encoderStatus = MteStatus.mte_status_license_error;
// Build error
System.out.print("MTE license appears to be invalid. MTE cannot be initalized (" + MteBase.getStatusName(_encoderStatus) + ": "
+ MteBase.getStatusDescription(_encoderStatus) + ". Press any key to end");
return;
}
import { MteWasm, MteBase, MteDrbgs } from "mte-wasm-browser";
// instantiate wasm
const mteWasm = new MteWasm();
await mteWasm.instantiate();
// create instance of MteBase
const mteBase = new MteBase(mteWasm, MteDrbgs.mte_drbgs_none);
// initialize MTE license
const initLicenseResult = mteBase.initLicense(licenseCompany, licenseKey);
if (!initLicenseResult) {
const licenseStatus = MteStatus.mte_status_license_error;
const status = mteBase.getStatusName(licenseStatus);
const message = mteBase.getStatusDescription(licenseStatus);
throw new Error(
`MTE license appears to be invalid. MTE cannot be initalized.\n${status}: ${message}`,
);
}
# Initialize the MTE license.
init_license_result = MteBase.init_license(license_company, license_key)
if not init_license_result:
# MTE cannot continue so this should be dealt with appropriately.
# Set encoder status to a license error.
encoder_status = MteStatus.mte_status_license_error
# License error, Mte cannot continue handle appropriately.
# Below is an example.
(status,message) = MteBase.get_status_name(encoder_status),
MteBase.get_status_description(encoder_status)
print("MTE license appears to be invalid. MTE cannot be initalized. ({0}): {1}. Press any key to end".format(
status,
message),
file=sys.stderr)
input()
exit(1)
// Initialize the MTE license
if (!MteBase.initLicense(licenseCompanyName, licenseKey)) {
// MTE cannot continue so this should be dealt with appropriately
print("MTE license appears to be invalid. MTE cannot be initalized")
}
// Initialize the MTE license
if !mte.InitLicense(companyName, companyLicense) {
fmt.Println("MTE license appears to be invalid. MTE cannot be initalized.")
return
}
<?php
define("COMPANY", "companyName");
define("LICENSE", "companyLicense");
// Initialize the MTE license
if(!mte_license_init(COMPANY, LICENSE)){
echo "MTE license appears to be invalid. MTE cannot be initalized.";
return;
}
?>