OLD | NEW |
(Empty) | |
| 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 |
| 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 // These constants are used as parameters when calling |DeriveKeyFromPassword|. |
| 15 const int kNumberOfIterations = 1; |
| 16 const int kDerivedKeySize = 128; |
| 17 const int kSaltSize = 33; |
| 18 |
| 19 ManagedUserPassphrase::ManagedUserPassphrase(const std::string& salt) |
| 20 : salt_(salt) { |
| 21 if (salt_.empty()) |
| 22 GenerateRandomSalt(); |
| 23 } |
| 24 |
| 25 ManagedUserPassphrase::~ManagedUserPassphrase() { |
| 26 } |
| 27 |
| 28 std::string ManagedUserPassphrase::GetSalt() { |
| 29 return salt_; |
| 30 } |
| 31 |
| 32 void ManagedUserPassphrase::GenerateRandomSalt() { |
| 33 std::string bytes; |
| 34 crypto::RandBytes(WriteInto(&bytes, kSaltSize), kSaltSize); |
| 35 bool success = base::Base64Encode(bytes, &salt_); |
| 36 DCHECK(success); |
| 37 } |
| 38 |
| 39 void ManagedUserPassphrase::GenerateHashFromPassphrase( |
| 40 const std::string& passphrase, |
| 41 std::string* encoded_passphrase_hash) const { |
| 42 std::string passphrase_hash; |
| 43 GetPassphraseHash(passphrase, &passphrase_hash); |
| 44 bool success = base::Base64Encode(passphrase_hash, encoded_passphrase_hash); |
| 45 DCHECK(success); |
| 46 } |
| 47 |
| 48 void ManagedUserPassphrase::GetPassphraseHash( |
| 49 const std::string& passphrase, |
| 50 std::string* passphrase_hash) const { |
| 51 DCHECK(passphrase_hash); |
| 52 // Create a hash from the user-provided passphrase and our hard-coded salt. |
| 53 scoped_ptr<crypto::SymmetricKey> encryption_key( |
| 54 crypto::SymmetricKey::DeriveKeyFromPassword( |
| 55 crypto::SymmetricKey::AES, |
| 56 passphrase, |
| 57 salt_, |
| 58 kNumberOfIterations, |
| 59 kDerivedKeySize)); |
| 60 bool success = encryption_key->GetRawKey(passphrase_hash); |
| 61 DCHECK(success); |
| 62 } |
OLD | NEW |