encrypt.c 12.7 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 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 296 297 298
/******************************************************************************
* 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
*
* @date     May 2020
******************************************************************************/
#include "skinny128.h"
#include "skinnyaead.h"
#include <string.h>
#include <stdio.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, u32* rtk1,
                    u32* rtk2_3, u64 mlen, const u8* ad, u64 adlen) {
    u64 lfsr = 1;
    u8 feedback;
    u8 tmp[2*BLOCKBYTES];
    memset(tmp, 0x00, 2*BLOCKBYTES);
    memset(auth, 0x00, BLOCKBYTES);
    SET_DOMAIN(tmp, 0x02);
    while (adlen >= 2*BLOCKBYTES) {
        LE_STR_64(tmp, lfsr);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x02);
        tkschedule_perm_tk1(rtk1, tmp, tmp+BLOCKBYTES);
        skinny128_384(tmp, tmp+BLOCKBYTES, ad, ad+BLOCKBYTES, rtk1, rtk2_3);
        xor_block(auth, tmp);
        xor_block(auth, tmp + BLOCKBYTES);
        adlen -= 2*BLOCKBYTES;
        ad += 2*BLOCKBYTES;
        UPDATE_LFSR(lfsr);
    }
    if (adlen > BLOCKBYTES) {                       // pad and process 2 blocs
        LE_STR_64(tmp, lfsr);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x03);         // domain for padding ad
        tkschedule_perm_tk1(rtk1, tmp, tmp + BLOCKBYTES);
        adlen -= BLOCKBYTES;
        memset(tmp, 0x00, BLOCKBYTES);
        memcpy(tmp, ad + BLOCKBYTES, adlen);
        tmp[adlen] ^= 0x80;                         // padding
        skinny128_384(tmp + BLOCKBYTES, tmp, ad, tmp, rtk1, rtk2_3);
        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
            tkschedule_perm_tk1(rtk1, tmp, tag);
            skinny128_384(auth, c, ad, c, rtk1, rtk2_3); 
        } else {                        // if tag has  been calculated yet
            tkschedule_perm_tk1(rtk1, tmp, tmp);    // process last ad block
            skinny128_384(auth, auth, ad, ad, rtk1, rtk2_3);
        }
    } 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
            tkschedule_perm_tk1(rtk1, tmp, tag);    // compute the tag
            skinny128_384(auth, c, tmp + BLOCKBYTES, c, rtk1, rtk2_3); 
        } else {                        // if tag has been calculated yet
            tkschedule_perm_tk1(rtk1, tmp,  tmp);   // process last ad block
            skinny128_384(auth, auth, tmp + BLOCKBYTES, tmp + BLOCKBYTES, rtk1, rtk2_3);
        }
    }
}

