Skip to content

Commit

Permalink
Queue and sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeferson Gonzalez committed Apr 4, 2022
1 parent 5f1bc00 commit 4f923ca
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 66 deletions.
70 changes: 49 additions & 21 deletions benchmarks/myapps/attestation/attestation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <chrono>
#include "sim_api.h"
#include <execinfo.h>
#include <unistd.h>

#define DEBUG 1
#define ATTACKER 0
Expand All @@ -32,7 +33,7 @@ void FIRFilter(uint16_t * input, float * output);
void encrypt(float * raw_data, unsigned char * save_buffer, const unsigned char * PRIVATE_KEY);
void saveFile(unsigned char * encrypted_data);


__uint128_t askForHash();
void computeChallenge(uint16_t challenge);
void enableHashComputation(uint16_t flag);
void disableAllFlags();
Expand Down Expand Up @@ -60,11 +61,12 @@ int mainLoop(int iter) {
app_id = (char*)malloc(8*sizeof(int) + 1);
uint64_t long_id = SimGetThreadId();
sprintf(app_id, "%d", static_cast<int>(long_id));

bool end = false;
int i = 0;
SimRoiStart();
if (DEBUG)
cout <<"Entering main loop for " <<iter << " iterations" <<endl;
for (size_t i = 0; i < iter; i++){
while (!end) { //for (size_t i = 0; i < iter; i++){
// Attestation control code
// Polls for attestation request by Verifier (simulator).
// This has the same return value for all applications (true/false) -> simultaneous attestation
Expand All @@ -73,17 +75,10 @@ int mainLoop(int iter) {
if (attestation) {
if (DEBUG)
cout << "Starting atatestation" <<endl;
while (!myTurn)
// Wait until it's my turn on the queue
myTurn = SimCheckAttestationTurn();
if (DEBUG)
cout << "It's my turn" <<endl;
// If it's my turn, get the challenge
uint16_t challenge = SimGetChallengeId();
// Try get the challenge
uint16_t challenge = 3;
// Now get the challenge's hash
// Due to limitiations on the simulator return size, we have to call it twice
hash_seed = SimGetChallengeHash(1); // Most Significant Word
hash_seed = hash_seed << 64 | SimGetChallengeHash(0); //Least Significant Word
hash_seed = askForHash();
debugPrintHash("MAIN", hash_seed);
if (DEBUG)
cout<< "Got challenge "<< challenge << endl;
Expand Down Expand Up @@ -120,14 +115,14 @@ int mainLoop(int iter) {

// Second part of attestation
// Sending the challenge result
if (attestation & myTurn) {
if (attestation) {
// Send the answer back to the Verifier (simulator).
uint64_t hash_msw = (hash_seed >> 64);
uint64_t hash_lsw = ((hash_seed << 64) >> 64);
// Again, because of limitations on the simulator we have to split
// the hash into two arguments for the function
if (SimSendChallengeResult(hash_msw, hash_lsw)) {
myTurn = false;
//myTurn = false;
attestation = false;
disableAllFlags();
}
Expand All @@ -137,13 +132,17 @@ int mainLoop(int iter) {
}
//Then wait for all the applications to finish
//their attestation computation
while(!SimCheckAllFinished());
volatile bool done = false;
int l = 0;

//while (SimCheckAllFinished());
end = true;
if (DEBUG)
cout << "All applications have finished their attestation computation" <<endl;

}
SimRoiEnd();
i++;
}

return 1;
}

Expand All @@ -165,7 +164,7 @@ void readADC(uint16_t * input) {
cout << "readADC under attestation " << endl;
__uint128_t diff_addr = init_pc_addr - reinterpret_cast<__uint128_t>(getPC()); // Get Current PC
// Now let's build a hash relative to the PC difference (should remain constant)
__uint128_t hash_module = diff_addr << 64 | (diff_addr << 120) >> 120;
__uint128_t hash_module = askForHash();//diff_addr << 64 | (diff_addr << 120) >> 120;
// Keep the hash chain
hash_seed = hash_seed ^ hash_module;
debugPrintHash("ADC", hash_seed);
Expand Down Expand Up @@ -198,7 +197,7 @@ void FIRFilter(uint16_t * input, float * output) {
cout << "FIRFilter under attestation " << endl;
__uint128_t diff_addr = init_pc_addr - reinterpret_cast<__uint128_t>(getPC()); // Get Current PC
// Now let's build a hash relative to the PC difference (should remain constant)
__uint128_t hash_module = diff_addr<<116 | diff_addr>>12;
__uint128_t hash_module = askForHash();//diff_addr<<116 | diff_addr>>12;
// Keep the hash chain
hash_seed = hash_seed ^ hash_module;
debugPrintHash("FIR", hash_seed);
Expand Down Expand Up @@ -226,7 +225,7 @@ void encrypt(float * raw_data, unsigned char * save_buffer, const unsigned char*
cout << "encrypt under attestation " << endl;
__uint128_t diff_addr = init_pc_addr - reinterpret_cast<__uint128_t>(getPC()); // Get Current PC
// Now let's build a hash relative to the PC difference (should remain constant)
__uint128_t hash_module = diff_addr<<116 | diff_addr>>12;
__uint128_t hash_module = askForHash();//diff_addr<<116 | diff_addr>>12;
// Keep the hash chain
hash_seed = hash_seed ^ hash_module;
debugPrintHash("ENCRYPT", hash_seed);
Expand All @@ -241,6 +240,14 @@ void saveFile(unsigned char * encrypted_data) {
fp = fopen(file_name, "wa");
fwrite (encrypted_data , sizeof(unsigned char *), 16, fp);
fclose (fp);
if (attestation_flags.at(2)) {
if (DEBUG)
cout << "save under attestation " << endl;
__uint128_t hash_module = askForHash();//diff_addr<<116 | diff_addr>>12;
// Keep the hash chain
hash_seed = hash_seed ^ hash_module;
debugPrintHash("SAVE", hash_seed);
}
}

// Main
Expand Down Expand Up @@ -312,4 +319,25 @@ void print_trace() {
printf ("%s\n", strings[i]);
}
free (strings);
}

__uint128_t askForHash() {
// First request for a turn on the queue
uint16_t ticket = SimGetRequestTurn();
if (DEBUG){
cout << "Got ticket " << std::dec <<ticket <<endl;
}

bool my_turn = false;
__uint128_t tmp;
while (!my_turn)
// Wait until it's my turn on the queue
my_turn = SimCheckAttestationTurn(ticket);
if (DEBUG)
cout << "It's my turn" <<endl;
// Due to limitiations on the simulator return size, we have to call it twice
tmp = SimGetChallengeHash(1); // Most Significant Word
tmp = tmp << 64 | SimGetChallengeHash(0); //Least Significant Word
return tmp;

}
8 changes: 6 additions & 2 deletions common/scheduler/scheduler_open.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ void SchedulerOpen::periodic(SubsecondTime time) {
}


if ((attestationPolicy != NULL) && (time.getNS() % attestationEpoch == 0)) {
if ((attestationPolicy != NULL) && (time.getNS() == 2000000)) { //% attestationEpoch == 0)) {
cout << "\n[Scheduler]: Attestation invoked at " << formatTime(time) << endl;
executeAttestationPolicy();
}
Expand Down Expand Up @@ -1414,7 +1414,11 @@ void SchedulerOpen::periodic(SubsecondTime time) {
cout << endl;
}
}

if (time.getNS() % 100000 == 0) {
cout << "Time "<< std::dec<< time.getUS() << "us" << endl;
if (Sim()->getAttestationManager()->checkUnderAttestationGlobal())
Sim()->getAttestationManager()->updateSequencer();
}

SubsecondTime delta = time - m_last_periodic;

Expand Down
36 changes: 23 additions & 13 deletions common/system/attestation_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
#include <algorithm>
#include <numeric>
#include <list>
#include <unistd.h>



using namespace std;

#define MSW 1
#define LSW 0


AttestationManager::AttestationManager() {
trustedHwPlatform = new TrustedHwPlatform;
//TODO: read from file
// seq_latency = new ComponentLatency(&seq_period, 1); //50000
}
AttestationManager::~AttestationManager() {
delete(trustedHwPlatform);
Expand All @@ -19,7 +23,6 @@ AttestationManager::~AttestationManager() {
void AttestationManager::setAttestation(thread_id_t thread_id){
cout <<"[Attestation Manager]: Setting Attestation for Thread " << thread_id <<endl;
m_curr_attest_threads.push_back(thread_id);
m_curr_attest_turn.push(thread_id);
}

// This method needs to be called twice for a single Hash
Expand All @@ -33,12 +36,6 @@ UInt64 AttestationManager::getChallengeHash(thread_id_t thread_id, UInt8 word){
switch (word){
case LSW:
returnHash = (challengeHash << 64) >> 64;
// After getting the hash, we unset the flag for this thread
// as the attestation verification will be done asynchronously
// through the hardware platform
unsetAttestation(thread_id);
// remove the thread for the current turn queue;
m_curr_attest_turn.pop();
break;
case MSW:
returnHash = challengeHash >> 64;
Expand All @@ -51,13 +48,16 @@ UInt64 AttestationManager::getChallengeHash(thread_id_t thread_id, UInt8 word){
return returnHash;
}

UInt16 AttestationManager::getChallengeId(thread_id_t thread_id){
return trustedHwPlatform->getChallengeId(thread_id);
UInt16 AttestationManager::requestTurn(thread_id_t thread_id){
TICKETS--;
m_curr_attest_turn.push(static_cast<thread_id_t>(TICKETS));
return TICKETS;
}

bool AttestationManager::checkChallengeResult(thread_id_t thread_id, UInt128 challenge_result) {
// TODO: The verification should be done by the magical "verifier"
// through the trusted platform
unsetAttestation(thread_id);
return trustedHwPlatform->checkChallengeResult(thread_id, challenge_result);

}
Expand All @@ -70,8 +70,8 @@ bool AttestationManager::checkUnderAttestation(thread_id_t thread_id) {
return false;
}

bool AttestationManager::checkAttestationTurn(thread_id_t thread_id) {
return (m_curr_attest_turn.front() == thread_id);
bool AttestationManager::checkAttestationTurn(UInt16 ticket) {
return (m_curr_attest_turn.front() == ticket);
}

void AttestationManager::unsetAttestation(thread_id_t thread_id) {
Expand All @@ -86,3 +86,13 @@ void AttestationManager::unsetAttestation(thread_id_t thread_id) {
bool AttestationManager::checkAllFinished() {
return m_curr_attest_turn.empty();
}
void AttestationManager::updateSequencer() {
if (!m_curr_attest_turn.empty()) {
cout << "[SEQUENCER]: Serving ticket " << std::dec <<m_curr_attest_turn.front() <<endl;
m_curr_attest_turn.pop();
}
}

bool AttestationManager::checkUnderAttestationGlobal() {
return (!m_curr_attest_threads.empty());
}
18 changes: 14 additions & 4 deletions common/system/attestation_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fixed_types.h"
#include "trusted_hw_platform.h"
#include "dev_under_attestation.h"
#include "subsecond_time.h"
//#include "simulator.h"
//#include "thread.h"
//#include "thread_manager.h"
Expand All @@ -16,6 +17,10 @@
#include <random>


#define MSW 1
#define LSW 0


using namespace std;


Expand All @@ -27,12 +32,13 @@ class AttestationManager {

void setAttestation(thread_id_t thread_id);
UInt64 getChallengeHash(thread_id_t thread_id, UInt8 word);
UInt16 getChallengeId(thread_id_t thread_id);
UInt16 requestTurn(thread_id_t thread_id);
bool checkChallengeResult(thread_id_t thread_id, UInt128 challenge_result);
bool checkUnderAttestation(thread_id_t thread_id);
bool checkAttestationTurn(thread_id_t thread_id);
bool checkAttestationTurn(UInt16 ticket);
bool checkAllFinished();
// void sequencer();
void updateSequencer();
bool checkUnderAttestationGlobal();



Expand All @@ -42,7 +48,11 @@ class AttestationManager {
UInt16 computeChallengeId();
void unsetAttestation(thread_id_t thread_id);
vector<thread_id_t> m_curr_attest_threads;
queue<thread_id_t> m_curr_attest_turn;
queue<thread_id_t> m_curr_attest_turn;
UInt16 TICKETS = 2000;
// ComponentLatency *seq_latency;
// ComponentPeriod seq_period = ComponentPeriod::fromFreqHz(1000000000);
// //const ComponentPeriod *seq_period_ptr =&seq_period;
};

#endif
6 changes: 3 additions & 3 deletions common/system/magic_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ UInt64 handleMagicInstruction(thread_id_t thread_id, UInt64 cmd, UInt64 arg0, UI
}
case SIM_CMD_ATTESTATION_TURN:
{
return Sim()->getAttestationManager()->checkAttestationTurn(thread_id);
return Sim()->getAttestationManager()->checkAttestationTurn(arg0);
}
case SIM_CMD_CHALLENGE_ID:
case SIM_CMD_REQUEST_TURN:
{
return Sim()->getAttestationManager()->getChallengeId(thread_id);
return Sim()->getAttestationManager()->requestTurn(thread_id);
}
case SIM_CMD_CHALLENGE_HASH:
{
Expand Down
41 changes: 21 additions & 20 deletions common/system/trusted_hw_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ TrustedHwPlatform::~TrustedHwPlatform() {
}

UInt128 TrustedHwPlatform::getChallengeHash(thread_id_t thread_id){
DevUnderAttestation * curr_dev = getDevicebyThreadId(thread_id);
UInt128 challengeHash;
if (curr_dev) {
if (curr_dev->getChallengeHash()!= 0)
return curr_dev->getChallengeHash();
else {
challengeHash = computeChallengeHash();
curr_dev->setChallengeHash(challengeHash);
curr_dev->printHash();
return challengeHash;
}
}
else {
curr_dev = new DevUnderAttestation(thread_id);
challengeHash = computeChallengeHash();
curr_dev->setChallengeHash(challengeHash);
curr_dev->printHash();
m_devices_table.push_back(curr_dev);
return challengeHash;
}
// DevUnderAttestation * curr_dev = getDevicebyThreadId(thread_id);
// UInt128 challengeHash;
// if (curr_dev) {
// if (curr_dev->getChallengeHash()!= 0)
// return curr_dev->getChallengeHash();
// else {
// challengeHash = computeChallengeHash();
// curr_dev->setChallengeHash(challengeHash);
// curr_dev->printHash();
// return challengeHash;
// }
// }
// else {
// curr_dev = new DevUnderAttestation(thread_id);
// challengeHash = computeChallengeHash();
// curr_dev->setChallengeHash(challengeHash);
// curr_dev->printHash();
// m_devices_table.push_back(curr_dev);
// return challengeHash;
// }
return computeChallengeHash();
}

UInt16 TrustedHwPlatform::getChallengeId(thread_id_t thread_id){
Expand Down
Loading

0 comments on commit 4f923ca

Please sign in to comment.