SHARE
TWEET

NoSuchCon 2013 Whitebox Generator

elvanderb Jun 3rd, 2013 836 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. const uint8_t S_box[]     = {0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16};
  6. const uint8_t gmul[3][0x100]    = {
  7.                                 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51,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,0x2A ,0x0B ,0x08 ,0x0D ,0x0E ,0x07 ,0x04 ,0x01 ,0x02 ,0x13 ,0x10 ,0x15 ,0x16 ,0x1F ,0x1C ,0x19 ,0x1A}
  10.                             };
  11. const uint8_t rcon[]      = {0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39, 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25, 0x4A, 0x94, 0x33, 0x66, 0xCC, 0x83, 0x1D, 0x3A, 0x74, 0xE8, 0xCB, 0x8D, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39, 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25, 0x4A, 0x94, 0x33, 0x66, 0xCC, 0x83, 0x1D, 0x3A, 0x74, 0xE8, 0xCB, 0x8D, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39, 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25, 0x4A, 0x94, 0x33, 0x66, 0xCC, 0x83, 0x1D, 0x3A, 0x74, 0xE8, 0xCB, 0x8D, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39, 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25, 0x4A, 0x94, 0x33, 0x66, 0xCC, 0x83, 0x1D, 0x3A, 0x74, 0xE8, 0xCB, 0x8D, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39, 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25, 0x4A, 0x94, 0x33, 0x66, 0xCC, 0x83, 0x1D, 0x3A, 0x74, 0xE8, 0xCB, 0x8D};
  12. const uint8_t MDS[4] = {2,3,1,1};
  13.  
  14. #define rotate(a) (((a) >> 8) | ((a) << 24))
  15. #define dword(t)  (((t)[0]) | (((t)[1]) << 8) | (((t)[2]) << 16) | (((t)[3]) << 24))
  16.  
  17. typedef struct sub_s {
  18.     uint8_t sub[0x100];
  19.     uint8_t inv_sub[0x100];
  20. } *sub_t;
  21.  
  22. typedef struct aes_wb_s {
  23.     struct sub_s initSub;
  24.     struct sub_s finalSub;
  25.     uint8_t xorTables[3][0x10000];
  26.     uint8_t roundTables[9][16][4][0x100];
  27.     uint8_t finalTable[16][0x100];
  28. } *aes_wb_t;
  29.  
  30. #include <windows.h>
  31. DWORD sec_rnd()
  32. {
  33.     HCRYPTPROV hProvider = 0;
  34.     DWORD retval;
  35.  
  36.     if (! CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  37.         return GetTickCount();
  38.  
  39.     if (! CryptGenRandom(hProvider, 4, (BYTE*)&retval))
  40.     {
  41.         CryptReleaseContext(hProvider, 0);
  42.         return GetTickCount();
  43.     }
  44.  
  45.     if (! CryptReleaseContext(hProvider, 0));
  46.  
  47.     return retval;
  48. }
  49.  
  50. void key_schedule(aes_wb_t aes, const uint8_t key[16], uint8_t (*xkey)[11][16])
  51. {
  52.     uint8_t i;
  53.     uint32_t d;
  54.  
  55.     memcpy((*xkey)[0], key, 16);
  56.     d = dword(&(*xkey)[0][12]);
  57.     for (i = 1; i < 11; i ++)
  58.     {
  59.         uint8_t j;
  60.  
  61.         d = rotate(d);
  62.         for (j = 0; j < 32; j += 8)
  63.             d = (S_box[(d >> j) & 0xFF] << j) | (d & ~(0xFF << j));
  64.         d ^= rcon[i];
  65.  
  66.         for (j = 0; j < 16; j += 4)
  67.         {
  68.             d ^= dword(&(*xkey)[i-1][j]);
  69.             (*xkey)[i][j] = d;
  70.             (*xkey)[i][j+1] = d >> 8;
  71.             (*xkey)[i][j+2] = d >> 16;
  72.             (*xkey)[i][j+3] = d >> 24;
  73.         }
  74.     }
  75.     /*
  76.     // uncomment to check key schedule
  77.     for (d = 0; d < 11u*16u; d++)
  78.     {
  79.         if (d % 16 == 0)
  80.             printf("\n");
  81.         printf("%02X ", (*xkey)[d/16u][d%16u]);
  82.     }
  83.     */
  84.  
  85. }
  86.  
  87. void new_sub(sub_t sub)
  88. {
  89.     uint16_t i;
  90.     uint8_t r;
  91.     const uint8_t masks[] = {0, 1, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
  92.     for (i = 0; i < 0x100; i++)
  93.         sub->sub[i] = (uint8_t)i;
  94.  
  95.     for (i = 0xFF; i > 0; i--)
  96.     {
  97.         uint8_t tmp;
  98.         while((r = (sec_rnd() & masks[i])) > i);
  99.         tmp = sub->sub[i];
  100.         sub->sub[i] = sub->sub[r];
  101.         sub->sub[r] = tmp;
  102.     }
  103.  
  104.     for (i = 0; i < 0x100; i++)
  105.         sub->inv_sub[sub->sub[i]] = (uint8_t)i;
  106. }
  107.  
  108. void new_aes(aes_wb_t aes, const uint8_t key[16])
  109. {
  110.     uint8_t i;
  111.     uint8_t round;
  112.     uint32_t w;
  113.     struct sub_s byteSubs[4];
  114.     struct sub_s xorSubs[3];
  115.     uint8_t xkey[11][16];
  116.  
  117.     key_schedule(aes, key, &xkey);
  118.  
  119.     new_sub(&aes->initSub);
  120.     new_sub(&aes->finalSub);
  121.     for  (i = 0; i < 4; i++)
  122.         new_sub(&byteSubs[i]);
  123.     for  (i = 0; i < 3; i++)
  124.         new_sub(&xorSubs[i]);
  125.  
  126.     for (w = 0; w < 0x10000u; w++)
  127.     {
  128.         uint8_t b0 = w >> 8;
  129.         uint8_t b1 = w & 0xFF;
  130.         b0 = byteSubs[0].inv_sub[b0];
  131.         b1 = byteSubs[1].inv_sub[b1];
  132.         aes->xorTables[0][w] = xorSubs[0].sub[b0 ^ b1];
  133.     }
  134.  
  135.     for (w = 0; w < 0x10000u; w++)
  136.     {
  137.         uint8_t b2 = w >> 8;
  138.         uint8_t b3 = w & 0xFF;
  139.         b2 = byteSubs[2].inv_sub[b2];
  140.         b3 = byteSubs[3].inv_sub[b3];
  141.         aes->xorTables[1][w] = xorSubs[1].sub[b2 ^ b3];
  142.     }
  143.  
  144.     for (w = 0; w < 0x10000u; w++)
  145.     {
  146.         uint8_t b01 = w >> 8;
  147.         uint8_t b23 = w & 0xFF;
  148.         b01 = xorSubs[0].inv_sub[b01];
  149.         b23 = xorSubs[1].inv_sub[b23];
  150.         aes->xorTables[2][w] = xorSubs[2].sub[b01 ^ b23];
  151.     }
  152.  
  153.     // round 0
  154.     for (i = 0; i < 16; i++)
  155.     {
  156.         uint8_t  j;
  157.         uint16_t b;
  158.         uint8_t  rands[4];
  159.  
  160.         for (j = 1; j < 4; j++)
  161.             rands[j] = (uint8_t)sec_rnd();
  162.         rands[0] = rands[1]^rands[2]^rands[3]^xkey[1][(i%4u)*4 + i/4u];
  163.  
  164.         for (j = 0; j < 4; j++)
  165.         {
  166.             uint8_t k1 = xkey[0][j + ((i+j) % 4u)*4];
  167.             const uint8_t* mul = gmul[MDS[((i/4u)*3 + j) % 4u]-1];
  168.             uint8_t k2 = rands[j];
  169.             uint8_t *bsub = byteSubs[j].sub;
  170.  
  171.             for (b = 0; b < 0x100; b++)
  172.             {
  173.                 uint8_t n = aes->initSub.sub[b];
  174.                 n ^= k1;
  175.                 n = S_box[n];
  176.                 n = mul[n];
  177.                 n ^= k2;
  178.                 aes->roundTables[0][i][j][b] = bsub[n];
  179.             }
  180.         }
  181.     }
  182.  
  183.     for (round = 1; round < 9; round ++)
  184.     {
  185.         for (i = 0; i < 16; i++)
  186.         {
  187.             uint8_t  j;
  188.             uint16_t b;
  189.             uint8_t  rands[4];
  190.  
  191.             for (j = 1; j < 4; j++)
  192.                 rands[j] = (uint8_t)sec_rnd();
  193.             rands[0] = rands[1]^rands[2]^rands[3]^xkey[round+1][(i%4u)*4 + i/4u];
  194.  
  195.             for (j = 0; j < 4; j++)
  196.             {
  197.                 const uint8_t* mul = gmul[MDS[((i/4u)*3 + j) % 4u]-1];
  198.                 uint8_t k = rands[j];
  199.                 uint8_t *bsub = byteSubs[j].sub;
  200.  
  201.                 for (b = 0; b < 0x100; b++)
  202.                 {
  203.                     uint8_t n = xorSubs[2].inv_sub[b];
  204.                     n = S_box[n];
  205.                     n = mul[n];
  206.                     n ^= k;
  207.                     aes->roundTables[round][i][j][b] = bsub[n];
  208.                 }
  209.             }
  210.         }
  211.     }
  212.  
  213.     // round 10
  214.     for ( i = 0; i < 16; i++)
  215.     {
  216.          uint16_t b;
  217.          uint8_t k = xkey[10][(i%4u)*4 + i/4u];
  218.  
  219.          for (b = 0; b < 0x100; b++)
  220.          {
  221.              uint8_t n = xorSubs[2].inv_sub[b];
  222.              n = S_box[n];
  223.              n ^= k;
  224.              aes->finalTable[i][b] = aes->finalSub.sub[n];
  225.          }
  226.     }
  227. }
  228.  
  229. int main()
  230. {
  231.     struct aes_wb_s aes;
  232.     uint8_t i, round;
  233.     uint8_t t[12][16];
  234.     uint8_t m[] = "testtesttesttest";
  235.     FILE* f;
  236.     new_aes(&aes, "KEY_KEY_KEY_KEY_");
  237.  
  238.     for (i = 0; i < 16; i++)
  239.         t[0][(i%4u)*4 + (i/4u)] = aes.initSub.inv_sub[m[i]];
  240.  
  241.     // black magic...
  242.     for (round = 1; round < 10; round ++)
  243.     {
  244.         for (i = 0; i < 16; i++)
  245.         {
  246.             uint8_t b[4];
  247.             uint8_t j;
  248.             for (j = 0; j < 4; j++)
  249.                 b[j] = aes.roundTables[round-1][i][j][t[round-1][j*4+((i+j)%4u)]];
  250.             t[round][i] = aes.xorTables[2][(aes.xorTables[0][(b[0]<<8)|b[1]] << 8) | aes.xorTables[1][(b[2]<<8)|b[3]]];
  251.         }
  252.     }
  253.     for (i = 0; i < 16; i++)
  254.         t[10][i/4u + (i%4u)*4] = aes.finalTable[i][t[9][(i&(~3)) +((i+i/4)%4u)]];
  255.  
  256.     /*
  257.     >>> from Crypto.Cipher import AES
  258.     >>> " ".join("%02X"%ord(c) for c in AES.new("KEY_KEY_KEY_KEY_").encrypt("testtesttesttest"))
  259.     '4C 74 7B 5C 53 54 DB 2B 6D 13 39 C9 31 81 33 40'
  260.     */
  261.     printf("pre-calc: 4C 74 7B 5C 53 54 DB 2B 6D 13 39 C9 31 81 33 40\ncomputed: ");
  262.  
  263.     // check...
  264.     for (i = 0; i < 16; i++)
  265.         printf("%02X ", aes.finalSub.inv_sub[t[10][i]]);
  266.     printf("\n");
  267.  
  268.  
  269.  
  270.     f = fopen("wbt", "wb");
  271.     fwrite(&aes, 1, sizeof(aes), f);
  272.     fclose(f);
  273.     return 0;
  274. }
RAW Paste Data
Top