]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librbd/crypto/openssl/test_DataCryptor.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / librbd / crypto / openssl / test_DataCryptor.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "test/librbd/test_fixture.h"
5 #include "librbd/crypto/openssl/DataCryptor.h"
6
7 namespace librbd {
8 namespace crypto {
9 namespace openssl {
10
11 const char* TEST_CIPHER_NAME = "aes-256-xts";
12 const unsigned char TEST_KEY[64] = {1};
13 const unsigned char TEST_IV[16] = {2};
14 const unsigned char TEST_IV_2[16] = {3};
15 const unsigned char TEST_DATA[4096] = {4};
16
17 struct TestCryptoOpensslDataCryptor : public TestFixture {
18 DataCryptor *cryptor;
19
20 void SetUp() override {
21 TestFixture::SetUp();
22 cryptor = new DataCryptor(reinterpret_cast<CephContext*>(m_ioctx.cct()));
23 ASSERT_EQ(0,
24 cryptor->init(TEST_CIPHER_NAME, TEST_KEY, sizeof(TEST_KEY)));
25 }
26
27 void TearDown() override {
28 delete cryptor;
29 TestFixture::TearDown();
30 }
31 };
32
33 TEST_F(TestCryptoOpensslDataCryptor, InvalidCipherName) {
34 EXPECT_EQ(-EINVAL, cryptor->init(nullptr, TEST_KEY, sizeof(TEST_KEY)));
35 EXPECT_EQ(-EINVAL, cryptor->init("", TEST_KEY, sizeof(TEST_KEY)));
36 EXPECT_EQ(-EINVAL, cryptor->init("Invalid", TEST_KEY, sizeof(TEST_KEY)));
37 }
38
39 TEST_F(TestCryptoOpensslDataCryptor, InvalidKey) {
40 EXPECT_EQ(-EINVAL, cryptor->init(TEST_CIPHER_NAME, nullptr, 0));
41 EXPECT_EQ(-EINVAL, cryptor->init(TEST_CIPHER_NAME, nullptr,
42 sizeof(TEST_KEY)));
43 EXPECT_EQ(-EINVAL, cryptor->init(TEST_CIPHER_NAME, TEST_KEY, 1));
44 }
45
46 TEST_F(TestCryptoOpensslDataCryptor, GetContextInvalidMode) {
47 EXPECT_EQ(nullptr, cryptor->get_context(static_cast<CipherMode>(-1)));
48 }
49
50 TEST_F(TestCryptoOpensslDataCryptor, ReturnNullContext) {
51 cryptor->return_context(nullptr, static_cast<CipherMode>(-1));
52 }
53
54 TEST_F(TestCryptoOpensslDataCryptor, ReturnContextInvalidMode) {
55 auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
56 ASSERT_NE(ctx, nullptr);
57 cryptor->return_context(ctx, CipherMode::CIPHER_MODE_DEC);
58 ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
59 ASSERT_NE(ctx, nullptr);
60 cryptor->return_context(ctx, static_cast<CipherMode>(-1));
61 }
62
63 TEST_F(TestCryptoOpensslDataCryptor, EncryptDecrypt) {
64 auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
65 ASSERT_NE(ctx, nullptr);
66 cryptor->init_context(ctx, TEST_IV, sizeof(TEST_IV));
67
68 unsigned char out[sizeof(TEST_DATA)];
69 ASSERT_EQ(sizeof(TEST_DATA),
70 cryptor->update_context(ctx, TEST_DATA, out, sizeof(TEST_DATA)));
71 cryptor->return_context(ctx, CipherMode::CIPHER_MODE_ENC);
72 ctx = cryptor->get_context(CipherMode::CIPHER_MODE_DEC);
73 ASSERT_NE(ctx, nullptr);
74 ASSERT_EQ(0, cryptor->init_context(ctx, TEST_IV, sizeof(TEST_IV)));
75 ASSERT_EQ(sizeof(TEST_DATA),
76 cryptor->update_context(ctx, out, out, sizeof(TEST_DATA)));
77 ASSERT_EQ(0, memcmp(out, TEST_DATA, sizeof(TEST_DATA)));
78 cryptor->return_context(ctx, CipherMode::CIPHER_MODE_DEC);
79 }
80
81 TEST_F(TestCryptoOpensslDataCryptor, ReuseContext) {
82 auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
83 ASSERT_NE(ctx, nullptr);
84
85 ASSERT_EQ(0, cryptor->init_context(ctx, TEST_IV, sizeof(TEST_IV)));
86 unsigned char out[sizeof(TEST_DATA)];
87 ASSERT_EQ(sizeof(TEST_DATA),
88 cryptor->update_context(ctx, TEST_DATA, out, sizeof(TEST_DATA)));
89
90 ASSERT_EQ(0, cryptor->init_context(ctx, TEST_IV_2, sizeof(TEST_IV_2)));
91 ASSERT_EQ(sizeof(TEST_DATA),
92 cryptor->update_context(ctx, TEST_DATA, out, sizeof(TEST_DATA)));
93
94 auto ctx2 = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
95 ASSERT_NE(ctx2, nullptr);
96
97 ASSERT_EQ(0, cryptor->init_context(ctx2, TEST_IV_2, sizeof(TEST_IV_2)));
98 unsigned char out2[sizeof(TEST_DATA)];
99 ASSERT_EQ(sizeof(TEST_DATA),
100 cryptor->update_context(ctx2, TEST_DATA, out2, sizeof(TEST_DATA)));
101
102 ASSERT_EQ(0, memcmp(out, out2, sizeof(TEST_DATA)));
103
104 cryptor->return_context(ctx, CipherMode::CIPHER_MODE_ENC);
105 cryptor->return_context(ctx2, CipherMode::CIPHER_MODE_ENC);
106 }
107
108 TEST_F(TestCryptoOpensslDataCryptor, InvalidIVLength) {
109 auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
110 ASSERT_NE(ctx, nullptr);
111
112 ASSERT_EQ(-EINVAL, cryptor->init_context(ctx, TEST_IV, 1));
113 cryptor->return_context(ctx, CipherMode::CIPHER_MODE_ENC);
114 }
115
116 } // namespace openssl
117 } // namespace crypto
118 } // namespace librbd