Coverage Report

Created: 2026-07-12 07:21

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