File: aes.c

package info (click to toggle)
valgrind 1%3A3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (373 lines) | stat: -rw-r--r-- 10,833 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

#include <string.h>
#include <stdio.h>
#include <assert.h>

typedef  unsigned int   UInt;
typedef  signed int     Int;
typedef  unsigned char  UChar;
typedef  unsigned long long int ULong;
typedef  UChar          Bool;
#define False ((Bool)0)
#define True  ((Bool)1)

//typedef  unsigned char  V128[16];
typedef
   union {
      UChar uChar[16];
      UInt  uInt[4];
   }
   V128;

static UChar fromhex(char x) {
   if      (x >= '0' && x <= '9') { return(x - '0'); }
   else if (x >= 'A' && x <= 'F') { return(x - 'A' + 10); }
   else if (x >= 'a' && x <= 'f') { return(x - 'a' + 10); }
   else assert(0);
}

static void expand ( V128* dst, char* summary )
{
   Int i;
   assert( strlen(summary) == 32 );
   for (i = 0; i < 16; i++) {
      UChar xx = 0;
      UChar x = summary[31-2*i];
      UChar yy = 0;
      UChar y = summary[31-2*i-1];
      xx = fromhex (x);
      yy = fromhex (y);

      assert(xx < 16);
      assert(yy < 16);
      xx = (yy << 4) | xx;
      assert(xx < 256);
      dst->uChar[i] = xx;
   }
}

static int tohex (int nib)
{
   if (nib < 10)
      return '0' + nib;
   else
      return 'a' + nib - 10;
}
static void unexpand ( V128* dst, char* summary )
{
   Int i;
   for (i = 0; i < 16; i++) {
      *summary++ = tohex((dst->uChar[i] >> 4) & 0xf);
      *summary++ = tohex(dst->uChar[i] & 0xf);
   }
   *summary = 0;
}

static void AESDEC(char *s_argL, char *s_argR, char *s_exp)
{
   /*
     ; xmm1 and xmm2 hold two 128-bit inputs (xmm1 = State; xmm2 = Round key).
     ; The result is delivered in xmm1.
   */
   V128 argL, argR;
   V128 res;
   char s_res[33];
   V128 exp;
   expand(&argL, s_argL);
   expand(&argR, s_argR);
   __asm__ __volatile__(
      "subq      $1024,  %%rsp"             "\n\t"
      "movdqu    %1,     %%xmm1"            "\n\t"
      "movdqu    %2,     %%xmm2"            "\n\t"
      "aesdec    %%xmm2, %%xmm1"            "\n\t"
      "movdqu    %%xmm1, %0"                "\n\t"
      "addq      $1024,  %%rsp"             "\n\t"
      : /*out*/ "=m"(res)
      : "m"/*in*/(argL), "m"/*in*/(argR)
      : /*trash*/ "xmm1", "xmm2"
   );

   if (strlen(s_exp) > 0) {
      expand(&exp,  s_exp);
      assert (0 == memcmp(&res, &exp, 16));
   }
   unexpand (&res, s_res);
   printf ("aesdec %s %s result %s\n", s_argL, s_argR, s_res);
}

static void AESDECLAST(char *s_argL, char *s_argR, char *s_exp)
{
   /*
     ; xmm1 and xmm2 hold two 128-bit inputs (xmm1 = State; xmm2 = Round key).
     ; The result is delivered in xmm1.
   */
   V128 argL, argR;
   V128 res;
   char s_res[33];
   V128 exp;
   expand(&argL, s_argL);
   expand(&argR, s_argR);
   __asm__ __volatile__(
      "subq       $1024,  %%rsp"             "\n\t"
      "movdqu     %1,     %%xmm1"            "\n\t"
      "movdqu     %2,     %%xmm2"            "\n\t"
      "aesdeclast %%xmm2, %%xmm1"            "\n\t"
      "movdqu     %%xmm1, %0"                "\n\t"
      "addq       $1024,  %%rsp"             "\n\t"
      : /*out*/ "=m"(res)
      : "m"/*in*/(argL), "m"/*in*/(argR)
      : /*trash*/ "xmm1", "xmm2"
   );

   if (strlen(s_exp) > 0) {
      expand(&exp,  s_exp);
      assert (0 == memcmp(&res, &exp, 16));
   }
   unexpand (&res, s_res);
   printf ("aesdeclast %s %s result %s\n", s_argL, s_argR, s_res);
}

