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.
5 #include "crypto/aead.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 const crypto::Aead::AeadAlgorithm kAllAlgorithms[]{
14 crypto::Aead::AES_128_CTR_HMAC_SHA256, crypto::Aead::AES_256_GCM,
15 crypto::Aead::AES_256_GCM_SIV,
18 class AeadTest : public testing::TestWithParam<crypto::Aead::AeadAlgorithm> {};
20 INSTANTIATE_TEST_CASE_P(, AeadTest, testing::ValuesIn(kAllAlgorithms));
22 TEST_P(AeadTest, SealOpen) {
23 crypto::Aead::AeadAlgorithm alg = GetParam();
24 crypto::Aead aead(alg);
25 std::string key(aead.KeyLength(), 0);
27 std::string nonce(aead.NonceLength(), 0);
28 std::string plaintext("this is the plaintext");
29 std::string ad("this is the additional data");
30 std::string ciphertext;
31 EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
32 EXPECT_LT(0U, ciphertext.size());
34 std::string decrypted;
35 EXPECT_TRUE(aead.Open(ciphertext, nonce, ad, &decrypted));
37 EXPECT_EQ(plaintext, decrypted);
40 TEST_P(AeadTest, SealOpenWrongKey) {
41 crypto::Aead::AeadAlgorithm alg = GetParam();
42 crypto::Aead aead(alg);
43 std::string key(aead.KeyLength(), 0);
44 std::string wrong_key(aead.KeyLength(), 1);
46 crypto::Aead aead_wrong_key(alg);
47 aead_wrong_key.Init(&wrong_key);
49 std::string nonce(aead.NonceLength(), 0);
50 std::string plaintext("this is the plaintext");
51 std::string ad("this is the additional data");
52 std::string ciphertext;
53 EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
54 EXPECT_LT(0U, ciphertext.size());
56 std::string decrypted;
57 EXPECT_FALSE(aead_wrong_key.Open(ciphertext, nonce, ad, &decrypted));
58 EXPECT_EQ(0U, decrypted.size());