Score:0

Performance of AES NI using crypto++

es flag

I have following simple function

uint128_t crypto_aes_prf( ECB_Mode< AES >::Encryption& e, int message)
{
    // Encrypt the input using AES
    unsigned char messageBytes[16]= {0};
    unsigned char ciphertext[16] = {0};
    std::memcpy(messageBytes, &message, sizeof(message));

    e.ProcessData(ciphertext,  messageBytes, sizeof(messageBytes));

    uint128_t result = 0;
    for (size_t i = 0; i < sizeof(ciphertext); ++i)
    {
        result = (result << 8) | static_cast<uint8_t>(ciphertext[i]);
    }

    return result;
}

Here the encryptor is initialized as follows:

std::string key = "0123456789abcdef";
CryptoPP::SecByteBlock aesKey(reinterpret_cast<const unsigned char*>(&key), 16);
CryptoPP::SecByteBlock iv(CryptoPP::AES::BLOCKSIZE);

// // Setup AES encryption
ECB_Mode< AES >::Encryption e;
e.SetKey(aesKey, CryptoPP::AES::DEFAULT_KEYLENGTH);

and then I call is using

auto c3= crypto_aes_prf(e, 65654);

Issue is that the performance is very slow, it is around 2 microseconds, more strangely when I call the function again using same encryptor then it takes only 150 nanoseconds.

I am not sure why this change in the performance based on how many times it is run?

Score:1
my flag

Issue is that the performance is very slow, it is around 2 microseconds, more strangely when I call the function again using same encryptor then it takes only 150 nanoseconds.

Two obvious possibilities:

  • Caching effect. That is, the CPU needs to pull in quite a lot of memory (possibly code, possibly data). The first time, it's not in the cache (and so you get a lot of cache misses). The second time, it's now in the cache (and so it goes a lot faster)

  • First time setup. That is, the code runs some 'setup' logic the first time it is called (and that setup logic is not cheap). It keeps it around (because it was expensive to do); the second time, it's there, and so it goes a lot faster.

muhammad haris avatar
es flag
okay that makes sense but also how can I validate that AES-NI is being used?
poncho avatar
my flag
@LWE-13: Dunno: I don't know the innards of crypto++
SAI Peregrinus avatar
si flag
Check the output assembly to see that the AES-NI instructions are used.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.