Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Unified Diff: tpm.cc

Issue 3116014: Add method to get random bytes from the TPM. (Closed) Base URL: http://src.chromium.org/git/tpm_init.git
Patch Set: Change int to size_t. Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tpm.h ('k') | tpm_init.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tpm.cc
diff --git a/tpm.cc b/tpm.cc
index 4de12a26f52d89677a0d09597922bf81c1a38009..66cb57f980b74c81f76860757b509391d1a757b8 100644
--- a/tpm.cc
+++ b/tpm.cc
@@ -585,4 +585,34 @@ bool Tpm::InitializeTpm() {
return took_ownership;
}
+bool Tpm::GetRandomData(size_t length, chromeos::Blob* data) {
+ TSS_HCONTEXT context_handle;
+ if (!OpenAndConnectTpm(&context_handle)) {
+ LOG(ERROR) << "Could not open the TPM";
+ return false;
+ }
+
+ TSS_HTPM tpm_handle;
+ if (!GetTpm(context_handle, &tpm_handle)) {
+ LOG(ERROR) << "Could not get a handle to the TPM.";
+ Tspi_Context_Close(context_handle);
+ return false;
+ }
+
+ TSS_RESULT result;
+ SecureBlob random(length);
+ BYTE* tpm_data = NULL;
+ if ((result = Tspi_TPM_GetRandom(tpm_handle, random.size(), &tpm_data))) {
+ LOG(ERROR) << "Could not get random data from the TPM: " << result;
+ Tspi_Context_Close(context_handle);
+ return false;
+ }
+ memcpy(random.data(), tpm_data, random.size());
+ Tspi_Context_FreeMemory(context_handle, tpm_data);
+ chromeos::SecureMemset(tpm_data, 0, random.size());
+ Tspi_Context_Close(context_handle);
+ data->swap(random);
+ return true;
+}
+
} // namespace tpm_init
« no previous file with comments | « tpm.h ('k') | tpm_init.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698