OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/encryptor/encryptor.h" | 5 #include "components/encryptor/os_crypt.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "crypto/encryptor.h" | 10 #include "crypto/encryptor.h" |
11 #include "crypto/symmetric_key.h" | 11 #include "crypto/symmetric_key.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Salt for Symmetric key derivation. | 15 // Salt for Symmetric key derivation. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 salt, | 47 salt, |
48 kEncryptionIterations, | 48 kEncryptionIterations, |
49 kDerivedKeySizeInBits)); | 49 kDerivedKeySizeInBits)); |
50 DCHECK(encryption_key.get()); | 50 DCHECK(encryption_key.get()); |
51 | 51 |
52 return encryption_key.release(); | 52 return encryption_key.release(); |
53 } | 53 } |
54 | 54 |
55 } // namespace | 55 } // namespace |
56 | 56 |
57 bool Encryptor::EncryptString16(const base::string16& plaintext, | 57 bool OSCrypt::EncryptString16(const base::string16& plaintext, |
58 std::string* ciphertext) { | 58 std::string* ciphertext) { |
59 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); | 59 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); |
60 } | 60 } |
61 | 61 |
62 bool Encryptor::DecryptString16(const std::string& ciphertext, | 62 bool OSCrypt::DecryptString16(const std::string& ciphertext, |
63 base::string16* plaintext) { | 63 base::string16* plaintext) { |
64 std::string utf8; | 64 std::string utf8; |
65 if (!DecryptString(ciphertext, &utf8)) | 65 if (!DecryptString(ciphertext, &utf8)) |
66 return false; | 66 return false; |
67 | 67 |
68 *plaintext = base::UTF8ToUTF16(utf8); | 68 *plaintext = base::UTF8ToUTF16(utf8); |
69 return true; | 69 return true; |
70 } | 70 } |
71 | 71 |
72 bool Encryptor::EncryptString(const std::string& plaintext, | 72 bool OSCrypt::EncryptString(const std::string& plaintext, |
73 std::string* ciphertext) { | 73 std::string* ciphertext) { |
74 // This currently "obfuscates" by encrypting with hard-coded password. | 74 // This currently "obfuscates" by encrypting with hard-coded password. |
75 // We need to improve this password situation by moving a secure password | 75 // We need to improve this password situation by moving a secure password |
76 // into a system-level key store. | 76 // into a system-level key store. |
77 // http://crbug.com/25404 and http://crbug.com/49115 | 77 // http://crbug.com/25404 and http://crbug.com/49115 |
78 | 78 |
79 if (plaintext.empty()) { | 79 if (plaintext.empty()) { |
80 *ciphertext = std::string(); | 80 *ciphertext = std::string(); |
81 return true; | 81 return true; |
82 } | 82 } |
83 | 83 |
84 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); | 84 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
85 if (!encryption_key.get()) | 85 if (!encryption_key.get()) |
86 return false; | 86 return false; |
87 | 87 |
88 std::string iv(kIVBlockSizeAES128, ' '); | 88 std::string iv(kIVBlockSizeAES128, ' '); |
89 crypto::Encryptor encryptor; | 89 crypto::Encryptor encryptor; |
90 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 90 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
91 return false; | 91 return false; |
92 | 92 |
93 if (!encryptor.Encrypt(plaintext, ciphertext)) | 93 if (!encryptor.Encrypt(plaintext, ciphertext)) |
94 return false; | 94 return false; |
95 | 95 |
96 // Prefix the cypher text with version information. | 96 // Prefix the cypher text with version information. |
97 ciphertext->insert(0, kObfuscationPrefix); | 97 ciphertext->insert(0, kObfuscationPrefix); |
98 return true; | 98 return true; |
99 } | 99 } |
100 | 100 |
101 bool Encryptor::DecryptString(const std::string& ciphertext, | 101 bool OSCrypt::DecryptString(const std::string& ciphertext, |
102 std::string* plaintext) { | 102 std::string* plaintext) { |
103 // This currently "obfuscates" by encrypting with hard-coded password. | 103 // This currently "obfuscates" by encrypting with hard-coded password. |
104 // We need to improve this password situation by moving a secure password | 104 // We need to improve this password situation by moving a secure password |
105 // into a system-level key store. | 105 // into a system-level key store. |
106 // http://crbug.com/25404 and http://crbug.com/49115 | 106 // http://crbug.com/25404 and http://crbug.com/49115 |
107 | 107 |
108 if (ciphertext.empty()) { | 108 if (ciphertext.empty()) { |
109 *plaintext = std::string(); | 109 *plaintext = std::string(); |
110 return true; | 110 return true; |
111 } | 111 } |
112 | 112 |
(...skipping 17 matching lines...) Expand all Loading... |
130 std::string iv(kIVBlockSizeAES128, ' '); | 130 std::string iv(kIVBlockSizeAES128, ' '); |
131 crypto::Encryptor encryptor; | 131 crypto::Encryptor encryptor; |
132 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 132 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
133 return false; | 133 return false; |
134 | 134 |
135 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) | 135 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) |
136 return false; | 136 return false; |
137 | 137 |
138 return true; | 138 return true; |
139 } | 139 } |
OLD | NEW |