Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 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 Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 16 | |
Alex Deymo | cddd2d0 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "shill/crypto_des_cbc.h" |
| 18 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 22 | #include <base/files/scoped_temp_dir.h> |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 25 | using base::FilePath; |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 26 | using std::string; |
| 27 | using std::vector; |
| 28 | using testing::Test; |
| 29 | |
| 30 | namespace shill { |
| 31 | |
| 32 | namespace { |
| 33 | const char kTestKey[] = "12345678"; |
| 34 | const char kTestIV[] = "abcdefgh"; |
| 35 | const char kEmptyPlain[] = ""; |
| 36 | const char kEmptyCipher[] = "02:4+O1a2KJVRM="; |
| 37 | const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM="; |
| 38 | const char kPlainText[] = "Hello world! ~123"; |
| 39 | const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp"; |
| 40 | const char kPlainVersion1[] = "This is a test!"; |
| 41 | const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w=="; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 42 | } // namespace |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 43 | |
| 44 | class CryptoDESCBCTest : public Test { |
| 45 | public: |
mukesh agrawal | d887589 | 2015-08-14 15:28:05 -0700 | [diff] [blame] | 46 | CryptoDESCBCTest() {} |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 47 | |
| 48 | protected: |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 49 | CryptoDESCBC crypto_; |
| 50 | }; |
| 51 | |
| 52 | TEST_F(CryptoDESCBCTest, GetID) { |
| 53 | EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID()); |
| 54 | } |
| 55 | |
| 56 | TEST_F(CryptoDESCBCTest, LoadKeyMatter) { |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 57 | base::ScopedTempDir temp_dir; |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 58 | 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 Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 68 | base::WriteFile(key_matter, matter.data(), matter.size() - 1); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 69 | EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); |
| 70 | EXPECT_TRUE(crypto_.key().empty()); |
| 71 | EXPECT_TRUE(crypto_.iv().empty()); |
| 72 | |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 73 | base::WriteFile(key_matter, matter.data(), matter.size()); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 74 | 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 Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 82 | base::WriteFile(key_matter, matter.data(), matter.size()); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 83 | 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 Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 87 | base::WriteFile(key_matter, " ", 1); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 88 | EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); |
| 89 | EXPECT_TRUE(crypto_.key().empty()); |
| 90 | EXPECT_TRUE(crypto_.iv().empty()); |
| 91 | } |
| 92 | |
| 93 | TEST_F(CryptoDESCBCTest, Encrypt) { |
| 94 | crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey)); |
| 95 | crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV)); |
| 96 | |
| 97 | string ciphertext; |
Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 98 | EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext)); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST_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 |