Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
35.5M
{
78
35.5M
    int max, min, dif;
79
35.5M
    const BN_ULONG *ap, *bp;
80
35.5M
    BN_ULONG *rp, carry, t1, t2;
81
82
35.5M
    bn_check_top(a);
83
35.5M
    bn_check_top(b);
84
85
35.5M
    if (a->top < b->top) {
86
2.34M
        const BIGNUM *tmp;
87
88
2.34M
        tmp = a;
89
2.34M
        a = b;
90
2.34M
        b = tmp;
91
2.34M
    }
92
35.5M
    max = a->top;
93
35.5M
    min = b->top;
94
35.5M
    dif = max - min;
95
96
35.5M
    if (bn_wexpand(r, max + 1) == NULL)
97
0
        return 0;
98
99
35.5M
    r->top = max;
100
101
35.5M
    ap = a->d;
102
35.5M
    bp = b->d;
103
35.5M
    rp = r->d;
104
105
35.5M
    carry = bn_add_words(rp, ap, bp, min);
106
35.5M
    rp += min;
107
35.5M
    ap += min;
108
109
47.7M
    while (dif) {
110
12.2M
        dif--;
111
12.2M
        t1 = *(ap++);
112
12.2M
        t2 = (t1 + carry) & BN_MASK2;
113
12.2M
        *(rp++) = t2;
114
12.2M
        carry &= (t2 == 0);
115
12.2M
    }
116
35.5M
    *rp = carry;
117
35.5M
    r->top += carry;
118
119
35.5M
    r->neg = 0;
120
35.5M
    bn_check_top(r);
121
35.5M
    return 1;
122
35.5M
}
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
28.7M
{
127
28.7M
    int max, min, dif;
128
28.7M
    BN_ULONG t1, t2, borrow, *rp;
129
28.7M
    const BN_ULONG *ap, *bp;
130
131
28.7M
    bn_check_top(a);
132
28.7M
    bn_check_top(b);
133
134
28.7M
    max = a->top;
135
28.7M
    min = b->top;
136
28.7M
    dif = max - min;
137
138
28.7M
    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
28.7M
    if (bn_wexpand(r, max) == NULL)
144
0
        return 0;
145
146
28.7M
    ap = a->d;
147
28.7M
    bp = b->d;
148
28.7M
    rp = r->d;
149
150
28.7M
    borrow = bn_sub_words(rp, ap, bp, min);
151
28.7M
    ap += min;
152
28.7M
    rp += min;
153
154
36.2M
    while (dif) {
155
7.52M
        dif--;
156
7.52M
        t1 = *(ap++);
157
7.52M
        t2 = (t1 - borrow) & BN_MASK2;
158
7.52M
        *(rp++) = t2;
159
7.52M
        borrow &= (t1 == 0);
160
7.52M
    }
161
162
32.2M
    while (max && *--rp == 0)
163
3.55M
        max--;
164
165
28.7M
    r->top = max;
166
28.7M
    r->neg = 0;
167
28.7M
    bn_pollute(r);
168
169
28.7M
    return 1;
170
28.7M
}