encrypt.c 9.68 KB
Newer Older
Alexandre Adomnicai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
#include "skinny128.h"
#include "tk_schedule.h"
#include "romulus.h"
#include <string.h>
#include <stdio.h>

//Encryption and authentication using Romulus-N1
int crypto_aead_encrypt
    (unsigned char *c, unsigned long long *clen,
     const unsigned char *m, unsigned long long mlen,
     const unsigned char *ad, unsigned long long adlen,
     const unsigned char *nsec,
     const unsigned char *npub,
     const unsigned char *k) {

    int i;
    u32 tmp;
    skinny_128_384_tks tks;
    u8 state[BLOCKBYTES], pad[BLOCKBYTES];
    (void)nsec;

    // ----------------- Initialization -----------------
    *clen = mlen + TAGBYTES;
    memset(tks.tk1, 0x00, KEYBYTES);
    memset(state, 0x00, BLOCKBYTES);
    tks.tk1[0] = 0x01;                          //56-bit LFSR counter
    // ----------------- Initialization -----------------

    // ----------------- Process the associated data -----------------
    //Handle the special case of no associated data
    if (adlen == 0) {
        UPDATE_CTR(tks.tk1);
        SET_DOMAIN(tks, 0x1A);
        precompute_rtk2_3(tks.rtk2_3, npub, k);
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3); 
    } else {
        // Process all double blocks except the last
        SET_DOMAIN(tks, 0x08);
        while (adlen > 2*BLOCKBYTES) {
            UPDATE_CTR(tks.tk1);
            XOR_BLOCK(state, state, ad);
            precompute_rtk2_3(tks.rtk2_3, ad + BLOCKBYTES, k);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3); 
            UPDATE_CTR(tks.tk1);
            ad += 2*BLOCKBYTES;
            adlen -= 2*BLOCKBYTES;
        }
        //Pad and process the left-over blocks 
        UPDATE_CTR(tks.tk1);
        if (adlen == 2*BLOCKBYTES) {
            // Left-over complete double block
            XOR_BLOCK(state, state, ad);
            precompute_rtk2_3(tks.rtk2_3, ad + BLOCKBYTES, k);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3); 
            UPDATE_CTR(tks.tk1);
            SET_DOMAIN(tks, 0x18);
        } else if (adlen > BLOCKBYTES) {
            //  Left-over partial double block
            adlen -= BLOCKBYTES;
            XOR_BLOCK(state, state, ad);
            memcpy(pad, ad + BLOCKBYTES, adlen);
            memset(pad + adlen, 0x00, 15 - adlen);
            pad[15] = adlen;
            precompute_rtk2_3(tks.rtk2_3, pad, k);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
            UPDATE_CTR(tks.tk1);
            SET_DOMAIN(tks, 0x1A);
        } else if (adlen == BLOCKBYTES) {
            //  Left-over complete single block 
            XOR_BLOCK(state, state, ad);
            SET_DOMAIN(tks, 0x18);
        } else {
            // Left-over partial single block
            for(i =0; i < (int)adlen; i++)
                state[i] ^= ad[i];
            state[15] ^= adlen;
            SET_DOMAIN(tks, 0x1A);
        }
        precompute_rtk2_3(tks.rtk2_3, npub, k);
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
    }
    // ----------------- Process the associated data -----------------

    // ----------------- Process the plaintext -----------------
    memset(tks.tk1, 0, KEYBYTES);
    tks.tk1[0] = 0x01;          //init the 56-bit LFSR counter
    if (mlen == 0) {
        UPDATE_CTR(tks.tk1);
        SET_DOMAIN(tks, 0x15);
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
    } else {
        //process all blocks except the last
        SET_DOMAIN(tks, 0x04);
        while (mlen > BLOCKBYTES) {
            RHO(state,c,m);
            UPDATE_CTR(tks.tk1);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
            c += BLOCKBYTES;
            m += BLOCKBYTES;
            mlen -= BLOCKBYTES;
        }
        //pad and process the last block
        UPDATE_CTR(tks.tk1);
        if (mlen < BLOCKBYTES) {
            for(i = 0; i < (int)mlen; i++) {
                tmp = m[i];         //use of tmp variable just in case 'c = m'
                c[i] = m[i] ^ (state[i] >> 1) ^ (state[i] & 0x80) ^ (state[i] << 7);
                state[i] ^= (u8)tmp;
            }
            state[15] ^= (u8)mlen; //padding
            SET_DOMAIN(tks, 0x15);
        } else {
            RHO(state,c,m);
            SET_DOMAIN(tks, 0x14);
        }
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
        c += mlen;
    }
    // ----------------- Process the plaintext -----------------

    // ----------------- Generate the tag -----------------
    G(state, state);
    memcpy(c, state, TAGBYTES);
    // ----------------- Generate the tag -----------------

    return 0;
}


