Skip to content

Commit b497dd0

Browse files
craig[bot]marc
andcommitted
Merge #26649
26649: libroach: make CryptoPP build with runtime AES-NI detection. r=mberhault a=mberhault Fixes #26383 Set appropriate `-m` flags for cryptopp to enable AES runtime detection checks (without those, it does not even try). Add a `UsesAESNI()` function in CryptoPP which returns true iff: * AES-NI runtime detection is compiled in * AES-NI instruction is available Add a warning to stdout (normal logging requires `vmodule=rocksdb=3`) if encryption is requested but AES-NI is not available. Add a test to make sure our builds always have AES-NI enabled. Release note (core): build release binaries with runtime AES detection. Co-authored-by: marc <marc@cockroachlabs.com>
2 parents f4faa15 + 6881f30 commit b497dd0

File tree

8 files changed

+37
-2
lines changed

8 files changed

+37
-2
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,17 @@ $(CGO_FLAGS_FILES): Makefile
558558
# only rebuild the affected objects, but in practice dependencies on configure
559559
# flags are not tracked correctly, and these stale artifacts can cause
560560
# particularly hard-to-debug errors.
561+
#
562+
# Flags needed to make cryptopp to runtime detection of AES cpu instruction sets.
563+
# pclmul and ssse3 need to be defined for the overall AES switch but are only used
564+
# in GCM mode (not currently in use by cockroach).
565+
AES_FLAGS := -maes -mpclmul -mssse3
561566
$(CRYPTOPP_DIR)/Makefile: $(C_DEPS_DIR)/cryptopp-rebuild | bin/.submodules-initialized
562567
rm -rf $(CRYPTOPP_DIR)
563568
mkdir -p $(CRYPTOPP_DIR)
564569
@# NOTE: If you change the CMake flags below, bump the version in
565570
@# $(C_DEPS_DIR)/cryptopp-rebuild. See above for rationale.
566-
cd $(CRYPTOPP_DIR) && cmake $(XCMAKE_FLAGS) $(CRYPTOPP_SRC_DIR) \
571+
cd $(CRYPTOPP_DIR) && CFLAGS+=" $(AES_FLAGS)" && CXXFLAGS+=" $(AES_FLAGS)" cmake $(XCMAKE_FLAGS) $(CRYPTOPP_SRC_DIR) \
567572
-DCMAKE_BUILD_TYPE=Release
568573

569574
$(JEMALLOC_SRC_DIR)/configure.ac: | bin/.submodules-initialized

build/variables.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
define VALID_VARS
44
.DEFAULT_GOAL
55
ACCEPTANCETIMEOUT
6+
AES_FLAGS
67
ARCHIVE
78
ARCHIVE_BASE
89
ARCHIVE_EXTRAS

c-deps/cryptopp

c-deps/libroach/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ set(tests
9292
encoding_test.cc
9393
file_registry_test.cc
9494
merge_test.cc
95+
ccl/crypto_utils_test.cc
9596
ccl/db_test.cc
9697
ccl/encrypted_env_test.cc
9798
ccl/key_manager_test.cc

c-deps/libroach/ccl/crypto_utils.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ class AESEncryptCipher : public rocksdb_utils::BlockCipher {
5959
rocksdb_utils::BlockCipher* NewAESEncryptCipher(const enginepbccl::SecretKey* key) {
6060
return new AESEncryptCipher(key->key());
6161
}
62+
63+
bool UsesAESNI() { return CryptoPP::UsesAESNI(); }

c-deps/libroach/ccl/crypto_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ std::string RandomBytes(size_t length);
2929
// Create a new AES cipher using the passed-in key.
3030
// Suitable for encryption only, Decrypt is not implemented.
3131
rocksdb_utils::BlockCipher* NewAESEncryptCipher(const enginepbccl::SecretKey* key);
32+
33+
// Returns true if CryptoPP is using AES-NI.
34+
bool UsesAESNI();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2017 The Cockroach Authors.
2+
//
3+
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
4+
// License (the "License"); you may not use this file except in compliance with
5+
// the License. You may obtain a copy of the License at
6+
//
7+
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
8+
9+
#include "../testutils.h"
10+
#include "crypto_utils.h"
11+
12+
TEST(CryptoUtils, HasAESNI) { EXPECT_TRUE(UsesAESNI()); }

c-deps/libroach/ccl/db.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "../status.h"
2323
#include "ccl/baseccl/encryption_options.pb.h"
2424
#include "ccl/storageccl/engineccl/enginepbccl/stats.pb.h"
25+
#include "crypto_utils.h"
2526
#include "ctr_stream.h"
2627
#include "key_manager.h"
2728

@@ -78,6 +79,16 @@ rocksdb::Status DBOpenHook(std::shared_ptr<rocksdb::Logger> info_log, const std:
7879
return rocksdb::Status::OK();
7980
}
8081

82+
// We have encryption options. Check whether the AES instruction set is supported.
83+
if (!UsesAESNI()) {
84+
// Shout loudly on standard out.
85+
std::cout << std::endl
86+
<< "*** WARNING ***" << std::endl
87+
<< "Encryption requested, but no AES instruction set detected" << std::endl
88+
<< "Expect significant performance degradation!" << std::endl
89+
<< std::endl;
90+
}
91+
8192
// The Go code sets the "file_registry" storage version if we specified encryption flags,
8293
// but let's double check anyway.
8394
if (!db_opts.use_file_registry) {

0 commit comments

Comments
 (0)