Midori128错误的密文

时间:2018-07-06 11:42:31

标签: c encryption block-cipher midori

我有一个项目,在其中必须为Arduino Uno R3实现各种安全协议,例如Present,Misty,Prince等。目前,我正在尝试实现Midori128。我设法使Midiori64正常工作,但对于Midori128,加密不正确,而解密正常。

这是当前的代码,请记住我是从GitHub上获取此代码,然后对其进行更改以使其尽可能高效地在Arduino上运行;但是就像我说的那样,加密是错误的,我也不知道为什么。

#include <stdio.h>
#include <stdint.h>


static uint16_t s_box[16] = { 0x1,0x0,0x5,0x3,0xe,0x2,0xf,0x7,0xd,0xa,0x9,0xb,0xc,0x8,0x4,0x6 };
static uint16_t const_key[19][16] = { { 0,0,0,1,0,1,0,1,1,0,1,1,0,0,1,1 },{ 0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0 },
                                     { 1,0,1,0,0,1,0,0,0,0,1,1,0,1,0,1 },{ 0,1,1,0,0,0,1,0,0,0,0,1,0,0,1,1 },
                                     { 0,0,0,1,0,0,0,0,0,1,0,0,1,1,1,1 },{ 1,1,0,1,0,0,0,1,0,1,1,1,0,0,0,0 },
                                     { 0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,0 },{ 0,0,0,0,1,0,1,1,1,1,0,0,1,1,0,0 },
                                     { 1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1 },{ 0,1,0,0,0,0,0,0,1,0,1,1,1,0,0,0 },
                                     { 0,1,1,1,0,0,0,1,1,0,0,1,0,1,1,1 },{ 0,0,1,0,0,0,1,0,1,0,0,0,1,1,1,0 },
                                     { 0,1,0,1,0,0,0,1,0,0,1,1,0,0,0,0 },{ 1,1,1,1,1,0,0,0,1,1,0,0,1,0,1,0 },
                                     { 1,1,0,1,1,1,1,1,1,0,0,1,0,0,0,0 },{ 0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1 },
                                     { 0,0,0,1,1,1,0,0,0,0,1,0,0,1,0,0 },{ 0,0,1,0,0,0,1,1,1,0,1,1,0,1,0,0 },
                                     {0,1,1,0,0,0,1,0,1,0,0,0,1,0,1,0} };

uint16_t bit_permutation_0(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x08) << 4), temp ^= ((x & 0x40)), temp ^= ((x & 0x02) << 4), temp ^= (x & 0x10),
        temp ^= ((x & 0x80) >> 4), temp ^= ((x & 0x04)), temp ^= ((x & 0x20) >> 4), temp ^= (x & 0x01);
    return temp;
}

uint16_t bit_permutation_1(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x40) << 1), temp ^= ((x & 0x02) << 5), temp ^= ((x & 0x01) << 5), temp ^= ((x & 0x80) >> 3),
        temp ^= ((x & 0x04) << 1), temp ^= ((x & 0x20) >> 3), temp ^= ((x & 0x10) >> 3), temp ^= ((x & 0x08) >> 3);
    return temp;
}

uint16_t inv_bit_permutation_1(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x10) << 3), temp ^= ((x & 0x80) >> 1), temp ^= ((x & 0x04) << 3), temp ^= ((x & 0x02) << 3),
        temp ^= ((x & 0x01) << 3), temp ^= ((x & 0x08) >> 1), temp ^= ((x & 0x40) >> 5), temp ^= ((x & 0x20) >> 5);
    return temp;
}

uint16_t bit_permutation_2(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x20) << 2), temp ^= ((x & 0x10) << 2), temp ^= ((x & 0x08) << 2), temp ^= ((x & 0x40) >> 2),
        temp ^= ((x & 0x02) << 2), temp ^= ((x & 0x01) << 2), temp ^= ((x & 0x80) >> 6), temp ^= ((x & 0x04) >> 2);
    return temp;
}

uint16_t inv_bit_permutation_2(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x02) << 6), temp ^= ((x & 0x10) << 2), temp ^= ((x & 0x80) >> 2), temp ^= ((x & 0x40) >> 2),
        temp ^= ((x & 0x20) >> 2), temp ^= ((x & 0x01) << 2), temp ^= ((x & 0x08) >> 2), temp ^= ((x & 0x04) >> 2);
    return temp;
}

