jeudi 26 février 2015

How to get/set internal state of a CFB_Mode Encryption Object?


Is it possible to create a new Encryption object that has the same internal state as a previously created Encryption object (I need this, to only keep the IV between function calls, and not the whole object)?


I thought it should be possible with the function GetNextIV but I didn't get the right result. In the following example the string This is the text gets encoded and the correct encoded text is:



94-41-d2-d4-06-05-f6-cd-d0-25-d6-f4-f6-52-55-7b-7c-


But I get:



94-a8-a9-b3-e0-a9-b3-e0-b4-a8-a5-e0-b4-a5-b8-b4-c0-


As you can see only the first byte (94) is encoded right. You can try it with following code sample:



#include <iostream>
#include <iomanip>
#include <crypto++/modes.h>
#include <crypto++/aes.h>
#include <crypto++/osrng.h>

using namespace CryptoPP;

void printHex(const byte in) {
std::cout << std::setfill('0') << std::setw(2) << std::hex << (int)in << "-";
}

int main() {

// Generate a random key and IV
AutoSeededRandomPool rnd;
SecByteBlock key(0x00, AES::DEFAULT_KEYLENGTH);
rnd.GenerateBlock(key, key.size());
byte iv[AES::BLOCKSIZE];
rnd.GenerateBlock(iv, AES::BLOCKSIZE);

// Encrypt byte by byte
// this results in the correct encrypted text
byte text[] = "This is the text";
int msgLen = sizeof(text);
CFB_Mode<AES>::Encryption cfbEncB(key, key.size(), iv);
for (byte* beg = &text[0]; beg != &text[msgLen]; ++beg) {
printHex(cfbEncB.ProcessByte(*beg));
}
std::cout << std::endl;

// Encrypt byte by byte only keeping IV for each iteration
// This is not the expected output, how to get it right?
byte nextiv[AES::BLOCKSIZE];
std::copy(&iv[0], &iv[AES::BLOCKSIZE], &nextiv[0]);
for (byte* beg = &text[0]; beg != &text[msgLen]; ++beg) {
CFB_Mode<AES>::Encryption cfbEncC(key, key.size(), nextiv);
printHex(cfbEncC.ProcessByte(*beg));
cfbEncC.GetNextIV(nextiv);
}
std::cout << std::endl;
}



Aucun commentaire:

Enregistrer un commentaire