Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | cddd2d0 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "shill/crypto_des_cbc.h" |
| 6 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 9 | #include <base/files/file_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 10 | #include <base/files/scoped_temp_dir.h> |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 13 | #include "shill/glib.h" |
| 14 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 15 | using base::FilePath; |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 16 | using std::string; |
| 17 | using std::vector; |
| 18 | using testing::Test; |
| 19 | |
| 20 | namespace shill { |
| 21 | |
| 22 | namespace { |
| 23 | const char kTestKey[] = "12345678"; |
| 24 | const char kTestIV[] = "abcdefgh"; |
| 25 | const char kEmptyPlain[] = ""; |
| 26 | const char kEmptyCipher[] = "02:4+O1a2KJVRM="; |
| 27 | const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM="; |
| 28 | const char kPlainText[] = "Hello world! ~123"; |
| 29 | const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp"; |
| 30 | const char kPlainVersion1[] = "This is a test!"; |
| 31 | const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w=="; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 32 | } // namespace |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 33 | |
| 34 | class CryptoDESCBCTest : public Test { |
| 35 | public: |
| 36 | CryptoDESCBCTest() : crypto_(&glib_) {} |
| 37 | |
| 38 | protected: |
| 39 | GLib glib_; // Use actual GLib for the test. |
| 40 | CryptoDESCBC crypto_; |
| 41 | }; |
| 42 | |
| 43 | TEST_F(CryptoDESCBCTest, GetID) { |
| 44 | EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID()); |
| 45 | } |
| 46 | |
| 47 | TEST_F(CryptoDESCBCTest, LoadKeyMatter) { |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 48 | base::ScopedTempDir temp_dir; |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 50 | const char kKeyMatterFile[] = "key-matter-file"; |
| 51 | FilePath key_matter = temp_dir.path().Append(kKeyMatterFile); |
| 52 | |
| 53 | EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); |
| 54 | EXPECT_TRUE(crypto_.key().empty()); |
| 55 | EXPECT_TRUE(crypto_.iv().empty()); |
| 56 | |
| 57 | string matter = string(kTestIV) + kTestKey; |
| 58 | |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 59 | base::WriteFile(key_matter, matter.data(), matter.size() - 1); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 60 | EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); |
| 61 | EXPECT_TRUE(crypto_.key().empty()); |
| 62 | EXPECT_TRUE(crypto_.iv().empty()); |
| 63 | |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 64 | base::WriteFile(key_matter, matter.data(), matter.size()); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 65 | EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter)); |
| 66 | EXPECT_EQ(kTestKey, string(crypto_.key().begin(), crypto_.key().end())); |
| 67 | EXPECT_EQ(kTestIV, string(crypto_.iv().begin(), crypto_.iv().end())); |
| 68 | |
| 69 | const char kKey2[] = "ABCDEFGH"; |
| 70 | const char kIV2[] = "87654321"; |
| 71 | matter = string("X") + kIV2 + kKey2; |
| 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(kKey2, string(crypto_.key().begin(), crypto_.key().end())); |
| 76 | EXPECT_EQ(kIV2, string(crypto_.iv().begin(), crypto_.iv().end())); |
| 77 | |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 78 | base::WriteFile(key_matter, " ", 1); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 79 | EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); |
| 80 | EXPECT_TRUE(crypto_.key().empty()); |
| 81 | EXPECT_TRUE(crypto_.iv().empty()); |
| 82 | } |
| 83 | |
| 84 | TEST_F(CryptoDESCBCTest, Encrypt) { |
| 85 | crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey)); |
| 86 | crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV)); |
| 87 | |
| 88 | string ciphertext; |
Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 89 | EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext)); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(CryptoDESCBCTest, Decrypt) { |
| 93 | crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey)); |
| 94 | crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV)); |
| 95 | |
| 96 | string plaintext; |
| 97 | EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext)); |
| 98 | EXPECT_EQ(kEmptyPlain, plaintext); |
| 99 | EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext)); |
| 100 | EXPECT_EQ(kPlainText, plaintext); |
| 101 | EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext)); |
| 102 | EXPECT_EQ(kPlainVersion1, plaintext); |
| 103 | |
| 104 | EXPECT_FALSE(crypto_.Decrypt("random", &plaintext)); |
| 105 | EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext)); |
| 106 | EXPECT_FALSE(crypto_.Decrypt("~", &plaintext)); |
| 107 | EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext)); |
| 108 | EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext)); |
| 109 | EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext)); |
| 110 | |
| 111 | // echo -n 12345678 | base64 |
| 112 | EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext)); |
| 113 | } |
| 114 | |
| 115 | } // namespace shill |