123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #ifndef DPFPP_AES_H
- #define DPFPP_AES_H
- #include <x86intrin.h>
- #define EXPAND_ASSIST(v1,v2,v3,v4,shuff_const,aes_const) \
- v2 = _mm_aeskeygenassist_si128(v4,aes_const); \
- v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
- _mm_castsi128_ps(v1), 16)); \
- v1 = _mm_xor_si128(v1,v3); \
- v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
- _mm_castsi128_ps(v1), 140)); \
- v1 = _mm_xor_si128(v1,v3); \
- v2 = _mm_shuffle_epi32(v2,shuff_const); \
- v1 = _mm_xor_si128(v1,v2)
- struct AES_KEY
- {
- AES_KEY(const __m128i userkey = _mm_set_epi64x(597349, 121379))
- : rounds(10)
- {
- __m128i x0, x1, x2;
- rd_key[0] = x0 = userkey;
- x2 = _mm_setzero_si128();
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 1);
- rd_key[1] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 2);
- rd_key[2] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 4);
- rd_key[3] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 8);
- rd_key[4] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 16);
- rd_key[5] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 32);
- rd_key[6] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 64);
- rd_key[7] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 128);
- rd_key[8] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 27);
- rd_key[9] = x0;
- EXPAND_ASSIST(x0, x1, x2, x0, 255, 54);
- rd_key[10] = x0;
- }
- __m128i rd_key[11];
- unsigned int rounds;
- } ;
- static const AES_KEY default_aes_key;
- static inline void
- AES_ecb_encrypt_blks(__m128i * __restrict__ blks, unsigned int nblks, const AES_KEY * __restrict__ key)
- {
- for (unsigned int i = 0; i < nblks; ++i)
- blks[i] = _mm_xor_si128(blks[i], key->rd_key[0]);
- for (unsigned int j = 1; j < key->rounds; ++j)
- for (unsigned int i = 0; i < nblks; ++i)
- blks[i] = _mm_aesenc_si128(blks[i], key->rd_key[j]);
- for (unsigned int i = 0; i < nblks; ++i)
- blks[i] = _mm_aesenclast_si128(blks[i], key->rd_key[key->rounds]);
- }
- static inline void
- AES_set_decrypt_key_fast(AES_KEY * __restrict__ dkey, const AES_KEY * __restrict__ ekey)
- {
- int j = 0;
- int i = ekey->rounds;
- #if (OCB_KEY_LEN == 0)
- dkey->rounds = i;
- #endif
- dkey->rd_key[i--] = ekey->rd_key[j++];
- while (i)
- dkey->rd_key[i--] = _mm_aesimc_si128(ekey->rd_key[j++]);
- dkey->rd_key[i] = ekey->rd_key[j];
- }
- static inline void
- AES_ecb_decrypt_blks(__m128i * __restrict__ blks, unsigned nblks, const AES_KEY * __restrict__ key)
- {
- unsigned i, j, rnds = key->rounds;
- for (i = 0; i < nblks; ++i)
- blks[i] = _mm_xor_si128(blks[i], key->rd_key[0]);
- for (j = 1; j < rnds; ++j)
- for (i = 0; i < nblks; ++i)
- blks[i] = _mm_aesdec_si128(blks[i], key->rd_key[j]);
- for (i = 0; i < nblks; ++i)
- blks[i] = _mm_aesdeclast_si128(blks[i], key->rd_key[j]);
- }
- #endif
|