From 25d29fd22b9b06b0cd8a69beb0daf0f43cd6e550 Mon Sep 17 00:00:00 2001 From: Rahul Frias Date: Thu, 13 Dec 2018 12:09:04 -0800 Subject: [PATCH] Use the std::chrono to get the time. [ Merge of http://go/wvgerrit/67985 ] Now that we can use C++11, we should use the cross-platform types for clocks instead of the platform-specific versions. Test: WV unit/integration tests. Change-Id: I50318e3d1caf9e814f33f497f83c19c9f3c154a1 --- .../cdm/metrics/include/timer_metric.h | 6 +- .../cdm/metrics/src/timer_metric.cpp | 22 +------- .../ref/src/oemcrypto_engine_ref.cpp | 11 +--- .../oemcrypto/ref/src/oemcrypto_ref.cpp | 16 ++---- .../oemcrypto/test/oemcrypto_test.cpp | 56 +++++++------------ 5 files changed, 35 insertions(+), 76 deletions(-) diff --git a/libwvdrmengine/cdm/metrics/include/timer_metric.h b/libwvdrmengine/cdm/metrics/include/timer_metric.h index 84a8d20a..ace35370 100644 --- a/libwvdrmengine/cdm/metrics/include/timer_metric.h +++ b/libwvdrmengine/cdm/metrics/include/timer_metric.h @@ -1,7 +1,7 @@ #ifndef WVCDM_METRICS_TIMER_METRIC_H_ #define WVCDM_METRICS_TIMER_METRIC_H_ -#include +#include namespace wvcdm { namespace metrics { @@ -23,8 +23,8 @@ class TimerMetric { double AsUs() const; private: - double sec_; - double usec_; + std::chrono::steady_clock clock_; + std::chrono::time_point start_; bool is_started_; }; diff --git a/libwvdrmengine/cdm/metrics/src/timer_metric.cpp b/libwvdrmengine/cdm/metrics/src/timer_metric.cpp index 03575567..c87c20b7 100644 --- a/libwvdrmengine/cdm/metrics/src/timer_metric.cpp +++ b/libwvdrmengine/cdm/metrics/src/timer_metric.cpp @@ -1,39 +1,23 @@ #include "timer_metric.h" -#include -#include - namespace wvcdm { namespace metrics { void TimerMetric::Start() { - struct timeval tv; - gettimeofday(&tv, NULL); - sec_ = tv.tv_sec; - usec_ = tv.tv_usec; + start_ = clock_.now(); is_started_ = true; } void TimerMetric::Clear() { is_started_ = false; - sec_ = 0; - usec_ = 0; } double TimerMetric::AsMs() const { - struct timeval tv; - gettimeofday(&tv, NULL); - return usec_ > tv.tv_usec ? - (tv.tv_sec - sec_ - 1) * 1000.0 + (tv.tv_usec - usec_ + 1000000.0) / 1000.0 : - (tv.tv_sec - sec_) * 1000.0 + (tv.tv_usec - usec_) / 1000.0; + return (clock_.now() - start_) / std::chrono::milliseconds(1); } double TimerMetric::AsUs() const { - struct timeval tv; - gettimeofday(&tv, NULL); - return usec_ > tv.tv_usec ? - (tv.tv_sec - sec_ - 1) * 1000000.0 + (tv.tv_usec - usec_ + 1000000.0) : - (tv.tv_sec - sec_) * 1000000.0 + (tv.tv_usec - usec_); + return (clock_.now() - start_) / std::chrono::microseconds(1); } } // namespace metrics diff --git a/libwvdrmengine/oemcrypto/ref/src/oemcrypto_engine_ref.cpp b/libwvdrmengine/oemcrypto/ref/src/oemcrypto_engine_ref.cpp index 3a83b5ed..843d9665 100644 --- a/libwvdrmengine/oemcrypto/ref/src/oemcrypto_engine_ref.cpp +++ b/libwvdrmengine/oemcrypto/ref/src/oemcrypto_engine_ref.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -90,14 +91,8 @@ SessionContext* CryptoEngine::FindSession(SessionId sid) { time_t CryptoEngine::OnlineTime() { // Use the monotonic clock for times that don't have to be stable across // device boots. - timespec current_time; - int gettime_result = clock_gettime(CLOCK_MONOTONIC, ¤t_time); - if (gettime_result == 0) { - return current_time.tv_sec; - } else { - // Can't use monotonic clock, use roll back time. - return RollbackCorrectedOfflineTime(); - } + std::chrono::steady_clock clock; + return clock.now().time_since_epoch() / std::chrono::seconds(1); } time_t CryptoEngine::RollbackCorrectedOfflineTime() { diff --git a/libwvdrmengine/oemcrypto/ref/src/oemcrypto_ref.cpp b/libwvdrmengine/oemcrypto/ref/src/oemcrypto_ref.cpp index f901776a..4140125d 100644 --- a/libwvdrmengine/oemcrypto/ref/src/oemcrypto_ref.cpp +++ b/libwvdrmengine/oemcrypto/ref/src/oemcrypto_ref.cpp @@ -15,8 +15,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -162,12 +161,6 @@ OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateDerivedKeys( return OEMCrypto_SUCCESS; } -static const uint64_t one_second = 1000000ull; -static uint64_t TimeStamp(void) { - struct timeval tv; - gettimeofday(&tv,NULL); - return tv.tv_sec * one_second + tv.tv_usec; -} OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session, uint32_t* nonce) { @@ -182,13 +175,14 @@ OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session, } // Prevent nonce flood. - uint64_t now = TimeStamp(); - static uint64_t last_nonce_time = now; + static std::chrono::steady_clock clock; + const auto now = clock.now().time_since_epoch(); + static auto last_nonce_time = now; // For testing, we set nonce_flood_count to 1. Since count is initialized to // 1, the very first nonce after initialization is counted as a flood. static int nonce_count = 1; - if (now - last_nonce_time < one_second) { + if (now - last_nonce_time < std::chrono::seconds(1)) { nonce_count++; if (nonce_count > crypto_engine->nonce_flood_count()) { LOGE("[OEMCrypto_GenerateNonce(): Nonce Flood detected]"); diff --git a/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp b/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp index 09fc1810..007a469f 100644 --- a/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp +++ b/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp @@ -16,11 +16,11 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -2818,32 +2818,25 @@ class OEMCryptoUsesCertificate : public OEMCryptoLoadsCertificate { // This test is not run by default, because it takes a long time and // is used to measure RSA performance, not test functionality. TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) { + const std::chrono::milliseconds kTestDuration(5000); OEMCryptoResult sts; + std::chrono::steady_clock clock; sleep(2); // Make sure are not nonce limited. - const uint32_t TestDuration = 5000; // milliseconds. - struct timeval start_time, end_time; - gettimeofday(&start_time, NULL); - gettimeofday(&end_time, NULL); - double mtime = 0; - long count = 0; - for (int i = 0; i < 15; i++) { // Only 20 nonce available. + auto start_time = clock.now(); + int count = 15; + for (int i = 0; i < count; i++) { // Only 20 nonce available. CreateWrappedRSAKey(kSign_RSASSA_PSS, true); - count++; - gettimeofday(&end_time, NULL); - long seconds = end_time.tv_sec - start_time.tv_sec; - long useconds = end_time.tv_usec - start_time.tv_usec; - mtime = seconds * 1e3 + useconds * 1e-3; } - double provision_time = mtime / count; + auto delta_time = clock.now() - start_time; + const double provision_time = + delta_time / std::chrono::milliseconds(1) / count; Session session; CreateWrappedRSAKey(kSign_RSASSA_PSS, true); - gettimeofday(&start_time, NULL); - gettimeofday(&end_time, NULL); - mtime = 0; + start_time = clock.now(); count = 0; - do { + while (clock.now() - start_time < kTestDuration) { Session s; ASSERT_NO_FATAL_FAILURE(s.open()); sts = OEMCrypto_LoadDeviceRSAKey(s.session_id(), wrapped_rsa_key_.data(), @@ -2865,12 +2858,10 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) { delete[] signature; ASSERT_EQ(OEMCrypto_SUCCESS, sts); count++; - gettimeofday(&end_time, NULL); - long seconds = end_time.tv_sec - start_time.tv_sec; - long useconds = end_time.tv_usec - start_time.tv_usec; - mtime = seconds * 1e3 + useconds * 1e-3; - } while (mtime < TestDuration); - double license_request_time = mtime / count; + } + delta_time = clock.now() - start_time; + const double license_request_time = + delta_time / std::chrono::milliseconds(1) / count; Session s; ASSERT_NO_FATAL_FAILURE(s.open()); @@ -2885,10 +2876,6 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) { vector mac_context; vector enc_context; s.FillDefaultContext(&mac_context, &enc_context); - gettimeofday(&start_time, NULL); - gettimeofday(&end_time, NULL); - mtime = 0; - count = 0; enc_session_key = wvcdm::a2b_hex( "7789c619aa3b9fa3c0a53f57a4abc6" @@ -2908,7 +2895,8 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) { "ad2b1254f80c0c5dd3cf111b56572217" "b9f58fc1dacbf74b59d354a1e62cfa0e" "bf"); - do { + start_time = clock.now(); + while (clock.now() - start_time < kTestDuration) { ASSERT_EQ(OEMCrypto_SUCCESS, OEMCrypto_DeriveKeysFromSessionKey( s.session_id(), @@ -2916,12 +2904,10 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) { mac_context.data(), mac_context.size(), enc_context.data(), enc_context.size())); count++; - gettimeofday(&end_time, NULL); - long seconds = end_time.tv_sec - start_time.tv_sec; - long useconds = end_time.tv_usec - start_time.tv_usec; - mtime = seconds * 1e3 + useconds * 1e-3; - } while (mtime < TestDuration); - double derive_keys_time = mtime / count; + } + delta_time = clock.now() - start_time; + const double derive_keys_time = + delta_time / std::chrono::milliseconds(1) / count; const char* level = OEMCrypto_SecurityLevel(); printf("PERF:head, security, provision (ms), lic req(ms), derive keys(ms)\n");