Merge from Widevine repo of http://go/wvgerrit/47860 This CL updates the copyright notice to indicate that files shared with partners are shared under the Widevine Master License Agreement. bug: 77926774 test: comment change only Change-Id: I0423668111578b80fb39a932d763df2827e2dfc3
70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine Master
|
|
// License Agreement.
|
|
//
|
|
// Mock implementation of OEMCrypto APIs
|
|
//
|
|
#include "oemcrypto_nonce_table.h"
|
|
|
|
namespace wvoec_mock {
|
|
|
|
void NonceTable::AddNonce(uint32_t nonce) {
|
|
int new_slot = -1;
|
|
int oldest_slot = -1;
|
|
|
|
// Flush any nonces that have been checked but not flushed.
|
|
// After flush, nonces will be either valid or invalid.
|
|
Flush();
|
|
|
|
for (int i = 0; i < kTableSize; ++i) {
|
|
// Increase age of all valid nonces.
|
|
if (kNTStateValid == state_[i]) {
|
|
++age_[i];
|
|
if (-1 == oldest_slot) {
|
|
oldest_slot = i;
|
|
} else {
|
|
if (age_[i] > age_[oldest_slot]) {
|
|
oldest_slot = i;
|
|
}
|
|
}
|
|
} else {
|
|
if (-1 == new_slot) {
|
|
age_[i] = 0;
|
|
nonces_[i] = nonce;
|
|
state_[i] = kNTStateValid;
|
|
new_slot = i;
|
|
}
|
|
}
|
|
}
|
|
if (-1 == new_slot) {
|
|
// reuse oldest
|
|
// assert (oldest_slot != -1)
|
|
int i = oldest_slot;
|
|
age_[i] = 0;
|
|
nonces_[i] = nonce;
|
|
state_[i] = kNTStateValid;
|
|
}
|
|
}
|
|
|
|
bool NonceTable::CheckNonce(uint32_t nonce) {
|
|
for (int i = 0; i < kTableSize; ++i) {
|
|
if (kNTStateInvalid != state_[i]) {
|
|
if (nonce == nonces_[i]) {
|
|
state_[i] = kNTStateFlushPending;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void NonceTable::Flush() {
|
|
for (int i = 0; i < kTableSize; ++i) {
|
|
if (kNTStateFlushPending == state_[i]) {
|
|
state_[i] = kNTStateInvalid;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace wvoec_mock
|