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