Rizin
unix-like reverse engineering framework and cli tools
aesdata.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Macros

#define BYTE_FORMAT   "0x%02x"
 
#define BYTE_COLUMNS   8
 
#define WORD_FORMAT   "0x%08lx"
 
#define WORD_COLUMNS   4
 

Functions

static unsigned xtime (unsigned x)
 
static void compute_log (void)
 
static unsigned mult (unsigned a, unsigned b)
 
static unsigned invert (unsigned x)
 
static unsigned affine (unsigned x)
 
static void compute_sbox (void)
 
static void compute_dtable (void)
 
static void compute_itable (void)
 
static void compute_mtable (void)
 
static void display_byte_table (const char *name, unsigned char *table)
 
static void display_table (const char *name, unsigned long table[][0x100])
 
static void display_polynomial (const unsigned *p)
 
int main (int argc, char **argv)
 

Variables

unsigned char sbox [0x100]
 
unsigned char isbox [0x100]
 
unsigned char gf2_log [0x100]
 
unsigned char gf2_exp [0x100]
 
unsigned long dtable [4][0x100]
 
unsigned long itable [4][0x100]
 
unsigned long mtable [4][0x100]
 

Macro Definition Documentation

◆ BYTE_COLUMNS

#define BYTE_COLUMNS   8

Definition at line 11 of file aesdata.c.

◆ BYTE_FORMAT

#define BYTE_FORMAT   "0x%02x"

Definition at line 10 of file aesdata.c.

◆ WORD_COLUMNS

#define WORD_COLUMNS   4

Definition at line 18 of file aesdata.c.

◆ WORD_FORMAT

#define WORD_FORMAT   "0x%08lx"

Definition at line 17 of file aesdata.c.

Function Documentation

◆ affine()

static unsigned affine ( unsigned  x)
static

Definition at line 79 of file aesdata.c.

80 {
81  return 0xff &
82  (0x63^x^(x>>4)^(x<<4)^(x>>5)^(x<<3)^(x>>6)^(x<<2)^(x>>7)^(x<<1));
83 }
int x
Definition: mipsasm.c:20

References x.

Referenced by compute_sbox().

◆ compute_dtable()

static void compute_dtable ( void  )
static

Definition at line 102 of file aesdata.c.

103 {
104  unsigned i;
105  for (i = 0; i<0x100; i++)
106  {
107  unsigned s = sbox[i];
108  unsigned j;
109  unsigned long t =( ( (s ^ xtime(s)) << 24)
110  | (s << 16) | (s << 8)
111  | xtime(s) );
112 
113  for (j = 0; j<4; j++, t = (t << 8) | (t >> 24))
114  dtable[j][i] = t;
115  }
116 }
unsigned long dtable[4][0x100]
Definition: aesdata.c:26
static unsigned xtime(unsigned x)
Definition: aesdata.c:31
lzma_index ** i
Definition: index.h:629
static RzSocket * s
Definition: rtr.c:28

References dtable, i, s, and xtime().

Referenced by main().

◆ compute_itable()

static void compute_itable ( void  )
static

Definition at line 121 of file aesdata.c.

122 {
123  unsigned i;
124  for (i = 0; i<0x100; i++)
125  {
126  unsigned s = isbox[i];
127  unsigned j;
128  unsigned long t = ( (mult(s, 0xb) << 24)
129  | (mult(s, 0xd) << 16)
130  | (mult(s, 0x9) << 8)
131  | (mult(s, 0xe) ));
132 
133  for (j = 0; j<4; j++, t = (t << 8) | (t >> 24))
134  itable[j][i] = t;
135  }
136 }
unsigned long itable[4][0x100]
Definition: aesdata.c:27
static unsigned mult(unsigned a, unsigned b)
Definition: aesdata.c:67
unsigned char isbox[0x100]
Definition: aesdata.c:21

References i, isbox, itable, mult(), and s.

Referenced by main().

◆ compute_log()

static void compute_log ( void  )
static

Definition at line 47 of file aesdata.c.

48 {
49  unsigned i = 0;
50  unsigned x = 1;
51 
52  memset(gf2_log, 0, 0x100);
53 
54  for (i = 0; i < 0x100; i++, x = x ^ xtime(x))
55  {
56  gf2_exp[i] = x;
57  gf2_log[x] = i;
58  }
59  /* Invalid. */
60  gf2_log[0] = 0;
61  /* The loop above sets gf2_log[1] = 0xff, which is correct,
62  * but gf2_log[1] = 0 is nicer. */
63  gf2_log[1] = 0;
64 }
unsigned char gf2_exp[0x100]
Definition: aesdata.c:24
unsigned char gf2_log[0x100]
Definition: aesdata.c:23
return memset(p, 0, total)

References gf2_exp, gf2_log, i, memset(), x, and xtime().

Referenced by main().

◆ compute_mtable()

static void compute_mtable ( void  )
static

