blob: 59b7a085aff10720431e4cba34be4876623b5d96 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov823c47e2011-06-27 16:15:35 -070016
Alex Deymocddd2d02014-11-10 19:55:35 -080017#include "shill/crypto_des_cbc.h"
18
Alex Vakulenko8a532292014-06-16 17:18:44 -070019#include <string>
20
Ben Chan11c213f2014-09-05 08:21:06 -070021#include <base/files/file_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -080022#include <base/files/scoped_temp_dir.h>
Darin Petkov823c47e2011-06-27 16:15:35 -070023#include <gtest/gtest.h>
24
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080025using base::FilePath;
Darin Petkov823c47e2011-06-27 16:15:35 -070026using std::string;
27using std::vector;
28using testing::Test;
29
30namespace shill {
31
32namespace {
33const char kTestKey[] = "12345678";
34const char kTestIV[] = "abcdefgh";
35const char kEmptyPlain[] = "";
36const char kEmptyCipher[] = "02:4+O1a2KJVRM=";
37const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM=";
38const char kPlainText[] = "Hello world! ~123";
39const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp";
40const char kPlainVersion1[] = "This is a test!";
41const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w==";
Alex Vakulenko8a532292014-06-16 17:18:44 -070042} // namespace
Darin Petkov823c47e2011-06-27 16:15:35 -070043
44class CryptoDESCBCTest : public Test {
45 public:
mukesh agrawald8875892015-08-14 15:28:05 -070046 CryptoDESCBCTest() {}
Darin Petkov823c47e2011-06-27 16:15:35 -070047
48 protected:
Darin Petkov823c47e2011-06-27 16:15:35 -070049 CryptoDESCBC crypto_;
50};
51
52TEST_F(CryptoDESCBCTest, GetID) {
53 EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID());
54}
55
56TEST_F(CryptoDESCBCTest, LoadKeyMatter) {
Paul Stewart5ad16062013-02-21 18:10:48 -080057 base::ScopedTempDir temp_dir;
Darin Petkov823c47e2011-06-27 16:15:35 -070058 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
59 const char kKeyMatterFile[] = "key-matter-file";
60 FilePath key_matter = temp_dir.path().Append(kKeyMatterFile);
61
62 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
63 EXPECT_TRUE(crypto_.key().empty());
64 EXPECT_TRUE(crypto_.iv().empty());
65
66 string matter = string(kTestIV) + kTestKey;
67
Ben Chan6fbf64f2014-05-21 18:07:01 -070068 base::WriteFile(key_matter, matter.data(), matter.size() - 1);
Darin Petkov823c47e2011-06-27 16:15:35 -070069 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
70 EXPECT_TRUE(crypto_.key().empty());
71 EXPECT_TRUE(crypto_.iv().empty());
72
Ben Chan6fbf64f2014-05-21 18:07:01 -070073 base::WriteFile(key_matter, matter.data(), matter.size());
Darin Petkov823c47e2011-06-27 16:15:35 -070074 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
75 EXPECT_EQ(kTestKey, string(crypto_.key().begin(), crypto_.key().end()));
76 EXPECT_EQ(kTestIV, string(crypto_.iv().begin(), crypto_.iv().end()));
77
78 const char kKey2[] = "ABCDEFGH";
79 const char kIV2[] = "87654321";
80 matter = string("X") + kIV2 + kKey2;
81
Ben Chan6fbf64f2014-05-21 18:07:01 -070082 base::WriteFile(key_matter, matter.data(), matter.size());
Darin Petkov823c47e2011-06-27 16:15:35 -070083 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
84 EXPECT_EQ(kKey2, string(crypto_.key().begin(), crypto_.key().end()));
85 EXPECT_EQ(kIV2, string(crypto_.iv().begin(), crypto_.iv().end()));
86
Ben Chan6fbf64f2014-05-21 18:07:01 -070087 base::WriteFile(key_matter, " ", 1);
Darin Petkov823c47e2011-06-27 16:15:35 -070088 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
89 EXPECT_TRUE(crypto_.key().empty());
90 EXPECT_TRUE(crypto_.iv().empty());
91}
92
93TEST_F(CryptoDESCBCTest, Encrypt) {
94 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
95 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
96
97 string ciphertext;
Darin Petkov7326cd82012-03-30 11:02:01 +020098 EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext));
Darin Petkov823c47e2011-06-27 16:15:35 -070099}
100
101TEST_F(CryptoDESCBCTest, Decrypt) {
102 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
103 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
104
105 string plaintext;
106 EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext));
107 EXPECT_EQ(kEmptyPlain, plaintext);
108 EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext));
109 EXPECT_EQ(kPlainText, plaintext);
110 EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext));
111 EXPECT_EQ(kPlainVersion1, plaintext);
112
113 EXPECT_FALSE(crypto_.Decrypt("random", &plaintext));
114 EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext));
115 EXPECT_FALSE(crypto_.Decrypt("~", &plaintext));
116 EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext));
117 EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext));
118 EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext));
119
120 // echo -n 12345678 | base64
121 EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext));
122}
123
124} // namespace shill