encoder_creation
- C
- C++
- CSharp
- Java
- Swift
- ObjC
- Python
- Go
- TypeScript
MTE_SIZE_T i;
/* Status. */
mte_status status;
/* Inputs. */
static const char* inputs[] =
{
"message 0",
"message 1",
"message 2",
"message 3"
};
MTE_SIZE_T input_bytes = (MTE_SIZE_T)strlen(inputs[0]);
/* Personalization string. */
static const char personal[] = "demo";
/* Options. */
mte_enc_init_info e_info = MTE_ENC_INIT_INFO_INIT(
MTE_DRBG_ENUM, MTE_TOKBYTES, MTE_VERIFIERS_ENUM, NULL, NULL);
mte_dec_init_info d_info = MTE_DEC_INIT_INFO_INIT(
MTE_DRBG_ENUM, MTE_TOKBYTES, MTE_VERIFIERS_ENUM, 1, 0, NULL, NULL);
mte_drbg_inst_info i_info =
{ &ei_cb, NULL, &n_cb, NULL, personal, sizeof(personal) - 1 };
/* Encoder. */
char* encodings[sizeof(inputs) / sizeof(inputs[0])];
MTE_HANDLE encoder = MTE_ALLOCA(mte_enc_state_bytes(&e_info));
mte_enc_args e_args = MTE_ENC_ARGS_INIT(NULL, 0, NULL, &t_cb, NULL);
/* Decoder. */
char* decoded;
MTE_UINT8_T* dsaved;
MTE_HANDLE decoderA = MTE_ALLOCA(mte_dec_state_bytes(&d_info));
mte_dec_args d_args = MTE_DEC_ARGS_INIT(NULL, 0, NULL, &t_cb, NULL);
/* Licensing. */
const char* company;
const char* license;
/* Suppress unused args. */
(void)argc;
(void)argv;
/* Initialize MTE. */
if (!mte_init(NULL, NULL))
{
fputs("MTE init error.", stderr);
return -1;
}
/* 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;
}
}
/* Create the encoder. */
status = mte_enc_state_init(encoder, &e_info);
if (status != mte_status_success)
{
fprintf(stderr, "Encoder init error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
status = mte_enc_instantiate(encoder, &i_info);
if (status != mte_status_success)
{
fprintf(stderr, "Encoder instantiate error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
/* Encode the inputs. */
for (i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i)
{
encodings[i] = MTE_ALLOCA(mte_enc_buff_bytes_b64(encoder, input_bytes));
MTE_SET_ENC_IO(e_args, inputs[i], input_bytes, encodings[i]);
status = mte_enc_encode_b64(encoder, &e_args);
if (status != mte_status_success)
{
fprintf(stderr, "Encode error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
encodings[i] = e_args.encoded;
printf("Encode #%u: %s -> %s\n", (unsigned)i, inputs[i], encodings[i]);
}
// Status.
mte_status status;
// Inputs.
static const std::string inputs[] =
{
"message 0",
"message 1",
"message 2",
"message 3"
};
// Personalization string.
static const std::string personal("demo");
// 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 the encoder.
MteEnc encoder;
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
size_t entropyBytes = MteBase::getDrbgsEntropyMinBytes(encoder.getDrbg());
uint8_t *entropy = new uint8_t[entropyBytes];
memset(entropy, 0, entropyBytes);
// Instantiate the encoder.
encoder.setEntropy(entropy, entropyBytes);
encoder.setNonce(0);
status = encoder.instantiate(personal);
if (status != mte_status_success)
{
std::cerr << "Encoder instantiate error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
// Encode the inputs.
std::vector<std::string> encodings;
for (size_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i)
{
const char *encoded = encoder.encodeB64(inputs[i], status);
if (status != mte_status_success)
{
std::cerr << "Encode error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
encodings.push_back(encoded);
std::cout << "Encode #" << i << ": " << inputs[i]
<< " -> " << encoded << std::endl;
}
// Status.
MteStatus status;
// Inputs.
string[] inputs = {
"message 1",
"message 2",
"message 3",
"message 4"
};
// Personalization string.
string personal = "demo";
// 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.
MteBase baseObj = new MteBase();
if (!baseObj.InitLicense("YOUR_COMPANY", "YOUR_LICENSE")) {
string company = Environment.GetEnvironmentVariable("MTE_COMPANY");
string license = Environment.GetEnvironmentVariable("MTE_LICENSE");
if (company == null || license == null ||
!baseObj.InitLicense(company, license)) {
status = MteStatus.mte_status_license_error;
Console.Error.WriteLine("License init error ({0}): {1}",
baseObj.GetStatusName(status),
baseObj.GetStatusDescription(status));
return (int)status;
}
}
// Create the Encoder.
MteEnc encoder = new MteEnc();
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
int entropyBytes = encoder.GetDrbgsEntropyMinBytes(encoder.GetDrbg());
byte[] entropy = new byte[entropyBytes];
// Instantiate the Encoder.
encoder.SetEntropy(entropy);
encoder.SetNonce(0);
status = encoder.Instantiate(personal);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Encoder instantiate error ({0}): {1}",
encoder.GetStatusName(status),
encoder.GetStatusDescription(status));
return (int)status;
}
// Encode the inputs.
string[] encodings = new string[inputs.Length];
for (int i = 0; i < inputs.Length; ++i) {
encodings[i] = encoder.EncodeB64(inputs[i], out status);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Encode error ({0}): {1}",
encoder.GetStatusName(status),
encoder.GetStatusDescription(status));
return (int)status;
}
Console.WriteLine("Encode #{0}: {1} -> {2}",
i,
inputs[i],
encodings[i]);
}
// Status.
MteStatus status;
// Inputs.
String[] inputs =
{
"message 1",
"message 2",
"message 3",
"message 4"
};
// Personalization string.
String personal = "demo";
// 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"))
{
String company = System.getenv("MTE_COMPANY");
String license = System.getenv("MTE_LICENSE");
if (company == null || license == null ||
!MteBase.initLicense(company, license))
{
status = MteStatus.mte_status_license_error;
System.err.println("Encode error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
}
// Create the Encoder.
MteEnc encoder = new MteEnc();
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
int entropyBytes = MteBase.getDrbgsEntropyMinBytes(encoder.getDrbg());
byte[] entropy = new byte[entropyBytes];
// Instantiate the Encoder.
encoder.setEntropy(entropy);
encoder.setNonce(0);
status = encoder.instantiate(personal);
if (status != MteStatus.mte_status_success)
{
System.err.println("Encoder instantiate error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
// Encode the inputs.
String[] encodings = new String[inputs.length];
for (int i = 0; i < inputs.length; ++i)
{
MteBase.StrStatus encoded = encoder.encodeB64(inputs[i]);
if (encoded.status != MteStatus.mte_status_success)
{
System.err.println("Encode error ("+
MteBase.getStatusName(encoded.status)+"): "+
MteBase.getStatusDescription(encoded.status));
System.exit(encoded.status.getValue());
}
encodings[i] = encoded.str;
System.out.printf("Encode #%d: %s -> %s\n", i, inputs[i], encodings[i]);
}
import Foundation;
// The timestamp window is hard coded for this demo.
let timestampWindow: UInt64 = 1
// Status.
var status: mte_status
// Inputs.
let inputs = [
"message 0",
"message 1",
"message 2",
"message 3"
]
// Personalization string.
let personal = "demo"
// 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") {
let company = ProcessInfo.processInfo.environment["MTE_COMPANY"]
let license = ProcessInfo.processInfo.environment["MTE_LICENSE"];
if company == nil || license == nil ||
!MteBase.initLicense(company!, license!) {
status = mte_status_license_error
print("License init error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
exit(Int32(status.rawValue))
}
}
// Create the encoder.
let encoder = try! MteEnc()
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
let entropyBytes = MteBase.getDrbgsEntropyMinBytes(encoder.getDrbg())
var entropy = [UInt8](repeating: 0, count: entropyBytes)
// Instantiate the encoder.
encoder.setEntropy(&entropy)
encoder.setNonce(0)
status = encoder.instantiate(personal)
if status != mte_status_success {
print("Encoder instantiate error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
exit(Int32(status.rawValue))
}
// Encode the inputs.
var encoded: String
var encodings = [String]()
for i in 0...inputs.count-1 {
(encoded, status) = encoder.encodeB64(inputs[i])
if status != mte_status_success {
print("Encode error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
exit(Int32(status.rawValue))
}
encodings.append(encoded)
print("Encode #\(i): \(inputs[i]) -> \(encoded)")
}
// Status.
mte_status status;
// Autorelease pool.
@autoreleasepool {
puts("****** Simple MTE Sequencing Console Demo ******");
// Inputs.
NSString *inputs[] =
{
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:"message 0"]),
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:"message 1"]),
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:"message 2"]),
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:"message 3"])
};
// Personalization string.
NSString *personal =
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:"demo"]);
// 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" code:@"YOUR_LICENSE"])
{
const char *company = getenv("MTE_COMPANY");
const char *license = getenv("MTE_LICENSE");
if (company == NULL || license == NULL ||
![MteBase initLicense:
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:company])
code:
MTE_AUTORELEASE([[NSString alloc] initWithUTF8String:license])])
{
status = mte_status_license_error;
fprintf(stderr, "License init error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
}
// Create the encoder.
MteEnc *encoder = MTE_AUTORELEASE([[MteEnc alloc] init]);
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
size_t entropyBytes = [MteBase getDrbgsEntropyMinBytes:[encoder getDrbg]];
uint8_t *entropy = calloc(entropyBytes, sizeof(uint8_t));
// Instantiate the encoder.
[encoder setEntropy:entropy bytes:entropyBytes];
[encoder setNonce:0];
status = [encoder instantiate:personal];
if (status != mte_status_success)
{
fprintf(stderr, "Encoder instantiate error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
// Encode the inputs.
NSString *encodings[sizeof(inputs) / sizeof(inputs[0])];
for (unsigned i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i)
{
encodings[i] = [encoder encodeB64:inputs[i] status:&status];
if (status != mte_status_success)
{
fprintf(stderr, "Encode error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
printf("Encode #%u: %s -> %s\n",
i,
[inputs[i] UTF8String],
[encodings[i] UTF8String]);
}
free(entropy);
}
# Import relevant modules for the program.
from MteStatus import MteStatus
from MteBase import MteBase
from MteEnc import MteEnc
from MteDec import MteDec
import os
import sys
# Inputs.
inputs = [
"message 0",
"message 1",
"message 2",
"message 3"
]
# Personalization string.
personal_str = "demo"
# 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 not MteBase.init_license("YOUR_COMPANY", "YOUR_LICENSE"):
company = os.getenv("MTE_COMPANY")
license = os.getenv("MTE_LICENSE")
if company is None or \
license is None or \
not MteBase.init_license(company, license):
print("License init error ({0}): {1}".format(
MteBase.get_status_name(MteStatus.mte_status_license_error),
MteBase.get_status_description(MteStatus.mte_status_license_error)),
file=sys.stderr)
return MteStatus.mte_status_license_error.value
# Create the Encoder.
encoder = MteEnc.fromdefault()
# Create all-zero entropy for this demo. The nonce will also be set to zero.
# This should never be done in real applications.
entropy_bytes = MteBase.get_drbgs_entropy_min_bytes(encoder.get_drbg())
entropy = bytes(entropy_bytes)
# Instantiate the Encoder.
encoder.set_entropy(entropy)
encoder.set_nonce(0)
encoder_status = encoder.instantiate(personal_str)
if encoder_status != MteStatus.mte_status_success:
(status,message) = MteBase.get_status_name(encoder_status),\
MteBase.get_status_description(encoder_status)
print("Encoder instantiate error ({0}): {1}".format(\
status,message),file=sys.stderr)
return encoder_status.value
# Encode the inputs.
encodings = []
for i in range(len(inputs)):
(encoded_message, encoder_status) = encoder.encode_b64(inputs[i])
if encoder_status != MteStatus.mte_status_success:
(status,message) = MteBase.get_status_name(encoder_status),\
MteBase.get_status_description(encoder_status)
print("Encode error ({0}): {1}".format(status,message),\
file=sys.stderr)
return encoder_status.value
encodings.append(encoded_message)
print(f'Encode #{i}: {inputs[i]} -> {encoded_message}')
// Status.
var status mte.Status
// Inputs.
inputs := [...]string{
"message 1",
"message 2",
"message 3",
"message 4"}
// Personalization string.
const personal = "demo"
// 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.InitLicense("YOUR_COMPANY", "YOUR_LICENSE") {
company := os.Getenv("MTE_COMPANY")
license := os.Getenv("MTE_LICENSE")
if len(company) == 0 || len(license) == 0 ||
!mte.InitLicense(company, license) {
fmt.Fprintf(os.Stderr, "License init error (%v): %v\n",
mte.GetStatusName(mte.Status_mte_status_license_error),
mte.GetStatusDescription(mte.Status_mte_status_license_error))
return int(mte.Status_mte_status_license_error)
}
}
// Create the Encoder.
encoder := mte.NewEncDef()
defer encoder.Destroy()
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
entropyBytes := mte.GetDrbgsEntropyMinBytes(encoder.GetDrbg())
entropy := make([]byte, entropyBytes)
// Instantiate the Encoder.
encoder.SetEntropy(entropy)
encoder.SetNonceInt(0)
status = encoder.InstantiateStr(personal)
if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Encoder instantiate error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
}
// Encode the inputs.
var encodings [len(inputs)]string
for i := 0; i < len(inputs); i++ {
encodings[i], status = encoder.EncodeStrB64(inputs[i])
if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Encode error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
}
fmt.Printf("Encode #%v: %v -> %v\n", i, inputs[i], encodings[i]);
}
import { MteBase, MteWasm, MteEnc, MteDec, MteStatus } from "./Mte";
import { exit } from "process";
// Create and instantiate the singleton WASM. The instantiate() call returns a
// promise that must be resolved before any other MTE objects can be created.
// The instantiated, resolved MteWasm object must be passed as an argument to
// create any other MTE objects.
let wasm = new MteWasm();
wasm.instantiate().then(() => { main(); });
// Status.
let stat: MteStatus;
// Inputs.
const inputs = [
"message 0",
"message 1",
"message 2",
"message 3"
];
// Personalization string.
const personal = "demo";
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped.
const base = new MteBase(wasm);
if (!base.initLicense("YOUR_COMPANY", "YOUR_LICENSE"))
{
const code = MteStatus.mte_status_license_error;
console.error("License init error (" + base.getStatusName(code) + "): " +
base.getStatusDescription(code));
exit(code);
}
// Create the encoder.
let encoder = MteEnc.fromdefault(wasm);
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
const entropyBytes = encoder.getDrbgsEntropyMinBytes(encoder.getDrbg());
const entropy = new Uint8Array(entropyBytes);
const nonce = "0";
// Instantiate the encoder.
encoder.setEntropyArr(entropy);
encoder.setNonce(nonce);
stat = encoder.instantiate(personal);
if (stat != MteStatus.mte_status_success) {
console.error("Encoder instantiate error (" +
encoder.getStatusName(stat) +
"): " +
encoder.getStatusDescription(stat));
exit(stat);
}
// Encode the inputs.
let encodings = Array<string>(inputs.length);
for (let i = 0; i < inputs.length; ++i) {
const encoded = encoder.encodeStrB64(inputs[i]);
if (encoded.status != MteStatus.mte_status_success) {
console.error("Encode error (" +
encoder.getStatusName(encoded.status) + "): " +
encoder.getStatusDescription(encoded.status));
exit(encoded.status);
}
encodings[i] = encoded.str!;
console.log("Encode #" + i + ": " + inputs[i] + " -> " + encodings[i]);
}