-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSSM-4294 Fixed buffer overrun in test CipherTest.cipher1
Signed-off-by: Ted Poole <tpoole@redhat.com>
- Loading branch information
Showing
3 changed files
with
40 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,47 @@ | ||
#include <gtest/gtest.h> | ||
#include <openssl/cipher.h> | ||
#include <openssl/rand.h> | ||
|
||
TEST(CipherTest, cipher1) { | ||
uint8_t key[EVP_MAX_KEY_LENGTH] = {0}; | ||
uint8_t iv[EVP_MAX_IV_LENGTH] = {1}; | ||
|
||
uint8_t plaintext1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; | ||
int plainlen1 = sizeof(plaintext1); | ||
|
||
uint8_t plaintext2[sizeof(plaintext1)]; | ||
int plainlen2; | ||
|
||
uint8_t ciphertext[1024]; | ||
int cipherlen; | ||
|
||
int tmplen; | ||
|
||
TEST(CipherTest, cipher1) { | ||
const EVP_CIPHER *cipher = EVP_aes_256_cbc(); | ||
|
||
const EVP_CIPHER *aes_256_cbc = EVP_aes_256_cbc(); | ||
ASSERT_TRUE(cipher); | ||
|
||
EXPECT_TRUE(aes_256_cbc); | ||
std::vector<uint8_t> key(EVP_CIPHER_key_length(cipher)); | ||
std::vector<uint8_t> iv(EVP_CIPHER_iv_length(cipher)); | ||
|
||
ASSERT_EQ(1, RAND_bytes(key.data(), key.size())); | ||
ASSERT_EQ(1, RAND_bytes(iv.data(), iv.size())); | ||
|
||
EXPECT_EQ(16, EVP_CIPHER_iv_length(aes_256_cbc)); | ||
EXPECT_EQ(32, EVP_CIPHER_key_length(aes_256_cbc)); | ||
std::vector<uint8_t> plaintext1 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; | ||
std::vector<uint8_t> ciphertext; | ||
std::vector<uint8_t> plaintext2; | ||
|
||
{ | ||
int l1, l2; | ||
bssl::UniquePtr<EVP_CIPHER_CTX> ctx(EVP_CIPHER_CTX_new()); | ||
|
||
EXPECT_EQ(1, EVP_EncryptInit_ex(ctx.get(), aes_256_cbc, nullptr, key, iv)); | ||
EXPECT_EQ(1, EVP_EncryptUpdate(ctx.get(), ciphertext, &cipherlen, plaintext1, plainlen1)); | ||
EXPECT_EQ(1, EVP_EncryptFinal_ex(ctx.get(), ciphertext + cipherlen, &tmplen)); | ||
cipherlen += tmplen; | ||
ciphertext.resize(plaintext1.size() + EVP_CIPHER_block_size(cipher)); | ||
ASSERT_EQ(1, EVP_EncryptInit_ex(ctx.get(), cipher, nullptr, key.data(), iv.data())); | ||
ASSERT_EQ(1, EVP_EncryptUpdate(ctx.get(), ciphertext.data(), &l1, plaintext1.data(), plaintext1.size())); | ||
ASSERT_EQ(1, EVP_EncryptFinal_ex(ctx.get(), ciphertext.data() + l1, &l2)); | ||
ciphertext.resize(l1 + l2); // Resize to the actual encrypted byte count | ||
} | ||
|
||
{ | ||
int l1, l2; | ||
bssl::UniquePtr<EVP_CIPHER_CTX> ctx(EVP_CIPHER_CTX_new()); | ||
|
||
EXPECT_EQ(1, EVP_DecryptInit_ex(ctx.get(), aes_256_cbc, nullptr, key, iv)); | ||
EXPECT_EQ(1, EVP_DecryptUpdate(ctx.get(), plaintext2, &plainlen2, ciphertext, cipherlen)); | ||
EXPECT_EQ(1, EVP_DecryptFinal_ex(ctx.get(), plaintext2 + plainlen2, &tmplen)); | ||
plainlen2 += tmplen; | ||
} | ||
|
||
EXPECT_EQ(plainlen1, plainlen2); | ||
for(auto i = 0; i < plainlen1; i++) { | ||
EXPECT_EQ(plaintext1[i], plaintext2[i]); | ||
plaintext2.resize(plaintext1.size() + EVP_CIPHER_block_size(cipher)); | ||
ASSERT_EQ(1, EVP_DecryptInit_ex(ctx.get(), cipher, nullptr, key.data(), iv.data())); | ||
ASSERT_EQ(1, EVP_DecryptUpdate(ctx.get(), plaintext2.data(), &l1, ciphertext.data(), ciphertext.size())); | ||
ASSERT_EQ(1, EVP_DecryptFinal_ex(ctx.get(), plaintext2.data() + l1, &l2)); | ||
plaintext2.resize(l1 + l2); // Resize to the actual decrypted byte count | ||
} | ||
} | ||
|
||
ASSERT_EQ(plaintext1, plaintext2); | ||
} |