1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "test/librbd/test_fixture.h"
5 #include "librbd/crypto/openssl/DataCryptor.h"
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};
17 struct TestCryptoOpensslDataCryptor
: public TestFixture
{
20 void SetUp() override
{
22 cryptor
= new DataCryptor(reinterpret_cast<CephContext
*>(m_ioctx
.cct()));
24 cryptor
->init(TEST_CIPHER_NAME
, TEST_KEY
, sizeof(TEST_KEY
)));
27 void TearDown() override
{
29 TestFixture::TearDown();
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
)));
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,
43 EXPECT_EQ(-EINVAL
, cryptor
->init(TEST_CIPHER_NAME
, TEST_KEY
, 1));
46 TEST_F(TestCryptoOpensslDataCryptor
, GetContextInvalidMode
) {
47 EXPECT_EQ(nullptr, cryptor
->get_context(static_cast<CipherMode
>(-1)));
50 TEST_F(TestCryptoOpensslDataCryptor
, ReturnNullContext
) {
51 cryptor
->return_context(nullptr, static_cast<CipherMode
>(-1));
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));
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
));
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
);
81 TEST_F(TestCryptoOpensslDataCryptor
, ReuseContext
) {
82 auto ctx
= cryptor
->get_context(CipherMode::CIPHER_MODE_ENC
);
83 ASSERT_NE(ctx
, nullptr);
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
)));
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
)));
94 auto ctx2
= cryptor
->get_context(CipherMode::CIPHER_MODE_ENC
);
95 ASSERT_NE(ctx2
, nullptr);
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
)));
102 ASSERT_EQ(0, memcmp(out
, out2
, sizeof(TEST_DATA
)));
104 cryptor
->return_context(ctx
, CipherMode::CIPHER_MODE_ENC
);
105 cryptor
->return_context(ctx2
, CipherMode::CIPHER_MODE_ENC
);
108 TEST_F(TestCryptoOpensslDataCryptor
, InvalidIVLength
) {
109 auto ctx
= cryptor
->get_context(CipherMode::CIPHER_MODE_ENC
);
110 ASSERT_NE(ctx
, nullptr);
112 ASSERT_EQ(-EINVAL
, cryptor
->init_context(ctx
, TEST_IV
, 1));
113 cryptor
->return_context(ctx
, CipherMode::CIPHER_MODE_ENC
);
116 } // namespace openssl
117 } // namespace crypto
118 } // namespace librbd