Rizin
unix-like reverse engineering framework and cli tools
big-ssl.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2010 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2020 FXTi <zjxiang1998@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_util.h>
6 
8  return BN_new();
9 }
10 
12  BN_free(b);
13 }
14 
16  BN_zero(b);
17 }
18 
20  BN_clear(b);
21 }
22 
24  if (v < 0) {
25  BN_set_word(b, -v);
26  BN_set_negative(b, v);
27  } else {
28  BN_set_word(b, v);
29  }
30 }
31 
33  BN_ULONG maxx = 0;
34  maxx = ~maxx;
35  BN_ULONG res = BN_get_word(b);
36  if (res == maxx) {
37  RzNumBig *B = rz_big_new();
38  rz_big_assign(B, b);
39  BN_mask_bits(B, BN_BYTES * 8 - 1);
40  res = BN_get_word(B);
41  rz_big_free(B);
42  }
43  res *= (BN_is_negative(b) ? -1 : 1);
44  return res;
45 }
46 
47 RZ_API void rz_big_from_hexstr(RzNumBig *b, const char *str) {
48  if (rz_str_startswith(str, "0x")) {
49  str += 2;
50  BN_hex2bn(&b, str);
51  } else if (rz_str_startswith(str, "-0x")) {
52  str += 3;
53  BN_hex2bn(&b, str);
54  BN_set_negative(b, -1);
55  }
56 }
57 
59  char *tmp = BN_bn2hex(b);
60  char *res;
61  if (tmp[0] == '-') {
62  res = rz_str_newf("-0x%s", &tmp[1]);
63  } else {
64  res = rz_str_newf("0x%s", tmp);
65  }
66  OPENSSL_free(tmp);
67  for (size_t i = 0; res[i]; i++) {
68  res[i] = tolower(res[i]);
69  }
70  return res;
71 }
72 
74  BN_copy(dst, src);
75 }
76 
78  BN_add(c, a, b);
79 }
80 
82  BN_sub(c, a, b);
83 }
84 
86  BN_CTX *bn_ctx = BN_CTX_new();
87  BN_mul(c, a, b, bn_ctx);
88  BN_CTX_free(bn_ctx);
89 }
90 
92  BN_CTX *bn_ctx = BN_CTX_new();
93  BN_div(c, NULL, a, b, bn_ctx);
94  BN_CTX_free(bn_ctx);
95 }
96 
98  BN_CTX *bn_ctx = BN_CTX_new();
99  BN_mod(c, a, b, bn_ctx);
100  BN_CTX_free(bn_ctx);
101 }
102 
104  BN_CTX *bn_ctx = BN_CTX_new();
105  BN_div(c, d, a, b, bn_ctx);
106  BN_CTX_free(bn_ctx);
107 }
108 
110  RzNumBig *A = rz_big_new();
111  RzNumBig *B = rz_big_new();
112  RzNumBig *C = rz_big_new();
113  RzNumBig *addition = rz_big_new();
114 
115  size_t step = 4 * 8, move = 0;
116  ut32 tmp = 0;
117  rz_big_assign(A, a);
118  rz_big_assign(B, b);
119 
120  while (!rz_big_is_zero(A) || !rz_big_is_zero(B)) {
121  tmp = rz_big_to_int(A);
122  tmp &= rz_big_to_int(B);
123  rz_big_rshift(A, A, step);
124  rz_big_rshift(B, B, step);
125  rz_big_from_int(addition, tmp);
126  rz_big_lshift(addition, addition, move);
127  rz_big_add(C, C, addition);
128 
129  move += step;
130  }
131 
132  rz_big_assign(c, C);
133 
134  rz_big_free(A);
135  rz_big_free(B);
136  rz_big_free(C);
137  rz_big_free(addition);
138 }
139 
141  RzNumBig *A = rz_big_new();
142  RzNumBig *B = rz_big_new();
143  RzNumBig *C = rz_big_new();
144  RzNumBig *addition = rz_big_new();
145 
146  size_t step = 4 * 8, move = 0;
147  ut32 tmp = 0;
148  rz_big_assign(A, a);
149  rz_big_assign(B, b);
150 
151  while (!rz_big_is_zero(A) || !rz_big_is_zero(B)) {
152  tmp = rz_big_to_int(A);
153  tmp |= rz_big_to_int(B);
154  rz_big_rshift(A, A, step);
155  rz_big_rshift(B, B, step);
156  rz_big_from_int(addition, tmp);
157  rz_big_lshift(addition, addition, move);
158  rz_big_add(C, C, addition);
159 
160  move += step;
161  }
162 
163  rz_big_assign(c, C);
164 
165  rz_big_free(A);
166  rz_big_free(B);
167  rz_big_free(C);
168  rz_big_free(addition);
169 }
170 
172  RzNumBig *A = rz_big_new();
173  RzNumBig *B = rz_big_new();
174  RzNumBig *C = rz_big_new();
175  RzNumBig *addition = rz_big_new();
176 
177  size_t step = 4 * 8, move = 0;
178  ut32 tmp = 0;
179  rz_big_assign(A, a);
180  rz_big_assign(B, b);
181 
182  while (!rz_big_is_zero(A) || !rz_big_is_zero(B)) {
183  tmp = rz_big_to_int(A);
184  tmp ^= rz_big_to_int(B);
185  rz_big_rshift(A, A, step);
186  rz_big_rshift(B, B, step);
187  rz_big_from_int(addition, tmp);
188  rz_big_lshift(addition, addition, move);
189  rz_big_add(C, C, addition);
190 
191  move += step;
192  }
193 
194  rz_big_assign(c, C);
195 
196  rz_big_free(A);
197  rz_big_free(B);
198  rz_big_free(C);
199  rz_big_free(addition);
200 }
201 
202 RZ_API void rz_big_lshift(RzNumBig *c, RzNumBig *a, size_t nbits) {
203  BN_lshift(c, a, nbits);
204 }
205 
206 RZ_API void rz_big_rshift(RzNumBig *c, RzNumBig *a, size_t nbits) {
207  BN_rshift(c, a, nbits);
208 }
209 
211  return BN_cmp(a, b);
212 }
213 
215  return BN_is_zero(a);
216 }
217 
219  BN_add_word(a, 1);
220 }
221 
223  BN_sub_word(a, 1);
224 }
225 
227  BN_CTX *bn_ctx = BN_CTX_new();
228  BN_mod_exp(c, a, b, m, bn_ctx);
229  BN_CTX_free(bn_ctx);
230 }
231 
233  RzNumBig *tmp = rz_big_new();
234  RzNumBig *low = rz_big_new();
235  RzNumBig *high = rz_big_new();
236  RzNumBig *mid = rz_big_new();
237 
238  rz_big_assign(high, a);
239  rz_big_rshift(mid, high, 1);
240  rz_big_inc(mid);
241 
242  while (rz_big_cmp(high, low) > 0) {
243  rz_big_mul(tmp, mid, mid);
244  if (rz_big_cmp(tmp, a) > 0) {
245  rz_big_assign(high, mid);
246  rz_big_dec(high);
247  } else {
248  rz_big_assign(low, mid);
249  }
250  rz_big_sub(mid, high, low);
251  rz_big_rshift(mid, mid, 1);
252  rz_big_add(mid, mid, low);
253  rz_big_inc(mid);
254  }
255  rz_big_assign(b, low);
256 
257  rz_big_free(tmp);
258  rz_big_free(low);
259  rz_big_free(high);
260  rz_big_free(mid);
261 }
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
#define A(x)
Definition: arc.h:165
#define B(x)
Definition: arc.h:166
#define C(x)
Definition: arc.h:167
RZ_API char * rz_big_to_hexstr(RzNumBig *b)
Definition: big-ssl.c:58
RZ_API void rz_big_dec(RzNumBig *a)
Definition: big-ssl.c:222
RZ_API int rz_big_cmp(RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:210
RZ_API void rz_big_assign(RzNumBig *dst, RzNumBig *src)
Definition: big-ssl.c:73
RZ_API void rz_big_and(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:109
RZ_API void rz_big_xor(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:171
RZ_API void rz_big_fini(RzNumBig *b)
Definition: big-ssl.c:19
RZ_API void rz_big_add(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:77
RZ_API void rz_big_from_hexstr(RzNumBig *b, const char *str)
Definition: big-ssl.c:47
RZ_API void rz_big_divmod(RzNumBig *c, RzNumBig *d, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:103
RZ_API void rz_big_free(RzNumBig *b)
Definition: big-ssl.c:11
RZ_API void rz_big_rshift(RzNumBig *c, RzNumBig *a, size_t nbits)
Definition: big-ssl.c:206
RZ_API void rz_big_lshift(RzNumBig *c, RzNumBig *a, size_t nbits)
Definition: big-ssl.c:202
RZ_API void rz_big_or(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:140
RZ_API st64 rz_big_to_int(RzNumBig *b)
Definition: big-ssl.c:32
RZ_API RzNumBig * rz_big_new(void)
Definition: big-ssl.c:7
RZ_API void rz_big_powm(RzNumBig *c, RzNumBig *a, RzNumBig *b, RzNumBig *m)
Definition: big-ssl.c:226
RZ_API void rz_big_div(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:91
RZ_API void rz_big_init(RzNumBig *b)
Definition: big-ssl.c:15
RZ_API void rz_big_sub(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:81
RZ_API void rz_big_mod(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:97
RZ_API void rz_big_isqrt(RzNumBig *b, RzNumBig *a)
Definition: big-ssl.c:232
RZ_API void rz_big_inc(RzNumBig *a)
Definition: big-ssl.c:218
RZ_API void rz_big_from_int(RzNumBig *b, st64 v)
Definition: big-ssl.c:23
RZ_API void rz_big_mul(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:85
RZ_API int rz_big_is_zero(RzNumBig *a)
Definition: big-ssl.c:214
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
const char * v
Definition: dsignal.c:12
static states step(struct re_guts *, sopno, sopno, states, int, states)
Definition: engine.c:888
char * dst
Definition: lz4.h:724
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286
#define st64
Definition: rz_types_base.h:10
#define tolower(c)
Definition: safe-ctype.h:149
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41