Time measurements differ on microcontroller

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
SuomyNonaPatri
Level 1
Level 1
10 sign-ins 5 questions asked 5 sign-ins

Hi

I am measuring the cycle count of different C functions which I try to make constant time in order to mitigate side channel attacks (crypto).

I am working with a microcontroller (aurix from infineon) which has an onboard cycle counter which gets incremented each clock tick and which I can read out.

Consider the following:

int result[32], cnt=0;
int secret[32];
/** some other code***/

reset_and_startCounter(); //resets cycles to 0 and starts the counter
int tmp = readCycles(); //read cycles before function call

function(secret) //I want to measure this function, should be constant time

result[cnt++] = readCycles() - tmp; //read out cycles and subtract to get correct result

When I measure the cycles like shown above, I will sometimes receive a different amount of cycles depending on the input given to the function. (~1-10 cycles difference, function itself takes about 3000 cycles).

I was now wondering if it not yet is perfectly constant time, and that the calculations depend on some input. I looked into the function and did the following:

void function(int* input){
    reset_and_startCounter(); 
    int tmp = readCycles(); 
   /*********************************
    ******calculations on input******
    *********************************/
    result[cnt++] = readCycles() - tmp;
}

and I received the same amount of cycles no matter what input is given.

I then also measured the time needed to call the function only, and to return from the function. Both measurements were the same no matter what input.

I was always using the gcc compiler flags -O3,-fomit-frame-pointer. -O3 because the runtime is critical and I need it to be fast. And also important, no other code has been running on the microcontroller (no OS etc.)

Does anyone have a possible explanation for this. I want to be secure, that my code is constant time, and those cycles are arbitrary...

And sorry for not providing a runnable code here, but I believe not many have an Aurix  shaglevoojio lying arround 😮

Thank you

 

0 Likes
4 Replies
TBencher
Level 6
Level 6
25 solutions authored 25 likes received 5 questions asked

Hi,

if you are not putting something into your function(int* input), your main() and  function() are quite similar except the function call. Could you try to do some more calculation or calls inside your function?  You could even block your function for a while just to see different results. 

Best regards,

TBencher

0 Likes
TBencher
Level 6
Level 6
25 solutions authored 25 likes received 5 questions asked

Hi,

could it be that your reset_and_startCounter() function causes the same result?

Best regards,

TBencher

0 Likes
TBencher
Level 6
Level 6
25 solutions authored 25 likes received 5 questions asked

Hi,

if you are using the same counter for both measurements... the measurement could be wrong...
Hence, just delete the reset_and_startCounter() in your function()...

Best regards,

TBencher

0 Likes
TBencher
Level 6
Level 6
25 solutions authored 25 likes received 5 questions asked

Hi,

I mean using the same counter is ok, but you should not reset the counter in between...

Best regards,

TBencher

0 Likes