Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/bn/bn_sqr.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
/* r must not be a */
14
/*
15
 * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96
16
 */
17
int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
18
84.3M
{
19
84.3M
    int ret = bn_sqr_fixed_top(r, a, ctx);
20
21
84.3M
    bn_correct_top(r);
22
84.3M
    bn_check_top(r);
23
24
84.3M
    return ret;
25
84.3M
}
26
27
int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
28
87.6M
{
29
87.6M
    int max, al;
30
87.6M
    int ret = 0;
31
87.6M
    BIGNUM *tmp, *rr;
32
33
87.6M
    bn_check_top(a);
34
35
87.6M
    al = a->top;
36
87.6M
    if (al <= 0) {
37
291k
        r->top = 0;
38
291k
        r->neg = 0;
39
291k
        return 1;
40
291k
    }
41
42
87.4M
    BN_CTX_start(ctx);
43
87.4M
    rr = (a != r) ? r : BN_CTX_get(ctx);
44
87.4M
    tmp = BN_CTX_get(ctx);
45
87.4M
    if (rr == NULL || tmp == NULL)
46
0
        goto err;
47
48
87.4M
    max = 2 * al; /* Non-zero (from above) */
49
87.4M
    if (bn_wexpand(rr, max) == NULL)
50
0
        goto err;
51
52
87.4M
    if (al == 4) {
53
#ifdef OPENSSL_SMALL_FOOTPRINT
54
        BN_ULONG t[8];
55
        bn_sqr_normal(rr->d, a->d, 4, t);
56
#else
57
82.5M
        bn_sqr_comba4(rr->d, a->d);
58
82.5M
#endif
59
82.5M
    } else if (al == 8) {
60
#ifdef OPENSSL_SMALL_FOOTPRINT
61
        BN_ULONG t[16];
62
        bn_sqr_normal(rr->d, a->d, 8, t);
63
#else
64
102k
        bn_sqr_comba8(rr->d, a->d);
65
102k
#endif
66
4.70M
    } else {
67
#ifdef OPENSSL_SMALL_FOOTPRINT
68
        if (bn_wexpand(tmp, max) == NULL)
69
            goto err;
70
        bn_sqr_normal(rr->d, a->d, al, tmp->d);
71
#else
72
4.70M
        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
73
3.82M
            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
74
3.82M
            bn_sqr_normal(rr->d, a->d, al, t);
75
3.82M
        } else {
76
878k
            int j, k;
77
78
878k
            j = BN_num_bits_word((BN_ULONG)al);
79
878k
            j = 1 << (j - 1);
80
878k
            k = j + j;
81
878k
            if (al == j) {
82
352k
                if (bn_wexpand(tmp, k * 2) == NULL)
83
0
                    goto err;
84
352k
                bn_sqr_recursive(rr->d, a->d, al, tmp->d);
85
526k
            } else {
86
526k
                if (bn_wexpand(tmp, max) == NULL)
87
0
                    goto err;
88
526k
                bn_sqr_normal(rr->d, a->d, al, tmp->d);
89
526k
            }
90
878k
        }
91
4.70M
#endif
92
4.70M
    }
93
94
87.4M
    rr->neg = 0;
95
87.4M
    rr->top = max;
96
87.4M
    rr->flags |= BN_FLG_FIXED_TOP;
97
87.4M
    if (r != rr && BN_copy(r, rr) == NULL)
98
0
        goto err;
99
100
87.4M
    ret = 1;
101
87.4M
err:
102
87.4M
    bn_check_top(rr);
103
87.4M
    bn_check_top(tmp);
104
87.4M
    BN_CTX_end(ctx);
105
87.4M
    return ret;