Definition at line 140 of file aesdata.c.

141 {
142  unsigned i;
143  for (i = 0; i<0x100; i++)
144  {
145  unsigned j;
146  unsigned long t = ( (mult(i, 0xb) << 24)
147  | (mult(i, 0xd) << 16)
148  | (mult(i, 0x9) << 8)
149  | (mult(i, 0xe) ));
150 
151  for (j = 0; j<4; j++, t = (t << 8) | (t >> 24))
152  mtable[j][i] = t;
153  }
154 }
unsigned long mtable[4][0x100]
Definition: aesdata.c:28

References i, mtable, and mult().

Referenced by main().

◆ compute_sbox()

static void compute_sbox ( void  )
static

Definition at line 86 of file aesdata.c.

87 {
88  unsigned i;
89  for (i = 0; i<0x100; i++)
90  {
91  sbox[i] = affine(invert(i));
92  isbox[sbox[i]] = i;
93  }
94 }
static unsigned invert(unsigned x)
Definition: aesdata.c:73
static unsigned affine(unsigned x)
Definition: aesdata.c:79

References affine(), i, invert(), and isbox.

Referenced by main().

◆ display_byte_table()

static void display_byte_table ( const char *  name,
unsigned char *  table 
)
static

Definition at line 157 of file aesdata.c.

158 {
159  unsigned i, j;
160 
161  printf("uint8_t %s[0x100] =\n{", name);
162 
163  for (i = 0; i<0x100; i+= BYTE_COLUMNS)
164  {
165  printf("\n ");
166  for (j = 0; j<BYTE_COLUMNS; j++)
167  printf(BYTE_FORMAT ",", table[i + j]);
168  }
169 
170  printf("\n};\n\n");
171 }
#define BYTE_COLUMNS
Definition: aesdata.c:11
#define BYTE_FORMAT
Definition: aesdata.c:10
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
Definition: z80asm.h:102

References BYTE_COLUMNS, BYTE_FORMAT, i, and printf().

Referenced by main().

◆ display_polynomial()

static void display_polynomial ( const unsigned p)
static

Definition at line 195 of file aesdata.c.

196 {
197  printf("(%x x^3 + %x x^2 + %x x + %x)",
198  p[3], p[2], p[1], p[0]);
199 }
void * p
Definition: libc.cpp:67

References p, and printf().

Referenced by main().

◆ display_table()

static void display_table ( const char *  name,
unsigned long  table[][0x100] 
)
static

Definition at line 174 of file aesdata.c.

175 {
176  unsigned i, j, k;
177 
178  printf("uint32_t %s[4][0x100] =\n{\n ", name);
179 
180  for (k = 0; k<4; k++)
181  {
182  printf("{ ");
183  for (i = 0; i<0x100; i+= WORD_COLUMNS)
184  {
185  printf("\n ");
186  for (j = 0; j<WORD_COLUMNS; j++)
187  printf(WORD_FORMAT ",", table[k][i + j]);
188  }
189  printf("\n },");
190  }
191  printf("\n};\n\n");
192 }
#define WORD_FORMAT
Definition: aesdata.c:17
#define WORD_COLUMNS
Definition: aesdata.c:18
const char * k
Definition: dsignal.c:11

References i, k, printf(), WORD_COLUMNS, and WORD_FORMAT.

Referenced by main().

◆ invert()

static unsigned invert ( unsigned  x)
static

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 202 of file aesdata.c.

