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
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <chrono>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
@@ -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() {
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -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]");
|
||||
|
||||
Reference in New Issue
Block a user