Rizin
unix-like reverse engineering framework and cli tools
crypto_aes.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_lib.h>
5 #include <rz_crypto.h>
6 #include <rz_util.h>
7 #include <aes.h>
8 
9 static void encryptaes(struct aes_ctx *ctx, ut8 *in, ut8 *out) {
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 }
25 
26 static void decryptaes(struct aes_ctx *ctx, ut8 *in, ut8 *out) {
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 }
42 
43 static bool aes_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
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 }
80 
81 static int aes_get_key_size(RzCrypto *cry) {
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 }
87 
88 static bool aes_use(const char *algo) {
89  return !strcmp(algo, "aes-ecb");
90 }
91 
92 static bool update(RzCrypto *cry, const ut8 *buf, int len) {
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 }
142 
143 static bool final(RzCrypto *cry, const ut8 *buf, int len) {
144  return update(cry, buf, len);
145 }
146 
147 static bool aes_ecb_init(RzCrypto *cry) {
148  rz_return_val_if_fail(cry, false);
149 
150  cry->user = RZ_NEW0(struct aes_ctx);
151  return cry->user != NULL;
152 }
153 
154 static bool aes_ecb_fini(RzCrypto *cry) {
155  rz_return_val_if_fail(cry, false);
156 
157  free(cry->user);
158  return true;
159 }
160 
162  .name = "aes-ecb",
163  .author = "Nettle project (algorithm implementation), pancake (plugin)",
164  .license = "LGPL3",
165  .set_key = aes_set_key,
166  .get_key_size = aes_get_key_size,
167  .use = aes_use,
168  .update = update,
169  .final = final,
170  .init = aes_ecb_init,
171  .fini = aes_ecb_fini,
172 };
173 
174 #ifndef RZ_PLUGIN_INCORE
177  .data = &rz_crypto_plugin_aes,
178  .version = RZ_VERSION
179 };
180 #endif
size_t len
Definition: 6502dis.c:15
void aes128_encrypt(const struct aes128_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
void aes128_decrypt(const struct aes128_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
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)
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)
#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)
void aes256_decrypt(const struct aes256_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
#define AES128_KEY_SIZE
Definition: aes.h:70
void aes192_decrypt(const struct aes192_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
void aes192_set_encrypt_key(struct aes192_ctx *ctx, const uint8_t *key)
lzma_index ** i
Definition: index.h:629
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 RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto.c:175
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 update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_aes.c:92
RZ_API RzLibStruct rizin_plugin
Definition: crypto_aes.c:175
static void encryptaes(struct aes_ctx *ctx, ut8 *in, ut8 *out)
Definition: crypto_aes.c:9
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 void decryptaes(struct aes_ctx *ctx, ut8 *in, ut8 *out)
Definition: crypto_aes.c:26
RzCryptoPlugin rz_crypto_plugin_aes
Definition: crypto_aes.c:161
static bool aes_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_aes.c:43
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
voidpf void * buf
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
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
@ RZ_CRYPTO_DIR_ENCRYPT
Definition: rz_crypto.h:23
@ RZ_LIB_TYPE_CRYPTO
Definition: rz_lib.h:81
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
Definition: aes.h:151
struct aes128_ctx ctx128
Definition: aes.h:154
union aes_ctx::@422 u
struct aes256_ctx ctx256
Definition: aes.h:156
unsigned key_size
Definition: aes.h:152
struct aes192_ctx ctx192
Definition: aes.h:155
const char * name
Definition: rz_crypto.h:41
void * user
Definition: rz_crypto.h:36
uint64_t blocks
Definition: list.c:104
static st64 delta
Definition: vmenus.c:2425
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static unsigned char * obuf
Definition: z80asm.c:36
#define AES_BLOCK_SIZE
Definition: zipint.h:77