Skip to content

Commit

Permalink
Added aestest.c to check the aesni invocation matches the sw version
Browse files Browse the repository at this point in the history
  • Loading branch information
IntelEntropyReport committed May 24, 2017
1 parent c7a53aa commit 87d1843
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
54 changes: 48 additions & 6 deletions aes128k128d.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,61 @@ void aessw128k128d(unsigned char *key, unsigned char *data, unsigned char *ciphe

/* AESNI version using intrinsics library */

__m128i aes128_keyexpand(__m128i key)
__m128i aes128_keyexpand(__m128i key, __m128i keygened)
{
key = _mm_xor_si128(key, _mm_slli_si128(key, 4));
key = _mm_xor_si128(key, _mm_slli_si128(key, 4));
return _mm_xor_si128(key, _mm_slli_si128(key, 4));
key = _mm_xor_si128(key, _mm_slli_si128(key, 4));
keygened = _mm_shuffle_epi32(keygened, _MM_SHUFFLE(3,3,3,3));
return _mm_xor_si128(key, keygened);
}

#define KEYEXP(K, I) aes128_keyexpand(K, _mm_aeskeygenassist_si128(K, I))

void aesni128k128d(unsigned char *key, const unsigned char *plaintext, const unsigned char *ciphertext)
{
__m128i rk[11];
__m128i m;

/* 128 bit Key Expansion */
rk[0] = _mm_loadu_si128((__m128i*) key);
rk[1] = KEYEXP(rk[0], 0x01);
rk[2] = KEYEXP(rk[1], 0x02);
rk[3] = KEYEXP(rk[2], 0x04);
rk[4] = KEYEXP(rk[3], 0x08);
rk[5] = KEYEXP(rk[4], 0x10);
rk[6] = KEYEXP(rk[5], 0x20);
rk[7] = KEYEXP(rk[6], 0x40);
rk[8] = KEYEXP(rk[7], 0x80);
rk[9] = KEYEXP(rk[8], 0x1b);
rk[10]= KEYEXP(rk[9], 0x36);

// Do the encrypt

m = _mm_loadu_si128((const __m128i*) plaintext);

/* first 9 rounds */
m = _mm_xor_si128(m, rk[0]);
m = _mm_aesenc_si128(m, rk[1]);
m = _mm_aesenc_si128(m, rk[2]);
m = _mm_aesenc_si128(m, rk[3]);
m = _mm_aesenc_si128(m, rk[4]);
m = _mm_aesenc_si128(m, rk[5]);
m = _mm_aesenc_si128(m, rk[6]);
m = _mm_aesenc_si128(m, rk[7]);
m = _mm_aesenc_si128(m, rk[8]);
m = _mm_aesenc_si128(m, rk[9]);
/* Last round */
m = _mm_aesenclast_si128(m, rk[10]);
_mm_storeu_si128((__m128i*) ciphertext, m);
}
/*
void aesni256k128d(unsigned char *key, const unsigned char *plaintext, const unsigned char *ciphertext)
{
__m128i rk[15];
__m128i m;
/* 256 bit Key Expansion */
256 bit Key Expansion
rk[0] = _mm_loadu_si128((const __m128i*) key);
rk[1] = _mm_loadu_si128((const __m128i*) (key+16));
Expand All @@ -383,11 +425,11 @@ void aesni128k128d(unsigned char *key, const unsigned char *plaintext, const un
rk[14] = _mm_xor_si128(aes128_keyexpand(rk[12]), _mm_shuffle_epi32(_mm_aeskeygenassist_si128(rk[13], 0x40), 0xff));
// Do the encrypt
Do the encrypt
m = _mm_loadu_si128((const __m128i*) plaintext);
/* first 9 rounds */
first 9 rounds
m = _mm_xor_si128(m, rk[0]);
m = _mm_aesenc_si128(m, rk[1]);
m = _mm_aesenc_si128(m, rk[2]);
Expand All @@ -405,7 +447,7 @@ void aesni128k128d(unsigned char *key, const unsigned char *plaintext, const un
m = _mm_aesenclast_si128(m, rk[14]);
_mm_storeu_si128((__m128i*) ciphertext, m);
}

*/
/* Choose between the two */

void aes128k128d(unsigned char *key, unsigned char *data, unsigned char *ciphertext) {
Expand Down
44 changes: 44 additions & 0 deletions aestest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <stdio.h>

extern void aesni128k128d(unsigned char *key, unsigned char *data, unsigned char *ciphertext);
extern void aessw128k128d(unsigned char *key, unsigned char *data, unsigned char *ciphertext);

int aesni_supported;

void printsample(unsigned char *thesample)
{
int tempindex;
int j;
int i;
tempindex = 0;
for (i=0;i<16;i++) printf("%02X",thesample[i]);
printf("\n");
}


int main() {

unsigned char key[16];
unsigned char data[16];
unsigned char ciphertext[16];

int i;

for(i=0;i<16;i++) {
key[i]=0x00;
data[i]=0x00;
ciphertext[i]=0x00;
}

key[15]=0x01;
data[15]=0x10;

aesni128k128d(key,data,ciphertext);
printsample(ciphertext);

aessw128k128d(key,data,ciphertext);
printsample(ciphertext);

}

5 changes: 2 additions & 3 deletions djenrandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
Contact. David Johnston dj@deadhat.com
*/
/* make isnan() visible */
/*#define _BSD_SOURCE */

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -56,7 +54,7 @@
int aesni_supported;

void display_usage() {
fprintf(stderr,"Usage: djrandom [-bsvh] [-x <bits>] [-y <bits>] [-z <bits>] [-c <generate length>]\n");
fprintf(stderr,"Usage: djrandom [-bsvhn] [-x <bits>] [-y <bits>] [-z <bits>] [-c <generate length>]\n");
fprintf(stderr," [-m <|pure(default)|sums|biased|correlated|normal|file>] [-l <left_stepsize>]\n");
fprintf(stderr," [-r <right_stepsize>] [--stepnoise=<noise on step>] [--bias=<bias>]\n");
fprintf(stderr," [--correlation=<correlation>] [--mean=<normal mean>] [--variance=<normal variance>]\n");
Expand Down Expand Up @@ -97,6 +95,7 @@ fprintf(stderr," -x, --xor=<bits> XOR 'bits' of entropy together
fprintf(stderr," -y, --xmin=<bits> Provides the start of a range of XOR ratios to be chosen at random per sample\n");
fprintf(stderr," -z, --xmax=<bits> Provides the end of a range of XOR ratios to be chosen at random per sample\n");
fprintf(stderr," -s, --seed seed the internal RNG with /dev/random\n");
fprintf(stderr," -n, --noaesni Don't use AESNI instruction.\n");
fprintf(stderr," -c, --cmax=<generate length> number of PRNG generates before a reseed\n");
fprintf(stderr," -v, --verbose output the parameters\n");
fprintf(stderr,"\nFile Options\n\n");
Expand Down
14 changes: 11 additions & 3 deletions djenrandommodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ int getrand16(t_rngstate* rngstate)
unsigned char temprand[16];
unsigned char temprand2[16];

/* CTR variables for random number gen */
unsigned char out[16];
unsigned char out2[16];

int i;
int j;
unsigned long int theint;

/* CTR variables for random number gen */
unsigned char out[16];

/* Make a uniform Random number. */
/* put the random bits into a long int */
Expand Down Expand Up @@ -260,6 +261,8 @@ int smoothsource(t_modelstate* modelstate, t_rngstate* rngstate)
double pmfc;
double randomnumber;
int result;
double maxp;
double entropy;

/* vars for converting from random bit to a float */
unsigned long int theint;
Expand Down Expand Up @@ -326,7 +329,12 @@ int smoothsource(t_modelstate* modelstate, t_rngstate* rngstate)

if (modelstate->using_jfile ==1)
{
fprintf(modelstate->jfile,"%0.6f\n",tee);
if (pmfc > 0.5) maxp = pmfc;
else maxp = 1.0-pmfc;

entropy = -log(maxp)/log(2);

fprintf(modelstate->jfile,"%d, %0.6f\n",result,entropy);
}

return(result);
Expand Down

0 comments on commit 87d1843

Please sign in to comment.