verification_only
- C
- C++
- CSharp
- Java
- Swift
- ObjC
- Python
- Go
- TypeScript
/* Create the Decoder. */
status = mte_dec_state_init(decoderV, &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(decoderV, &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;
}
/* Allocate the buffer for decoding. */
decoded = MTE_ALLOCA(mte_dec_buff_bytes_b64(decoderV, e_args.bytes));
/* Decode in verification-only mode. */
puts("\nVerification-only mode (sequence window = 0):");
/* Decode the first message. */
/* Output: Decode #1: mte_status_success, message 1. */
MTE_SET_DEC_IO(d_args, encodings[0], e_args.bytes, decoded);
status = mte_dec_decode_b64(decoderV, &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. */
/* 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(decoderV, &d_args);
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode the third message -- out of sequence should not work. */
/* 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(decoderV, &d_args);
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode the second message. */
/* 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(decoderV, &d_args);
printf("Decode #1: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode the 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(decoderV, &d_args);
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
/* Decode the 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(decoderV, &d_args);
printf("Decode #3: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? (const char*)d_args.decoded : "");
MteDec decoderV(0, 0);
decoderV.setEntropy(entropy, entropyBytes);
decoderV.setNonce(0);
status = decoderV.instantiate(personal);
if (status != mte_status_success)
{
std::cerr << "Decoder instantiate error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
// String to decode to.
std::string decoded;
// Decode in verification-only mode.
std::cout << "\nVerification-only mode (sequence window = 0):" << std::endl;
status = decoderV.decodeB64(encodings[0].c_str(), decoded);
std::cout << "Decode #0: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderV.decodeB64(encodings[0].c_str(), decoded);
std::cout << "Decode #0: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderV.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Decode #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderV.decodeB64(encodings[1].c_str(), decoded);
std::cout << "Decode #1: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderV.decodeB64(encodings[2].c_str(), decoded);
std::cout << "Decode #2: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
status = decoderV.decodeB64(encodings[3].c_str(), decoded);
std::cout << "Decode #3: " << MteBase::getStatusName(status)
<< ", " << decoded << std::endl;
// Create Decoder with Verification-only sequencing
// When constructing an MteDec the second argument is
// the sequence window (the first is the timestamp window)
MteDec decoderV = new MteDec(0, 0);
// Instantiate the Decoder.
decoderV.SetEntropy(entropy);
decoderV.SetNonce(0);
status = decoderV.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;
}
// Output that decoding is in verification-only sequencing mode
Console.WriteLine("\nVerification-only mode (sequence window = 0):");
// Decode the first message
// Output: Decode #1: mte_status_success, message 1
decoded = decoderV.DecodeStrB64(encodings[0], out status);
Console.WriteLine("Decode #1: {0}, {1}",
decoderV.GetStatusName(status),
decoded);
// Try to decode the first message again -- out of sequence, should not work
// Output: Decode #1: mte_status_seq_outside_window,
decoded = decoderV.DecodeStrB64(encodings[0], out status);
Console.WriteLine("Decode #1: {0}, {1}",
decoderV.GetStatusName(status),
decoded);
// Decode the third message -- out of sequence should not work
// Output: Decode #3: mte_status_seq_outside_window,
decoded = decoderV.DecodeStrB64(encodings[2], out status);
Console.WriteLine("Decode #3: {0}, {1}",
decoderV.GetStatusName(status),
decoded);
// Decode the second message
// Output: Decode #2: mte_status_success, message 2
decoded = decoderV.DecodeStrB64(encodings[1], out status);
Console.WriteLine("Decode #2: {0}, {1}",
decoderV.GetStatusName(status),
decoded);
// Decode the third message
// Output: Decode #3: mte_status_success, message 3
decoded = decoderV.DecodeStrB64(encodings[2], out status);
Console.WriteLine("Decode #3: {0}, {1}",
decoderV.GetStatusName(status),
decoded);
// Decode the fourth message
// Output: Decode #4: mte_status_success, message 4
decoded = decoderV.DecodeStrB64(encodings[3], out status);
Console.WriteLine("Decode #4: {0}, {1}",
decoderV.GetStatusName(status),
decoded);
// Create Verification-only Decoder
// When constructing an MteDec the second argument is
// the sequence window (the first is the timestamp window)
MteDec decoderV = new MteDec(0, 0);
// Instantiate the Decoder.
decoderV.setEntropy(entropy);
decoderV.setNonce(0);
status = decoderV.instantiate(personal);
if (status != MteStatus.mte_status_success)
{
System.err.println("Decoder instantiate error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
// String to decode to.
MteBase.StrStatus decoded;
// Output that decoding is in verification-only sequencing mode.
System.out.println("\nVerification-only mode (sequence window = 0):");
// Decode first message
// Output: Decode #1: mte_status_success, message 1
decoded = decoderV.decodeStrB64(encodings[0]);
System.out.println("Decode #1: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode first message again -- out of sequence, should not work
// Output: Decode #1: mte_status_seq_outside_window,
decoded = decoderV.decodeStrB64(encodings[0]);
System.out.println("Decode #1: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode third message - (out of sequence)
// Output: Decode #3: mte_status_seq_outside_window,
decoded = decoderV.decodeStrB64(encodings[2]);
System.out.println("Decode #3: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode second message
// Output: Decode #2: mte_status_success, message 2
decoded = decoderV.decodeStrB64(encodings[1]);
System.out.println("Decode #2: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
// Decode third message (in correct order)
// Output: Decode #3: mte_status_success, message 3
decoded = decoderV.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 = decoderV.decodeStrB64(encodings[3]);
System.out.println("Decode #4: " + MteBase.getStatusName(decoded.status) +
", " + (decoded.str == null ? "" : decoded.str));
let decoderV = try! MteDec(0, 0)
decoderV.setEntropy(&entropy)
decoderV.setNonce(0)
status = decoderV.instantiate(personal
if status != mte_status_success {
print("Decoder instantiate error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
exit(Int32(status.rawValue))
}
// 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 verification-only mode.
print("\nVerification-only mode (sequence window = 0):")
(decoded, status) = decoderV.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderV.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderV.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderV.decodeStrB64(encodings[1])
print("Decode #1: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderV.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderV.decodeStrB64(encodings[3])
print("Decode #3: \(MteBase.getStatusName(status)), \(decoded)")
// Decode in forward-only mode.
print("\nForward-only mode (sequence window = 2):")
(decoded, status) = decoderF.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderF.decodeStrB64(encodings[0])
print("Decode #0: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderF.decodeStrB64(corrupt)
print("Corrupt #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderF.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderF.decodeStrB64(encodings[1])
print("Decode #1: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderF.decodeStrB64(encodings[2])
print("Decode #2: \(MteBase.getStatusName(status)), \(decoded)")
(decoded, status) = decoderF.decodeStrB64(encodings[3])
print("Decode #3: \(MteBase.getStatusName(status)), \(decoded)")
// 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]);
}
// Create decoder.
MteDec *decoderV = MTE_AUTORELEASE([[MteDec alloc] initWithTWindow:0
sWindow:0]);
// Instantiate the decoder.
[decoderV setEntropy:entropy bytes:entropyBytes];
[decoderV setNonce:0];
status = [decoderV 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 = [decoderV 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 verification-only mode.
puts("\nVerification-only mode (sequence window = 0):");
decoded = [decoderV decodeB64:encodings[0] status:&status];
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderV decodeB64:encodings[0] status:&status];
printf("Decode #0: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderV decodeB64:encodings[2] status:&status];
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderV decodeB64:encodings[1] status:&status];
printf("Decode #1: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderV decodeB64:encodings[2] status:&status];
printf("Decode #2: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
decoded = [decoderV decodeB64:encodings[3] status:&status];
printf("Decode #3: %s, %s\n",
mte_base_status_name(status),
status == mte_status_success ? [decoded UTF8String] : "");
free(entropy);
}
# Create Decoder with Verification-only sequencing.
# When constructing an MteDec the second argument is
# the sequence window (the first is the timestamp window which is set to zero).
decoder_v = MteDec.fromdefault(s_window=0)
# Instantiate the Decoder.
decoder_v.set_entropy(entropy)
decoder_v.set_nonce(0)
decoder_status = decoder_v.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
# Decode in verification-only mode.
print("\nVerification-only mode (sequence window = 0):")
# Decode the first message.
# Output: Decode #0: mte_status_success, message 0.
(decoded_message, status) = decoder_v.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_v.decode_str_b64(encodings[0])
print("Decode #0: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode the third message -- out of sequence should not work.
# Output: Decode #2: mte_status_seq_outside_window.
(decoded_message, status) = decoder_v.decode_str_b64(encodings[2])
print("Decode #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode the second message.
# Output: Decode #1: mte_status_success, message 1.
(decoded_message, status) = decoder_v.decode_str_b64(encodings[1])
print("Decode #1: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode the third message.
# Output: Decode #2: mte_status_success, message 2.
(decoded_message, status) = decoder_v.decode_str_b64(encodings[2])
print("Decode #2: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
# Decode the fourth message.
# Output: Decode #3: mte_status_success, message 3.
(decoded_message, status) = decoder_v.decode_str_b64(encodings[3])
print("Decode #3: {0}, {1}".format(MteBase.get_status_name(status), decoded_message))
// Create decoder with Verification-only sequencing
// when constructing an NewDecWin the second argument is
// the sequence window (the first is the timestamp window)
decoderV := mte.NewDecWin(0, 0);
// Instantiate the decoder.
decoderV.SetEntropy(entropy)
decoderV.SetNonceInt(0)
status = decoderV.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)
}
// 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 verification-only mode.
fmt.Println("\nVerification-only mode (sequence window = 0):")
// Decode the first message
// Output: Decode #1: mte_status_success, message 1
decoded, status = decoderV.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 #1: mte_status_seq_outside_window,
decoded, status = decoderV.DecodeStrB64(encodings[0])
fmt.Printf("Decode #1: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode the third message -- out of sequence should not work
// Output: Decode #3: mte_status_seq_outside_window,
decoded, status = decoderV.DecodeStrB64(encodings[2])
fmt.Printf("Decode #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode the second message
// Output: Decode #2: mte_status_success, message 2
decoded, status = decoderV.DecodeStrB64(encodings[1])
fmt.Printf("Decode #2: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode the third message
// Output: Decode #3: mte_status_success, message 3
decoded, status = decoderV.DecodeStrB64(encodings[2])
fmt.Printf("Decode #3: %v, %v\n", mte.GetStatusName(status), decoded)
// Decode the fourth message
// Output: Decode #4: mte_status_success, message 4
decoded, status = decoderV.DecodeStrB64(encodings[3])
fmt.Printf("Decode #4: %v, %v\n", mte.GetStatusName(status), decoded)
// Create Decoder with Verification-only sequencing
// When constructing an MteDec the second argument is
// the sequence window (the first is the timestamp window)
let decoderV = MteDec.fromdefault(wasm, 0, 0);
// Instantiate the Decoder
decoderV.setEntropyArr(entropy);
decoderV.setNonce(nonce);
stat = decoderV.instantiate(personal);
if (stat != MteStatus.mte_status_success) {
console.error("Decoder instantiate error (" +
decoderV.getStatusName(stat) +
"): " +
decoderV.getStatusDescription(stat));
exit(stat);
}
// Decode in verification-only sequencing mode
console.log("\nVerification-only mode (sequence window = 0):");
let decoded = decoderV.decodeStrB64(encodings[0]);
console.log("Decode #0: " +
decoderV.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderV.decodeStrB64(encodings[0]);
console.log("Decode #0: " +
decoderV.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderV.decodeStrB64(encodings[2]);
console.log("Decode #2: " +
decoderV.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderV.decodeStrB64(encodings[1]);
console.log("Decode #1: " +
decoderV.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderV.decodeStrB64(encodings[2]);
console.log("Decode #2: " +
decoderV.getStatusName(decoded.status) + ", " + decoded.str);
decoded = decoderV.decodeStrB64(encodings[3]);
console.log("Decode #3: " +
decoderV.getStatusName(decoded.status) + ", " + decoded.str);