/src/openssl35/crypto/bn/bn_add.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 | 60.6M | { |
16 | 60.6M | int ret, r_neg, cmp_res; |
17 | | |
18 | 60.6M | bn_check_top(a); |
19 | 60.6M | bn_check_top(b); |
20 | | |
21 | 60.6M | if (a->neg == b->neg) { |
22 | 35.5M | r_neg = a->neg; |
23 | 35.5M | ret = BN_uadd(r, a, b); |
24 | 35.5M | } else { |
25 | 25.0M | cmp_res = BN_ucmp(a, b); |
26 | 25.0M | if (cmp_res > 0) { |
27 | 8.15M | r_neg = a->neg; |
28 | 8.15M | ret = BN_usub(r, a, b); |
29 | 16.9M | } else if (cmp_res < 0) { |
30 | 16.9M | r_neg = b->neg; |
31 | 16.9M | ret = BN_usub(r, b, a); |
32 | 16.9M | } else { |
33 | 5.93k | r_neg = 0; |
34 | 5.93k | BN_zero(r); |
35 | 5.93k | ret = 1; |
36 | 5.93k | } |
37 | 25.0M | } |
38 | | |
39 | 60.6M | r->neg = r_neg; |
40 | 60.6M | bn_check_top(r); |
41 | 60.6M | return ret; |
42 | 60.6M | } |
43 | | |
44 | | /* signed sub of b from a. */ |
45 | | int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
46 | 15.5M | { |
47 | 15.5M | int ret, r_neg, cmp_res; |
48 | | |
49 | 15.5M | bn_check_top(a); |
50 | 15.5M | bn_check_top(b); |
51 | | |
52 | 15.5M | if (a->neg != b->neg) { |
53 | 0 | r_neg = a->neg; |
54 | 0 | ret = BN_uadd(r, a, b); |
55 | 15.5M | } else { |
56 | 15.5M | cmp_res = BN_ucmp(a, b); |
57 | 15.5M | if (cmp_res > 0) { |
58 | 9.59M | r_neg = a->neg; |
59 | 9.59M | ret = BN_usub(r, a, b); |
60 | 9.59M | } else if (cmp_res < 0) { |
61 | 5.75M | r_neg = !b->neg; |
62 | 5.75M | ret = BN_usub(r, b, a); |
63 | 5.75M | } else { |
64 | 217k | r_neg = 0; |
65 | 217k | BN_zero(r); |
66 | 217k | ret = 1; |
67 | 217k | } |
68 | 15.5M | } |
69 | | |
70 | 15.5M | r->neg = r_neg; |
71 | 15.5M | bn_check_top(r); |
72 | 15.5M | return ret; |
73 | 15.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 | 106M | { |
78 | 106M | int max, min, dif; |
79 | 106M | const BN_ULONG *ap, *bp; |
80 | 106M | BN_ULONG *rp, carry, t1, t2; |
81 | | |
82 | 106M | bn_check_top(a); |
83 | 106M | bn_check_top(b); |
84 | | |
85 | 106M | if (a->top < b->top) { |
86 | 7.63M | const BIGNUM *tmp; |
87 | | |
88 | 7.63M | tmp = a; |
89 | 7.63M | a = b; |
90 | 7.63M | b = tmp; |
91 | 7.63M | } |
92 | 106M | max = a->top; |
93 | 106M | min = b->top; |
94 | 106M | dif = max - min; |
95 | | |
96 | 106M | if (bn_wexpand(r, max + 1) == NULL) |
97 | 0 | return 0; |
98 | | |
99 | 106M | r->top = max; |
100 | 106M | if (max == 0) |
101 | 1.52k | goto end; |
102 | | |
103 | 106M | ap = a->d; |
104 | 106M | bp = b->d; |
105 | 106M | rp = r->d; |
106 | | |
107 | 106M | carry = bn_add_words(rp, ap, bp, min); |
108 | 106M | rp += min; |
109 | 106M | ap += min; |
110 | | |
111 | 171M | while (dif) { |
112 | 64.6M | dif--; |
113 | 64.6M | t1 = *(ap++); |
114 | 64.6M | t2 = (t1 + carry) & BN_MASK2; |
115 | 64.6M | *(rp++) = t2; |
116 | 64.6M | carry &= (t2 == 0); |
117 | 64.6M | } |
118 | 106M | *rp = carry; |
119 | 106M | r->top += carry; |
120 | | |
121 | 106M | end: |
122 | 106M | r->neg = 0; |
123 | 106M | bn_check_top(r); |
124 | 106M | return 1; |
125 | 106M | } |
126 | | |
127 | | /* unsigned subtraction of b from a, a must be larger than b. */ |
128 | | int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
129 | 86.8M | { |
130 | 86.8M | int max, min, dif; |
131 | 86.8M | BN_ULONG t1, t2, borrow, *rp; |
132 | 86.8M | const BN_ULONG *ap, *bp; |
133 | | |
134 | 86.8M | bn_check_top(a); |
135 | 86.8M | bn_check_top(b); |
136 | | |
137 | 86.8M | max = a->top; |
138 | 86.8M | min = b->top; |
139 | 86.8M | dif = max - min; |
140 | | |
141 | 86.8M | if (dif < 0) { /* hmm... should not be happening */ |
142 | 0 | ERR_raise(ERR_LIB_BN, BN_R_ARG2_LT_ARG3); |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | 86.8M | if (bn_wexpand(r, max) == NULL) |
147 | 0 | return 0; |
148 | | |
149 | 86.8M | if (max == 0) |
150 | 0 | goto end; |
151 | | |
152 | 86.8M | ap = a->d; |
153 | 86.8M | bp = b->d; |
154 | 86.8M | rp = r->d; |
155 | | |
156 | 86.8M | borrow = bn_sub_words(rp, ap, bp, min); |
157 | 86.8M | ap += min; |
158 | 86.8M | rp += min; |
159 | | |
160 | 129M | while (dif) { |
161 | 42.2M | dif--; |
162 | 42.2M | t1 = *(ap++); |
163 | 42.2M | t2 = (t1 - borrow) & BN_MASK2; |
164 | 42.2M | *(rp++) = t2; |
165 | 42.2M | borrow &= (t1 == 0); |
166 | 42.2M | } |
167 | | |
168 | 105M | while (max && *--rp == 0) |
169 | 18.4M | max--; |
170 | | |
171 | 86.8M | end: |
172 | 86.8M | r->top = max; |
173 | 86.8M | r->neg = 0; |
174 | 86.8M | bn_pollute(r); |
175 | | |
176 | 86.8M | return 1; |
177 | 86.8M | } |