Coverage Report

Created: 2026-04-01 06:39

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