static void AESENC(char *s_argL, char *s_argR, char *s_exp)
{
   /*
     ; xmm1 and xmm2 hold two 128-bit inputs (xmm1 = State; xmm2 = Round key).
     ; The result is delivered in xmm1.
   */
   V128 argL, argR;
   V128 res;
   char s_res[33];
   V128 exp;
   expand(&argL, s_argL);
   expand(&argR, s_argR);
   __asm__ __volatile__(
      "subq      $1024,  %%rsp"             "\n\t"
      "movdqu    %1,     %%xmm1"            "\n\t"
      "movdqu    %2,     %%xmm2"            "\n\t"
      "aesenc    %%xmm2, %%xmm1"            "\n\t"
      "movdqu    %%xmm1, %0"                "\n\t"
      "addq      $1024,  %%rsp"             "\n\t"
      : /*out*/ "=m"(res)
      : "m"/*in*/(argL), "m"/*in*/(argR)
      : /*trash*/ "xmm1", "xmm2"
   );

   if (strlen(s_exp) > 0) {
      expand(&exp,  s_exp);
      assert (0 == memcmp(&res, &exp, 16));
   }
   unexpand (&res, s_res);
   printf ("aesenc %s %s result %s\n", s_argL, s_argR, s_res);
}

static void AESENCLAST(char *s_argL, char *s_argR, char *s_exp)
{
   /*
     ; xmm1 and xmm2 hold two 128-bit inputs (xmm1 = State; xmm2 = Round key)
     ; The result delivered in xmm1
   */
   V128 argL, argR;
   V128 res;
   char s_res[33];
   V128 exp;
   expand(&argL, s_argL);
   expand(&argR, s_argR);
   __asm__ __volatile__(
      "subq       $1024,  %%rsp"             "\n\t"
      "movdqu     %1,     %%xmm1"            "\n\t"
      "movdqu     %2,     %%xmm2"            "\n\t"
      "aesenclast %%xmm2, %%xmm1"            "\n\t"
      "movdqu     %%xmm1, %0"                "\n\t"
      "addq       $1024,  %%rsp"             "\n\t"
      : /*out*/ "=m"(res)
      : "m"/*in*/(argL), "m"/*in*/(argR)
      : /*trash*/ "xmm1", "xmm2"
   );

   if (strlen(s_exp) > 0) {
      expand(&exp,  s_exp);
      assert (0 == memcmp(&res, &exp, 16));
   }
   unexpand (&res, s_res);
   printf ("aesenclast %s %s result %s\n", s_argL, s_argR, s_res);
}

static void AESIMC(char *s_argR, char *s_exp)
{
   /* We test another way to pass input and get results */
   /* ; argR hold one 128-bit inputs (argR = Round key)
      ; result delivered in xmm5 */

   V128 argR;
   V128 res;
   char s_res[33];
   V128 exp;
   expand(&argR, s_argR);

   __asm__ __volatile__(
      "subq       $1024,  %%rsp"             "\n\t"
      "aesimc     %1,     %%xmm5"            "\n\t"
      "movdqu     %%xmm5, %0"                "\n\t"
      "addq       $1024,  %%rsp"             "\n\t"
      : /*out*/ "=m"(res)
      : "m"/*in*/(argR)
      : /*trash*/ "xmm5"
   );

   if (strlen(s_exp) > 0) {
      expand(&exp,  s_exp);
      assert (0 == memcmp(&res, &exp, 16));
   }
   unexpand (&res, s_res);
   printf ("aesimc %s result %s\n", s_argR, s_res);
}

static void AESKEYGENASSIST(int imm, char* s_argL, char* s_exp)
{
   /*
     ; xmm2 holds a 128-bit input; imm8 holds the RCON value
     ; result delivered in xmm1
   */

   V128 argL;
   V128 res;
   char s_res[33];
   V128 exp;
   expand(&argL, s_argL);
   if (imm == 1)
      __asm__ __volatile__(
         "subq       $1024,  %%rsp"             "\n\t"
         "movdqu     %1,     %%xmm2"            "\n\t"
         "aeskeygenassist $1,%%xmm2, %%xmm1"    "\n\t"
         "movdqu     %%xmm1, %0"                "\n\t"
         "addq       $1024,  %%rsp"             "\n\t"
         : /*out*/ "=m"(res)
         : "m"/*in*/(argL)
         : /*trash*/ "xmm1", "xmm2"
      );
   else if (imm == 2)
      __asm__ __volatile__(
         "subq       $1024,  %%rsp"             "\n\t"
         "movdqu     %1,     %%xmm2"            "\n\t"
         "aeskeygenassist $2,%%xmm2, %%xmm1"    "\n\t"
         "movdqu     %%xmm1, %0"                "\n\t"
         "addq       $1024,  %%rsp"             "\n\t"
         : /*out*/ "=m"(res)
         : "m"/*in*/(argL)
         : /*trash*/ "xmm1", "xmm2"
      );
   else if (imm == 8)
      __asm__ __volatile__(
         "subq       $1024,  %%rsp"             "\n\t"
         "movdqu     %1,     %%xmm2"            "\n\t"
         "aeskeygenassist $8,%%xmm2, %%xmm1"    "\n\t"
         "movdqu     %%xmm1, %0"                "\n\t"
         "addq       $1024,  %%rsp"             "\n\t"
         : /*out*/ "=m"(res)
         : "m"/*in*/(argL)
         : /*trash*/ "xmm1", "xmm2"
      );
   else assert (0);

   if (strlen(s_exp) > 0) {
      expand(&exp,  s_exp);
      assert (0 == memcmp(&res, &exp, 16));
   }
   unexpand (&res, s_res);
   printf ("aeskeygenassist %d %s result %s\n", imm, s_argL, s_res);
}

