SHARE
TWEET

RE150 ELF

finalshare Feb 5th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define ROTL8(x,shift) ((uint8_t) ((x) << (shift)) | ((x) >> (8 - (shift))))
  7.  
  8. void
  9. init_table(uint8_t sbox[256])
  10. {
  11.     int t;
  12.     srand(time(NULL));
  13.     do {
  14.         t = rand() & 0xff;
  15.     } while (!t);
  16.  
  17.     sbox[0] = t;
  18.     /* loop invariant: p * q == 1 in the Galois field */
  19.     uint8_t p = 1, q = 1;
  20.     do {
  21.         /* multiply p by x+1 */
  22.         p = p ^ (p << 1) ^ (p & 0x80 ? 0x1B : 0);
  23.         /* divide q by x+1 */
  24.         q ^= q << 1;
  25.         q ^= q << 2;
  26.         q ^= q << 4;
  27.         q ^= q & 0x80 ? 0x09 : 0;
  28.         /* compute the affine transformation */
  29.         sbox[p] = sbox[0] ^ q ^ ROTL8(q, 1) ^ ROTL8(q, 2) ^ ROTL8(q, 3) ^ ROTL8(q, 4);
  30.     } while (p != 1);
  31.     /* 0 is a special case since it has no inverse */
  32. }
  33.  
  34. int
  35. main(int argc, char *argv[])
  36. {
  37.     uint8_t sbox[256];
  38.     int i;
  39.  
  40.     if(argc < 2){
  41.         printf("usage: %s flag\n", argv[0]);
  42.         exit(1);
  43.     }
  44.  
  45.     init_table(sbox);
  46.  
  47.     for (i = 0; i < strlen(argv[1]); ++i){
  48.         printf("%02x", sbox[argv[1][i]]);
  49.     }
  50.     putchar('\n');
  51.  
  52.     return 0;
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top