Get current IV value in decrypt AES256 CTR mode

89 views
Skip to first unread message

Xamix

unread,
Jan 8, 2021, 10:44:43 AM1/8/21
to Crypto++ Users
Hi,

I'm using the library with the templated AES CTR decryptor:

CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption decryptor_ctr_;

I decrypt input data which is previouslly encoded data blocks by using the following function:

decryptor_ecb_.ProcessData(output, input, input_len);

Now I want to get get the IV value after encoding, in order to save it.
I haven't found any function to retrieve the current IV value which is normally incremented by 1 after each block encoding.
I can compute it myself but the decryptor should have the value currently after encoding.

Is there a solution to get back the current IV value after encoding?

Regards

Jeffrey Walton

unread,
Jan 8, 2021, 8:05:59 PM1/8/21
to Crypto++ Users List
No, there is no function to retrieve the IV.

There is a function to retrieve the next IV, which only generates a
random block with size of IVsize().

Jeff

Jeffrey Walton

unread,
Jan 8, 2021, 8:14:52 PM1/8/21
to Crypto++ Users List
By the way, for CTR mode, it is pretty easy to calculate the next IV.
Something like:

// AES block size
byte iv[AES::BLOCKSIZE] = ...;

// i-th block
size_t i = ...;

for (size_t b=0; b < i; ++i)
IncrementCounterByOne(iv, 1);

// The mask is AES_enc(iv)
byte mask[16];
AES::Encryption enc(key, key.size());
enc.ProcessBlock(mask, iv);

Then XOR mask with the plaintext or ciphertext. AES::Encryption is
used for both the forward and reverse directions.

Jeff

Xamix

unread,
Jan 9, 2021, 5:07:56 AM1/9/21
to Crypto++ Users
Thank you for your answer,

For the moment I will do something like the following:

// Create vector to store result
std::vector<CryptoPP::byte> decoded(data_len);

// Set counter before decoding
decryptor_.Resynchronize(counter, 16);
// Decrypt data
decryptor_.ProcessData(decoded.data(), data, data_len);

// For the moment we compute ourself the value of IV after decrypting
// But maybe we could optimize by retrieving it from decryptor object?
for(std::size_t i = 0; i < data_len / 16; ++i)
{
    for(int j = 15; j >= 0; --j)
    {
        if (++counter[j] != 0)
            break;
    }
}

Note that data_len is a multiple of block size (i.e 16).

Do you think it is possible to me to patch your library to get back the current IV in order to optimize the code a bit?
I haven't take a look in the underling code, but is the current IV available in private member at one moment?
Or maybe it is not available because you are using some Hardware function which do not return the IV?

Regards

Jeffrey Walton

unread,
Feb 3, 2021, 8:08:05 AM2/3/21
to Crypto++ Users List
On Sat, Jan 9, 2021 at 5:07 AM Xamix <gma...@gmail.com> wrote:
>
> Thank you for your answer,
>
> For the moment I will do something like the following:
>
> // Create vector to store result
> std::vector<CryptoPP::byte> decoded(data_len);
>
> // Set counter before decoding
> decryptor_.Resynchronize(counter, 16);
> // Decrypt data
> decryptor_.ProcessData(decoded.data(), data, data_len);
>
> // For the moment we compute ourself the value of IV after decrypting
> // But maybe we could optimize by retrieving it from decryptor object?
> for(std::size_t i = 0; i < data_len / 16; ++i)
> {
> for(int j = 15; j >= 0; --j)
> {
> if (++counter[j] != 0)
> break;
> }
> }
>
> Note that data_len is a multiple of block size (i.e 16).
>
> Do you think it is possible to me to patch your library to get back the current IV in order to optimize the code a bit?
> I haven't take a look in the underling code, but is the current IV available in private member at one moment?
> Or maybe it is not available because you are using some Hardware function which do not return the IV?

The problem is not so much hardware. The problem is Filters and
Buffering. The current IV is held in an appropriate buffer provided by
a mode object, like CBC_Mode or CTR_Mode. The mode may only use a few
bytes of the iv, so the question becomes - what do you return?

Jeff
Reply all
Reply to author
Forward
0 new messages