encoder-reseed
- C
- C++
- CSharp
- Java
- JavaScript
- Swift
- Go
- PHP
// Get the Encoder DRBG reseed counter.
// This is the MTE's current seed count.
uint64_t current_seed = mte_enc_reseed_counter(encoder);
// Get the Encoder DRBG max reseed interval.
uint64_t max_seed = mte_base_drbgs_reseed_interval(MTE_DRBG_ENUM);
// For example, if the current seed is greater than 90% of the max seed,
// uninstantiate the MTE then Reinitialize the MTE.
// with a new entropy and nonce to reseed.
if (current_seed > (max_seed * 0.9))
{
// Uninstantiate the Encoder.
mte_status encoder_status = mte_enc_uninstantiate(encoder);
if (encoder_status != mte_status_success)
{
fprintf(stderr, "Failed to uninstantiate Encoder (%s): %s\n",
mte_base_status_name(encoder_status),
mte_base_status_description(encoder_status));
return encoder_status;
}
// Now the Encoder and matching Decoder must be re-paired with a new entropy and nonce.
//=============================================================
// TODO: Developer adds code to re-pair with entropy and nonce.
//=============================================================
}
// Get the Encoder DRBG reseed counter.
// This is the MTE's current seed count.
uint64_t currentSeed = encoder.getReseedCounter();
// Get the Encoder DRBG max reseed interval.
uint64_t maxSeed = MteBase::getDrbgsReseedInterval(MTE_DRBG_ENUM);
// For example, if the current seed is greater than 90% of the max seed,
// uninstantiate the MTE then Reinitialize the MTE.
// with a new entropy and nonce to reseed.
if (currentSeed > (maxSeed * 0.9))
{
// Uninstantiate the Encoder.
mte_status encoderStatus = encoder.uninstantiate();
if (encoderStatus != mte_status_success)
{
std::cerr << "Encoder uninstantiate error ("
<< MteBase::getStatusName(encoderStatus)
<< "): "
<< MteBase::getStatusDescription(encoderStatus)
<< std::endl;
return encoderStatus;
}
// Now the Encoder and matching Decoder must be re-paired with a new entropy and nonce.
//=============================================================
// TODO: Developer adds code to re-pair with entropy and nonce.
//=============================================================
}
//--------------------------------------
// Get the Encoder DRBG reseed counter
// This is the MTE's current seed count
ulong currentSeed = mteEncoder.GetReseedCounter();
//------------------------------------------
// Get the Encoder DRBG max reseed interval
ulong maxSeed = mteBase.GetDrbgsReseedInterval(mteEncoder.GetDrbg());
//---------------------------------------------------------
// If the current seed is greater than 90% of the max seed
// Uninstantiate the MTE then Reinitialize the MTE
// with a new entropy and nonce to reseed
if (currentSeed > (maxSeed * 0.9)){
//---------------------------
// Uninstantiate the Encoder
MteStatus encoderStatus = mteEncoder.Uninstantiate();
if(encoderStatus != MteStatus.mte_status_success) {
//-------------------------------------------------
// MTE was not uninstantiated as desired so handle
// failure appropriately, below is only an example
throw new Exception("Failed to uninstantiate Encoder. Status: "
+ mteEncoder.GetStatusName(encoderStatus)+ " / "
+ mteEncoder.GetStatusDescription(encoderStatus));
}
//---------------------------------------
// Re-handshake to get new entropy value
// AND new nonce value
// Full code sample not here, to see example
// please see Diffie-Hellman Key Exchange
HandshakeModel handshake = MethodToHandshake();
//-------------------------------
// Set Encoder entropy and nonce
mteEncoder.SetEntropy(Encoding.UTF8.GetBytes(handshake.NewEncoderEntropy));
mteEncoder.SetNonce(handshake.NewNonce);
//------------------------
// Initialize MTE Encoder
MteStatus encoderStatus = mteEncoder.Instantiate(personalizationString);
if(encoderStatus !=MteStatus.mte_status_success) {
//-----------------------------------------------------
// MTE cannot continue so handle failure appropriately
// Below is just an example
throw new ApplicationException($"Failed to initialize the MTE Encoder engine." +
$"Status: {mteEncoder.GetStatusName(encoderStatus)} / " +
$"{mteEncoder.GetStatusDescription(encoderStatus)}");
}
}
//--------------------------------------
// Get the Encoder DRBG reseed counter
// This is the MTE's current seed count
long currentSeed = mteEncoder.getReseedCounter();
//------------------------------------------
// Get the Encoder DRBG max reseed interval
long maxSeed = MteBase.getDrbgsReseedInterval(mteEncoder.getDrbg());
//---------------------------------------------------------
// If the current seed is greater than 90% of the max seed
// Uninstantiate the MTE then Reinitialize the MTE
// with a new entropy and nonce to reseed
if(currentSeed > (_maxSeed * .9)) {
// Uninstantiate the Encoder
MteStatus encoderStatus = mkeEncoder.uninstantiate();
if(encoderStatus != MteStatus.mte_status_success) {
// MTE was not uninstantiated as desired so handle failure appropriately
// Below is only an example
throw new Exception("Failed to uninstantiate Encoder. Status: "
+ MteBase.getStatusName(encoderStatus)+ " / "
+ MteBase.getStatusDescription(encoderStatus));
}
//---------------------------------------
// Re-handshake to get new entropy value
// AND new nonce value
// Full code sample not here, to see example
// please see Diffie-Hellman Key Exchange
HandshakeResponse handshake = MethodToHandshake();
//-------------------------------
// Set Encoder entropy and nonce
mteEncoder.setEntropy(handshake.NewEncoderEntropy.getBytes()));
mteEncoder.setNonce(handshake.NewNonce);
//------------------------
// Initialize MTE Encoder
MteStatus encoderStatus = mteEncoder.instantiate(personalizationString);
if(encoderStatus !=MteStatus.mte_status_success) {
//-----------------------------------------------------
// MTE cannot continue so handle failure appropriately
// Below is just an example
throw new Exception("Error creating Encoder: Status: "
+ MteBase.getStatusName(encoderStatus) + " / "
+ MteBase.getStatusDescription(encoderStatus);
}
}
info
JavaScript works a little differently than other languages due to having to use a different type once it reaches 16 digits. You can get more info on BigInts on MDN. Because of this, we specifically cast to Number, only grab 15 digits of precision, and reseed at 80% of the max seed instead of 90%.
//--------------------------------------
// Get the Encoder DRBG reseed counter
// This is the MTE's current seed count
const currentSeed = Number(
String(mteEncoder.getReseedCounter()).substring(0, 15),
);
//------------------------------------------
// Get the Encoder DRBG max reseed interval
const maxSeed = Number(
String(mteEncoder.getDrbgsReseedInterval(drbg)).substring(0, 15),
);
//---------------------------------------------------------
// If the current seed is greater than 80% of the max seed
// Uninstantiate the MTE then Reinitialize the MTE
// with a new entropy and nonce to reseed
if (Number(currentSeed) > Number(maxSeed) * 0.8) {
//---------------------------
// Uninstantiate the Encoder
const encoderStatus = mteEncoder.uninstantiate();
if (encoderStatus !== MteStatus.mte_status_success) {
//-------------------------------------------------
// MTE was not uninstantiated as desired so handle
// failure appropriately, below is only an example
throw new Error(
`Failed to uninstantiate Encoder. ` +
`Status: ${mteEncoder.getStatusName(encoderStatus)} ` +
`/ ${mteEncoder.getStatusDescription(encoderStatus)}`,
);
}
//---------------------------------------
// Re-handshake to get new entropy value
// AND new nonce value
// Full code sample not here, to see example
// please see Diffie-Hellman Key Exchange
const handshake = methodToHandshake();
//-------------------------------
// Set Encoder entropy and nonce
mteEncoder.setEntropy(handshake.newEncoderEntropy);
mteEncoder.setNonce(handshake.newNonce);
//------------------------
// Initialize MTE Encoder
const encoderStatus = mteEncoder.instantiate(personalizationString);
if (encoderStatus !== MteStatus.mte_status_success) {
//-----------------------------------------------------
// MTE cannot continue so handle failure appropriately
// Below is just an example
throw new Error(
`Failed to initialize the MTE Encoder engine.` +
`Status: ${mteEncoder.getStatusName(encoderStatus)} / ` +
`${mteEncoder.getStatusDescription(encoderStatus)}`,
);
}
}
// Get the Encoder DRBG reseed counter.
// This is the MTE's current seed count.
let currentSeed:UInt64 = encoder.getReseedCounter()
// Get the Encoder DRBG max reseed interval.
let maxSeed:UInt64 = MteBase.getDrbgsReseedInterval(encoder.getDrbg())
// For example, if the current seed is greater than 90% of the max seed,
// uninstantiate the MTE then Reinitialize the MTE.
// with a new entropy and nonce to reseed.
if (currentSeed > (maxSeed * UInt64(0.9))) {
// Uninstantiate the Encoder.
let encoderStatus:mte_status = encoder.uninstantiate()
if (encoderStatus != mte_status_success) {
print("Encoder uninstantiate error (\(MteBase.getStatusName(encoderStatus))): " +
MteBase.getStatusDescription(encoderStatus))
return Int32(encoderStatus.rawValue)
}
// Now the Encoder and matching Decoder must be re-paired with a new entropy and nonce.
//=============================================================
// TODO: Developer adds code to re-pair with entropy and nonce.
//=============================================================
}
//--------------------------------------
// Get the Encoder DRBG reseed counter
// This is the MTE's current seed count
currentSeed := mteEncoder.getReseedCounter()
//------------------------------------------
// Get the Encoder DRBG max reseed interval
maxSeed := mteEncoder.getDrbgReseedInterval(mteEncoder.getDrbg())
if currentSeed > (maxSeed * .9) {
//---------------------------
// Uninstantiate the Encoder
encoderStatus := mteEncoder.Uninstantiate()
if encoderStatus != mte.Status_mte_status_success {
//----------------------------------------------------
// Handle Encoder uninstantiate failure appropriately
// Below is only an example
fmt.Fprintf(os.Stderr, "Encoder uninstantiate error (%v): %v\n",
mte.GetStatusName(encoderStatus), mte.GetStatusDescription(encoderStatus))
return int(encoderStatus)
}
//---------------------------------------
// Re-handshake to get new entropy value
// AND new nonce value
// Full code sample not here, to see example
// please see Diffie-Hellman Key Exchange
handshake := MethodToHandshake();
//--------------------
// Initialize Encoder
//--------------------
mteEncoder.SetEntropy(handshake.newEncoderEntropy)
mteEncoder.SetNonceInt(handshake.newNonce)
encoderStatus := mteEncoder.InstantiateStr(personalizationString)
if encoderStatus != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Encoder instantiate error (%v): %v\n",
mte.GetStatusName(encoderStatus), mte.GetStatusDescription(encoderStatus))
return (int)encoderStatus
}
}
<?php
//--------------------------------------
// Get the Encoder DRBG reseed counter
// This is the MTE's current seed count
$currentSeed = $mteEncoder->getReseedCounter();
//------------------------------------------
// Get the Encoder DRBG max reseed interval
$maxSeed = $mteEncoder->getDrbgReseedInterval(constant($mteEncoder->getDrbg()));
if ($currentSeed > ($maxSeed * .9)) {
//---------------------------
// Uninstantiate the Encoder
$encoderStatus = $mteEncoder->uninstantiate();
if (constant($encoderStatus) != mte_status_success) {
//----------------------------------------------------
// Handle Encoder uninstantiate failure appropriately
// Below is only an example
echo "Encoder uninstantiate error: "
.$mteEncoder->getStatusName(constant($encoderStatus)).":"
.$mteEncoder->getStatusDescription(constant($encoderStatus));
return $mteEncoder->getStatusCode(constant($encoderStatus));
}
unset($mteEncoder);
//---------------------------------------
// Re-handshake to get new entropy value
// AND new nonce value
// Full code sample not here, to see example
// please see Diffie-Hellman Key Exchange
$handshake = MethodToHandshake();
//--------------------
// Initialize Encoder
//--------------------
$mteEncoder = new MteEnc();
$mteEncoder->setEntropy($handshake["newEncoderEntropy"]);
$mteEncoder->setNonce($handshake["newNonce"]);
$encoderStatus = $mteEncoder->instantiate($personalizationString);
if (constant($encoderStatus) != mte_status_success) {
//----------------------------------------------------
// Handle Encoder instantiate failure appropriately
// Below is only an example
echo "Encoder instantiate error: "
.$mteEncoder->getStatusName(constant($encoderStatus)).":"
.$mteEncoder->getStatusDescription(constant($encoderStatus));
return $mteEncoder->getStatusCode(constant($encoderStatus));
}
}
?>