libbtc
bitcoinclibrary
Data Structures | Macros | Typedefs | Functions
aes.h File Reference

Go to the source code of this file.

Data Structures

struct  aes_context
 

Macros

#define AES_ENC_PREKEYED   /* AES encryption with a precomputed key schedule */
 
#define AES_DEC_PREKEYED   /* AES decryption with a precomputed key schedule */
 
#define N_ROW   4
 
#define N_COL   4
 
#define N_BLOCK   (N_ROW * N_COL)
 
#define N_MAX_ROUNDS   14
 

Typedefs

typedef unsigned char uint_8t
 
typedef uint_8t return_type
 
typedef uint_8t length_type
 

Functions

return_type aes_set_key (const unsigned char key[], length_type keylen, aes_context ctx[1])
 
return_type aes_encrypt (const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1])
 
return_type aes_cbc_encrypt (const unsigned char *in, unsigned char *out, int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1])
 
return_type aes_decrypt (const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1])
 
return_type aes_cbc_decrypt (const unsigned char *in, unsigned char *out, int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1])
 

Macro Definition Documentation

#define AES_DEC_PREKEYED   /* AES decryption with a precomputed key schedule */

Definition at line 43 of file aes.h.

#define AES_ENC_PREKEYED   /* AES encryption with a precomputed key schedule */

Definition at line 40 of file aes.h.

#define N_BLOCK   (N_ROW * N_COL)
#define N_COL   4

Definition at line 59 of file aes.h.

#define N_MAX_ROUNDS   14

Definition at line 61 of file aes.h.

#define N_ROW   4

Definition at line 58 of file aes.h.

Typedef Documentation

Definition at line 71 of file aes.h.

Definition at line 65 of file aes.h.

typedef unsigned char uint_8t

Definition at line 63 of file aes.h.

Function Documentation

return_type aes_cbc_decrypt ( const unsigned char *  in,
unsigned char *  out,
int  n_block,
unsigned char  iv[N_BLOCK],
const aes_context  ctx[1] 
)

Definition at line 586 of file aes.c.

References aes_decrypt(), N_BLOCK, and xor_block().

587 {
588  while (n_block--) {
589  uint_8t tmp[N_BLOCK];
590 
591  memcpy(tmp, in, N_BLOCK);
592  if (aes_decrypt(in, out, ctx) != EXIT_SUCCESS) {
593  return EXIT_FAILURE;
594  }
595  xor_block(out, iv);
596  memcpy(iv, tmp, N_BLOCK);
597  in += N_BLOCK;
598  out += N_BLOCK;
599  }
600  return EXIT_SUCCESS;
601 }
return_type aes_decrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1])
Definition: aes.c:557
unsigned char uint_8t
Definition: aes.h:63
#define N_BLOCK
Definition: aes.h:60
static void xor_block(void *d, const void *s)
Definition: aes.c:265
return_type aes_cbc_encrypt ( const unsigned char *  in,
unsigned char *  out,
int  n_block,
unsigned char  iv[N_BLOCK],
const aes_context  ctx[1] 
)

Definition at line 537 of file aes.c.

References aes_encrypt(), N_BLOCK, and xor_block().

538 {
539  while (n_block--) {
540  xor_block(iv, in);
541  if (aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS) {
542  return EXIT_FAILURE;
543  }
544  memcpy(out, iv, N_BLOCK);
545  in += N_BLOCK;
546  out += N_BLOCK;
547  }
548  return EXIT_SUCCESS;
549 }
#define N_BLOCK
Definition: aes.h:60
static void xor_block(void *d, const void *s)
Definition: aes.c:265
return_type aes_encrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1])
Definition: aes.c:508
return_type aes_decrypt ( const unsigned char  in[N_BLOCK],
unsigned char  out[N_BLOCK],
const aes_context  ctx[1] 
)

Definition at line 557 of file aes.c.

References add_round_key(), copy_and_key(), inv_mix_sub_columns(), inv_shift_sub_rows(), aes_context::ksch, N_BLOCK, and aes_context::rnd.

Referenced by aes_cbc_decrypt().

558 {
559  if (ctx->rnd) {
560  uint_8t s1[N_BLOCK], r;
561  copy_and_key(s1, in, ctx->ksch + ctx->rnd * N_BLOCK);
562  inv_shift_sub_rows(s1);
563 
564  for (r = ctx->rnd; --r;)
565 #if defined(VERSION_1)
566  {
567  add_round_key(s1, ctx->ksch + r * N_BLOCK);
569  }
570 #else
571  {
572  uint_8t s2[N_BLOCK];
573  copy_and_key(s2, s1, ctx->ksch + r * N_BLOCK);
574  inv_mix_sub_columns(s1, s2);
575  }
576 #endif
577  copy_and_key(out, s1, ctx->ksch);
578  } else {
579  return -1;
580  }
581  return 0;
582 }
uint_8t rnd
Definition: aes.h:76
unsigned char uint_8t
Definition: aes.h:63
static void inv_shift_sub_rows(uint_8t st[N_BLOCK])
Definition: aes.c:356
static void copy_and_key(void *d, const void *s, const void *k)
Definition: aes.c:292
static void inv_mix_sub_columns(uint_8t dt[N_BLOCK])
Definition: aes.c:416
#define N_BLOCK
Definition: aes.h:60
uint_8t ksch[(N_MAX_ROUNDS+1)*N_BLOCK]
Definition: aes.h:75
static void add_round_key(uint_8t d[N_BLOCK], const uint_8t k[N_BLOCK])
Definition: aes.c:322
return_type aes_encrypt ( const unsigned char  in[N_BLOCK],
unsigned char  out[N_BLOCK],
const aes_context  ctx[1] 
)

