cipher

=programming =cryptography =algorithms

 

 

This should be a cipher version of my hash algorithm.

 

 

int length = 64;
uint64_t[length] input;
uint64_t[length] old_input;
uint64_t[length] temp_array;
uint8_t[length] keys;

uint64_t[] sbox(uint64_t[] input)
    {
    int i = 0;
    for (int n=0; n<length; n=n+4)
        {
        temp_array[i] = input[n + 1];
        i++;
        temp_array[i] = input[n] ^ input[n + 3];
        i++;
        }
    for (int n=0; n<length; n=n+4)
        {
        temp_array[i] = input[n + 1] ^ input[n + 2];
        i++;
        }
    for (int n=0; n<length; n=n+4)
        {
        temp_array[i] = input[n] ^ input[n + 1] ^ input[n + 2];
        i++;
        }
    tmp = temp_array;
    temp_array = input;
    input = tmp;
    }

 

int block_length = length/4;

int two_blocks = block_length*2;

 

uint64_t[] anti_sbox(uint64_t[] input)
    {

for (int i=0; i<block_length; i++)

{

int half_index = i + two_blocks;

uint64_t zero = input[half_index];

uint64_t one = input[half_index + block_length];

uint64_t two = input[i];

uint64_t three = input[i+1];

 

int offset = i*4;

temp_array[offset] = zero^one;

temp_array[1 + offset] = two;

temp_array[2 + offset] = zero^two;

temp_array[3 + offset] = zero^one^three;

}

tmp = temp_array;
temp_array = input;
input = tmp;

}


uint64_t[] sbox_set(uint64_t[] input)
    {
    for (int i=0; i<6; i++)
        {sbox(input);}
    for (int i=0; i<length; i++)
        {
        input[i] = rotate_right(input[i], i)
        }
    for (int i=0; i<6; i++)
        {sbox(input);}
    }


uint64_t[] anti_sbox_set(uint64_t[] input)
    {
    for (int i=0; i<6; i++)
        {anti_sbox(input);}
    for (int i=0; i<length; i++)
        {
        input[i] = rotate_left(input[i], i)
        }
    for (int i=0; i<6; i++)
        {anti_sbox(input);}
    }

uint64_t[] pbox(uint64_t[] input, uint64_t[] modifier)
    {
    for (int i=0; i<length; i++)
        {
        uint64_t mod_element = modifier[i];
        input[i] = rotate_right(input[i], mod_element & 63);
        keys[i] = mod_element >> 6;
        }
   
    for (int count=0; count<3; count++)
        {
        int start_index = 0;
        int end_index = length-1;
       
        for (int i=0; i<length; i++)
            {
            uint8_t key = keys[i] & 3;
            uint64_t element = input[i];
            if (key == 0)
                {
                temp_array[start_index] = element;
                start_index++;
                }
            if (key == 3)
                {
                temp_array[end_index] = element;
                end_index--;
                }
            }
        for (int i=0; i<length; i++)
            {
            uint8_t key = keys[i];
            keys[i] = key >> 2;
            key = key & 3;
            uint64_t element = input[i];
            if (key == 1)
                {
                temp_array[start_index] = element;
                start_index++;
                }
            if (key == 2)
                {
                temp_array[end_index] = element;
                end_index--;
                }
            }
        tmp = temp_array;
        temp_array = input;
        input = tmp;
        }
    }

 

uint64_t[] anti_pbox(uint64_t[] input, uint64_t[] modifier)
    { 
    for (int count=0; count<3; count++)
        {
        int start_index = 0;
        int end_index = length-1;

        int shift = 10 - 2*count;

        
        for (int i=0; i<length; i++)
            {
            uint8_t key = (modifier[i] >> shift) & 3;
            if (key == 0)
                {
                temp_array[i] = input[start_index];
                start_index++;
                }
            if (key == 3)
                {
                temp_array[i] = input[end_index];
                end_index--;
                }
            }
        for (int i=0; i<length; i++)
            {
            uint8_t key = (modifier[i] >> shift) & 3;
            if (key == 1)
                {
                temp_array[i] = input[start_index];
                start_index++;
                }
            if (key == 2)
                {
                temp_array[i] = input[end_index];
                end_index--;
                }
            }
        tmp = temp_array;
        temp_array = input;
        input = tmp;
        }

 

    for (int i=0; i<length; i++)
        {
        input[i] = rotate_left(input[i], modifier[i] & 63);
        }
    }

 

 

int rounds = 6;

uint64_t[length] cipher_key_temp;

 

uint64_t[] cipher_key = (repeat the key for 512 bytes);

sbox_set(cipher_key);

 

uint64_t[] encrypt_block(uint64_t[] input, uint64_t[] cipher_key)

{

memcpy(cipher_key, cipher_key_temp, sizeof(cipher_key_temp));

for (int j=0; j<length; j++)

{

cipher_key_temp[j] = rotate_right(cipher_key_temp[j], 13*rounds);

}

for (int i=0; i<rounds; i++)

{

for (int j=0; j<length; j++)

{

cipher_key_temp[j] = rotate_left(cipher_key_temp[j], 13);

input[j] = input[j] ^ cipher_key_temp[j];

}

sbox_set(input);

pbox(input, cipher_key_temp);

}

}

 

uint64_t[] decrypt_block(uint64_t[] input, uint64_t[] cipher_key)

{

memcpy(cipher_key, cipher_key_temp, sizeof(cipher_key_temp));

for (int i=0; i<rounds; i++)

{

anti_pbox(input, cipher_key_temp);

anti_sbox_set(input);

for (int j=0; j<length; j++)

{

input[j] = input[j] ^ cipher_key_temp[j];

cipher_key_temp[j] = rotate_right(cipher_key_temp[j], 13);

}

}

}

 

 

 




back to index