203 {
204  compute_log();
205  if (argc == 1)
206  {
207  display_byte_table("gf2_log", gf2_log);
208  display_byte_table("gf2_exp", gf2_exp);
209 
210  compute_sbox();
211  display_byte_table("sbox", sbox);
212  display_byte_table("isbox", isbox);
213 
214  compute_dtable();
215  display_table("dtable", dtable);
216 
217  compute_itable();
218  display_table("itable", itable);
219 
220  compute_mtable();
221  display_table("mtable", mtable);
222 
223  return 0;
224  }
225  else if (argc == 2)
226  {
227  unsigned a;
228  for (a = 1; a<0x100; a++)
229  {
230  unsigned a1 = invert(a);
231  unsigned b;
232  unsigned u;
233  if (a1 == 0)
234  printf("invert(%x) = 0 !\n", a);
235 
236  u = mult(a, a1);
237  if (u != 1)
238  printf("invert(%x) = %x; product = %x\n",
239  a, a1, u);
240 
241  for (b = 1; b<0x100; b++)
242  {
243  unsigned b1 = invert(b);
244  unsigned c = mult(a, b);
245 
246  if (c == 0)
247  printf("%x x %x = 0\n", a, b);
248 
249  u = mult(c, a1);
250  if (u != b)
251  printf("%x x %x = %x, invert(%x) = %x, %x x %x = %x\n",
252  a, b, c, a, a1, c, a1, u);
253 
254  u = mult(c, b1);
255  if (u != a)
256  printf("%x x %x = %x, invert(%x) = %x, %x x %x = %x\n",
257  a, b, c, b, b1, c, b1, u);
258  }
259  }
260  return 0;
261  }
262  else if (argc == 4)
263  {
264  unsigned a, b, c;
265  int op = argv[2][0];
266  a = strtoul(argv[1], NULL, 16);
267  b = strtoul(argv[3], NULL, 16);
268  switch (op)
269  {
270  case '+':
271  c = a ^ b;
272  break;
273  case '*':
274  case 'x':
275  c = mult(a,b);
276  break;
277  case '/':
278  c = mult(a, invert(b));
279  break;
280  default:
281  return 1;
282  }
283  printf("%x %c %x = %x\n", a, op, b, c);
284  return 0;
285  }
286 #if 0
287  else if (argc == 5)
288  {
289  /* Compute gcd(a, x^4+1) */
290  unsigned d[4];
291  unsigned u[4];
292 
293  for (i = 0; i<4; i++)
294  a[i] = strtoul(argv[1+i], NULL, 16);
295  }
296 #endif
297  else if (argc == 9)
298  {
299  unsigned a[4];
300  unsigned b[4];
301  unsigned c[4];
302  unsigned i;
303  for (i = 0; i<4; i++)
304  {
305  a[i] = strtoul(argv[1+i], NULL, 16);
306  b[i] = strtoul(argv[5+i], NULL, 16);
307  }
308 
309  c[0] = mult(a[0],b[0])^mult(a[3],b[1])^mult(a[2],b[2])^mult(a[1],b[3]);
310  c[1] = mult(a[1],b[0])^mult(a[0],b[1])^mult(a[3],b[2])^mult(a[2],b[3]);
311  c[2] = mult(a[2],b[0])^mult(a[1],b[1])^mult(a[0],b[2])^mult(a[3],b[3]);
312  c[3] = mult(a[3],b[0])^mult(a[2],b[1])^mult(a[1],b[2])^mult(a[0],b[3]);
313 
315  printf(" = "); display_polynomial(c); printf("\n");
316  }
317  return 1;
318 }
static void compute_itable(void)
Definition: aesdata.c:121
static void display_polynomial(const unsigned *p)
Definition: aesdata.c:195
static void compute_log(void)
Definition: aesdata.c:47
static void compute_sbox(void)
Definition: aesdata.c:86
static void compute_dtable(void)
Definition: aesdata.c:102
static void compute_mtable(void)
Definition: aesdata.c:140
static void display_table(const char *name, unsigned long table[][0x100])
Definition: aesdata.c:174
static void display_byte_table(const char *name, unsigned char *table)
Definition: aesdata.c:157
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
Definition: dis.c:32

References a, argv, b, b1, c, compute_dtable(), compute_itable(), compute_log(), compute_mtable(), compute_sbox(), d, display_byte_table(), display_polynomial(), display_table(), dtable, gf2_exp, gf2_log, i, invert(), isbox, itable, mtable, mult(), NULL, and printf().

◆ mult()

static unsigned mult ( unsigned  a,
unsigned  b 
)
static

Definition at line 67 of file aesdata.c.

68 {
69  return (a && b) ? gf2_exp[ (gf2_log[a] + gf2_log[b]) % 255] : 0;
70 }

References a, b, gf2_exp, and gf2_log.

Referenced by compute_itable(), compute_mtable(), and main().

◆ xtime()

static unsigned xtime ( unsigned  x)
static

Definition at line 31 of file aesdata.c.

32 {
33  assert (x < 0x100);
34 
35  x <<= 1;
36  if (x & 0x100)
37  x ^= 0x11b;
38 
39  assert (x < 0x100);
40 
41  return x;
42 }
assert(limit<=UINT32_MAX/2)

References assert(), and x.

Referenced by compute_dtable(), and compute_log().

Variable Documentation

◆ dtable

unsigned long dtable[4][0x100]

Definition at line 26 of file aesdata.c.

Referenced by compute_dtable(), and main().

◆ gf2_exp

unsigned char gf2_exp[0x100]

Definition at line 24 of file aesdata.c.

Referenced by compute_log(), invert(), main(), and mult().

◆ gf2_log

unsigned char gf2_log[0x100]

Definition at line 23 of file aesdata.c.

Referenced by compute_log(), invert(), main(), and mult().

◆ isbox

unsigned char isbox[0x100]

Definition at line 21 of file aesdata.c.

Referenced by compute_itable(), compute_sbox(), and main().

◆ itable

unsigned long itable[4][0x100]

Definition at line 27 of file aesdata.c.

Referenced by compute_itable(), and main().

◆ mtable

unsigned long mtable[4][0x100]

Definition at line 28 of file aesdata.c.

Referenced by compute_mtable(), and main().

◆ sbox

unsigned char sbox[0x100]

Definition at line 20 of file aesdata.c.