Exemple #1
0
static NORX_INLINE void norx_encrypt_lastblock(norx_state_t state, uint8_t *out, const uint8_t * in, size_t inlen)
{
    uint8_t lastblock[BYTES(RATE)];
    norx_pad(lastblock, in, inlen);
    norx_encrypt_block(state, lastblock, lastblock);
    memcpy(out, lastblock, inlen);
}
Exemple #2
0
        /* Merge */
        memset(state, 0, sizeof(norx_state_t));
        for (i = 0; i < NORX_P; ++i) {
            norx_merge(state, lane[i]);
            burn(lane[i], 0, sizeof(norx_state_t));
        }

        #if defined(NORX_DEBUG)
        printf("Decryption finalised\n");
        norx_debug(state, NULL, 0, NULL, 0);
        #endif
    }
}

#elif NORX_P == 0 /* Unlimited parallelism */
void norx_encrypt_data(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if (inlen > 0)
    {
        size_t lane = 0;
        norx_state_t sum;
        norx_state_t state2;

        memset(sum, 0, sizeof(norx_state_t));

        while (inlen >= BYTES(NORX_R))
        {
            /* branch */
            memcpy(state2, state, sizeof(norx_state_t));
            norx_branch(state2, lane++);
            /* encrypt */
            norx_encrypt_block(state2, out, in);

            #if defined(NORX_DEBUG)
            printf("Encrypt block (lane: %lu)\n", lane - 1);
            norx_debug(state2, in, BYTES(NORX_R), out, BYTES(NORX_R));
            #endif

            /* merge */
            norx_merge(sum, state2);

            inlen -= BYTES(NORX_R);
            in    += BYTES(NORX_R);
            out   += BYTES(NORX_R);
        }

        /* last block, 0 <= inlen < BYTES(NORX_R) */

        /* branch */
        memcpy(state2, state, sizeof(norx_state_t));
        norx_branch(state2, lane++);

        /* encrypt */
        norx_encrypt_lastblock(state2, out, in, inlen);

        #if defined(NORX_DEBUG)
        printf("Encrypt lastblock (lane: %lu)\n", lane - 1);
        norx_debug(state2, in, inlen, out, inlen);
        #endif

        /* merge */
        norx_merge(sum, state2);

        memcpy(state, sum, sizeof(norx_state_t));
        burn(state2, 0, sizeof(norx_state_t));
        burn(sum, 0, sizeof(norx_state_t));

        #if defined(NORX_DEBUG)
        printf("Encryption finalised\n");
        norx_debug(state, NULL, 0, NULL, 0);
        #endif
    }
}
Exemple #3
0
void norx_encrypt_msg(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if(inlen > 0)
    {
        norx_state_t lane[NORX_D];
        uint8_t emptyblock[BYTES(RATE)];
        size_t i, j;

        /* Initialize states + branch */
        for(i = 0; i < NORX_D; ++i)
        {
            memcpy(lane[i], state, sizeof lane[i]);
            norx_branch(lane[i], i);
        }

        /* parallel payload processing */
        for(i = 0; inlen >= BYTES(RATE); ++i)
        {
            norx_encrypt_block(lane[i%NORX_D], out, in);
            inlen -= BYTES(RATE);
            out   += BYTES(RATE);
            in    += BYTES(RATE);
        }
        norx_encrypt_lastblock(lane[i++%NORX_D], out, in, inlen);

        /* Pad remaining blocks */
        norx_pad(emptyblock, emptyblock, 0);
        for(j = 0; j < (NORX_D-1); ++j, ++i)
        {
            norx_absorb(lane[i%NORX_D], emptyblock, PAYLOAD_TAG);
        }

        /* Merge */
        memset(state, 0, sizeof(norx_state_t));
        for(i = 0; i < NORX_D; ++i)
        {
#if defined(NORX_DEBUG)
            printf("End of payload processing (lane %lu) \n", i);
            norx_print_state(lane[i]);
#endif
            norx_merge(state, lane[i]);
            burn(lane[i], 0, sizeof(norx_state_t));
        }

#if defined(NORX_DEBUG)
        printf("End of merging:\n");
        norx_print_state(state);
#endif
    }
}
Exemple #4
0
void norx_encrypt_msg(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if(inlen > 0)
    {
        while (inlen >= BYTES(RATE))
        {
            norx_encrypt_block(state, out, in);
            inlen -= BYTES(RATE);
            in    += BYTES(RATE);
            out   += BYTES(RATE);
        }

        norx_encrypt_lastblock(state, out, in, inlen);
    }
}
Exemple #5
0
/* state = state xor state1 */
static NORX_INLINE void norx_merge(norx_state_t state, norx_state_t state1)
{
    size_t i;
    norx_word_t * S = state->S;
    norx_word_t * S1 = state1->S;

    S1[15] ^= MERGE_TAG;
    norx_permute(state1);

    for (i = 0; i < 16; ++i) {
        S[i] ^= S1[i];
    }
}
#endif

