Skip to content

Instantly share code, notes, and snippets.

@enukane
Created December 5, 2011 03:12
Show Gist options
  • Save enukane/1432141 to your computer and use it in GitHub Desktop.
Save enukane/1432141 to your computer and use it in GitHub Desktop.
AESNI AESENC test (not working)
#include <stdio.h>
#include <stdint.h>
//#include <cpuid.h>
#include <emmintrin.h>
//#include <smmintrin.h>
typedef union {
__attribute__ ((aligned(16))) unsigned int i[4];
__m128i m;
} m128i;
#define PRINT_M128I(m) \
printf("0x%08x%08x%08x%08x", m.i[3], m.i[2], m.i[1], m.i[0])
int main()
{
/*
unsigned char key[16] = 0x0f0e0d0c0b0a09080706050403020100;
unsigned char ptext[16] = 0xffeeddccbbaa99887766554433221100;
unsigned char ctext[16] = {0};
*/
// m128i key = { 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100 };
m128i key = { 0x03020100, 0x07060504,0x0b0a0908, 0x0f0e0d0c};
// m128i ptext = { 0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100 };
m128i ptext = { 0x33221100, 0x77665544, 0xbbaa9988, 0xffeeddcc};
m128i ctext = {0};
// m128i exptext= { 0x5ac5b470, 0x80b7cdd8, 0x30047b6a, 0xd8e0c469 };
m128i exptext= { 0xd8e0c469 , 0x30047b6a, 0x80b7cdd8, 0x5ac5b470};
uintptr_t key_addr = (uintptr_t)&key;
uintptr_t ptext_addr = (uintptr_t)&ptext;
uintptr_t ctext_addr = (uintptr_t)&ctext;
printf("before:\n");
printf("\tkey = \t\t"); PRINT_M128I(key); printf("\n");
printf("\tptext = \t"); PRINT_M128I(ptext); printf("\n");
printf("\tctext = \t"); PRINT_M128I(ctext); printf("\n");
printf("\texptext = \t"); PRINT_M128I(exptext); printf("\n");
/*
__asm__ (
"mov eax %2;"
"movdqa xmm1, [eax];"
"aesenc xmm1, [%1];"
"movdqa [%0], xmm1;"
: "=r"(ctext)
: "r"(key), "r"(ptext)
: "xmm1"
);
*/
/* load key */
asm ("mov %0, %%rax;"
: /* no outputs */
: "r"(key_addr)
: "%rax"
);
asm ("movdqu (%%rax), %%xmm2;"
:
:
: "%xmm2"
);
/* load ptext */
asm ("mov %0, %%rax;"
: /* no outputs */
: "r"(ptext_addr)
: "%rax"
);
asm ("movdqu (%%rax), %%xmm1;"
:
:
: "%xmm1"
);
/* load ctext */
// asm ("mov %0, %%rax;"
// : /* no outputs */
// : "r"(ctext_addr)
// : "%rax"
// );
//
// asm ("movdqu (%%rax), %%xmm3;"
// :
// :
// : "%xmm3"
// );
asm ("aesenc %xmm2, %xmm1");
printf("checkit!");
printf("\tctext = \t"); PRINT_M128I(ctext); printf("\n");
asm ("mov %0, %%rax;"
:
: "r"(ctext_addr)
: "%rax"
);
asm ("movdqu %%xmm1, (%%rax);"
:
:
: "%rax"
);
printf("after:\n");
printf("\tkey = \t\t"); PRINT_M128I(key); printf("\n");
printf("\tptext = \t"); PRINT_M128I(ptext); printf("\n");
printf("\tctext = \t"); PRINT_M128I(ctext); printf("\n");
printf("\texptext = \t"); PRINT_M128I(exptext); printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment