12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #ifndef __AES_HPP__
- #define __AES_HPP__
- /* Based on reference code from the Intel AES-NI whitepaper
- * http://www.intel.com/content/dam/doc/white-paper/advanced-encryption-standard-new-instructions-set-paper.pdf
- */
- #include <wmmintrin.h>
- using AESkey = __m128i[11];
- static inline __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2)
- {
- __m128i temp3;
- temp2 = _mm_shuffle_epi32 (temp2 ,0xff);
- temp3 = _mm_slli_si128 (temp1, 0x4);
- temp1 = _mm_xor_si128 (temp1, temp3);
- temp3 = _mm_slli_si128 (temp3, 0x4);
- temp1 = _mm_xor_si128 (temp1, temp3);
- temp3 = _mm_slli_si128 (temp3, 0x4);
- temp1 = _mm_xor_si128 (temp1, temp3);
- temp1 = _mm_xor_si128 (temp1, temp2);
- return temp1;
- }
- static inline void AES_128_Key_Expansion (AESkey &key, __m128i rawkey)
- {
- __m128i temp1, temp2;
- __m128i *Key_Schedule = key;
- temp1 = rawkey;
- Key_Schedule[0] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1 ,0x1);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[1] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x2);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[2] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x4);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[3] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x8);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[4] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x10);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[5] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x20);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[6] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x40);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[7] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x80);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[8] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x1b);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[9] = temp1;
- temp2 = _mm_aeskeygenassist_si128 (temp1,0x36);
- temp1 = AES_128_ASSIST(temp1, temp2);
- Key_Schedule[10] = temp1;
- }
- static inline void AES_ECB_encrypt(__m128i &ciphertext, __m128i plaintext,
- const AESkey &key, size_t &aes_ops)
- {
- __m128i tmp;
- int j;
- tmp = plaintext;
- tmp = _mm_xor_si128 (tmp,key[0]);
- for(j=1; j<10; j++){
- tmp = _mm_aesenc_si128 (tmp,key[j]);
- }
- tmp = _mm_aesenclast_si128 (tmp,key[j]);
- ciphertext=tmp;
- ++aes_ops;
- }
- #endif
|