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

Go to the source code of this file.

Classes

struct  rc4_state
 

Functions

static __inline void swap_bytes (ut8 *a, ut8 *b)
 
static bool rc4_init_state (struct rc4_state *const state, const ut8 *key, int keylen)
 
static void rc4_crypt (struct rc4_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
 
static bool rc4_set_key (RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
 
static int rc4_get_key_size (RzCrypto *cry)
 
static bool rc4_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 rc4_init (RzCrypto *cry)
 
static bool rc4_fini (RzCrypto *cry)
 

Variables

RzCryptoPlugin rz_crypto_plugin_rc4
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ final()

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

Definition at line 108 of file crypto_rc4.c.

108  {
109  return update(cry, buf, len);
110 }
size_t len
Definition: 6502dis.c:15
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_rc4.c:94
voidpf void * buf
Definition: ioapi.h:138

References len, and update().

◆ rc4_crypt()

static void rc4_crypt ( struct rc4_state *const  state,
const ut8 inbuf,
ut8 outbuf,
int  buflen 
)
static

Definition at line 57 of file crypto_rc4.c.

57  {
58  int i;
59  ut8 j;
60 
61  for (i = 0; i < buflen; i++) {
62  /* Update modification indices */
63  state->index1++;
64  state->index2 += state->perm[state->index1];
65  /* Modify permutation */
66  swap_bytes(&state->perm[state->index1],
67  &state->perm[state->index2]);
68  /* Encrypt/decrypt next byte */
69  j = state->perm[state->index1] + state->perm[state->index2];
70  outbuf[i] = inbuf[i] ^ state->perm[j];
71  }
72 }
lzma_index ** i
Definition: index.h:629
static __inline void swap_bytes(ut8 *a, ut8 *b)
Definition: crypto_rc4.c:15
unsigned char outbuf[SIZE]
Definition: gun.c:162
unsigned char inbuf[SIZE]
Definition: gun.c:161
uint8_t ut8
Definition: lh5801.h:11
Definition: dis.h:43
ut64 buflen
Definition: core.c:76

References buflen, i, inbuf, outbuf, and swap_bytes().

Referenced by update().

◆ rc4_fini()

static bool rc4_fini ( RzCrypto cry)
static

Definition at line 119 of file crypto_rc4.c.

119  {
120  rz_return_val_if_fail(cry, false);
121 
122  free(cry->user);
123  return true;
124 }
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.

◆ rc4_get_key_size()

static int rc4_get_key_size ( RzCrypto cry)
static

Definition at line 83 of file crypto_rc4.c.

83  {
84  rz_return_val_if_fail(cry->user, 0);
85  struct rc4_state *st = (struct rc4_state *)cry->user;
86 
87  return st->key_size;
88 }
int key_size
Definition: crypto_rc4.c:12

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

◆ rc4_init()

static bool rc4_init ( RzCrypto cry)
static

Definition at line 112 of file crypto_rc4.c.

112  {
113  rz_return_val_if_fail(cry, false);
114 
115  cry->user = RZ_NEW0(struct rc4_state);
116  return cry->user != NULL;
117 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEW0(x)
Definition: rz_types.h:284

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

◆ rc4_init_state()

static bool rc4_init_state ( struct rc4_state *const  state,
const ut8 key,
int  keylen 
)
static

Definition at line 28 of file crypto_rc4.c.

28  {
29  ut8 j;
30  int i;
31 
32  if (!state || !key || keylen < 1) {
33  return false;
34  }
35  state->key_size = keylen;
36  /* Initialize state with identity permutation */
37  for (i = 0; i < 256; i++) {
38  state->perm[i] = (ut8)i;
39  }
40  state->index1 = 0;
41  state->index2 = 0;
42 
43  /* Randomize the permutation using key data */
44  for (j = i = 0; i < 256; i++) {
45  j += state->perm[i] + key[i % keylen];
46  swap_bytes(&state->perm[i], &state->perm[j]);
47  }
48  return true;
49 }
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 ut8
Definition: dcpu16.h:8

References i, key, swap_bytes(), and ut8.

Referenced by rc4_set_key().

◆ rc4_set_key()

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

Definition at line 76 of file crypto_rc4.c.

76  {
77  rz_return_val_if_fail(cry->user && key, false);
78  struct rc4_state *st = (struct rc4_state *)cry->user;
79 
80  return rc4_init_state(st, key, keylen);
81 }
static bool rc4_init_state(struct rc4_state *const state, const ut8 *key, int keylen)
Definition: crypto_rc4.c:28

References key, rc4_init_state(), rz_return_val_if_fail, and rz_crypto_t::user.

◆ rc4_use()

static bool rc4_use ( const char *  algo)
static

Definition at line 90 of file crypto_rc4.c.

90  {
91  return !strcmp(algo, "rc4");
92 }

◆ swap_bytes()

static __inline void swap_bytes ( ut8 a,
ut8 b 
)
static

Definition at line 15 of file crypto_rc4.c.

15  {
16  if (a != b) {
17  ut8 temp = *a;
18  *a = *b;
19  *b = temp;
20  }
21 }
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, and b.

Referenced by rc4_crypt(), and rc4_init_state().

◆ update()

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

Definition at line 94 of file crypto_rc4.c.

94  {
95  rz_return_val_if_fail(cry->user, false);
96  struct rc4_state *st = (struct rc4_state *)cry->user;
97 
98  ut8 *obuf = calloc(1, len);
99  if (!obuf) {
100  return false;
101  }
102  rc4_crypt(st, buf, obuf, len);
103  rz_crypto_append(cry, obuf, len);
104  free(obuf);
105  return false;
106 }
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto.c:175
static void rc4_crypt(struct rc4_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
Definition: crypto_rc4.c:57
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static unsigned char * obuf
Definition: z80asm.c:36

References calloc(), free(), len, obuf, rc4_crypt(), rz_crypto_append(), rz_return_val_if_fail, and rz_crypto_t::user.

Referenced by final().

Variable Documentation

◆ rizin_plugin

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

Definition at line 140 of file crypto_rc4.c.

◆ rz_crypto_plugin_rc4

RzCryptoPlugin rz_crypto_plugin_rc4
Initial value:
= {
.name = "rc4",
.author = "pancake",
.license = "LGPL-3",
.set_key = rc4_set_key,
.get_key_size = rc4_get_key_size,
.use = rc4_use,
.update = update,
.final = final,
.init = rc4_init,
.fini = rc4_fini,
}
static bool rc4_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_rc4.c:76
static int rc4_get_key_size(RzCrypto *cry)
Definition: crypto_rc4.c:83
static bool rc4_use(const char *algo)
Definition: crypto_rc4.c:90
static bool rc4_fini(RzCrypto *cry)
Definition: crypto_rc4.c:119
static bool rc4_init(RzCrypto *cry)
Definition: crypto_rc4.c:112

Definition at line 126 of file crypto_rc4.c.