encrypt.c 13.1 KB
Newer Older
Alexandre Adomnicai committed
1 2 3 4 5 6 7 8 9 10
/******************************************************************************
* Constant-time implementation of SKINNY-AEAD-M1 (v1.1).
*
* Two blocks are treated in parallel with SKINNY-128-384 whenever possible.
*
* For more details, see the paper at: https://
*
* @author   Alexandre Adomnicai, Nanyang Technological University,
*           alexandre.adomnicai@ntu.edu.sg
*
Alexandre Adomnicai committed
11
* @date     June 2020
Alexandre Adomnicai committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
******************************************************************************/
#include "skinnyaead.h"
#include <string.h>

/******************************************************************************
* x ^= y where x, y are 128-bit blocks (16 bytes array).
******************************************************************************/
static void xor_block(u8 * x, const u8* y) {
    for(int i = 0; i < BLOCKBYTES; i++)
        x[i] ^= y[i];
}

/******************************************************************************
* Process the associated data. Common to SKINNY-AEAD-M1 encrypt and decrypt
* functions.
******************************************************************************/
static void skinny_aead_m1_auth(u8* auth, u8* c, u8* tag, tweakey* tk,
                    u64 mlen, const u8* ad, u64 adlen) {
    u64 lfsr = 1;
    u8 feedback;
    u8 tmp[2*BLOCKBYTES];
    memset(tmp, 0x00, 2*BLOCKBYTES);
    SET_DOMAIN(tmp, 0x02);
Alexandre Adomnicai committed
35 36
    SET_DOMAIN(tmp + BLOCKBYTES, 0x02);
    memset(auth, 0x00, BLOCKBYTES);
Alexandre Adomnicai committed
37 38 39 40 41 42 43 44 45 46 47
    while (adlen >= 2*BLOCKBYTES) {
        LE_STR_64(tmp, lfsr);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        precompute_rtk1(tk->rtk1, tmp, tmp+BLOCKBYTES);
        skinny128_384_encrypt(tmp, tmp+BLOCKBYTES, ad, ad+BLOCKBYTES, *tk);
        xor_block(auth, tmp);
        xor_block(auth, tmp + BLOCKBYTES);
        adlen -= 2*BLOCKBYTES;
        ad += 2*BLOCKBYTES;
        UPDATE_LFSR(lfsr);
Alexandre Adomnicai committed
48 49 50
        memset(tmp, 0x00, 2*BLOCKBYTES);    // to save 32 bytes of RAM
        SET_DOMAIN(tmp, 0x02);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x02);
Alexandre Adomnicai committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    }
    if (adlen > BLOCKBYTES) {               // pad and process 2 blocs in //
        LE_STR_64(tmp, lfsr);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x03); // domain for padding ad
        precompute_rtk1(tk->rtk1, tmp, tmp + BLOCKBYTES);
        adlen -= BLOCKBYTES;
        memset(tmp, 0x00, BLOCKBYTES);
        memcpy(tmp, ad + BLOCKBYTES, adlen);
        tmp[adlen] ^= 0x80;                 // padding
        skinny128_384_encrypt(tmp + BLOCKBYTES, tmp, ad, tmp, *tk);
        xor_block(auth, tmp);
        xor_block(auth, tmp + BLOCKBYTES);
    } else if (adlen == BLOCKBYTES) {
        LE_STR_64(tmp, lfsr);
        if (mlen == 0) {    // if tag has *NOT* been calculated yet
            precompute_rtk1(tk->rtk1, tmp, tag);    // compute the tag
Alexandre Adomnicai committed
69
            skinny128_384_encrypt(tmp, c, ad, c, *tk); 
Alexandre Adomnicai committed
70 71
        } else {            // if tag has  been calculated yet
            precompute_rtk1(tk->rtk1, tmp, tmp);    // process last ad block
Alexandre Adomnicai committed
72
            skinny128_384_encrypt(tmp, tmp, ad, ad, *tk);
Alexandre Adomnicai committed
73
        }
Alexandre Adomnicai committed
74
        xor_block(auth, tmp);
Alexandre Adomnicai committed
75 76 77 78 79 80 81 82
    } else if (adlen > 0) {
        LE_STR_64(tmp, lfsr);
        SET_DOMAIN(tmp, 0x03);                      // domain for padding ad
        memset(tmp + BLOCKBYTES, 0x00, BLOCKBYTES); // padding
        memcpy(tmp + BLOCKBYTES, ad, adlen);        // padding
        tmp[BLOCKBYTES + adlen] ^= 0x80;            // padding
        if (mlen == 0) {    // if tag has *NOT* been calculated yet
            precompute_rtk1(tk->rtk1, tmp, tag);    // compute the tag
Alexandre Adomnicai committed
83
            skinny128_384_encrypt(tmp, c, tmp + BLOCKBYTES, c, *tk); 
Alexandre Adomnicai committed
84 85
        } else {            // if tag has been calculated yet
            precompute_rtk1(tk->rtk1, tmp,  tmp);   // process last ad block
Alexandre Adomnicai committed
86
            skinny128_384_encrypt(tmp, tmp, tmp + BLOCKBYTES, tmp + BLOCKBYTES, *tk);
Alexandre Adomnicai committed
87
        }
Alexandre Adomnicai committed
88
        xor_block(auth, tmp);
Alexandre Adomnicai committed
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    }
}

