Coverage Report

Created: 2023-09-25 06:41

/src/openssl111/crypto/bn/bn_add.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
2.87M
{
16
2.87M
    int ret, r_neg, cmp_res;
17
18
2.87M
    bn_check_top(a);
19
2.87M
    bn_check_top(b);
20
21
2.87M
    if (a->neg == b->neg) {
22
2.50M
        r_neg = a->neg;
23
2.50M
        ret = BN_uadd(r, a, b);
24
2.50M
    } else {
25
372k
        cmp_res = BN_ucmp(a, b);
26
372k
        if (cmp_res > 0) {
27
0
            r_neg = a->neg;
28
0
            ret = BN_usub(r, a, b);
29
372k
        } else if (cmp_res < 0) {
30
372k
            r_neg = b->neg;
31
372k
            ret = BN_usub(r, b, a);
32
372k
        } else {
33
0
            r_neg = 0;
34
0
            BN_zero(r);
35
0
            ret = 1;
36
0
        }
37
372k
    }
38
39
2.87M
    r->neg = r_neg;
40
2.87M
    bn_check_top(r);
41
2.87M
    return ret;
42
2.87M
}
43
44
/* signed sub of b from a. */
45
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
46
1.10M
{
47
1.10M
    int ret, r_neg, cmp_res;
48
49
1.10M
    bn_check_top(a);
50
1.10M
    bn_check_top(b);
51
52
1.10M
    if (a->neg != b->neg) {
53
0
        r_neg = a->neg;
54
0
        ret = BN_uadd(r, a, b);
55
1.10M
    } else {
56
1.10M
        cmp_res = BN_ucmp(a, b);
57
1.10M
        if (cmp_res > 0) {
58
730k
            r_neg = a->neg;
59
730k
            ret = BN_usub(r, a, b);
60
730k
        } else if (cmp_res < 0) {
61
372k
            r_neg = !b->neg;
62
372k
            ret = BN_usub(r, b, a);
63
372k
        } else {
64
2.08k
            r_neg = 0;
65
2.08k
            BN_zero(r);
66
2.08k
            ret = 1;
67
2.08k
        }
68
1.10M
    }
69
70
1.10M
    r->neg = r_neg;
71
1.10M
    bn_check_top(r);
72
1.10M
    return ret;
73
1.10M
}
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.63M
{
78
5.63M
    int max, min, dif;
79
5.63M
    const BN_ULONG *ap, *bp;
80
5.63M
    BN_ULONG *rp, carry, t1, t2;
81
82
5.63M
    bn_check_top(a);
83
5.63M
    bn_check_top(b);
84
85
5.63M
    if (a->top < b->top) {
86
43.8k
        const BIGNUM *tmp;
87
88
43.8k
        tmp = a;
89
43.8k
        a = b;
90
43.8k
        b = tmp;
91
43.8k
    }
92
5.63M
    max = a->top;
93
5.63M
    min = b->top;
94
5.63M
    dif = max - min;
95
96
5.63M
    if (bn_wexpand(r, max + 1) == NULL)
97
0
        return 0;
98
99
5.63M
    r->top = max;
100
101
5.63M
    ap = a->d;
102
5.63M
    bp = b->d;
103
5.63M
    rp = r->d;
104
105
5.63M
    carry = bn_add_words(rp, ap, bp, min);
106
5.63M
    rp += min;
107
5.63M
    ap += min;
108
109
6.23M
    while (dif) {
110
600k
        dif--;
111
600k
        t1 = *(ap++);
112
600k
        t2 = (t1 + carry) & BN_MASK2;
113
600k
        *(rp++) = t2;
114
600k
        carry &= (t2 == 0);
115
600k
    }
116
5.63M
    *rp = carry;
117
5.63M
    r->top += carry;
118
119
5.63M
    r->neg = 0;
120
5.63M
    bn_check_top(r);
121
5.63M
    return 1;
122
5.63M
}
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
3.29M
{
127
3.29M
    int max, min, dif;
128
3.29M
    BN_ULONG t1, t2, borrow, *rp;
129
3.29M
    const BN_ULONG *ap, *bp;
130
131
3.29M
    bn_check_top(a);
132
3.29M
    bn_check_top(b);
133
134
3.29M
    max = a->top;
135
3.29M
    min = b->top;
136
3.29M
    dif = max - min;
137
138
3.29M
    if (dif < 0) {              /* hmm... should not be happening */
139
0
        BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);
140
0
        return 0;
141
0
    }
142
143
3.29M
    if (bn_wexpand(r, max) == NULL)
144
0
        return 0;
145
146
3.29M
    ap = a->d;
147
3.29M
    bp = b->d;
148
3.29M
    rp = r->d;
149
150
3.29M
    borrow = bn_sub_words(rp, ap, bp, min);
151
3.29M
    ap += min;
152
3.29M
    rp += min;
153
154
3.67M
    while (dif) {
155
380k
        dif--;
156
380k
        t1 = *(ap++);
157
380k
        t2 = (t1 - borrow) & BN_MASK2;
158
380k
        *(rp++) = t2;
159
380k
        borrow &= (t1 == 0);
160
380k
    }
161
162
3.67M
    while (max && *--rp == 0)
163
387k
        max--;
164
165
3.29M
    r->top = max;
166
3.29M
    r->neg = 0;
167
3.29M
    bn_pollute(r);
168
169
3.29M
    return 1;
170
3.29M
}
171