Rizin
unix-like reverse engineering framework and cli tools
crypto_aes.c File Reference
#include <rz_lib.h>
#include <rz_crypto.h>
#include <rz_util.h>
#include <aes.h>

Go to the source code of this file.

Functions

static void encryptaes (struct aes_ctx *ctx, ut8 *in, ut8 *out)
 
static void decryptaes (struct aes_ctx *ctx, ut8 *in, ut8 *out)
 
static bool aes_set_key (RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
 
static int aes_get_key_size (RzCrypto *cry)
 
static bool aes_use (const char *algo)
 
static bool update (RzCrypto *cry, const ut8 *buf, int len)
 
static bool final (RzCrypto *cry, const ut8 *buf, int len)
 
static bool aes_ecb_init (RzCrypto *cry)
 
static bool aes_ecb_fini (RzCrypto *cry)
 

Variables

RzCryptoPlugin rz_crypto_plugin_aes
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ aes_ecb_fini()

static bool aes_ecb_fini ( RzCrypto cry)
static

Definition at line 154 of file crypto_aes.c.

154  {
155  rz_return_val_if_fail(cry, false);
156 
157  free(cry->user);
158  return true;
159 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
void * user
Definition: rz_crypto.h:36

References free(), rz_return_val_if_fail, and rz_crypto_t::user.

◆ aes_ecb_init()

static bool aes_ecb_init ( RzCrypto cry)
static

Definition at line 147 of file crypto_aes.c.

147  {
148  rz_return_val_if_fail(cry, false);
149 
150  cry->user = RZ_NEW0(struct aes_ctx);
151  return cry->user != NULL;
152 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: aes.h:151

References NULL, RZ_NEW0, rz_return_val_if_fail, and rz_crypto_t::user.

◆ aes_get_key_size()

static int aes_get_key_size ( RzCrypto cry)
static

Definition at line 81 of file crypto_aes.c.

81  {
82  rz_return_val_if_fail(cry->user, 0);
83  struct aes_ctx *st = (struct aes_ctx *)cry->user;
84 
85  return st->key_size;
86 }
unsigned key_size
Definition: aes.h:152

References aes_ctx::key_size, rz_return_val_if_fail, and rz_crypto_t::user.

◆ aes_set_key()

static bool aes_set_key ( RzCrypto cry,
const ut8 key,
int  keylen,
int  mode,
int  direction 
)
static

Definition at line 43 of file crypto_aes.c.

43  {
44  rz_return_val_if_fail(cry->user && key, false);
45  struct aes_ctx *st = (struct aes_ctx *)cry->user;
46 
47  if (!(keylen == AES128_KEY_SIZE || keylen == AES192_KEY_SIZE || keylen == AES256_KEY_SIZE)) {
48  return false;
49  }
50  st->key_size = keylen;
51  switch (keylen) {
52  case AES128_KEY_SIZE:
53  if (direction == RZ_CRYPTO_DIR_ENCRYPT) {
55  } else {
57  }
58  break;
59  case AES192_KEY_SIZE:
60  if (direction == RZ_CRYPTO_DIR_ENCRYPT) {
62  } else {
64  }
65  break;
66  case AES256_KEY_SIZE:
67  if (direction == RZ_CRYPTO_DIR_ENCRYPT) {
69  } else {
71  }
72  break;
73  default:
75  break;
76  }
77  cry->dir = direction;
78  return true;
79 }
void aes128_set_encrypt_key(struct aes128_ctx *ctx, const uint8_t *key)
#define AES192_KEY_SIZE
Definition: aes.h:71
void aes128_set_decrypt_key(struct aes128_ctx *ctx, const uint8_t *key)
#define AES256_KEY_SIZE
Definition: aes.h:72
void aes256_set_decrypt_key(struct aes256_ctx *ctx, const uint8_t *key)
void aes256_set_encrypt_key(struct aes256_ctx *ctx, const uint8_t *key)
void aes192_set_decrypt_key(struct aes192_ctx *ctx, const uint8_t *key)
#define AES128_KEY_SIZE
Definition: aes.h:70
void aes192_set_encrypt_key(struct aes192_ctx *ctx, const uint8_t *key)
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
#define rz_warn_if_reached()
Definition: rz_assert.h:29
@ RZ_CRYPTO_DIR_ENCRYPT
Definition: rz_crypto.h:23
struct aes128_ctx ctx128
Definition: aes.h:154
union aes_ctx::@422 u
struct aes256_ctx ctx256
Definition: aes.h:156
struct aes192_ctx ctx192
Definition: aes.h:155
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References AES128_KEY_SIZE, aes128_set_decrypt_key(), aes128_set_encrypt_key(), AES192_KEY_SIZE, aes192_set_decrypt_key(), aes192_set_encrypt_key(), AES256_KEY_SIZE, aes256_set_decrypt_key(), aes256_set_encrypt_key(), aes_ctx::ctx128, aes_ctx::ctx192, aes_ctx::ctx256, rz_crypto_t::dir, if(), key, aes_ctx::key_size, RZ_CRYPTO_DIR_ENCRYPT, rz_return_val_if_fail, rz_warn_if_reached, aes_ctx::u, and rz_crypto_t::user.

◆ aes_use()

static bool aes_use ( const char *  algo)
static

Definition at line 88 of file crypto_aes.c.

88  {
89  return !strcmp(algo, "aes-ecb");
90 }

◆ decryptaes()

static void decryptaes ( struct aes_ctx ctx,
ut8 in,
ut8 out 
)
static

Definition at line 26 of file crypto_aes.c.

26  {
27  switch (ctx->key_size) {
28  case AES128_KEY_SIZE:
29  aes128_decrypt(&ctx->u.ctx128, AES_BLOCK_SIZE, out, in);
30  break;
31  case AES192_KEY_SIZE:
32  aes192_decrypt(&ctx->u.ctx192, AES_BLOCK_SIZE, out, in);
33  break;
34  case AES256_KEY_SIZE:
35  aes256_decrypt(&ctx->u.ctx256, AES_BLOCK_SIZE, out, in);
36  break;
37  default:
39  break;
40  }
41 }
void aes128_decrypt(const struct aes128_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
void aes256_decrypt(const struct aes256_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
void aes192_decrypt(const struct aes192_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define AES_BLOCK_SIZE
Definition: zipint.h:77

References aes128_decrypt(), AES128_KEY_SIZE, aes192_decrypt(), AES192_KEY_SIZE, aes256_decrypt(), AES256_KEY_SIZE, AES_BLOCK_SIZE, in, out, and rz_warn_if_reached.

Referenced by update().

◆ encryptaes()

static void encryptaes ( struct aes_ctx ctx,
ut8 in,
ut8 out 
)
static

Definition at line 9 of file crypto_aes.c.

9  {
10  switch (ctx->key_size) {
11  case AES128_KEY_SIZE:
12  aes128_encrypt(&ctx->u.ctx128, AES_BLOCK_SIZE, out, in);
13  break;
14  case AES192_KEY_SIZE:
15  aes192_encrypt(&ctx->u.ctx192, AES_BLOCK_SIZE, out, in);
16  break;
17  case AES256_KEY_SIZE:
18  aes256_encrypt(&ctx->u.ctx256, AES_BLOCK_SIZE, out, in);
19  break;
20  default:
22  break;
23  }
24 }
void aes128_encrypt(const struct aes128_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
void aes192_encrypt(const struct aes192_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
void aes256_encrypt(const struct aes256_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)

References aes128_encrypt(), AES128_KEY_SIZE, aes192_encrypt(), AES192_KEY_SIZE, aes256_encrypt(), AES256_KEY_SIZE, AES_BLOCK_SIZE, in, out, and rz_warn_if_reached.

Referenced by update().

◆ final()

static bool final ( RzCrypto cry,
const ut8 buf,
int  len 
)
static

Definition at line 143 of file crypto_aes.c.

143  {
144  return update(cry, buf, len);
145 }
size_t len
Definition: 6502dis.c:15
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_aes.c:92
voidpf void * buf
Definition: ioapi.h:138

References len, and update().

◆ update()

static bool update ( RzCrypto cry,
const ut8 buf,
int  len 
)
static

Definition at line 92 of file crypto_aes.c.

92  {
93  rz_return_val_if_fail(cry->user, 0);
94  struct aes_ctx *st = (struct aes_ctx *)cry->user;
95 
96  if (len < 1) {
97  return false;
98  }
99 
100  // Pad to the block size, do not append dummy block
101  const int diff = (AES_BLOCK_SIZE - (len % AES_BLOCK_SIZE)) % AES_BLOCK_SIZE;
102  const int size = len + diff;
103  const int blocks = size / AES_BLOCK_SIZE;
104  int i;
105 
106  ut8 *const obuf = calloc(1, size);
107  if (!obuf) {
108  return false;
109  }
110  ut8 *const ibuf = calloc(1, size);
111  if (!ibuf) {
112  free(obuf);
113  return false;
114  }
115 
116  memset(ibuf, 0, size);
117  memcpy(ibuf, buf, len);
118  // Padding should start like 100000...
119  if (diff) {
120  ibuf[len] = 8; // 0b1000;
121  }
122 
123  if (cry->dir == RZ_CRYPTO_DIR_ENCRYPT) {
124  for (i = 0; i < blocks; i++) {
125  const int delta = AES_BLOCK_SIZE * i;
126  encryptaes(st, ibuf + delta, obuf + delta);
127  }
128  } else {
129  for (i = 0; i < blocks; i++) {
130  const int delta = AES_BLOCK_SIZE * i;
131  decryptaes(st, ibuf + delta, obuf + delta);
132  }
133  }
134 
135  // printf("%128s\n", obuf);
136 
137  rz_crypto_append(cry, obuf, size);
138  free(obuf);
139  free(ibuf);
140  return true;
141 }
lzma_index ** i
Definition: index.h:629
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto.c:175
static void encryptaes(struct aes_ctx *ctx, ut8 *in, ut8 *out)
Definition: crypto_aes.c:9
static void decryptaes(struct aes_ctx *ctx, ut8 *in, ut8 *out)
Definition: crypto_aes.c:26
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
uint64_t blocks
Definition: list.c:104
static st64 delta
Definition: vmenus.c:2425
static unsigned char * obuf
Definition: z80asm.c:36

References AES_BLOCK_SIZE, blocks, calloc(), decryptaes(), delta, rz_crypto_t::dir, encryptaes(), free(), i, if(), len, memcpy(), memset(), obuf, rz_crypto_append(), RZ_CRYPTO_DIR_ENCRYPT, rz_return_val_if_fail, and rz_crypto_t::user.

Referenced by __print_default_cb(), __print_disasmsummary_cb(), __print_graph_cb(), delete_element(), final(), rz_skiplist_insert(), sdb_ht_insert_kvp(), and sdb_ht_internal_insert().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.version = RZ_VERSION
}
RzCryptoPlugin rz_crypto_plugin_aes
Definition: crypto_aes.c:161
@ RZ_LIB_TYPE_CRYPTO
Definition: rz_lib.h:81
#define RZ_VERSION
Definition: rz_version.h:8

Definition at line 175 of file crypto_aes.c.

◆ rz_crypto_plugin_aes

RzCryptoPlugin rz_crypto_plugin_aes
Initial value:
= {
.name = "aes-ecb",
.author = "Nettle project (algorithm implementation), pancake (plugin)",
.license = "LGPL3",
.set_key = aes_set_key,
.get_key_size = aes_get_key_size,
.use = aes_use,
.update = update,
.final = final,
.init = aes_ecb_init,
.fini = aes_ecb_fini,
}
static bool aes_use(const char *algo)
Definition: crypto_aes.c:88
static bool aes_ecb_fini(RzCrypto *cry)
Definition: crypto_aes.c:154
static bool aes_ecb_init(RzCrypto *cry)
Definition: crypto_aes.c:147
static int aes_get_key_size(RzCrypto *cry)
Definition: crypto_aes.c:81
static bool aes_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_aes.c:43

Definition at line 161 of file crypto_aes.c.