Coverage Report

Created: 2023-09-25 06:41

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