/******************************************************************************
* Encryption and authentication using SKINNY-AEAD-M1
******************************************************************************/
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) {
    u64 i,lfsr = 1;
    u8 feedback;
    tweakey tk;
    u8 tmp[2*BLOCKBYTES], tag[BLOCKBYTES], auth[BLOCKBYTES];
    (void)nsec;

    // ----------------- Initialization -----------------
    *clen = mlen + TAGBYTES;
    precompute_rtk2_3(tk.rtk2_3, npub, k, SKINNY128_384_ROUNDS);
    memset(tmp, 0x00, 2*BLOCKBYTES);
    memset(tag, 0x00, BLOCKBYTES);
    memset(auth, 0x00, BLOCKBYTES);
    memset(c + mlen, 0x00, BLOCKBYTES);
    // ----------------- Initialization -----------------

    // ----------------- Process the plaintext -----------------
    while (mlen >= 2*BLOCKBYTES) {          // process 2 blocks in //
        LE_STR_64(tmp, lfsr);               // lfsr for 1st block
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);  // lfsr for 2nd block
        precompute_rtk1(tk.rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384_encrypt(c, c + BLOCKBYTES, m, m + BLOCKBYTES, tk);
        xor_block(c + mlen, m);                 // sum for tag computation
        xor_block(c + mlen, m + BLOCKBYTES);    // sum for tag computation
        mlen -= 2*BLOCKBYTES;
        c += 2*BLOCKBYTES;
        m += 2*BLOCKBYTES;
        UPDATE_LFSR(lfsr);
    }
    SET_DOMAIN(tag, 0x04);                  // domain for tag computation
    if (mlen > BLOCKBYTES) {                // pad and process 2 blocs in //
        LE_STR_64(tmp, lfsr);               // lfsr for 1st block
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);  // lfsr for 2nd block
        SET_DOMAIN(tmp + BLOCKBYTES, 0x01);       // domain for padding m
        precompute_rtk1(tk.rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384_encrypt(c, auth, m, auth, tk);
        xor_block(c + mlen, m);
        for(i = 0; i < mlen - BLOCKBYTES; i++) {
            c[BLOCKBYTES + i] = auth[i] ^ m[BLOCKBYTES + i];
            c[mlen + i] ^= m[BLOCKBYTES + i]; 
        }
        c[mlen + i] ^= 0x80;                    // padding
        SET_DOMAIN(tag, 0x05);                  // domain for tag computation
        m += mlen;
        c += mlen;
        mlen = 0;
        UPDATE_LFSR(lfsr);
    } else if (mlen == BLOCKBYTES) {            // last block is full
        LE_STR_64(tmp, lfsr);                   // lfsr for last full block
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);      // lfsr for tag computation
        SET_DOMAIN(tmp + BLOCKBYTES, 0x04);     // domain for tag computation
        xor_block(c + mlen, m);                 // sum for tag computation
        precompute_rtk1(tk.rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384_encrypt(c, c + mlen, m, c + mlen, tk);
        c += BLOCKBYTES;
    } else if (mlen > 0) {                      // last block is partial
        LE_STR_64(tmp, lfsr);               // lfsr for last block
        SET_DOMAIN(tmp, 0x01);              // domain for padding
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);       // lfsr for tag computation
        SET_DOMAIN(tmp + BLOCKBYTES, 0x05);      // domain for tag computation
        for(i = 0; i < mlen; i++)  // sum for tag computation
            c[mlen + i] ^= m[i];                // sum for tag computation
        c[mlen + i] ^= 0x80;                    // padding
        precompute_rtk1(tk.rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384_encrypt(auth, c + mlen, auth, c + mlen, tk);
        for(i = 0; i < mlen; i++)
            c[i] = auth[i] ^ m[i];               // encrypted padded block
        c += mlen;
    }
    if (mlen == 0) {    // if tag has *NOT* been calculated yet 
        LE_STR_64(tag, lfsr);               // lfsr for tag computation                                     
        if((adlen % 32) == 0 || (adlen % 32) > BLOCKBYTES) {    //if all AD can be processed in //
            precompute_rtk1(tk.rtk1, tag, tag);
            skinny128_384_encrypt(c, c, c, c, tk); // compute the tag
        }
    }
    // ----------------- Process the plaintext -----------------

    // ----------------- Process the associated data -----------------
    skinny_aead_m1_auth(auth, c, tag, &tk, mlen, ad, adlen);
    xor_block(c, auth);
    // ----------------- Process the associated data -----------------

    return 0;
}


