Coverage Report

Created: 2025-11-16 06:40

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