Coverage Report

Created: 2024-11-21 07:03

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