blob: 3cdb4fd3a429ca6db9aec451411a82da7d774f58 [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cryptohome/tpm.h"
#if USE_TPM2
#include "cryptohome/tpm2_impl.h"
#else
#include "cryptohome/tpm_impl.h"
#endif
namespace cryptohome {
Tpm* Tpm::singleton_ = NULL;
base::Lock Tpm::singleton_lock_;
ScopedKeyHandle::ScopedKeyHandle()
: tpm_(nullptr), handle_(kInvalidKeyHandle) {}
ScopedKeyHandle::~ScopedKeyHandle() {
if (tpm_ != nullptr && handle_ != kInvalidKeyHandle) {
tpm_->CloseHandle(handle_);
}
}
TpmKeyHandle ScopedKeyHandle::value() {
return handle_;
}
TpmKeyHandle ScopedKeyHandle::release() {
TpmKeyHandle return_handle = handle_;
tpm_ = nullptr;
handle_ = kInvalidKeyHandle;
return return_handle;
}
void ScopedKeyHandle::reset(Tpm* tpm, TpmKeyHandle handle) {
tpm_ = tpm;
handle_ = handle;
}
Tpm* Tpm::GetSingleton() {
// TODO(fes): Replace with a better atomic operation
singleton_lock_.Acquire();
if (!singleton_)
#if USE_TPM2
singleton_ = new Tpm2Impl();
#else
singleton_ = new TpmImpl();
#endif
singleton_lock_.Release();
return singleton_;
}
} // namespace cryptohome