aead_unittest.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "crypto/aead.h"
  5. #include <string>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace {
  8. const crypto::Aead::AeadAlgorithm kAllAlgorithms[]{
  9. crypto::Aead::AES_128_CTR_HMAC_SHA256,
  10. crypto::Aead::AES_256_GCM,
  11. crypto::Aead::AES_256_GCM_SIV,
  12. crypto::Aead::CHACHA20_POLY1305,
  13. };
  14. class AeadTest : public testing::TestWithParam<crypto::Aead::AeadAlgorithm> {};
  15. INSTANTIATE_TEST_SUITE_P(All, AeadTest, testing::ValuesIn(kAllAlgorithms));
  16. TEST_P(AeadTest, SealOpen) {
  17. crypto::Aead::AeadAlgorithm alg = GetParam();
  18. crypto::Aead aead(alg);
  19. std::string key(aead.KeyLength(), 0);
  20. aead.Init(&key);
  21. std::string nonce(aead.NonceLength(), 0);
  22. std::string plaintext("this is the plaintext");
  23. std::string ad("this is the additional data");
  24. std::string ciphertext;
  25. EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
  26. EXPECT_LT(0U, ciphertext.size());
  27. std::string decrypted;
  28. EXPECT_TRUE(aead.Open(ciphertext, nonce, ad, &decrypted));
  29. EXPECT_EQ(plaintext, decrypted);
  30. }
  31. TEST_P(AeadTest, SealOpenSpan) {
  32. crypto::Aead::AeadAlgorithm alg = GetParam();
  33. crypto::Aead aead(alg);
  34. std::vector<uint8_t> key(aead.KeyLength(), 0u);
  35. aead.Init(key);
  36. std::vector<uint8_t> nonce(aead.NonceLength(), 0u);
  37. static constexpr uint8_t kPlaintext[] = "plaintext";
  38. static constexpr uint8_t kAdditionalData[] = "additional data input";
  39. std::vector<uint8_t> ciphertext =
  40. aead.Seal(kPlaintext, nonce, kAdditionalData);
  41. EXPECT_LT(sizeof(kPlaintext), ciphertext.size());
  42. absl::optional<std::vector<uint8_t>> decrypted =
  43. aead.Open(ciphertext, nonce, kAdditionalData);
  44. ASSERT_TRUE(decrypted);
  45. ASSERT_EQ(decrypted->size(), sizeof(kPlaintext));
  46. ASSERT_EQ(0, memcmp(decrypted->data(), kPlaintext, sizeof(kPlaintext)));
  47. std::vector<uint8_t> wrong_key(aead.KeyLength(), 1u);
  48. crypto::Aead aead_wrong_key(alg);
  49. aead_wrong_key.Init(wrong_key);
  50. decrypted = aead_wrong_key.Open(ciphertext, nonce, kAdditionalData);
  51. EXPECT_FALSE(decrypted);
  52. }
  53. TEST_P(AeadTest, SealOpenWrongKey) {
  54. crypto::Aead::AeadAlgorithm alg = GetParam();
  55. crypto::Aead aead(alg);
  56. std::string key(aead.KeyLength(), 0);
  57. std::string wrong_key(aead.KeyLength(), 1);
  58. aead.Init(&key);
  59. crypto::Aead aead_wrong_key(alg);
  60. aead_wrong_key.Init(&wrong_key);
  61. std::string nonce(aead.NonceLength(), 0);
  62. std::string plaintext("this is the plaintext");
  63. std::string ad("this is the additional data");
  64. std::string ciphertext;
  65. EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
  66. EXPECT_LT(0U, ciphertext.size());
  67. std::string decrypted;
  68. EXPECT_FALSE(aead_wrong_key.Open(ciphertext, nonce, ad, &decrypted));
  69. EXPECT_EQ(0U, decrypted.size());
  70. }
  71. } // namespace