| 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 "sync/util/nigori.h" | 5 #include "sync/util/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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 bool Nigori::InitByDerivation(const std::string& hostname, | 70 bool Nigori::InitByDerivation(const std::string& hostname, |
| 71 const std::string& username, | 71 const std::string& username, |
| 72 const std::string& password) { | 72 const std::string& password) { |
| 73 NigoriStream salt_password; | 73 NigoriStream salt_password; |
| 74 salt_password << username << hostname; | 74 salt_password << username << hostname; |
| 75 | 75 |
| 76 // Suser = PBKDF2(Username || Servername, "saltsalt", Nsalt, 8) | 76 // Suser = PBKDF2(Username || Servername, "saltsalt", Nsalt, 8) |
| 77 std::unique_ptr<SymmetricKey> user_salt(SymmetricKey::DeriveKeyFromPassword( | 77 std::unique_ptr<SymmetricKey> user_salt(SymmetricKey::DeriveKeyFromPassword( |
| 78 SymmetricKey::HMAC_SHA1, salt_password.str(), kSaltSalt, kSaltIterations, | 78 SymmetricKey::HMAC_SHA1, salt_password.str(), kSaltSalt, kSaltIterations, |
| 79 kSaltKeySizeInBits)); | 79 kSaltKeySizeInBits)); |
| 80 DCHECK(user_salt.get()); | 80 DCHECK(user_salt); |
| 81 | 81 |
| 82 std::string raw_user_salt; | 82 std::string raw_user_salt; |
| 83 if (!user_salt->GetRawKey(&raw_user_salt)) | 83 if (!user_salt->GetRawKey(&raw_user_salt)) |
| 84 return false; | 84 return false; |
| 85 | 85 |
| 86 // Kuser = PBKDF2(P, Suser, Nuser, 16) | 86 // Kuser = PBKDF2(P, Suser, Nuser, 16) |
| 87 user_key_.reset(SymmetricKey::DeriveKeyFromPassword(SymmetricKey::AES, | 87 user_key_ = SymmetricKey::DeriveKeyFromPassword( |
| 88 password, raw_user_salt, kUserIterations, kDerivedKeySizeInBits)); | 88 SymmetricKey::AES, password, raw_user_salt, kUserIterations, |
| 89 DCHECK(user_key_.get()); | 89 kDerivedKeySizeInBits); |
| 90 DCHECK(user_key_); |
| 90 | 91 |
| 91 // Kenc = PBKDF2(P, Suser, Nenc, 16) | 92 // Kenc = PBKDF2(P, Suser, Nenc, 16) |
| 92 encryption_key_.reset(SymmetricKey::DeriveKeyFromPassword(SymmetricKey::AES, | 93 encryption_key_ = SymmetricKey::DeriveKeyFromPassword( |
| 93 password, raw_user_salt, kEncryptionIterations, kDerivedKeySizeInBits)); | 94 SymmetricKey::AES, password, raw_user_salt, kEncryptionIterations, |
| 94 DCHECK(encryption_key_.get()); | 95 kDerivedKeySizeInBits); |
| 96 DCHECK(encryption_key_); |
| 95 | 97 |
| 96 // Kmac = PBKDF2(P, Suser, Nmac, 16) | 98 // Kmac = PBKDF2(P, Suser, Nmac, 16) |
| 97 mac_key_.reset(SymmetricKey::DeriveKeyFromPassword( | 99 mac_key_ = SymmetricKey::DeriveKeyFromPassword( |
| 98 SymmetricKey::HMAC_SHA1, password, raw_user_salt, kSigningIterations, | 100 SymmetricKey::HMAC_SHA1, password, raw_user_salt, kSigningIterations, |
| 99 kDerivedKeySizeInBits)); | 101 kDerivedKeySizeInBits); |
| 100 DCHECK(mac_key_.get()); | 102 DCHECK(mac_key_); |
| 101 | 103 |
| 102 return user_key_.get() && encryption_key_.get() && mac_key_.get(); | 104 return user_key_ && encryption_key_ && mac_key_; |
| 103 } | 105 } |
| 104 | 106 |
| 105 bool Nigori::InitByImport(const std::string& user_key, | 107 bool Nigori::InitByImport(const std::string& user_key, |
| 106 const std::string& encryption_key, | 108 const std::string& encryption_key, |
| 107 const std::string& mac_key) { | 109 const std::string& mac_key) { |
| 108 user_key_.reset(SymmetricKey::Import(SymmetricKey::AES, user_key)); | 110 user_key_ = SymmetricKey::Import(SymmetricKey::AES, user_key); |
| 109 DCHECK(user_key_.get()); | 111 DCHECK(user_key_); |
| 110 | 112 |
| 111 encryption_key_.reset(SymmetricKey::Import(SymmetricKey::AES, | 113 encryption_key_ = SymmetricKey::Import(SymmetricKey::AES, encryption_key); |
| 112 encryption_key)); | 114 DCHECK(encryption_key_); |
| 113 DCHECK(encryption_key_.get()); | |
| 114 | 115 |
| 115 mac_key_.reset(SymmetricKey::Import(SymmetricKey::HMAC_SHA1, mac_key)); | 116 mac_key_ = SymmetricKey::Import(SymmetricKey::HMAC_SHA1, mac_key); |
| 116 DCHECK(mac_key_.get()); | 117 DCHECK(mac_key_); |
| 117 | 118 |
| 118 return user_key_.get() && encryption_key_.get() && mac_key_.get(); | 119 return user_key_ && encryption_key_ && mac_key_; |
| 119 } | 120 } |
| 120 | 121 |
| 121 // Permute[Kenc,Kmac](type || name) | 122 // Permute[Kenc,Kmac](type || name) |
| 122 bool Nigori::Permute(Type type, const std::string& name, | 123 bool Nigori::Permute(Type type, const std::string& name, |
| 123 std::string* permuted) const { | 124 std::string* permuted) const { |
| 124 DCHECK_LT(0U, name.size()); | 125 DCHECK_LT(0U, name.size()); |
| 125 | 126 |
| 126 NigoriStream plaintext; | 127 NigoriStream plaintext; |
| 127 plaintext << type << name; | 128 plaintext << type << name; |
| 128 | 129 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 DCHECK(user_key); | 243 DCHECK(user_key); |
| 243 DCHECK(encryption_key); | 244 DCHECK(encryption_key); |
| 244 DCHECK(mac_key); | 245 DCHECK(mac_key); |
| 245 | 246 |
| 246 return user_key_->GetRawKey(user_key) && | 247 return user_key_->GetRawKey(user_key) && |
| 247 encryption_key_->GetRawKey(encryption_key) && | 248 encryption_key_->GetRawKey(encryption_key) && |
| 248 mac_key_->GetRawKey(mac_key); | 249 mac_key_->GetRawKey(mac_key); |
| 249 } | 250 } |
| 250 | 251 |
| 251 } // namespace syncer | 252 } // namespace syncer |
| OLD | NEW |