|
34 | 34 | #include "internal.h"
|
35 | 35 |
|
36 | 36 | #include <openssl/crypto.h>
|
| 37 | + |
| 38 | +#include "openssl/cmac.h" |
37 | 39 | #if defined(OPENSSL_IS_AWSLC)
|
38 | 40 | #include "bssl_bm.h"
|
39 | 41 | #include "../crypto/internal.h"
|
@@ -1262,6 +1264,70 @@ static bool SpeedHmacOneShot(const EVP_MD *md, const std::string &name,
|
1262 | 1264 | return true;
|
1263 | 1265 | }
|
1264 | 1266 |
|
| 1267 | +#if !defined(OPENSSL_1_0_BENCHMARK) |
| 1268 | +static bool SpeedCmacChunk(const EVP_CIPHER *cipher, std::string name, |
| 1269 | + size_t chunk_len) { |
| 1270 | + BM_NAMESPACE::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new()); |
| 1271 | + std::unique_ptr<uint8_t[]> input(new uint8_t[chunk_len]); |
| 1272 | + const size_t key_len = EVP_CIPHER_key_length(cipher); |
| 1273 | + std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]); |
| 1274 | + BM_memset(key.get(), 0, key_len); |
| 1275 | + |
| 1276 | + if (!CMAC_Init(ctx.get(), key.get(), key_len, cipher, NULL /* ENGINE */)) { |
| 1277 | + fprintf(stderr, "Failed to create CMAC_CTX.\n"); |
| 1278 | + } |
| 1279 | + TimeResults results; |
| 1280 | + if (!TimeFunction(&results, [&ctx, chunk_len, &input]() -> bool { |
| 1281 | + uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1282 | + size_t out_len; |
| 1283 | + |
| 1284 | + return |
| 1285 | +#if defined(OPENSSL_IS_AWSLC) || defined(OPENSSL_IS_BORINGSSL) |
| 1286 | + CMAC_Reset(ctx.get()) && |
| 1287 | +#else |
| 1288 | + CMAC_Init(ctx.get(), nullptr, 0, nullptr, nullptr /* ENGINE */) && |
| 1289 | +#endif |
| 1290 | + CMAC_Update(ctx.get(), input.get(), chunk_len) && |
| 1291 | + CMAC_Final(ctx.get(), digest, &out_len); |
| 1292 | + })) { |
| 1293 | + fprintf(stderr, "CMAC_Final failed.\n"); |
| 1294 | + ERR_print_errors_fp(stderr); |
| 1295 | + return false; |
| 1296 | + } |
| 1297 | + |
| 1298 | + results.PrintWithBytes(name, chunk_len); |
| 1299 | + return true; |
| 1300 | +} |
| 1301 | + |
| 1302 | +static bool SpeedCmac(const EVP_CIPHER *cipher, const std::string &name, const std::string &selected) { |
| 1303 | + if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 1304 | + return true; |
| 1305 | + } |
| 1306 | + TimeResults results; |
| 1307 | + const size_t key_len = EVP_CIPHER_key_length(cipher); |
| 1308 | + std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]); |
| 1309 | + BM_memset(key.get(), 0, key_len); |
| 1310 | + BM_NAMESPACE::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new()); |
| 1311 | + if (!TimeFunction(&results, [&]() -> bool { |
| 1312 | + return CMAC_Init(ctx.get(), key.get(), key_len, cipher, |
| 1313 | + nullptr /* ENGINE */); |
| 1314 | + })) { |
| 1315 | + fprintf(stderr, "CMAC_Init failed.\n"); |
| 1316 | + ERR_print_errors_fp(stderr); |
| 1317 | + return false; |
| 1318 | + } |
| 1319 | + results.Print(name + " init"); |
| 1320 | + |
| 1321 | + for (size_t chunk_len : g_chunk_lengths) { |
| 1322 | + if (!SpeedCmacChunk(cipher, name, chunk_len)) { |
| 1323 | + return false; |
| 1324 | + } |
| 1325 | + } |
| 1326 | + |
| 1327 | + return true; |
| 1328 | +} |
| 1329 | + |
| 1330 | +#endif |
1265 | 1331 |
|
1266 | 1332 | using RandomFunction = std::function<void(uint8_t *, size_t)>;
|
1267 | 1333 | static bool SpeedRandomChunk(RandomFunction function, std::string name, size_t chunk_len) {
|
@@ -2832,6 +2898,10 @@ bool Speed(const std::vector<std::string> &args) {
|
2832 | 2898 | !SpeedHmacOneShot(EVP_sha256(), "HMAC-SHA256-OneShot", selected) ||
|
2833 | 2899 | !SpeedHmacOneShot(EVP_sha384(), "HMAC-SHA384-OneShot", selected) ||
|
2834 | 2900 | !SpeedHmacOneShot(EVP_sha512(), "HMAC-SHA512-OneShot", selected) ||
|
| 2901 | +#if !defined(OPENSSL_1_0_BENCHMARK) |
| 2902 | + !SpeedCmac(EVP_aes_128_cbc(), "CMAC-AES-128-CBC", selected) || |
| 2903 | + !SpeedCmac(EVP_aes_256_cbc(), "CMAC-AES-256-CBC", selected) || |
| 2904 | +#endif |
2835 | 2905 | !SpeedRandom(RAND_bytes, "RNG", selected) ||
|
2836 | 2906 | !SpeedECDH(selected) ||
|
2837 | 2907 | !SpeedECDSA(selected) ||
|
|
0 commit comments