Rizin
unix-like reverse engineering framework and cli tools
crypto_rot.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-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 
8 int mod(int a, int b) {
9  if (b < 0) {
10  return mod(-a, -b);
11  }
12  int ret = a % b;
13  if (ret < 0) {
14  ret += b;
15  }
16  return ret;
17 }
18 
19 static bool rot_init_state(ut8 *rotkey, const ut8 *key, int keylen) {
20  if (rotkey && key && keylen > 0) {
21  int i = atoi((const char *)key);
22  *rotkey = (ut8)mod(i, 26);
23  return true;
24  }
25  return false;
26 }
27 
28 static void rot_crypt(ut8 key, const ut8 *inbuf, ut8 *outbuf, int buflen) {
29  int i;
30  for (i = 0; i < buflen; i++) {
31  outbuf[i] = inbuf[i];
32  if ((inbuf[i] < 'a' || inbuf[i] > 'z') && (inbuf[i] < 'A' || inbuf[i] > 'Z')) {
33  continue;
34  }
35  outbuf[i] += key;
36  outbuf[i] -= (inbuf[i] >= 'a' && inbuf[i] <= 'z') ? 'a' : 'A';
37  outbuf[i] = mod(outbuf[i], 26);
38  outbuf[i] += (inbuf[i] >= 'a' && inbuf[i] <= 'z') ? 'a' : 'A';
39  }
40 }
41 
42 static void rot_decrypt(ut8 key, const ut8 *inbuf, ut8 *outbuf, int buflen) {
43  int i;
44  for (i = 0; i < buflen; i++) {
45  outbuf[i] = inbuf[i];
46  if ((inbuf[i] < 'a' || inbuf[i] > 'z') && (inbuf[i] < 'A' || inbuf[i] > 'Z')) {
47  continue;
48  }
49  outbuf[i] += 26; // adding so that subtracting does not make it negative
50  outbuf[i] -= key;
51  outbuf[i] -= (inbuf[i] >= 'a' && inbuf[i] <= 'z') ? 'a' : 'A';
52  outbuf[i] = mod(outbuf[i], 26);
53  outbuf[i] += (inbuf[i] >= 'a' && inbuf[i] <= 'z') ? 'a' : 'A';
54  }
55 }
56 
57 static bool rot_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
58  rz_return_val_if_fail(cry->user && key, false);
59  ut8 *rot_key = (ut8 *)cry->user;
60 
61  if (keylen > (sizeof(ut8) * 8) || keylen < 0) {
62  return false;
63  }
64 
65  cry->dir = direction;
66 
67  return rot_init_state(rot_key, key, keylen);
68 }
69 
70 static int rot_get_key_size(RzCrypto *cry) {
71  // Returning number of bytes occupied by ut8
72  return 1;
73 }
74 
75 static bool rot_use(const char *algo) {
76  return !strcmp(algo, "rot");
77 }
78 
79 static bool update(RzCrypto *cry, const ut8 *buf, int len) {
80  rz_return_val_if_fail(cry->user, false);
81  ut8 *rot_key = (ut8 *)cry->user;
82 
83  ut8 *obuf = calloc(1, len);
84  if (!obuf) {
85  return false;
86  }
87  if (cry->dir == RZ_CRYPTO_DIR_ENCRYPT) {
88  rot_crypt(*rot_key, buf, obuf, len);
89  } else {
90  rot_decrypt(*rot_key, buf, obuf, len);
91  }
92  rz_crypto_append(cry, obuf, len);
93  free(obuf);
94  return true;
95 }
96 
97 static bool final(RzCrypto *cry, const ut8 *buf, int len) {
98  return update(cry, buf, len);
99 }
100 
101 static bool rol_init(RzCrypto *cry) {
102  rz_return_val_if_fail(cry, false);
103 
104  cry->user = RZ_NEW0(ut8);
105  return cry->user != NULL;
106 }
107 
108 static bool rol_fini(RzCrypto *cry) {
109  rz_return_val_if_fail(cry, false);
110 
111  free(cry->user);
112  return true;
113 }
114 
116  .name = "rot",
117  .author = "pancake",
118  .license = "LGPL-3",
119  .set_key = rot_set_key,
120  .get_key_size = rot_get_key_size,
121  .use = rot_use,
122  .update = update,
123  .final = final,
124  .init = rol_init,
125  .fini = rol_fini,
126 };
127 
128 #ifndef RZ_PLUGIN_INCORE
131  .data = &rz_crypto_plugin_rot,
132  .version = RZ_VERSION
133 };
134 #endif
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#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 rol_init(RzCrypto *cry)
Definition: crypto_rot.c:101
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_rot.c:79
int mod(int a, int b)
Definition: crypto_rot.c:8
RZ_API RzLibStruct rizin_plugin
Definition: crypto_rot.c:129
static void rot_crypt(ut8 key, const ut8 *inbuf, ut8 *outbuf, int buflen)
Definition: crypto_rot.c:28
static bool rot_init_state(ut8 *rotkey, const ut8 *key, int keylen)
Definition: crypto_rot.c:19
static bool rol_fini(RzCrypto *cry)
Definition: crypto_rot.c:108
static bool rot_use(const char *algo)
Definition: crypto_rot.c:75
static void rot_decrypt(ut8 key, const ut8 *inbuf, ut8 *outbuf, int buflen)
Definition: crypto_rot.c:42
RzCryptoPlugin rz_crypto_plugin_rot
Definition: crypto_rot.c:115
static bool rot_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_rot.c:57
static int rot_get_key_size(RzCrypto *cry)
Definition: crypto_rot.c:70
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
unsigned char outbuf[SIZE]
Definition: gun.c:162
unsigned char inbuf[SIZE]
Definition: gun.c:161
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#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
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
const char * name
Definition: rz_crypto.h:41
void * user
Definition: rz_crypto.h:36
ut64 buflen
Definition: core.c:76
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static unsigned char * obuf
Definition: z80asm.c:36