Skip to content

Instantly share code, notes, and snippets.

@jens-na
Last active December 19, 2019 14:45
Show Gist options
  • Save jens-na/9ef4a04cea5382c4495c11c6282c3d5f to your computer and use it in GitHub Desktop.
Save jens-na/9ef4a04cea5382c4495c11c6282c3d5f to your computer and use it in GitHub Desktop.
#include "wmmintrin.h"
#include "emmintrin.h"
#include "smmintrin.h"
#include <stdio.h>
#include <stdint.h>
void print128_num(__m128i var)
{
uint8_t *val = (uint8_t*) &var;
printf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
val[0], val[1], val[2], val[3], val[4], val[5],
val[6], val[7], val[8], val[9], val[10], val[11],
val[12], val[13], val[14], val[15]);
}
void printbytes(unsigned char *m, int len) {
int i;
for (i = 0; i < len-1; ++i)
printf("%02x ", m[i]);
printf("%02x\n", m[len-1]);
}
void test_aes(const unsigned char *msg) {
__m128i rc[10];
__m128i s;
rc[0] = _mm_set_epi32(0x0684704c,0xe620c00a,0xb2c5fef0,0x75817b9d);
rc[1] = _mm_set_epi32(0x8b66b4e1,0x88f3a06b,0x640f6ba4,0x2f08f717);
rc[2] = _mm_set_epi32(0x3402de2d,0x53f28498,0xcf029d60,0x9f029114);
rc[3] = _mm_set_epi32(0x0ed6eae6,0x2e7b4f08,0xbbf3bcaf,0xfd5b4f79);
rc[4] = _mm_set_epi32(0xcbcfb0cb,0x4872448b,0x79eecd1c,0xbe397044);
rc[5] = _mm_set_epi32(0x7eeacdee,0x6e9032b7,0x8d5335ed,0x2b8a057b);
rc[6] = _mm_set_epi32(0x67c28f43,0x5e2e7cd0,0xe2412761,0xda4fef1b);
rc[7] = _mm_set_epi32(0x2924d9b0,0xafcacc07,0x675ffde2,0x1fc70b3b);
rc[8] = _mm_set_epi32(0xab4d63f1,0xe6867fe9,0xecdb8fca,0xb9d465ee);
rc[9] = _mm_set_epi32(0x1c30bf84,0xd4b7cd64,0x5b2a404f,0xad037e33);
s = _mm_load_si128(&((__m128i*)msg)[0]);
s = _mm_aesenc_si128(s, rc[0]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[1]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[2]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[3]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[4]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[5]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[6]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[7]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[8]);
print128_num(s); printf("\n");
s = _mm_aesenc_si128(s, rc[9]);
print128_num(s); printf("\n");
}
// $ gcc -std=c99 -O3 -Wno-format -march=native -funroll-loops -fomit-frame-pointer aes_test.c
// $ ./a.out
// Input block
// 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
// AES Rounds
// f7 11 dd 30 dc 93 f6 e3 ba 19 7d 87 6b ec a5 5a
// f9 98 80 d3 d6 50 5d 2f 01 9a 63 a2 d7 05 26 e4
// 5a d1 9e 55 d5 64 13 4d 05 77 5b 5b 8d b2 9c 58
// 88 56 6a 8a 8f cb bd 76 6f b4 0f 72 5a 75 04 d8
// e1 71 ad 42 fa cc 6d 6f 46 f3 0b 44 5e b9 f4 59
// ad dd 29 d5 33 f4 62 10 c4 c8 4e 08 87 97 44 3f
// aa 1b a4 04 c7 01 59 49 04 55 ae d8 af d4 0b 55
// e4 94 db 8b b3 57 3e 1e 52 4e af d5 02 c2 dc 04
// 5a c3 19 df da 10 c9 88 4d 97 5c 9e c8 9e 62 a4
// 12 d8 38 66 56 67 03 a7 b4 9b bb a6 eb 7c 4a da
void main(int argc, char **argv) {
unsigned char *msg = (unsigned char *)calloc(16, sizeof(unsigned char));
int i;
printf("Input block\n");
for (i = 0; i < 16; ++i)
msg[i] = i;
printbytes(msg, 16);
printf("AES Rounds\n");
test_aes(msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment