• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <openssl/cpu.h>
20 
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_MIN_CACHE_LINE_WIDTH|
36 // bytes.
37 void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16], const BN_ULONG base_norm[16],
38                             const BN_ULONG exponent[16],
39                             const BN_ULONG m_norm[16], const BN_ULONG RR[16],
40                             BN_ULONG k0,
41                             BN_ULONG storage_words[MOD_EXP_CTIME_STORAGE_LEN]);
42 
rsaz_avx2_capable(void)43 OPENSSL_INLINE int rsaz_avx2_capable(void) {
44   const uint32_t *cap = OPENSSL_ia32cap_get();
45   return (cap[2] & (1 << 5)) != 0;  // AVX2
46 }
47 
rsaz_avx2_preferred(void)48 OPENSSL_INLINE int rsaz_avx2_preferred(void) {
49   const uint32_t *cap = OPENSSL_ia32cap_get();
50   static const uint32_t kBMI2AndADX = (1 << 8) | (1 << 19);
51   if ((cap[2] & kBMI2AndADX) == kBMI2AndADX) {
52     // If BMI2 and ADX are available, x86_64-mont5.pl is faster.
53     return 0;
54   }
55   return (cap[2] & (1 << 5)) != 0;  // AVX2
56 }
57 
58 
59 // Assembly functions.
60 
61 // RSAZ represents 1024-bit integers using unsaturated 29-bit limbs stored in
62 // 64-bit integers. This requires 36 limbs but padded up to 40.
63 //
64 // See crypto/bn/asm/rsaz-avx2.pl for further details.
65 
66 // rsaz_1024_norm2red_avx2 converts |norm| from |BIGNUM| to RSAZ representation
67 // and writes the result to |red|.
68 void rsaz_1024_norm2red_avx2(BN_ULONG red[40], const BN_ULONG norm[16]);
69 
70 // rsaz_1024_mul_avx2 computes |a| * |b| mod |n| and writes the result to |ret|.
71 // Inputs and outputs are in Montgomery form, using RSAZ's representation. |k|
72 // is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|.
73 void rsaz_1024_mul_avx2(BN_ULONG ret[40], const BN_ULONG a[40],
74                         const BN_ULONG b[40], const BN_ULONG n[40], BN_ULONG k);
75 
76 // rsaz_1024_mul_avx2 computes |a|^(2*|count|) mod |n| and writes the result to
77 // |ret|. Inputs and outputs are in Montgomery form, using RSAZ's
78 // representation. |k| is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|.
79 void rsaz_1024_sqr_avx2(BN_ULONG ret[40], const BN_ULONG a[40],
80                         const BN_ULONG n[40], BN_ULONG k, int count);
81 
82 // rsaz_1024_scatter5_avx2 stores |val| at index |i| of |tbl|. |i| must be
83 // positive and at most 31. Note the table only uses 18 |BN_ULONG|s per entry
84 // instead of 40. It packs two 29-bit limbs into each |BN_ULONG| and only stores
85 // 36 limbs rather than the padded 40.
86 void rsaz_1024_scatter5_avx2(BN_ULONG tbl[32 * 18], const BN_ULONG val[40],
87                              int i);
88 
89 // rsaz_1024_gather5_avx2 loads index |i| of |tbl| and writes it to |val|.
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|.
95 void rsaz_1024_red2norm_avx2(BN_ULONG norm[16], const BN_ULONG red[40]);
96 
97 
98 #endif  // !OPENSSL_NO_ASM && OPENSSL_X86_64
99 
100 #if defined(__cplusplus)
101 }  // extern "C"
102 #endif
103 
104 #endif  // OPENSSL_HEADER_BN_RSAZ_EXP_H
105