async
- C
- C++
- CSharp
- Java
- Swift
- ObjC
- Python
- Go
- TypeScript
/* 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);
/* Create the Decoder. */
status = mte_dec_state_init(decoderA, &d_info);
if (status != mte_status_success)
{
fprintf(stderr, "Decoder init error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
/* Instantiate the Decoder. */
status = mte_dec_instantiate(decoderA, &i_info);
if (status != mte_status_success)
{
fprintf(stderr, "Decoder instantiate error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
/* Save the async decoder state. */
dsaved = MTE_ALLOCA(mte_dec_save_bytes(decoderA));
mte_dec_state_save(decoderA, dsaved);
/* Allocate the buffer for decoding. */
decoded = MTE_ALLOCA(mte_dec_buff_bytes_b64(decoderA, e_args.bytes));
/* Decode in async mode. */
puts("\nAsync mode (sequence window = -2):");
/* Decode the first message. */
MTE_SET_DEC_IO(d_args, encodings[0], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Try to decode the first message again -- out of sequence, should not work.*/
MTE_SET_DEC_IO(d_args, encodings[0], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Try to decode the corrupted second message -- should not work.*/
++encodings[2][0];
MTE_SET_DEC_IO(d_args, encodings[2], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Corrupt #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Try to decode the corrupted second message -- should not work.*/
--encodings[2][0];
MTE_SET_DEC_IO(d_args, encodings[2], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode third message.*/
/* Output: Decode #3: mte_status_success, message. 3 */
MTE_SET_DEC_IO(d_args, encodings[2], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode second message (async should work because we have NOT decoded yet) */
/* Output: Decode #2: mte_status_success, message 2 */
MTE_SET_DEC_IO(d_args, encodings[1], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #1: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode third message again (since after message 1 and 2 different error) */
/* Output: Decode #3: mte_status_seq_outside_window. */
MTE_SET_DEC_IO(d_args, encodings[2], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode fourth message. */
/* Output: Decode #4: mte_status_success, message 4. */
MTE_SET_DEC_IO(d_args, encodings[3], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #3: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Restore and decode again in a different order. */
mte_dec_state_restore(decoderA, dsaved);
puts("\nAsync mode (sequence window = -2):");
/* Decode fourth message first. */
/* Output: Decode #4: mte_status_success, message 4. */
MTE_SET_DEC_IO(d_args, encodings[3], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #4: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode first message (error - sequence window too large). */
/* Output: Decode #1: mte_status_seq_outside_window. */
MTE_SET_DEC_IO(d_args, encodings[0], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode third message (inside sequence window). */
/* Output: Decode #3: mte_status_success, message 3. */
MTE_SET_DEC_IO(d_args, encodings[2], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode non-corrupted second message (still inside sequence window). */
/* Output: Decode #2: mte_status_success, message 2. */
MTE_SET_DEC_IO(d_args, encodings[1], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderA, &d_args);
printf("Decode #1: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
}
static mte_status ei_cb(void* context, mte_drbg_ei_info* info)
{
/* Create all-zero entropy for this demo. This should never be done in real
applications. */
(void)context;
info->bytes = info->min_length;
memset(info->buff, 0, info->min_length);
return mte_status_success;
}
static void n_cb(void* context, mte_drbg_nonce_info* info)
{
/* Create all-zero nonce for this demo. This should never be done in real
applications. */
(void)context;
info->bytes = info->min_length;
memset(info->buff, 0, info->min_length);
}
static MTE_UINT64_T t_cb(void* context)
{
/* Return 0 for the timestamp. Real applications would request an actual
timestamp as appropriate for their system. */
(void)context;
return 0;
}
MteDec decoderA(0, -2);
decoderA.setEntropy(entropy, entropyBytes);
decoderA.setNonce(0);
status = decoderA.instantiate(personal);
if (status != mte_status_success)
{
std::cerr << "Decoder instantiate error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
// Save the async decoder state.
size_t stateBytes;
const void* dsaved = decoderA.saveState(stateBytes);
// String to decode to.
std::string decoded;
Decode in async mode.
std::cout << "\nAsync mode (sequence window = -2):" << std::endl;
status = decoderA.decodeB64(encodings[0].c_str(), decoded);
std::cout << "Decode #0: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[0].c_str(), decoded);
std::cout << "Decode #0: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
++encodings[2][0];
status = decoderA.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Corrupt #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
--encodings[2][0];
status = decoderA.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Decode #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Decode #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[1].c_str(), decoded);
std::cout << "Decode #1: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Decode #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[3].c_str(), decoded);
std::cout << "Decode #3: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
// Restore and decode again in a different order.
decoderA.restoreState(dsaved);
std::cout << "\nAsync mode (sequence window = -2):" << std::endl;
status = decoderA.decodeB64(encodings[3].c_str(), decoded);
std::cout << "Decode #3: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[0].c_str(), decoded);
std::cout << "Decode #0: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Decode #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderA.decodeB64(encodings[1].c_str(), decoded);
std::cout << "Decode #1: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
//For this sample sequence window is being set to -2
MteDec decoderA = new MteDec(0, -2);
// Instantiate the Decoder
decoderA.SetEntropy(entropy);
decoderA.SetNonce(0);
status = decoderA.Instantiate(personal);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decoder instantiate error ({0}): {1}",
decoderV.GetStatusName(status),
decoderV.GetStatusDescription(status));
return (int)status;
}
// Save the async Decoder state.
byte[] dsaved = decoderA.SaveState();
// String to decode to.
string decoded;
// Create the corrupt version of message #2.
// Doing this so we can intentionally fail
char first = encodings[2][0];
++first;
string corrupt =
encodings[2].Substring(1).Insert(0, new string(first, 1));
// Output that decoding is in Async sequencing mode
Console.WriteLine("\nAsync mode (sequence window = -2):");
// Decode the first message
// Output: Decode #1: mte_status_success, message 1
decoded = decoderA.DecodeStrB64(encodings[0], out status);
Console.WriteLine("Decode #0: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Try to decode the first message again -- out of sequence, should not work
// Output: Decode #0: mte_status_seq_outside_window,
decoded = decoderA.DecodeStrB64(encodings[0], out status);
Console.WriteLine("Decode #1: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Try to decode the corrupted second message -- should not work
// Output: Corrupt #3: mte_status_seq_mismatch,
decoded = decoderA.DecodeStrB64(corrupt, out status);
Console.WriteLine("Corrupt #3: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode third message
// Output: Decode #3: mte_status_success, message 3
decoded = decoderA.DecodeStrB64(encodings[2], out status);
Console.WriteLine("Decode #3: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode second message (async should work because we have NOT decoded yet)
// Output: Decode #2: mte_status_success, message 2
decoded = decoderA.DecodeStrB64(encodings[1], out status);
Console.WriteLine("Decode #2: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode third message again (since after message 1 and 2 different error)
// Output: Decode #3: mte_status_seq_outside_window,
decoded = decoderA.DecodeStrB64(encodings[2], out status);
Console.WriteLine("Decode #3: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode fourth message
// Output: Decode #4: mte_status_success, message 4
decoded = decoderA.DecodeStrB64(encodings[3], out status);
Console.WriteLine("Decode #4: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Restore and decode again in a different order.
decoderA.RestoreState(dsaved);
Console.WriteLine("\nAsync mode (sequence window = -2):");
// Decode fourth message first
// Output: Decode #4: mte_status_success, message 4
decoded = decoderA.DecodeStrB64(encodings[3], out status);
Console.WriteLine("Decode #4: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode first message (error - sequence window too large)
// Output: Decode #1: mte_status_seq_outside_window,
decoded = decoderA.DecodeStrB64(encodings[0], out status);
Console.WriteLine("Decode #1: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode third message (inside sequence window)
// Output: Decode #3: mte_status_success, message 3
decoded = decoderA.DecodeStrB64(encodings[2], out status);
Console.WriteLine("Decode #3: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
// Decode non-corrupted second message (still inside sequence window)
// Output: Decode #2: mte_status_success, message 2
decoded = decoderA.DecodeStrB64(encodings[1], out status);
Console.WriteLine("Decode #2: {0}, {1}",
decoderA.GetStatusName(status),
decoded);
//For this sample sequence window is being set to -2
MteDec decoderA = new MteDec(0, -2);
// Instantiate the Decoder
decoderA.setEntropy(entropy);
decoderA.setNonce(0);
status = decoderA.instantiate(personal);
if (status != MteStatus.mte_status_success)
{
System.err.println("Decoder instantiate error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
// Save the async Decoder state.
byte[] dsaved = decoderA.saveState();
// String to decode to.
MteBase.StrStatus decoded;
// Create the corrupt version of message #2.
// Doing this so we can intentionally fail
char[] e2 = encodings[2].toCharArray();
++e2[0];
String corrupt = new String(e2);
// Output that decoding is in Async sequencing mode
System.out.println("\nAsync mode (sequence window = -2):");
// Decode first message
// Output: Decode #1: mte_status_success, message 1
decoded = decoderA.decodeStrB64(encodings[0]);
System.out.println("Decode #1: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode first message again (error) -- out of sequence, should not work
// Output: Decode #1: mte_status_seq_outside_window,
decoded = decoderA.decodeStrB64(encodings[0]);
System.out.println("Decode #1: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode corrupt message
// Output: Corrupt #3: mte_status_seq_mismatch,
decoded = decoderA.decodeStrB64(corrupt);
System.out.println("Corrupt #3: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode third message
// Output: Decode #3: mte_status_success, message 3
decoded = decoderA.decodeStrB64(encodings[2]);
System.out.println("Decode #3: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode third message again -- out of sequence (repeat message), should not work
// Output: Decode #3: mte_status_seq_async_replay,
decoded = decoderA.decodeStrB64(encodings[2]);
System.out.println("Decode #3: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode second message (inside sequence window and can be out of order)
// Output: Decode #2: mte_status_success, message 2
decoded = decoderA.decodeStrB64(encodings[1]);
System.out.println("Decode #2: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode third message
// Output: Decode #3: mte_status_seq_outside_window,
decoded = decoderA.decodeStrB64(encodings[2]);
System.out.println("Decode #3: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode fourth message
// Output: Decode #4: mte_status_success, message 4
decoded = decoderA.decodeStrB64(encodings[3]);
System.out.println("Decode #4: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Restore and decode again in a different order.
decoderA.restoreState(dsaved);
System.out.println("\nAsync mode (sequence window = -2):");
// Decode fourth message
// Decode #4: mte_status_success, message 4
decoded = decoderA.decodeStrB64(encodings[3]);
System.out.println("Decode #4: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode first message (error - sequence window too large)
// Output: Decode #1: mte_status_seq_outside_window,
decoded = decoderA.decodeStrB64(encodings[0]);
System.out.println("Decode #1: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode third message (inside sequence window)
// Output: Decode #3: mte_status_success, message 3
decoded = decoderA.decodeStrB64(encodings[2]);
System.out.println("Decode #3: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode non-corrupted second message (still inside sequence window)
// Output: Decode #2: mte_status_success, message 2
decoded = decoderA.decodeStrB64(encodings[1]);
System.out.println("Decode #2: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
let decoderA = try! MteDec(0, -2)
decoderA.setEntropy(&entropy)
decoderA.setNonce(0)
status = decoderA.instantiate(personal)
if status != mte_status_success {
print("Decoder instantiate error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
exit(Int32(status.rawValue))
}
// Save the async decoder state.
let dsaved = decoderA.saveState()!
// String to decode to.
var decoded: String
// Create the corrupt version of message #2.
let first = encodings[2][encodings[2].startIndex].asciiValue! + 1
var corrupt = encodings[2]
corrupt.remove(at: corrupt.startIndex)
corrupt.insert(Character(Unicode.Scalar(first)), at: corrupt.startIndex)
// Decode in async mode.
print("\nAsync mode (sequence window = -2):")
(decoded, status) = decoderA.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(corrupt)
print("Corrupt #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[1])
print("Decode #1: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[3])
print("Decode #3: \(MteBase.getStatusName(status)), \(decoded)")
// Restore and decode again in a different order.
decoderA.restoreState(dsaved)
print("\nAsync mode (sequence window = -2):")
(decoded, status) = decoderA.decodeStrB64(encodings[3])
print("Decode #3: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderA.decodeStrB64(encodings[1])
print("Decode #1: \(MteBase.getStatusName(status)), \(decoded)")
// 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]);
}
// Create decoder.
MteDec *decoderA = MTE_AUTORELEASE([[MteDec alloc] initWithTWindow:0
sWindow:-2]);
// Instantiate the decoder.
[decoderA setEntropy:entropy bytes:entropyBytes];
[decoderA setNonce:0];
status = [decoderA instantiate:personal];
if (status != mte_status_success)
{
fprintf(stderr, "Decoder instantiate error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
// Save the async decoder state.
size_t stateBytes;
const void *dsaved = [decoderA saveState:&stateBytes];
// String to decode to.
NSString *decoded;
// Create the corrupt version of message #2.
char first[] = { [encodings[2] UTF8String][0] + 1, '\0' };
NSString *nsfirst = [[NSString alloc] initWithUTF8String:first];
NSString *corrupt =
[encodings[2] stringByReplacingCharactersInRange:NSMakeRange(0, 1)
withString:nsfirst];
// Decode in async mode.
puts("\nAsync mode (sequence window = -2):");
decoded = [decoderA decodeB64:encodings[0] status:&status];
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[0] status:&status];
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:corrupt status:&status];
printf("Corrupt #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[2] status:&status];
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[2] status:&status];
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[1] status:&status];
printf("Decode #1: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[2] status:&status];
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[3] status:&status];
printf("Decode #3: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
// Restore and decode again in a different order.
[decoderA restoreState:dsaved];
puts("\nAsync mode (sequence window = -2):");
decoded = [decoderA decodeB64:encodings[3] status:&status];
printf("Decode #3: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[0] status:&status];
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[2] status:&status];
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderA decodeB64:encodings[1] status:&status];
printf("Decode #1: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
free(entropy);
}
# For this sample sequence window is being set to -2.
decoder_a = MteDec.fromdefault(s_window=-2)
# Instantiate Decoder.
decoder_a.set_entropy(entropy)
decoder_a.set_nonce(0)
decoder_status = decoder_a.instantiate(personal_str)
if decoder_status != MteStatus.mte_status_success:
(status,message) = MteBase.get_status_name(decoder_status),\
MteBase.get_status_description(decoder_status)
print("Decoder instantiate error ({0}): {1}".format(\
status,message),file=sys.stderr)
return decoder_status.value
# Save the async Decoder state.
decoder_save_state = decoder_a.save_state()
# Create the corrupt version of message #2.
# Doing this to ensure decode fails.
corrupt = chr(ord(encodings[2][0]) + 1) + encodings[2][1:]
# Output that decoding is in Async sequencing mode.
print("\nAsync mode (sequence window = -2):")
# Decode the first message.
# Output: Decode #0: mte_status_success, message 0.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[0])
print("Decode #0: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Try to decode the first message again -- out of sequence, should not work.
# Output: Decode #0: mte_status_seq_outside_window.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[0])
print("Decode #0: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Try to decode the corrupted second message -- should not work.
# Output: Corrupt #2: mte_status_seq_mismatch.
(decoded_message, status) = decoder_a.decode_str_b64(corrupt)
print("Corrupt #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode third message.
# Output: Decode #2: mte_status_success, message 2.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[2])
print("Decode #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode third message again.
# Output: Decode #2: mte_status_seq_async_replay.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[2])
print("Decode #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode second message (async should work because we have NOT decoded yet).
# Output: Decode #1: mte_status_success, message 1.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[1])
print("Decode #1: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode third message again (since after message 1 and 2 different error).
# Output: Decode #2: mte_status_seq_outside_window.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[2])
print("Decode #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode fourth message.
# Output: Decode #3: mte_status_success, message 3.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[3])
print("Decode #3: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Restore and decode again in a different order.
decoder_a.restore_state(decoder_save_state)
print("\nAsync mode out of order (sequence window = -2):")
# Decode fourth message first.
# Output: Decode #3: mte_status_success, message 3.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[3])
print("Decode #3: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode first message (error - sequence window too large).
# Output: Decode #0: mte_status_seq_outside_window.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[0])
print("Decode #0: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode third message (inside sequence window).
# Output: Decode #2: mte_status_success, message 2.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[2])
print("Decode #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode non-corrupted second message (still inside sequence window).
# Output: Decode #1: mte_status_success, message 1.
(decoded_message, status) = decoder_a.decode_str_b64(encodings[1])
print("Decode #1: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
// Create Async decoder
// For this sample sequence window is being set to -2
decoderA := mte.NewDecWin(0, -2);
// Instantiate the Decoder.
decoderA.SetEntropy(entropy)
decoderA.SetNonceInt(0)
status = decoderA.InstantiateStr(personal)
if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Decoder instantiate error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
}
// Save the async Decoder state.
dsaved := decoderA.SaveState()
// String to decode to.
var decoded string
// Create the corrupt version of message #2.
first := true
corrupt := strings.Map(func(r rune) rune {
if first {
first = false
return r + 1
}
return r
}, encodings[2])
// Decode in async mode.
fmt.Println("\nAsync mode (sequence window = -2):")
// Decode the first message
// Output: Decode #1: mte_status_success, message 1
decoded, status = decoderA.DecodeStrB64(encodings[0])
fmt.Printf("Decode #1: %v, %v\n", mte.GetStatusName(status), decoded)
// Try to decode the first message again -- out of sequence, should not work
// Output: Decode #0: mte_status_seq_outside_window,
decoded, status = decoderA.DecodeStrB64(encodings[0])
fmt.Printf("Decode #1: %v, %v\n", mte.GetStatusName(status), decoded)
// Try to decode the corrupted second message -- should not work
// Output: Corrupt #3: mte_status_seq_mismatch,
decoded, status = decoderA.DecodeStrB64(corrupt)
fmt.Printf("Corrupt #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode third message
// Output: Decode #3: mte_status_success, message 3
decoded, status = decoderA.DecodeStrB64(encodings[2])
fmt.Printf("Decode #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode third message
// Output: Decode #3: mte_status_success, message 3
decoded, status = decoderA.DecodeStrB64(encodings[2])
fmt.Printf("Decode #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode second message (async should work because we have NOT decoded yet)
// Output: Decode #2: mte_status_success, message 2
decoded, status = decoderA.DecodeStrB64(encodings[1])
fmt.Printf("Decode #2: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode third message again (since after message 1 and 2 different error)
// Output: Decode #3: mte_status_seq_outside_window,
decoded, status = decoderA.DecodeStrB64(encodings[2])
fmt.Printf("Decode #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode fourth message
// Output: Decode #4: mte_status_success, message 4
decoded, status = decoderA.DecodeStrB64(encodings[3])
fmt.Printf("Decode #4: %v, %v\n", mte.GetStatusName(status), decoded)
// Restore and decode again in a different order.
decoderA.RestoreState(dsaved)
fmt.Println("\nAsync mode (sequence window = -2):")
// Decode fourth message first
// Output: Decode #4: mte_status_success, message 4
decoded, status = decoderA.DecodeStrB64(encodings[3])
fmt.Printf("Decode #4: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode first message (error - sequence window too large)
// Output: Decode #1: mte_status_seq_outside_window,
decoded, status = decoderA.DecodeStrB64(encodings[0])
fmt.Printf("Decode #1: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode third message (inside sequence window)
// Output: Decode #3: mte_status_success, message 3
decoded, status = decoderA.DecodeStrB64(encodings[2])
fmt.Printf("Decode #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode non-corrupted second message (still inside sequence window)
// Output: Decode #2: mte_status_success, message 2
decoded, status = decoderA.DecodeStrB64(encodings[1])
fmt.Printf("Decode #2: %v, %v\n", mte.GetStatusName(status), decoded)
// For this sample, sequence window is being set to -2
let decoderA = MteDec.fromdefault(wasm, 0, -2);
// Instantiate the Decoder
decoderA.setEntropyArr(entropy);
decoderA.setNonce(nonce);
stat = decoderA.instantiate(personal);
if (stat != MteStatus.mte_status_success) {
console.error("Decoder instantiate error (" +
decoderA.getStatusName(stat) +
"): " +
decoderA.getStatusDescription(stat));
exit(stat);
}
// Save the async decoder state.
const dsaved = decoderA.saveState();
// Create the corrupt version of message #2.
// Doing this to ensure decode fails
const corrupt = String.fromCharCode(encodings[2].charCodeAt(0)! + 1) +
encodings[2].substr(1, encodings[2].length - 1);
// Decode in async mode.
console.log("\nAsync mode (sequence window = -2):");
decoded = decoderA.decodeStrB64(encodings[0]);
console.log("Decode #0: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[0]);
console.log("Decode #0: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(corrupt);
console.log("Corrupt #2: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[2]);
console.log("Decode #2: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[2]);
console.log("Decode #2: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[1]);
console.log("Decode #1: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[2]);
console.log("Decode #2: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[3]);
console.log("Decode #3: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
// Restore and decode again in a different order.
decoderA.restoreState(dsaved!);
console.log("\nAsync mode (sequence window = -2):");
decoded = decoderA.decodeStrB64(encodings[3]);
console.log("Decode #3: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[0]);
console.log("Decode #0: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[2]);
console.log("Decode #2: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderA.decodeStrB64(encodings[1]);
console.log("Decode #1: " +
decoderA.getStatusName(decoded.status) + ", " + decoded.str);