Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef WILDFIRE_2_H
- #define WILDFIRE_2_H
- #include <string.h>
- #define ROUNDS 20
- #define ROUNDS_BEFORE_SUB 4
- #define WORD_SIZE 4
- #define BLOCK_SIZE 16
- #define EXPANDED_KEY_SIZE (WORD_SIZE * ROUNDS)
- #define WORDS_PER_BLOCK (BLOCK_SIZE / WORD_SIZE)
- typedef unsigned char byte;
- void xor_buffer(byte* src, const byte* out, const size_t len) {
- for (unsigned int i = 0; i < len; i++) {
- src[i] ^= out[i];
- }
- }
- /* S-box generation algorithm (unused in compiled code)
- Modified from Rijndael's S-box, with a different affine transformation */
- void generate_sbox(byte target[256]) {
- byte q = 1;
- byte p = 1;
- do {
- // Rijndael multiplicative inverse in GF(2**8) = GF(2)[x]/(x**8 + x**4 + x**3 + x + 1)
- p = p ^ (p << 1) ^ ((p & 0x80) ? 0x1B : 0);
- q ^= q << 1;
- q ^= q << 2;
- q ^= q << 4;
- q ^= (q & 0x80) ? 0x09 : 0;
- // Affine transformation on result
- byte f = q;
- for (unsigned int i = 0; i < 9; i++) {
- f ^= 0x11;
- f = (f << 1) | (f >> 7); // Rotate f left by 1
- f += 0x2F;
- }
- target[p] = f ^ 0x53;
- } while (p != 1);
- target[0] = 0x7B;
- }
- /* Lazy inverse S-box generation code */
- void generate_inv_sbox(const byte sbox[256], byte target[256]) {
- for (unsigned int i = 0; i < 256; i++) {
- target[sbox[i]] = i;
- }
- }
- /* Pre-generated lookup table of S-box */
- const byte S[256] = {
- 0x7B, 0xA6, 0x73, 0x81, 0x14, 0x82, 0xFF, 0x5A, 0xA3, 0xAB, 0x7F, 0xE7, 0x0C, 0x0B, 0x86, 0x8C,
- 0x1D, 0x91, 0x26, 0x20, 0xE4, 0x25, 0xB1, 0x4E, 0x38, 0xD8, 0xAC, 0x78, 0x58, 0x2F, 0x72, 0x67,
- 0x32, 0xBF, 0xF4, 0x17, 0x84, 0x1B, 0x7A, 0xDF, 0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x28, 0x69, 0x49, 0xD0, 0x18,
- 0x74, 0xE8, 0x4B, 0x51, 0x6D, 0x2D, 0x05, 0x3F, 0x5F, 0x59, 0x95, 0xA0, 0x1A, 0x3E, 0xCE, 0xCD,
- 0xC6, 0xB0, 0xAD, 0x6A, 0xC2, 0xB3, 0xF9, 0xF8, 0x41, 0xCB, 0xBB, 0xD5, 0xA2, 0x97, 0xD1, 0x43
- };
- /* Pre-generated lookup table of inverse S-box */
- const byte inv_S[256] = {
- 0x7F, 0xCD, 0x2B, 0x76, 0x81, 0xE6, 0x55, 0x92, 0xB7, 0xCC, 0x56, 0x0D, 0x0C, 0x32, 0xC0, 0x8F,
- 0x2D, 0xAB, 0x86, 0x83, 0x04, 0x72, 0x8D, 0x23, 0xDF, 0x94, 0xEC, 0x25, 0x6F, 0x10, 0x68, 0x84,
- 0x13, 0xB6, 0xA4, 0x61, 0xD6, 0x15, 0x12, 0xBC, 0xDB, 0xAE, 0xBA, 0xA6, 0x70, 0xE5, 0x6A, 0x1D,
- 0xB0, 0xB8, 0x20, 0xCB, 0xC9, 0x4F, 0x50, 0xD9, 0x18, 0xB1, 0x5E, 0xAC, 0x6B, 0x3D, 0xED, 0xE7,
- 0x47, 0xF8, 0x3E, 0xFF, 0xC1, 0xB4, 0x2A, 0x6D, 0x88, 0xDD, 0x49, 0xE2, 0xC7, 0x34, 0x17, 0x7B,
- 0xA5, 0xE3, 0x5F, 0xAF, 0x2C, 0x82, 0x78, 0xBB, 0x1C, 0xE9, 0x07, 0xD8, 0x73, 0x7E, 0x93, 0xE8,
- 0xB2, 0xCE, 0x64, 0x6E, 0x37, 0x3A, 0xA9, 0x1F, 0x7C, 0xDC, 0xF3, 0xA3, 0x3F, 0xE4, 0xC2, 0x3C,
- 0x28, 0xD1, 0x1E, 0x02, 0xE0, 0xCF, 0xAA, 0x4C, 0x1B, 0x44, 0x26, 0x00, 0x57, 0x40, 0x96, 0x0A,
- 0x8B, 0x03, 0x05, 0x45, 0x24, 0xD0, 0x0E, 0x30, 0xC6, 0x31, 0x5B, 0x63, 0x0F, 0xBD, 0x77, 0x4B,
- 0xB3, 0x11, 0x74, 0x38, 0xD3, 0xEA, 0x67, 0xFD, 0x97, 0x6C, 0x9F, 0x9E, 0xB9, 0x95, 0x66, 0x9D,
- 0xEB, 0x5A, 0xFC, 0x08, 0x65, 0x8A, 0x01, 0x99, 0x60, 0x35, 0x9B, 0x09, 0x1A, 0xF2, 0xC3, 0x89,
- 0xF1, 0x16, 0xAD, 0xF5, 0x7D, 0x85, 0x2F, 0x46, 0x80, 0x53, 0x59, 0xFA, 0x33, 0x36, 0x9C, 0x21,
- 0x7A, 0x4E, 0xF4, 0x62, 0x39, 0xC4, 0xF0, 0xA8, 0x79, 0x4D, 0x8E, 0xF9, 0x2E, 0xEF, 0xEE, 0xD4,
- 0xDE, 0xFE, 0xC5, 0x8C, 0x4A, 0xFB, 0xA7, 0x43, 0x19, 0xA1, 0xA0, 0x98, 0xA2, 0xBF, 0x90, 0x27,
- 0x87, 0x3B, 0xBE, 0x75, 0x14, 0x41, 0x29, 0x0B, 0xE1, 0xC8, 0xB5, 0x58, 0x51, 0x52, 0x5D, 0x71,
- 0xD7, 0xDA, 0x5C, 0x54, 0x22, 0xCA, 0x48, 0x91, 0xF7, 0xF6, 0x9A, 0xD5, 0x42, 0xD2, 0x69, 0x06
- };
- /* Random unique masks, applied before a given byte passes through the S-box.
- Initialized here as Pi in hexadecimal, with duplicates and 0x00s removed, for the
- sake of removing magic numbers. */
- const byte K[32] = {
- 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
- 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 0x4a,
- 0x40, 0x93, 0x82, 0x22, 0x99, 0xf3, 0x1d, 0xef,
- 0xa9, 0x8e, 0xc4, 0xe6, 0xc8, 0x94, 0x52, 0x1e
- };
- const unsigned char k_len = 32;
- /* Remap function
- This is key to the cipher. The remap function takes in two 32-bit words and outputs a third
- 32-bit word. If any of the bits in the input words change, the entire output word will be different
- and should not have any visible correlation to the changes. */
- void remap(const byte i[WORD_SIZE], const byte w[WORD_SIZE], byte o[WORD_SIZE]) {
- unsigned char k = 0;
- for (unsigned char b = 0; b < WORD_SIZE; b++) {
- o[b] = 0;
- for (unsigned char j = 0; j < WORD_SIZE; j++) {
- unsigned char off = (b + j) % WORD_SIZE;
- byte x = S[ K[ k % k_len] ^ i[off] ] + // Apply mask, pass through S-box
- S[ K[(k + 1) % k_len] ^ w[off] ]; // Sum values produced by word i and word w
- o[b] += (x >> j) | (x << (8 - j)); // Rotate and add to o[b]
- k += 2; // Increment mask index by 2
- }
- }
- }
- void key_schedule(const byte *key, const size_t key_len, byte expanded_key[EXPANDED_KEY_SIZE]) {
- unsigned int rg = 0;
- // Call remap() with each adjacent pair of words in the key
- // Result is appended to expanded key
- for (unsigned int i = 0; i < (key_len - WORD_SIZE); i += WORD_SIZE) {
- remap(&key[i],
- &key[i + WORD_SIZE],
- &expanded_key[i]);
- rg++;
- }
- // Special case, the first and last word of the key must be
- // used as well to avoid compression of the key (key collisions)
- remap(&key[0],
- &key[key_len - WORD_SIZE],
- &expanded_key[rg * WORD_SIZE]);
- rg++;
- // Call remap() with the leftmost word that has not been used yet (indexed with in)
- // as well as the most recently generated word. Result is appended to the expanded
- // key, and this repeats until it reaches the required size
- unsigned int in = 0;
- while (rg < ROUNDS) {
- remap(&expanded_key[in * WORD_SIZE],
- &expanded_key[(rg - 1) * WORD_SIZE],
- &expanded_key[rg * WORD_SIZE]);
- in++;
- rg++;
- }
- }
- void encrypt_block_scheduled(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte scheduled_key[EXPANDED_KEY_SIZE]) {
- memcpy(outblock, inblock, BLOCK_SIZE);
- byte s[4] = { 0 };
- // Loop once per round; a literal interpretation of the diagram
- /*
- // Array of pointers to each word in the block
- // Extra slot is reserved for temporary storage when doing shifts
- byte* words[WORDS_PER_BLOCK + 1];
- for (unsigned int i = 0; i < WORDS_PER_BLOCK; i++) {
- words[i] = &outblock[WORD_SIZE * i];
- }
- for (unsigned int i = 0; i < ROUNDS; i++) {
- // Substitution at every multiple of four except the first rounds
- // (The last round exits the for loop)
- if (i != 0 && i % ROUNDS_BEFORE_SUB == 0) {
- for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
- outblock[j] = S[outblock[j]];
- }
- }
- // Call remap() with first word and roundkey
- remap(words[0], &scheduled_key[i * WORD_SIZE], s);
- // Xor remapping with all other words in block
- for (unsigned int j = 1; j < WORDS_PER_BLOCK; j++) {
- xor_buffer(words[j], s, WORD_SIZE);
- }
- // Shifting pointers left
- // Save first pointer into the extra slot so it will be shifted to the penultimate slot
- words[WORDS_PER_BLOCK] = words[0];
- for (unsigned int j = 0; j < WORDS_PER_BLOCK; j++) {
- words[j] = words[j + 1];
- }
- }
- */
- // Faster version, loop unrolled to (i += 4) to avoid mucking with shifting pointers. Bonus,
- // it's actually more readable this way
- for (unsigned int i = 0; i < ROUNDS; i += 4) {
- // Xor remapping of first word and round key with other blocks
- remap(&outblock[0], &scheduled_key[i * WORD_SIZE], s);
- xor_buffer(&outblock[4], s, WORD_SIZE);
- xor_buffer(&outblock[8], s, WORD_SIZE);
- xor_buffer(&outblock[12], s, WORD_SIZE);
- // Xor remapping of second word and round key with other blocks
- remap(&outblock[4], &scheduled_key[(i + 1) * WORD_SIZE], s);
- xor_buffer(&outblock[0], s, WORD_SIZE);
- xor_buffer(&outblock[8], s, WORD_SIZE);
- xor_buffer(&outblock[12], s, WORD_SIZE);
- // Xor remapping of third word and round key with other blocks
- remap(&outblock[8], &scheduled_key[(i + 2) * WORD_SIZE], s);
- xor_buffer(&outblock[0], s, WORD_SIZE);
- xor_buffer(&outblock[4], s, WORD_SIZE);
- xor_buffer(&outblock[12], s, WORD_SIZE);
- // Xor remapping of fourth word and round key with other blocks
- remap(&outblock[12], &scheduled_key[(i + 3) * WORD_SIZE], s);
- xor_buffer(&outblock[0], s, WORD_SIZE);
- xor_buffer(&outblock[4], s, WORD_SIZE);
- xor_buffer(&outblock[8], s, WORD_SIZE);
- // If this is the last round, we can exit
- if (i >= ROUNDS - 4) {
- return;
- }
- // Otherwise we must perform the substitution
- for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
- outblock[j] = S[outblock[j]];
- }
- }
- }
- void decrypt_block_scheduled(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte scheduled_key[EXPANDED_KEY_SIZE]) {
- memcpy(outblock, inblock, BLOCK_SIZE);
- byte s[4] = { 0 };
- // Loop once per round; a literal interpretation of the diagram
- /*
- // Array of pointers to each word in the block
- // Extra slot is reserved for temporary storage when doing shifts
- byte* words[WORDS_PER_BLOCK + 1];
- for (unsigned int i = 0; i < WORDS_PER_BLOCK; i++) {
- words[i] = &outblock[WORD_SIZE * i];
- }
- for (int i = ROUNDS - 1; i >= 0; i--) {
- // Substitution every 4 rounds, as long as this is not the first loop
- if (i != ROUNDS - 1 && i % ROUNDS_BEFORE_SUB == ROUNDS_BEFORE_SUB - 1) {
- for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
- outblock[j] = inv_S[outblock[j]];
- }
- }
- // Call remap() with last word and roundkey
- remap(words[WORDS_PER_BLOCK - 1], &scheduled_key[i * WORD_SIZE], s);
- // Xor remapping with all other words in block
- for (unsigned int j = 0; j < WORDS_PER_BLOCK - 1; j++) {
- xor_buffer(words[j], s, WORD_SIZE);
- }
- // Shifting pointers right
- for (int j = WORDS_PER_BLOCK - 1; j >= 0; j--) {
- words[j + 1] = words[j];
- }
- // Move last pointer (that was shifted into the extra slot) to the first slot
- words[0] = words[WORDS_PER_BLOCK];
- }
- */
- // Faster version, loop unrolled to (i -= 4) to avoid mucking with shifting pointers
- for (int i = ROUNDS - 1; i >= 0; i -= 4) {
- // Substitution as long as this is not the first loop
- if (i != ROUNDS - 1) {
- for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
- outblock[j] = inv_S[outblock[j]];
- }
- }
- // Xor remapping of fourth word and round key with other blocks
- remap(&outblock[12], &scheduled_key[i * WORD_SIZE], s);
- xor_buffer(&outblock[0], s, WORD_SIZE);
- xor_buffer(&outblock[4], s, WORD_SIZE);
- xor_buffer(&outblock[8], s, WORD_SIZE);
- // Xor remapping of third word and round key with other blocks
- remap(&outblock[8], &scheduled_key[(i - 1) * WORD_SIZE], s);
- xor_buffer(&outblock[0], s, WORD_SIZE);
- xor_buffer(&outblock[4], s, WORD_SIZE);
- xor_buffer(&outblock[12], s, WORD_SIZE);
- // Xor remapping of second word and round key with other blocks
- remap(&outblock[4], &scheduled_key[(i - 2) * WORD_SIZE], s);
- xor_buffer(&outblock[0], s, WORD_SIZE);
- xor_buffer(&outblock[8], s, WORD_SIZE);
- xor_buffer(&outblock[12], s, WORD_SIZE);
- // Xor remapping of first word and round key with other blocks
- remap(&outblock[0], &scheduled_key[(i - 3) * WORD_SIZE], s);
- xor_buffer(&outblock[4], s, WORD_SIZE);
- xor_buffer(&outblock[8], s, WORD_SIZE);
- xor_buffer(&outblock[12], s, WORD_SIZE);
- }
- }
- void decrypt_block(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte* key, const size_t key_len) {
- byte key_s[EXPANDED_KEY_SIZE];
- key_schedule(key, key_len, key_s);
- decrypt_block_scheduled(inblock, outblock, key_s);
- }
- void encrypt_block(const byte inblock[BLOCK_SIZE], byte outblock[BLOCK_SIZE], const byte* key, const size_t key_len) {
- byte key_s[EXPANDED_KEY_SIZE];
- key_schedule(key, key_len, key_s);
- encrypt_block_scheduled(inblock, outblock, key_s);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement