Rizin
unix-like reverse engineering framework and cli tools
rz_crypto.h File Reference
#include "rz_types.h"
#include "rz_list.h"
#include "rz_crypto/rz_des.h"

Go to the source code of this file.

Classes

struct  rz_crypto_t
 
struct  rz_crypto_plugin_t
 

Macros

#define RZ_CRYPTO_NONE   0ULL
 
#define RZ_CRYPTO_RC2   1ULL
 
#define RZ_CRYPTO_RC4   1ULL << 1
 
#define RZ_CRYPTO_RC6   1ULL << 2
 
#define RZ_CRYPTO_AES_ECB   1ULL << 3
 
#define RZ_CRYPTO_AES_CBC   1ULL << 4
 
#define RZ_CRYPTO_ROR   1ULL << 5
 
#define RZ_CRYPTO_ROL   1ULL << 6
 
#define RZ_CRYPTO_ROT   1ULL << 7
 
#define RZ_CRYPTO_BLOWFISH   1ULL << 8
 
#define RZ_CRYPTO_CPS2   1ULL << 9
 
#define RZ_CRYPTO_DES_ECB   1ULL << 10
 
#define RZ_CRYPTO_XOR   1ULL << 11
 
#define RZ_CRYPTO_SERPENT   1ULL << 12
 
#define RZ_CRYPTO_ALL   0xFFFF
 
#define RZ_CODEC_NONE   0ULL
 
#define RZ_CODEC_B64   1ULL
 
#define RZ_CODEC_B91   1ULL << 1
 
#define RZ_CODEC_PUNYCODE   1ULL << 2
 
#define RZ_CODEC_ALL   0xFFFF
 

Typedefs

typedef struct rz_crypto_t RzCrypto
 
typedef struct rz_crypto_plugin_t RzCryptoPlugin
 
typedef ut64 RzCryptoSelector
 

Enumerations

enum  { RZ_CRYPTO_MODE_ECB , RZ_CRYPTO_MODE_CBC , RZ_CRYPTO_MODE_OFB , RZ_CRYPTO_MODE_CFB }
 
enum  { RZ_CRYPTO_DIR_ENCRYPT = 0 , RZ_CRYPTO_DIR_DECRYPT }
 

Functions

 RZ_LIB_VERSION_HEADER (rz_crypto)
 
RZ_API int rz_crypto_add (RzCrypto *cry, RzCryptoPlugin *h)
 
RZ_API RzCryptorz_crypto_new (void)
 
RZ_API void rz_crypto_free (RzCrypto *cry)
 
RZ_API bool rz_crypto_use (RzCrypto *cry, const char *algo)
 
RZ_API bool rz_crypto_set_key (RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
 
RZ_API bool rz_crypto_set_iv (RzCrypto *cry, const ut8 *iv, int ivlen)
 
RZ_API int rz_crypto_update (RzCrypto *cry, const ut8 *buf, int len)
 
RZ_API int rz_crypto_final (RzCrypto *cry, const ut8 *buf, int len)
 
RZ_API int rz_crypto_append (RzCrypto *cry, const ut8 *buf, int len)
 
RZ_API const ut8rz_crypto_get_output (RzCrypto *cry, int *size)
 
RZ_API const char * rz_crypto_name (const RzCryptoSelector bit)
 
RZ_API const char * rz_crypto_codec_name (const RzCryptoSelector bit)
 
RZ_API const RzCryptoPluginrz_crypto_plugin_by_index (size_t index)
 

Variables

RzCryptoPlugin rz_crypto_plugin_aes
 
RzCryptoPlugin rz_crypto_plugin_des
 
RzCryptoPlugin rz_crypto_plugin_rc4
 
RzCryptoPlugin rz_crypto_plugin_xor
 
RzCryptoPlugin rz_crypto_plugin_blowfish
 
RzCryptoPlugin rz_crypto_plugin_rc2
 
RzCryptoPlugin rz_crypto_plugin_rot
 
RzCryptoPlugin rz_crypto_plugin_rol
 
RzCryptoPlugin rz_crypto_plugin_ror
 
RzCryptoPlugin rz_crypto_plugin_base64
 
RzCryptoPlugin rz_crypto_plugin_base91
 
RzCryptoPlugin rz_crypto_plugin_aes_cbc
 
RzCryptoPlugin rz_crypto_plugin_punycode
 
RzCryptoPlugin rz_crypto_plugin_rc6
 
RzCryptoPlugin rz_crypto_plugin_cps2
 
RzCryptoPlugin rz_crypto_plugin_serpent
 

Macro Definition Documentation

◆ RZ_CODEC_ALL

#define RZ_CODEC_ALL   0xFFFF

Definition at line 110 of file rz_crypto.h.

◆ RZ_CODEC_B64

#define RZ_CODEC_B64   1ULL

Definition at line 107 of file rz_crypto.h.

◆ RZ_CODEC_B91

#define RZ_CODEC_B91   1ULL << 1

Definition at line 108 of file rz_crypto.h.

◆ RZ_CODEC_NONE

#define RZ_CODEC_NONE   0ULL

Definition at line 106 of file rz_crypto.h.

◆ RZ_CODEC_PUNYCODE

#define RZ_CODEC_PUNYCODE   1ULL << 2

Definition at line 109 of file rz_crypto.h.

◆ RZ_CRYPTO_AES_CBC

#define RZ_CRYPTO_AES_CBC   1ULL << 4

Definition at line 95 of file rz_crypto.h.

◆ RZ_CRYPTO_AES_ECB

#define RZ_CRYPTO_AES_ECB   1ULL << 3

Definition at line 94 of file rz_crypto.h.

◆ RZ_CRYPTO_ALL

#define RZ_CRYPTO_ALL   0xFFFF

Definition at line 104 of file rz_crypto.h.

◆ RZ_CRYPTO_BLOWFISH

#define RZ_CRYPTO_BLOWFISH   1ULL << 8

Definition at line 99 of file rz_crypto.h.

◆ RZ_CRYPTO_CPS2

#define RZ_CRYPTO_CPS2   1ULL << 9

Definition at line 100 of file rz_crypto.h.

◆ RZ_CRYPTO_DES_ECB

#define RZ_CRYPTO_DES_ECB   1ULL << 10

Definition at line 101 of file rz_crypto.h.

◆ RZ_CRYPTO_NONE

#define RZ_CRYPTO_NONE   0ULL

Definition at line 90 of file rz_crypto.h.

◆ RZ_CRYPTO_RC2

#define RZ_CRYPTO_RC2   1ULL

Definition at line 91 of file rz_crypto.h.

◆ RZ_CRYPTO_RC4

#define RZ_CRYPTO_RC4   1ULL << 1

Definition at line 92 of file rz_crypto.h.

◆ RZ_CRYPTO_RC6

#define RZ_CRYPTO_RC6   1ULL << 2

Definition at line 93 of file rz_crypto.h.

◆ RZ_CRYPTO_ROL

#define RZ_CRYPTO_ROL   1ULL << 6

Definition at line 97 of file rz_crypto.h.

◆ RZ_CRYPTO_ROR

#define RZ_CRYPTO_ROR   1ULL << 5

Definition at line 96 of file rz_crypto.h.

◆ RZ_CRYPTO_ROT

#define RZ_CRYPTO_ROT   1ULL << 7

Definition at line 98 of file rz_crypto.h.

◆ RZ_CRYPTO_SERPENT

#define RZ_CRYPTO_SERPENT   1ULL << 12

Definition at line 103 of file rz_crypto.h.

◆ RZ_CRYPTO_XOR

#define RZ_CRYPTO_XOR   1ULL << 11

Definition at line 102 of file rz_crypto.h.

Typedef Documentation

◆ RzCrypto

typedef struct rz_crypto_t RzCrypto

◆ RzCryptoPlugin

◆ RzCryptoSelector

Definition at line 54 of file rz_crypto.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RZ_CRYPTO_MODE_ECB 
RZ_CRYPTO_MODE_CBC 
RZ_CRYPTO_MODE_OFB 
RZ_CRYPTO_MODE_CFB 

Definition at line 14 of file rz_crypto.h.

14  {
19 };
@ RZ_CRYPTO_MODE_CFB
Definition: rz_crypto.h:18
@ RZ_CRYPTO_MODE_OFB
Definition: rz_crypto.h:17
@ RZ_CRYPTO_MODE_ECB
Definition: rz_crypto.h:15
@ RZ_CRYPTO_MODE_CBC
Definition: rz_crypto.h:16

◆ anonymous enum

anonymous enum
Enumerator
RZ_CRYPTO_DIR_ENCRYPT 
RZ_CRYPTO_DIR_DECRYPT 

Definition at line 22 of file rz_crypto.h.

22  {
25 };
@ RZ_CRYPTO_DIR_DECRYPT
Definition: rz_crypto.h:24
@ RZ_CRYPTO_DIR_ENCRYPT
Definition: rz_crypto.h:23

Function Documentation

◆ rz_crypto_add()

RZ_API int rz_crypto_add ( RzCrypto cry,
RzCryptoPlugin h 
)

Definition at line 72 of file crypto.c.

72  {
73  // add a check ?
74  rz_list_append(cry->plugins, h);
75  return true;
76 }
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define h(i)
Definition: sha256.c:48
RzList * plugins
Definition: rz_crypto.h:37

References h, rz_crypto_t::plugins, and rz_list_append().

Referenced by rz_crypto_new().

◆ rz_crypto_append()

RZ_API int rz_crypto_append ( RzCrypto cry,
const ut8 buf,
int  len 
)

Definition at line 175 of file crypto.c.

175  {
176  if (!cry || !buf) {
177  return -1;
178  }
179  if (cry->output_len + len > cry->output_size) {
180  cry->output_size += 4096 + len;
181  cry->output = realloc(cry->output, cry->output_size);
182  }
183  if (!cry->output) {
185  cry->output_size = 0;
186  return 0;
187  }
188  memcpy(cry->output + cry->output_len, buf, len);
189  cry->output_len += len;
190  return cry->output_len;
191 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
#define rz_warn_if_reached()
Definition: rz_assert.h:29
ut8 * output
Definition: rz_crypto.h:32
int output_len
Definition: rz_crypto.h:33
int output_size
Definition: rz_crypto.h:34

References len, memcpy(), rz_crypto_t::output, rz_crypto_t::output_len, rz_crypto_t::output_size, realloc(), and rz_warn_if_reached.

Referenced by update().

◆ rz_crypto_codec_name()

RZ_API const char* rz_crypto_codec_name ( const RzCryptoSelector  bit)

Definition at line 54 of file crypto.c.

54  {
55  size_t i;
56  for (i = 1; i < RZ_ARRAY_SIZE(codec_name_bytes); i++) {
57  if (bit == codec_name_bytes[i].bit) {
58  return codec_name_bytes[i].name;
59  }
60  }
61  return "";
62 }
lzma_index ** i
Definition: index.h:629
static const struct @228 codec_name_bytes[]
RzCryptoSelector bit
Definition: crypto.c:16
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300

References bit, codec_name_bytes, i, and RZ_ARRAY_SIZE.

◆ rz_crypto_final()

RZ_API int rz_crypto_final ( RzCrypto cry,
const ut8 buf,
int  len 
)

Definition at line 170 of file crypto.c.

170  {
171  return (cry && cry->h && cry->h->final) ? cry->h->final(cry, buf, len) : 0;
172 }
bool(* final)(RzCrypto *cry, const ut8 *buf, int len)
Definition: rz_crypto.h:48
struct rz_crypto_plugin_t * h
Definition: rz_crypto.h:28

References rz_crypto_plugin_t::final, rz_crypto_t::h, and len.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ rz_crypto_free()

RZ_API void rz_crypto_free ( RzCrypto cry)

Definition at line 116 of file crypto.c.

116  {
117  if (!cry) {
118  return;
119  }
120  if (cry->h && cry->h->fini && !cry->h->fini(cry)) {
121  RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name);
122  }
123  rz_list_free(cry->plugins);
124  free(cry->output);
125  free(cry->key);
126  free(cry->iv);
127  free(cry);
128 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
const char * name
Definition: rz_crypto.h:41
bool(* fini)(RzCrypto *cry)
Definition: rz_crypto.h:51
ut8 * iv
Definition: rz_crypto.h:30
ut8 * key
Definition: rz_crypto.h:29

References rz_crypto_plugin_t::fini, free(), rz_crypto_t::h, rz_crypto_t::iv, rz_crypto_t::key, rz_crypto_plugin_t::name, rz_crypto_t::output, rz_crypto_t::plugins, rz_list_free(), and RZ_LOG_ERROR.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), encrypt_or_decrypt_block(), and rz_crypto_new().

◆ rz_crypto_get_output()

RZ_API const ut8* rz_crypto_get_output ( RzCrypto cry,
int size 
)

Definition at line 193 of file crypto.c.

193  {
194  if (cry->output_size < 1 || !cry->output) {
195  if (size) {
196  *size = 0;
197  }
198  return NULL;
199  }
200  if (size) {
201  *size = cry->output_len;
202  }
203  return cry->output;
204 }
#define NULL
Definition: cris-opc.c:27
voidpf void uLong size
Definition: ioapi.h:138

References NULL, rz_crypto_t::output, rz_crypto_t::output_len, and rz_crypto_t::output_size.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ rz_crypto_name()

RZ_API const char* rz_crypto_name ( const RzCryptoSelector  bit)

Definition at line 44 of file crypto.c.

44  {
45  size_t i;
46  for (i = 1; i < RZ_ARRAY_SIZE(crypto_name_bytes); i++) {
47  if (bit == crypto_name_bytes[i].bit) {
48  return crypto_name_bytes[i].name;
49  }
50  }
51  return "";
52 }
static const struct @227 crypto_name_bytes[]

References bit, crypto_name_bytes, i, and RZ_ARRAY_SIZE.

◆ rz_crypto_new()

RZ_API RzCrypto* rz_crypto_new ( void  )

Definition at line 83 of file crypto.c.

83  {
84  RzCrypto *cry = RZ_NEW0(RzCrypto);
85  if (!cry) {
86  goto rz_crypto_new_bad;
87  }
88 
91  if (!cry->output) {
92  goto rz_crypto_new_bad;
93  }
94 
96  if (!cry->plugins) {
97  goto rz_crypto_new_bad;
98  }
99 
100  for (ut32 i = 0; i < RZ_ARRAY_SIZE(crypto_static_plugins); i++) {
102  if (!p) {
103  goto rz_crypto_new_bad;
104  }
106  rz_crypto_add(cry, p);
107  }
108  return cry;
109 
110 rz_crypto_new_bad:
111  RZ_LOG_ERROR("[!] crypto: failed to allocate\n");
112  rz_crypto_free(cry);
113  return NULL;
114 }
RZ_API int rz_crypto_add(RzCrypto *cry, RzCryptoPlugin *h)
Definition: crypto.c:72
#define RZ_CRYPTO_OUTPUT_SIZE
Definition: crypto.c:8
RZ_API void rz_crypto_free(RzCrypto *cry)
Definition: crypto.c:116
static RzCryptoPlugin * crypto_static_plugins[]
Definition: crypto.c:12
uint32_t ut32
void * p
Definition: libc.cpp:67
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
void * malloc(size_t size)
Definition: malloc.c:123
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_NEW0(x)
Definition: rz_types.h:284

References crypto_static_plugins, free(), i, malloc(), memcpy(), NULL, rz_crypto_t::output, rz_crypto_t::output_size, p, rz_crypto_t::plugins, RZ_ARRAY_SIZE, rz_crypto_add(), rz_crypto_free(), RZ_CRYPTO_OUTPUT_SIZE, rz_list_newf(), RZ_LOG_ERROR, and RZ_NEW0.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ rz_crypto_plugin_by_index()

RZ_API const RzCryptoPlugin* rz_crypto_plugin_by_index ( size_t  index)

Definition at line 64 of file crypto.c.

64  {
66  if (index >= size) {
67  return NULL;
68  }
69  return crypto_static_plugins[index];
70 }

References crypto_static_plugins, NULL, and RZ_ARRAY_SIZE.

Referenced by rz_hash_show_algorithms().

◆ rz_crypto_set_iv()

RZ_API bool rz_crypto_set_iv ( RzCrypto cry,
const ut8 iv,
int  ivlen 
)

Definition at line 161 of file crypto.c.

161  {
162  return (cry && cry->h && cry->h->set_iv) ? cry->h->set_iv(cry, iv, ivlen) : 0;
163 }
bool(* set_iv)(RzCrypto *cry, const ut8 *iv, int ivlen)
Definition: rz_crypto.h:45

References rz_crypto_t::h, and rz_crypto_plugin_t::set_iv.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ rz_crypto_set_key()

RZ_API bool rz_crypto_set_key ( RzCrypto cry,
const ut8 key,
int  keylen,
int  mode,
int  direction 
)

Definition at line 151 of file crypto.c.

151  {
152  if (keylen < 0) {
153  keylen = strlen((const char *)key);
154  }
155  if (!cry || !cry->h || !cry->h->set_key) {
156  return false;
157  }
158  return cry->h->set_key(cry, key, keylen, mode, direction);
159 }
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
const char int mode
Definition: ioapi.h:137
bool(* set_key)(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: rz_crypto.h:46

References rz_crypto_t::h, key, and rz_crypto_plugin_t::set_key.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ rz_crypto_update()

RZ_API int rz_crypto_update ( RzCrypto cry,
const ut8 buf,
int  len 
)

Definition at line 166 of file crypto.c.

166  {
167  return (cry && cry->h && cry->h->update) ? cry->h->update(cry, buf, len) : 0;
168 }
bool(* update)(RzCrypto *cry, const ut8 *buf, int len)
Definition: rz_crypto.h:47

References rz_crypto_t::h, len, and rz_crypto_plugin_t::update.

Referenced by calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ rz_crypto_use()

RZ_API bool rz_crypto_use ( RzCrypto cry,
const char *  algo 
)

Definition at line 130 of file crypto.c.

130  {
131  RzListIter *iter;
132  RzCryptoPlugin *h;
133  if (cry->h && cry->h->fini && !cry->h->fini(cry)) {
134  RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name);
135  }
136  rz_list_foreach (cry->plugins, iter, h) {
137  rz_warn_if_fail(h && h->use);
138  if (h && h->use(algo)) {
139  if (h->init && !h->init(cry)) {
140  RZ_LOG_ERROR("[!] crypto: error initializing '%s' plugin\n", cry->h->name);
141  return false;
142  }
143 
144  cry->h = h;
145  return true;
146  }
147  }
148  return false;
149 }
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35

References rz_crypto_plugin_t::fini, rz_crypto_t::h, h, rz_crypto_plugin_t::name, rz_crypto_t::plugins, RZ_LOG_ERROR, and rz_warn_if_fail.

Referenced by _decrypt(), _encrypt(), calculate_decrypt(), calculate_encrypt(), and encrypt_or_decrypt_block().

◆ RZ_LIB_VERSION_HEADER()

RZ_LIB_VERSION_HEADER ( rz_crypto  )

Variable Documentation

◆ rz_crypto_plugin_aes

RzCryptoPlugin rz_crypto_plugin_aes
extern

Definition at line 161 of file crypto_aes.c.

◆ rz_crypto_plugin_aes_cbc

RzCryptoPlugin rz_crypto_plugin_aes_cbc
extern

Definition at line 188 of file crypto_aes_cbc.c.

◆ rz_crypto_plugin_base64

RzCryptoPlugin rz_crypto_plugin_base64
extern

Definition at line 60 of file crypto_base64.c.

◆ rz_crypto_plugin_base91

RzCryptoPlugin rz_crypto_plugin_base91
extern

Definition at line 53 of file crypto_base91.c.

◆ rz_crypto_plugin_blowfish

RzCryptoPlugin rz_crypto_plugin_blowfish
extern

Definition at line 348 of file crypto_blowfish.c.

◆ rz_crypto_plugin_cps2

RzCryptoPlugin rz_crypto_plugin_cps2
extern

Definition at line 2954 of file crypto_cps2.c.

◆ rz_crypto_plugin_des

RzCryptoPlugin rz_crypto_plugin_des
extern

Definition at line 183 of file crypto_des.c.

◆ rz_crypto_plugin_punycode

RzCryptoPlugin rz_crypto_plugin_punycode
extern

Definition at line 42 of file crypto_punycode.c.

◆ rz_crypto_plugin_rc2

RzCryptoPlugin rz_crypto_plugin_rc2
extern

Definition at line 257 of file crypto_rc2.c.

◆ rz_crypto_plugin_rc4

RzCryptoPlugin rz_crypto_plugin_rc4
extern

Definition at line 126 of file crypto_rc4.c.

◆ rz_crypto_plugin_rc6

RzCryptoPlugin rz_crypto_plugin_rc6
extern

Definition at line 219 of file crypto_rc6.c.

◆ rz_crypto_plugin_rol

RzCryptoPlugin rz_crypto_plugin_rol
extern

Definition at line 89 of file crypto_rol.c.

◆ rz_crypto_plugin_ror

RzCryptoPlugin rz_crypto_plugin_ror
extern

Definition at line 89 of file crypto_ror.c.

◆ rz_crypto_plugin_rot

RzCryptoPlugin rz_crypto_plugin_rot
extern

Definition at line 115 of file crypto_rot.c.

◆ rz_crypto_plugin_serpent

RzCryptoPlugin rz_crypto_plugin_serpent
extern

Definition at line 124 of file crypto_serpent.c.

◆ rz_crypto_plugin_xor

RzCryptoPlugin rz_crypto_plugin_xor
extern

Definition at line 90 of file crypto_xor.c.