Пример #1
0
/* Low-level operations */
        static unsafe void norx_init(norx_state_t *state, byte *k, byte *n)
        {
            norx_word_t *S = state->S;
            ulong        i;

            for (i = 0; i < 16; ++i)
            {
                S[i] = (norx_word_t)i;
            }

            F(S);
            F(S);

            S[0] = load32(n + 0 * (((32) + 7) / 8));
            S[1] = load32(n + 1 * (((32) + 7) / 8));
            S[2] = load32(n + 2 * (((32) + 7) / 8));
            S[3] = load32(n + 3 * (((32) + 7) / 8));

            S[4] = load32(k + 0 * (((32) + 7) / 8));
            S[5] = load32(k + 1 * (((32) + 7) / 8));
            S[6] = load32(k + 2 * (((32) + 7) / 8));
            S[7] = load32(k + 3 * (((32) + 7) / 8));

            S[12] ^= 32;
            S[13] ^= 4;
            S[14] ^= 1;
            S[15] ^= (32 * 4);

            norx_permute(state);

            S[12] ^= load32(k + 0 * (((32) + 7) / 8));
            S[13] ^= load32(k + 1 * (((32) + 7) / 8));
            S[14] ^= load32(k + 2 * (((32) + 7) / 8));
            S[15] ^= load32(k + 3 * (((32) + 7) / 8));
        }
Пример #2
0
        static unsafe void norx_finalise(norx_state_t *state, byte *tag, byte *k)
        {
            norx_word_t *S         = state->S;
            byte *       lastblock = stackalloc byte[((((32 * 4)) + 7) / 8)];

            S[15] ^= (norx_word_t)tag_t.FINAL_TAG;

            norx_permute(state);

            S[12] ^= load32(k + 0 * (((32) + 7) / 8));
            S[13] ^= load32(k + 1 * (((32) + 7) / 8));
            S[14] ^= load32(k + 2 * (((32) + 7) / 8));
            S[15] ^= load32(k + 3 * (((32) + 7) / 8));

            norx_permute(state);

            S[12] ^= load32(k + 0 * (((32) + 7) / 8));
            S[13] ^= load32(k + 1 * (((32) + 7) / 8));
            S[14] ^= load32(k + 2 * (((32) + 7) / 8));
            S[15] ^= load32(k + 3 * (((32) + 7) / 8));

            store32(lastblock + 0 * (((32) + 7) / 8), S[12]);
            store32(lastblock + 1 * (((32) + 7) / 8), S[13]);
            store32(lastblock + 2 * (((32) + 7) / 8), S[14]);
            store32(lastblock + 3 * (((32) + 7) / 8), S[15]);

            memcpy(tag, lastblock, ((((32 * 4)) + 7) / 8));



            memset(lastblock, 0, ((((32 * 4)) + 7) / 8)); /* burn buffer */
            *state = default(norx_state_t);               /* at this point we can also burn the state */
        }
Пример #3
0
        static unsafe void norx_decrypt_lastblock(norx_state_t *state, byte *@out, byte * @in, ulong inlen)
        {
            norx_word_t *S         = state->S;
            byte *       lastblock = stackalloc byte[(((((32 * 16) - (32 * 4))) + 7) / 8)];
            ulong        i;

            S[15] ^= (norx_word_t)tag_t.PAYLOAD_TAG;
            norx_permute(state);

            for (i = 0; i < (((((32 * 16) - (32 * 4))) + (32 - 1)) / 32); ++i)
            {
                store32(lastblock + i * (((32) + 7) / 8), S[i]);
            }

            memcpy(lastblock, @in, inlen);
            lastblock[inlen] ^= 0x01;
            lastblock[(((((32 * 16) - (32 * 4))) + 7) / 8) - 1] ^= 0x80;

            for (i = 0; i < (((((32 * 16) - (32 * 4))) + (32 - 1)) / 32); ++i)
            {
                norx_word_t c = load32(lastblock + i * (((32) + 7) / 8));
                store32(lastblock + i * (((32) + 7) / 8), S[i] ^ c);
                S[i] = c;
            }

            memcpy(@out, lastblock, inlen);
            memset(lastblock, 0, (((((32 * 16) - (32 * 4))) + 7) / 8));
        }
Пример #4
0
/* The core permutation */
        static unsafe void norx_permute(norx_state_t *state)
        {
            ulong        i;
            norx_word_t *S = state->S;

            for (i = 0; i < 4; ++i)
            {
                F(S);
            }
        }
Пример #5
0
        static unsafe void norx_absorb_block(norx_state_t *state, byte * @in, tag_t tag)
        {
            ulong        i;
            norx_word_t *S = state->S;

            S[15] ^= (norx_word_t)tag;
            norx_permute(state);

            for (i = 0; i < (((((32 * 16) - (32 * 4))) + (32 - 1)) / 32); ++i)
            {
                S[i] ^= load32(@in + i * (((32) + 7) / 8));
            }
        }
