Advertisement
Guest User

wildfire-2.h

a guest
Feb 20th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.99 KB | None | 0 0
  1. #ifndef WILDFIRE_2_H
  2. #define WILDFIRE_2_H
  3.  
  4. #include <string.h>
  5.  
  6. #define ROUNDS 20
  7. #define ROUNDS_BEFORE_SUB 4
  8. #define WORD_SIZE 4
  9. #define BLOCK_SIZE 16
  10. #define EXPANDED_KEY_SIZE (WORD_SIZE * ROUNDS)
  11. #define WORDS_PER_BLOCK (BLOCK_SIZE / WORD_SIZE)
  12.  
  13. typedef unsigned char byte;
  14.  
  15. void xor_buffer(byte* src, const byte* out, const size_t len) {
  16.     for (unsigned int i = 0; i < len; i++) {
  17.         src[i] ^= out[i];
  18.     }
  19. }
  20.  
  21.  
  22. /*  S-box generation algorithm (unused in compiled code)
  23.     Modified from Rijndael's S-box, with a different affine transformation */
  24. void generate_sbox(byte target[256]) {
  25.    
  26.     byte q = 1;
  27.     byte p = 1;
  28.  
  29.     do {
  30.         // Rijndael multiplicative inverse in GF(2**8) = GF(2)[x]/(x**8 + x**4 + x**3 + x + 1)
  31.         p = p ^ (p << 1) ^ ((p & 0x80) ? 0x1B : 0);
  32.        
  33.         q ^= q << 1;
  34.         q ^= q << 2;
  35.         q ^= q << 4;
  36.         q ^= (q & 0x80) ? 0x09 : 0;
  37.        
  38.         // Affine transformation on result
  39.         byte f = q;
  40.         for (unsigned int i = 0; i < 9; i++) {
  41.             f ^= 0x11;
  42.             f = (f << 1) | (f >> 7); // Rotate f left by 1
  43.             f += 0x2F;
  44.         }
  45.        
  46.         target[p] = f ^ 0x53;
  47.        
  48.     } while (p != 1);
  49.  
  50.     target[0] = 0x7B;
  51. }
  52.  
  53.  
  54. /* Lazy inverse S-box generation code */
  55. void generate_inv_sbox(const byte sbox[256], byte target[256]) {
  56.     for (unsigned int i = 0; i < 256; i++) {
  57.         target[sbox[i]] = i;
  58.     }
  59. }
  60.  
  61.  
  62. /* Pre-generated lookup table of S-box */
  63. const byte S[256] = {
  64.     0x7B, 0xA6, 0x73, 0x81, 0x14, 0x82, 0xFF, 0x5A, 0xA3, 0xAB, 0x7F, 0xE7, 0x0C, 0x0B, 0x86, 0x8C,
  65.     0x1D, 0x91, 0x26, 0x20, 0xE4, 0x25, 0xB1, 0x4E, 0x38, 0xD8, 0xAC, 0x78, 0x58, 0x2F, 0x72, 0x67,
  66.     0x32, 0xBF, 0xF4, 0x17, 0x84, 0x1B, 0x7A, 0xDF, 0x70, 0xE6, 0x46, 0x02, 0x54, 0x10, 0xCC, 0xB6,
  67.     0x87, 0x89, 0x0D, 0xBC, 0x4D, 0xA9, 0xBD, 0x64, 0x93, 0xC4, 0x65, 0xE1, 0x6F, 0x3D, 0x42, 0x6C,
  68.     0x7D, 0xE5, 0xFC, 0xD7, 0x79, 0x83, 0xB7, 0x40, 0xF6, 0x4A, 0xD4, 0x8F, 0x77, 0xC9, 0xC1, 0x35,
  69.     0x36, 0xEC, 0xED, 0xB9, 0xF3, 0x06, 0x0A, 0x7C, 0xEB, 0xBA, 0xA1, 0x8A, 0xF2, 0xEE, 0x3A, 0x52,
  70.     0xA8, 0x23, 0xC3, 0x8B, 0x62, 0xA4, 0x9E, 0x96, 0x1E, 0xFE, 0x2E, 0x3C, 0x99, 0x47, 0x63, 0x1C,
  71.     0x2C, 0xEF, 0x15, 0x5C, 0x92, 0xE3, 0x03, 0x8E, 0x56, 0xC8, 0xC0, 0x4F, 0x68, 0xB4, 0x5D, 0x00,
  72.     0xB8, 0x04, 0x55, 0x13, 0x1F, 0xB5, 0x12, 0xE0, 0x48, 0xAF, 0xA5, 0x80, 0xD3, 0x16, 0xCA, 0x0F,
  73.     0xDE, 0xF7, 0x07, 0x5E, 0x19, 0x9D, 0x7E, 0x98, 0xDB, 0xA7, 0xFA, 0xAA, 0xBE, 0x9F, 0x9B, 0x9A,
  74.     0xDA, 0xD9, 0xDC, 0x6B, 0x22, 0x50, 0x2B, 0xD6, 0xC7, 0x66, 0x76, 0x11, 0x3B, 0xB2, 0x29, 0x53,
  75.     0x30, 0x39, 0x60, 0x90, 0x45, 0xEA, 0x21, 0x08, 0x31, 0x9C, 0x2A, 0x57, 0x27, 0x8D, 0xE2, 0xDD,
  76.     0x0E, 0x44, 0x6E, 0xAE, 0xC5, 0xD2, 0x88, 0x4C, 0xE9, 0x34, 0xF5, 0x33, 0x09, 0x01, 0x61, 0x75,
  77.     0x85, 0x71, 0xFD, 0x94, 0xCF, 0xFB, 0x24, 0xF0, 0x5B, 0x37, 0xF1, 0x28, 0x69, 0x49, 0xD0, 0x18,
  78.     0x74, 0xE8, 0x4B, 0x51, 0x6D, 0x2D, 0x05, 0x3F, 0x5F, 0x59, 0x95, 0xA0, 0x1A, 0x3E, 0xCE, 0xCD,
  79.     0xC6, 0xB0, 0xAD, 0x6A, 0xC2, 0xB3, 0xF9, 0xF8, 0x41, 0xCB, 0xBB, 0xD5, 0xA2, 0x97, 0xD1, 0x43
  80. };
  81.  
  82.  
  83. /* Pre-generated lookup table of inverse S-box */
  84. const byte inv_S[256] = {
  85.     0x7F, 0xCD, 0x2B, 0x76, 0x81, 0xE6, 0x55, 0x92, 0xB7, 0xCC, 0x56, 0x0D, 0x0C, 0x32, 0xC0, 0x8F,
  86.     0x2D, 0xAB, 0x86, 0x83, 0x04, 0x72, 0x8D, 0x23, 0xDF, 0x94, 0xEC, 0x25, 0x6F, 0x10, 0x68, 0x84,
  87.     0x13, 0xB6, 0xA4, 0x61, 0xD6, 0x15, 0x12, 0xBC, 0xDB, 0xAE, 0xBA, 0xA6, 0x70, 0xE5, 0x6A, 0x1D,
  88.     0xB0, 0xB8, 0x20, 0xCB, 0xC9, 0x4F, 0x50, 0xD9, 0x18, 0xB1, 0x5E, 0xAC, 0x6B, 0x3D, 0xED, 0xE7,
  89.     0x47, 0xF8, 0x3E, 0xFF, 0xC1, 0xB4, 0x2A, 0x6D, 0x88, 0xDD, 0x49, 0xE2, 0xC7, 0x34, 0x17, 0x7B,
  90.     0xA5, 0xE3, 0x5F, 0xAF, 0x2C, 0x82, 0x78, 0xBB, 0x1C, 0xE9, 0x07, 0xD8, 0x73, 0x7E, 0x93, 0xE8,
  91.     0xB2, 0xCE, 0x64, 0x6E, 0x37, 0x3A, 0xA9, 0x1F, 0x7C, 0xDC, 0xF3, 0xA3, 0x3F, 0xE4, 0xC2, 0x3C,
  92.     0x28, 0xD1, 0x1E, 0x02, 0xE0, 0xCF, 0xAA, 0x4C, 0x1B, 0x44, 0x26, 0x00, 0x57, 0x40, 0x96, 0x0A,
  93.     0x8B, 0x03, 0x05, 0x45, 0x24, 0xD0, 0x0E, 0x30, 0xC6, 0x31, 0x5B, 0x63, 0x0F, 0xBD, 0x77, 0x4B,
  94.     0xB3, 0x11, 0x74, 0x38, 0xD3, 0xEA, 0x67, 0xFD, 0x97, 0x6C, 0x9F, 0x9E, 0xB9, 0x95, 0x66, 0x9D,
  95.     0xEB, 0x5A, 0xFC, 0x08, 0x65, 0x8A, 0x01, 0x99, 0x60, 0x35, 0x9B, 0x09, 0x1A, 0xF2, 0xC3, 0x89,
  96.     0xF1, 0x16, 0xAD, 0xF5, 0x7D, 0x85, 0x2F, 0x46, 0x80, 0x53, 0x59, 0xFA, 0x33, 0x36, 0x9C, 0x21,
  97.     0x7A, 0x4E, 0xF4, 0x62, 0x39, 0xC4, 0xF0, 0xA8, 0x79, 0x4D, 0x8E, 0xF9, 0x2E, 0xEF, 0xEE, 0xD4,
  98.     0xDE, 0xFE, 0xC5, 0x8C, 0x4A, 0xFB, 0xA7, 0x43, 0x19, 0xA1, 0xA0, 0x98, 0xA2, 0xBF, 0x90, 0x27,
  99.     0x87, 0x3B, 0xBE, 0x75, 0x14, 0x41, 0x29, 0x0B, 0xE1, 0xC8, 0xB5, 0x58, 0x51, 0x52, 0x5D, 0x71,
  100.     0xD7, 0xDA, 0x5C, 0x54, 0x22, 0xCA, 0x48, 0x91, 0xF7, 0xF6, 0x9A, 0xD5, 0x42, 0xD2, 0x69, 0x06
  101. };
  102.  
  103.  
  104. /* Random unique masks, applied before a given byte passes through the S-box.
  105.    Initialized here as Pi in hexadecimal, with duplicates and 0x00s removed, for the
  106.    sake of removing magic numbers. */
  107. const byte K[32] = {
  108.     0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
  109.     0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 0x4a,
  110.     0x40, 0x93, 0x82, 0x22, 0x99, 0xf3, 0x1d, 0xef,
  111.     0xa9, 0x8e, 0xc4, 0xe6, 0xc8, 0x94, 0x52, 0x1e
  112. };
  113.  
  114. const unsigned char k_len = 32;
  115.  
  116.  
  117. /* Remap function
  118.    This is key to the cipher. The remap function takes in two 32-bit words and outputs a third
  119.    32-bit word. If any of the bits in the input words change, the entire output word will be different
  120.    and should not have any visible correlation to the changes. */
  121. void remap(const byte i[WORD_SIZE], const byte w[WORD_SIZE], byte o[WORD_SIZE]) {
  122.     unsigned char k = 0;
  123.    
  124.     for (unsigned char b = 0; b < WORD_SIZE; b++) {
  125.         o[b] = 0;
  126.         for (unsigned char j = 0; j < WORD_SIZE; j++) {
  127.             unsigned char off = (b + j) % WORD_SIZE;
  128.            
  129.             byte x = S[ K[ k      % k_len] ^ i[off] ] + // Apply mask, pass through S-box
  130.                      S[ K[(k + 1) % k_len] ^ w[off] ];  // Sum values produced by word i and word w
  131.            
  132.            
  133.             o[b] += (x >> j) | (x << (8 - j)); // Rotate and add to o[b]
  134.             k += 2; // Increment mask index by 2
  135.         }  
  136.     }
  137. }
  138.  
  139.  
  140. void key_schedule(const byte *key, const size_t key_len, byte expanded_key[EXPANDED_KEY_SIZE]) {
  141.     unsigned int rg = 0;
  142.    
  143.     // Call remap() with each adjacent pair of words in the key
  144.     // Result is appended to expanded key
  145.     for (unsigned int i = 0; i < (key_len - WORD_SIZE); i += WORD_SIZE) {
  146.         remap(&key[i],
  147.               &key[i + WORD_SIZE],
  148.               &expanded_key[i]);
  149.        
  150.         rg++;
  151.     }
  152.    
  153.     // Special case, the first and last word of the key must be
  154.     // used as well to avoid compression of the key (key collisions)
  155.     remap(&key[0],
  156.           &key[key_len - WORD_SIZE],
  157.           &expanded_key[rg * WORD_SIZE]);
  158.    
  159.     rg++;
  160.    
  161.     // Call remap() with the leftmost word that has not been used yet (indexed with in)
  162.     // as well as the most recently generated word. Result is appended to the expanded
  163.     // key, and this repeats until it reaches the required size
  164.     unsigned int in = 0;
  165.     while (rg < ROUNDS) {
  166.         remap(&expanded_key[in * WORD_SIZE],
  167.               &expanded_key[(rg - 1) * WORD_SIZE],
  168.               &expanded_key[rg * WORD_SIZE]);
  169.        
  170.         in++;
  171.         rg++;
  172.     }
  173. }
  174.  
  175.  
  176. void encrypt_block_scheduled(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte scheduled_key[EXPANDED_KEY_SIZE]) {
  177.     memcpy(outblock, inblock, BLOCK_SIZE);
  178.     byte s[4] = { 0 };
  179.    
  180.     // Loop once per round; a literal interpretation of the diagram
  181.    
  182.     /*
  183.     // Array of pointers to each word in the block
  184.     // Extra slot is reserved for temporary storage when doing shifts
  185.     byte* words[WORDS_PER_BLOCK + 1];
  186.    
  187.     for (unsigned int i = 0; i < WORDS_PER_BLOCK; i++) {
  188.         words[i] = &outblock[WORD_SIZE * i];
  189.     }
  190.    
  191.     for (unsigned int i = 0; i < ROUNDS; i++) {
  192.        
  193.         // Substitution at every multiple of four except the first rounds
  194.         // (The last round exits the for loop)
  195.         if (i != 0 && i % ROUNDS_BEFORE_SUB == 0) {
  196.             for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
  197.                 outblock[j] = S[outblock[j]];
  198.             }
  199.         }
  200.        
  201.         // Call remap() with first word and roundkey
  202.         remap(words[0], &scheduled_key[i * WORD_SIZE], s);
  203.        
  204.         // Xor remapping with all other words in block
  205.         for (unsigned int j = 1; j < WORDS_PER_BLOCK; j++) {
  206.             xor_buffer(words[j], s, WORD_SIZE);
  207.         }
  208.        
  209.         // Shifting pointers left
  210.         // Save first pointer into the extra slot so it will be shifted to the penultimate slot
  211.         words[WORDS_PER_BLOCK] = words[0];
  212.         for (unsigned int j = 0; j < WORDS_PER_BLOCK; j++) {
  213.             words[j] = words[j + 1];
  214.         }
  215.     }
  216.     */
  217.    
  218.     // Faster version, loop unrolled to (i += 4) to avoid mucking with shifting pointers. Bonus,
  219.     // it's actually more readable this way
  220.     for (unsigned int i = 0; i < ROUNDS; i += 4) {
  221.        
  222.         // Xor remapping of first word and round key with other blocks
  223.         remap(&outblock[0], &scheduled_key[i * WORD_SIZE], s);
  224.         xor_buffer(&outblock[4],  s, WORD_SIZE);
  225.         xor_buffer(&outblock[8],  s, WORD_SIZE);
  226.         xor_buffer(&outblock[12], s, WORD_SIZE);
  227.        
  228.         // Xor remapping of second word and round key with other blocks
  229.         remap(&outblock[4], &scheduled_key[(i + 1) * WORD_SIZE], s);
  230.         xor_buffer(&outblock[0],  s, WORD_SIZE);
  231.         xor_buffer(&outblock[8],  s, WORD_SIZE);
  232.         xor_buffer(&outblock[12], s, WORD_SIZE);
  233.        
  234.         // Xor remapping of third word and round key with other blocks
  235.         remap(&outblock[8], &scheduled_key[(i + 2) * WORD_SIZE], s);
  236.         xor_buffer(&outblock[0],  s, WORD_SIZE);
  237.         xor_buffer(&outblock[4],  s, WORD_SIZE);
  238.         xor_buffer(&outblock[12], s, WORD_SIZE);
  239.        
  240.         // Xor remapping of fourth word and round key with other blocks
  241.         remap(&outblock[12], &scheduled_key[(i + 3) * WORD_SIZE], s);
  242.         xor_buffer(&outblock[0], s, WORD_SIZE);
  243.         xor_buffer(&outblock[4], s, WORD_SIZE);
  244.         xor_buffer(&outblock[8], s, WORD_SIZE);
  245.        
  246.         // If this is the last round, we can exit
  247.         if (i >= ROUNDS - 4) {
  248.             return;
  249.         }
  250.        
  251.         // Otherwise we must perform the substitution
  252.         for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
  253.             outblock[j] = S[outblock[j]];
  254.         }
  255.     }
  256. }
  257.  
  258.  
  259. void decrypt_block_scheduled(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte scheduled_key[EXPANDED_KEY_SIZE]) {
  260.     memcpy(outblock, inblock, BLOCK_SIZE);
  261.     byte s[4] = { 0 };
  262.    
  263.     // Loop once per round; a literal interpretation of the diagram
  264.    
  265.     /*
  266.     // Array of pointers to each word in the block
  267.     // Extra slot is reserved for temporary storage when doing shifts
  268.     byte* words[WORDS_PER_BLOCK + 1];
  269.    
  270.     for (unsigned int i = 0; i < WORDS_PER_BLOCK; i++) {
  271.         words[i] = &outblock[WORD_SIZE * i];
  272.     }
  273.    
  274.     for (int i = ROUNDS - 1; i >= 0; i--) {
  275.        
  276.         // Substitution every 4 rounds, as long as this is not the first loop
  277.         if (i != ROUNDS - 1 && i % ROUNDS_BEFORE_SUB == ROUNDS_BEFORE_SUB - 1) {
  278.             for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
  279.                 outblock[j] = inv_S[outblock[j]];
  280.             }
  281.         }
  282.        
  283.         // Call remap() with last word and roundkey
  284.         remap(words[WORDS_PER_BLOCK - 1], &scheduled_key[i * WORD_SIZE], s);
  285.        
  286.         // Xor remapping with all other words in block
  287.         for (unsigned int j = 0; j < WORDS_PER_BLOCK - 1; j++) {
  288.             xor_buffer(words[j], s, WORD_SIZE);
  289.         }
  290.        
  291.         // Shifting pointers right
  292.         for (int j = WORDS_PER_BLOCK - 1; j >= 0; j--) {
  293.             words[j + 1] = words[j];
  294.         }
  295.        
  296.         // Move last pointer (that was shifted into the extra slot) to the first slot
  297.         words[0] = words[WORDS_PER_BLOCK];
  298.     }
  299.     */
  300.    
  301.     // Faster version, loop unrolled to (i -= 4) to avoid mucking with shifting pointers
  302.     for (int i = ROUNDS - 1; i >= 0; i -= 4) {
  303.        
  304.         // Substitution as long as this is not the first loop
  305.         if (i != ROUNDS - 1) {
  306.             for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
  307.                 outblock[j] = inv_S[outblock[j]];
  308.             }
  309.         }
  310.        
  311.         // Xor remapping of fourth word and round key with other blocks
  312.         remap(&outblock[12], &scheduled_key[i * WORD_SIZE], s);
  313.         xor_buffer(&outblock[0], s, WORD_SIZE);
  314.         xor_buffer(&outblock[4], s, WORD_SIZE);
  315.         xor_buffer(&outblock[8], s, WORD_SIZE);
  316.        
  317.         // Xor remapping of third word and round key with other blocks
  318.         remap(&outblock[8], &scheduled_key[(i - 1) * WORD_SIZE], s);
  319.         xor_buffer(&outblock[0],  s, WORD_SIZE);
  320.         xor_buffer(&outblock[4],  s, WORD_SIZE);
  321.         xor_buffer(&outblock[12], s, WORD_SIZE);
  322.        
  323.         // Xor remapping of second word and round key with other blocks
  324.         remap(&outblock[4], &scheduled_key[(i - 2) * WORD_SIZE], s);
  325.         xor_buffer(&outblock[0],  s, WORD_SIZE);
  326.         xor_buffer(&outblock[8],  s, WORD_SIZE);
  327.         xor_buffer(&outblock[12], s, WORD_SIZE);
  328.        
  329.         // Xor remapping of first word and round key with other blocks
  330.         remap(&outblock[0], &scheduled_key[(i - 3) * WORD_SIZE], s);
  331.         xor_buffer(&outblock[4],  s, WORD_SIZE);
  332.         xor_buffer(&outblock[8],  s, WORD_SIZE);
  333.         xor_buffer(&outblock[12], s, WORD_SIZE);
  334.     }
  335. }
  336.  
  337.  
  338. void decrypt_block(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte* key, const size_t key_len) {
  339.     byte key_s[EXPANDED_KEY_SIZE];
  340.     key_schedule(key, key_len, key_s);
  341.     decrypt_block_scheduled(inblock, outblock, key_s); 
  342. }
  343.  
  344.  
  345. void encrypt_block(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte* key, const size_t key_len) {
  346.     byte key_s[EXPANDED_KEY_SIZE];
  347.     key_schedule(key, key_len, key_s);
  348.     encrypt_block_scheduled(inblock, outblock, key_s);
  349. }
  350.  
  351. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement