Coverage Report

Created: 2023-09-25 06:41

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