| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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. | |
| 4 | |
| 5 #include "chrome/browser/managed_mode/managed_user_passphrase.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/string_util.h" | |
| 10 #include "crypto/encryptor.h" | |
| 11 #include "crypto/random.h" | |
| 12 #include "crypto/symmetric_key.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // These constants are used as parameters when calling |DeriveKeyFromPassword|. | |
| 17 const int kNumberOfIterations = 1; | |
| 18 const int kDerivedKeySize = 128; | |
| 19 const int kSaltSize = 32; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 ManagedUserPassphrase::ManagedUserPassphrase(const std::string& salt) | |
| 24 : salt_(salt) { | |
| 25 if (salt_.empty()) | |
| 26 GenerateRandomSalt(); | |
| 27 } | |
| 28 | |
| 29 ManagedUserPassphrase::~ManagedUserPassphrase() { | |
| 30 } | |
| 31 | |
| 32 std::string ManagedUserPassphrase::GetSalt() { | |
| 33 return salt_; | |
| 34 } | |
| 35 | |
| 36 void ManagedUserPassphrase::GenerateRandomSalt() { | |
| 37 std::string bytes; | |
| 38 crypto::RandBytes(WriteInto(&bytes, kSaltSize+1), kSaltSize); | |
| 39 bool success = base::Base64Encode(bytes, &salt_); | |
| 40 DCHECK(success); | |
| 41 } | |
| 42 | |
| 43 bool ManagedUserPassphrase::GenerateHashFromPassphrase( | |
| 44 const std::string& passphrase, | |
| 45 std::string* encoded_passphrase_hash) const { | |
| 46 std::string passphrase_hash; | |
| 47 if (!GetPassphraseHash(passphrase, &passphrase_hash)) | |
| 48 return false; | |
| 49 return base::Base64Encode(passphrase_hash, encoded_passphrase_hash); | |
| 50 } | |
| 51 | |
| 52 bool ManagedUserPassphrase::GetPassphraseHash( | |
| 53 const std::string& passphrase, | |
| 54 std::string* passphrase_hash) const { | |
| 55 DCHECK(passphrase_hash); | |
| 56 // Create a hash from the user-provided passphrase and the randomly generated | |
| 57 // (or provided) salt. | |
| 58 scoped_ptr<crypto::SymmetricKey> encryption_key( | |
| 59 crypto::SymmetricKey::DeriveKeyFromPassword( | |
| 60 crypto::SymmetricKey::AES, | |
| 61 passphrase, | |
| 62 salt_, | |
| 63 kNumberOfIterations, | |
| 64 kDerivedKeySize)); | |
| 65 return encryption_key->GetRawKey(passphrase_hash); | |
| 66 } | |
| OLD | NEW |