1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
11e1051a39Sopenharmony_ci#include "internal/constant_time.h"
12e1051a39Sopenharmony_ci#include "bn_local.h"
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci#include <stdlib.h>
15e1051a39Sopenharmony_ci#ifdef _WIN32
16e1051a39Sopenharmony_ci# include <malloc.h>
17e1051a39Sopenharmony_ci# ifndef alloca
18e1051a39Sopenharmony_ci#  define alloca _alloca
19e1051a39Sopenharmony_ci# endif
20e1051a39Sopenharmony_ci#elif defined(__GNUC__)
21e1051a39Sopenharmony_ci# ifndef alloca
22e1051a39Sopenharmony_ci#  define alloca(s) __builtin_alloca((s))
23e1051a39Sopenharmony_ci# endif
24e1051a39Sopenharmony_ci#elif defined(__sun)
25e1051a39Sopenharmony_ci# include <alloca.h>
26e1051a39Sopenharmony_ci#endif
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci#include "rsaz_exp.h"
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci#undef SPARC_T4_MONT
31e1051a39Sopenharmony_ci#if defined(OPENSSL_BN_ASM_MONT) && (defined(__sparc__) || defined(__sparc))
32e1051a39Sopenharmony_ci# include "crypto/sparc_arch.h"
33e1051a39Sopenharmony_ci# define SPARC_T4_MONT
34e1051a39Sopenharmony_ci#endif
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci/* maximum precomputation table size for *variable* sliding windows */
37e1051a39Sopenharmony_ci#define TABLE_SIZE      32
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci/*
40e1051a39Sopenharmony_ci * Beyond this limit the constant time code is disabled due to
41e1051a39Sopenharmony_ci * the possible overflow in the computation of powerbufLen in
42e1051a39Sopenharmony_ci * BN_mod_exp_mont_consttime.
43e1051a39Sopenharmony_ci * When this limit is exceeded, the computation will be done using
44e1051a39Sopenharmony_ci * non-constant time code, but it will take very long.
45e1051a39Sopenharmony_ci */
46e1051a39Sopenharmony_ci#define BN_CONSTTIME_SIZE_LIMIT (INT_MAX / BN_BYTES / 256)
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci/* this one works - simple but works */
49e1051a39Sopenharmony_ciint BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
50e1051a39Sopenharmony_ci{
51e1051a39Sopenharmony_ci    int i, bits, ret = 0;
52e1051a39Sopenharmony_ci    BIGNUM *v, *rr;
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
55e1051a39Sopenharmony_ci            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) {
56e1051a39Sopenharmony_ci        /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
57e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
58e1051a39Sopenharmony_ci        return 0;
59e1051a39Sopenharmony_ci    }
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
62e1051a39Sopenharmony_ci    rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : r;
63e1051a39Sopenharmony_ci    v = BN_CTX_get(ctx);
64e1051a39Sopenharmony_ci    if (rr == NULL || v == NULL)
65e1051a39Sopenharmony_ci        goto err;
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci    if (BN_copy(v, a) == NULL)
68e1051a39Sopenharmony_ci        goto err;
69e1051a39Sopenharmony_ci    bits = BN_num_bits(p);
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci    if (BN_is_odd(p)) {
72e1051a39Sopenharmony_ci        if (BN_copy(rr, a) == NULL)
73e1051a39Sopenharmony_ci            goto err;
74e1051a39Sopenharmony_ci    } else {
75e1051a39Sopenharmony_ci        if (!BN_one(rr))
76e1051a39Sopenharmony_ci            goto err;
77e1051a39Sopenharmony_ci    }
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    for (i = 1; i < bits; i++) {
80e1051a39Sopenharmony_ci        if (!BN_sqr(v, v, ctx))
81e1051a39Sopenharmony_ci            goto err;
82e1051a39Sopenharmony_ci        if (BN_is_bit_set(p, i)) {
83e1051a39Sopenharmony_ci            if (!BN_mul(rr, rr, v, ctx))
84e1051a39Sopenharmony_ci                goto err;
85e1051a39Sopenharmony_ci        }
86e1051a39Sopenharmony_ci    }
87e1051a39Sopenharmony_ci    if (r != rr && BN_copy(r, rr) == NULL)
88e1051a39Sopenharmony_ci        goto err;
89e1051a39Sopenharmony_ci
90e1051a39Sopenharmony_ci    ret = 1;
91e1051a39Sopenharmony_ci err:
92e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
93e1051a39Sopenharmony_ci    bn_check_top(r);
94e1051a39Sopenharmony_ci    return ret;
95e1051a39Sopenharmony_ci}
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_ciint BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
98e1051a39Sopenharmony_ci               BN_CTX *ctx)
99e1051a39Sopenharmony_ci{
100e1051a39Sopenharmony_ci    int ret;
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_ci    bn_check_top(a);
103e1051a39Sopenharmony_ci    bn_check_top(p);
104e1051a39Sopenharmony_ci    bn_check_top(m);
105e1051a39Sopenharmony_ci
106e1051a39Sopenharmony_ci    /*-
107e1051a39Sopenharmony_ci     * For even modulus  m = 2^k*m_odd, it might make sense to compute
108e1051a39Sopenharmony_ci     * a^p mod m_odd  and  a^p mod 2^k  separately (with Montgomery
109e1051a39Sopenharmony_ci     * exponentiation for the odd part), using appropriate exponent
110e1051a39Sopenharmony_ci     * reductions, and combine the results using the CRT.
111e1051a39Sopenharmony_ci     *
112e1051a39Sopenharmony_ci     * For now, we use Montgomery only if the modulus is odd; otherwise,
113e1051a39Sopenharmony_ci     * exponentiation using the reciprocal-based quick remaindering
114e1051a39Sopenharmony_ci     * algorithm is used.
115e1051a39Sopenharmony_ci     *
116e1051a39Sopenharmony_ci     * (Timing obtained with expspeed.c [computations  a^p mod m
117e1051a39Sopenharmony_ci     * where  a, p, m  are of the same length: 256, 512, 1024, 2048,
118e1051a39Sopenharmony_ci     * 4096, 8192 bits], compared to the running time of the
119e1051a39Sopenharmony_ci     * standard algorithm:
120e1051a39Sopenharmony_ci     *
121e1051a39Sopenharmony_ci     *   BN_mod_exp_mont   33 .. 40 %  [AMD K6-2, Linux, debug configuration]
122e1051a39Sopenharmony_ci     *                     55 .. 77 %  [UltraSparc processor, but
123e1051a39Sopenharmony_ci     *                                  debug-solaris-sparcv8-gcc conf.]
124e1051a39Sopenharmony_ci     *
125e1051a39Sopenharmony_ci     *   BN_mod_exp_recp   50 .. 70 %  [AMD K6-2, Linux, debug configuration]
126e1051a39Sopenharmony_ci     *                     62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc]
127e1051a39Sopenharmony_ci     *
128e1051a39Sopenharmony_ci     * On the Sparc, BN_mod_exp_recp was faster than BN_mod_exp_mont
129e1051a39Sopenharmony_ci     * at 2048 and more bits, but at 512 and 1024 bits, it was
130e1051a39Sopenharmony_ci     * slower even than the standard algorithm!
131e1051a39Sopenharmony_ci     *
132e1051a39Sopenharmony_ci     * "Real" timings [linux-elf, solaris-sparcv9-gcc configurations]
133e1051a39Sopenharmony_ci     * should be obtained when the new Montgomery reduction code
134e1051a39Sopenharmony_ci     * has been integrated into OpenSSL.)
135e1051a39Sopenharmony_ci     */
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ci#define MONT_MUL_MOD
138e1051a39Sopenharmony_ci#define MONT_EXP_WORD
139e1051a39Sopenharmony_ci#define RECP_MUL_MOD
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_ci#ifdef MONT_MUL_MOD
142e1051a39Sopenharmony_ci    if (BN_is_odd(m)) {
143e1051a39Sopenharmony_ci# ifdef MONT_EXP_WORD
144e1051a39Sopenharmony_ci        if (a->top == 1 && !a->neg
145e1051a39Sopenharmony_ci            && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)
146e1051a39Sopenharmony_ci            && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)
147e1051a39Sopenharmony_ci            && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {
148e1051a39Sopenharmony_ci            BN_ULONG A = a->d[0];
149e1051a39Sopenharmony_ci            ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);
150e1051a39Sopenharmony_ci        } else
151e1051a39Sopenharmony_ci# endif
152e1051a39Sopenharmony_ci            ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);
153e1051a39Sopenharmony_ci    } else
154e1051a39Sopenharmony_ci#endif
155e1051a39Sopenharmony_ci#ifdef RECP_MUL_MOD
156e1051a39Sopenharmony_ci    {
157e1051a39Sopenharmony_ci        ret = BN_mod_exp_recp(r, a, p, m, ctx);
158e1051a39Sopenharmony_ci    }
159e1051a39Sopenharmony_ci#else
160e1051a39Sopenharmony_ci    {
161e1051a39Sopenharmony_ci        ret = BN_mod_exp_simple(r, a, p, m, ctx);
162e1051a39Sopenharmony_ci    }
163e1051a39Sopenharmony_ci#endif
164e1051a39Sopenharmony_ci
165e1051a39Sopenharmony_ci    bn_check_top(r);
166e1051a39Sopenharmony_ci    return ret;
167e1051a39Sopenharmony_ci}
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_ciint BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
170e1051a39Sopenharmony_ci                    const BIGNUM *m, BN_CTX *ctx)
171e1051a39Sopenharmony_ci{
172e1051a39Sopenharmony_ci    int i, j, bits, ret = 0, wstart, wend, window, wvalue;
173e1051a39Sopenharmony_ci    int start = 1;
174e1051a39Sopenharmony_ci    BIGNUM *aa;
175e1051a39Sopenharmony_ci    /* Table of variables obtained from 'ctx' */
176e1051a39Sopenharmony_ci    BIGNUM *val[TABLE_SIZE];
177e1051a39Sopenharmony_ci    BN_RECP_CTX recp;
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ci    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
180e1051a39Sopenharmony_ci            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0
181e1051a39Sopenharmony_ci            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {
182e1051a39Sopenharmony_ci        /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
183e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
184e1051a39Sopenharmony_ci        return 0;
185e1051a39Sopenharmony_ci    }
186e1051a39Sopenharmony_ci
187e1051a39Sopenharmony_ci    bits = BN_num_bits(p);
188e1051a39Sopenharmony_ci    if (bits == 0) {
189e1051a39Sopenharmony_ci        /* x**0 mod 1, or x**0 mod -1 is still zero. */
190e1051a39Sopenharmony_ci        if (BN_abs_is_word(m, 1)) {
191e1051a39Sopenharmony_ci            ret = 1;
192e1051a39Sopenharmony_ci            BN_zero(r);
193e1051a39Sopenharmony_ci        } else {
194e1051a39Sopenharmony_ci            ret = BN_one(r);
195e1051a39Sopenharmony_ci        }
196e1051a39Sopenharmony_ci        return ret;
197e1051a39Sopenharmony_ci    }
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_ci    BN_RECP_CTX_init(&recp);
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
202e1051a39Sopenharmony_ci    aa = BN_CTX_get(ctx);
203e1051a39Sopenharmony_ci    val[0] = BN_CTX_get(ctx);
204e1051a39Sopenharmony_ci    if (val[0] == NULL)
205e1051a39Sopenharmony_ci        goto err;
206e1051a39Sopenharmony_ci
207e1051a39Sopenharmony_ci    if (m->neg) {
208e1051a39Sopenharmony_ci        /* ignore sign of 'm' */
209e1051a39Sopenharmony_ci        if (!BN_copy(aa, m))
210e1051a39Sopenharmony_ci            goto err;
211e1051a39Sopenharmony_ci        aa->neg = 0;
212e1051a39Sopenharmony_ci        if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)
213e1051a39Sopenharmony_ci            goto err;
214e1051a39Sopenharmony_ci    } else {
215e1051a39Sopenharmony_ci        if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)
216e1051a39Sopenharmony_ci            goto err;
217e1051a39Sopenharmony_ci    }
218e1051a39Sopenharmony_ci
219e1051a39Sopenharmony_ci    if (!BN_nnmod(val[0], a, m, ctx))
220e1051a39Sopenharmony_ci        goto err;               /* 1 */
221e1051a39Sopenharmony_ci    if (BN_is_zero(val[0])) {
222e1051a39Sopenharmony_ci        BN_zero(r);
223e1051a39Sopenharmony_ci        ret = 1;
224e1051a39Sopenharmony_ci        goto err;
225e1051a39Sopenharmony_ci    }
226e1051a39Sopenharmony_ci
227e1051a39Sopenharmony_ci    window = BN_window_bits_for_exponent_size(bits);
228e1051a39Sopenharmony_ci    if (window > 1) {
229e1051a39Sopenharmony_ci        if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))
230e1051a39Sopenharmony_ci            goto err;           /* 2 */
231e1051a39Sopenharmony_ci        j = 1 << (window - 1);
232e1051a39Sopenharmony_ci        for (i = 1; i < j; i++) {
233e1051a39Sopenharmony_ci            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
234e1051a39Sopenharmony_ci                !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))
235e1051a39Sopenharmony_ci                goto err;
236e1051a39Sopenharmony_ci        }
237e1051a39Sopenharmony_ci    }
238e1051a39Sopenharmony_ci
239e1051a39Sopenharmony_ci    start = 1;                  /* This is used to avoid multiplication etc
240e1051a39Sopenharmony_ci                                 * when there is only the value '1' in the
241e1051a39Sopenharmony_ci                                 * buffer. */
242e1051a39Sopenharmony_ci    wvalue = 0;                 /* The 'value' of the window */
243e1051a39Sopenharmony_ci    wstart = bits - 1;          /* The top bit of the window */
244e1051a39Sopenharmony_ci    wend = 0;                   /* The bottom bit of the window */
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ci    if (!BN_one(r))
247e1051a39Sopenharmony_ci        goto err;
248e1051a39Sopenharmony_ci
249e1051a39Sopenharmony_ci    for (;;) {
250e1051a39Sopenharmony_ci        if (BN_is_bit_set(p, wstart) == 0) {
251e1051a39Sopenharmony_ci            if (!start)
252e1051a39Sopenharmony_ci                if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
253e1051a39Sopenharmony_ci                    goto err;
254e1051a39Sopenharmony_ci            if (wstart == 0)
255e1051a39Sopenharmony_ci                break;
256e1051a39Sopenharmony_ci            wstart--;
257e1051a39Sopenharmony_ci            continue;
258e1051a39Sopenharmony_ci        }
259e1051a39Sopenharmony_ci        /*
260e1051a39Sopenharmony_ci         * We now have wstart on a 'set' bit, we now need to work out how bit
261e1051a39Sopenharmony_ci         * a window to do.  To do this we need to scan forward until the last
262e1051a39Sopenharmony_ci         * set bit before the end of the window
263e1051a39Sopenharmony_ci         */
264e1051a39Sopenharmony_ci        wvalue = 1;
265e1051a39Sopenharmony_ci        wend = 0;
266e1051a39Sopenharmony_ci        for (i = 1; i < window; i++) {
267e1051a39Sopenharmony_ci            if (wstart - i < 0)
268e1051a39Sopenharmony_ci                break;
269e1051a39Sopenharmony_ci            if (BN_is_bit_set(p, wstart - i)) {
270e1051a39Sopenharmony_ci                wvalue <<= (i - wend);
271e1051a39Sopenharmony_ci                wvalue |= 1;
272e1051a39Sopenharmony_ci                wend = i;
273e1051a39Sopenharmony_ci            }
274e1051a39Sopenharmony_ci        }
275e1051a39Sopenharmony_ci
276e1051a39Sopenharmony_ci        /* wend is the size of the current window */
277e1051a39Sopenharmony_ci        j = wend + 1;
278e1051a39Sopenharmony_ci        /* add the 'bytes above' */
279e1051a39Sopenharmony_ci        if (!start)
280e1051a39Sopenharmony_ci            for (i = 0; i < j; i++) {
281e1051a39Sopenharmony_ci                if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
282e1051a39Sopenharmony_ci                    goto err;
283e1051a39Sopenharmony_ci            }
284e1051a39Sopenharmony_ci
285e1051a39Sopenharmony_ci        /* wvalue will be an odd number < 2^window */
286e1051a39Sopenharmony_ci        if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))
287e1051a39Sopenharmony_ci            goto err;
288e1051a39Sopenharmony_ci
289e1051a39Sopenharmony_ci        /* move the 'window' down further */
290e1051a39Sopenharmony_ci        wstart -= wend + 1;
291e1051a39Sopenharmony_ci        wvalue = 0;
292e1051a39Sopenharmony_ci        start = 0;
293e1051a39Sopenharmony_ci        if (wstart < 0)
294e1051a39Sopenharmony_ci            break;
295e1051a39Sopenharmony_ci    }
296e1051a39Sopenharmony_ci    ret = 1;
297e1051a39Sopenharmony_ci err:
298e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
299e1051a39Sopenharmony_ci    BN_RECP_CTX_free(&recp);
300e1051a39Sopenharmony_ci    bn_check_top(r);
301e1051a39Sopenharmony_ci    return ret;
302e1051a39Sopenharmony_ci}
303e1051a39Sopenharmony_ci
304e1051a39Sopenharmony_ciint BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
305e1051a39Sopenharmony_ci                    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
306e1051a39Sopenharmony_ci{
307e1051a39Sopenharmony_ci    int i, j, bits, ret = 0, wstart, wend, window, wvalue;
308e1051a39Sopenharmony_ci    int start = 1;
309e1051a39Sopenharmony_ci    BIGNUM *d, *r;
310e1051a39Sopenharmony_ci    const BIGNUM *aa;
311e1051a39Sopenharmony_ci    /* Table of variables obtained from 'ctx' */
312e1051a39Sopenharmony_ci    BIGNUM *val[TABLE_SIZE];
313e1051a39Sopenharmony_ci    BN_MONT_CTX *mont = NULL;
314e1051a39Sopenharmony_ci
315e1051a39Sopenharmony_ci    bn_check_top(a);
316e1051a39Sopenharmony_ci    bn_check_top(p);
317e1051a39Sopenharmony_ci    bn_check_top(m);
318e1051a39Sopenharmony_ci
319e1051a39Sopenharmony_ci    if (!BN_is_odd(m)) {
320e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
321e1051a39Sopenharmony_ci        return 0;
322e1051a39Sopenharmony_ci    }
323e1051a39Sopenharmony_ci
324e1051a39Sopenharmony_ci    if (m->top <= BN_CONSTTIME_SIZE_LIMIT
325e1051a39Sopenharmony_ci        && (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
326e1051a39Sopenharmony_ci            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0
327e1051a39Sopenharmony_ci            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0)) {
328e1051a39Sopenharmony_ci        return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
329e1051a39Sopenharmony_ci    }
330e1051a39Sopenharmony_ci
331e1051a39Sopenharmony_ci    bits = BN_num_bits(p);
332e1051a39Sopenharmony_ci    if (bits == 0) {
333e1051a39Sopenharmony_ci        /* x**0 mod 1, or x**0 mod -1 is still zero. */
334e1051a39Sopenharmony_ci        if (BN_abs_is_word(m, 1)) {
335e1051a39Sopenharmony_ci            ret = 1;
336e1051a39Sopenharmony_ci            BN_zero(rr);
337e1051a39Sopenharmony_ci        } else {
338e1051a39Sopenharmony_ci            ret = BN_one(rr);
339e1051a39Sopenharmony_ci        }
340e1051a39Sopenharmony_ci        return ret;
341e1051a39Sopenharmony_ci    }
342e1051a39Sopenharmony_ci
343e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
344e1051a39Sopenharmony_ci    d = BN_CTX_get(ctx);
345e1051a39Sopenharmony_ci    r = BN_CTX_get(ctx);
346e1051a39Sopenharmony_ci    val[0] = BN_CTX_get(ctx);
347e1051a39Sopenharmony_ci    if (val[0] == NULL)
348e1051a39Sopenharmony_ci        goto err;
349e1051a39Sopenharmony_ci
350e1051a39Sopenharmony_ci    /*
351e1051a39Sopenharmony_ci     * If this is not done, things will break in the montgomery part
352e1051a39Sopenharmony_ci     */
353e1051a39Sopenharmony_ci
354e1051a39Sopenharmony_ci    if (in_mont != NULL)
355e1051a39Sopenharmony_ci        mont = in_mont;
356e1051a39Sopenharmony_ci    else {
357e1051a39Sopenharmony_ci        if ((mont = BN_MONT_CTX_new()) == NULL)
358e1051a39Sopenharmony_ci            goto err;
359e1051a39Sopenharmony_ci        if (!BN_MONT_CTX_set(mont, m, ctx))
360e1051a39Sopenharmony_ci            goto err;
361e1051a39Sopenharmony_ci    }
362e1051a39Sopenharmony_ci
363e1051a39Sopenharmony_ci    if (a->neg || BN_ucmp(a, m) >= 0) {
364e1051a39Sopenharmony_ci        if (!BN_nnmod(val[0], a, m, ctx))
365e1051a39Sopenharmony_ci            goto err;
366e1051a39Sopenharmony_ci        aa = val[0];
367e1051a39Sopenharmony_ci    } else
368e1051a39Sopenharmony_ci        aa = a;
369e1051a39Sopenharmony_ci    if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))
370e1051a39Sopenharmony_ci        goto err;               /* 1 */
371e1051a39Sopenharmony_ci
372e1051a39Sopenharmony_ci    window = BN_window_bits_for_exponent_size(bits);
373e1051a39Sopenharmony_ci    if (window > 1) {
374e1051a39Sopenharmony_ci        if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))
375e1051a39Sopenharmony_ci            goto err;           /* 2 */
376e1051a39Sopenharmony_ci        j = 1 << (window - 1);
377e1051a39Sopenharmony_ci        for (i = 1; i < j; i++) {
378e1051a39Sopenharmony_ci            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
379e1051a39Sopenharmony_ci                !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))
380e1051a39Sopenharmony_ci                goto err;
381e1051a39Sopenharmony_ci        }
382e1051a39Sopenharmony_ci    }
383e1051a39Sopenharmony_ci
384e1051a39Sopenharmony_ci    start = 1;                  /* This is used to avoid multiplication etc
385e1051a39Sopenharmony_ci                                 * when there is only the value '1' in the
386e1051a39Sopenharmony_ci                                 * buffer. */
387e1051a39Sopenharmony_ci    wvalue = 0;                 /* The 'value' of the window */
388e1051a39Sopenharmony_ci    wstart = bits - 1;          /* The top bit of the window */
389e1051a39Sopenharmony_ci    wend = 0;                   /* The bottom bit of the window */
390e1051a39Sopenharmony_ci
391e1051a39Sopenharmony_ci#if 1                           /* by Shay Gueron's suggestion */
392e1051a39Sopenharmony_ci    j = m->top;                 /* borrow j */
393e1051a39Sopenharmony_ci    if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
394e1051a39Sopenharmony_ci        if (bn_wexpand(r, j) == NULL)
395e1051a39Sopenharmony_ci            goto err;
396e1051a39Sopenharmony_ci        /* 2^(top*BN_BITS2) - m */
397e1051a39Sopenharmony_ci        r->d[0] = (0 - m->d[0]) & BN_MASK2;
398e1051a39Sopenharmony_ci        for (i = 1; i < j; i++)
399e1051a39Sopenharmony_ci            r->d[i] = (~m->d[i]) & BN_MASK2;
400e1051a39Sopenharmony_ci        r->top = j;
401e1051a39Sopenharmony_ci        r->flags |= BN_FLG_FIXED_TOP;
402e1051a39Sopenharmony_ci    } else
403e1051a39Sopenharmony_ci#endif
404e1051a39Sopenharmony_ci    if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))
405e1051a39Sopenharmony_ci        goto err;
406e1051a39Sopenharmony_ci    for (;;) {
407e1051a39Sopenharmony_ci        if (BN_is_bit_set(p, wstart) == 0) {
408e1051a39Sopenharmony_ci            if (!start) {
409e1051a39Sopenharmony_ci                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))
410e1051a39Sopenharmony_ci                    goto err;
411e1051a39Sopenharmony_ci            }
412e1051a39Sopenharmony_ci            if (wstart == 0)
413e1051a39Sopenharmony_ci                break;
414e1051a39Sopenharmony_ci            wstart--;
415e1051a39Sopenharmony_ci            continue;
416e1051a39Sopenharmony_ci        }
417e1051a39Sopenharmony_ci        /*
418e1051a39Sopenharmony_ci         * We now have wstart on a 'set' bit, we now need to work out how bit
419e1051a39Sopenharmony_ci         * a window to do.  To do this we need to scan forward until the last
420e1051a39Sopenharmony_ci         * set bit before the end of the window
421e1051a39Sopenharmony_ci         */
422e1051a39Sopenharmony_ci        wvalue = 1;
423e1051a39Sopenharmony_ci        wend = 0;
424e1051a39Sopenharmony_ci        for (i = 1; i < window; i++) {
425e1051a39Sopenharmony_ci            if (wstart - i < 0)
426e1051a39Sopenharmony_ci                break;
427e1051a39Sopenharmony_ci            if (BN_is_bit_set(p, wstart - i)) {
428e1051a39Sopenharmony_ci                wvalue <<= (i - wend);
429e1051a39Sopenharmony_ci                wvalue |= 1;
430e1051a39Sopenharmony_ci                wend = i;
431e1051a39Sopenharmony_ci            }
432e1051a39Sopenharmony_ci        }
433e1051a39Sopenharmony_ci
434e1051a39Sopenharmony_ci        /* wend is the size of the current window */
435e1051a39Sopenharmony_ci        j = wend + 1;
436e1051a39Sopenharmony_ci        /* add the 'bytes above' */
437e1051a39Sopenharmony_ci        if (!start)
438e1051a39Sopenharmony_ci            for (i = 0; i < j; i++) {
439e1051a39Sopenharmony_ci                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))
440e1051a39Sopenharmony_ci                    goto err;
441e1051a39Sopenharmony_ci            }
442e1051a39Sopenharmony_ci
443e1051a39Sopenharmony_ci        /* wvalue will be an odd number < 2^window */
444e1051a39Sopenharmony_ci        if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))
445e1051a39Sopenharmony_ci            goto err;
446e1051a39Sopenharmony_ci
447e1051a39Sopenharmony_ci        /* move the 'window' down further */
448e1051a39Sopenharmony_ci        wstart -= wend + 1;
449e1051a39Sopenharmony_ci        wvalue = 0;
450e1051a39Sopenharmony_ci        start = 0;
451e1051a39Sopenharmony_ci        if (wstart < 0)
452e1051a39Sopenharmony_ci            break;
453e1051a39Sopenharmony_ci    }
454e1051a39Sopenharmony_ci    /*
455e1051a39Sopenharmony_ci     * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery
456e1051a39Sopenharmony_ci     * removes padding [if any] and makes return value suitable for public
457e1051a39Sopenharmony_ci     * API consumer.
458e1051a39Sopenharmony_ci     */
459e1051a39Sopenharmony_ci#if defined(SPARC_T4_MONT)
460e1051a39Sopenharmony_ci    if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
461e1051a39Sopenharmony_ci        j = mont->N.top;        /* borrow j */
462e1051a39Sopenharmony_ci        val[0]->d[0] = 1;       /* borrow val[0] */
463e1051a39Sopenharmony_ci        for (i = 1; i < j; i++)
464e1051a39Sopenharmony_ci            val[0]->d[i] = 0;
465e1051a39Sopenharmony_ci        val[0]->top = j;
466e1051a39Sopenharmony_ci        if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))
467e1051a39Sopenharmony_ci            goto err;
468e1051a39Sopenharmony_ci    } else
469e1051a39Sopenharmony_ci#endif
470e1051a39Sopenharmony_ci    if (!BN_from_montgomery(rr, r, mont, ctx))
471e1051a39Sopenharmony_ci        goto err;
472e1051a39Sopenharmony_ci    ret = 1;
473e1051a39Sopenharmony_ci err:
474e1051a39Sopenharmony_ci    if (in_mont == NULL)
475e1051a39Sopenharmony_ci        BN_MONT_CTX_free(mont);
476e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
477e1051a39Sopenharmony_ci    bn_check_top(rr);
478e1051a39Sopenharmony_ci    return ret;
479e1051a39Sopenharmony_ci}
480e1051a39Sopenharmony_ci
481e1051a39Sopenharmony_cistatic BN_ULONG bn_get_bits(const BIGNUM *a, int bitpos)
482e1051a39Sopenharmony_ci{
483e1051a39Sopenharmony_ci    BN_ULONG ret = 0;
484e1051a39Sopenharmony_ci    int wordpos;
485e1051a39Sopenharmony_ci
486e1051a39Sopenharmony_ci    wordpos = bitpos / BN_BITS2;
487e1051a39Sopenharmony_ci    bitpos %= BN_BITS2;
488e1051a39Sopenharmony_ci    if (wordpos >= 0 && wordpos < a->top) {
489e1051a39Sopenharmony_ci        ret = a->d[wordpos] & BN_MASK2;
490e1051a39Sopenharmony_ci        if (bitpos) {
491e1051a39Sopenharmony_ci            ret >>= bitpos;
492e1051a39Sopenharmony_ci            if (++wordpos < a->top)
493e1051a39Sopenharmony_ci                ret |= a->d[wordpos] << (BN_BITS2 - bitpos);
494e1051a39Sopenharmony_ci        }
495e1051a39Sopenharmony_ci    }
496e1051a39Sopenharmony_ci
497e1051a39Sopenharmony_ci    return ret & BN_MASK2;
498e1051a39Sopenharmony_ci}
499e1051a39Sopenharmony_ci
500e1051a39Sopenharmony_ci/*
501e1051a39Sopenharmony_ci * BN_mod_exp_mont_consttime() stores the precomputed powers in a specific
502e1051a39Sopenharmony_ci * layout so that accessing any of these table values shows the same access
503e1051a39Sopenharmony_ci * pattern as far as cache lines are concerned.  The following functions are
504e1051a39Sopenharmony_ci * used to transfer a BIGNUM from/to that table.
505e1051a39Sopenharmony_ci */
506e1051a39Sopenharmony_ci
507e1051a39Sopenharmony_cistatic int MOD_EXP_CTIME_COPY_TO_PREBUF(const BIGNUM *b, int top,
508e1051a39Sopenharmony_ci                                        unsigned char *buf, int idx,
509e1051a39Sopenharmony_ci                                        int window)
510e1051a39Sopenharmony_ci{
511e1051a39Sopenharmony_ci    int i, j;
512e1051a39Sopenharmony_ci    int width = 1 << window;
513e1051a39Sopenharmony_ci    BN_ULONG *table = (BN_ULONG *)buf;
514e1051a39Sopenharmony_ci
515e1051a39Sopenharmony_ci    if (top > b->top)
516e1051a39Sopenharmony_ci        top = b->top;           /* this works because 'buf' is explicitly
517e1051a39Sopenharmony_ci                                 * zeroed */
518e1051a39Sopenharmony_ci    for (i = 0, j = idx; i < top; i++, j += width) {
519e1051a39Sopenharmony_ci        table[j] = b->d[i];
520e1051a39Sopenharmony_ci    }
521e1051a39Sopenharmony_ci
522e1051a39Sopenharmony_ci    return 1;
523e1051a39Sopenharmony_ci}
524e1051a39Sopenharmony_ci
525e1051a39Sopenharmony_cistatic int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,
526e1051a39Sopenharmony_ci                                          unsigned char *buf, int idx,
527e1051a39Sopenharmony_ci                                          int window)
528e1051a39Sopenharmony_ci{
529e1051a39Sopenharmony_ci    int i, j;
530e1051a39Sopenharmony_ci    int width = 1 << window;
531e1051a39Sopenharmony_ci    /*
532e1051a39Sopenharmony_ci     * We declare table 'volatile' in order to discourage compiler
533e1051a39Sopenharmony_ci     * from reordering loads from the table. Concern is that if
534e1051a39Sopenharmony_ci     * reordered in specific manner loads might give away the
535e1051a39Sopenharmony_ci     * information we are trying to conceal. Some would argue that
536e1051a39Sopenharmony_ci     * compiler can reorder them anyway, but it can as well be
537e1051a39Sopenharmony_ci     * argued that doing so would be violation of standard...
538e1051a39Sopenharmony_ci     */
539e1051a39Sopenharmony_ci    volatile BN_ULONG *table = (volatile BN_ULONG *)buf;
540e1051a39Sopenharmony_ci
541e1051a39Sopenharmony_ci    if (bn_wexpand(b, top) == NULL)
542e1051a39Sopenharmony_ci        return 0;
543e1051a39Sopenharmony_ci
544e1051a39Sopenharmony_ci    if (window <= 3) {
545e1051a39Sopenharmony_ci        for (i = 0; i < top; i++, table += width) {
546e1051a39Sopenharmony_ci            BN_ULONG acc = 0;
547e1051a39Sopenharmony_ci
548e1051a39Sopenharmony_ci            for (j = 0; j < width; j++) {
549e1051a39Sopenharmony_ci                acc |= table[j] &
550e1051a39Sopenharmony_ci                       ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));
551e1051a39Sopenharmony_ci            }
552e1051a39Sopenharmony_ci
553e1051a39Sopenharmony_ci            b->d[i] = acc;
554e1051a39Sopenharmony_ci        }
555e1051a39Sopenharmony_ci    } else {
556e1051a39Sopenharmony_ci        int xstride = 1 << (window - 2);
557e1051a39Sopenharmony_ci        BN_ULONG y0, y1, y2, y3;
558e1051a39Sopenharmony_ci
559e1051a39Sopenharmony_ci        i = idx >> (window - 2);        /* equivalent of idx / xstride */
560e1051a39Sopenharmony_ci        idx &= xstride - 1;             /* equivalent of idx % xstride */
561e1051a39Sopenharmony_ci
562e1051a39Sopenharmony_ci        y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);
563e1051a39Sopenharmony_ci        y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);
564e1051a39Sopenharmony_ci        y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);
565e1051a39Sopenharmony_ci        y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);
566e1051a39Sopenharmony_ci
567e1051a39Sopenharmony_ci        for (i = 0; i < top; i++, table += width) {
568e1051a39Sopenharmony_ci            BN_ULONG acc = 0;
569e1051a39Sopenharmony_ci
570e1051a39Sopenharmony_ci            for (j = 0; j < xstride; j++) {
571e1051a39Sopenharmony_ci                acc |= ( (table[j + 0 * xstride] & y0) |
572e1051a39Sopenharmony_ci                         (table[j + 1 * xstride] & y1) |
573e1051a39Sopenharmony_ci                         (table[j + 2 * xstride] & y2) |
574e1051a39Sopenharmony_ci                         (table[j + 3 * xstride] & y3) )
575e1051a39Sopenharmony_ci                       & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));
576e1051a39Sopenharmony_ci            }
577e1051a39Sopenharmony_ci
578e1051a39Sopenharmony_ci            b->d[i] = acc;
579e1051a39Sopenharmony_ci        }
580e1051a39Sopenharmony_ci    }
581e1051a39Sopenharmony_ci
582e1051a39Sopenharmony_ci    b->top = top;
583e1051a39Sopenharmony_ci    b->flags |= BN_FLG_FIXED_TOP;
584e1051a39Sopenharmony_ci    return 1;
585e1051a39Sopenharmony_ci}
586e1051a39Sopenharmony_ci
587e1051a39Sopenharmony_ci/*
588e1051a39Sopenharmony_ci * Given a pointer value, compute the next address that is a cache line
589e1051a39Sopenharmony_ci * multiple.
590e1051a39Sopenharmony_ci */
591e1051a39Sopenharmony_ci#define MOD_EXP_CTIME_ALIGN(x_) \
592e1051a39Sopenharmony_ci        ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
593e1051a39Sopenharmony_ci
594e1051a39Sopenharmony_ci/*
595e1051a39Sopenharmony_ci * This variant of BN_mod_exp_mont() uses fixed windows and the special
596e1051a39Sopenharmony_ci * precomputation memory layout to limit data-dependency to a minimum to
597e1051a39Sopenharmony_ci * protect secret exponents (cf. the hyper-threading timing attacks pointed
598e1051a39Sopenharmony_ci * out by Colin Percival,
599e1051a39Sopenharmony_ci * http://www.daemonology.net/hyperthreading-considered-harmful/)
600e1051a39Sopenharmony_ci */
601e1051a39Sopenharmony_ciint BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
602e1051a39Sopenharmony_ci                              const BIGNUM *m, BN_CTX *ctx,
603e1051a39Sopenharmony_ci                              BN_MONT_CTX *in_mont)
604e1051a39Sopenharmony_ci{
605e1051a39Sopenharmony_ci    int i, bits, ret = 0, window, wvalue, wmask, window0;
606e1051a39Sopenharmony_ci    int top;
607e1051a39Sopenharmony_ci    BN_MONT_CTX *mont = NULL;
608e1051a39Sopenharmony_ci
609e1051a39Sopenharmony_ci    int numPowers;
610e1051a39Sopenharmony_ci    unsigned char *powerbufFree = NULL;
611e1051a39Sopenharmony_ci    int powerbufLen = 0;
612e1051a39Sopenharmony_ci    unsigned char *powerbuf = NULL;
613e1051a39Sopenharmony_ci    BIGNUM tmp, am;
614e1051a39Sopenharmony_ci#if defined(SPARC_T4_MONT)
615e1051a39Sopenharmony_ci    unsigned int t4 = 0;
616e1051a39Sopenharmony_ci#endif
617e1051a39Sopenharmony_ci
618e1051a39Sopenharmony_ci    bn_check_top(a);
619e1051a39Sopenharmony_ci    bn_check_top(p);
620e1051a39Sopenharmony_ci    bn_check_top(m);
621e1051a39Sopenharmony_ci
622e1051a39Sopenharmony_ci    if (!BN_is_odd(m)) {
623e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
624e1051a39Sopenharmony_ci        return 0;
625e1051a39Sopenharmony_ci    }
626e1051a39Sopenharmony_ci
627e1051a39Sopenharmony_ci    top = m->top;
628e1051a39Sopenharmony_ci
629e1051a39Sopenharmony_ci    if (top > BN_CONSTTIME_SIZE_LIMIT) {
630e1051a39Sopenharmony_ci        /* Prevent overflowing the powerbufLen computation below */
631e1051a39Sopenharmony_ci        return BN_mod_exp_mont(rr, a, p, m, ctx, in_mont);
632e1051a39Sopenharmony_ci    }
633e1051a39Sopenharmony_ci
634e1051a39Sopenharmony_ci    /*
635e1051a39Sopenharmony_ci     * Use all bits stored in |p|, rather than |BN_num_bits|, so we do not leak
636e1051a39Sopenharmony_ci     * whether the top bits are zero.
637e1051a39Sopenharmony_ci     */
638e1051a39Sopenharmony_ci    bits = p->top * BN_BITS2;
639e1051a39Sopenharmony_ci    if (bits == 0) {
640e1051a39Sopenharmony_ci        /* x**0 mod 1, or x**0 mod -1 is still zero. */
641e1051a39Sopenharmony_ci        if (BN_abs_is_word(m, 1)) {
642e1051a39Sopenharmony_ci            ret = 1;
643e1051a39Sopenharmony_ci            BN_zero(rr);
644e1051a39Sopenharmony_ci        } else {
645e1051a39Sopenharmony_ci            ret = BN_one(rr);
646e1051a39Sopenharmony_ci        }
647e1051a39Sopenharmony_ci        return ret;
648e1051a39Sopenharmony_ci    }
649e1051a39Sopenharmony_ci
650e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
651e1051a39Sopenharmony_ci
652e1051a39Sopenharmony_ci    /*
653e1051a39Sopenharmony_ci     * Allocate a montgomery context if it was not supplied by the caller. If
654e1051a39Sopenharmony_ci     * this is not done, things will break in the montgomery part.
655e1051a39Sopenharmony_ci     */
656e1051a39Sopenharmony_ci    if (in_mont != NULL)
657e1051a39Sopenharmony_ci        mont = in_mont;
658e1051a39Sopenharmony_ci    else {
659e1051a39Sopenharmony_ci        if ((mont = BN_MONT_CTX_new()) == NULL)
660e1051a39Sopenharmony_ci            goto err;
661e1051a39Sopenharmony_ci        if (!BN_MONT_CTX_set(mont, m, ctx))
662e1051a39Sopenharmony_ci            goto err;
663e1051a39Sopenharmony_ci    }
664e1051a39Sopenharmony_ci
665e1051a39Sopenharmony_ci    if (a->neg || BN_ucmp(a, m) >= 0) {
666e1051a39Sopenharmony_ci        BIGNUM *reduced = BN_CTX_get(ctx);
667e1051a39Sopenharmony_ci        if (reduced == NULL
668e1051a39Sopenharmony_ci            || !BN_nnmod(reduced, a, m, ctx)) {
669e1051a39Sopenharmony_ci            goto err;
670e1051a39Sopenharmony_ci        }
671e1051a39Sopenharmony_ci        a = reduced;
672e1051a39Sopenharmony_ci    }
673e1051a39Sopenharmony_ci
674e1051a39Sopenharmony_ci#ifdef RSAZ_ENABLED
675e1051a39Sopenharmony_ci    /*
676e1051a39Sopenharmony_ci     * If the size of the operands allow it, perform the optimized
677e1051a39Sopenharmony_ci     * RSAZ exponentiation. For further information see
678e1051a39Sopenharmony_ci     * crypto/bn/rsaz_exp.c and accompanying assembly modules.
679e1051a39Sopenharmony_ci     */
680e1051a39Sopenharmony_ci    if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)
681e1051a39Sopenharmony_ci        && rsaz_avx2_eligible()) {
682e1051a39Sopenharmony_ci        if (NULL == bn_wexpand(rr, 16))
683e1051a39Sopenharmony_ci            goto err;
684e1051a39Sopenharmony_ci        RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,
685e1051a39Sopenharmony_ci                               mont->n0[0]);
686e1051a39Sopenharmony_ci        rr->top = 16;
687e1051a39Sopenharmony_ci        rr->neg = 0;
688e1051a39Sopenharmony_ci        bn_correct_top(rr);
689e1051a39Sopenharmony_ci        ret = 1;
690e1051a39Sopenharmony_ci        goto err;
691e1051a39Sopenharmony_ci    } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {
692e1051a39Sopenharmony_ci        if (NULL == bn_wexpand(rr, 8))
693e1051a39Sopenharmony_ci            goto err;
694e1051a39Sopenharmony_ci        RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);
695e1051a39Sopenharmony_ci        rr->top = 8;
696e1051a39Sopenharmony_ci        rr->neg = 0;
697e1051a39Sopenharmony_ci        bn_correct_top(rr);
698e1051a39Sopenharmony_ci        ret = 1;
699e1051a39Sopenharmony_ci        goto err;
700e1051a39Sopenharmony_ci    }
701e1051a39Sopenharmony_ci#endif
702e1051a39Sopenharmony_ci
703e1051a39Sopenharmony_ci    /* Get the window size to use with size of p. */
704e1051a39Sopenharmony_ci    window = BN_window_bits_for_ctime_exponent_size(bits);
705e1051a39Sopenharmony_ci#if defined(SPARC_T4_MONT)
706e1051a39Sopenharmony_ci    if (window >= 5 && (top & 15) == 0 && top <= 64 &&
707e1051a39Sopenharmony_ci        (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
708e1051a39Sopenharmony_ci        (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))
709e1051a39Sopenharmony_ci        window = 5;
710e1051a39Sopenharmony_ci    else
711e1051a39Sopenharmony_ci#endif
712e1051a39Sopenharmony_ci#if defined(OPENSSL_BN_ASM_MONT5)
713e1051a39Sopenharmony_ci    if (window >= 5 && top <= BN_SOFT_LIMIT) {
714e1051a39Sopenharmony_ci        window = 5;             /* ~5% improvement for RSA2048 sign, and even
715e1051a39Sopenharmony_ci                                 * for RSA4096 */
716e1051a39Sopenharmony_ci        /* reserve space for mont->N.d[] copy */
717e1051a39Sopenharmony_ci        powerbufLen += top * sizeof(mont->N.d[0]);
718e1051a39Sopenharmony_ci    }
719e1051a39Sopenharmony_ci#endif
720e1051a39Sopenharmony_ci    (void)0;
721e1051a39Sopenharmony_ci
722e1051a39Sopenharmony_ci    /*
723e1051a39Sopenharmony_ci     * Allocate a buffer large enough to hold all of the pre-computed powers
724e1051a39Sopenharmony_ci     * of am, am itself and tmp.
725e1051a39Sopenharmony_ci     */
726e1051a39Sopenharmony_ci    numPowers = 1 << window;
727e1051a39Sopenharmony_ci    powerbufLen += sizeof(m->d[0]) * (top * numPowers +
728e1051a39Sopenharmony_ci                                      ((2 * top) >
729e1051a39Sopenharmony_ci                                       numPowers ? (2 * top) : numPowers));
730e1051a39Sopenharmony_ci#ifdef alloca
731e1051a39Sopenharmony_ci    if (powerbufLen < 3072)
732e1051a39Sopenharmony_ci        powerbufFree =
733e1051a39Sopenharmony_ci            alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
734e1051a39Sopenharmony_ci    else
735e1051a39Sopenharmony_ci#endif
736e1051a39Sopenharmony_ci        if ((powerbufFree =
737e1051a39Sopenharmony_ci             OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))
738e1051a39Sopenharmony_ci            == NULL)
739e1051a39Sopenharmony_ci        goto err;
740e1051a39Sopenharmony_ci
741e1051a39Sopenharmony_ci    powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
742e1051a39Sopenharmony_ci    memset(powerbuf, 0, powerbufLen);
743e1051a39Sopenharmony_ci
744e1051a39Sopenharmony_ci#ifdef alloca
745e1051a39Sopenharmony_ci    if (powerbufLen < 3072)
746e1051a39Sopenharmony_ci        powerbufFree = NULL;
747e1051a39Sopenharmony_ci#endif
748e1051a39Sopenharmony_ci
749e1051a39Sopenharmony_ci    /* lay down tmp and am right after powers table */
750e1051a39Sopenharmony_ci    tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);
751e1051a39Sopenharmony_ci    am.d = tmp.d + top;
752e1051a39Sopenharmony_ci    tmp.top = am.top = 0;
753e1051a39Sopenharmony_ci    tmp.dmax = am.dmax = top;
754e1051a39Sopenharmony_ci    tmp.neg = am.neg = 0;
755e1051a39Sopenharmony_ci    tmp.flags = am.flags = BN_FLG_STATIC_DATA;
756e1051a39Sopenharmony_ci
757e1051a39Sopenharmony_ci    /* prepare a^0 in Montgomery domain */
758e1051a39Sopenharmony_ci#if 1                           /* by Shay Gueron's suggestion */
759e1051a39Sopenharmony_ci    if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
760e1051a39Sopenharmony_ci        /* 2^(top*BN_BITS2) - m */
761e1051a39Sopenharmony_ci        tmp.d[0] = (0 - m->d[0]) & BN_MASK2;
762e1051a39Sopenharmony_ci        for (i = 1; i < top; i++)
763e1051a39Sopenharmony_ci            tmp.d[i] = (~m->d[i]) & BN_MASK2;
764e1051a39Sopenharmony_ci        tmp.top = top;
765e1051a39Sopenharmony_ci    } else
766e1051a39Sopenharmony_ci#endif
767e1051a39Sopenharmony_ci    if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))
768e1051a39Sopenharmony_ci        goto err;
769e1051a39Sopenharmony_ci
770e1051a39Sopenharmony_ci    /* prepare a^1 in Montgomery domain */
771e1051a39Sopenharmony_ci    if (!bn_to_mont_fixed_top(&am, a, mont, ctx))
772e1051a39Sopenharmony_ci        goto err;
773e1051a39Sopenharmony_ci
774e1051a39Sopenharmony_ci    if (top > BN_SOFT_LIMIT)
775e1051a39Sopenharmony_ci        goto fallback;
776e1051a39Sopenharmony_ci
777e1051a39Sopenharmony_ci#if defined(SPARC_T4_MONT)
778e1051a39Sopenharmony_ci    if (t4) {
779e1051a39Sopenharmony_ci        typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,
780e1051a39Sopenharmony_ci                                       const BN_ULONG *n0, const void *table,
781e1051a39Sopenharmony_ci                                       int power, int bits);
782e1051a39Sopenharmony_ci        int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,
783e1051a39Sopenharmony_ci                              const BN_ULONG *n0, const void *table,
784e1051a39Sopenharmony_ci                              int power, int bits);
785e1051a39Sopenharmony_ci        int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,
786e1051a39Sopenharmony_ci                               const BN_ULONG *n0, const void *table,
787e1051a39Sopenharmony_ci                               int power, int bits);
788e1051a39Sopenharmony_ci        int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,
789e1051a39Sopenharmony_ci                               const BN_ULONG *n0, const void *table,
790e1051a39Sopenharmony_ci                               int power, int bits);
791e1051a39Sopenharmony_ci        int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,
792e1051a39Sopenharmony_ci                               const BN_ULONG *n0, const void *table,
793e1051a39Sopenharmony_ci                               int power, int bits);
794e1051a39Sopenharmony_ci        static const bn_pwr5_mont_f pwr5_funcs[4] = {
795e1051a39Sopenharmony_ci            bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,
796e1051a39Sopenharmony_ci            bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32
797e1051a39Sopenharmony_ci        };
798e1051a39Sopenharmony_ci        bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];
799e1051a39Sopenharmony_ci
800e1051a39Sopenharmony_ci        typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
801e1051a39Sopenharmony_ci                                      const void *bp, const BN_ULONG *np,
802e1051a39Sopenharmony_ci                                      const BN_ULONG *n0);
803e1051a39Sopenharmony_ci        int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,
804e1051a39Sopenharmony_ci                             const BN_ULONG *np, const BN_ULONG *n0);
805e1051a39Sopenharmony_ci        int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
806e1051a39Sopenharmony_ci                              const void *bp, const BN_ULONG *np,
807e1051a39Sopenharmony_ci                              const BN_ULONG *n0);
808e1051a39Sopenharmony_ci        int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
809e1051a39Sopenharmony_ci                              const void *bp, const BN_ULONG *np,
810e1051a39Sopenharmony_ci                              const BN_ULONG *n0);
811e1051a39Sopenharmony_ci        int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
812e1051a39Sopenharmony_ci                              const void *bp, const BN_ULONG *np,
813e1051a39Sopenharmony_ci                              const BN_ULONG *n0);
814e1051a39Sopenharmony_ci        static const bn_mul_mont_f mul_funcs[4] = {
815e1051a39Sopenharmony_ci            bn_mul_mont_t4_8, bn_mul_mont_t4_16,
816e1051a39Sopenharmony_ci            bn_mul_mont_t4_24, bn_mul_mont_t4_32
817e1051a39Sopenharmony_ci        };
818e1051a39Sopenharmony_ci        bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];
819e1051a39Sopenharmony_ci
820e1051a39Sopenharmony_ci        void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,
821e1051a39Sopenharmony_ci                              const void *bp, const BN_ULONG *np,
822e1051a39Sopenharmony_ci                              const BN_ULONG *n0, int num);
823e1051a39Sopenharmony_ci        void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,
824e1051a39Sopenharmony_ci                            const void *bp, const BN_ULONG *np,
825e1051a39Sopenharmony_ci                            const BN_ULONG *n0, int num);
826e1051a39Sopenharmony_ci        void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,
827e1051a39Sopenharmony_ci                                    const void *table, const BN_ULONG *np,
828e1051a39Sopenharmony_ci                                    const BN_ULONG *n0, int num, int power);
829e1051a39Sopenharmony_ci        void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,
830e1051a39Sopenharmony_ci                                   void *table, size_t power);
831e1051a39Sopenharmony_ci        void bn_gather5_t4(BN_ULONG *out, size_t num,
832e1051a39Sopenharmony_ci                           void *table, size_t power);
833e1051a39Sopenharmony_ci        void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);
834e1051a39Sopenharmony_ci
835e1051a39Sopenharmony_ci        BN_ULONG *np = mont->N.d, *n0 = mont->n0;
836e1051a39Sopenharmony_ci        int stride = 5 * (6 - (top / 16 - 1)); /* multiple of 5, but less
837e1051a39Sopenharmony_ci                                                * than 32 */
838e1051a39Sopenharmony_ci
839e1051a39Sopenharmony_ci        /*
840e1051a39Sopenharmony_ci         * BN_to_montgomery can contaminate words above .top [in
841e1051a39Sopenharmony_ci         * BN_DEBUG build...
842e1051a39Sopenharmony_ci         */
843e1051a39Sopenharmony_ci        for (i = am.top; i < top; i++)
844e1051a39Sopenharmony_ci            am.d[i] = 0;
845e1051a39Sopenharmony_ci        for (i = tmp.top; i < top; i++)
846e1051a39Sopenharmony_ci            tmp.d[i] = 0;
847e1051a39Sopenharmony_ci
848e1051a39Sopenharmony_ci        bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);
849e1051a39Sopenharmony_ci        bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);
850e1051a39Sopenharmony_ci        if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&
851e1051a39Sopenharmony_ci            !(*mul_worker) (tmp.d, am.d, am.d, np, n0))
852e1051a39Sopenharmony_ci            bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);
853e1051a39Sopenharmony_ci        bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);
854e1051a39Sopenharmony_ci
855e1051a39Sopenharmony_ci        for (i = 3; i < 32; i++) {
856e1051a39Sopenharmony_ci            /* Calculate a^i = a^(i-1) * a */
857e1051a39Sopenharmony_ci            if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&
858e1051a39Sopenharmony_ci                !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))
859e1051a39Sopenharmony_ci                bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);
860e1051a39Sopenharmony_ci            bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);
861e1051a39Sopenharmony_ci        }
862e1051a39Sopenharmony_ci
863e1051a39Sopenharmony_ci        /* switch to 64-bit domain */
864e1051a39Sopenharmony_ci        np = alloca(top * sizeof(BN_ULONG));
865e1051a39Sopenharmony_ci        top /= 2;
866e1051a39Sopenharmony_ci        bn_flip_t4(np, mont->N.d, top);
867e1051a39Sopenharmony_ci
868e1051a39Sopenharmony_ci        /*
869e1051a39Sopenharmony_ci         * The exponent may not have a whole number of fixed-size windows.
870e1051a39Sopenharmony_ci         * To simplify the main loop, the initial window has between 1 and
871e1051a39Sopenharmony_ci         * full-window-size bits such that what remains is always a whole
872e1051a39Sopenharmony_ci         * number of windows
873e1051a39Sopenharmony_ci         */
874e1051a39Sopenharmony_ci        window0 = (bits - 1) % 5 + 1;
875e1051a39Sopenharmony_ci        wmask = (1 << window0) - 1;
876e1051a39Sopenharmony_ci        bits -= window0;
877e1051a39Sopenharmony_ci        wvalue = bn_get_bits(p, bits) & wmask;
878e1051a39Sopenharmony_ci        bn_gather5_t4(tmp.d, top, powerbuf, wvalue);
879e1051a39Sopenharmony_ci
880e1051a39Sopenharmony_ci        /*
881e1051a39Sopenharmony_ci         * Scan the exponent one window at a time starting from the most
882e1051a39Sopenharmony_ci         * significant bits.
883e1051a39Sopenharmony_ci         */
884e1051a39Sopenharmony_ci        while (bits > 0) {
885e1051a39Sopenharmony_ci            if (bits < stride)
886e1051a39Sopenharmony_ci                stride = bits;
887e1051a39Sopenharmony_ci            bits -= stride;
888e1051a39Sopenharmony_ci            wvalue = bn_get_bits(p, bits);
889e1051a39Sopenharmony_ci
890e1051a39Sopenharmony_ci            if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
891e1051a39Sopenharmony_ci                continue;
892e1051a39Sopenharmony_ci            /* retry once and fall back */
893e1051a39Sopenharmony_ci            if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
894e1051a39Sopenharmony_ci                continue;
895e1051a39Sopenharmony_ci
896e1051a39Sopenharmony_ci            bits += stride - 5;
897e1051a39Sopenharmony_ci            wvalue >>= stride - 5;
898e1051a39Sopenharmony_ci            wvalue &= 31;
899e1051a39Sopenharmony_ci            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
900e1051a39Sopenharmony_ci            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
901e1051a39Sopenharmony_ci            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
902e1051a39Sopenharmony_ci            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
903e1051a39Sopenharmony_ci            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
904e1051a39Sopenharmony_ci            bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,
905e1051a39Sopenharmony_ci                                   wvalue);
906e1051a39Sopenharmony_ci        }
907e1051a39Sopenharmony_ci
908e1051a39Sopenharmony_ci        bn_flip_t4(tmp.d, tmp.d, top);
909e1051a39Sopenharmony_ci        top *= 2;
910e1051a39Sopenharmony_ci        /* back to 32-bit domain */
911e1051a39Sopenharmony_ci        tmp.top = top;
912e1051a39Sopenharmony_ci        bn_correct_top(&tmp);
913e1051a39Sopenharmony_ci        OPENSSL_cleanse(np, top * sizeof(BN_ULONG));
914e1051a39Sopenharmony_ci    } else
915e1051a39Sopenharmony_ci#endif
916e1051a39Sopenharmony_ci#if defined(OPENSSL_BN_ASM_MONT5)
917e1051a39Sopenharmony_ci    if (window == 5 && top > 1) {
918e1051a39Sopenharmony_ci        /*
919e1051a39Sopenharmony_ci         * This optimization uses ideas from https://eprint.iacr.org/2011/239,
920e1051a39Sopenharmony_ci         * specifically optimization of cache-timing attack countermeasures,
921e1051a39Sopenharmony_ci         * pre-computation optimization, and Almost Montgomery Multiplication.
922e1051a39Sopenharmony_ci         *
923e1051a39Sopenharmony_ci         * The paper discusses a 4-bit window to optimize 512-bit modular
924e1051a39Sopenharmony_ci         * exponentiation, used in RSA-1024 with CRT, but RSA-1024 is no longer
925e1051a39Sopenharmony_ci         * important.
926e1051a39Sopenharmony_ci         *
927e1051a39Sopenharmony_ci         * |bn_mul_mont_gather5| and |bn_power5| implement the "almost"
928e1051a39Sopenharmony_ci         * reduction variant, so the values here may not be fully reduced.
929e1051a39Sopenharmony_ci         * They are bounded by R (i.e. they fit in |top| words), not |m|.
930e1051a39Sopenharmony_ci         * Additionally, we pass these "almost" reduced inputs into
931e1051a39Sopenharmony_ci         * |bn_mul_mont|, which implements the normal reduction variant.
932e1051a39Sopenharmony_ci         * Given those inputs, |bn_mul_mont| may not give reduced
933e1051a39Sopenharmony_ci         * output, but it will still produce "almost" reduced output.
934e1051a39Sopenharmony_ci         */
935e1051a39Sopenharmony_ci        void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,
936e1051a39Sopenharmony_ci                                 const void *table, const BN_ULONG *np,
937e1051a39Sopenharmony_ci                                 const BN_ULONG *n0, int num, int power);
938e1051a39Sopenharmony_ci        void bn_scatter5(const BN_ULONG *inp, size_t num,
939e1051a39Sopenharmony_ci                         void *table, size_t power);
940e1051a39Sopenharmony_ci        void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);
941e1051a39Sopenharmony_ci        void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,
942e1051a39Sopenharmony_ci                       const void *table, const BN_ULONG *np,
943e1051a39Sopenharmony_ci                       const BN_ULONG *n0, int num, int power);
944e1051a39Sopenharmony_ci        int bn_get_bits5(const BN_ULONG *ap, int off);
945e1051a39Sopenharmony_ci
946e1051a39Sopenharmony_ci        BN_ULONG *n0 = mont->n0, *np;
947e1051a39Sopenharmony_ci
948e1051a39Sopenharmony_ci        /*
949e1051a39Sopenharmony_ci         * BN_to_montgomery can contaminate words above .top [in
950e1051a39Sopenharmony_ci         * BN_DEBUG build...
951e1051a39Sopenharmony_ci         */
952e1051a39Sopenharmony_ci        for (i = am.top; i < top; i++)
953e1051a39Sopenharmony_ci            am.d[i] = 0;
954e1051a39Sopenharmony_ci        for (i = tmp.top; i < top; i++)
955e1051a39Sopenharmony_ci            tmp.d[i] = 0;
956e1051a39Sopenharmony_ci
957e1051a39Sopenharmony_ci        /*
958e1051a39Sopenharmony_ci         * copy mont->N.d[] to improve cache locality
959e1051a39Sopenharmony_ci         */
960e1051a39Sopenharmony_ci        for (np = am.d + top, i = 0; i < top; i++)
961e1051a39Sopenharmony_ci            np[i] = mont->N.d[i];
962e1051a39Sopenharmony_ci
963e1051a39Sopenharmony_ci        bn_scatter5(tmp.d, top, powerbuf, 0);
964e1051a39Sopenharmony_ci        bn_scatter5(am.d, am.top, powerbuf, 1);
965e1051a39Sopenharmony_ci        bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);
966e1051a39Sopenharmony_ci        bn_scatter5(tmp.d, top, powerbuf, 2);
967e1051a39Sopenharmony_ci
968e1051a39Sopenharmony_ci# if 0
969e1051a39Sopenharmony_ci        for (i = 3; i < 32; i++) {
970e1051a39Sopenharmony_ci            /* Calculate a^i = a^(i-1) * a */
971e1051a39Sopenharmony_ci            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
972e1051a39Sopenharmony_ci            bn_scatter5(tmp.d, top, powerbuf, i);
973e1051a39Sopenharmony_ci        }
974e1051a39Sopenharmony_ci# else
975e1051a39Sopenharmony_ci        /* same as above, but uses squaring for 1/2 of operations */
976e1051a39Sopenharmony_ci        for (i = 4; i < 32; i *= 2) {
977e1051a39Sopenharmony_ci            bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
978e1051a39Sopenharmony_ci            bn_scatter5(tmp.d, top, powerbuf, i);
979e1051a39Sopenharmony_ci        }
980e1051a39Sopenharmony_ci        for (i = 3; i < 8; i += 2) {
981e1051a39Sopenharmony_ci            int j;
982e1051a39Sopenharmony_ci            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
983e1051a39Sopenharmony_ci            bn_scatter5(tmp.d, top, powerbuf, i);
984e1051a39Sopenharmony_ci            for (j = 2 * i; j < 32; j *= 2) {
985e1051a39Sopenharmony_ci                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
986e1051a39Sopenharmony_ci                bn_scatter5(tmp.d, top, powerbuf, j);
987e1051a39Sopenharmony_ci            }
988e1051a39Sopenharmony_ci        }
989e1051a39Sopenharmony_ci        for (; i < 16; i += 2) {
990e1051a39Sopenharmony_ci            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
991e1051a39Sopenharmony_ci            bn_scatter5(tmp.d, top, powerbuf, i);
992e1051a39Sopenharmony_ci            bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
993e1051a39Sopenharmony_ci            bn_scatter5(tmp.d, top, powerbuf, 2 * i);
994e1051a39Sopenharmony_ci        }
995e1051a39Sopenharmony_ci        for (; i < 32; i += 2) {
996e1051a39Sopenharmony_ci            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
997e1051a39Sopenharmony_ci            bn_scatter5(tmp.d, top, powerbuf, i);
998e1051a39Sopenharmony_ci        }
999e1051a39Sopenharmony_ci# endif
1000e1051a39Sopenharmony_ci        /*
1001e1051a39Sopenharmony_ci         * The exponent may not have a whole number of fixed-size windows.
1002e1051a39Sopenharmony_ci         * To simplify the main loop, the initial window has between 1 and
1003e1051a39Sopenharmony_ci         * full-window-size bits such that what remains is always a whole
1004e1051a39Sopenharmony_ci         * number of windows
1005e1051a39Sopenharmony_ci         */
1006e1051a39Sopenharmony_ci        window0 = (bits - 1) % 5 + 1;
1007e1051a39Sopenharmony_ci        wmask = (1 << window0) - 1;
1008e1051a39Sopenharmony_ci        bits -= window0;
1009e1051a39Sopenharmony_ci        wvalue = bn_get_bits(p, bits) & wmask;
1010e1051a39Sopenharmony_ci        bn_gather5(tmp.d, top, powerbuf, wvalue);
1011e1051a39Sopenharmony_ci
1012e1051a39Sopenharmony_ci        /*
1013e1051a39Sopenharmony_ci         * Scan the exponent one window at a time starting from the most
1014e1051a39Sopenharmony_ci         * significant bits.
1015e1051a39Sopenharmony_ci         */
1016e1051a39Sopenharmony_ci        if (top & 7) {
1017e1051a39Sopenharmony_ci            while (bits > 0) {
1018e1051a39Sopenharmony_ci                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
1019e1051a39Sopenharmony_ci                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
1020e1051a39Sopenharmony_ci                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
1021e1051a39Sopenharmony_ci                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
1022e1051a39Sopenharmony_ci                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
1023e1051a39Sopenharmony_ci                bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,
1024e1051a39Sopenharmony_ci                                    bn_get_bits5(p->d, bits -= 5));
1025e1051a39Sopenharmony_ci            }
1026e1051a39Sopenharmony_ci        } else {
1027e1051a39Sopenharmony_ci            while (bits > 0) {
1028e1051a39Sopenharmony_ci                bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,
1029e1051a39Sopenharmony_ci                          bn_get_bits5(p->d, bits -= 5));
1030e1051a39Sopenharmony_ci            }
1031e1051a39Sopenharmony_ci        }
1032e1051a39Sopenharmony_ci
1033e1051a39Sopenharmony_ci        tmp.top = top;
1034e1051a39Sopenharmony_ci        /*
1035e1051a39Sopenharmony_ci         * The result is now in |tmp| in Montgomery form, but it may not be
1036e1051a39Sopenharmony_ci         * fully reduced. This is within bounds for |BN_from_montgomery|
1037e1051a39Sopenharmony_ci         * (tmp < R <= m*R) so it will, when converting from Montgomery form,
1038e1051a39Sopenharmony_ci         * produce a fully reduced result.
1039e1051a39Sopenharmony_ci         *
1040e1051a39Sopenharmony_ci         * This differs from Figure 2 of the paper, which uses AMM(h, 1) to
1041e1051a39Sopenharmony_ci         * convert from Montgomery form with unreduced output, followed by an
1042e1051a39Sopenharmony_ci         * extra reduction step. In the paper's terminology, we replace
1043e1051a39Sopenharmony_ci         * steps 9 and 10 with MM(h, 1).
1044e1051a39Sopenharmony_ci         */
1045e1051a39Sopenharmony_ci    } else
1046e1051a39Sopenharmony_ci#endif
1047e1051a39Sopenharmony_ci    {
1048e1051a39Sopenharmony_ci fallback:
1049e1051a39Sopenharmony_ci        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))
1050e1051a39Sopenharmony_ci            goto err;
1051e1051a39Sopenharmony_ci        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))
1052e1051a39Sopenharmony_ci            goto err;
1053e1051a39Sopenharmony_ci
1054e1051a39Sopenharmony_ci        /*
1055e1051a39Sopenharmony_ci         * If the window size is greater than 1, then calculate
1056e1051a39Sopenharmony_ci         * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even
1057e1051a39Sopenharmony_ci         * powers could instead be computed as (a^(i/2))^2 to use the slight
1058e1051a39Sopenharmony_ci         * performance advantage of sqr over mul).
1059e1051a39Sopenharmony_ci         */
1060e1051a39Sopenharmony_ci        if (window > 1) {
1061e1051a39Sopenharmony_ci            if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))
1062e1051a39Sopenharmony_ci                goto err;
1063e1051a39Sopenharmony_ci            if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,
1064e1051a39Sopenharmony_ci                                              window))
1065e1051a39Sopenharmony_ci                goto err;
1066e1051a39Sopenharmony_ci            for (i = 3; i < numPowers; i++) {
1067e1051a39Sopenharmony_ci                /* Calculate a^i = a^(i-1) * a */
1068e1051a39Sopenharmony_ci                if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))
1069e1051a39Sopenharmony_ci                    goto err;
1070e1051a39Sopenharmony_ci                if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,
1071e1051a39Sopenharmony_ci                                                  window))
1072e1051a39Sopenharmony_ci                    goto err;
1073e1051a39Sopenharmony_ci            }
1074e1051a39Sopenharmony_ci        }
1075e1051a39Sopenharmony_ci
1076e1051a39Sopenharmony_ci        /*
1077e1051a39Sopenharmony_ci         * The exponent may not have a whole number of fixed-size windows.
1078e1051a39Sopenharmony_ci         * To simplify the main loop, the initial window has between 1 and
1079e1051a39Sopenharmony_ci         * full-window-size bits such that what remains is always a whole
1080e1051a39Sopenharmony_ci         * number of windows
1081e1051a39Sopenharmony_ci         */
1082e1051a39Sopenharmony_ci        window0 = (bits - 1) % window + 1;
1083e1051a39Sopenharmony_ci        wmask = (1 << window0) - 1;
1084e1051a39Sopenharmony_ci        bits -= window0;
1085e1051a39Sopenharmony_ci        wvalue = bn_get_bits(p, bits) & wmask;
1086e1051a39Sopenharmony_ci        if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,
1087e1051a39Sopenharmony_ci                                            window))
1088e1051a39Sopenharmony_ci            goto err;
1089e1051a39Sopenharmony_ci
1090e1051a39Sopenharmony_ci        wmask = (1 << window) - 1;
1091e1051a39Sopenharmony_ci        /*
1092e1051a39Sopenharmony_ci         * Scan the exponent one window at a time starting from the most
1093e1051a39Sopenharmony_ci         * significant bits.
1094e1051a39Sopenharmony_ci         */
1095e1051a39Sopenharmony_ci        while (bits > 0) {
1096e1051a39Sopenharmony_ci
1097e1051a39Sopenharmony_ci            /* Square the result window-size times */
1098e1051a39Sopenharmony_ci            for (i = 0; i < window; i++)
1099e1051a39Sopenharmony_ci                if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))
1100e1051a39Sopenharmony_ci                    goto err;
1101e1051a39Sopenharmony_ci
1102e1051a39Sopenharmony_ci            /*
1103e1051a39Sopenharmony_ci             * Get a window's worth of bits from the exponent
1104e1051a39Sopenharmony_ci             * This avoids calling BN_is_bit_set for each bit, which
1105e1051a39Sopenharmony_ci             * is not only slower but also makes each bit vulnerable to
1106e1051a39Sopenharmony_ci             * EM (and likely other) side-channel attacks like One&Done
1107e1051a39Sopenharmony_ci             * (for details see "One&Done: A Single-Decryption EM-Based
1108e1051a39Sopenharmony_ci             *  Attack on OpenSSL's Constant-Time Blinded RSA" by M. Alam,
1109e1051a39Sopenharmony_ci             *  H. Khan, M. Dey, N. Sinha, R. Callan, A. Zajic, and
1110e1051a39Sopenharmony_ci             *  M. Prvulovic, in USENIX Security'18)
1111e1051a39Sopenharmony_ci             */
1112e1051a39Sopenharmony_ci            bits -= window;
1113e1051a39Sopenharmony_ci            wvalue = bn_get_bits(p, bits) & wmask;
1114e1051a39Sopenharmony_ci            /*
1115e1051a39Sopenharmony_ci             * Fetch the appropriate pre-computed value from the pre-buf
1116e1051a39Sopenharmony_ci             */
1117e1051a39Sopenharmony_ci            if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,
1118e1051a39Sopenharmony_ci                                                window))
1119e1051a39Sopenharmony_ci                goto err;
1120e1051a39Sopenharmony_ci
1121e1051a39Sopenharmony_ci            /* Multiply the result into the intermediate result */
1122e1051a39Sopenharmony_ci            if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))
1123e1051a39Sopenharmony_ci                goto err;
1124e1051a39Sopenharmony_ci        }
1125e1051a39Sopenharmony_ci    }
1126e1051a39Sopenharmony_ci
1127e1051a39Sopenharmony_ci    /*
1128e1051a39Sopenharmony_ci     * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery
1129e1051a39Sopenharmony_ci     * removes padding [if any] and makes return value suitable for public
1130e1051a39Sopenharmony_ci     * API consumer.
1131e1051a39Sopenharmony_ci     */
1132e1051a39Sopenharmony_ci#if defined(SPARC_T4_MONT)
1133e1051a39Sopenharmony_ci    if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
1134e1051a39Sopenharmony_ci        am.d[0] = 1;            /* borrow am */
1135e1051a39Sopenharmony_ci        for (i = 1; i < top; i++)
1136e1051a39Sopenharmony_ci            am.d[i] = 0;
1137e1051a39Sopenharmony_ci        if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))
1138e1051a39Sopenharmony_ci            goto err;
1139e1051a39Sopenharmony_ci    } else
1140e1051a39Sopenharmony_ci#endif
1141e1051a39Sopenharmony_ci    if (!BN_from_montgomery(rr, &tmp, mont, ctx))
1142e1051a39Sopenharmony_ci        goto err;
1143e1051a39Sopenharmony_ci    ret = 1;
1144e1051a39Sopenharmony_ci err:
1145e1051a39Sopenharmony_ci    if (in_mont == NULL)
1146e1051a39Sopenharmony_ci        BN_MONT_CTX_free(mont);
1147e1051a39Sopenharmony_ci    if (powerbuf != NULL) {
1148e1051a39Sopenharmony_ci        OPENSSL_cleanse(powerbuf, powerbufLen);
1149e1051a39Sopenharmony_ci        OPENSSL_free(powerbufFree);
1150e1051a39Sopenharmony_ci    }
1151e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
1152e1051a39Sopenharmony_ci    return ret;
1153e1051a39Sopenharmony_ci}
1154e1051a39Sopenharmony_ci
1155e1051a39Sopenharmony_ciint BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
1156e1051a39Sopenharmony_ci                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1157e1051a39Sopenharmony_ci{
1158e1051a39Sopenharmony_ci    BN_MONT_CTX *mont = NULL;
1159e1051a39Sopenharmony_ci    int b, bits, ret = 0;
1160e1051a39Sopenharmony_ci    int r_is_one;
1161e1051a39Sopenharmony_ci    BN_ULONG w, next_w;
1162e1051a39Sopenharmony_ci    BIGNUM *r, *t;
1163e1051a39Sopenharmony_ci    BIGNUM *swap_tmp;
1164e1051a39Sopenharmony_ci#define BN_MOD_MUL_WORD(r, w, m) \
1165e1051a39Sopenharmony_ci                (BN_mul_word(r, (w)) && \
1166e1051a39Sopenharmony_ci                (/* BN_ucmp(r, (m)) < 0 ? 1 :*/  \
1167e1051a39Sopenharmony_ci                        (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
1168e1051a39Sopenharmony_ci    /*
1169e1051a39Sopenharmony_ci     * BN_MOD_MUL_WORD is only used with 'w' large, so the BN_ucmp test is
1170e1051a39Sopenharmony_ci     * probably more overhead than always using BN_mod (which uses BN_copy if
1171e1051a39Sopenharmony_ci     * a similar test returns true).
1172e1051a39Sopenharmony_ci     */
1173e1051a39Sopenharmony_ci    /*
1174e1051a39Sopenharmony_ci     * We can use BN_mod and do not need BN_nnmod because our accumulator is
1175e1051a39Sopenharmony_ci     * never negative (the result of BN_mod does not depend on the sign of
1176e1051a39Sopenharmony_ci     * the modulus).
1177e1051a39Sopenharmony_ci     */
1178e1051a39Sopenharmony_ci#define BN_TO_MONTGOMERY_WORD(r, w, mont) \
1179e1051a39Sopenharmony_ci                (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
1180e1051a39Sopenharmony_ci
1181e1051a39Sopenharmony_ci    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
1182e1051a39Sopenharmony_ci            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {
1183e1051a39Sopenharmony_ci        /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
1184e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1185e1051a39Sopenharmony_ci        return 0;
1186e1051a39Sopenharmony_ci    }
1187e1051a39Sopenharmony_ci
1188e1051a39Sopenharmony_ci    bn_check_top(p);
1189e1051a39Sopenharmony_ci    bn_check_top(m);
1190e1051a39Sopenharmony_ci
1191e1051a39Sopenharmony_ci    if (!BN_is_odd(m)) {
1192e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
1193e1051a39Sopenharmony_ci        return 0;
1194e1051a39Sopenharmony_ci    }
1195e1051a39Sopenharmony_ci    if (m->top == 1)
1196e1051a39Sopenharmony_ci        a %= m->d[0];           /* make sure that 'a' is reduced */
1197e1051a39Sopenharmony_ci
1198e1051a39Sopenharmony_ci    bits = BN_num_bits(p);
1199e1051a39Sopenharmony_ci    if (bits == 0) {
1200e1051a39Sopenharmony_ci        /* x**0 mod 1, or x**0 mod -1 is still zero. */
1201e1051a39Sopenharmony_ci        if (BN_abs_is_word(m, 1)) {
1202e1051a39Sopenharmony_ci            ret = 1;
1203e1051a39Sopenharmony_ci            BN_zero(rr);
1204e1051a39Sopenharmony_ci        } else {
1205e1051a39Sopenharmony_ci            ret = BN_one(rr);
1206e1051a39Sopenharmony_ci        }
1207e1051a39Sopenharmony_ci        return ret;
1208e1051a39Sopenharmony_ci    }
1209e1051a39Sopenharmony_ci    if (a == 0) {
1210e1051a39Sopenharmony_ci        BN_zero(rr);
1211e1051a39Sopenharmony_ci        ret = 1;
1212e1051a39Sopenharmony_ci        return ret;
1213e1051a39Sopenharmony_ci    }
1214e1051a39Sopenharmony_ci
1215e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
1216e1051a39Sopenharmony_ci    r = BN_CTX_get(ctx);
1217e1051a39Sopenharmony_ci    t = BN_CTX_get(ctx);
1218e1051a39Sopenharmony_ci    if (t == NULL)
1219e1051a39Sopenharmony_ci        goto err;
1220e1051a39Sopenharmony_ci
1221e1051a39Sopenharmony_ci    if (in_mont != NULL)
1222e1051a39Sopenharmony_ci        mont = in_mont;
1223e1051a39Sopenharmony_ci    else {
1224e1051a39Sopenharmony_ci        if ((mont = BN_MONT_CTX_new()) == NULL)
1225e1051a39Sopenharmony_ci            goto err;
1226e1051a39Sopenharmony_ci        if (!BN_MONT_CTX_set(mont, m, ctx))
1227e1051a39Sopenharmony_ci            goto err;
1228e1051a39Sopenharmony_ci    }
1229e1051a39Sopenharmony_ci
1230e1051a39Sopenharmony_ci    r_is_one = 1;               /* except for Montgomery factor */
1231e1051a39Sopenharmony_ci
1232e1051a39Sopenharmony_ci    /* bits-1 >= 0 */
1233e1051a39Sopenharmony_ci
1234e1051a39Sopenharmony_ci    /* The result is accumulated in the product r*w. */
1235e1051a39Sopenharmony_ci    w = a;                      /* bit 'bits-1' of 'p' is always set */
1236e1051a39Sopenharmony_ci    for (b = bits - 2; b >= 0; b--) {
1237e1051a39Sopenharmony_ci        /* First, square r*w. */
1238e1051a39Sopenharmony_ci        next_w = w * w;
1239e1051a39Sopenharmony_ci        if ((next_w / w) != w) { /* overflow */
1240e1051a39Sopenharmony_ci            if (r_is_one) {
1241e1051a39Sopenharmony_ci                if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
1242e1051a39Sopenharmony_ci                    goto err;
1243e1051a39Sopenharmony_ci                r_is_one = 0;
1244e1051a39Sopenharmony_ci            } else {
1245e1051a39Sopenharmony_ci                if (!BN_MOD_MUL_WORD(r, w, m))
1246e1051a39Sopenharmony_ci                    goto err;
1247e1051a39Sopenharmony_ci            }
1248e1051a39Sopenharmony_ci            next_w = 1;
1249e1051a39Sopenharmony_ci        }
1250e1051a39Sopenharmony_ci        w = next_w;
1251e1051a39Sopenharmony_ci        if (!r_is_one) {
1252e1051a39Sopenharmony_ci            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
1253e1051a39Sopenharmony_ci                goto err;
1254e1051a39Sopenharmony_ci        }
1255e1051a39Sopenharmony_ci
1256e1051a39Sopenharmony_ci        /* Second, multiply r*w by 'a' if exponent bit is set. */
1257e1051a39Sopenharmony_ci        if (BN_is_bit_set(p, b)) {
1258e1051a39Sopenharmony_ci            next_w = w * a;
1259e1051a39Sopenharmony_ci            if ((next_w / a) != w) { /* overflow */
1260e1051a39Sopenharmony_ci                if (r_is_one) {
1261e1051a39Sopenharmony_ci                    if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
1262e1051a39Sopenharmony_ci                        goto err;
1263e1051a39Sopenharmony_ci                    r_is_one = 0;
1264e1051a39Sopenharmony_ci                } else {
1265e1051a39Sopenharmony_ci                    if (!BN_MOD_MUL_WORD(r, w, m))
1266e1051a39Sopenharmony_ci                        goto err;
1267e1051a39Sopenharmony_ci                }
1268e1051a39Sopenharmony_ci                next_w = a;
1269e1051a39Sopenharmony_ci            }
1270e1051a39Sopenharmony_ci            w = next_w;
1271e1051a39Sopenharmony_ci        }
1272e1051a39Sopenharmony_ci    }
1273e1051a39Sopenharmony_ci
1274e1051a39Sopenharmony_ci    /* Finally, set r:=r*w. */
1275e1051a39Sopenharmony_ci    if (w != 1) {
1276e1051a39Sopenharmony_ci        if (r_is_one) {
1277e1051a39Sopenharmony_ci            if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
1278e1051a39Sopenharmony_ci                goto err;
1279e1051a39Sopenharmony_ci            r_is_one = 0;
1280e1051a39Sopenharmony_ci        } else {
1281e1051a39Sopenharmony_ci            if (!BN_MOD_MUL_WORD(r, w, m))
1282e1051a39Sopenharmony_ci                goto err;
1283e1051a39Sopenharmony_ci        }
1284e1051a39Sopenharmony_ci    }
1285e1051a39Sopenharmony_ci
1286e1051a39Sopenharmony_ci    if (r_is_one) {             /* can happen only if a == 1 */
1287e1051a39Sopenharmony_ci        if (!BN_one(rr))
1288e1051a39Sopenharmony_ci            goto err;
1289e1051a39Sopenharmony_ci    } else {
1290e1051a39Sopenharmony_ci        if (!BN_from_montgomery(rr, r, mont, ctx))
1291e1051a39Sopenharmony_ci            goto err;
1292e1051a39Sopenharmony_ci    }
1293e1051a39Sopenharmony_ci    ret = 1;
1294e1051a39Sopenharmony_ci err:
1295e1051a39Sopenharmony_ci    if (in_mont == NULL)
1296e1051a39Sopenharmony_ci        BN_MONT_CTX_free(mont);
1297e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
1298e1051a39Sopenharmony_ci    bn_check_top(rr);
1299e1051a39Sopenharmony_ci    return ret;
1300e1051a39Sopenharmony_ci}
1301e1051a39Sopenharmony_ci
1302e1051a39Sopenharmony_ci/* The old fallback, simple version :-) */
1303e1051a39Sopenharmony_ciint BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1304e1051a39Sopenharmony_ci                      const BIGNUM *m, BN_CTX *ctx)
1305e1051a39Sopenharmony_ci{
1306e1051a39Sopenharmony_ci    int i, j, bits, ret = 0, wstart, wend, window, wvalue;
1307e1051a39Sopenharmony_ci    int start = 1;
1308e1051a39Sopenharmony_ci    BIGNUM *d;
1309e1051a39Sopenharmony_ci    /* Table of variables obtained from 'ctx' */
1310e1051a39Sopenharmony_ci    BIGNUM *val[TABLE_SIZE];
1311e1051a39Sopenharmony_ci
1312e1051a39Sopenharmony_ci    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
1313e1051a39Sopenharmony_ci            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0
1314e1051a39Sopenharmony_ci            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {
1315e1051a39Sopenharmony_ci        /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
1316e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1317e1051a39Sopenharmony_ci        return 0;
1318e1051a39Sopenharmony_ci    }
1319e1051a39Sopenharmony_ci
1320e1051a39Sopenharmony_ci    bits = BN_num_bits(p);
1321e1051a39Sopenharmony_ci    if (bits == 0) {
1322e1051a39Sopenharmony_ci        /* x**0 mod 1, or x**0 mod -1 is still zero. */
1323e1051a39Sopenharmony_ci        if (BN_abs_is_word(m, 1)) {
1324e1051a39Sopenharmony_ci            ret = 1;
1325e1051a39Sopenharmony_ci            BN_zero(r);
1326e1051a39Sopenharmony_ci        } else {
1327e1051a39Sopenharmony_ci            ret = BN_one(r);
1328e1051a39Sopenharmony_ci        }
1329e1051a39Sopenharmony_ci        return ret;
1330e1051a39Sopenharmony_ci    }
1331e1051a39Sopenharmony_ci
1332e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
1333e1051a39Sopenharmony_ci    d = BN_CTX_get(ctx);
1334e1051a39Sopenharmony_ci    val[0] = BN_CTX_get(ctx);
1335e1051a39Sopenharmony_ci    if (val[0] == NULL)
1336e1051a39Sopenharmony_ci        goto err;
1337e1051a39Sopenharmony_ci
1338e1051a39Sopenharmony_ci    if (!BN_nnmod(val[0], a, m, ctx))
1339e1051a39Sopenharmony_ci        goto err;               /* 1 */
1340e1051a39Sopenharmony_ci    if (BN_is_zero(val[0])) {
1341e1051a39Sopenharmony_ci        BN_zero(r);
1342e1051a39Sopenharmony_ci        ret = 1;
1343e1051a39Sopenharmony_ci        goto err;
1344e1051a39Sopenharmony_ci    }
1345e1051a39Sopenharmony_ci
1346e1051a39Sopenharmony_ci    window = BN_window_bits_for_exponent_size(bits);
1347e1051a39Sopenharmony_ci    if (window > 1) {
1348e1051a39Sopenharmony_ci        if (!BN_mod_mul(d, val[0], val[0], m, ctx))
1349e1051a39Sopenharmony_ci            goto err;           /* 2 */
1350e1051a39Sopenharmony_ci        j = 1 << (window - 1);
1351e1051a39Sopenharmony_ci        for (i = 1; i < j; i++) {
1352e1051a39Sopenharmony_ci            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
1353e1051a39Sopenharmony_ci                !BN_mod_mul(val[i], val[i - 1], d, m, ctx))
1354e1051a39Sopenharmony_ci                goto err;
1355e1051a39Sopenharmony_ci        }
1356e1051a39Sopenharmony_ci    }
1357e1051a39Sopenharmony_ci
1358e1051a39Sopenharmony_ci    start = 1;                  /* This is used to avoid multiplication etc
1359e1051a39Sopenharmony_ci                                 * when there is only the value '1' in the
1360e1051a39Sopenharmony_ci                                 * buffer. */
1361e1051a39Sopenharmony_ci    wvalue = 0;                 /* The 'value' of the window */
1362e1051a39Sopenharmony_ci    wstart = bits - 1;          /* The top bit of the window */
1363e1051a39Sopenharmony_ci    wend = 0;                   /* The bottom bit of the window */
1364e1051a39Sopenharmony_ci
1365e1051a39Sopenharmony_ci    if (!BN_one(r))
1366e1051a39Sopenharmony_ci        goto err;
1367e1051a39Sopenharmony_ci
1368e1051a39Sopenharmony_ci    for (;;) {
1369e1051a39Sopenharmony_ci        if (BN_is_bit_set(p, wstart) == 0) {
1370e1051a39Sopenharmony_ci            if (!start)
1371e1051a39Sopenharmony_ci                if (!BN_mod_mul(r, r, r, m, ctx))
1372e1051a39Sopenharmony_ci                    goto err;
1373e1051a39Sopenharmony_ci            if (wstart == 0)
1374e1051a39Sopenharmony_ci                break;
1375e1051a39Sopenharmony_ci            wstart--;
1376e1051a39Sopenharmony_ci            continue;
1377e1051a39Sopenharmony_ci        }
1378e1051a39Sopenharmony_ci        /*
1379e1051a39Sopenharmony_ci         * We now have wstart on a 'set' bit, we now need to work out how bit
1380e1051a39Sopenharmony_ci         * a window to do.  To do this we need to scan forward until the last
1381e1051a39Sopenharmony_ci         * set bit before the end of the window
1382e1051a39Sopenharmony_ci         */
1383e1051a39Sopenharmony_ci        wvalue = 1;
1384e1051a39Sopenharmony_ci        wend = 0;
1385e1051a39Sopenharmony_ci        for (i = 1; i < window; i++) {
1386e1051a39Sopenharmony_ci            if (wstart - i < 0)
1387e1051a39Sopenharmony_ci                break;
1388e1051a39Sopenharmony_ci            if (BN_is_bit_set(p, wstart - i)) {
1389e1051a39Sopenharmony_ci                wvalue <<= (i - wend);
1390e1051a39Sopenharmony_ci                wvalue |= 1;
1391e1051a39Sopenharmony_ci                wend = i;
1392e1051a39Sopenharmony_ci            }
1393e1051a39Sopenharmony_ci        }
1394e1051a39Sopenharmony_ci
1395e1051a39Sopenharmony_ci        /* wend is the size of the current window */
1396e1051a39Sopenharmony_ci        j = wend + 1;
1397e1051a39Sopenharmony_ci        /* add the 'bytes above' */
1398e1051a39Sopenharmony_ci        if (!start)
1399e1051a39Sopenharmony_ci            for (i = 0; i < j; i++) {
1400e1051a39Sopenharmony_ci                if (!BN_mod_mul(r, r, r, m, ctx))
1401e1051a39Sopenharmony_ci                    goto err;
1402e1051a39Sopenharmony_ci            }
1403e1051a39Sopenharmony_ci
1404e1051a39Sopenharmony_ci        /* wvalue will be an odd number < 2^window */
1405e1051a39Sopenharmony_ci        if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))
1406e1051a39Sopenharmony_ci            goto err;
1407e1051a39Sopenharmony_ci
1408e1051a39Sopenharmony_ci        /* move the 'window' down further */
1409e1051a39Sopenharmony_ci        wstart -= wend + 1;
1410e1051a39Sopenharmony_ci        wvalue = 0;
1411e1051a39Sopenharmony_ci        start = 0;
1412e1051a39Sopenharmony_ci        if (wstart < 0)
1413e1051a39Sopenharmony_ci            break;
1414e1051a39Sopenharmony_ci    }
1415e1051a39Sopenharmony_ci    ret = 1;
1416e1051a39Sopenharmony_ci err:
1417e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
1418e1051a39Sopenharmony_ci    bn_check_top(r);
1419e1051a39Sopenharmony_ci    return ret;
1420e1051a39Sopenharmony_ci}
1421e1051a39Sopenharmony_ci
1422e1051a39Sopenharmony_ci/*
1423e1051a39Sopenharmony_ci * This is a variant of modular exponentiation optimization that does
1424e1051a39Sopenharmony_ci * parallel 2-primes exponentiation using 256-bit (AVX512VL) AVX512_IFMA ISA
1425e1051a39Sopenharmony_ci * in 52-bit binary redundant representation.
1426e1051a39Sopenharmony_ci * If such instructions are not available, or input data size is not supported,
1427e1051a39Sopenharmony_ci * it falls back to two BN_mod_exp_mont_consttime() calls.
1428e1051a39Sopenharmony_ci */
1429e1051a39Sopenharmony_ciint BN_mod_exp_mont_consttime_x2(BIGNUM *rr1, const BIGNUM *a1, const BIGNUM *p1,
1430e1051a39Sopenharmony_ci                                 const BIGNUM *m1, BN_MONT_CTX *in_mont1,
1431e1051a39Sopenharmony_ci                                 BIGNUM *rr2, const BIGNUM *a2, const BIGNUM *p2,
1432e1051a39Sopenharmony_ci                                 const BIGNUM *m2, BN_MONT_CTX *in_mont2,
1433e1051a39Sopenharmony_ci                                 BN_CTX *ctx)
1434e1051a39Sopenharmony_ci{
1435e1051a39Sopenharmony_ci    int ret = 0;
1436e1051a39Sopenharmony_ci
1437e1051a39Sopenharmony_ci#ifdef RSAZ_ENABLED
1438e1051a39Sopenharmony_ci    BN_MONT_CTX *mont1 = NULL;
1439e1051a39Sopenharmony_ci    BN_MONT_CTX *mont2 = NULL;
1440e1051a39Sopenharmony_ci
1441e1051a39Sopenharmony_ci    if (ossl_rsaz_avx512ifma_eligible() &&
1442e1051a39Sopenharmony_ci        ((a1->top == 16) && (p1->top == 16) && (BN_num_bits(m1) == 1024) &&
1443e1051a39Sopenharmony_ci         (a2->top == 16) && (p2->top == 16) && (BN_num_bits(m2) == 1024))) {
1444e1051a39Sopenharmony_ci
1445e1051a39Sopenharmony_ci        if (bn_wexpand(rr1, 16) == NULL)
1446e1051a39Sopenharmony_ci            goto err;
1447e1051a39Sopenharmony_ci        if (bn_wexpand(rr2, 16) == NULL)
1448e1051a39Sopenharmony_ci            goto err;
1449e1051a39Sopenharmony_ci
1450e1051a39Sopenharmony_ci        /*  Ensure that montgomery contexts are initialized */
1451e1051a39Sopenharmony_ci        if (in_mont1 != NULL) {
1452e1051a39Sopenharmony_ci            mont1 = in_mont1;
1453e1051a39Sopenharmony_ci        } else {
1454e1051a39Sopenharmony_ci            if ((mont1 = BN_MONT_CTX_new()) == NULL)
1455e1051a39Sopenharmony_ci                goto err;
1456e1051a39Sopenharmony_ci            if (!BN_MONT_CTX_set(mont1, m1, ctx))
1457e1051a39Sopenharmony_ci                goto err;
1458e1051a39Sopenharmony_ci        }
1459e1051a39Sopenharmony_ci        if (in_mont2 != NULL) {
1460e1051a39Sopenharmony_ci            mont2 = in_mont2;
1461e1051a39Sopenharmony_ci        } else {
1462e1051a39Sopenharmony_ci            if ((mont2 = BN_MONT_CTX_new()) == NULL)
1463e1051a39Sopenharmony_ci                goto err;
1464e1051a39Sopenharmony_ci            if (!BN_MONT_CTX_set(mont2, m2, ctx))
1465e1051a39Sopenharmony_ci                goto err;
1466e1051a39Sopenharmony_ci        }
1467e1051a39Sopenharmony_ci
1468e1051a39Sopenharmony_ci        ret = ossl_rsaz_mod_exp_avx512_x2(rr1->d, a1->d, p1->d, m1->d,
1469e1051a39Sopenharmony_ci                                          mont1->RR.d, mont1->n0[0],
1470e1051a39Sopenharmony_ci                                          rr2->d, a2->d, p2->d, m2->d,
1471e1051a39Sopenharmony_ci                                          mont2->RR.d, mont2->n0[0],
1472e1051a39Sopenharmony_ci                                          1024 /* factor bit size */);
1473e1051a39Sopenharmony_ci
1474e1051a39Sopenharmony_ci        rr1->top = 16;
1475e1051a39Sopenharmony_ci        rr1->neg = 0;
1476e1051a39Sopenharmony_ci        bn_correct_top(rr1);
1477e1051a39Sopenharmony_ci        bn_check_top(rr1);
1478e1051a39Sopenharmony_ci
1479e1051a39Sopenharmony_ci        rr2->top = 16;
1480e1051a39Sopenharmony_ci        rr2->neg = 0;
1481e1051a39Sopenharmony_ci        bn_correct_top(rr2);
1482e1051a39Sopenharmony_ci        bn_check_top(rr2);
1483e1051a39Sopenharmony_ci
1484e1051a39Sopenharmony_ci        goto err;
1485e1051a39Sopenharmony_ci    }
1486e1051a39Sopenharmony_ci#endif
1487e1051a39Sopenharmony_ci
1488e1051a39Sopenharmony_ci    /* rr1 = a1^p1 mod m1 */
1489e1051a39Sopenharmony_ci    ret = BN_mod_exp_mont_consttime(rr1, a1, p1, m1, ctx, in_mont1);
1490e1051a39Sopenharmony_ci    /* rr2 = a2^p2 mod m2 */
1491e1051a39Sopenharmony_ci    ret &= BN_mod_exp_mont_consttime(rr2, a2, p2, m2, ctx, in_mont2);
1492e1051a39Sopenharmony_ci
1493e1051a39Sopenharmony_ci#ifdef RSAZ_ENABLED
1494e1051a39Sopenharmony_cierr:
1495e1051a39Sopenharmony_ci    if (in_mont2 == NULL)
1496e1051a39Sopenharmony_ci        BN_MONT_CTX_free(mont2);
1497e1051a39Sopenharmony_ci    if (in_mont1 == NULL)
1498e1051a39Sopenharmony_ci        BN_MONT_CTX_free(mont1);
1499e1051a39Sopenharmony_ci#endif
1500e1051a39Sopenharmony_ci
1501e1051a39Sopenharmony_ci    return ret;
1502e1051a39Sopenharmony_ci}
1503