106
87.4M
}
107
108
/* tmp must have 2*n words */
109
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
110
4.35M
{
111
4.35M
    int i, j, max;
112
4.35M
    const BN_ULONG *ap;
113
4.35M
    BN_ULONG *rp;
114
115
4.35M
    max = n * 2;
116
4.35M
    ap = a;
117
4.35M
    rp = r;
118
4.35M
    rp[0] = rp[max - 1] = 0;
119
4.35M
    rp++;
120
4.35M
    j = n;
121
122
4.35M
    if (--j > 0) {
123
1.12M
        ap++;
124
1.12M
        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
125
1.12M
        rp += 2;
126
1.12M
    }
127
128
77.2M
    for (i = n - 2; i > 0; i--) {
129
72.8M
        j--;
130
72.8M
        ap++;
131
72.8M
        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
132
72.8M
        rp += 2;
133
72.8M
    }
134
135
4.35M
    bn_add_words(r, r, r, max);
136
137
    /* There will not be a carry */
138
139
4.35M
    bn_sqr_words(tmp, a, n);
140
141
4.35M
    bn_add_words(r, r, tmp, max);
142
4.35M
}
143
144
#ifndef OPENSSL_SMALL_FOOTPRINT
145
/*-
146
 * r is 2*n words in size,
147
 * a and b are both n words in size.    (There's not actually a 'b' here ...)
148
 * n must be a power of 2.
149
 * We multiply and return the result.
150
 * t must be 2*n words in size
151
 * We calculate
152
 * a[0]*b[0]
153
 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
154
 * a[1]*b[1]
155
 */
156
void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
157
20.1M
{
158
20.1M
    int n = n2 / 2;
159
20.1M
    int zero, c1;
160
20.1M
    BN_ULONG ln, lo, *p;
161
162
20.1M
    if (n2 == 4) {
163
0
        bn_sqr_comba4(r, a);
164
0
        return;
165
20.1M
    } else if (n2 == 8) {
166
13.5M
        bn_sqr_comba8(r, a);
167
13.5M
        return;
168
13.5M
    }
169
6.60M
    if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
170
0
        bn_sqr_normal(r, a, n2, t);
171
0
        return;
172
0
    }
173
    /* r=(a[0]-a[1])*(a[1]-a[0]) */
174
6.60M
    c1 = bn_cmp_words(a, &(a[n]), n);
175
6.60M
    zero = 0;
176
6.60M
    if (c1 > 0)
177
3.80M
        bn_sub_words(t, a, &(a[n]), n);
178
2.80M
    else if (c1 < 0)
179
2.74M
        bn_sub_words(t, &(a[n]), a, n);
180
57.4k
    else
181
57.4k
        zero = 1;
182
183
    /* The result will always be negative unless it is zero */
184
6.60M
    p = &(t[n2 * 2]);
185
186
6.60M
    if (!zero)
187
6.54M
        bn_sqr_recursive(&(t[n2]), t, n, p);
188
57.4k
    else
189
57.4k
        memset(&t[n2], 0, sizeof(*t) * n2);
190
6.60M
    bn_sqr_recursive(r, a, n, p);
191
6.60M
    bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
192
193
    /*-
194
     * t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
195
     * r[10] holds (a[0]*b[0])
196
     * r[32] holds (b[1]*b[1])
197
     */
198
199
6.60M
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
200
201
    /* t[32] is negative */
202
6.60M
    c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
203
204
    /*-
205
     * t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
206
     * r[10] holds (a[0]*a[0])
207
     * r[32] holds (a[1]*a[1])
208
     * c1 holds the carry bits
209
     */
210
6.60M
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
211
6.60M
    if (c1) {
212
2.50M
        p = &(r[n + n2]);
213
2.50M
        lo = *p;
214
2.50M
        ln = (lo + c1) & BN_MASK2;
215
2.50M
        *p = ln;
216
217
        /*
218
         * The overflow will stop before we over write words we should not
219
         * overwrite
220
         */
221
2.50M
        if (ln < (BN_ULONG)c1) {
222
62.8k
            do {
223
62.8k
                p++;
224
62.8k
                lo = *p;
225
62.8k
                ln = (lo + 1) & BN_MASK2;
226
62.8k
                *p = ln;
227
62.8k
            } while (ln == 0);
228
19.5k
        }
229
2.50M
    }
230
6.60M
}
231
#endif