blob: 446bca2cb02f362bd435d09210ceebb21500bdb0 [file] [log] [blame]
Daniel Erat59c5f4b2015-08-24 12:50:25 -06001// 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
5#include "crypto/aead_openssl.h"
6
7#include <string>
8
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13#if defined(USE_OPENSSL)
14
15TEST(AeadTest, SealOpen) {
16 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256);
17 std::string key(aead.KeyLength(), 0);
18 aead.Init(&key);
19 std::string nonce(aead.NonceLength(), 0);
20 std::string plaintext("this is the plaintext");
21 std::string ad("this is the additional data");
22 std::string ciphertext;
23 EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
24 EXPECT_LT(0U, ciphertext.size());
25
26 std::string decrypted;
27 EXPECT_TRUE(aead.Open(ciphertext, nonce, ad, &decrypted));
28
29 EXPECT_EQ(plaintext, decrypted);
30}
31
32TEST(AeadTest, SealOpenWrongKey) {
33 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256);
34 std::string key(aead.KeyLength(), 0);
35 std::string wrong_key(aead.KeyLength(), 1);
36 aead.Init(&key);
37 crypto::Aead aead_wrong_key(crypto::Aead::AES_128_CTR_HMAC_SHA256);
38 aead_wrong_key.Init(&wrong_key);
39
40 std::string nonce(aead.NonceLength(), 0);
41 std::string plaintext("this is the plaintext");
42 std::string ad("this is the additional data");
43 std::string ciphertext;
44 EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
45 EXPECT_LT(0U, ciphertext.size());
46
47 std::string decrypted;
48 EXPECT_FALSE(aead_wrong_key.Open(ciphertext, nonce, ad, &decrypted));
49 EXPECT_EQ(0U, decrypted.size());
50}
51
52#endif
53
54} // namespace