1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2013-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright (c) 2020, Intel Corporation. All Rights Reserved.
4e1051a39Sopenharmony_ci *
5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
6e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
9e1051a39Sopenharmony_ci *
10e1051a39Sopenharmony_ci * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
11e1051a39Sopenharmony_ci * (1) Intel Corporation, Israel Development Center, Haifa, Israel
12e1051a39Sopenharmony_ci * (2) University of Haifa, Israel
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci#ifndef OSSL_CRYPTO_BN_RSAZ_EXP_H
16e1051a39Sopenharmony_ci# define OSSL_CRYPTO_BN_RSAZ_EXP_H
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci# undef RSAZ_ENABLED
19e1051a39Sopenharmony_ci# if defined(OPENSSL_BN_ASM_MONT) && \
20e1051a39Sopenharmony_ci        (defined(__x86_64) || defined(__x86_64__) || \
21e1051a39Sopenharmony_ci         defined(_M_AMD64) || defined(_M_X64))
22e1051a39Sopenharmony_ci#  define RSAZ_ENABLED
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci#  include <openssl/bn.h>
25e1051a39Sopenharmony_ci#  include "internal/constant_time.h"
26e1051a39Sopenharmony_ci#  include "bn_local.h"
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_civoid RSAZ_1024_mod_exp_avx2(BN_ULONG result[16],
29e1051a39Sopenharmony_ci                            const BN_ULONG base_norm[16],
30e1051a39Sopenharmony_ci                            const BN_ULONG exponent[16],
31e1051a39Sopenharmony_ci                            const BN_ULONG m_norm[16], const BN_ULONG RR[16],
32e1051a39Sopenharmony_ci                            BN_ULONG k0);
33e1051a39Sopenharmony_ciint rsaz_avx2_eligible(void);
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_civoid RSAZ_512_mod_exp(BN_ULONG result[8],
36e1051a39Sopenharmony_ci                      const BN_ULONG base_norm[8], const BN_ULONG exponent[8],
37e1051a39Sopenharmony_ci                      const BN_ULONG m_norm[8], BN_ULONG k0,
38e1051a39Sopenharmony_ci                      const BN_ULONG RR[8]);
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ciint ossl_rsaz_avx512ifma_eligible(void);
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ciint ossl_rsaz_mod_exp_avx512_x2(BN_ULONG *res1,
44e1051a39Sopenharmony_ci                                const BN_ULONG *base1,
45e1051a39Sopenharmony_ci                                const BN_ULONG *exponent1,
46e1051a39Sopenharmony_ci                                const BN_ULONG *m1,
47e1051a39Sopenharmony_ci                                const BN_ULONG *RR1,
48e1051a39Sopenharmony_ci                                BN_ULONG k0_1,
49e1051a39Sopenharmony_ci                                BN_ULONG *res2,
50e1051a39Sopenharmony_ci                                const BN_ULONG *base2,
51e1051a39Sopenharmony_ci                                const BN_ULONG *exponent2,
52e1051a39Sopenharmony_ci                                const BN_ULONG *m2,
53e1051a39Sopenharmony_ci                                const BN_ULONG *RR2,
54e1051a39Sopenharmony_ci                                BN_ULONG k0_2,
55e1051a39Sopenharmony_ci                                int factor_size);
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_cistatic ossl_inline void bn_select_words(BN_ULONG *r, BN_ULONG mask,
58e1051a39Sopenharmony_ci                                        const BN_ULONG *a,
59e1051a39Sopenharmony_ci                                        const BN_ULONG *b, size_t num)
60e1051a39Sopenharmony_ci{
61e1051a39Sopenharmony_ci    size_t i;
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    for (i = 0; i < num; i++) {
64e1051a39Sopenharmony_ci        r[i] = constant_time_select_64(mask, a[i], b[i]);
65e1051a39Sopenharmony_ci    }
66e1051a39Sopenharmony_ci}
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_cistatic ossl_inline BN_ULONG bn_reduce_once_in_place(BN_ULONG *r,
69e1051a39Sopenharmony_ci                                                    BN_ULONG carry,
70e1051a39Sopenharmony_ci                                                    const BN_ULONG *m,
71e1051a39Sopenharmony_ci                                                    BN_ULONG *tmp, size_t num)
72e1051a39Sopenharmony_ci{
73e1051a39Sopenharmony_ci    carry -= bn_sub_words(tmp, r, m, num);
74e1051a39Sopenharmony_ci    bn_select_words(r, carry, r /* tmp < 0 */, tmp /* tmp >= 0 */, num);
75e1051a39Sopenharmony_ci    return carry;
76e1051a39Sopenharmony_ci}
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ci# endif
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci#endif
81