/******************************************************************************
* 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) {
    u8 feedback;
    u64 i,lfsr = 1;
    u32 rtk1[8*16];
    u32 rtk2_3[8*SKINNY128_384_ROUNDS];
    u8 tmp[2*BLOCKBYTES], tag[BLOCKBYTES], auth[BLOCKBYTES], sum[BLOCKBYTES];
    (void)nsec;

    // ----------------- Initialization -----------------
    *clen = mlen + TAGBYTES;
    tkschedule_lfsr_2(rtk2_3, npub, npub, SKINNY128_384_ROUNDS);
    tkschedule_lfsr_3(rtk2_3, k, k, SKINNY128_384_ROUNDS);
    tkschedule_perm(rtk2_3);
    memset(tmp, 0x00, 2*BLOCKBYTES);
    memset(tag, 0x00, BLOCKBYTES);
    memset(auth, 0x00, BLOCKBYTES);
    memset(sum, 0x00, BLOCKBYTES);
    // ----------------- Initialization -----------------

    // ----------------- Process the plaintext -----------------
    while (mlen >= 2*BLOCKBYTES) {          // process 2 blocks in //
        LE_STR_64(tmp, lfsr);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        tkschedule_perm_tk1(rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384(c, c + BLOCKBYTES, m, m + BLOCKBYTES, rtk1, rtk2_3);
        xor_block(sum, m);                 // sum for tag computation
        xor_block(sum, 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);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x01); // domain for padding m
        tkschedule_perm_tk1(rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384(c, auth, m, auth, rtk1, rtk2_3);
        xor_block(sum, m);
        for(i = 0; i < mlen - BLOCKBYTES; i++) {
            c[BLOCKBYTES + i] = auth[i] ^ m[BLOCKBYTES + i];
            sum[i] ^= m[BLOCKBYTES + i]; 
        }
        sum[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);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x04); // domain for tag computation
        xor_block(sum, m);                  // sum for tag computation
        tkschedule_perm_tk1(rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384(c, sum, m, sum, rtk1, rtk2_3);
        c += BLOCKBYTES;
    } else if (mlen > 0) {                  // last block is partial
        LE_STR_64(tmp, lfsr);
        SET_DOMAIN(tmp, 0x01);              // domain for padding
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        SET_DOMAIN(tmp + BLOCKBYTES, 0x05); // domain for tag computation
        for(i = 0; i < mlen; i++)           // sum for tag computation
            sum[i] ^= m[i];                 // sum for tag computation
        sum[i] ^= 0x80;                     // padding
        tkschedule_perm_tk1(rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384(auth, sum, auth, sum, rtk1, rtk2_3);
        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) {
            tkschedule_perm_tk1(rtk1, tag, tag);
            skinny128_384(sum, sum, sum, sum,  rtk1, rtk2_3); // compute the tag
        }
    }
    // ----------------- Process the plaintext -----------------

    // ----------------- Process the associated data -----------------
    skinny_aead_m1_auth(auth, sum, tag, rtk1, rtk2_3, mlen, ad, adlen);
    xor_block(sum, auth);
    memcpy(c, sum, TAGBYTES);
    // ----------------- 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) {
    u8 feedback;
    u64 i,lfsr = 1;
    u32 rtk1[8*16];
    u32 rtk2_3[8*SKINNY128_384_ROUNDS];
    u8 tmp[2*BLOCKBYTES];
    u8 sum[BLOCKBYTES], tag[BLOCKBYTES], auth[BLOCKBYTES];
    (void)nsec;

    if (clen < TAGBYTES)
        return -1;

    // ----------------- Initialization -----------------
    clen -= TAGBYTES;
    *mlen = clen;
    tkschedule_lfsr_2(rtk2_3, npub, npub, SKINNY128_384_ROUNDS);
    tkschedule_lfsr_3(rtk2_3, k, k, SKINNY128_384_ROUNDS);
    tkschedule_perm(rtk2_3);
    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);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp + BLOCKBYTES, lfsr);
        tkschedule_perm_tk1(rtk1, tmp, tmp + BLOCKBYTES);
        skinny128_384_inv(m, m + BLOCKBYTES, c, c + BLOCKBYTES, rtk1, rtk2_3);
        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);
        tkschedule_perm_tk1(rtk1, tmp, tmp);
        skinny128_384_inv(m, m, c, c, rtk1, rtk2_3);
        xor_block(sum, m);
        UPDATE_LFSR(lfsr);
        LE_STR_64(tmp, lfsr);
        SET_DOMAIN(tmp, 0x01);              // domain for padding m
        tkschedule_perm_tk1(rtk1, tmp, tmp);
        skinny128_384(auth, auth, auth, auth, rtk1, rtk2_3);
        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
        c += clen;
        clen = 0;
        UPDATE_LFSR(lfsr);
    } else if (clen == BLOCKBYTES) {        // last block is full
        LE_STR_64(tmp, lfsr);
        tkschedule_perm_tk1(rtk1, tmp, tmp);
        skinny128_384_inv(m, m, c, c, rtk1, rtk2_3);
        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);
        SET_DOMAIN(tmp, 0x01);              // domain for padding
        tkschedule_perm_tk1(rtk1, tmp, tmp);
        skinny128_384(auth, auth, auth, auth, rtk1, rtk2_3);
        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);
        c += clen;
        clen = 0;
    }
    if (clen == 0) {                    // if tag has *NOT* been calculated yet
        LE_STR_64(tag, lfsr);
        if((adlen % 32) == 0 || (adlen % 32) > BLOCKBYTES) {
            tkschedule_perm_tk1(rtk1, tag, tag); //if AD can be processed in //
            skinny128_384(sum, sum, sum, sum, rtk1, rtk2_3); // compute the tag
        }
    }

    // ----------------- Process the associated data -----------------
    skinny_aead_m1_auth(auth, sum, tag, rtk1, rtk2_3, 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 -----------------
}