/src/openssl36/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 | 85.4M | { |
16 | 85.4M | int ret, r_neg, cmp_res; |
17 | | |
18 | 85.4M | bn_check_top(a); |
19 | 85.4M | bn_check_top(b); |
20 | | |
21 | 85.4M | if (a->neg == b->neg) { |
22 | 49.7M | r_neg = a->neg; |
23 | 49.7M | ret = BN_uadd(r, a, b); |
24 | 49.7M | } else { |
25 | 35.6M | cmp_res = BN_ucmp(a, b); |
26 | 35.6M | if (cmp_res > 0) { |
27 | 12.5M | r_neg = a->neg; |
28 | 12.5M | ret = BN_usub(r, a, b); |
29 | 23.1M | } else if (cmp_res < 0) { |
30 | 23.1M | r_neg = b->neg; |
31 | 23.1M | ret = BN_usub(r, b, a); |
32 | 23.1M | } else { |
33 | 10.1k | r_neg = 0; |
34 | 10.1k | BN_zero(r); |
35 | 10.1k | ret = 1; |
36 | 10.1k | } |
37 | 35.6M | } |
38 | | |
39 | 85.4M | r->neg = r_neg; |
40 | 85.4M | bn_check_top(r); |
41 | 85.4M | return ret; |
42 | 85.4M | } |
43 | | |
44 | | /* signed sub of b from a. */ |
45 | | int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
46 | 17.2M | { |
47 | 17.2M | int ret, r_neg, cmp_res; |
48 | | |
49 | 17.2M | bn_check_top(a); |
50 | 17.2M | bn_check_top(b); |
51 | | |
52 | 17.2M | if (a->neg != b->neg) { |
53 | 0 | r_neg = a->neg; |
54 | 0 | ret = BN_uadd(r, a, b); |
55 | 17.2M | } else { |
56 | 17.2M | cmp_res = BN_ucmp(a, b); |
57 | 17.2M | if (cmp_res > 0) { |
58 | 10.8M | r_neg = a->neg; |
59 | 10.8M | ret = BN_usub(r, a, b); |
60 | 10.8M | } else if (cmp_res < 0) { |
61 | 6.21M | r_neg = !b->neg; |
62 | 6.21M | ret = BN_usub(r, b, a); |
63 | 6.21M | } else { |
64 | 236k | r_neg = 0; |
65 | 236k | BN_zero(r); |
66 | 236k | ret = 1; |
67 | 236k | } |
68 | 17.2M | } |
69 | | |
70 | 17.2M | r->neg = r_neg; |
71 | 17.2M | bn_check_top(r); |
72 | 17.2M | return ret; |
73 | 17.2M | } |
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 | 133M | { |
78 | 133M | int max, min, dif; |
79 | 133M | const BN_ULONG *ap, *bp; |
80 | 133M | BN_ULONG *rp, carry, t1, t2; |
81 | | |
82 | 133M | bn_check_top(a); |
83 | 133M | bn_check_top(b); |
84 | | |
85 | 133M | if (a->top < b->top) { |
86 | 11.2M | const BIGNUM *tmp; |
87 | | |
88 | 11.2M | tmp = a; |
89 | 11.2M | a = b; |
90 | 11.2M | b = tmp; |
91 | 11.2M | } |
92 | 133M | max = a->top; |
93 | 133M | min = b->top; |
94 | 133M | dif = max - min; |
95 | | |
96 | 133M | if (bn_wexpand(r, max + 1) == NULL) |
97 | 0 | return 0; |
98 | | |
99 | 133M | r->top = max; |
100 | 133M | if (max == 0) |
101 | 1.80k | goto end; |
102 | | |
103 | 133M | ap = a->d; |
104 | 133M | bp = b->d; |
105 | 133M | rp = r->d; |
106 | | |
107 | 133M | carry = bn_add_words(rp, ap, bp, min); |
108 | 133M | rp += min; |
109 | 133M | ap += min; |
110 | | |
111 | 207M | while (dif) { |
112 | 73.6M | dif--; |
113 | 73.6M | t1 = *(ap++); |
114 | 73.6M | t2 = (t1 + carry) & BN_MASK2; |
115 | 73.6M | *(rp++) = t2; |
116 | 73.6M | carry &= (t2 == 0); |
117 | 73.6M | } |
118 | 133M | *rp = carry; |
119 | 133M | r->top += (int)carry; |
120 | | |
121 | 133M | end: |
122 | 133M | r->neg = 0; |
123 | 133M | bn_check_top(r); |
124 | 133M | return 1; |
125 | 133M | } |
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 | 108M | { |
130 | 108M | int max, min, dif; |
131 | 108M | BN_ULONG t1, t2, borrow, *rp; |
132 | 108M | const BN_ULONG *ap, *bp; |
133 | | |
134 | 108M | bn_check_top(a); |
135 | 108M | bn_check_top(b); |
136 | | |
137 | 108M | max = a->top; |
138 | 108M | min = b->top; |
139 | 108M | dif = max - min; |
140 | | |
141 | 108M | 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 | 108M | if (bn_wexpand(r, max) == NULL) |
147 | 0 | return 0; |
148 | | |
149 | 108M | if (max == 0) |
150 | 0 | goto end; |
151 | | |
152 | 108M | ap = a->d; |
153 | 108M | bp = b->d; |
154 | 108M | rp = r->d; |
155 | | |
156 | 108M | borrow = bn_sub_words(rp, ap, bp, min); |
157 | 108M | ap += min; |
158 | 108M | rp += min; |
159 | | |
160 | 161M | while (dif) { |
161 | 52.8M | dif--; |
162 | 52.8M | t1 = *(ap++); |
163 | 52.8M | t2 = (t1 - borrow) & BN_MASK2; |
164 | 52.8M | *(rp++) = t2; |
165 | 52.8M | borrow &= (t1 == 0); |
166 | 52.8M | } |
167 | | |
168 | 139M | while (max && *--rp == 0) |
169 | 31.3M | max--; |
170 | | |
171 | 108M | end: |
172 | 108M | r->top = max; |
173 | 108M | r->neg = 0; |
174 | 108M | bn_pollute(r); |
175 | | |
176 | 108M | return 1; |
177 | 108M | } |