uint16_t bit_permutation_3(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x01) << 7), temp ^= ((x & 0x08) << 3), temp ^= ((x & 0x40) >> 1), temp ^= ((x & 0x20) >> 1),
        temp ^= ((x & 0x10) >> 1), temp ^= ((x & 0x80) >> 5), temp ^= ((x & 0x04) >> 1), temp ^= ((x & 0x02) >> 1);
    return temp;
}

uint16_t inv_bit_permutation_3(uint16_t x)
{
    uint16_t temp = 0x00;
    temp ^= ((x & 0x04) << 5), temp ^= ((x & 0x20) << 1), temp ^= ((x & 0x10) << 1), temp ^= ((x & 0x08) << 1),
        temp ^= ((x & 0x40) >> 3), temp ^= ((x & 0x02) << 1), temp ^= ((x & 0x01) << 1), temp ^= ((x & 0x80) >> 7);
    return temp;
}

uint16_t Midori128_S_Box(int r, uint16_t x)
{
    uint16_t y;
    if (r % 4 == 0)
    {
        y = bit_permutation_0(x);
        y = (s_box[(y & 0xf0) >> 4] << 4) ^ (s_box[(y & 0x0f)]);
        y = bit_permutation_0(y);
    }
    if (r % 4 == 1)
    {
        y = bit_permutation_1(x);
        y = (s_box[(y & 0xf0) >> 4] << 4) ^ s_box[(y & 0x0f)];
        y = inv_bit_permutation_1(y);
    }
    if (r % 4 == 2)
    {
        y = bit_permutation_2(x);
        y = (s_box[(y & 0xf0) >> 4] << 4) ^ s_box[(y & 0x0f)];
        y = inv_bit_permutation_2(y);
    }
    else
    {
        y = bit_permutation_3(x);
        y = (s_box[(y & 0xf0) >> 4] << 4) ^ s_box[(y & 0x0f)];
        y = inv_bit_permutation_3(y);
    }
    return y;
}

void SubCell(int r,uint16_t *state)
{
    int i;
    for (i = 0; i <= 15; i++)
    {
        state[i] = Midori128_S_Box(r%4,state[i]);
    }
}

void ShuffleCell(uint16_t *state)
{
    int i;
    uint16_t temp[16];
    temp[0] = state[0], temp[1] = state[10], temp[2] = state[5], temp[3] = state[15],
        temp[4] = state[14], temp[5] = state[4], temp[6] = state[11], temp[7] = state[1],
        temp[8] = state[9], temp[9] = state[3], temp[10] = state[12], temp[11] = state[6],
        temp[12] = state[7], temp[13] = state[13], temp[14] = state[2], temp[15] = state[8];
    for (i = 0; i <= 15; i++)
    {
        state[i] = temp[i];
    }
}

void Inv_ShuffleCell(uint16_t *state)
{
    int i;
    uint16_t temp[16];
    temp[0] = state[0], temp[1] = state[7], temp[2] = state[14], temp[3] = state[9],
        temp[4] = state[5], temp[5] = state[2], temp[6] = state[11], temp[7] = state[12],
        temp[8] = state[15], temp[9] = state[8], temp[10] = state[1], temp[11] = state[6],
        temp[12] = state[10], temp[13] = state[13], temp[14] = state[4], temp[15] = state[3];
    for (i = 0; i <= 15; i++)
    {
        state[i] = temp[i];
    }
}


void MixColumn(uint16_t *state)
{
    int i;
    uint16_t temp[16];
    for (i = 0; i <= 3; i++)
    {
        temp[4 * i + 0] = state[4 * i + 1] ^ state[4 * i + 2] ^ state[4 * i + 3];
        temp[4 * i + 1] = state[4 * i + 0] ^ state[4 * i + 2] ^ state[4 * i + 3];
        temp[4 * i + 2] = state[4 * i + 0] ^ state[4 * i + 1] ^ state[4 * i + 3];
        temp[4 * i + 3] = state[4 * i + 0] ^ state[4 * i + 1] ^ state[4 * i + 2];
    }
    for (i = 0; i <= 15; i++)
    {
        state[i] = temp[i];
    }
}


