diff options
author | Spencer Jackson <spencer.jackson@mongodb.com> | 2021-11-03 21:17:02 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-01-11 18:24:37 +0000 |
commit | 1535e0bd581310b0762ca1af49e4be1c6e62d8c5 (patch) | |
tree | 297b672e82a68062a84b1800ddae1b6b46984f91 /src/mongo/crypto/symmetric_crypto_test.cpp | |
parent | f18bb1e1c2d7bedeb3c08964e6a65bcc794e2a2a (diff) | |
download | mongo-1535e0bd581310b0762ca1af49e4be1c6e62d8c5.tar.gz |
SERVER-61020 Add tests for corrupt GCM tags
Diffstat (limited to 'src/mongo/crypto/symmetric_crypto_test.cpp')
-rw-r--r-- | src/mongo/crypto/symmetric_crypto_test.cpp | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/mongo/crypto/symmetric_crypto_test.cpp b/src/mongo/crypto/symmetric_crypto_test.cpp index 47834b77770..7af1cf6df1f 100644 --- a/src/mongo/crypto/symmetric_crypto_test.cpp +++ b/src/mongo/crypto/symmetric_crypto_test.cpp @@ -522,6 +522,7 @@ public: const size_t kBufferSize = test.plaintext.size(); { + // Validate encryption std::vector<uint8_t> encryptionResult(kBufferSize); auto cipherLen = uassertStatusOK(encryptor->update(asUint8(test.plaintext.c_str()), test.plaintext.size(), @@ -535,17 +536,21 @@ public: hexblob::encode( StringData(asChar(encryptionResult.data()), encryptionResult.size()))); + // The symmetric crypto framework uses 12 byte GCM tags. The tags used in NIST test + // vectors can be larger than 12 bytes, but may be truncated. + std::array<std::uint8_t, 12> tag; const auto taglen = uassertStatusOK(encryptor->finalizeTag(tag.data(), tag.size())); ASSERT_EQ(tag.size(), taglen); - ASSERT_EQ(hexblob::encode(StringData(test.tag.data(), test.tag.size())).substr(0, 24), + ASSERT_EQ(hexblob::encode(StringData(test.tag.data(), test.tag.size())) + .substr(0, aesGCMTagSize * 2), hexblob::encode(StringData(asChar(tag.data()), tag.size()))); } { // Validate decryption auto decryptor = uassertStatusOK(crypto::SymmetricDecryptor::create( key, mode, asUint8(test.iv.c_str()), test.iv.size())); - uassertStatusOK(decryptor->updateTag(asUint8(test.tag.data()), 12)); + uassertStatusOK(decryptor->updateTag(asUint8(test.tag.data()), aesGCMTagSize)); ASSERT_OK(decryptor->addAuthenticatedData(asUint8(test.a.c_str()), test.a.size())); std::vector<uint8_t> decryptionResult(kBufferSize); @@ -561,6 +566,23 @@ public: hexblob::encode( StringData(asChar(decryptionResult.data()), decryptionResult.size()))); } + { + // Validate that decryption with incorrect tag does not succeed + auto decryptor = uassertStatusOK(crypto::SymmetricDecryptor::create( + key, mode, asUint8(test.iv.c_str()), test.iv.size())); + auto tag = test.tag; + tag[0]++; + uassertStatusOK(decryptor->updateTag(asUint8(tag.data()), aesGCMTagSize)); + + ASSERT_OK(decryptor->addAuthenticatedData(asUint8(test.a.c_str()), test.a.size())); + std::vector<uint8_t> decryptionResult(kBufferSize); + auto decipherLen = uassertStatusOK(decryptor->update(asUint8(test.ciphertext.c_str()), + test.ciphertext.size(), + decryptionResult.data(), + decryptionResult.size())); + ASSERT_NOT_OK(decryptor->finalize(decryptionResult.data() + decipherLen, + decryptionResult.size() - decipherLen)); + } } }; |