Skip to content

Commit

Permalink
Merge PR #23291 into master
Browse files Browse the repository at this point in the history
* refs/pull/23291/head:
	tests: add Cephx-resembling scenarios to crypto tests.
	tests: differentiate AES testing regarding text size.

Reviewed-by: Sage Weil <sage@redhat.com>
  • Loading branch information
liewegas committed Aug 13, 2018
2 parents 8f3560d + 846502f commit 3d774d6
Showing 1 changed file with 76 additions and 4 deletions.
80 changes: 76 additions & 4 deletions src/test/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,57 @@ TEST(AES, DecryptNoBl) {
ASSERT_EQ(0, err);
}

TEST(AES, Loop) {
template <std::size_t TextSizeV>
static void aes_loop_cephx() {
CryptoHandler *h = g_ceph_context->get_crypto_handler(CEPH_CRYPTO_AES);

CryptoRandom random;

bufferptr secret(16);
random.get_bytes(secret.c_str(), secret.length());
std::string error;
std::unique_ptr<CryptoKeyHandler> kh(h->get_key_handler(secret, error));

unsigned char plaintext[TextSizeV];
random.get_bytes(reinterpret_cast<char*>(plaintext), sizeof(plaintext));

const CryptoKey::in_slice_t plain_slice { sizeof(plaintext), plaintext };

// we need to deduce size first
const CryptoKey::out_slice_t probe_slice { 0, nullptr };
const auto needed = kh->encrypt(plain_slice, probe_slice);
ASSERT_GE(needed, plain_slice.length);

boost::container::small_vector<
// FIXME?
//unsigned char, sizeof(plaintext) + kh->get_block_size()> buf;
unsigned char, sizeof(plaintext) + 16> buf(needed);

std::size_t cipher_size;
for (std::size_t i = 0; i < 1000000; i++) {
const CryptoKey::out_slice_t cipher_slice { needed, buf.data() };
cipher_size = kh->encrypt(plain_slice, cipher_slice);
ASSERT_EQ(cipher_size, needed);
}
}

// These magics reflects Cephx's signature size. Please consult
// CephxSessionHandler::_calc_signature() for more details.
TEST(AES, LoopCephx) {
aes_loop_cephx<29>();
}

bufferptr orig_plaintext(256);
TEST(AES, LoopCephxV2) {
aes_loop_cephx<32>();
}

static void aes_loop(const std::size_t text_size) {
CryptoRandom random;

bufferptr secret(16);
random.get_bytes(secret.c_str(), secret.length());

bufferptr orig_plaintext(text_size);
random.get_bytes(orig_plaintext.c_str(), orig_plaintext.length());

bufferlist plaintext;
Expand Down Expand Up @@ -426,14 +470,28 @@ TEST(AES, Loop) {
ASSERT_EQ(orig, plaintext);
}

TEST(AES, LoopKey) {
TEST(AES, Loop) {
aes_loop(256);
}

// These magics reflects Cephx's signature size. Please consult
// CephxSessionHandler::_calc_signature() for more details.
TEST(AES, Loop_29) {
aes_loop(29);
}

TEST(AES, Loop_32) {
aes_loop(32);
}

void aes_loopkey(const std::size_t text_size) {
CryptoRandom random;
bufferptr k(16);
random.get_bytes(k.c_str(), k.length());
CryptoKey key(CEPH_CRYPTO_AES, ceph_clock_now(), k);

bufferlist data;
bufferptr r(128);
bufferptr r(text_size);
random.get_bytes(r.c_str(), r.length());
data.append(r);

Expand All @@ -451,3 +509,17 @@ TEST(AES, LoopKey) {
utime_t dur = end - start;
cout << n << " encoded in " << dur << std::endl;
}

TEST(AES, LoopKey) {
aes_loopkey(128);
}

// These magics reflects Cephx's signature size. Please consult
// CephxSessionHandler::_calc_signature() for more details.
TEST(AES, LoopKey_29) {
aes_loopkey(29);
}

TEST(AES, LoopKey_32) {
aes_loopkey(32);
}

0 comments on commit 3d774d6

Please sign in to comment.