//Decryption and tag verification using Romulus-N1
int crypto_aead_decrypt
    (unsigned char *m, unsigned long long *mlen,
     unsigned char *nsec,
     const unsigned char *c, unsigned long long clen,
     const unsigned char *ad, unsigned long long adlen,
     const unsigned char *npub,
     const unsigned char *k) {

    int i;
    u32 tmp;
    skinny_128_384_tks tks;
    u8 state[BLOCKBYTES], pad[BLOCKBYTES];
    (void)nsec;

    if (clen < TAGBYTES)
        return -1;

    // ----------------- Initialization -----------------
    *mlen = clen - TAGBYTES;
    memset(tks.tk1, 0x00, KEYBYTES);
    memset(state, 0x00, BLOCKBYTES);
    tks.tk1[0] = 0x01;                          //56-bit LFSR counter
    // ----------------- Initialization -----------------

    // ----------------- Process the associated data -----------------
    //Handle the special case of no associated data
    if (adlen == 0) {
        UPDATE_CTR(tks.tk1);
        SET_DOMAIN(tks, 0x1A);
        precompute_rtk2_3(tks.rtk2_3, npub, k);
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
    } else {
        // Process all double blocks except the last
        SET_DOMAIN(tks, 0x08);
        while (adlen > 2*BLOCKBYTES) {
            UPDATE_CTR(tks.tk1);
            XOR_BLOCK(state, state, ad);
            precompute_rtk2_3(tks.rtk2_3, ad + BLOCKBYTES, k);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3); 
            UPDATE_CTR(tks.tk1);
            ad += 2*BLOCKBYTES;
            adlen -= 2*BLOCKBYTES;
        }
        //Pad and process the left-over blocks 
        UPDATE_CTR(tks.tk1);
        if (adlen == 2*BLOCKBYTES) {
            // Left-over complete double block
            XOR_BLOCK(state, state, ad);
            precompute_rtk2_3(tks.rtk2_3, ad + BLOCKBYTES, k);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3); 
            UPDATE_CTR(tks.tk1);
            SET_DOMAIN(tks, 0x18);
        } else if (adlen > BLOCKBYTES) {
            //  Left-over partial double block
            adlen -= BLOCKBYTES;
            XOR_BLOCK(state, state, ad);
            memcpy(pad, ad + BLOCKBYTES, adlen);
            memset(pad + adlen, 0x00, 15 - adlen);
            pad[15] = adlen;
            precompute_rtk2_3(tks.rtk2_3, pad, k);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);  
            UPDATE_CTR(tks.tk1);
            SET_DOMAIN(tks, 0x1A);
        } else if (adlen == BLOCKBYTES) {
            //  Left-over complete single block 
            XOR_BLOCK(state, state, ad);
            SET_DOMAIN(tks, 0x18);
        } else {
            // Left-over partial single block
            for(i =0; i < (int)adlen; i++)
                state[i] ^= ad[i];
            state[15] ^= adlen;
            SET_DOMAIN(tks, 0x1A);
        }
        precompute_rtk2_3(tks.rtk2_3, npub, k);
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
    }
    // ----------------- Process the associated data -----------------

    // ----------------- Process the ciphertext -----------------
    clen -= TAGBYTES;
    memset(tks.tk1, 0, KEYBYTES);
    tks.tk1[0] = 0x01;          //init the 56-bit LFSR counter
    if (clen == 0) {
        UPDATE_CTR(tks.tk1);
        SET_DOMAIN(tks, 0x15);
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
    } else {
        //process all blocks except the last
        SET_DOMAIN(tks, 0x04);
        while (clen > BLOCKBYTES) {
            RHO_INV(state,c,m);
            UPDATE_CTR(tks.tk1);
            precompute_rtk1(tks.rtk1, tks.tk1);
            skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
            c += BLOCKBYTES;
            m += BLOCKBYTES;
            clen -= BLOCKBYTES;
        }
        //pad and process the last block
        UPDATE_CTR(tks.tk1);
        if (clen < BLOCKBYTES) {
            for(i = 0; i < (int)clen; i++) {
                m[i] = c[i] ^ (state[i] >> 1) ^ (state[i] & 0x80) ^ (state[i] << 7);
                state[i] ^= m[i];
            }
            state[15] ^= (u8)clen; //padding
            SET_DOMAIN(tks, 0x15);
        } else {
            RHO_INV(state,c,m);
            SET_DOMAIN(tks, 0x14);
        }
        precompute_rtk1(tks.rtk1, tks.tk1);
        skinny128_384_plus(state, state, tks.rtk1, tks.rtk2_3);
    }
    // ----------------- Process the plaintext -----------------

    // ----------------- Generate and check the tag -----------------
    G(state,state);
    tmp = 0;
    for(i = 0; i < TAGBYTES; i++)
        tmp |= state[i] ^ c[clen+i];   //constant-time tag comparison
    // ----------------- Generate and check the tag -----------------

    return tmp;
}