Test basic encryption functionality.
| 135 | |
| 136 | /// Test basic encryption functionality. |
| 137 | TEST_F(OpenSSLUtilTest, Encryption) { |
| 138 | const int buffer_size = 1024 * 1024; |
| 139 | vector<uint8_t> original(buffer_size); |
| 140 | vector<uint8_t> prev_encrypted(buffer_size); |
| 141 | vector<uint8_t> encrypted(buffer_size); |
| 142 | vector<uint8_t> decrypted(buffer_size); |
| 143 | GenerateRandomData(original.data(), buffer_size); |
| 144 | |
| 145 | // Check GCM, CTR and CFB |
| 146 | AES_CIPHER_MODE modes[] = { |
| 147 | AES_CIPHER_MODE::AES_256_GCM, |
| 148 | AES_CIPHER_MODE::AES_256_CTR, |
| 149 | AES_CIPHER_MODE::AES_256_CFB, |
| 150 | AES_CIPHER_MODE::AES_128_GCM |
| 151 | }; |
| 152 | for (auto m : modes) { |
| 153 | // Iterate multiple times to ensure that key regeneration works correctly. |
| 154 | EncryptionKey key; |
| 155 | for (int i = 0; i < 2; ++i) { |
| 156 | // Generate a new key for each iteration. |
| 157 | ASSERT_OK(key.InitializeRandom(AES_BLOCK_SIZE, m)); |
| 158 | |
| 159 | // Check that OpenSSL is happy with the amount of entropy we're feeding it. |
| 160 | DCHECK_EQ(1, RAND_status()); |
| 161 | |
| 162 | ASSERT_OK(key.Encrypt(original.data(), buffer_size, encrypted.data())); |
| 163 | if (i > 0) { |
| 164 | // Check that we're not somehow reusing the same key. |
| 165 | ASSERT_NE(0, memcmp(encrypted.data(), prev_encrypted.data(), buffer_size)); |
| 166 | } |
| 167 | memcpy(prev_encrypted.data(), encrypted.data(), buffer_size); |
| 168 | |
| 169 | // We should get the original data by decrypting it. |
| 170 | ASSERT_OK(key.Decrypt(encrypted.data(), buffer_size, decrypted.data())); |
| 171 | ASSERT_EQ(0, memcmp(original.data(), decrypted.data(), buffer_size)); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Test to check whether key and mode are validated in InitializeFields(). |
| 177 | TEST_F(OpenSSLUtilTest, ValidateInitialize) { |
nothing calls this directly
no test coverage detected