PostgreSQL Source Code  git master
rijndael.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _rijndael_ctx
 

Typedefs

typedef uint8 u1byte
 
typedef uint16 u2byte
 
typedef uint32 u4byte
 
typedef int8 s1byte
 
typedef int16 s2byte
 
typedef int32 s4byte
 
typedef struct _rijndael_ctx rijndael_ctx
 

Functions

rijndael_ctxrijndael_set_key (rijndael_ctx *, const u4byte *, const u4byte, int)
 
void rijndael_encrypt (rijndael_ctx *, const u4byte *, u4byte *)
 
void rijndael_decrypt (rijndael_ctx *, const u4byte *, u4byte *)
 
void aes_set_key (rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
 
void aes_ecb_encrypt (rijndael_ctx *ctx, uint8 *data, unsigned len)
 
void aes_ecb_decrypt (rijndael_ctx *ctx, uint8 *data, unsigned len)
 
void aes_cbc_encrypt (rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
 
void aes_cbc_decrypt (rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
 

Typedef Documentation

◆ rijndael_ctx

◆ s1byte

Definition at line 29 of file rijndael.h.

◆ s2byte

Definition at line 30 of file rijndael.h.

◆ s4byte

Definition at line 31 of file rijndael.h.

◆ u1byte

Definition at line 25 of file rijndael.h.

◆ u2byte

Definition at line 26 of file rijndael.h.

◆ u4byte

Definition at line 27 of file rijndael.h.

Function Documentation

◆ aes_cbc_decrypt()

void aes_cbc_decrypt ( rijndael_ctx ctx,
uint8 iva,
uint8 data,
unsigned  len 
)

Definition at line 567 of file rijndael.c.

References buf, gen_tabs(), i, main(), name, and rijndael_decrypt().

Referenced by rj_decrypt().

568 {
569  uint32 *d = (uint32 *) data;
570  unsigned bs = 16;
571  uint32 buf[4],
572  iv[4];
573 
574  memcpy(iv, iva, bs);
575  while (len >= bs)
576  {
577  buf[0] = d[0];
578  buf[1] = d[1];
579  buf[2] = d[2];
580  buf[3] = d[3];
581 
582  rijndael_decrypt(ctx, buf, d);
583 
584  d[0] ^= iv[0];
585  d[1] ^= iv[1];
586  d[2] ^= iv[2];
587  d[3] ^= iv[3];
588 
589  iv[0] = buf[0];
590  iv[1] = buf[1];
591  iv[2] = buf[2];
592  iv[3] = buf[3];
593  d += 4;
594  len -= bs;
595  }
596 }
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:306
void rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
Definition: rijndael.c:451

◆ aes_cbc_encrypt()

void aes_cbc_encrypt ( rijndael_ctx ctx,
uint8 iva,
uint8 data,
unsigned  len 
)

Definition at line 545 of file rijndael.c.

References rijndael_encrypt().

Referenced by rj_encrypt().

546 {
547  uint32 *iv = (uint32 *) iva;
548  uint32 *d = (uint32 *) data;
549  unsigned bs = 16;
550 
551  while (len >= bs)
552  {
553  d[0] ^= iv[0];
554  d[1] ^= iv[1];
555  d[2] ^= iv[2];
556  d[3] ^= iv[3];
557 
558  rijndael_encrypt(ctx, d, d);
559 
560  iv = d;
561  d += bs / 4;
562  len -= bs;
563  }
564 }
unsigned int uint32
Definition: c.h:306
void rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
Definition: rijndael.c:387

◆ aes_ecb_decrypt()

void aes_ecb_decrypt ( rijndael_ctx ctx,
uint8 data,
unsigned  len 
)

Definition at line 529 of file rijndael.c.

References rijndael_decrypt().

Referenced by rj_decrypt().

530 {
531  unsigned bs = 16;
532  uint32 *d;
533 
534  while (len >= bs)
535  {
536  d = (uint32 *) data;
537  rijndael_decrypt(ctx, d, d);
538 
539  len -= bs;
540  data += bs;
541  }
542 }
unsigned int uint32
Definition: c.h:306
void rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
Definition: rijndael.c:451

◆ aes_ecb_encrypt()

void aes_ecb_encrypt ( rijndael_ctx ctx,
uint8 data,
unsigned  len 
)

Definition at line 513 of file rijndael.c.

References rijndael_encrypt().

Referenced by rj_encrypt().

514 {
515  unsigned bs = 16;
516  uint32 *d;
517 
518  while (len >= bs)
519  {
520  d = (uint32 *) data;
521  rijndael_encrypt(ctx, d, d);
522 
523  len -= bs;
524  data += bs;
525  }
526 }
unsigned int uint32
Definition: c.h:306
void rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
Definition: rijndael.c:387

◆ aes_set_key()

void aes_set_key ( rijndael_ctx ctx,
const uint8 key,
unsigned  keybits,
int  enc 
)

Definition at line 504 of file rijndael.c.

References rijndael_set_key().

Referenced by rj_real_init().

505 {
506  uint32 *k;
507 
508  k = (uint32 *) key;
509  rijndael_set_key(ctx, k, keybits, enc);
510 }
rijndael_ctx * rijndael_set_key(rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len, int encrypt)
Definition: rijndael.c:305
struct pg_encoding enc
Definition: encode.c:522
unsigned int uint32
Definition: c.h:306

◆ rijndael_decrypt()

void rijndael_decrypt ( rijndael_ctx ,
const u4byte ,
u4byte  
)

Definition at line 451 of file rijndael.c.

References _rijndael_ctx::d_key, _rijndael_ctx::e_key, i_lround, i_nround, io_swap, and _rijndael_ctx::k_len.

Referenced by aes_cbc_decrypt(), and aes_ecb_decrypt().

452 {
453  u4byte b0[4],
454  b1[4],
455  *kp;
456  u4byte k_len = ctx->k_len;
457  u4byte *e_key = ctx->e_key;
458  u4byte *d_key = ctx->d_key;
459 
460  b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
461  b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
462  b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
463  b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];
464 
465  kp = d_key + 4 * (k_len + 5);
466 
467  if (k_len > 6)
468  {
469  i_nround(b1, b0, kp);
470  i_nround(b0, b1, kp);
471  }
472 
473  if (k_len > 4)
474  {
475  i_nround(b1, b0, kp);
476  i_nround(b0, b1, kp);
477  }
478 
479  i_nround(b1, b0, kp);
480  i_nround(b0, b1, kp);
481  i_nround(b1, b0, kp);
482  i_nround(b0, b1, kp);
483  i_nround(b1, b0, kp);
484  i_nround(b0, b1, kp);
485  i_nround(b1, b0, kp);
486  i_nround(b0, b1, kp);
487  i_nround(b1, b0, kp);
488  i_lround(b0, b1, kp);
489 
490  out_blk[0] = io_swap(b0[0]);
491  out_blk[1] = io_swap(b0[1]);
492  out_blk[2] = io_swap(b0[2]);
493  out_blk[3] = io_swap(b0[3]);
494 }
#define i_lround(bo, bi, k)
Definition: rijndael.c:442
uint32 u4byte
Definition: rijndael.h:27
#define i_nround(bo, bi, k)
Definition: rijndael.c:433
#define io_swap(x)
Definition: rijndael.c:73

◆ rijndael_encrypt()

void rijndael_encrypt ( rijndael_ctx ,
const u4byte ,
u4byte  
)

Definition at line 387 of file rijndael.c.

References _rijndael_ctx::e_key, f_lround, f_nround, io_swap, and _rijndael_ctx::k_len.

Referenced by aes_cbc_encrypt(), and aes_ecb_encrypt().

388 {
389  u4byte k_len = ctx->k_len;
390  u4byte *e_key = ctx->e_key;
391  u4byte b0[4],
392  b1[4],
393  *kp;
394 
395  b0[0] = io_swap(in_blk[0]) ^ e_key[0];
396  b0[1] = io_swap(in_blk[1]) ^ e_key[1];
397  b0[2] = io_swap(in_blk[2]) ^ e_key[2];
398  b0[3] = io_swap(in_blk[3]) ^ e_key[3];
399 
400  kp = e_key + 4;
401 
402  if (k_len > 6)
403  {
404  f_nround(b1, b0, kp);
405  f_nround(b0, b1, kp);
406  }
407 
408  if (k_len > 4)
409  {
410  f_nround(b1, b0, kp);
411  f_nround(b0, b1, kp);
412  }
413 
414  f_nround(b1, b0, kp);
415  f_nround(b0, b1, kp);
416  f_nround(b1, b0, kp);
417  f_nround(b0, b1, kp);
418  f_nround(b1, b0, kp);
419  f_nround(b0, b1, kp);
420  f_nround(b1, b0, kp);
421  f_nround(b0, b1, kp);
422  f_nround(b1, b0, kp);
423  f_lround(b0, b1, kp);
424 
425  out_blk[0] = io_swap(b0[0]);
426  out_blk[1] = io_swap(b0[1]);
427  out_blk[2] = io_swap(b0[2]);
428  out_blk[3] = io_swap(b0[3]);
429 }
uint32 u4byte
Definition: rijndael.h:27
#define f_nround(bo, bi, k)
Definition: rijndael.c:369
#define f_lround(bo, bi, k)
Definition: rijndael.c:378
#define io_swap(x)
Definition: rijndael.c:73

◆ rijndael_set_key()

rijndael_ctx* rijndael_set_key ( rijndael_ctx ,
const u4byte ,
const u4byte  ,
int   
)

Definition at line 305 of file rijndael.c.

References _rijndael_ctx::d_key, _rijndael_ctx::decrypt, _rijndael_ctx::e_key, gen_tabs(), i, imix_col, io_swap, _rijndael_ctx::k_len, loop4, loop6, loop8, and tab_gen.

Referenced by aes_set_key().

307 {
308  u4byte i,
309  t,
310  u,
311  v,
312  w;
313  u4byte *e_key = ctx->e_key;
314  u4byte *d_key = ctx->d_key;
315 
316  ctx->decrypt = !encrypt;
317 
318  if (!tab_gen)
319  gen_tabs();
320 
321  ctx->k_len = (key_len + 31) / 32;
322 
323  e_key[0] = io_swap(in_key[0]);
324  e_key[1] = io_swap(in_key[1]);
325  e_key[2] = io_swap(in_key[2]);
326  e_key[3] = io_swap(in_key[3]);
327 
328  switch (ctx->k_len)
329  {
330  case 4:
331  t = e_key[3];
332  for (i = 0; i < 10; ++i)
333  loop4(i);
334  break;
335 
336  case 6:
337  e_key[4] = io_swap(in_key[4]);
338  t = e_key[5] = io_swap(in_key[5]);
339  for (i = 0; i < 8; ++i)
340  loop6(i);
341  break;
342 
343  case 8:
344  e_key[4] = io_swap(in_key[4]);
345  e_key[5] = io_swap(in_key[5]);
346  e_key[6] = io_swap(in_key[6]);
347  t = e_key[7] = io_swap(in_key[7]);
348  for (i = 0; i < 7; ++i)
349  loop8(i);
350  break;
351  }
352 
353  if (!encrypt)
354  {
355  d_key[0] = e_key[0];
356  d_key[1] = e_key[1];
357  d_key[2] = e_key[2];
358  d_key[3] = e_key[3];
359 
360  for (i = 4; i < 4 * ctx->k_len + 24; ++i)
361  imix_col(d_key[i], e_key[i]);
362  }
363 
364  return ctx;
365 }
#define loop6(i)
Definition: rijndael.c:281
#define loop4(i)
Definition: rijndael.c:273
#define imix_col(y, x)
Definition: rijndael.c:259
uint32 u4byte
Definition: rijndael.h:27
#define io_swap(x)
Definition: rijndael.c:73
#define tab_gen
Definition: rijndael.c:83
#define loop8(i)
Definition: rijndael.c:291
static void gen_tabs(void)
Definition: rijndael.c:157
int i