Coverage Report

Created: 2023-06-08 06:40

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