Definition at line 508 of file aes.c.

References add_round_key(), copy_and_key(), aes_context::ksch, mix_sub_columns(), N_BLOCK, aes_context::rnd, shift_sub_rows(), and VERSION_1.

Referenced by aes_cbc_encrypt().

509 {
510  if (ctx->rnd) {
511  uint_8t s1[N_BLOCK], r;
512  copy_and_key(s1, in, ctx->ksch);
513 
514  for (r = 1; r < ctx->rnd; ++r)
515 #if defined(VERSION_1)
516  {
517  mix_sub_columns(s1);
518  add_round_key(s1, ctx->ksch + r * N_BLOCK);
519  }
520 #else
521  {
522  uint_8t s2[N_BLOCK];
523  mix_sub_columns(s2, s1);
524  copy_and_key(s1, s2, ctx->ksch + r * N_BLOCK);
525  }
526 #endif
527  shift_sub_rows(s1);
528  copy_and_key(out, s1, ctx->ksch + r * N_BLOCK);
529  } else {
530  return -1;
531  }
532  return 0;
533 }
static void shift_sub_rows(uint_8t st[N_BLOCK])
Definition: aes.c:327
uint_8t rnd
Definition: aes.h:76
static void mix_sub_columns(uint_8t dt[N_BLOCK])
Definition: aes.c:386
#define VERSION_1
Definition: aes.c:68
unsigned char uint_8t
Definition: aes.h:63
static void copy_and_key(void *d, const void *s, const void *k)
Definition: aes.c:292
#define N_BLOCK
Definition: aes.h:60
uint_8t ksch[(N_MAX_ROUNDS+1)*N_BLOCK]
Definition: aes.h:75
static void add_round_key(uint_8t d[N_BLOCK], const uint_8t k[N_BLOCK])
Definition: aes.c:322
return_type aes_set_key ( const unsigned char  key[],
length_type  keylen,
aes_context  ctx[1] 
)

Definition at line 449 of file aes.c.

References block_copy_nn, f2, aes_context::ksch, aes_context::rnd, and s_box.

450 {
451  uint_8t cc, rc, hi;
452 
453  switch (keylen) {
454  case 16:
455  case 128:
456  keylen = 16;
457  break;
458  case 24:
459  case 192:
460  keylen = 24;
461  break;
462  case 32:
463  //case 256:
464  keylen = 32;
465  break;
466  default:
467  ctx->rnd = 0;
468  return -1;
469  }
470  block_copy_nn(ctx->ksch, key, keylen);
471  hi = (keylen + 28) << 2;
472  ctx->rnd = (hi >> 4) - 1;
473  for (cc = keylen, rc = 1; cc < hi; cc += 4) {
474  uint_8t tt, t0, t1, t2, t3;
475 
476  t0 = ctx->ksch[cc - 4];
477  t1 = ctx->ksch[cc - 3];
478  t2 = ctx->ksch[cc - 2];
479  t3 = ctx->ksch[cc - 1];
480  if (cc % keylen == 0) {
481  tt = t0;
482  t0 = s_box(t1) ^ rc;
483  t1 = s_box(t2);
484  t2 = s_box(t3);
485  t3 = s_box(tt);
486  rc = f2(rc);
487  } else if (keylen > 24 && cc % keylen == 16) {
488  t0 = s_box(t0);
489  t1 = s_box(t1);
490  t2 = s_box(t2);
491  t3 = s_box(t3);
492  }
493  tt = cc - keylen;
494  ctx->ksch[cc + 0] = ctx->ksch[tt + 0] ^ t0;
495  ctx->ksch[cc + 1] = ctx->ksch[tt + 1] ^ t1;
496  ctx->ksch[cc + 2] = ctx->ksch[tt + 2] ^ t2;
497  ctx->ksch[cc + 3] = ctx->ksch[tt + 3] ^ t3;
498  }
499  return 0;
500 }
#define s_box(x)
Definition: aes.c:124
uint_8t rnd
Definition: aes.h:76
#define block_copy_nn(d, s, l)
Definition: aes.c:219
#define f2(x)
Definition: aes.c:85
unsigned char uint_8t
Definition: aes.h:63
uint_8t ksch[(N_MAX_ROUNDS+1)*N_BLOCK]
Definition: aes.h:75