vestigia

Detect where execution depends on secrets.
git clone git://git.ppad.tech/vestigia.git
Log | Files | Refs | README | LICENSE

diffprobe.c (887B)


      1 /* A secret chosen by the first argument (same width for every
      2  * secret, so the stack lays out the same) reaching a branch and a
      3  * table index, and a constant-time select that touches neither. */
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 static unsigned char table[256];
      8 
      9 __attribute__((noinline)) int branchy(unsigned s) {
     10   int r = 0;
     11   for (unsigned i = 0; i < (s & 7); i++) r += i;
     12   return r;
     13 }
     14 
     15 __attribute__((noinline)) int lookup(unsigned s) {
     16   return table[s & 0xff];
     17 }
     18 
     19 __attribute__((noinline)) unsigned ct(unsigned s, unsigned p) {
     20   unsigned m = 0u - (p & 1u);
     21   return (s & m) | (s ^ m);
     22 }
     23 
     24 int main(int argc, char **argv) {
     25   unsigned s = (unsigned)strtoul(argc > 1 ? argv[1] : "00", NULL, 16);
     26   for (int i = 0; i < 256; i++) table[i] = (unsigned char)i;
     27   int a = branchy(s);
     28   int b = lookup(s);
     29   unsigned c = ct(s, 1);
     30   printf("%d %d %u\n", a, b, c);
     31   return 0;
     32 }