/src/boringssl/crypto/fipsmodule/bn/rsaz_exp.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2012, Intel Corporation. All Rights Reserved. |
4 | | * |
5 | | * Licensed under the OpenSSL license (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 OPENSSL_HEADER_BN_RSAZ_EXP_H |
16 | | #define OPENSSL_HEADER_BN_RSAZ_EXP_H |
17 | | |
18 | | #include <openssl/bn.h> |
19 | | |
20 | | #include "internal.h" |
21 | | #include "../../internal.h" |
22 | | |
23 | | #if defined(__cplusplus) |
24 | | extern "C" { |
25 | | #endif |
26 | | |
27 | | #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) |
28 | | #define RSAZ_ENABLED |
29 | | |
30 | | |
31 | | // RSAZ_1024_mod_exp_avx2 sets |result| to |base_norm| raised to |exponent| |
32 | | // modulo |m_norm|. |base_norm| must be fully-reduced and |exponent| must have |
33 | | // the high bit set (it is 1024 bits wide). |RR| and |k0| must be |RR| and |n0|, |
34 | | // respectively, extracted from |m_norm|'s |BN_MONT_CTX|. |storage_words| is a |
35 | | // temporary buffer that must be aligned to |MOD_EXP_CTIME_ALIGN| bytes. |
36 | | void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16], const BN_ULONG base_norm[16], |
37 | | const BN_ULONG exponent[16], |
38 | | const BN_ULONG m_norm[16], const BN_ULONG RR[16], |
39 | | BN_ULONG k0, |
40 | | BN_ULONG storage_words[MOD_EXP_CTIME_STORAGE_LEN]); |
41 | | |
42 | 0 | OPENSSL_INLINE int rsaz_avx2_capable(void) { |
43 | 0 | return CRYPTO_is_AVX2_capable(); |
44 | 0 | } |
45 | | |
46 | 0 | OPENSSL_INLINE int rsaz_avx2_preferred(void) { |
47 | 0 | if (CRYPTO_is_BMI1_capable() && CRYPTO_is_BMI2_capable() && |
48 | 0 | CRYPTO_is_ADX_capable()) { |
49 | | // If BMI1, BMI2, and ADX are available, x86_64-mont5.pl is faster. See the |
50 | | // .Lmulx4x_enter and .Lpowerx5_enter branches. |
51 | 0 | return 0; |
52 | 0 | } |
53 | 0 | return CRYPTO_is_AVX2_capable(); |
54 | 0 | } |
55 | | |
56 | | |
57 | | // Assembly functions. |
58 | | |
59 | | // RSAZ represents 1024-bit integers using unsaturated 29-bit limbs stored in |
60 | | // 64-bit integers. This requires 36 limbs but padded up to 40. |
61 | | // |
62 | | // See crypto/bn/asm/rsaz-avx2.pl for further details. |
63 | | |
64 | | // rsaz_1024_norm2red_avx2 converts |norm| from |BIGNUM| to RSAZ representation |
65 | | // and writes the result to |red|. |
66 | | void rsaz_1024_norm2red_avx2(BN_ULONG red[40], const BN_ULONG norm[16]); |
67 | | |
68 | | // rsaz_1024_mul_avx2 computes |a| * |b| mod |n| and writes the result to |ret|. |
69 | | // Inputs and outputs are in Montgomery form, using RSAZ's representation. |k| |
70 | | // is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|. |
71 | | void rsaz_1024_mul_avx2(BN_ULONG ret[40], const BN_ULONG a[40], |
72 | | const BN_ULONG b[40], const BN_ULONG n[40], BN_ULONG k); |
73 | | |
74 | | // rsaz_1024_mul_avx2 computes |a|^(2*|count|) mod |n| and writes the result to |
75 | | // |ret|. Inputs and outputs are in Montgomery form, using RSAZ's |
76 | | // representation. |k| is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|. |
77 | | void rsaz_1024_sqr_avx2(BN_ULONG ret[40], const BN_ULONG a[40], |
78 | | const BN_ULONG n[40], BN_ULONG k, int count); |
79 | | |
80 | | // rsaz_1024_scatter5_avx2 stores |val| at index |i| of |tbl|. |i| must be |
81 | | // positive and at most 31. It is treated as public. Note the table only uses 18 |
82 | | // |BN_ULONG|s per entry instead of 40. It packs two 29-bit limbs into each |
83 | | // |BN_ULONG| and only stores 36 limbs rather than the padded 40. |
84 | | void rsaz_1024_scatter5_avx2(BN_ULONG tbl[32 * 18], const BN_ULONG val[40], |
85 | | int i); |
86 | | |
87 | | // rsaz_1024_gather5_avx2 loads index |i| of |tbl| and writes it to |val|. |i| |
88 | | // must be positive and at most 31. It is treated as secret. |tbl| must be |
89 | | // aligned to 32 bytes. |
90 | | void rsaz_1024_gather5_avx2(BN_ULONG val[40], const BN_ULONG tbl[32 * 18], |
91 | | int i); |
92 | | |
93 | | // rsaz_1024_red2norm_avx2 converts |red| from RSAZ to |BIGNUM| representation |
94 | | // and writes the result to |norm|. The result will be <= the modulus. |
95 | | // |
96 | | // WARNING: The result of this operation may not be fully reduced. |norm| may be |
97 | | // the modulus instead of zero. This function should be followed by a call to |
98 | | // |bn_reduce_once|. |
99 | | void rsaz_1024_red2norm_avx2(BN_ULONG norm[16], const BN_ULONG red[40]); |
100 | | |
101 | | |
102 | | #endif // !OPENSSL_NO_ASM && OPENSSL_X86_64 |
103 | | |
104 | | #if defined(__cplusplus) |
105 | | } // extern "C" |
106 | | #endif |
107 | | |
108 | | #endif // OPENSSL_HEADER_BN_RSAZ_EXP_H |