其他分享
首页 > 其他分享> > c – 在Visual Studio 2017中计算AES / CCM的时间加密

c – 在Visual Studio 2017中计算AES / CCM的时间加密

作者:互联网

我使用的是Crypto 5.6.5和Visual Studio 2017库.

如何计算AES-CCM的加密时间?

解决方法:

I would like to know how to calculate the encryption time for AES-CCM.

Crypto wiki提供了一篇文章Benchmarks.它提供了大量有关库性能,吞吐量计算方法的详细信息,甚至还提供了测量实际吞吐量的源代码.信不信由你,简单的时钟调用可以很好地测量批量加密.另请参阅同一维基文章中的Benchmarks | Timing Loop.

要对AES / CCM进行基准测试,请执行以下操作.它基于Crypto基准测试代码,但它使用ThreadUserTimer而不是直接调用时钟. ThreadUserTimer适用于所有操作系统和所有版本的C.

您需要在cpuFreq上拨入处理器速度.您还应运行./governor.sh perf以将CPU从空闲或C级睡眠状态移开,但您不能,因为它是Linux脚本.您可以在TestScript /文件夹中找到它.

#include "cryptlib.h"
#include "secblock.h"
#include "hrtimer.h"
#include "osrng.h"
#include "modes.h"
#include "aes.h"
#include "ccm.h"
#include <iostream>

const double runTimeInSeconds = 3.0;
const double cpuFreq = 2.7*1000*1000*1000;

int main(int argc, char* argv[])
{
    using namespace CryptoPP;
    AutoSeededRandomPool prng;

    SecByteBlock key(16);
    prng.GenerateBlock(key, key.size());

    CCM<AES>::Encryption cipher;
    cipher.SetKeyWithIV(key, key.size(), key);

    const int BUF_SIZE=RoundUpToMultipleOf(2048U,
        dynamic_cast<StreamTransformation&>(cipher).OptimalBlockSize());

    AlignedSecByteBlock buf(BUF_SIZE);
    prng.GenerateBlock(buf, BUF_SIZE);

    double elapsedTimeInSeconds;
    unsigned long i=0, blocks=1;

    ThreadUserTimer timer;
    timer.StartTimer();

    do
    {
        blocks *= 2;
        for (; i<blocks; i++)
            cipher.ProcessString(buf, BUF_SIZE);
        elapsedTimeInSeconds = timer.ElapsedTimeAsDouble();
    }
    while (elapsedTimeInSeconds < runTimeInSeconds);

    const double bytes = static_cast<double>(BUF_SIZE) * blocks;
    const double ghz = cpuFreq / 1000 / 1000 / 1000;
    const double mbs = bytes / 1024 / 1024 / elapsedTimeInSeconds;
    const double cpb = elapsedTimeInSeconds * cpuFreq / bytes;

    std::cout << cipher.AlgorithmName() << " benchmarks..." << std::endl;
    std::cout << "  " << ghz << " GHz cpu frequency" << std::endl;
    std::cout << "  " << cpb << " cycles per byte (cpb)" << std::endl;
    std::cout << "  " << mbs << " MiB per second (MiB)" << std::endl;
    // std::cout << "  " << elapsedTimeInSeconds << " seconds passed" << std::endl;
    // std::cout << "  " << (word64) bytes << " bytes processed"  << std::endl;

    return 0;
}

在2.7 GHz的Core i5-6400上运行它会导致:

$g++ bench.cxx ./libcryptopp.a -o bench.exe
$./bench.exe
AES/CCM benchmarks...
  2.7 GHz cpu frequency
  3.00491 cycles per byte (cpb)
  856.904 MiB per second (MiB)

通常您使用CTR模式对库进行基准测试.例如,使用该模式执行所有SUPERCOP基准测试.您可以通过包含“modes.h”切换到CTR模式,然后:

CTR_Mode<AES>::Encryption cipher;
cipher.SetKeyWithIV(key, key.size(), key);

最后,使用CTR模式进行相同的测试:

$./bench.exe
AES/CTR benchmarks...
  2.7 GHz cpu frequency
  0.568922 cycles per byte (cpb)
  4525.97 MiB per second (MiB)

标签:crypto,microbenchmark,c,visual-c,benchmarking
来源: https://codeday.me/bug/20190929/1833463.html