typedef struct Aes_Args {
   char* argL;
   char* argR;
   int imm; // only for aeskeygenassist
} Aes_Args;

/* Just a bunch of various data to compare a native run
   with a run under Valgrind. */
static const Aes_Args aes_args[] = {
   {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
    8},
   {"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    8},
   {"3243f6a8885a308d313198a2e0370734",
    "2b7e151628aed2a6abf7158809cf4f3c",
    2},
   {"193de3bea0f4e22b9ac68d2ae9f84808",
    "d42711aee0bf98f1b8b45de51e415230",
    2},
   {"d4bf5d30e0b452aeb84111f11e2798e5",
    "046681e5e0cb199a48f8d37a2806264c",
    1},
   {"a0fafe1788542cb123a339392a6c7605",
    "a49c7ff2689f352b6b5bea43026a5049",
    1},
   {"49ded28945db96f17f39871a7702533b",
    "49db873b453953897f02d2f177de961a",
    8},
   {"584dcaf11b4b5aacdbe7caa81b6bb0e5",
    "f2c295f27a96b9435935807a7359f67f",
    8},
   {"aa8f5f0361dde3ef82d24ad26832469a",
    "ac73cf7befc111df13b5d6b545235ab8",
    2},
   {"acc1d6b8efb55a7b1323cfdf457311b5",
    "75ec0993200b633353c0cf7cbb25d0dc",
    2},
   {"e9317db5cb322c723d2e895faf090794",
    "d014f9a8c9ee2589e13f0cc8b6630ca6",
    1},
   {NULL,
    NULL,
    0}
};

int main ( void )
{
   int i;

   /* test the various instructions, using the examples provided
      in  "White Paper Intel Advanced Encryption Standard AES
          instruction set" January 2010 (26/1/2010)
          Rev. 3.0
          by Shay Gueron */
   AESKEYGENASSIST(1,
                   "3c4fcf098815f7aba6d2ae2816157e2b",
                   "01eb848beb848a013424b5e524b5e434");
   AESENC("7b5b54657374566563746f725d53475d",
          "48692853686179295b477565726f6e5d",
          "a8311c2f9fdba3c58b104b58ded7e595");
   AESENCLAST("7b5b54657374566563746f725d53475d",
              "48692853686179295b477565726f6e5d",
              "c7fb881e938c5964177ec42553fdc611");
   AESDEC("7b5b54657374566563746f725d53475d",
          "48692853686179295b477565726f6e5d",
          "138ac342faea2787b58eb95eb730392a");
   AESDECLAST("7b5b54657374566563746f725d53475d",
              "48692853686179295b477565726f6e5d",
              "c5a391ef6b317f95d410637b72a593d0");
   /* ??? the AESIMC example given in the Intel White paper
      seems wrong.
      The below fails both under Valgrind and natively.
      AESIMC("48692853686179295b477565726f6e5d",
             "627a6f6644b109c82b18330a81c3b3e5");
      So we use the example given for the InvMixColums 
      transformation. */
   AESIMC("8dcab9dc035006bc8f57161e00cafd8d",
          "d635a667928b5eaeeec9cc3bc55f5777");


   /* and now a bunch of other calls. The below are verified
      using the aes.stdout.exp (produced by a native run). */
   
   for (i = 0; aes_args[i].argL != NULL; i++) {
      AESKEYGENASSIST(aes_args[i].imm, aes_args[i].argL, "");
      AESENC(aes_args[i].argL, aes_args[i].argR, "");
      AESENCLAST(aes_args[i].argL, aes_args[i].argR, "");
      AESDEC(aes_args[i].argL, aes_args[i].argR, "");
      AESDECLAST(aes_args[i].argL, aes_args[i].argR, "");
      AESIMC(aes_args[i].argL, "");
   }
   return 0;
}