Coverage Report

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