Coverage Report

Created: 2026-05-24 07:14

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
86.5M
{
16
86.5M
    int ret, r_neg, cmp_res;
17
18
86.5M
    bn_check_top(a);
19
86.5M
    bn_check_top(b);
20
21
86.5M
    if (a->neg == b->neg) {
22
48.5M
        r_neg = a->neg;
23
48.5M
        ret = BN_uadd(r, a, b);
24
48.5M
    } else {
25
38.0M
        cmp_res = BN_ucmp(a, b);
26
38.0M
        if (cmp_res > 0) {
27
11.6M
            r_neg = a->neg;
28
11.6M
            ret = BN_usub(r, a, b);
29
26.3M
        } else if (cmp_res < 0) {
30
26.3M
            r_neg = b->neg;
31
26.3M
            ret = BN_usub(r, b, a);
32
26.3M
        } else {
33
7.32k
            r_neg = 0;
34
7.32k
            BN_zero(r);
35
7.32k
            ret = 1;
36
7.32k
        }
37
38.0M
    }
38
39
86.5M
    r->neg = r_neg;
40
86.5M
    bn_check_top(r);
41
86.5M
    return ret;
42
86.5M
}
43
44
/* signed sub of b from a. */
45
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
46
28.2M
{
47
28.2M
    int ret, r_neg, cmp_res;
48
49
28.2M
    bn_check_top(a);
50
28.2M
    bn_check_top(b);
51
52
28.2M
    if (a->neg != b->neg) {
53
0
        r_neg = a->neg;
54
0
        ret = BN_uadd(r, a, b);
55
28.2M
    } else {
56
28.2M
        cmp_res = BN_ucmp(a, b);
57
28.2M
        if (cmp_res > 0) {
58
17.6M
            r_neg = a->neg;
59
17.6M
            ret = BN_usub(r, a, b);
60
17.6M
        } else if (cmp_res < 0) {
61
10.3M
            r_neg = !b->neg;
62
10.3M
            ret = BN_usub(r, b, a);
63
10.3M
        } else {
64
287k
            r_neg = 0;
65
287k
            BN_zero(r);
66
287k
            ret = 1;
67
287k
        }
68
28.2M
    }
69
70
28.2M
    r->neg = r_neg;
71
28.2M
    bn_check_top(r);
72
28.2M
    return ret;
73
28.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
162M
{
78
162M
    int max, min, dif;
79
162M
    const BN_ULONG *ap, *bp;
80
162M
    BN_ULONG *rp, carry, t1, t2;
81
82
162M
    bn_check_top(a);
83
162M
    bn_check_top(b);
84
85
162M
    if (a->top < b->top) {
86
10.7M
        const BIGNUM *tmp;
87
88
10.7M
        tmp = a;
89
10.7M
        a = b;
90
10.7M
        b = tmp;
91
10.7M
    }
92
162M
    max = a->top;
93
162M
    min = b->top;
94
162M
    dif = max - min;
95
96
162M
    if (bn_wexpand(r, max + 1) == NULL)
97
0
        return 0;
98
99
162M
    r->top = max;
100
101
162M
    ap = a->d;
102
162M
    bp = b->d;
103
162M
    rp = r->d;
104
105
162M
    carry = bn_add_words(rp, ap, bp, min);
106
162M
    rp += min;
107
162M
    ap += min;
108
109
245M
    while (dif) {
110
83.0M
        dif--;
111
83.0M
        t1 = *(ap++);
112
83.0M
        t2 = (t1 + carry) & BN_MASK2;
113
83.0M
        *(rp++) = t2;
114
83.0M
        carry &= (t2 == 0);
115
83.0M
    }
116
162M
    *rp = carry;
117
162M
    r->top += carry;
118
119
162M
    r->neg = 0;
120
162M
    bn_check_top(r);
121
162M
    return 1;
122
162M
}
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
139M
{
127
139M
    int max, min, dif;
128
139M
    BN_ULONG t1, t2, borrow, *rp;
129
139M
    const BN_ULONG *ap, *bp;
130
131
139M
    bn_check_top(a);
132
139M
    bn_check_top(b);
133
134
139M
    max = a->top;
135
139M
    min = b->top;
136
139M
    dif = max - min;
137
138
139M
    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
139M
    if (bn_wexpand(r, max) == NULL)
144
0
        return 0;
145
146
139M
    ap = a->d;
147
139M
    bp = b->d;
148
139M
    rp = r->d;
149
150
139M
    borrow = bn_sub_words(rp, ap, bp, min);
151
139M
    ap += min;
152
139M
    rp += min;
153
154
198M
    while (dif) {
155
58.6M
        dif--;
156
58.6M
        t1 = *(ap++);
157
58.6M
        t2 = (t1 - borrow) & BN_MASK2;
158
58.6M
        *(rp++) = t2;
159
58.6M
        borrow &= (t1 == 0);
160
58.6M
    }
161
162
166M
    while (max && *--rp == 0)
163
27.2M
        max--;
164
165
139M
    r->top = max;
166
139M
    r->neg = 0;
167
139M
    bn_pollute(r);
168
169
139M
    return 1;
170
139M
}