Package list libcryptx-perl / 72a9bea
libtommath update Karel Miko 2 years ago
11 changed file(s) with 463 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
136136 ltm/bn_fast_s_mp_mul_digs.o ltm/bn_fast_s_mp_mul_high_digs.o ltm/bn_fast_s_mp_sqr.o \
137137 ltm/bn_mp_2expt.o ltm/bn_mp_abs.o ltm/bn_mp_add.o ltm/bn_mp_addmod.o ltm/bn_mp_add_d.o \
138138 ltm/bn_mp_and.o ltm/bn_mp_clamp.o ltm/bn_mp_clear.o ltm/bn_mp_clear_multi.o ltm/bn_mp_cmp.o \
139 ltm/bn_mp_cmp_d.o ltm/bn_mp_cmp_mag.o ltm/bn_mp_cnt_lsb.o ltm/bn_mp_copy.o ltm/bn_mp_count_bits.o \
140 ltm/bn_mp_div.o ltm/bn_mp_div_2.o ltm/bn_mp_div_2d.o ltm/bn_mp_div_3.o ltm/bn_mp_div_d.o \
141 ltm/bn_mp_dr_is_modulus.o ltm/bn_mp_dr_reduce.o ltm/bn_mp_dr_setup.o ltm/bn_mp_exch.o \
142 ltm/bn_mp_export.o ltm/bn_mp_exptmod.o ltm/bn_mp_exptmod_fast.o ltm/bn_mp_expt_d.o \
143 ltm/bn_mp_expt_d_ex.o ltm/bn_mp_exteuclid.o ltm/bn_mp_fread.o ltm/bn_mp_fwrite.o \
144 ltm/bn_mp_gcd.o ltm/bn_mp_get_int.o ltm/bn_mp_get_long.o ltm/bn_mp_grow.o ltm/bn_mp_import.o \
145 ltm/bn_mp_init.o ltm/bn_mp_init_copy.o ltm/bn_mp_init_multi.o ltm/bn_mp_init_set.o \
146 ltm/bn_mp_init_set_int.o ltm/bn_mp_init_size.o ltm/bn_mp_invmod.o ltm/bn_mp_invmod_slow.o \
147 ltm/bn_mp_is_square.o ltm/bn_mp_jacobi.o ltm/bn_mp_karatsuba_mul.o ltm/bn_mp_karatsuba_sqr.o \
148 ltm/bn_mp_lcm.o ltm/bn_mp_lshd.o ltm/bn_mp_mod.o ltm/bn_mp_mod_2d.o ltm/bn_mp_mod_d.o \
149 ltm/bn_mp_montgomery_calc_normalization.o ltm/bn_mp_montgomery_reduce.o ltm/bn_mp_montgomery_setup.o \
150 ltm/bn_mp_mul.o ltm/bn_mp_mulmod.o ltm/bn_mp_mul_2.o ltm/bn_mp_mul_2d.o ltm/bn_mp_mul_d.o \
151 ltm/bn_mp_neg.o ltm/bn_mp_n_root.o ltm/bn_mp_n_root_ex.o ltm/bn_mp_or.o ltm/bn_mp_prime_fermat.o \
152 ltm/bn_mp_prime_is_divisible.o ltm/bn_mp_prime_is_prime.o ltm/bn_mp_prime_miller_rabin.o \
153 ltm/bn_mp_prime_next_prime.o ltm/bn_mp_prime_rabin_miller_trials.o ltm/bn_mp_prime_random_ex.o \
154 ltm/bn_mp_radix_size.o ltm/bn_mp_radix_smap.o ltm/bn_mp_rand.o ltm/bn_mp_read_radix.o \
155 ltm/bn_mp_read_signed_bin.o ltm/bn_mp_read_unsigned_bin.o ltm/bn_mp_reduce.o ltm/bn_mp_reduce_2k.o \
156 ltm/bn_mp_reduce_2k_l.o ltm/bn_mp_reduce_2k_setup.o ltm/bn_mp_reduce_2k_setup_l.o \
157 ltm/bn_mp_reduce_is_2k.o ltm/bn_mp_reduce_is_2k_l.o ltm/bn_mp_reduce_setup.o ltm/bn_mp_rshd.o \
158 ltm/bn_mp_set.o ltm/bn_mp_set_int.o ltm/bn_mp_set_long.o ltm/bn_mp_shrink.o ltm/bn_mp_signed_bin_size.o \
159 ltm/bn_mp_sqr.o ltm/bn_mp_sqrmod.o ltm/bn_mp_sqrt.o ltm/bn_mp_sqrtmod_prime.o ltm/bn_mp_sub.o \
160 ltm/bn_mp_submod.o ltm/bn_mp_sub_d.o ltm/bn_mp_toom_mul.o ltm/bn_mp_toom_sqr.o ltm/bn_mp_toradix.o \
161 ltm/bn_mp_toradix_n.o ltm/bn_mp_to_signed_bin.o ltm/bn_mp_to_signed_bin_n.o ltm/bn_mp_to_unsigned_bin.o \
162 ltm/bn_mp_to_unsigned_bin_n.o ltm/bn_mp_unsigned_bin_size.o ltm/bn_mp_xor.o ltm/bn_mp_zero.o \
163 ltm/bn_prime_tab.o ltm/bn_reverse.o ltm/bn_s_mp_add.o ltm/bn_s_mp_exptmod.o ltm/bn_s_mp_mul_digs.o \
164 ltm/bn_s_mp_mul_high_digs.o ltm/bn_s_mp_sqr.o ltm/bn_s_mp_sub.o
139 ltm/bn_mp_cmp_d.o ltm/bn_mp_cmp_mag.o ltm/bn_mp_cnt_lsb.o ltm/bn_mp_complement.o \
140 ltm/bn_mp_copy.o ltm/bn_mp_count_bits.o ltm/bn_mp_div.o ltm/bn_mp_div_2.o ltm/bn_mp_div_2d.o \
141 ltm/bn_mp_div_3.o ltm/bn_mp_div_d.o ltm/bn_mp_dr_is_modulus.o ltm/bn_mp_dr_reduce.o \
142 ltm/bn_mp_dr_setup.o ltm/bn_mp_exch.o ltm/bn_mp_export.o ltm/bn_mp_exptmod.o ltm/bn_mp_exptmod_fast.o \
143 ltm/bn_mp_expt_d.o ltm/bn_mp_expt_d_ex.o ltm/bn_mp_exteuclid.o ltm/bn_mp_fread.o \
144 ltm/bn_mp_fwrite.o ltm/bn_mp_gcd.o ltm/bn_mp_get_int.o ltm/bn_mp_get_long.o ltm/bn_mp_grow.o \
145 ltm/bn_mp_import.o ltm/bn_mp_init.o ltm/bn_mp_init_copy.o ltm/bn_mp_init_multi.o \
146 ltm/bn_mp_init_set.o ltm/bn_mp_init_set_int.o ltm/bn_mp_init_size.o ltm/bn_mp_invmod.o \
147 ltm/bn_mp_invmod_slow.o ltm/bn_mp_is_square.o ltm/bn_mp_jacobi.o ltm/bn_mp_karatsuba_mul.o \
148 ltm/bn_mp_karatsuba_sqr.o ltm/bn_mp_lcm.o ltm/bn_mp_lshd.o ltm/bn_mp_mod.o ltm/bn_mp_mod_2d.o \
149 ltm/bn_mp_mod_d.o ltm/bn_mp_montgomery_calc_normalization.o ltm/bn_mp_montgomery_reduce.o \
150 ltm/bn_mp_montgomery_setup.o ltm/bn_mp_mul.o ltm/bn_mp_mulmod.o ltm/bn_mp_mul_2.o \
151 ltm/bn_mp_mul_2d.o ltm/bn_mp_mul_d.o ltm/bn_mp_neg.o ltm/bn_mp_n_root.o ltm/bn_mp_n_root_ex.o \
152 ltm/bn_mp_or.o ltm/bn_mp_prime_fermat.o ltm/bn_mp_prime_is_divisible.o ltm/bn_mp_prime_is_prime.o \
153 ltm/bn_mp_prime_miller_rabin.o ltm/bn_mp_prime_next_prime.o ltm/bn_mp_prime_rabin_miller_trials.o \
154 ltm/bn_mp_prime_random_ex.o ltm/bn_mp_radix_size.o ltm/bn_mp_radix_smap.o ltm/bn_mp_rand.o \
155 ltm/bn_mp_read_radix.o ltm/bn_mp_read_signed_bin.o ltm/bn_mp_read_unsigned_bin.o \
156 ltm/bn_mp_reduce.o ltm/bn_mp_reduce_2k.o ltm/bn_mp_reduce_2k_l.o ltm/bn_mp_reduce_2k_setup.o \
157 ltm/bn_mp_reduce_2k_setup_l.o ltm/bn_mp_reduce_is_2k.o ltm/bn_mp_reduce_is_2k_l.o \
158 ltm/bn_mp_reduce_setup.o ltm/bn_mp_rshd.o ltm/bn_mp_set.o ltm/bn_mp_set_int.o ltm/bn_mp_set_long.o \
159 ltm/bn_mp_shrink.o ltm/bn_mp_signed_bin_size.o ltm/bn_mp_sqr.o ltm/bn_mp_sqrmod.o \
160 ltm/bn_mp_sqrt.o ltm/bn_mp_sqrtmod_prime.o ltm/bn_mp_sub.o ltm/bn_mp_submod.o ltm/bn_mp_sub_d.o \
161 ltm/bn_mp_tc_and.o ltm/bn_mp_tc_div_2d.o ltm/bn_mp_tc_or.o ltm/bn_mp_tc_xor.o ltm/bn_mp_toom_mul.o \
162 ltm/bn_mp_toom_sqr.o ltm/bn_mp_toradix.o ltm/bn_mp_toradix_n.o ltm/bn_mp_to_signed_bin.o \
163 ltm/bn_mp_to_signed_bin_n.o ltm/bn_mp_to_unsigned_bin.o ltm/bn_mp_to_unsigned_bin_n.o \
164 ltm/bn_mp_unsigned_bin_size.o ltm/bn_mp_xor.o ltm/bn_mp_zero.o ltm/bn_prime_tab.o \
165 ltm/bn_reverse.o ltm/bn_s_mp_add.o ltm/bn_s_mp_exptmod.o ltm/bn_s_mp_mul_digs.o ltm/bn_s_mp_mul_high_digs.o \
166 ltm/bn_s_mp_sqr.o ltm/bn_s_mp_sub.o
165167
166168 LIB_EXT =.a
167169 OBJ_EXT =.o
144144 ltm/bn_fast_s_mp_sqr.obj ltm/bn_mp_2expt.obj ltm/bn_mp_abs.obj ltm/bn_mp_add.obj \
145145 ltm/bn_mp_addmod.obj ltm/bn_mp_add_d.obj ltm/bn_mp_and.obj ltm/bn_mp_clamp.obj ltm/bn_mp_clear.obj \
146146 ltm/bn_mp_clear_multi.obj ltm/bn_mp_cmp.obj ltm/bn_mp_cmp_d.obj ltm/bn_mp_cmp_mag.obj \
147 ltm/bn_mp_cnt_lsb.obj ltm/bn_mp_copy.obj ltm/bn_mp_count_bits.obj ltm/bn_mp_div.obj \
148 ltm/bn_mp_div_2.obj ltm/bn_mp_div_2d.obj ltm/bn_mp_div_3.obj ltm/bn_mp_div_d.obj \
147 ltm/bn_mp_cnt_lsb.obj ltm/bn_mp_complement.obj ltm/bn_mp_copy.obj ltm/bn_mp_count_bits.obj \
148 ltm/bn_mp_div.obj ltm/bn_mp_div_2.obj ltm/bn_mp_div_2d.obj ltm/bn_mp_div_3.obj ltm/bn_mp_div_d.obj \
149149 ltm/bn_mp_dr_is_modulus.obj ltm/bn_mp_dr_reduce.obj ltm/bn_mp_dr_setup.obj ltm/bn_mp_exch.obj \
150150 ltm/bn_mp_export.obj ltm/bn_mp_exptmod.obj ltm/bn_mp_exptmod_fast.obj ltm/bn_mp_expt_d.obj \
151151 ltm/bn_mp_expt_d_ex.obj ltm/bn_mp_exteuclid.obj ltm/bn_mp_fread.obj ltm/bn_mp_fwrite.obj \
167167 ltm/bn_mp_rshd.obj ltm/bn_mp_set.obj ltm/bn_mp_set_int.obj ltm/bn_mp_set_long.obj \
168168 ltm/bn_mp_shrink.obj ltm/bn_mp_signed_bin_size.obj ltm/bn_mp_sqr.obj ltm/bn_mp_sqrmod.obj \
169169 ltm/bn_mp_sqrt.obj ltm/bn_mp_sqrtmod_prime.obj ltm/bn_mp_sub.obj ltm/bn_mp_submod.obj \
170 ltm/bn_mp_sub_d.obj ltm/bn_mp_toom_mul.obj ltm/bn_mp_toom_sqr.obj ltm/bn_mp_toradix.obj \
170 ltm/bn_mp_sub_d.obj ltm/bn_mp_tc_and.obj ltm/bn_mp_tc_div_2d.obj ltm/bn_mp_tc_or.obj \
171 ltm/bn_mp_tc_xor.obj ltm/bn_mp_toom_mul.obj ltm/bn_mp_toom_sqr.obj ltm/bn_mp_toradix.obj \
171172 ltm/bn_mp_toradix_n.obj ltm/bn_mp_to_signed_bin.obj ltm/bn_mp_to_signed_bin_n.obj \
172173 ltm/bn_mp_to_unsigned_bin.obj ltm/bn_mp_to_unsigned_bin_n.obj ltm/bn_mp_unsigned_bin_size.obj \
173174 ltm/bn_mp_xor.obj ltm/bn_mp_zero.obj ltm/bn_prime_tab.obj ltm/bn_reverse.obj ltm/bn_s_mp_add.obj \
137137 goto LBL_ERR;
138138 }
139139 }
140
141 /* too big */
142 while (mp_cmp_mag(&D, b) != MP_LT) {
143 if ((res = mp_sub(&D, b, &D)) != MP_OKAY) {
144 goto LBL_ERR;
145 }
146 }
147
140148 mp_exch(&D, c);
141149 c->sign = neg;
142150 res = MP_OKAY;
0 #include "tommath_private.h"
1 #ifdef BN_MP_COMPLEMENT_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 */
14
15 /* b = ~a */
16 int mp_complement(const mp_int *a, mp_int *b)
17 {
18 int res = mp_neg(a, b);
19 return (res == MP_OKAY) ? mp_sub_d(b, 1uL, b) : res;
20 }
21 #endif
22
23 /* ref: $Format:%D$ */
24 /* git commit: $Format:%H$ */
25 /* commit time: $Format:%ai$ */
2121 #define MP_GEN_RANDOM_MAX 0xffffffffu
2222 #define MP_GEN_RANDOM_SHIFT 32
2323
24 static int s_read_arc4random(mp_digit* p)
24 static int s_read_arc4random(mp_digit *p)
2525 {
2626 mp_digit d = 0, msk = 0;
2727 do {
3939 #define MP_WIN_CSP
4040
4141 #ifndef _WIN32_WINNT
42 #define _WIN32_WINNT 0x0400
42 #define _WIN32_WINNT 0x0400
4343 #endif
4444 #ifdef _WIN32_WCE
45 #define UNDER_CE
46 #define ARM
45 #define UNDER_CE
46 #define ARM
4747 #endif
4848
4949 #define WIN32_LEAN_AND_MEAN
5858 hProv = 0;
5959 }
6060
61 static int s_read_win_csp(mp_digit* p)
61 static int s_read_win_csp(mp_digit *p)
6262 {
6363 int ret = -1;
6464 if (hProv == 0) {
6565 if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
6666 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
67 !CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
67 !CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
6868 CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
6969 hProv = 0;
7070 return ret;
7171 }
7272 atexit(s_cleanup_win_csp);
7373 }
74 if (CryptGenRandom(hProv, sizeof(*p), (void*)p) == TRUE) {
74 if (CryptGenRandom(hProv, sizeof(*p), (void *)p) == TRUE) {
7575 ret = MP_OKAY;
7676 }
7777 return ret;
8484 #include <sys/random.h>
8585 #include <errno.h>
8686
87 static int s_read_getrandom(mp_digit* p)
87 static int s_read_getrandom(mp_digit *p)
8888 {
8989 int ret;
9090 do {
9191 ret = getrandom(p, sizeof(*p), 0);
92 } while((ret == -1) && (errno == EINTR));
92 } while ((ret == -1) && (errno == EINTR));
9393 if (ret == sizeof(*p)) return MP_OKAY;
9494 return -1;
9595 }
107107 #include <errno.h>
108108 #include <unistd.h>
109109
110 static int s_read_dev_urandom(mp_digit* p)
110 static int s_read_dev_urandom(mp_digit *p)
111111 {
112112 ssize_t r;
113113 int fd;
114114 do {
115115 fd = open(MP_DEV_URANDOM, O_RDONLY);
116 } while((fd == -1) && (errno == EINTR));
116 } while ((fd == -1) && (errno == EINTR));
117117 if (fd == -1) return -1;
118118 do {
119119 r = read(fd, p, sizeof(*p));
120 } while((r == -1) && (errno == EINTR));
120 } while ((r == -1) && (errno == EINTR));
121121 close(fd);
122122 if (r != sizeof(*p)) return -1;
123123 return MP_OKAY;
128128 unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
129129 void (*ltm_rng_callback)(void);
130130
131 static int s_read_ltm_rng(mp_digit* p)
131 static int s_read_ltm_rng(mp_digit *p)
132132 {
133133 unsigned long ret;
134134 if (ltm_rng == NULL) return -1;
135 ret = ltm_rng((void*)p, sizeof(*p), ltm_rng_callback);
135 ret = ltm_rng((void *)p, sizeof(*p), ltm_rng_callback);
136136 if (ret != sizeof(*p)) return -1;
137137 return MP_OKAY;
138138 }
139139 #endif
140140
141 static int s_rand_digit(mp_digit* p)
141 static int s_rand_digit(mp_digit *p)
142142 {
143143 int ret = -1;
144144
0 #include "tommath_private.h"
1 #ifdef BN_MP_TC_AND_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 */
14
15 /* two complement and */
16 int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
17 {
18 int res = MP_OKAY, bits;
19 int as = mp_isneg(a), bs = mp_isneg(b);
20 mp_int *mx = NULL, _mx, acpy, bcpy;
21
22 if ((as != MP_NO) || (bs != MP_NO)) {
23 bits = MAX(mp_count_bits(a), mp_count_bits(b));
24 res = mp_init_set_int(&_mx, 1uL);
25 if (res != MP_OKAY) {
26 goto end;
27 }
28
29 mx = &_mx;
30 res = mp_mul_2d(mx, bits + 1, mx);
31 if (res != MP_OKAY) {
32 goto end;
33 }
34
35 if (as != MP_NO) {
36 res = mp_init(&acpy);
37 if (res != MP_OKAY) {
38 goto end;
39 }
40
41 res = mp_add(mx, a, &acpy);
42 if (res != MP_OKAY) {
43 mp_clear(&acpy);
44 goto end;
45 }
46 a = &acpy;
47 }
48 if (bs != MP_NO) {
49 res = mp_init(&bcpy);
50 if (res != MP_OKAY) {
51 goto end;
52 }
53
54 res = mp_add(mx, b, &bcpy);
55 if (res != MP_OKAY) {
56 mp_clear(&bcpy);
57 goto end;
58 }
59 b = &bcpy;
60 }
61 }
62
63 res = mp_and(a, b, c);
64
65 if ((as != MP_NO) && (bs != MP_NO) && (res == MP_OKAY)) {
66 res = mp_sub(c, mx, c);
67 }
68
69 end:
70 if (a == &acpy) {
71 mp_clear(&acpy);
72 }
73
74 if (b == &bcpy) {
75 mp_clear(&bcpy);
76 }
77
78 if (mx == &_mx) {
79 mp_clear(mx);
80 }
81
82 return res;
83 }
84 #endif
85
86 /* ref: $Format:%D$ */
87 /* git commit: $Format:%H$ */
88 /* commit time: $Format:%ai$ */
0 #include "tommath_private.h"
1 #ifdef BN_MP_TC_DIV_2D_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 */
14
15 /* two complement right shift */
16 int mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
17 {
18 int res;
19 if (mp_isneg(a) == MP_NO) {
20 return mp_div_2d(a, b, c, NULL);
21 }
22
23 res = mp_add_d(a, 1uL, c);
24 if (res != MP_OKAY) {
25 return res;
26 }
27
28 res = mp_div_2d(c, b, c, NULL);
29 return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res;
30 }
31 #endif
32
33 /* ref: $Format:%D$ */
34 /* git commit: $Format:%H$ */
35 /* commit time: $Format:%ai$ */
0 #include "tommath_private.h"
1 #ifdef BN_MP_TC_OR_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 */
14
15 /* two complement or */
16 int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
17 {
18 int res = MP_OKAY, bits;
19 int as = mp_isneg(a), bs = mp_isneg(b);
20 mp_int *mx = NULL, _mx, acpy, bcpy;
21
22 if ((as != MP_NO) || (bs != MP_NO)) {
23 bits = MAX(mp_count_bits(a), mp_count_bits(b));
24 res = mp_init_set_int(&_mx, 1uL);
25 if (res != MP_OKAY) {
26 goto end;
27 }
28
29 mx = &_mx;
30 res = mp_mul_2d(mx, bits + 1, mx);
31 if (res != MP_OKAY) {
32 goto end;
33 }
34
35 if (as != MP_NO) {
36 res = mp_init(&acpy);
37 if (res != MP_OKAY) {
38 goto end;
39 }
40
41 res = mp_add(mx, a, &acpy);
42 if (res != MP_OKAY) {
43 mp_clear(&acpy);
44 goto end;
45 }
46 a = &acpy;
47 }
48 if (bs != MP_NO) {
49 res = mp_init(&bcpy);
50 if (res != MP_OKAY) {
51 goto end;
52 }
53
54 res = mp_add(mx, b, &bcpy);
55 if (res != MP_OKAY) {
56 mp_clear(&bcpy);
57 goto end;
58 }
59 b = &bcpy;
60 }
61 }
62
63 res = mp_or(a, b, c);
64
65 if (((as != MP_NO) || (bs != MP_NO)) && (res == MP_OKAY)) {
66 res = mp_sub(c, mx, c);
67 }
68
69 end:
70 if (a == &acpy) {
71 mp_clear(&acpy);
72 }
73
74 if (b == &bcpy) {
75 mp_clear(&bcpy);
76 }
77
78 if (mx == &_mx) {
79 mp_clear(mx);
80 }
81
82 return res;
83 }
84 #endif
85
86 /* ref: $Format:%D$ */
87 /* git commit: $Format:%H$ */
88 /* commit time: $Format:%ai$ */
0 #include "tommath_private.h"
1 #ifdef BN_MP_TC_XOR_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 */
14
15 /* two complement xor */
16 int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
17 {
18 int res = MP_OKAY, bits;
19 int as = mp_isneg(a), bs = mp_isneg(b);
20 mp_int *mx = NULL, _mx, acpy, bcpy;
21
22 if ((as != MP_NO) || (bs != MP_NO)) {
23 bits = MAX(mp_count_bits(a), mp_count_bits(b));
24 res = mp_init_set_int(&_mx, 1uL);
25 if (res != MP_OKAY) {
26 goto end;
27 }
28
29 mx = &_mx;
30 res = mp_mul_2d(mx, bits + 1, mx);
31 if (res != MP_OKAY) {
32 goto end;
33 }
34
35 if (as != MP_NO) {
36 res = mp_init(&acpy);
37 if (res != MP_OKAY) {
38 goto end;
39 }
40
41 res = mp_add(mx, a, &acpy);
42 if (res != MP_OKAY) {
43 mp_clear(&acpy);
44 goto end;
45 }
46 a = &acpy;
47 }
48 if (bs != MP_NO) {
49 res = mp_init(&bcpy);
50 if (res != MP_OKAY) {
51 goto end;
52 }
53
54 res = mp_add(mx, b, &bcpy);
55 if (res != MP_OKAY) {
56 mp_clear(&bcpy);
57 goto end;
58 }
59 b = &bcpy;
60 }
61 }
62
63 res = mp_xor(a, b, c);
64
65 if ((as != bs) && (res == MP_OKAY)) {
66 res = mp_sub(c, mx, c);
67 }
68
69 end:
70 if (a == &acpy) {
71 mp_clear(&acpy);
72 }
73
74 if (b == &bcpy) {
75 mp_clear(&bcpy);
76 }
77
78 if (mx == &_mx) {
79 mp_clear(mx);
80 }
81
82 return res;
83 }
84 #endif
85
86 /* ref: $Format:%D$ */
87 /* git commit: $Format:%H$ */
88 /* commit time: $Format:%ai$ */
3636 defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
3737 # if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
3838 # if defined(__GNUC__)
39 /* we support 128bit integers only via: __attribute__((mode(TI))) */
39 /* we support 128bit integers only via: __attribute__((mode(TI))) */
4040 # define MP_64BIT
4141 # else
42 /* otherwise we fall back to MP_32BIT even on 64bit platforms */
42 /* otherwise we fall back to MP_32BIT even on 64bit platforms */
4343 # define MP_32BIT
4444 # endif
4545 # endif
294294 /* c = a AND b */
295295 int mp_and(const mp_int *a, const mp_int *b, mp_int *c);
296296
297 /* c = a XOR b (two complement) */
298 int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c);
299
300 /* c = a OR b (two complement) */
301 int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c);
302
303 /* c = a AND b (two complement) */
304 int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c);
305
306 /* right shift (two complement) */
307 int mp_tc_div_2d(const mp_int *a, int b, mp_int *c);
308
297309 /* ---> Basic arithmetic <--- */
310
311 /* b = ~a */
312 int mp_complement(const mp_int *a, mp_int *b);
298313
299314 /* b = -a */
300315 int mp_neg(const mp_int *a, mp_int *b);
2626 # define BN_MP_CMP_D_C
2727 # define BN_MP_CMP_MAG_C
2828 # define BN_MP_CNT_LSB_C
29 # define BN_MP_COMPLEMENT_C
2930 # define BN_MP_COPY_C
3031 # define BN_MP_COUNT_BITS_C
3132 # define BN_MP_DIV_C
115116 # define BN_MP_SUB_C
116117 # define BN_MP_SUB_D_C
117118 # define BN_MP_SUBMOD_C
119 # define BN_MP_TC_AND_C
120 # define BN_MP_TC_DIV_2D_C
121 # define BN_MP_TC_OR_C
122 # define BN_MP_TC_XOR_C
118123 # define BN_MP_TO_SIGNED_BIN_C
119124 # define BN_MP_TO_SIGNED_BIN_N_C
120125 # define BN_MP_TO_UNSIGNED_BIN_C
146151 # define BN_MP_INIT_MULTI_C
147152 # define BN_MP_COPY_C
148153 # define BN_MP_MOD_C
154 # define BN_MP_ISZERO_C
149155 # define BN_MP_SET_C
150156 # define BN_MP_DIV_2_C
151157 # define BN_MP_ISODD_C
152158 # define BN_MP_SUB_C
153159 # define BN_MP_CMP_C
154 # define BN_MP_ISZERO_C
155160 # define BN_MP_CMP_D_C
156161 # define BN_MP_ADD_C
157162 # define BN_MP_EXCH_C
238243
239244 #if defined(BN_MP_CNT_LSB_C)
240245 # define BN_MP_ISZERO_C
246 #endif
247
248 #if defined(BN_MP_COMPLEMENT_C)
249 # define BN_MP_NEG_C
250 # define BN_MP_SUB_D_C
241251 #endif
242252
243253 #if defined(BN_MP_COPY_C)
389399
390400 #if defined(BN_MP_FREAD_C)
391401 # define BN_MP_ZERO_C
392 # define BN_MP_S_RMAP_C
402 # define BN_MP_S_RMAP_REVERSE_SZ_C
403 # define BN_MP_S_RMAP_REVERSE_C
393404 # define BN_MP_MUL_D_C
394405 # define BN_MP_ADD_D_C
395406 # define BN_MP_CMP_D_C
461472 #endif
462473
463474 #if defined(BN_MP_INVMOD_C)
464 # define BN_MP_ISZERO_C
475 # define BN_MP_CMP_D_C
465476 # define BN_MP_ISODD_C
466 # define BN_MP_CMP_D_C
467477 # define BN_FAST_MP_INVMOD_C
468478 # define BN_MP_INVMOD_SLOW_C
469479 #endif
540550 #endif
541551
542552 #if defined(BN_MP_LSHD_C)
553 # define BN_MP_ISZERO_C
543554 # define BN_MP_GROW_C
544555 # define BN_MP_RSHD_C
545556 #endif
713724
714725 #if defined(BN_MP_RADIX_SMAP_C)
715726 # define BN_MP_S_RMAP_C
727 # define BN_MP_S_RMAP_REVERSE_C
728 # define BN_MP_S_RMAP_REVERSE_SZ_C
716729 #endif
717730
718731 #if defined(BN_MP_RAND_C)
723736
724737 #if defined(BN_MP_READ_RADIX_C)
725738 # define BN_MP_ZERO_C
726 # define BN_MP_S_RMAP_C
739 # define BN_MP_S_RMAP_REVERSE_SZ_C
740 # define BN_MP_S_RMAP_REVERSE_C
727741 # define BN_MP_MUL_D_C
728742 # define BN_MP_ADD_D_C
729743 # define BN_MP_ISZERO_C
903917 # define BN_MP_MOD_C
904918 #endif
905919
920 #if defined(BN_MP_TC_AND_C)
921 # define BN_MP_ISNEG_C
922 # define BN_MP_COUNT_BITS_C
923 # define BN_MP_INIT_SET_INT_C
924 # define BN_MP_MUL_2D_C
925 # define BN_MP_INIT_C
926 # define BN_MP_ADD_C
927 # define BN_MP_CLEAR_C
928 # define BN_MP_AND_C
929 # define BN_MP_SUB_C
930 #endif
931
932 #if defined(BN_MP_TC_DIV_2D_C)
933 # define BN_MP_ISNEG_C
934 # define BN_MP_DIV_2D_C
935 # define BN_MP_ADD_D_C
936 # define BN_MP_SUB_D_C
937 #endif
938
939 #if defined(BN_MP_TC_OR_C)
940 # define BN_MP_ISNEG_C
941 # define BN_MP_COUNT_BITS_C
942 # define BN_MP_INIT_SET_INT_C
943 # define BN_MP_MUL_2D_C
944 # define BN_MP_INIT_C
945 # define BN_MP_ADD_C
946 # define BN_MP_CLEAR_C
947 # define BN_MP_OR_C
948 # define BN_MP_SUB_C
949 #endif
950
951 #if defined(BN_MP_TC_XOR_C)
952 # define BN_MP_ISNEG_C
953 # define BN_MP_COUNT_BITS_C
954 # define BN_MP_INIT_SET_INT_C
955 # define BN_MP_MUL_2D_C
956 # define BN_MP_INIT_C
957 # define BN_MP_ADD_C
958 # define BN_MP_CLEAR_C
959 # define BN_MP_XOR_C
960 # define BN_MP_SUB_C
961 #endif
962
906963 #if defined(BN_MP_TO_SIGNED_BIN_C)
907964 # define BN_MP_TO_UNSIGNED_BIN_C
908965 #endif