Coverage Report

Created: 2025-06-13 06:57

/src/openssl/crypto/bn/bn_sqr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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
0
{
19
0
    int ret = bn_sqr_fixed_top(r, a, ctx);
20
21
0
    bn_correct_top(r);
22
0
    bn_check_top(r);
23
24
0
    return ret;
25
0
}
26
27
int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
28
0
{
29
0
    int max, al;
30
0
    int ret = 0;
31
0
    BIGNUM *tmp, *rr;
32
33
0
    bn_check_top(a);
34
35
0
    al = a->top;
36
0
    if (al <= 0) {
37
0
        r->top = 0;
38
0
        r->neg = 0;
39
0
        return 1;
40
0
    }
41
42
0
    BN_CTX_start(ctx);
43
0
    rr = (a != r) ? r : BN_CTX_get(ctx);
44
0
    tmp = BN_CTX_get(ctx);
45
0
    if (rr == NULL || tmp == NULL)
46
0
        goto err;
47
48
0
    max = 2 * al;               /* Non-zero (from above) */
49
0
    if (bn_wexpand(rr, max) == NULL)
50
0
        goto err;
51
52
0
    if (al == 4) {
53
#ifndef BN_SQR_COMBA
54
        BN_ULONG t[8];
55
        bn_sqr_normal(rr->d, a->d, 4, t);
56
#else
57
0
        bn_sqr_comba4(rr->d, a->d);
58
0
#endif
59
0
    } else if (al == 8) {
60
#ifndef BN_SQR_COMBA
61
        BN_ULONG t[16];
62
        bn_sqr_normal(rr->d, a->d, 8, t);
63
#else
64
0
        bn_sqr_comba8(rr->d, a->d);
65
0
#endif
66
0
    } else {
67
0
#if defined(BN_RECURSION)
68
0
        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
69
0
            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
70
0
            bn_sqr_normal(rr->d, a->d, al, t);
71
0
        } else {
72
0
            int j, k;
73
74
0
            j = BN_num_bits_word((BN_ULONG)al);
75
0
            j = 1 << (j - 1);
76
0
            k = j + j;
77
0
            if (al == j) {
78
0
                if (bn_wexpand(tmp, k * 2) == NULL)
79
0
                    goto err;
80
0
                bn_sqr_recursive(rr->d, a->d, al, tmp->d);
81
0
            } else {
82
0
                if (bn_wexpand(tmp, max) == NULL)
83
0
                    goto err;
84
0
                bn_sqr_normal(rr->d, a->d, al, tmp->d);
85
0
            }
86
0
        }
87
#else
88
        if (bn_wexpand(tmp, max) == NULL)
89
            goto err;
90
        bn_sqr_normal(rr->d, a->d, al, tmp->d);
91
#endif
92
0
    }
93
94
0
    rr->neg = 0;
95
0
    rr->top = max;
96
0
    rr->flags |= BN_FLG_FIXED_TOP;
97
0
    if (r != rr && BN_copy(r, rr) == NULL)
98
0
        goto err;
99
100
0
    ret = 1;
101
0
 err:
102
0
    bn_check_top(rr);
103
0
    bn_check_top(tmp);
104
0
    BN_CTX_end(ctx);
105
0
    return ret;
106
0
}
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
0
{
111
0
    int i, j, max;
112
0
    const BN_ULONG *ap;
113
0
    BN_ULONG *rp;
114
115
0
    max = n * 2;
116
0
    ap = a;
117
0
    rp = r;
118
0
    rp[0] = rp[max - 1] = 0;
119
0
    rp++;
120
0
    j = n;
121
122
0
    if (--j > 0) {
123
0
        ap++;
124
0
        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
125
0
        rp += 2;
126
0
    }
127
128
0
    for (i = n - 2; i > 0; i--) {
129
0
        j--;
130
0
        ap++;
131
0
        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
132
0
        rp += 2;
133
0
    }
134
135
0
    bn_add_words(r, r, r, max);
136
137
    /* There will not be a carry */
138
139
0
    bn_sqr_words(tmp, a, n);
140
141
0
    bn_add_words(r, r, tmp, max);
142
0
}
143
144
#ifdef BN_RECURSION
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
0
{
158
0
    int n = n2 / 2;
159
0
    int zero, c1;
160
0
    BN_ULONG ln, lo, *p;
161
162
0
    if (n2 == 4) {
163
# ifndef BN_SQR_COMBA
164
        bn_sqr_normal(r, a, 4, t);
165
# else
166
0
        bn_sqr_comba4(r, a);
167
0
# endif
168
0
        return;
169
0
    } else if (n2 == 8) {
170
# ifndef BN_SQR_COMBA
171
        bn_sqr_normal(r, a, 8, t);
172
# else
173
0
        bn_sqr_comba8(r, a);
174
0
# endif
175
0
        return;
176
0
    }
177
0
    if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
178
0
        bn_sqr_normal(r, a, n2, t);
179
0
        return;
180
0
    }
181
    /* r=(a[0]-a[1])*(a[1]-a[0]) */
182
0
    c1 = bn_cmp_words(a, &(a[n]), n);
183
0
    zero = 0;
184
0
    if (c1 > 0)
185
0
        bn_sub_words(t, a, &(a[n]), n);
186
0
    else if (c1 < 0)
187
0
        bn_sub_words(t, &(a[n]), a, n);
188
0
    else
189
0
        zero = 1;
190
191
    /* The result will always be negative unless it is zero */
192
0
    p = &(t[n2 * 2]);
193
194
0
    if (!zero)
195
0
        bn_sqr_recursive(&(t[n2]), t, n, p);
196
0
    else
197
0
        memset(&t[n2], 0, sizeof(*t) * n2);
198
0
    bn_sqr_recursive(r, a, n, p);
199
0
    bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
200
201
    /*-
202
     * t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
203
     * r[10] holds (a[0]*b[0])
204
     * r[32] holds (b[1]*b[1])
205
     */
206
207
0
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
208
209
    /* t[32] is negative */
210
0
    c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
211
212
    /*-
213
     * t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
214
     * r[10] holds (a[0]*a[0])
215
     * r[32] holds (a[1]*a[1])
216
     * c1 holds the carry bits
217
     */
218
0
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
219
0
    if (c1) {
220
0
        p = &(r[n + n2]);
221
0
        lo = *p;
222
0
        ln = (lo + c1) & BN_MASK2;
223
0
        *p = ln;
224
225
        /*
226
         * The overflow will stop before we over write words we should not
227
         * overwrite
228
         */
229
0
        if (ln < (BN_ULONG)c1) {
230
0
            do {
231
0
                p++;
232
0
                lo = *p;
233
0
                ln = (lo + 1) & BN_MASK2;
234
0
                *p = ln;
235
0
            } while (ln == 0);
236
0
        }
237
0
    }
238
0
}
239
#endif