/* Encryption key setup */ static void aes_key_setup_enc(__m128i rk[], const u8* cipherKey, int keylen) { switch (keylen) { case 16: { /* 128 bit key setup */ rk[0] = _mm_loadu_si128((const __m128i*) cipherKey); rk[1] = KEYEXP128(rk[0], 0x01); rk[2] = KEYEXP128(rk[1], 0x02); rk[3] = KEYEXP128(rk[2], 0x04); rk[4] = KEYEXP128(rk[3], 0x08); rk[5] = KEYEXP128(rk[4], 0x10); rk[6] = KEYEXP128(rk[5], 0x20); rk[7] = KEYEXP128(rk[6], 0x40); rk[8] = KEYEXP128(rk[7], 0x80); rk[9] = KEYEXP128(rk[8], 0x1B); rk[10] = KEYEXP128(rk[9], 0x36); break; } case 24: { /* 192 bit key setup */ __m128i temp[2]; rk[0] = _mm_loadu_si128((const __m128i*) cipherKey); rk[1] = _mm_loadu_si128((const __m128i*) (cipherKey+16)); temp[0] = KEYEXP192(rk[0], rk[1], 0x01); temp[1] = KEYEXP192_2(temp[0], rk[1]); rk[1] = (__m128i)_mm_shuffle_pd((__m128d)rk[1], (__m128d)temp[0], 0); rk[2] = (__m128i)_mm_shuffle_pd((__m128d)temp[0], (__m128d)temp[1], 1); rk[3] = KEYEXP192(temp[0], temp[1], 0x02); rk[4] = KEYEXP192_2(rk[3], temp[1]); temp[0] = KEYEXP192(rk[3], rk[4], 0x04); temp[1] = KEYEXP192_2(temp[0], rk[4]); rk[4] = (__m128i)_mm_shuffle_pd((__m128d)rk[4], (__m128d)temp[0], 0); rk[5] = (__m128i)_mm_shuffle_pd((__m128d)temp[0], (__m128d)temp[1], 1); rk[6] = KEYEXP192(temp[0], temp[1], 0x08); rk[7] = KEYEXP192_2(rk[6], temp[1]); temp[0] = KEYEXP192(rk[6], rk[7], 0x10); temp[1] = KEYEXP192_2(temp[0], rk[7]); rk[7] = (__m128i)_mm_shuffle_pd((__m128d)rk[7], (__m128d)temp[0], 0); rk[8] = (__m128i)_mm_shuffle_pd((__m128d)temp[0], (__m128d)temp[1], 1); rk[9] = KEYEXP192(temp[0], temp[1], 0x20); rk[10] = KEYEXP192_2(rk[9], temp[1]); temp[0] = KEYEXP192(rk[9], rk[10], 0x40); temp[1] = KEYEXP192_2(temp[0], rk[10]); rk[10] = (__m128i)_mm_shuffle_pd((__m128d)rk[10], (__m128d) temp[0], 0); rk[11] = (__m128i)_mm_shuffle_pd((__m128d)temp[0],(__m128d) temp[1], 1); rk[12] = KEYEXP192(temp[0], temp[1], 0x80); break; } case 32: { /* 256 bit key setup */ rk[0] = _mm_loadu_si128((const __m128i*) cipherKey); rk[1] = _mm_loadu_si128((const __m128i*) (cipherKey+16)); rk[2] = KEYEXP256(rk[0], rk[1], 0x01); rk[3] = KEYEXP256_2(rk[1], rk[2]); rk[4] = KEYEXP256(rk[2], rk[3], 0x02); rk[5] = KEYEXP256_2(rk[3], rk[4]); rk[6] = KEYEXP256(rk[4], rk[5], 0x04); rk[7] = KEYEXP256_2(rk[5], rk[6]); rk[8] = KEYEXP256(rk[6], rk[7], 0x08); rk[9] = KEYEXP256_2(rk[7], rk[8]); rk[10] = KEYEXP256(rk[8], rk[9], 0x10); rk[11] = KEYEXP256_2(rk[9], rk[10]); rk[12] = KEYEXP256(rk[10], rk[11], 0x20); rk[13] = KEYEXP256_2(rk[11], rk[12]); rk[14] = KEYEXP256(rk[12], rk[13], 0x40); break; } } }
/* Encryption key setup */ static void aes_key_setup_enc(__m128i rk[], const uint8_t* cipherKey, int keylen) { switch (keylen) { case 16: { /* 128 bit key setup */ rk[0] = _mm_loadu_si128((const __m128i*) cipherKey); rk[1] = KEYEXP128(rk[0], 0x01); rk[2] = KEYEXP128(rk[1], 0x02); rk[3] = KEYEXP128(rk[2], 0x04); rk[4] = KEYEXP128(rk[3], 0x08); rk[5] = KEYEXP128(rk[4], 0x10); rk[6] = KEYEXP128(rk[5], 0x20); rk[7] = KEYEXP128(rk[6], 0x40); rk[8] = KEYEXP128(rk[7], 0x80); rk[9] = KEYEXP128(rk[8], 0x1B); rk[10] = KEYEXP128(rk[9], 0x36); break; } case 24: { /* 192 bit key setup */ uint8_t key[24]; __m128i temp[2]; memcpy(key, cipherKey, 24); rk[0] = _mm_loadu_si128((const __m128i*) key); rk[1] = _mm_loadu_si128((const __m128i*) (key+16)); temp[0] = KEYEXP192(rk[0], rk[1], 0x01); temp[1] = KEYEXP192_2(temp[0], rk[1]); rk[1] = SHUFFLE128_0(rk[1], temp[0]); rk[2] = SHUFFLE128_1(temp[0], temp[1]); rk[3] = KEYEXP192(temp[0], temp[1], 0x02); rk[4] = KEYEXP192_2(rk[3], temp[1]); temp[0] = KEYEXP192(rk[3], rk[4], 0x04); temp[1] = KEYEXP192_2(temp[0], rk[4]); rk[4] = SHUFFLE128_0(rk[4], temp[0]); rk[5] = SHUFFLE128_1(temp[0], temp[1]); rk[6] = KEYEXP192(temp[0], temp[1], 0x08); rk[7] = KEYEXP192_2(rk[6], temp[1]); temp[0] = KEYEXP192(rk[6], rk[7], 0x10); temp[1] = KEYEXP192_2(temp[0], rk[7]); rk[7] = SHUFFLE128_0(rk[7], temp[0]); rk[8] = SHUFFLE128_1(temp[0], temp[1]); rk[9] = KEYEXP192(temp[0], temp[1], 0x20); rk[10] = KEYEXP192_2(rk[9], temp[1]); temp[0] = KEYEXP192(rk[9], rk[10], 0x40); temp[1] = KEYEXP192_2(temp[0], rk[10]); rk[10] = SHUFFLE128_0(rk[10], temp[0]); rk[11] = SHUFFLE128_1(temp[0], temp[1]); rk[12] = KEYEXP192(temp[0], temp[1], 0x80); break; } case 32: { /* 256 bit key setup */ rk[0] = _mm_loadu_si128((const __m128i*) cipherKey); rk[1] = _mm_loadu_si128((const __m128i*) (cipherKey+16)); rk[2] = KEYEXP256(rk[0], rk[1], 0x01); rk[3] = KEYEXP256_2(rk[1], rk[2]); rk[4] = KEYEXP256(rk[2], rk[3], 0x02); rk[5] = KEYEXP256_2(rk[3], rk[4]); rk[6] = KEYEXP256(rk[4], rk[5], 0x04); rk[7] = KEYEXP256_2(rk[5], rk[6]); rk[8] = KEYEXP256(rk[6], rk[7], 0x08); rk[9] = KEYEXP256_2(rk[7], rk[8]); rk[10] = KEYEXP256(rk[8], rk[9], 0x10); rk[11] = KEYEXP256_2(rk[9], rk[10]); rk[12] = KEYEXP256(rk[10], rk[11], 0x20); rk[13] = KEYEXP256_2(rk[11], rk[12]); rk[14] = KEYEXP256(rk[12], rk[13], 0x40); break; } } }