Coverage Report

Created: 2025-06-24 07:26

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