about summary refs log tree commit diff stats
path: root/archive/2025/summer/bsc_karidas/tests/unit/test_Crypto.cpp
blob: 1dc30da93254681347735a75d0f81ede1f725278 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <gtest/gtest.h>
#include "Crypto.hpp"
#include <string>
#include <vector>
#include <algorithm>

class CryptoTest : public ::testing::Test
{
protected:
    Crypto crypto;

    // Helper method to create a random key of proper size
    std::vector<uint8_t> createRandomKey()
    {
        std::vector<uint8_t> key(Crypto::KEY_SIZE);
        for (size_t i = 0; i < key.size(); ++i)
        {
            key[i] = static_cast<uint8_t>(rand() % 256);
        }
        return key;
    }

    // Helper method to create a dummy IV
    std::vector<uint8_t> createDummyIV()
    {
        return std::vector<uint8_t>(Crypto::GCM_IV_SIZE, 0x24);
    }

    // Helper method to convert string to byte vector
    std::vector<uint8_t> stringToBytes(const std::string &str)
    {
        return std::vector<uint8_t>(str.begin(), str.end());
    }

    // Helper method to convert byte vector to string
    std::string bytesToString(const std::vector<uint8_t> &bytes)
    {
        return std::string(bytes.begin(), bytes.end());
    }

    void SetUp() override
    {
        // Seed random number generator for consistent test results
        srand(42);
    }
};

// Test empty data encryption and decryption
TEST_F(CryptoTest, EmptyData)
{
    std::vector<uint8_t> emptyData;
    std::vector<uint8_t> key = createRandomKey();
    std::vector<uint8_t> iv = createDummyIV();

    // Encrypt empty data
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(emptyData), key, iv);
    EXPECT_TRUE(encrypted.empty());

    // Decrypt empty data
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, iv);
    EXPECT_TRUE(decrypted.empty());
}

// Test basic encryption and decryption
TEST_F(CryptoTest, BasicEncryptDecrypt)
{
    std::string testMessage = "This is a test message for encryption";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> key = createRandomKey();
    std::vector<uint8_t> iv = createDummyIV();

    // Encrypt the data
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(data), key, iv);
    EXPECT_FALSE(encrypted.empty());

    // The encrypted data should be different from the original
    EXPECT_NE(data, encrypted);

    // Decrypt the data
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, iv);

    // The decrypted data should match the original
    EXPECT_EQ(data, decrypted);
    EXPECT_EQ(testMessage, bytesToString(decrypted));
}

// Test encryption with various data sizes
TEST_F(CryptoTest, VariousDataSizes)
{
    std::vector<size_t> sizes = {10, 100, 1000, 10000};
    std::vector<uint8_t> key = createRandomKey();
    std::vector<uint8_t> iv = createDummyIV();

    for (size_t size : sizes)
    {
        // Create data of specified size
        std::vector<uint8_t> data(size);
        for (size_t i = 0; i < size; ++i)
        {
            data[i] = static_cast<uint8_t>(i % 256);
        }

        // Encrypt the data
        std::vector<uint8_t> encrypted = crypto.encrypt(std::move(data), key, iv);
        EXPECT_FALSE(encrypted.empty());

        // Decrypt the data
        std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, iv);

        // The decrypted data should match the original
        EXPECT_EQ(data, decrypted);
    }
}

// Test encryption with invalid key size
TEST_F(CryptoTest, InvalidKeySize)
{
    std::string testMessage = "Testing invalid key size";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> iv = createDummyIV();

    // Create keys with invalid sizes
    std::vector<uint8_t> shortKey(16); // Too short
    std::vector<uint8_t> longKey(64);  // Too long

    // Encryption with short key should throw
    EXPECT_THROW(crypto.encrypt(std::move(data), shortKey, iv), std::runtime_error);

    // Encryption with long key should throw
    EXPECT_THROW(crypto.encrypt(std::move(data), longKey, iv), std::runtime_error);
}

// Test encryption with invalid IV size
TEST_F(CryptoTest, InvalidIVSize)
{
    std::string testMessage = "Testing invalid IV size";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> key = createRandomKey();

    // Create IVs with invalid sizes
    std::vector<uint8_t> shortIV(8); // Too short
    std::vector<uint8_t> longIV(16); // Too long

    // Encryption with short IV should throw
    EXPECT_THROW(crypto.encrypt(std::move(data), key, shortIV), std::runtime_error);

    // Encryption with long IV should throw
    EXPECT_THROW(crypto.encrypt(std::move(data), key, longIV), std::runtime_error);
}

// Test decryption with wrong key
TEST_F(CryptoTest, WrongKey)
{
    std::string testMessage = "This should not decrypt correctly with wrong key";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> iv = createDummyIV();

    // Create two different keys
    std::vector<uint8_t> correctKey = createRandomKey();
    std::vector<uint8_t> wrongKey = createRandomKey();

    // Make sure the keys are different
    ASSERT_NE(correctKey, wrongKey);

    // Encrypt with the correct key
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(data), correctKey, iv);

    // Attempt to decrypt with the wrong key
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, wrongKey, iv);

    // The decryption should fail (return empty vector) or the result should be different
    // from the original data
    EXPECT_TRUE(decrypted.empty() || decrypted != data);
}

