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