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

Go to the source code of this file.

Functions

RZ_API RzNumBigrz_big_new (void)
 
RZ_API void rz_big_free (RzNumBig *b)
 
RZ_API void rz_big_init (RzNumBig *b)
 
RZ_API void rz_big_fini (RzNumBig *b)
 
RZ_API void rz_big_from_int (RzNumBig *b, st64 v)
 
RZ_API st64 rz_big_to_int (RzNumBig *b)
 
RZ_API void rz_big_from_hexstr (RzNumBig *b, const char *str)
 
RZ_API char * rz_big_to_hexstr (RzNumBig *b)
 
RZ_API void rz_big_assign (RzNumBig *dst, RzNumBig *src)
 
RZ_API void rz_big_add (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_sub (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_mul (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_div (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_mod (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_divmod (RzNumBig *c, RzNumBig *d, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_and (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_or (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_xor (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_lshift (RzNumBig *c, RzNumBig *a, size_t nbits)
 
RZ_API void rz_big_rshift (RzNumBig *c, RzNumBig *a, size_t nbits)
 
RZ_API int rz_big_cmp (RzNumBig *a, RzNumBig *b)
 
RZ_API int rz_big_is_zero (RzNumBig *a)
 
RZ_API void rz_big_inc (RzNumBig *a)
 
RZ_API void rz_big_dec (RzNumBig *a)
 
RZ_API void rz_big_powm (RzNumBig *c, RzNumBig *a, RzNumBig *b, RzNumBig *m)
 
RZ_API void rz_big_isqrt (RzNumBig *b, RzNumBig *a)
 

Function Documentation

◆ rz_big_add()

RZ_API void rz_big_add ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 77 of file big-ssl.c.

77  {
78  BN_add(c, a, b);
79 }
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41

References a, b, and c.

Referenced by rz_big_and(), rz_big_isqrt(), rz_big_or(), and rz_big_xor().

◆ rz_big_and()

RZ_API void rz_big_and ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 109 of file big-ssl.c.

109  {
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 }
#define A(x)
Definition: arc.h:165
#define B(x)
Definition: arc.h:166
#define C(x)
Definition: arc.h:167
RZ_API void rz_big_assign(RzNumBig *dst, RzNumBig *src)
Definition: big-ssl.c:73
RZ_API void rz_big_add(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:77
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 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_from_int(RzNumBig *b, st64 v)
Definition: big-ssl.c:23
RZ_API int rz_big_is_zero(RzNumBig *a)
Definition: big-ssl.c:214
uint32_t ut32
static states step(struct re_guts *, sopno, sopno, states, int, states)
Definition: engine.c:888

References a, A, b, B, c, C, rz_big_add(), rz_big_assign(), rz_big_free(), rz_big_from_int(), rz_big_is_zero(), rz_big_lshift(), rz_big_new(), rz_big_rshift(), rz_big_to_int(), step(), and autogen_x86imm::tmp.

◆ rz_big_assign()

RZ_API void rz_big_assign ( RzNumBig dst,
RzNumBig src 
)

Definition at line 73 of file big-ssl.c.

73  {
74  BN_copy(dst, src);
75 }
lzma_index * src
Definition: index.h:567
char * dst
Definition: lz4.h:724

References dst, and src.

Referenced by rz_big_and(), rz_big_isqrt(), rz_big_or(), rz_big_to_int(), and rz_big_xor().

◆ rz_big_cmp()

RZ_API int rz_big_cmp ( RzNumBig a,
RzNumBig b 
)

Definition at line 210 of file big-ssl.c.

210  {
211  return BN_cmp(a, b);
212 }

References a, and b.

Referenced by rz_big_isqrt().

◆ rz_big_dec()

RZ_API void rz_big_dec ( RzNumBig a)

Definition at line 222 of file big-ssl.c.

222  {
223  BN_sub_word(a, 1);
224 }

References a.

Referenced by rz_big_isqrt().

◆ rz_big_div()

RZ_API void rz_big_div ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 91 of file big-ssl.c.

91  {
92  BN_CTX *bn_ctx = BN_CTX_new();
93  BN_div(c, NULL, a, b, bn_ctx);
94  BN_CTX_free(bn_ctx);
95 }
#define NULL
Definition: cris-opc.c:27

References a, b, c, and NULL.

◆ rz_big_divmod()

RZ_API void rz_big_divmod ( RzNumBig c,
RzNumBig d,
RzNumBig a,
RzNumBig b 
)

Definition at line 103 of file big-ssl.c.

103  {
104  BN_CTX *bn_ctx = BN_CTX_new();
105  BN_div(c, d, a, b, bn_ctx);
106  BN_CTX_free(bn_ctx);
107 }
#define d(i)
Definition: sha256.c:44

References a, b, c, and d.

◆ rz_big_fini()

RZ_API void rz_big_fini ( RzNumBig b)

Definition at line 19 of file big-ssl.c.

19  {
20  BN_clear(b);
21 }

References b.

◆ rz_big_free()

RZ_API void rz_big_free ( RzNumBig b)

Definition at line 11 of file big-ssl.c.

11  {
12  BN_free(b);
13 }

References b.

Referenced by rz_big_and(), rz_big_isqrt(), rz_big_or(), rz_big_to_int(), and rz_big_xor().

◆ rz_big_from_hexstr()

RZ_API void rz_big_from_hexstr ( RzNumBig b,
const char *  str 
)

Definition at line 47 of file big-ssl.c.

47  {
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 }
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

References b, rz_str_startswith(), and cmd_descs_generate::str.

◆ rz_big_from_int()

RZ_API void rz_big_from_int ( RzNumBig b,
st64  v 
)

Definition at line 23 of file big-ssl.c.

23  {
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 }
const char * v
Definition: dsignal.c:12

References b, and v.

Referenced by rz_big_and(), rz_big_or(), and rz_big_xor().

◆ rz_big_inc()

RZ_API void rz_big_inc ( RzNumBig a)

Definition at line 218 of file big-ssl.c.

218  {
219  BN_add_word(a, 1);
220 }

References a.

Referenced by rz_big_isqrt().

◆ rz_big_init()

RZ_API void rz_big_init ( RzNumBig b)

Definition at line 15 of file big-ssl.c.

15  {
16  BN_zero(b);
17 }

References b.

◆ rz_big_is_zero()

RZ_API int rz_big_is_zero ( RzNumBig a)

Definition at line 214 of file big-ssl.c.

214  {
215  return BN_is_zero(a);
216 }

References a.

Referenced by rz_big_and(), rz_big_or(), and rz_big_xor().

◆ rz_big_isqrt()

RZ_API void rz_big_isqrt ( RzNumBig b,
RzNumBig a 
)

Definition at line 232 of file big-ssl.c.

232  {
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 }
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_sub(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:81
RZ_API void rz_big_inc(RzNumBig *a)
Definition: big-ssl.c:218
RZ_API void rz_big_mul(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-ssl.c:85

References a, b, rz_big_add(), rz_big_assign(), rz_big_cmp(), rz_big_dec(), rz_big_free(), rz_big_inc(), rz_big_mul(), rz_big_new(), rz_big_rshift(), rz_big_sub(), and autogen_x86imm::tmp.

◆ rz_big_lshift()

RZ_API void rz_big_lshift ( RzNumBig c,
RzNumBig a,
size_t  nbits 
)

Definition at line 202 of file big-ssl.c.

202  {
203  BN_lshift(c, a, nbits);
204 }

References a, and c.

Referenced by rz_big_and(), rz_big_or(), and rz_big_xor().

◆ rz_big_mod()

RZ_API void rz_big_mod ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 97 of file big-ssl.c.

97  {
98  BN_CTX *bn_ctx = BN_CTX_new();
99  BN_mod(c, a, b, bn_ctx);
100  BN_CTX_free(bn_ctx);
101 }

References a, b, and c.

◆ rz_big_mul()

RZ_API void rz_big_mul ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 85 of file big-ssl.c.

85  {
86  BN_CTX *bn_ctx = BN_CTX_new();
87  BN_mul(c, a, b, bn_ctx);
88  BN_CTX_free(bn_ctx);
89 }

References a, b, and c.

Referenced by rz_big_isqrt().

◆ rz_big_new()

RZ_API RzNumBig* rz_big_new ( void  )

Definition at line 7 of file big-ssl.c.

7  {
8  return BN_new();
9 }

Referenced by rz_big_and(), rz_big_isqrt(), rz_big_or(), rz_big_to_int(), and rz_big_xor().

◆ rz_big_or()

RZ_API void rz_big_or ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 140 of file big-ssl.c.

140  {
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 }

References a, A, b, B, c, C, rz_big_add(), rz_big_assign(), rz_big_free(), rz_big_from_int(), rz_big_is_zero(), rz_big_lshift(), rz_big_new(), rz_big_rshift(), rz_big_to_int(), step(), and autogen_x86imm::tmp.

◆ rz_big_powm()

RZ_API void rz_big_powm ( RzNumBig c,
RzNumBig a,
RzNumBig b,
RzNumBig m 
)

Definition at line 226 of file big-ssl.c.

226  {
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 }

References a, b, c, and regress::m.

◆ rz_big_rshift()

RZ_API void rz_big_rshift ( RzNumBig c,
RzNumBig a,
size_t  nbits 
)

Definition at line 206 of file big-ssl.c.

206  {
207  BN_rshift(c, a, nbits);
208 }

References a, and c.

Referenced by rz_big_and(), rz_big_isqrt(), rz_big_or(), and rz_big_xor().

◆ rz_big_sub()

RZ_API void rz_big_sub ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 81 of file big-ssl.c.

81  {
82  BN_sub(c, a, b);
83 }

References a, b, and c.

Referenced by rz_big_isqrt().

◆ rz_big_to_hexstr()

RZ_API char* rz_big_to_hexstr ( RzNumBig b)

Definition at line 58 of file big-ssl.c.

58  {
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 }
lzma_index ** i
Definition: index.h:629
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define tolower(c)
Definition: safe-ctype.h:149

References b, i, rz_str_newf(), autogen_x86imm::tmp, and tolower.

◆ rz_big_to_int()

RZ_API st64 rz_big_to_int ( RzNumBig b)

Definition at line 32 of file big-ssl.c.

32  {
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 }

References b, B, rz_big_assign(), rz_big_free(), and rz_big_new().

Referenced by rz_big_and(), rz_big_or(), and rz_big_xor().

◆ rz_big_xor()

RZ_API void rz_big_xor ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 171 of file big-ssl.c.

171  {
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 }

References a, A, b, B, c, C, rz_big_add(), rz_big_assign(), rz_big_free(), rz_big_from_int(), rz_big_is_zero(), rz_big_lshift(), rz_big_new(), rz_big_rshift(), rz_big_to_int(), step(), and autogen_x86imm::tmp.