Coverage Report

Created: 2023-06-08 06:40

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