Coverage Report

Created: 2024-05-21 06:52

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