void rth_Round_Encrypt_KeyAdd(int r, uint16_t *state, uint16_t *K)
{
    int i;  
    for (i = 0; i <= 15; i++)
    {
        state[i] = state[i] ^ K[i] ^ const_key[r][i];
    }

}

void rth_Round_Decrypt_KeyAdd(int r, uint16_t *state, uint16_t *K)
{
    int i;
    uint16_t Kr[16];
    for (i = 0; i <= 15; i++)
    {
        Kr[i] = K[i] ^ const_key[r][i];
    }
    MixColumn(Kr);
    Inv_ShuffleCell(Kr);
    for (i = 0; i <= 15; i++)
    {
        state[i] = state[i] ^ Kr[i];
    }
}

void Encrypt(int r, uint16_t *plaintext, uint16_t *K, uint16_t *ciphertext)
{
    int i;
    for (i = 0; i <= 15; i++)
    {
        ciphertext[i] = plaintext[i];
    }
    for (i = 0; i <= 15; i++)
    {
        ciphertext[i] = ciphertext[i] ^ K[i];
    }
    for (i = 0; i <= (r - 2); i++)
    {
        SubCell(i, ciphertext);
        ShuffleCell(ciphertext);
        MixColumn(ciphertext);
        rth_Round_Encrypt_KeyAdd(i, ciphertext, K);
    }
    SubCell(i, ciphertext);
    for (i = 0; i <= 15; i++)
    {
        ciphertext[i] = ciphertext[i] ^ K[i];
    }
}
void Decrypt(int r, uint16_t *ciphertext, uint16_t *K,uint16_t *plaintext)
{
    int i;
    for (i = 0; i <= 15; i++)
    {
        plaintext[i] = ciphertext[i];
    }
    for (i = 0; i <= 15; i++)
    {
        plaintext[i] = plaintext[i] ^ K[i];
    }
    for (i = (r-2); i >=0;i--)
    {
        SubCell(i + 1, plaintext);
        MixColumn(plaintext);
        Inv_ShuffleCell(plaintext);
        rth_Round_Decrypt_KeyAdd(i, plaintext, K);

    }
    SubCell(i, plaintext);
    for (i = 0; i <= 15; i++)
    {
        plaintext[i] = plaintext[i] ^ K[i];
    }
}
void setup (){
Serial.begin(9600);
printf.begin();
}

void loop ()
{
    int i,j1,j2,j3;
    uint16_t plaintext[16] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
    uint16_t Key[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    uint16_t plaintext_temp[16];
    uint16_t ciphertext_temp[16];
    for (j1 = 0; j1 <= 0xff; j1++)
    { 
        plaintext[0] = j1;
        for (j2 = 0; j2 <= 0xff; j2++)
        {
            plaintext[1] = j2;
            for (j3 = 0; j3 <= 0xff; j3++)
            {
                plaintext[2] = j3;
                /*printf("Before Encryption: ");
                for (i = 0; i < 16; i++)
                {
                    printf("%x ", plaintext[i]);
                }
                printf("\n");*/
                Encrypt(20, plaintext, Key, ciphertext_temp);
                printf("After Encryption: ");
                for (i = 0; i < 16; i++)
                {
                    printf("%x ", ciphertext_temp[i]);
                        }
                        printf("\n");
                        /*Decrypt(20, ciphertext_temp, Key, plaintext_temp);
                        printf("After Decryption: ");
                        for (i = 0; i < 16; i++)
                        {
                            printf("%x ", plaintext_temp[i]);
                        }
                        printf("\n");*/
                        printf("\n");
                    }
                }
            }
        }

我对原始论文进行了无数次审查,并且对方法非常了解,但是我无法查明其未输出正确密文的确切原因,即c055cbb95996d14902b60574d5e728d6。任何帮助或提示将不胜感激。谢谢。

仅供参考,如果有帮助,则此刻为密文输出:4d643d33500079d920dbdd42e71d3e1d。我暗中怀疑排列是罪魁祸首,仍然不确定。我认为这是因为Midori64使用了类似的舍入函数(Subcell,Mix等),并且运行良好。唯一的新介绍是排列和轮数。

0 个答案:

没有答案