/******************************************************************************
* Decryption and authentication using SKINNY-AEAD-M1
******************************************************************************/
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) {
    u64 i,lfsr = 1;
    u8 feedback;
    tweakey tk;
    u8 tmp[2*BLOCKBYTES];
    u8 sum[BLOCKBYTES], tag[BLOCKBYTES], auth[BLOCKBYTES];
    (void)nsec;

    if (clen < TAGBYTES)
        return -1;

    // ----------------- Initialization -----------------
    clen -= TAGBYTES;
    *mlen = clen;
    precompute_rtk2_3(tk.rtk2_3, npub, k, SKINNY128_384_ROUNDS);
    memset(tmp, 0x00, 2*BLOCKBYTES);
    memset(tag, 0x00, BLOCKBYTES);
    memset(auth, 0x00, BLOCKBYTES);
    memset(sum, 0x00, BLOCKBYTES);
    // ----------------- Initialization -----------------

    // ----------------- Process the plaintext -----------------
    while (clen >= 2*BLOCKBYTES) {          // process 2 blocks in //
        LE_STR_64(tmp, lfsr);               // lfsr for 1st block
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);  // lfsr for 2nd block
        precompute_rtk1(tk.rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384_decrypt(m, m + BLOCKBYTES, c, c + BLOCKBYTES, tk);
        xor_block(sum, m);                 // sum for tag computation
        xor_block(sum, m + BLOCKBYTES);    // sum for tag computation
        clen -= 2*BLOCKBYTES;
        c += 2*BLOCKBYTES;
        m += 2*BLOCKBYTES;
        UPDATE_LFSR(lfsr);
    }
    SET_DOMAIN(tag, 0x04);                  // domain for tag computation
    if (clen > BLOCKBYTES) {                // pad and process 2 blocs in //
        LE_STR_64(tmp, lfsr);               // lfsr for 1st block
        precompute_rtk1(tk.rtk1, tmp, tmp);
        skinny128_384_decrypt(m, m, c, c, tk);
        xor_block(sum, m);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp, lfsr);               // lfsr for 2nd block
        SET_DOMAIN(tmp, 0x01);              // domain for padding m
        precompute_rtk1(tk.rtk1, tmp, tmp);
        skinny128_384_encrypt(auth, auth, auth, auth, tk);
        for(i = 0; i < clen - BLOCKBYTES; i++) {
            m[BLOCKBYTES + i] = auth[i] ^ c[BLOCKBYTES + i];
            sum[i] ^= m[BLOCKBYTES + i]; 
        }
        sum[i] ^= 0x80;                     // padding
        SET_DOMAIN(tag, 0x05);              // domain for tag computation
        m += clen;
        c += clen;
        clen = 0;
        UPDATE_LFSR(lfsr);
    } else if (clen == BLOCKBYTES) {        // last block is full
        LE_STR_64(tmp, lfsr);               // lfsr for last full block
        precompute_rtk1(tk.rtk1, tmp, tmp);
        skinny128_384_decrypt(m, m, c, c, tk);
        xor_block(sum, m);                  // sum for tag computation
        SET_DOMAIN(tag, 0x04);              // domain for tag computation
        UPDATE_LFSR(lfsr);
        c += BLOCKBYTES;
        clen = 0;
    } else if (clen > 0) {                  // last block is partial
        LE_STR_64(tmp, lfsr);               // lfsr for last block
        SET_DOMAIN(tmp, 0x01);              // domain for padding
        precompute_rtk1(tk.rtk1, tmp, tmp);
        skinny128_384_encrypt(auth, auth, auth, auth, tk);
        for(i = 0; i < clen; i++) {
            m[i] = auth[i] ^ c[i];          // encrypted padded block
            sum[i] ^= m[i];                 // sum for tag computation
        }
        sum[i] ^= 0x80;                     // padding
        SET_DOMAIN(tag, 0x05);              // domain for tag computation
        UPDATE_LFSR(lfsr);
        m += clen;
        c += clen;
        clen = 0;
    }
    if (clen == 0) {                // if tag has *NOT* been calculated yet
        LE_STR_64(tag, lfsr);       // lfsr for tag computation                        
        if((adlen % 32) == 0 || (adlen % 32) > BLOCKBYTES) {
            precompute_rtk1(tk.rtk1, tag, tag); //if AD can be processed in //
            skinny128_384_encrypt(sum, sum, sum, sum, tk); // compute the tag
        }
    }

    // ----------------- Process the associated data -----------------
    skinny_aead_m1_auth(auth, sum, tag, &tk, clen, ad, adlen);
    xor_block(sum, auth);
    feedback = 0;
    for(i = 0; i < TAGBYTES; i++)
        feedback |= sum[i] ^ c[i];  // constant-time tag verification
    return feedback;
    // ----------------- Process the associated data -----------------
Alexandre Adomnicai committed
296
}