]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
tests/crypto: add tests for the no-bl encrypt/decrypt.
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Fri, 27 Apr 2018 18:01:26 +0000 (20:01 +0200)
committerRadoslaw Zarzynski <rzarzyns@redhat.com>
Fri, 18 May 2018 14:49:08 +0000 (10:49 -0400)
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
src/test/crypto.cc

index d397f0a25bf15a639b8d2af4fb242839a30f4737..bdc6773da52fcd9d5a3768a84d223467751b0d22 100644 (file)
@@ -1,6 +1,8 @@
 #include <errno.h>
 #include <time.h>
 
+#include <boost/container/small_vector.hpp>
+
 #include "gtest/gtest.h"
 #include "include/types.h"
 #include "auth/Crypto.h"
@@ -75,6 +77,50 @@ TEST(AES, Encrypt) {
   delete kh;
 }
 
+TEST(AES, EncryptNoBl) {
+  CryptoHandler *h = g_ceph_context->get_crypto_handler(CEPH_CRYPTO_AES);
+  char secret_s[] = {
+    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+  };
+  bufferptr secret(secret_s, sizeof(secret_s));
+
+  const unsigned char plaintext[] = {
+    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+  };
+
+  std::string error;
+  std::unique_ptr<CryptoKeyHandler> kh(h->get_key_handler(secret, error));
+
+  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);
+  const CryptoKey::out_slice_t cipher_slice { needed, buf.data() };
+  const auto cipher_size = kh->encrypt(plain_slice, cipher_slice);
+  ASSERT_EQ(cipher_size, needed);
+
+  const unsigned char want_cipher[] = {
+    0xb3, 0x8f, 0x5b, 0xc9, 0x35, 0x4c, 0xf8, 0xc6,
+    0x13, 0x15, 0x66, 0x6f, 0x37, 0xd7, 0x79, 0x3a,
+    0x11, 0x90, 0x7b, 0xe9, 0xd8, 0x3c, 0x35, 0x70,
+    0x58, 0x7b, 0x97, 0x9b, 0x03, 0xd2, 0xa5, 0x01,
+  };
+
+  ASSERT_EQ(sizeof(want_cipher), cipher_size);
+
+  const int err = memcmp(buf.data(), want_cipher, sizeof(want_cipher));
+  ASSERT_EQ(0, err);
+}
+
 TEST(AES, Decrypt) {
   CryptoHandler *h = g_ceph_context->get_crypto_handler(CEPH_CRYPTO_AES);
   char secret_s[] = {
@@ -115,6 +161,40 @@ TEST(AES, Decrypt) {
   delete kh;
 }
 
+TEST(AES, DecryptNoBl) {
+  CryptoHandler *h = g_ceph_context->get_crypto_handler(CEPH_CRYPTO_AES);
+  const char secret_s[] = {
+    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+  };
+  bufferptr secret(secret_s, sizeof(secret_s));
+
+  const unsigned char ciphertext[] = {
+    0xb3, 0x8f, 0x5b, 0xc9, 0x35, 0x4c, 0xf8, 0xc6,
+    0x13, 0x15, 0x66, 0x6f, 0x37, 0xd7, 0x79, 0x3a,
+    0x11, 0x90, 0x7b, 0xe9, 0xd8, 0x3c, 0x35, 0x70,
+    0x58, 0x7b, 0x97, 0x9b, 0x03, 0xd2, 0xa5, 0x01,
+  };
+
+  const unsigned char want_plaintext[] = {
+    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+  };
+  unsigned char plaintext[sizeof(want_plaintext)];
+
+  std::string error;
+  std::unique_ptr<CryptoKeyHandler> kh(h->get_key_handler(secret, error));
+
+  CryptoKey::in_slice_t cipher_slice { sizeof(ciphertext), ciphertext };
+  CryptoKey::out_slice_t plain_slice { sizeof(ciphertext), plaintext };
+  const auto plain_size = kh->decrypt(cipher_slice, plain_slice);
+
+  ASSERT_EQ(plain_size, sizeof(want_plaintext));
+
+  const int err = memcmp(plaintext, want_plaintext, sizeof(plain_size));
+  ASSERT_EQ(0, err);
+}
+
 TEST(AES, Loop) {
   CryptoRandom random;