1 // Copyright 2014 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.
5 #include "components/os_crypt/os_crypt.h"
7 #include <CommonCrypto/CommonCryptor.h> // for kCCBlockSizeAES128
9 #include "base/command_line.h"
10 #include "base/debug/leak_annotations.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/synchronization/lock.h"
15 #include "components/os_crypt/keychain_password_mac.h"
16 #include "components/os_crypt/os_crypt_switches.h"
17 #include "crypto/apple_keychain.h"
18 #include "crypto/encryptor.h"
19 #include "crypto/mock_apple_keychain.h"
20 #include "crypto/symmetric_key.h"
22 using crypto::AppleKeychain;
26 // Salt for Symmetric key derivation.
27 const char kSalt[] = "saltysalt";
29 // Key size required for 128 bit AES.
30 const size_t kDerivedKeySizeInBits = 128;
32 // Constant for Symmetic key derivation.
33 const size_t kEncryptionIterations = 1003;
35 // TODO(dhollowa): Refactor to allow dependency injection of Keychain.
36 static bool use_mock_keychain = false;
38 // Prefix for cypher text returned by current encryption version. We prefix
39 // the cypher text with this string so that future data migration can detect
40 // this and migrate to different encryption without data loss.
41 const char kEncryptionVersionPrefix[] = "v10";
43 // Generates a newly allocated SymmetricKey object based on the password found
44 // in the Keychain. The generated key is for AES encryption. Returns NULL key
45 // in the case password access is denied or key generation error occurs.
46 crypto::SymmetricKey* GetEncryptionKey() {
47 static crypto::SymmetricKey* cached_encryption_key = NULL;
48 static bool key_is_cached = false;
49 static base::Lock lock;
50 base::AutoLock auto_lock(lock);
53 return cached_encryption_key;
55 static bool mock_keychain_command_line_flag =
56 base::CommandLine::ForCurrentProcess()->HasSwitch(
57 os_crypt::switches::kUseMockKeychain);
60 if (use_mock_keychain || mock_keychain_command_line_flag) {
61 crypto::MockAppleKeychain keychain;
62 password = keychain.GetEncryptionPassword();
64 AppleKeychain keychain;
65 KeychainPassword encryptor_password(keychain);
66 password = encryptor_password.GetPassword();
69 // Subsequent code must guarantee that the correct key is cached before
74 return cached_encryption_key;
76 std::string salt(kSalt);
78 // Create an encryption key from our password and salt. The key is
79 // intentionally leaked.
80 cached_encryption_key =
81 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES,
84 kEncryptionIterations,
85 kDerivedKeySizeInBits);
86 ANNOTATE_LEAKING_OBJECT_PTR(cached_encryption_key);
87 DCHECK(cached_encryption_key);
88 return cached_encryption_key;
93 bool OSCrypt::EncryptString16(const base::string16& plaintext,
94 std::string* ciphertext) {
95 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext);
98 bool OSCrypt::DecryptString16(const std::string& ciphertext,
99 base::string16* plaintext) {
101 if (!DecryptString(ciphertext, &utf8))
104 *plaintext = base::UTF8ToUTF16(utf8);
108 bool OSCrypt::EncryptString(const std::string& plaintext,
109 std::string* ciphertext) {
110 if (plaintext.empty()) {
111 *ciphertext = std::string();
115 crypto::SymmetricKey* encryption_key = GetEncryptionKey();
119 std::string iv(kCCBlockSizeAES128, ' ');
120 crypto::Encryptor encryptor;
121 if (!encryptor.Init(encryption_key, crypto::Encryptor::CBC, iv))
124 if (!encryptor.Encrypt(plaintext, ciphertext))
127 // Prefix the cypher text with version information.
128 ciphertext->insert(0, kEncryptionVersionPrefix);
132 bool OSCrypt::DecryptString(const std::string& ciphertext,
133 std::string* plaintext) {
134 if (ciphertext.empty()) {
135 *plaintext = std::string();
139 // Check that the incoming cyphertext was indeed encrypted with the expected
140 // version. If the prefix is not found then we'll assume we're dealing with
141 // old data saved as clear text and we'll return it directly.
142 // Credit card numbers are current legacy data, so false match with prefix
144 if (ciphertext.find(kEncryptionVersionPrefix) != 0) {
145 *plaintext = ciphertext;
149 // Strip off the versioning prefix before decrypting.
150 std::string raw_ciphertext =
151 ciphertext.substr(strlen(kEncryptionVersionPrefix));
153 crypto::SymmetricKey* encryption_key = GetEncryptionKey();
157 std::string iv(kCCBlockSizeAES128, ' ');
158 crypto::Encryptor encryptor;
159 if (!encryptor.Init(encryption_key, crypto::Encryptor::CBC, iv))
162 if (!encryptor.Decrypt(raw_ciphertext, plaintext))
168 void OSCrypt::UseMockKeychain(bool use_mock) {
169 use_mock_keychain = use_mock;