comparison libtommath/bn_mp_tc_and.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include "tommath_private.h"
2 #ifdef BN_MP_TC_AND_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * SPDX-License-Identifier: Unlicense
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, abits, bbits;
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 abits = mp_count_bits(a);
24 bbits = mp_count_bits(b);
25 bits = MAX(abits, bbits);
26 res = mp_init_set_int(&_mx, 1uL);
27 if (res != MP_OKAY) {
28 goto end;
29 }
30
31 mx = &_mx;
32 res = mp_mul_2d(mx, bits + 1, mx);
33 if (res != MP_OKAY) {
34 goto end;
35 }
36
37 if (as != MP_NO) {
38 res = mp_init(&acpy);
39 if (res != MP_OKAY) {
40 goto end;
41 }
42
43 res = mp_add(mx, a, &acpy);
44 if (res != MP_OKAY) {
45 mp_clear(&acpy);
46 goto end;
47 }
48 a = &acpy;
49 }
50 if (bs != MP_NO) {
51 res = mp_init(&bcpy);
52 if (res != MP_OKAY) {
53 goto end;
54 }
55
56 res = mp_add(mx, b, &bcpy);
57 if (res != MP_OKAY) {
58 mp_clear(&bcpy);
59 goto end;
60 }
61 b = &bcpy;
62 }
63 }
64
65 res = mp_and(a, b, c);
66
67 if ((as != MP_NO) && (bs != MP_NO) && (res == MP_OKAY)) {
68 res = mp_sub(c, mx, c);
69 }
70
71 end:
72 if (a == &acpy) {
73 mp_clear(&acpy);
74 }
75
76 if (b == &bcpy) {
77 mp_clear(&bcpy);
78 }
79
80 if (mx == &_mx) {
81 mp_clear(mx);
82 }
83
84 return res;
85 }
86 #endif
87
88 /* ref: HEAD -> master, tag: v1.1.0 */
89 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
90 /* commit time: 2019-01-28 20:32:32 +0100 */