Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdlib.h>
- #include <inttypes.h>
- #include <stdio.h>
- typedef struct andIdea128_key_schedule
- {
- uint8_t num_rounds;
- uint16_t *subkeys;
- } anIdea128_key_schedule;
- static inline uint16_t mult(uint16_t a, uint16_t b)
- {
- return (uint16_t)(((uint32_t)a+1)*((uint32_t)b+1)%65537 - 1);
- }
- static inline void anIdea128_round(uint16_t block[4], const uint16_t key[4])
- {
- block[0] = mult(block[0], key[0]);
- block[1] ^= key[1];
- block[2] ^= key[2];
- block[3] = mult(block[3], key[3]);
- block[1] = mult(block[0], block[1]);
- block[2] = mult(block[3], block[2]);
- block[2] += block[1];
- block[1] += block[2];
- block[0] ^= block[2];
- block[3] ^= block[1];
- }
- anIdea128_key_schedule anIdea128_init(const uint16_t *key, size_t key_length,
- uint8_t num_rounds)
- {
- uint16_t i;
- uint16_t pi[4] = {0x243F, 0x6A88, 0x85A3, 0x08D3};
- uint16_t num_subkeys = num_rounds*4+16;
- uint16_t temp_length = num_subkeys+7;
- uint16_t *temp_subkeys;
- anIdea128_key_schedule sched;
- if (key_length % 4 != 0 || num_rounds < 1)
- return (anIdea128_key_schedule) {0, NULL};
- temp_subkeys = (uint16_t *)malloc(sizeof(uint16_t)*temp_length);
- sched.subkeys = (uint16_t *)malloc(sizeof(uint16_t)*num_subkeys);
- sched.num_rounds = num_rounds;
- for (i=0; i<temp_length; i++)
- {
- temp_subkeys[i] = i;
- }
- for (i=0; i<key_length; i+=4)
- {
- anIdea128_round(temp_subkeys, key+i);
- }
- for (i=1; i<(temp_length-3); i++)
- {
- temp_subkeys[i+3] ^= temp_subkeys[i-1];
- temp_subkeys[i] ^= key[i*4%key_length];
- temp_subkeys[i+1] ^= key[i*4%key_length+1];
- temp_subkeys[i+2] ^= key[i*4%key_length+2];
- temp_subkeys[i+3] ^= key[i*4%key_length+3];
- anIdea128_round(temp_subkeys+i, pi);
- }
- for (i=4; i<(temp_length-3); i++)
- {
- sched.subkeys[i-4] = temp_subkeys[i];
- }
- free(temp_subkeys);
- return sched;
- }
- void anIdea128_dest(anIdea128_key_schedule *sched)
- {
- free(sched->subkeys);
- sched->subkeys = NULL;
- sched->num_rounds = 0;
- }
- void anIdea128_encrypt(uint16_t block[8], anIdea128_key_schedule sched)
- {
- int16_t i;
- uint16_t temp[4];
- for (i=0; i<8; i++)
- {
- block[i] ^= sched.subkeys[i];
- }
- for (i=0; i<sched.num_rounds; i++)
- {
- temp[0] = block[i*4%8];
- temp[1] = block[i*4%8+1];
- temp[2] = block[i*4%8+2];
- temp[3] = block[i*4%8+3];
- anIdea128_round(temp, sched.subkeys+(i+2)*4);
- block[(i+1)*4%8] ^= temp[0];
- block[(i+1)*4%8+1] ^= temp[1];
- block[(i+1)*4%8+2] ^= temp[2];
- block[(i+1)*4%8+3] ^= temp[3];
- }
- for (i=0; i<8; i++)
- {
- block[i] ^= sched.subkeys[(sched.num_rounds+2)*4+i];
- }
- }
- void anIdea128_decrypt(uint16_t block[8], anIdea128_key_schedule sched)
- {
- int16_t i;
- uint16_t temp[4];
- for (i=0; i<8; i++)
- {
- block[i] ^= sched.subkeys[(sched.num_rounds+2)*4+i];
- }
- for (i=sched.num_rounds-1; i>=0; i--)
- {
- temp[0] = block[i*4%8];
- temp[1] = block[i*4%8+1];
- temp[2] = block[i*4%8+2];
- temp[3] = block[i*4%8+3];
- anIdea128_round(temp, sched.subkeys+(i+2)*4);
- block[(i+1)*4%8] ^= temp[0];
- block[(i+1)*4%8+1] ^= temp[1];
- block[(i+1)*4%8+2] ^= temp[2];
- block[(i+1)*4%8+3] ^= temp[3];
- }
- for (i=0; i<8; i++)
- {
- block[i] ^= sched.subkeys[i];
- }
- }
- int main()
- {
- int i;
- uint16_t block[8] = {0};
- uint16_t key[16] = {0};
- anIdea128_key_schedule sched = anIdea128_init(key, sizeof(key)/sizeof(uint16_t), 16);
- for (i=0; i<sched.num_rounds*4+16; i++)
- printf("%04"PRIx16"\n", sched.subkeys[i]);
- anIdea128_encrypt(block, sched);
- for (i=0; i<8; i++)
- {
- printf("%04"PRIx16, block[i]);
- }
- anIdea128_decrypt(block, sched);
- printf("\n");
- for (i=0; i<8; i++)
- {
- printf("%04"PRIx16, block[i]);
- }
- printf("\n");
- anIdea128_dest(&sched);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement