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