Rizin
unix-like reverse engineering framework and cli tools
algo_crca.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_hash.h>
5 #include <rz_util/rz_assert.h>
6 
7 #include "../algorithms/crc/crca.h"
8 
9 #define plugin_crca_preset_context_new(crcalgo, preset) \
10  static void *plugin_crca_##crcalgo##_context_new() { \
11  RzCrc *crc = RZ_NEW0(RzCrc); \
12  if (!crc) { \
13  return NULL; \
14  } \
15  crc_init_preset(crc, preset); \
16  return crc; \
17  }
18 
19 #define plugin_crca_preset_init(crcalgo, preset) \
20  static bool plugin_crca_##crcalgo##_init(void *context) { \
21  rz_return_val_if_fail(context, false); \
22  crc_init_preset((RzCrc *)context, preset); \
23  return true; \
24  }
25 
26 #define plugin_crca_preset_small_block(crcalgo, preset) \
27  static bool plugin_crca_##crcalgo##_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) { \
28  rz_return_val_if_fail(data &&digest, false); \
29  RzCrc ctx; \
30  crc_init_preset(&ctx, preset); \
31  ut8 *dgst = malloc(plugin_crca_digest_size(&ctx)); \
32  if (!dgst) { \
33  return false; \
34  } \
35  crc_update(&ctx, data, size); \
36  plugin_crca_final((void *)&ctx, dgst); \
37  *digest = dgst; \
38  if (digest_size) { \
39  *digest_size = plugin_crca_digest_size(&ctx); \
40  } \
41  return true; \
42  }
43 
44 static void plugin_crca_context_free(void *context) {
45  free(context);
46 }
47 
50  RzCrc *ctx = (RzCrc *)context;
51  if (ctx->size <= 8) {
52  return 1;
53  } else if (ctx->size > 8 && ctx->size <= 16) {
54  return 2;
55  } else if (ctx->size > 16 && ctx->size <= 32) {
56  return 4;
57  } else if (ctx->size > 32 && ctx->size <= 64) {
58  return 8;
59  }
60  RZ_LOG_ERROR("msg digest: unknown size %d.\n", ctx->size);
61  return 0;
62 }
63 
65  return 0;
66 }
67 
68 static bool plugin_crca_update(void *context, const ut8 *data, ut64 size) {
69  rz_return_val_if_fail(context && data, false);
70 
71  crc_update((RzCrc *)context, data, size);
72  return true;
73 }
74 
75 static bool plugin_crca_final(void *context, ut8 *digest) {
76  rz_return_val_if_fail(context && digest, false);
77  RzCrc *ctx = (RzCrc *)context;
78  utcrc r;
79  crc_final(ctx, &r);
80  if (ctx->size <= 8) {
81  rz_write_be8(digest, (ut8)r);
82  } else if (ctx->size > 8 && ctx->size <= 16) {
83  rz_write_be16(digest, (ut16)r);
84  } else if (ctx->size > 16 && ctx->size <= 32) {
85  rz_write_be32(digest, (ut32)r);
86  } else if (ctx->size > 32 && ctx->size <= 64) {
87  rz_write_be64(digest, r);
88  } else {
89  RZ_LOG_ERROR("msg digest: unknown size %d.\n", ctx->size);
90  }
91  return true;
92 }
93 
94 #define rz_hash_plugin_crca_preset(crcalgo, preset) \
95  plugin_crca_preset_context_new(crcalgo, preset); \
96  plugin_crca_preset_init(crcalgo, preset); \
97  plugin_crca_preset_small_block(crcalgo, preset); \
98  RzHashPlugin rz_hash_plugin_crca_##crcalgo = { \
99  .name = #crcalgo, \
100  .license = "LGPL3", \
101  .author = "deroad", \
102  .support_hmac = false, \
103  .context_new = plugin_crca_##crcalgo##_context_new, \
104  .context_free = plugin_crca_context_free, \
105  .digest_size = plugin_crca_digest_size, \
106  .block_size = plugin_crca_block_size, \
107  .init = plugin_crca_##crcalgo##_init, \
108  .update = plugin_crca_update, \
109  .final = plugin_crca_final, \
110  .small_block = plugin_crca_##crcalgo##_small_block, \
111  }
112 
113 #ifndef RZ_PLUGIN_INCORE
114 #define rz_lib_plugin_crca_preset(crcalgo) \
115  RZ_API RzLibStruct rizin_plugin = { \
116  .type = RZ_LIB_TYPE_HASH, \
117  .data = &rz_hash_plugin_crca_##crcalgo, \
118  .version = RZ_VERSION \
119  }
120 #else
121 #define rz_lib_plugin_crca_preset(crcalgo)
122 #endif
123 
124 #define rz_plugin_crca_preset_definition(crcalgo, preset) \
125  rz_hash_plugin_crca_preset(crcalgo, preset); \
126  rz_lib_plugin_crca_preset(crcalgo)
127 
static bool plugin_crca_final(void *context, ut8 *digest)
Definition: algo_crca.c:75
static RzHashSize plugin_crca_digest_size(void *context)
Definition: algo_crca.c:48
static void plugin_crca_context_free(void *context)
Definition: algo_crca.c:44
static RzHashSize plugin_crca_block_size(void *context)
Definition: algo_crca.c:64
static bool plugin_crca_update(void *context, const ut8 *data, ut64 size)
Definition: algo_crca.c:68
#define rz_plugin_crca_preset_definition(crcalgo, preset)
Definition: algo_crca.c:124
void crc_final(RzCrc *ctx, utcrc *r)
Definition: crca.c:38
void crc_update(RzCrc *ctx, const ut8 *data, ut32 sz)
Definition: crca.c:16
ut64 utcrc
Definition: crca.h:9
@ CRC_PRESET_32
Definition: crca.h:49
@ CRC_PRESET_CRC32_MPEG2
Definition: crca.h:54
@ CRC_PRESET_CRC16_RIELLO
Definition: crca.h:39
@ CRC_PRESET_CRC16_TELEDISK
Definition: crca.h:41
@ CRC_PRESET_24
Definition: crca.h:48
@ CRC_PRESET_16_HDLC
Definition: crca.h:27
@ CRC_PRESET_CRC16_CDMA2000
Definition: crca.h:30
@ CRC_PRESET_CRC32_BZIP2
Definition: crca.h:52
@ CRC_PRESET_CRC8_ROHC
Definition: crca.h:21
@ CRC_PRESET_CRC16_X25
Definition: crca.h:46
@ CRC_PRESET_32C
Definition: crca.h:51
@ CRC_PRESET_CRC8_DVB_S2
Definition: crca.h:16
@ CRC_PRESET_CRC8_CDMA2000
Definition: crca.h:14
@ CRC_PRESET_CRC8_WCDMA
Definition: crca.h:22
@ CRC_PRESET_15_CAN
Definition: crca.h:23
@ CRC_PRESET_CRC16_AUG_CCITT
Definition: crca.h:28
@ CRC_PRESET_CRC16_MCRF4XX
Definition: crca.h:38
@ CRC_PRESET_16_USB
Definition: crca.h:26
@ CRC_PRESET_8_SMBUS
Definition: crca.h:13
@ CRC_PRESET_CRC16_GENIBUS
Definition: crca.h:36
@ CRC_PRESET_CRC32_JAMCRC
Definition: crca.h:57
@ CRC_PRESET_CRC32_XFER
Definition: crca.h:58
@ CRC_PRESET_CRCA
Definition: crca.h:43
@ CRC_PRESET_CRC16_DECT_R
Definition: crca.h:32
@ CRC_PRESET_16
Definition: crca.h:24
@ CRC_PRESET_CRC16_MAXIM
Definition: crca.h:37
@ CRC_PRESET_CRC64_XZ
Definition: crca.h:62
@ CRC_PRESET_CRC16_DDS110
Definition: crca.h:31
@ CRC_PRESET_16_CITT
Definition: crca.h:25
@ CRC_PRESET_32_ECMA_267
Definition: crca.h:50
@ CRC_PRESET_CRC8_DARC
Definition: crca.h:15
@ CRC_PRESET_CRC64_ISO
Definition: crca.h:63
@ CRC_PRESET_CRC64
Definition: crca.h:59
@ CRC_PRESET_CRC16_DECT_X
Definition: crca.h:33
@ CRC_PRESET_CRC8_ITU
Definition: crca.h:19
@ CRC_PRESET_CRC16_MODBUS
Definition: crca.h:45
@ CRC_PRESET_CRC16_KERMIT
Definition: crca.h:44
@ CRC_PRESET_CRC8_EBU
Definition: crca.h:17
@ CRC_PRESET_CRC64_ECMA182
Definition: crca.h:60
@ CRC_PRESET_CRC16_BUYPASS
Definition: crca.h:29
@ CRC_PRESET_CRC32Q
Definition: crca.h:56
@ CRC_PRESET_CRC8_ICODE
Definition: crca.h:18
@ CRC_PRESET_CRC16_DNP
Definition: crca.h:34
@ CRC_PRESET_CRC16_TMS37157
Definition: crca.h:42
@ CRC_PRESET_CRC16_T10_DIF
Definition: crca.h:40
@ CRC_PRESET_CRC32_POSIX
Definition: crca.h:55
@ CRC_PRESET_CRC8_MAXIM
Definition: crca.h:20
@ CRC_PRESET_CRC16_XMODEM
Definition: crca.h:47
@ CRC_PRESET_CRC32D
Definition: crca.h:53
@ CRC_PRESET_CRC16_EN13757
Definition: crca.h:35
@ CRC_PRESET_CRC64_WE
Definition: crca.h:61
#define r
Definition: crypto_rc6.c:12
uint16_t ut16
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static void rz_write_be16(void *dest, ut16 val)
Definition: rz_endian.h:60
static void rz_write_be64(void *dest, ut64 val)
Definition: rz_endian.h:119
static void rz_write_be8(void *dest, ut8 val)
Definition: rz_endian.h:42
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98
ut32 RzHashSize
Definition: rz_hash.h:24
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
Definition: crca.h:68
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:1063