Coverage Report

Created: 2026-07-23 06:28

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