// Test decryption with wrong IV
TEST_F(CryptoTest, WrongIV)
{
    std::string testMessage = "This should not decrypt correctly with wrong IV";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> key = createRandomKey();

    // Create two different IVs
    std::vector<uint8_t> correctIV = createDummyIV();
    std::vector<uint8_t> wrongIV(Crypto::GCM_IV_SIZE, 0x42); // Different value

    // Make sure the IVs are different
    ASSERT_NE(correctIV, wrongIV);

    // Encrypt with the correct IV
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(data), key, correctIV);

    // Attempt to decrypt with the wrong IV
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, wrongIV);

    // The decryption should fail (return empty vector) or the result should be different
    // from the original data
    EXPECT_TRUE(decrypted.empty() || decrypted != data);
}

// Test tampering detection
TEST_F(CryptoTest, TamperingDetection)
{
    std::string testMessage = "This message should be protected against tampering";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> key = createRandomKey();
    std::vector<uint8_t> iv = createDummyIV();

    // Encrypt the data
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(data), key, iv);
    ASSERT_FALSE(encrypted.empty());

    // Tamper with the encrypted data (modify a byte in the middle)
    if (encrypted.size() > 20)
    {
        encrypted[encrypted.size() / 2] ^= 0xFF; // Flip all bits in one byte

        // Decryption should now fail or produce incorrect results
        std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, iv);
        EXPECT_TRUE(decrypted.empty() || decrypted != data);
    }
}

// Test binary data encryption and decryption
TEST_F(CryptoTest, BinaryData)
{
    // Create binary data with all possible byte values
    std::vector<uint8_t> binaryData(256);
    for (int i = 0; i < 256; ++i)
    {
        binaryData[i] = static_cast<uint8_t>(i);
    }

    std::vector<uint8_t> key = createRandomKey();
    std::vector<uint8_t> iv = createDummyIV();

    // Encrypt the binary data
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(binaryData), key, iv);
    EXPECT_FALSE(encrypted.empty());

    // Decrypt the data
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, iv);

    // The decrypted data should match the original
    EXPECT_EQ(binaryData, decrypted);
}

// Test large data encryption and decryption
TEST_F(CryptoTest, LargeData)
{
    // Create a large data set (1MB)
    const size_t size = 1024 * 1024;
    std::vector<uint8_t> largeData(size);
    for (size_t i = 0; i < size; ++i)
    {
        largeData[i] = static_cast<uint8_t>(i % 256);
    }

    std::vector<uint8_t> key = createRandomKey();
    std::vector<uint8_t> iv = createDummyIV();

    // Encrypt the large data
    std::vector<uint8_t> encrypted = crypto.encrypt(std::move(largeData), key, iv);
    EXPECT_FALSE(encrypted.empty());

    // Decrypt the data
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted, key, iv);

    // The decrypted data should match the original
    EXPECT_EQ(largeData, decrypted);
}

// Test encryption and decryption with a fixed key and IV (for reproducibility)
TEST_F(CryptoTest, FixedKeyAndIV)
{
    std::string testMessage = "Testing with fixed key and IV";
    std::vector<uint8_t> data = stringToBytes(testMessage);

    // Create a fixed key and IV
    std::vector<uint8_t> fixedKey(Crypto::KEY_SIZE, 0x42);   // Fill with the value 0x42
    std::vector<uint8_t> fixedIV(Crypto::GCM_IV_SIZE, 0x24); // Fill with the value 0x24

    // Encrypt with the fixed key and IV
    std::vector<uint8_t> encrypted1 = crypto.encrypt(std::move(data), fixedKey, fixedIV);
    EXPECT_FALSE(encrypted1.empty());

    // Decrypt with the same key and IV
    std::vector<uint8_t> decrypted = crypto.decrypt(encrypted1, fixedKey, fixedIV);
    EXPECT_EQ(data, decrypted);

    // The same data encrypted with the same key and IV should produce the same ciphertexts
    // unlike the previous version with random IVs
    std::vector<uint8_t> encrypted2 = crypto.encrypt(std::move(data), fixedKey, fixedIV);
    EXPECT_EQ(encrypted1, encrypted2); // This test should now PASS with fixed IV
}

// Test that different IVs produce different ciphertexts
TEST_F(CryptoTest, DifferentIVs)
{
    std::string testMessage = "Testing with different IVs";
    std::vector<uint8_t> data = stringToBytes(testMessage);
    std::vector<uint8_t> key = createRandomKey();

    // Create two different IVs
    std::vector<uint8_t> iv1(Crypto::GCM_IV_SIZE, 0x24);
    std::vector<uint8_t> iv2(Crypto::GCM_IV_SIZE, 0x42);

    // Encrypt with different IVs
    std::vector<uint8_t> encrypted1 = crypto.encrypt(std::move(data), key, iv1);
    std::vector<uint8_t> encrypted2 = crypto.encrypt(std::move(data), key, iv2);

    // The ciphertexts should be different
    EXPECT_NE(encrypted1, encrypted2);

    // But both should decrypt correctly with their respective IVs
    std::vector<uint8_t> decrypted1 = crypto.decrypt(encrypted1, key, iv1);
    std::vector<uint8_t> decrypted2 = crypto.decrypt(encrypted2, key, iv2);

    EXPECT_EQ(data, decrypted1);
    EXPECT_EQ(data, decrypted2);
}

// Main function that runs all the tests
int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}