Coverage Report

Created: 2025-12-04 06:33

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