OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/sync/base/nigori.h" | 5 #include "components/sync/base/nigori.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <sstream> | 9 #include <sstream> |
10 #include <vector> | 10 #include <vector> |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 // Suser = PBKDF2(Username || Servername, "saltsalt", Nsalt, 8) | 72 // Suser = PBKDF2(Username || Servername, "saltsalt", Nsalt, 8) |
73 std::unique_ptr<SymmetricKey> user_salt(SymmetricKey::DeriveKeyFromPassword( | 73 std::unique_ptr<SymmetricKey> user_salt(SymmetricKey::DeriveKeyFromPassword( |
74 SymmetricKey::HMAC_SHA1, salt_password.str(), kSaltSalt, kSaltIterations, | 74 SymmetricKey::HMAC_SHA1, salt_password.str(), kSaltSalt, kSaltIterations, |
75 kSaltKeySizeInBits)); | 75 kSaltKeySizeInBits)); |
76 DCHECK(user_salt); | 76 DCHECK(user_salt); |
77 | 77 |
78 std::string raw_user_salt; | 78 std::string raw_user_salt; |
79 if (!user_salt->GetRawKey(&raw_user_salt)) | 79 if (!user_salt->GetRawKey(&raw_user_salt)) |
80 return false; | 80 return false; |
81 | 81 |
82 // Kuser = PBKDF2(P, Suser, Nuser, 16) | |
83 user_key_ = SymmetricKey::DeriveKeyFromPassword( | |
84 SymmetricKey::AES, password, raw_user_salt, kUserIterations, | |
85 kDerivedKeySizeInBits); | |
86 DCHECK(user_key_); | |
87 | |
88 // Kenc = PBKDF2(P, Suser, Nenc, 16) | 82 // Kenc = PBKDF2(P, Suser, Nenc, 16) |
89 encryption_key_ = SymmetricKey::DeriveKeyFromPassword( | 83 encryption_key_ = SymmetricKey::DeriveKeyFromPassword( |
90 SymmetricKey::AES, password, raw_user_salt, kEncryptionIterations, | 84 SymmetricKey::AES, password, raw_user_salt, kEncryptionIterations, |
91 kDerivedKeySizeInBits); | 85 kDerivedKeySizeInBits); |
92 DCHECK(encryption_key_); | 86 DCHECK(encryption_key_); |
93 | 87 |
94 // Kmac = PBKDF2(P, Suser, Nmac, 16) | 88 // Kmac = PBKDF2(P, Suser, Nmac, 16) |
95 mac_key_ = SymmetricKey::DeriveKeyFromPassword( | 89 mac_key_ = SymmetricKey::DeriveKeyFromPassword( |
96 SymmetricKey::HMAC_SHA1, password, raw_user_salt, kSigningIterations, | 90 SymmetricKey::HMAC_SHA1, password, raw_user_salt, kSigningIterations, |
97 kDerivedKeySizeInBits); | 91 kDerivedKeySizeInBits); |
98 DCHECK(mac_key_); | 92 DCHECK(mac_key_); |
99 | 93 |
100 return user_key_ && encryption_key_ && mac_key_; | 94 return encryption_key_ && mac_key_; |
101 } | 95 } |
102 | 96 |
103 bool Nigori::InitByImport(const std::string& user_key, | 97 bool Nigori::InitByImport(const std::string& encryption_key, |
104 const std::string& encryption_key, | |
105 const std::string& mac_key) { | 98 const std::string& mac_key) { |
106 user_key_ = SymmetricKey::Import(SymmetricKey::AES, user_key); | |
107 | |
108 encryption_key_ = SymmetricKey::Import(SymmetricKey::AES, encryption_key); | 99 encryption_key_ = SymmetricKey::Import(SymmetricKey::AES, encryption_key); |
109 DCHECK(encryption_key_); | 100 DCHECK(encryption_key_); |
110 | 101 |
111 mac_key_ = SymmetricKey::Import(SymmetricKey::HMAC_SHA1, mac_key); | 102 mac_key_ = SymmetricKey::Import(SymmetricKey::HMAC_SHA1, mac_key); |
112 DCHECK(mac_key_); | 103 DCHECK(mac_key_); |
113 | 104 |
114 return encryption_key_ && mac_key_; | 105 return encryption_key_ && mac_key_; |
115 } | 106 } |
116 | 107 |
117 // Permute[Kenc,Kmac](type || name) | 108 // Permute[Kenc,Kmac](type || name) |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 Encryptor encryptor; | 216 Encryptor encryptor; |
226 if (!encryptor.Init(encryption_key_.get(), Encryptor::CBC, iv)) | 217 if (!encryptor.Init(encryption_key_.get(), Encryptor::CBC, iv)) |
227 return false; | 218 return false; |
228 | 219 |
229 if (!encryptor.Decrypt(ciphertext, value)) | 220 if (!encryptor.Decrypt(ciphertext, value)) |
230 return false; | 221 return false; |
231 | 222 |
232 return true; | 223 return true; |
233 } | 224 } |
234 | 225 |
235 bool Nigori::ExportKeys(std::string* user_key, | 226 bool Nigori::ExportKeys(std::string* encryption_key, |
236 std::string* encryption_key, | |
237 std::string* mac_key) const { | 227 std::string* mac_key) const { |
238 DCHECK(encryption_key); | 228 DCHECK(encryption_key); |
239 DCHECK(mac_key); | 229 DCHECK(mac_key); |
240 | 230 |
241 user_key_->GetRawKey(user_key); | |
242 | |
243 return encryption_key_->GetRawKey(encryption_key) && | 231 return encryption_key_->GetRawKey(encryption_key) && |
244 mac_key_->GetRawKey(mac_key); | 232 mac_key_->GetRawKey(mac_key); |
245 } | 233 } |
246 | 234 |
247 } // namespace syncer | 235 } // namespace syncer |
OLD | NEW |