/src/openssl30/crypto/bn/bn_add.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/cryptlib.h" |
11 | | #include "bn_local.h" |
12 | | |
13 | | /* signed add of b to a. */ |
14 | | int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
15 | 37.5M | { |
16 | 37.5M | int ret, r_neg, cmp_res; |
17 | | |
18 | 37.5M | bn_check_top(a); |
19 | 37.5M | bn_check_top(b); |
20 | | |
21 | 37.5M | if (a->neg == b->neg) { |
22 | 20.7M | r_neg = a->neg; |
23 | 20.7M | ret = BN_uadd(r, a, b); |
24 | 20.7M | } else { |
25 | 16.7M | cmp_res = BN_ucmp(a, b); |
26 | 16.7M | if (cmp_res > 0) { |
27 | 4.28M | r_neg = a->neg; |
28 | 4.28M | ret = BN_usub(r, a, b); |
29 | 12.5M | } else if (cmp_res < 0) { |
30 | 12.5M | r_neg = b->neg; |
31 | 12.5M | ret = BN_usub(r, b, a); |
32 | 12.5M | } else { |
33 | 2.32k | r_neg = 0; |
34 | 2.32k | BN_zero(r); |
35 | 2.32k | ret = 1; |
36 | 2.32k | } |
37 | 16.7M | } |
38 | | |
39 | 37.5M | r->neg = r_neg; |
40 | 37.5M | bn_check_top(r); |
41 | 37.5M | return ret; |
42 | 37.5M | } |
43 | | |
44 | | /* signed sub of b from a. */ |
45 | | int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
46 | 19.5M | { |
47 | 19.5M | int ret, r_neg, cmp_res; |
48 | | |
49 | 19.5M | bn_check_top(a); |
50 | 19.5M | bn_check_top(b); |
51 | | |
52 | 19.5M | if (a->neg != b->neg) { |
53 | 0 | r_neg = a->neg; |
54 | 0 | ret = BN_uadd(r, a, b); |
55 | 19.5M | } else { |
56 | 19.5M | cmp_res = BN_ucmp(a, b); |
57 | 19.5M | if (cmp_res > 0) { |
58 | 12.5M | r_neg = a->neg; |
59 | 12.5M | ret = BN_usub(r, a, b); |
60 | 12.5M | } else if (cmp_res < 0) { |
61 | 6.84M | r_neg = !b->neg; |
62 | 6.84M | ret = BN_usub(r, b, a); |
63 | 6.84M | } else { |
64 | 75.4k | r_neg = 0; |
65 | 75.4k | BN_zero(r); |
66 | 75.4k | ret = 1; |
67 | 75.4k | } |
68 | 19.5M | } |
69 | | |
70 | 19.5M | r->neg = r_neg; |
71 | 19.5M | bn_check_top(r); |
72 | 19.5M | return ret; |
73 | 19.5M | } |
74 | | |
75 | | /* unsigned add of b to a, r can be equal to a or b. */ |
76 | | int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
77 | 91.6M | { |
78 | 91.6M | int max, min, dif; |
79 | 91.6M | const BN_ULONG *ap, *bp; |
80 | 91.6M | BN_ULONG *rp, carry, t1, t2; |
81 | | |
82 | 91.6M | bn_check_top(a); |
83 | 91.6M | bn_check_top(b); |
84 | | |
85 | 91.6M | if (a->top < b->top) { |
86 | 4.53M | const BIGNUM *tmp; |
87 | | |
88 | 4.53M | tmp = a; |
89 | 4.53M | a = b; |
90 | 4.53M | b = tmp; |
91 | 4.53M | } |
92 | 91.6M | max = a->top; |
93 | 91.6M | min = b->top; |
94 | 91.6M | dif = max - min; |
95 | | |
96 | 91.6M | if (bn_wexpand(r, max + 1) == NULL) |
97 | 0 | return 0; |
98 | | |
99 | 91.6M | r->top = max; |
100 | | |
101 | 91.6M | ap = a->d; |
102 | 91.6M | bp = b->d; |
103 | 91.6M | rp = r->d; |
104 | | |
105 | 91.6M | carry = bn_add_words(rp, ap, bp, min); |
106 | 91.6M | rp += min; |
107 | 91.6M | ap += min; |
108 | | |
109 | 115M | while (dif) { |
110 | 23.9M | dif--; |
111 | 23.9M | t1 = *(ap++); |
112 | 23.9M | t2 = (t1 + carry) & BN_MASK2; |
113 | 23.9M | *(rp++) = t2; |
114 | 23.9M | carry &= (t2 == 0); |
115 | 23.9M | } |
116 | 91.6M | *rp = carry; |
117 | 91.6M | r->top += carry; |
118 | | |
119 | 91.6M | r->neg = 0; |
120 | 91.6M | bn_check_top(r); |
121 | 91.6M | return 1; |
122 | 91.6M | } |
123 | | |
124 | | /* unsigned subtraction of b from a, a must be larger than b. */ |
125 | | int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
126 | 80.7M | { |
127 | 80.7M | int max, min, dif; |
128 | 80.7M | BN_ULONG t1, t2, borrow, *rp; |
129 | 80.7M | const BN_ULONG *ap, *bp; |
130 | | |
131 | 80.7M | bn_check_top(a); |
132 | 80.7M | bn_check_top(b); |
133 | | |
134 | 80.7M | max = a->top; |
135 | 80.7M | min = b->top; |
136 | 80.7M | dif = max - min; |
137 | | |
138 | 80.7M | if (dif < 0) { /* hmm... should not be happening */ |
139 | 0 | ERR_raise(ERR_LIB_BN, BN_R_ARG2_LT_ARG3); |
140 | 0 | return 0; |
141 | 0 | } |
142 | | |
143 | 80.7M | if (bn_wexpand(r, max) == NULL) |
144 | 0 | return 0; |
145 | | |
146 | 80.7M | ap = a->d; |
147 | 80.7M | bp = b->d; |
148 | 80.7M | rp = r->d; |
149 | | |
150 | 80.7M | borrow = bn_sub_words(rp, ap, bp, min); |
151 | 80.7M | ap += min; |
152 | 80.7M | rp += min; |
153 | | |
154 | 91.8M | while (dif) { |
155 | 11.1M | dif--; |
156 | 11.1M | t1 = *(ap++); |
157 | 11.1M | t2 = (t1 - borrow) & BN_MASK2; |
158 | 11.1M | *(rp++) = t2; |
159 | 11.1M | borrow &= (t1 == 0); |
160 | 11.1M | } |
161 | | |
162 | 91.0M | while (max && *--rp == 0) |
163 | 10.3M | max--; |
164 | | |
165 | 80.7M | r->top = max; |
166 | 80.7M | r->neg = 0; |
167 | 80.7M | bn_pollute(r); |
168 | | |
169 | 80.7M | return 1; |
170 | 80.7M | } |
171 | | |