Пример #6
0
        static unsafe void norx_encrypt_block(norx_state_t *state, byte *@out, byte * @in)
        {
            ulong        i;
            norx_word_t *S = state->S;

            S[15] ^= (norx_word_t)tag_t.PAYLOAD_TAG;
            norx_permute(state);

            for (i = 0; i < (((((32 * 16) - (32 * 4))) + (32 - 1)) / 32); ++i)
            {
                S[i] ^= load32(@in + i * (((32) + 7) / 8));
                store32(@out + i * (((32) + 7) / 8), S[i]);
            }
        }
Пример #7
0
        static unsafe void norx_decrypt_block(norx_state_t *state, byte *@out, byte * @in)
        {
            ulong        i;
            norx_word_t *S = state->S;

            S[15] ^= (norx_word_t)tag_t.PAYLOAD_TAG;
            norx_permute(state);

            for (i = 0; i < (((((64 * 16) - (64 * 4))) + (64 - 1)) / 64); ++i)
            {
                norx_word_t c = load64(@in + i * (((64) + 7) / 8));
                store64(@out + i * (((64) + 7) / 8), S[i] ^ c);
                S[i] = c;
            }
        }
Пример #8
0
        /* Rotation constants */
/* The nonlinear primitive */


/* The quarter-round */
/* The full round */
        static unsafe void F(norx_word_t *S)
        {
            /* Column step */
            do
            {
                (S[0]) = (((S[0]) ^ (S[4])) ^ (((S[0]) & (S[4])) << 1)); (S[12]) ^= (S[0]); (S[12]) = ((((S[12])) >> (8)) | (((S[12])) << (-(8)))); (S[8]) = (((S[8]) ^ (S[12])) ^ (((S[8]) & (S[12])) << 1)); (S[4]) ^= (S[8]); (S[4]) = ((((S[4])) >> (11)) | (((S[4])) << (-(11)))); (S[0]) = (((S[0]) ^ (S[4])) ^ (((S[0]) & (S[4])) << 1)); (S[12]) ^= (S[0]); (S[12]) = ((((S[12])) >> (16)) | (((S[12])) << (-(16)))); (S[8]) = (((S[8]) ^ (S[12])) ^ (((S[8]) & (S[12])) << 1)); (S[4]) ^= (S[8]); (S[4]) = ((((S[4])) >> (31)) | (((S[4])) << (-(31))));
            } while (false);
            do
            {
                (S[1]) = (((S[1]) ^ (S[5])) ^ (((S[1]) & (S[5])) << 1)); (S[13]) ^= (S[1]); (S[13]) = ((((S[13])) >> (8)) | (((S[13])) << (-(8)))); (S[9]) = (((S[9]) ^ (S[13])) ^ (((S[9]) & (S[13])) << 1)); (S[5]) ^= (S[9]); (S[5]) = ((((S[5])) >> (11)) | (((S[5])) << (-(11)))); (S[1]) = (((S[1]) ^ (S[5])) ^ (((S[1]) & (S[5])) << 1)); (S[13]) ^= (S[1]); (S[13]) = ((((S[13])) >> (16)) | (((S[13])) << (-(16)))); (S[9]) = (((S[9]) ^ (S[13])) ^ (((S[9]) & (S[13])) << 1)); (S[5]) ^= (S[9]); (S[5]) = ((((S[5])) >> (31)) | (((S[5])) << (-(31))));
            } while (false);
            do
            {
                (S[2]) = (((S[2]) ^ (S[6])) ^ (((S[2]) & (S[6])) << 1)); (S[14]) ^= (S[2]); (S[14]) = ((((S[14])) >> (8)) | (((S[14])) << (-(8)))); (S[10]) = (((S[10]) ^ (S[14])) ^ (((S[10]) & (S[14])) << 1)); (S[6]) ^= (S[10]); (S[6]) = ((((S[6])) >> (11)) | (((S[6])) << (-(11)))); (S[2]) = (((S[2]) ^ (S[6])) ^ (((S[2]) & (S[6])) << 1)); (S[14]) ^= (S[2]); (S[14]) = ((((S[14])) >> (16)) | (((S[14])) << (-(16)))); (S[10]) = (((S[10]) ^ (S[14])) ^ (((S[10]) & (S[14])) << 1)); (S[6]) ^= (S[10]); (S[6]) = ((((S[6])) >> (31)) | (((S[6])) << (-(31))));
            } while (false);
            do
            {
                (S[3]) = (((S[3]) ^ (S[7])) ^ (((S[3]) & (S[7])) << 1)); (S[15]) ^= (S[3]); (S[15]) = ((((S[15])) >> (8)) | (((S[15])) << (-(8)))); (S[11]) = (((S[11]) ^ (S[15])) ^ (((S[11]) & (S[15])) << 1)); (S[7]) ^= (S[11]); (S[7]) = ((((S[7])) >> (11)) | (((S[7])) << (-(11)))); (S[3]) = (((S[3]) ^ (S[7])) ^ (((S[3]) & (S[7])) << 1)); (S[15]) ^= (S[3]); (S[15]) = ((((S[15])) >> (16)) | (((S[15])) << (-(16)))); (S[11]) = (((S[11]) ^ (S[15])) ^ (((S[11]) & (S[15])) << 1)); (S[7]) ^= (S[11]); (S[7]) = ((((S[7])) >> (31)) | (((S[7])) << (-(31))));
            } while (false);
            /* Diagonal step */
            do
            {
                (S[0]) = (((S[0]) ^ (S[5])) ^ (((S[0]) & (S[5])) << 1)); (S[15]) ^= (S[0]); (S[15]) = ((((S[15])) >> (8)) | (((S[15])) << (-(8)))); (S[10]) = (((S[10]) ^ (S[15])) ^ (((S[10]) & (S[15])) << 1)); (S[5]) ^= (S[10]); (S[5]) = ((((S[5])) >> (11)) | (((S[5])) << (-(11)))); (S[0]) = (((S[0]) ^ (S[5])) ^ (((S[0]) & (S[5])) << 1)); (S[15]) ^= (S[0]); (S[15]) = ((((S[15])) >> (16)) | (((S[15])) << (-(16)))); (S[10]) = (((S[10]) ^ (S[15])) ^ (((S[10]) & (S[15])) << 1)); (S[5]) ^= (S[10]); (S[5]) = ((((S[5])) >> (31)) | (((S[5])) << (-(31))));
            } while (false);
            do
            {
                (S[1]) = (((S[1]) ^ (S[6])) ^ (((S[1]) & (S[6])) << 1)); (S[12]) ^= (S[1]); (S[12]) = ((((S[12])) >> (8)) | (((S[12])) << (-(8)))); (S[11]) = (((S[11]) ^ (S[12])) ^ (((S[11]) & (S[12])) << 1)); (S[6]) ^= (S[11]); (S[6]) = ((((S[6])) >> (11)) | (((S[6])) << (-(11)))); (S[1]) = (((S[1]) ^ (S[6])) ^ (((S[1]) & (S[6])) << 1)); (S[12]) ^= (S[1]); (S[12]) = ((((S[12])) >> (16)) | (((S[12])) << (-(16)))); (S[11]) = (((S[11]) ^ (S[12])) ^ (((S[11]) & (S[12])) << 1)); (S[6]) ^= (S[11]); (S[6]) = ((((S[6])) >> (31)) | (((S[6])) << (-(31))));
            } while (false);
            do
            {
                (S[2]) = (((S[2]) ^ (S[7])) ^ (((S[2]) & (S[7])) << 1)); (S[13]) ^= (S[2]); (S[13]) = ((((S[13])) >> (8)) | (((S[13])) << (-(8)))); (S[8]) = (((S[8]) ^ (S[13])) ^ (((S[8]) & (S[13])) << 1)); (S[7]) ^= (S[8]); (S[7]) = ((((S[7])) >> (11)) | (((S[7])) << (-(11)))); (S[2]) = (((S[2]) ^ (S[7])) ^ (((S[2]) & (S[7])) << 1)); (S[13]) ^= (S[2]); (S[13]) = ((((S[13])) >> (16)) | (((S[13])) << (-(16)))); (S[8]) = (((S[8]) ^ (S[13])) ^ (((S[8]) & (S[13])) << 1)); (S[7]) ^= (S[8]); (S[7]) = ((((S[7])) >> (31)) | (((S[7])) << (-(31))));
            } while (false);
            do
            {
                (S[3]) = (((S[3]) ^ (S[4])) ^ (((S[3]) & (S[4])) << 1)); (S[14]) ^= (S[3]); (S[14]) = ((((S[14])) >> (8)) | (((S[14])) << (-(8)))); (S[9]) = (((S[9]) ^ (S[14])) ^ (((S[9]) & (S[14])) << 1)); (S[4]) ^= (S[9]); (S[4]) = ((((S[4])) >> (11)) | (((S[4])) << (-(11)))); (S[3]) = (((S[3]) ^ (S[4])) ^ (((S[3]) & (S[4])) << 1)); (S[14]) ^= (S[3]); (S[14]) = ((((S[14])) >> (16)) | (((S[14])) << (-(16)))); (S[9]) = (((S[9]) ^ (S[14])) ^ (((S[9]) & (S[14])) << 1)); (S[4]) ^= (S[9]); (S[4]) = ((((S[4])) >> (31)) | (((S[4])) << (-(31))));
            } while (false);
        }