#if NORX_P == 1 /* Sequential encryption/decryption */
void norx_encrypt_data(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if (inlen > 0)
    {
        while (inlen >= BYTES(NORX_R))
        {
            norx_encrypt_block(state, out, in);
            #if defined(NORX_DEBUG)
            printf("Encrypt block\n");
            norx_debug(state, in, BYTES(NORX_R), out, BYTES(NORX_R));
            #endif
            inlen -= BYTES(NORX_R);
            in    += BYTES(NORX_R);
            out   += BYTES(NORX_R);
        }
        norx_encrypt_lastblock(state, out, in, inlen);
        #if defined(NORX_DEBUG)
        printf("Encrypt lastblock\n");
        norx_debug(state, in, inlen, out, inlen);
        #endif
    }
}

void norx_decrypt_data(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if (inlen > 0)
    {
        while (inlen >= BYTES(NORX_R))
        {
            norx_decrypt_block(state, out, in);
            #if defined(NORX_DEBUG)
            printf("Decrypt block\n");
            norx_debug(state, in, BYTES(NORX_R), out, BYTES(NORX_R));
            #endif
            inlen -= BYTES(NORX_R);
            in    += BYTES(NORX_R);
            out   += BYTES(NORX_R);
        }
        norx_decrypt_lastblock(state, out, in, inlen);
        #if defined(NORX_DEBUG)
        printf("Decrypt lastblock\n");
        norx_debug(state, in, inlen, out, inlen);
        #endif
    }
}

#elif NORX_P > 1 /* Parallel encryption/decryption */
void norx_encrypt_data(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if (inlen > 0)
    {
        size_t i;
        norx_state_t lane[NORX_P];

        /* Initialize states + branch */
        for (i = 0; i < NORX_P; ++i) {
            memcpy(lane[i], state, sizeof lane[i]);
            norx_branch(lane[i], i);
        }

        /* Parallel payload processing */
        for (i = 0; inlen >= BYTES(NORX_R); ++i) {
            norx_encrypt_block(lane[i%NORX_P], out, in);
            #if defined(NORX_DEBUG)
            printf("Encrypt block (lane: %lu)\n", i%NORX_P);
            norx_debug(lane[i%NORX_P], in, BYTES(NORX_R), out, BYTES(NORX_R));
            #endif
            inlen -= BYTES(NORX_R);
            out   += BYTES(NORX_R);
            in    += BYTES(NORX_R);
        }
        norx_encrypt_lastblock(lane[i%NORX_P], out, in, inlen);
        #if defined(NORX_DEBUG)
        printf("Encrypt lastblock (lane: %lu)\n", i%NORX_P);
        norx_debug(lane[i%NORX_P], in, inlen, out, inlen);
        #endif

        /* Merge */
        memset(state, 0, sizeof(norx_state_t));
        for (i = 0; i < NORX_P; ++i) {
            norx_merge(state, lane[i]);
            burn(lane[i], 0, sizeof(norx_state_t));
        }

        #if defined(NORX_DEBUG)
        printf("Encryption finalised\n");
        norx_debug(state, NULL, 0, NULL, 0);
        #endif
    }
}
Exemple #6
0
void norx_encrypt_msg(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if(inlen > 0)
    {
        uint8_t emptyblock[BYTES(RATE)];
        norx_state_t sum;
        norx_state_t state2;
        uint64_t lane = 0;

        norx_pad(emptyblock, emptyblock, 0);
        memset(sum, 0, sizeof(norx_state_t));

        while (inlen >= BYTES(RATE))
        {
            /* branch */
            memcpy(state2, state, sizeof(norx_state_t));
            norx_branch(state2, lane++);
            /* encrypt */
            norx_encrypt_block(state2, out, in);
            /* empty padding */
            norx_absorb(state2, emptyblock, PAYLOAD_TAG);
            /* merge */
            norx_merge(sum, state2);

            inlen -= BYTES(RATE);
            in    += BYTES(RATE);
            out   += BYTES(RATE);
        }
        /* last block, 0 < inlen < BYTES(RATE) */
        if(inlen > 0)
        {
            /* branch */
            memcpy(state2, state, sizeof(norx_state_t));
            norx_branch(state2, lane++);
            /* encrypt */
            norx_encrypt_lastblock(state2, out, in, inlen);
            /* merge */
            norx_merge(sum, state2);
        }
        memcpy(state, sum, sizeof(norx_state_t));
    }
}
Exemple #7
0
/* state = state xor state1 */
static NORX_INLINE void norx_merge(norx_state_t state, norx_state_t state1)
{
    size_t i;
    norx_word_t * S = state->S;
    norx_word_t * S1 = state1->S;

    S1[15] ^= MERGE_TAG;
    norx_permute(state1);

    for (i = 0; i < 16; ++i) {
        S[i] ^= S1[i];
    }
}
#endif

#if NORX_P == 1 /* Sequential encryption/decryption */
void norx_encrypt_data(norx_state_t state, unsigned char *out, const unsigned char * in, size_t inlen)
{
    if (inlen > 0)
    {
        while (inlen >= BYTES(NORX_R))
        {
            norx_encrypt_block(state, out, in);
            #if defined(NORX_DEBUG)
            printf("Encrypt block\n");
            norx_debug(state, in, BYTES(NORX_R), out, BYTES(NORX_R));
            #endif
            inlen -= BYTES(NORX_R);
            in    += BYTES(NORX_R);
            out   += BYTES(NORX_R);
        }
        norx_encrypt_lastblock(state, out, in, inlen);
        #if defined(NORX_DEBUG)
        printf("Encrypt lastblock\n");
        norx_debug(state, in, inlen, out, inlen);
        #endif
    }
}