Coverage Report

Created: 2025-04-22 06:18

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