Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/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 OpenSSL license (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
1.55k
{
19
1.55k
    int ret = bn_sqr_fixed_top(r, a, ctx);
20
21
1.55k
    bn_correct_top(r);
22
1.55k
    bn_check_top(r);
23
24
1.55k
    return ret;
25
1.55k
}
26
27
int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
28
22.7k
{
29
22.7k
    int max, al;
30
22.7k
    int ret = 0;
31
22.7k
    BIGNUM *tmp, *rr;
32
33
22.7k
    bn_check_top(a);
34
35
22.7k
    al = a->top;
36
22.7k
    if (al <= 0) {
37
509
        r->top = 0;
38
509
        r->neg = 0;
39
509
        return 1;
40
509
    }
41
42
22.2k
    BN_CTX_start(ctx);
43
22.2k
    rr = (a != r) ? r : BN_CTX_get(ctx);
44
22.2k
    tmp = BN_CTX_get(ctx);
45
22.2k
    if (rr == NULL || tmp == NULL)
46
0
        goto err;
47
48
22.2k
    max = 2 * al;               /* Non-zero (from above) */
49
22.2k
    if (bn_wexpand(rr, max) == NULL)
50
0
        goto err;
51
52
22.2k
    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
920
        bn_sqr_comba4(rr->d, a->d);
58
920
#endif
59
21.2k
    } 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
440
        bn_sqr_comba8(rr->d, a->d);
65
440
#endif
66
20.8k
    } else {
67
20.8k
#if defined(BN_RECURSION)
68
20.8k
        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
69
2.79k
            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
70
2.79k
            bn_sqr_normal(rr->d, a->d, al, t);
71
18.0k
        } else {
72
18.0k
            int j, k;
73
74
18.0k
            j = BN_num_bits_word((BN_ULONG)al);
75
18.0k
            j = 1 << (j - 1);
76
18.0k
            k = j + j;
77
18.0k
            if (al == j) {
78
15.4k
                if (bn_wexpand(tmp, k * 2) == NULL)
79
0
                    goto err;
80
15.4k
                bn_sqr_recursive(rr->d, a->d, al, tmp->d);
81
15.4k
            } else {
82
2.58k
                if (bn_wexpand(tmp, max) == NULL)
83
0
                    goto err;
84
2.58k
                bn_sqr_normal(rr->d, a->d, al, tmp->d);
85
2.58k
            }
86
18.0k
        }
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
20.8k
    }
93
94
22.2k
    rr->neg = 0;
95
22.2k
    rr->top = max;
96
22.2k
    rr->flags |= BN_FLG_FIXED_TOP;
97
22.2k
    if (r != rr && BN_copy(r, rr) == NULL)
98
0
        goto err;
99
100
22.2k
    ret = 1;
101
22.2k
 err:
102
22.2k
    bn_check_top(rr);
103
22.2k
    bn_check_top(tmp);
104
22.2k
    BN_CTX_end(ctx);
105
22.2k
    return ret;
106
22.2k
}
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
5.37k
{
111
5.37k
    int i, j, max;
112
5.37k
    const BN_ULONG *ap;
113
5.37k
    BN_ULONG *rp;
114
115
5.37k
    max = n * 2;
116
5.37k
    ap = a;
117
5.37k
    rp = r;
118
5.37k
    rp[0] = rp[max - 1] = 0;
119
5.37k
    rp++;
120
5.37k
    j = n;
121
122
5.37k
    if (--j > 0) {
123
4.81k
        ap++;
124
4.81k
        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
125
4.81k
        rp += 2;
126
4.81k
    }
127
128
89.9k
    for (i = n - 2; i > 0; i--) {
129
84.5k
        j--;
130
84.5k
        ap++;
131
84.5k
        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
132
84.5k
        rp += 2;
133
84.5k
    }
134
135
5.37k
    bn_add_words(r, r, r, max);
136
137
    /* There will not be a carry */
138
139
5.37k
    bn_sqr_words(tmp, a, n);
140
141
5.37k
    bn_add_words(r, r, tmp, max);
142
5.37k
}
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
85.0k
{
158
85.0k
    int n = n2 / 2;
159
85.0k
    int zero, c1;
160
85.0k
    BN_ULONG ln, lo, *p;
161
162
85.0k
    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
85.0k
    } else if (n2 == 8) {
170
# ifndef BN_SQR_COMBA
171
        bn_sqr_normal(r, a, 8, t);
172
# else
173
61.8k
        bn_sqr_comba8(r, a);
174
61.8k
# endif
175
61.8k
        return;
176
61.8k
    }
177
23.2k
    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
23.2k
    c1 = bn_cmp_words(a, &(a[n]), n);
183
23.2k
    zero = 0;
184
23.2k
    if (c1 > 0)
185
12.1k
        bn_sub_words(t, a, &(a[n]), n);
186
11.0k
    else if (c1 < 0)
187
10.9k
        bn_sub_words(t, &(a[n]), a, n);
188
71
    else
189
71
        zero = 1;
190
191
    /* The result will always be negative unless it is zero */
192
23.2k
    p = &(t[n2 * 2]);
193
194
23.2k
    if (!zero)
195
23.1k
        bn_sqr_recursive(&(t[n2]), t, n, p);
196
71
    else
197
71
        memset(&t[n2], 0, sizeof(*t) * n2);
198
23.2k
    bn_sqr_recursive(r, a, n, p);
199
23.2k
    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
23.2k
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
208
209
    /* t[32] is negative */
210
23.2k
    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
23.2k
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
219
23.2k
    if (c1) {
220
10.4k
        p = &(r[n + n2]);
221
10.4k
        lo = *p;
222
10.4k
        ln = (lo + c1) & BN_MASK2;
223
10.4k
        *p = ln;
224
225
        /*
226
         * The overflow will stop before we over write words we should not
227
         * overwrite
228
         */
229
10.4k
        if (ln < (BN_ULONG)c1) {
230
222
            do {
231
222
                p++;
232
222
                lo = *p;
233
222
                ln = (lo + 1) & BN_MASK2;
234
222
                *p = ln;
235
222
            } while (ln == 0);
236
109
        }
237
10.4k
    }
238
23.2k
}
239
#endif