Rizin
unix-like reverse engineering framework and cli tools
crypto_base64.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016-2017 rakholiyajenish.07 <rakholiyajenish.07@gmail.com>
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 static bool base64_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
9  cry->dir = direction;
10  return true;
11 }
12 
13 static int base64_get_key_size(RzCrypto *cry) {
14  return 0;
15 }
16 
17 static bool base64_use(const char *algo) {
18  return !strcmp(algo, "base64");
19 }
20 
21 static bool update(RzCrypto *cry, const ut8 *buf, int len) {
22  if (len < 1) {
23  return false;
24  }
25 
26  int olen = 0;
27  ut8 *obuf = NULL;
28  if (cry->dir == RZ_CRYPTO_DIR_ENCRYPT) {
29  olen = ((len + 2) / 3) * 4;
30  obuf = malloc(olen + 1);
31  if (!obuf) {
32  return false;
33  }
34  rz_base64_encode((char *)obuf, (const ut8 *)buf, len);
35  } else {
36  olen = 4 + ((len / 4) * 3);
37  if (len > 0) {
38  olen -= (buf[len - 1] == '=') ? ((buf[len - 2] == '=') ? 2 : 1) : 0;
39  }
40  obuf = malloc(olen + 4);
41  if (!obuf) {
42  return false;
43  }
44  olen = rz_base64_decode(obuf, (const char *)buf, len);
45  }
46  if (olen > 0) {
47  rz_crypto_append(cry, obuf, olen);
48  }
49  free(obuf);
50  return true;
51 }
52 
53 static bool final(RzCrypto *cry, const ut8 *buf, int len) {
54  if (!buf) {
55  return true;
56  }
57  return update(cry, buf, len);
58 }
59 
61  .name = "base64",
62  .author = "rakholiyajenish.07",
63  .license = "LGPL-3",
64  .set_key = base64_set_key,
65  .get_key_size = base64_get_key_size,
66  .use = base64_use,
67  .update = update,
68  .final = final
69 };
70 
71 #ifndef RZ_PLUGIN_INCORE
74  .data = &rz_crypto_plugin_base64,
75  .version = RZ_VERSION
76 };
77 #endif
size_t len
Definition: 6502dis.c:15
#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 int base64_get_key_size(RzCrypto *cry)
Definition: crypto_base64.c:13
static bool base64_use(const char *algo)
Definition: crypto_base64.c:17
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_base64.c:21
static bool base64_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_base64.c:8
RZ_API RzLibStruct rizin_plugin
Definition: crypto_base64.c:72
RzCryptoPlugin rz_crypto_plugin_base64
Definition: crypto_base64.c:60
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
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81
RZ_API int rz_base64_decode(ut8 *bout, const char *bin, int len)
Definition: ubase64.c:48
@ RZ_CRYPTO_DIR_ENCRYPT
Definition: rz_crypto.h:23
@ RZ_LIB_TYPE_CRYPTO
Definition: rz_lib.h:81
#define RZ_VERSION
Definition: rz_version.h:8
const char * name
Definition: rz_crypto.h:41
static unsigned char * obuf
Definition: z80asm.c:36