Skip to content

Commit

Permalink
Basic attestation compleated
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeferson Gonzalez committed Mar 25, 2022
1 parent 8b85520 commit 202b70a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 67 deletions.
65 changes: 21 additions & 44 deletions benchmarks/myapps/attestation/attestation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ void readADC(uint16_t * input);
void FIRFilter(uint16_t * input, float * output);


//bool simCheckAttestation();
//bool SimCheckAttestationTurn();
//uint16_t SimGetChallengeId();
//__uint128_t simGetChallengeSeed();
void computeChallenge(uint16_t challenge);
void simSendChallengeResult(__uint128_t result);
void enableHashComputation(uint16_t flag);
Expand All @@ -52,12 +48,8 @@ void mainSetup() {

int mainLoop(int iter) {
SimRoiStart();
cout <<"Entering main loop for " <<iter << " iterations" <<endl;
// float filtered;
// float encrypted;

// "Infinite" loop
//while (true) {
if (DEBUG)
cout <<"Entering main loop for " <<iter << " iterations" <<endl;
for (size_t i = 0; i < iter; i++){
//cout << "*************Iteration = " <<std::dec<<i<<" *************"<<endl;
// ADDED CODE HERE
Expand All @@ -66,26 +58,30 @@ int mainLoop(int iter) {
bool attestation = SimCheckAttestation();
bool myTurn = false;
if (attestation) {
cout << "Starting atatestation" <<endl;
if (DEBUG)
cout << "Starting atatestation" <<endl;
while (!myTurn)
// Wait until it's my turn on the queue
myTurn = SimCheckAttestationTurn(); //need id?
cout << "It's my turn" <<endl;
if (DEBUG)
cout << "It's my turn" <<endl;
// If it's my turn, get the challenge
uint16_t challenge = SimGetChallengeId(); //need id?
// Now get the challenge's hash
hash_seed = SimGetChallengeHashMSW(); // Most Significant Word
hash_seed = hash_seed << 64 | SimGetChallengeHashLSW(); //Least Significant Word
debugPrintHash("MAIN", hash_seed);
cout<< "Got challenge "<< challenge << endl;
if (DEBUG)
cout<< "Got challenge "<< challenge << endl;
// Compute Challenge
computeChallenge(challenge);

}
// END of ADDED CODE

processing_:
cout << "Doing normal processing on iteration " <<std::dec<< i <<endl;
if (DEBUG)
cout << "Doing normal processing on iteration " <<std::dec<< i <<endl;

uint16_t inputs [SAMPLES];
float outputs [SAMPLES];
Expand Down Expand Up @@ -125,7 +121,8 @@ void readADC(uint16_t * input) {
input[i] = ushort(gen);

if (attestation_flags.at(0)) {
cout << "readADC under attestation " << endl;
if (DEBUG)
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;
Expand Down Expand Up @@ -157,7 +154,8 @@ void FIRFilter(uint16_t * input, float * output) {
output[sample] = filtered;
}
if (attestation_flags.at(1)) {
cout << "FIRFilter under attestation " << endl;
if (DEBUG)
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;
Expand All @@ -181,7 +179,8 @@ int main(int argc, char* argv[]){
// This method sets global flags to enable
// different hash computations on attestation
void computeChallenge(uint16_t challenge) {
cout <<"Enabling proper flags for challenge computing"<<endl;
if (DEBUG)
cout <<"Enabling proper flags for challenge computing"<<endl;
switch(challenge) {
case 0:
enableHashComputation(1);
Expand All @@ -199,45 +198,23 @@ void computeChallenge(uint16_t challenge) {
}

void enableHashComputation(uint16_t flag) {
cout << "Enable Hash Called with " << flag <<endl;
if (DEBUG)
cout << "Enable Hash Called with " << flag <<endl;
for (size_t i = 0; i < flag; i++){
attestation_flags.at(i) = true;
}
}

void disableAllFlags(){
for (size_t i = 0; i < attestation_flags.size(); i++){
attestation_flags.at(i) = false;
}

}



// Simulator Mockup Methods
// bool simCheckAttestation(){
// cout << "Check Attestation Called" <<endl;
// std::uniform_int_distribution<> uchar(0, 255);
// return (uchar(gen) > 127);
// }
// bool SimCheckAttestationTurn(){
// cout << "Check Turn Called "<< endl;
// std::uniform_int_distribution<> uchar(0, 255);
// return (uchar(gen) > 150);
// }
// uint16_t SimGetChallengeId(){
// cout<< "Get Challenge Called "<< endl;
// std::uniform_int_distribution<> uchar(0, 2);
// return uchar(gen);
// }
// __uint128_t simGetChallengeSeed(){
// cout<< "Get Challenge Seed Called "<< endl;
// std::uniform_int_distribution<> distrib;
// __uint128_t out = (static_cast<__uint128_t>(distrib(gen)) << 96) | (static_cast<__uint128_t>(distrib(gen)) << 64) |
// (static_cast<__uint128_t>(distrib(gen)) << 32) | (static_cast<__uint128_t>(distrib(gen)));
// return out;
// }
void simSendChallengeResult(__uint128_t result) {
cout <<"Send Challenge Called" <<endl;
if (DEBUG)
cout <<"Send Challenge Called" <<endl;
debugPrintHash("SIM", result);
}

Expand Down
2 changes: 1 addition & 1 deletion common/scheduler/scheduler_open.cc
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ void SchedulerOpen::threadExit(thread_id_t thread_id, SubsecondTime time) {
}
}

cout << "\n[Scheduler][Result]: Task " << app_id << " (Response/Service/Wait) Time (ns) " << " :\t" << time.getNS() - openTasks[app_id].taskArrivalTime << "\t" << time.getNS() - openTasks[app_id].taskStartTime << "\t" << openTasks[app_id].taskStartTime - openTasks[app_id].taskArrivalTime << "\n";
cout << "\n[Scheduler][Result]: Task " << app_id << " (Response/Service/Wait) Time (ns) " << " :\t" << std::dec << time.getNS() - openTasks[app_id].taskArrivalTime << "\t" << time.getNS() - openTasks[app_id].taskStartTime << "\t" << openTasks[app_id].taskStartTime - openTasks[app_id].taskArrivalTime << "\n";

}

Expand Down
33 changes: 18 additions & 15 deletions common/system/attestation_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ AttestationManager::~AttestationManager() {
free(m_devices.at(i));
}



void AttestationManager::setAttestation(thread_id_t thread_id){
//TODO: I don't think we need to set the attestation flag on the thread itself
// the manager should be able to handle this
Sim()->getThreadManager()->getThreadFromID(thread_id)->setUnderAttestation();
cout <<"[Attestation Manager]: Setting Attestation for Thread " << thread_id <<endl;
DevUnderAttestation * curr_device = getDevicebyThreadId(thread_id);
if (!curr_device) {
curr_device = new DevUnderAttestation(thread_id);
m_devices.push_back(curr_device);
m_fifo.push(curr_device);
}
}

Expand All @@ -47,23 +44,25 @@ UInt64 AttestationManager::getChallengeHash(thread_id_t thread_id){
//return LSW
return (curr_device->getChallengeHash() << 64) >> 64;
}

UInt16 AttestationManager::getChallengeId(thread_id_t thread_id){
DevUnderAttestation * curr_device = getDevicebyThreadId(thread_id);
if (!curr_device) {
curr_device = new DevUnderAttestation(thread_id);
m_devices.push_back(curr_device);
}
curr_device->setChallengeId(computeChallengeId());
curr_device->printChallengeId();
return curr_device->getChallengeId();
}


bool AttestationManager::checkChallengeResult(thread_id_t thread_id, UInt128 challenge_result) {
DevUnderAttestation * curr_device = getDevicebyThreadId(thread_id);
// The verification is done with by the DUA module itself
// let's return what they say
if (curr_device->verifyChallenge(challenge_result)) {
unsetAttestation(thread_id);
m_fifo.pop();
return true;
}
return false;
Expand All @@ -77,15 +76,8 @@ bool AttestationManager::checkUnderAttestation(thread_id_t thread_id) {
return false;
}

void AttestationManager::unsetAttestation(thread_id_t thread_id) {
for (size_t i = 0; i < m_devices.size(); i++) {
if (m_devices.at(i)->m_thread_id == thread_id)
m_devices.at(i)->m_marked_for_delete = true;
}

m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
[] (DevUnderAttestation * t) {return t->m_marked_for_delete;}),
m_devices.end());
bool AttestationManager::checkAttestationTurn(thread_id_t thread_id) {
return (m_fifo.front()->m_thread_id == thread_id);
}

// Private methods
Expand All @@ -107,4 +99,15 @@ UInt128 AttestationManager::computeChallengeHash() {
UInt16 AttestationManager::computeChallengeId() {
std::uniform_int_distribution<> uchar(0, 2);
return uchar(gentr);
}

void AttestationManager::unsetAttestation(thread_id_t thread_id) {
for (size_t i = 0; i < m_devices.size(); i++) {
if (m_devices.at(i)->m_thread_id == thread_id)
m_devices.at(i)->m_marked_for_delete = true;
}

m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
[] (DevUnderAttestation * t) {return t->m_marked_for_delete;}),
m_devices.end());
}
7 changes: 5 additions & 2 deletions common/system/attestation_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "thread.h"
#include "thread_manager.h"
#include <vector>
#include <queue>
#include <random>
#include <list>

Expand All @@ -28,7 +29,7 @@ class AttestationManager {
UInt16 getChallengeId(thread_id_t thread_id);
bool checkChallengeResult(thread_id_t thread_id, UInt128 challenge_result);
bool checkUnderAttestation(thread_id_t thread_id);
void unsetAttestation(thread_id_t thread_id);
bool checkAttestationTurn(thread_id_t thread_id);



Expand All @@ -37,7 +38,9 @@ class AttestationManager {
vector<DevUnderAttestation*> m_devices;
DevUnderAttestation * getDevicebyThreadId(thread_id_t);
UInt128 computeChallengeHash();
UInt16 computeChallengeId();
UInt16 computeChallengeId();
void unsetAttestation(thread_id_t thread_id);
queue<DevUnderAttestation *> m_fifo;
};

#endif
4 changes: 4 additions & 0 deletions common/system/dev_under_attestation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ bool DevUnderAttestation::verifyChallenge(UInt128 challenge_result) {
m_challenge_hash = 0;
m_challenge_id = 0;
return true;
}

void DevUnderAttestation::printChallengeId() {
cout<<"[Attestation Manager] [DUA/"<< m_thread_id <<"]:" << " Challenge ID = 0x" << m_challenge_id <<endl;
}
1 change: 1 addition & 0 deletions common/system/dev_under_attestation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DevUnderAttestation{
UInt128 getChallengeHash() const {return m_challenge_hash; }
bool verifyChallenge(UInt128 challenge_result);
void printHash();
void printChallengeId();
bool m_marked_for_delete = false;

private:
Expand Down
3 changes: 1 addition & 2 deletions common/system/magic_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ UInt64 handleMagicInstruction(thread_id_t thread_id, UInt64 cmd, UInt64 arg0, UI
}
case SIM_CMD_ATTESTATION_TURN:
{
LOG_PRINT_WARNING("Check attestation turn not implemented yet, returning true by default");
return Sim()->getThreadManager()->getThreadFromID(thread_id)->isnextForAttestation();
return Sim()->getAttestationManager()->checkAttestationTurn(thread_id);
}
case SIM_CMD_CHALLENGE_ID:
{
Expand Down
4 changes: 2 additions & 2 deletions config/base.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ queuePolicy = FIFO #Set the queuing policy. Currently support: FIFO, priority.
distribution = poisson #Set the arrival distribution of open workload. Currently supported: uniform, poisson, explicit
distributionSeed = 815 #Set the seed for the random distribution (for repeatability). Use 0 to generate a seed.
arrivalRate = 1 #Set the rate at which tasks arrive together.
arrivalInterval = 10000000 #Set the (expected) interval between two arrivals in nano seconds.
arrivalInterval = 0 #Set the (expected) interval between two arrivals in nano seconds. 10000000 was default
explicitArrivalTimes=0,0,0,0
core_mask = 1 # Mask of cores on which threads can be scheduled (default: 1, all cores)
preferred_core = -1 # -1 is used to detect the end of the preferred order
Expand All @@ -346,7 +346,7 @@ explicitPriorityValues = 1,2,3,4,5,6,7

[scheduler/open/attestation]
logic = all # set the attestation algorithm used. Possible algorithms: off (no attestation), all (all cores), perf (performance based)
epoch = 10000000 # set the attestation epoch in ns; granularity at which the attestation should be checked
epoch = 5000000 # set the attestation epoch in ns; granularity at which the attestation should be checked

[scheduler/open/migration]
logic = off # set the migration algorithm used. Possible algorithms: off (no migration)
Expand Down
2 changes: 1 addition & 1 deletion simulationcontrol/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_static_power():
run(['4.0GHz', 'testStaticPower', 'slowDVFS'], get_instance('parsec-blackscholes', 3, input_set='simsmall'))

def test_custom_app(appname):
run(['4.0GHz', 'fastDVFS', 'maxFreq'], '{}-{}-{}'.format(appname, 2, 1))
run(['4.0GHz', 'fastDVFS', 'maxFreq'], '{}-{}-{}'.format(appname, 100, 1) + ',{}-{}-{}'.format(appname, 100, 1))
#run(['4.0GHz', 'mediumDVFS', 'maxFreq'], '{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format(appname,1000000, 1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 1000000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 1000000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 1000000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 1000000000,1) + ',{}-{}-{}'.format('myapps-my_pi', 100000000,1))


Expand Down

0 comments on commit 202b70a

Please sign in to comment.