File: os_crypt_linux_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (188 lines) | stat: -rw-r--r-- 6,304 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/os_crypt/sync/os_crypt.h"

#include <array>
#include <string>

#include "base/functional/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "components/os_crypt/sync/key_storage_linux.h"
#include "components/os_crypt/sync/os_crypt_mocker_linux.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

std::unique_ptr<KeyStorageLinux> GetNullKeyStorage() {
  return nullptr;
}

class OSCryptLinuxTest : public testing::Test {
 public:
  OSCryptLinuxTest() = default;

  OSCryptLinuxTest(const OSCryptLinuxTest&) = delete;
  OSCryptLinuxTest& operator=(const OSCryptLinuxTest&) = delete;

  ~OSCryptLinuxTest() override = default;

  void SetUp() override {
    OSCryptMockerLinux::SetUp();
    OSCrypt::SetEncryptionPasswordForTesting("something");
  }

  void TearDown() override { OSCryptMockerLinux::TearDown(); }
};

TEST_F(OSCryptLinuxTest, VerifyV10) {
  const std::string originaltext = "hello";
  std::string ciphertext;
  std::string decipheredtext;

  OSCrypt::SetEncryptionPasswordForTesting("peanuts");
  ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
  OSCrypt::SetEncryptionPasswordForTesting("not_peanuts");
  ciphertext = ciphertext.substr(3).insert(0, "v10");
  ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
  ASSERT_EQ(originaltext, decipheredtext);
}

TEST_F(OSCryptLinuxTest, VerifyV11) {
  const std::string originaltext = "hello";
  std::string ciphertext;
  std::string decipheredtext;

  OSCrypt::SetEncryptionPasswordForTesting(std::string());
  ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
  ASSERT_EQ(ciphertext.substr(0, 3), "v11");
  ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
  ASSERT_EQ(originaltext, decipheredtext);
}

TEST_F(OSCryptLinuxTest, IsEncryptionAvailable) {
  EXPECT_TRUE(OSCrypt::IsEncryptionAvailable());
  OSCrypt::ClearCacheForTesting();
  // Mock the GetKeyStorage function.
  OSCrypt::UseMockKeyStorageForTesting(base::BindOnce(&GetNullKeyStorage));
  EXPECT_FALSE(OSCrypt::IsEncryptionAvailable());
}

TEST_F(OSCryptLinuxTest, SetRawEncryptionKey) {
  const std::string originaltext = "hello";
  std::string ciphertext;
  std::string decipheredtext;

  // Encrypt with not_peanuts and save the raw encryption key.
  OSCrypt::SetEncryptionPasswordForTesting("not_peanuts");
  ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
  ASSERT_EQ(ciphertext.substr(0, 3), "v11");
  std::string raw_key = OSCrypt::GetRawEncryptionKey();
  ASSERT_FALSE(raw_key.empty());

  // Clear the cached encryption key.
  OSCrypt::ClearCacheForTesting();

  // Set the raw encryption key and make sure decryption works.
  OSCrypt::SetRawEncryptionKey(raw_key);
  ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
  ASSERT_EQ(originaltext, decipheredtext);
}

// Because of crbug.com/1195256, there might be data that is encrypted with an
// empty key. These should remain decryptable, even when a proper key is
// available.
TEST_F(OSCryptLinuxTest, DecryptWhenEncryptionKeyIsEmpty) {
  base::HistogramTester histogram_tester;
  const std::string originaltext = "hello";
  std::string ciphertext;
  std::string decipheredtext;

  // Encrypt a value using "" as the key.
  OSCrypt::SetEncryptionPasswordForTesting("");
  ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));

  // Set a proper encryption key.
  OSCrypt::ClearCacheForTesting();
  OSCrypt::SetEncryptionPasswordForTesting("key");
  // The text is decryptable.
  ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
  EXPECT_EQ(originaltext, decipheredtext);
  histogram_tester.ExpectUniqueSample("OSCrypt.Linux.DecryptedWithEmptyKey",
                                      true, 1);
}

struct KeyDerivationKnownAnswer {
  const char* password;
  std::array<uint8_t, 16> answer;
};

TEST_F(OSCryptLinuxTest, KeyDerivationKnownAnswers) {
  // Known answers from PBKDF2-HMAC-SHA1 producing 128 bits of output with 1
  // round and salted with "saltysalt".
  // TODO(https://crbug.com/380912393): re-enable clang-format for this block.
  // clang-format off
  constexpr auto kCases = std::to_array<KeyDerivationKnownAnswer>({
    {
      // The hardcoded V10 obfuscation key.
      .password = "peanuts",
      .answer = {
        0xfd, 0x62, 0x1f, 0xe5, 0xa2, 0xb4, 0x02, 0x53,
        0x9d, 0xfa, 0x14, 0x7c, 0xa9, 0x27, 0x27, 0x78,
      },
    },
    {
      // The empty password fallback obfuscation key.
      .password = "",
      .answer = {
        0xd0, 0xd0, 0xec, 0x9c, 0x7d, 0x77, 0xd4, 0x3a,
        0xc5, 0x41, 0x87, 0xfa, 0x48, 0x18, 0xd1, 0x7f,
      },
    },
    {
      // An example V11 key generated from a random password.
      .password = "zxqfb",
      .answer = {
        0xb7, 0x56, 0x30, 0x74, 0x74, 0xb0, 0x0d, 0xa3,
        0x55, 0xf7, 0x73, 0xf0, 0x2f, 0x86, 0x1a, 0xe4,
      },
    },
  });
  // clang-format on

  for (const auto& c : kCases) {
    OSCrypt::SetEncryptionPasswordForTesting(c.password);
    std::string r = OSCrypt::GetRawEncryptionKey();
    EXPECT_EQ(c.answer, base::as_byte_span(r));
  }
}

TEST_F(OSCryptLinuxTest, EncryptDecryptKnownAnswers) {
  OSCrypt::SetEncryptionPasswordForTesting("zxqfb");
  const std::string kInput = "Hello, World! This is a longer string.";

  // The input string, encrypted using AES-128-CBC, with the "V11" version
  // prefix and with a PKCS#5 padding block at the end.
  // clang-format off
  constexpr auto kExpectedCiphertext = std::to_array<uint8_t>({
    0x76, 0x31, 0x31,
    0x77, 0x21, 0x00, 0x60, 0x87, 0x09, 0xe8, 0x94,
    0xde, 0x63, 0x4f, 0x80, 0x36, 0x40, 0xb7, 0xdd,
    0x80, 0xe1, 0x2c, 0x6f, 0x9a, 0xb6, 0x6b, 0xba,
    0x93, 0xaf, 0xec, 0xb5, 0x89, 0xe6, 0x12, 0x28,
    0xf0, 0x85, 0xa2, 0xbb, 0xf9, 0x30, 0x30, 0x87,
    0xf8, 0xd1, 0x10, 0x4e, 0x97, 0xe1, 0x75, 0x73,
  });
  // clang-format on

  std::string ciphertext;
  ASSERT_TRUE(OSCrypt::EncryptString(kInput, &ciphertext));
  EXPECT_EQ(kExpectedCiphertext, base::as_byte_span(ciphertext));

  std::string plaintext;
  ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &plaintext));
  EXPECT_EQ(kInput, plaintext);
}

}  // namespace