Coverage Report

Created: 2019-09-11 14:12

/src/botan/src/lib/math/numbertheory/monty_exp.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Montgomery Exponentiation
3
* (C) 1999-2010,2012,2018 Jack Lloyd
4
*     2016 Matthias Gierlings
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#include <botan/internal/monty_exp.h>
10
#include <botan/internal/ct_utils.h>
11
#include <botan/internal/rounding.h>
12
#include <botan/numthry.h>
13
#include <botan/reducer.h>
14
#include <botan/monty.h>
15
16
namespace Botan {
17
18
class Montgomery_Exponentation_State
19
   {
20
   public:
21
      Montgomery_Exponentation_State(std::shared_ptr<const Montgomery_Params> params,
22
                                     const BigInt& g,
23
                                     size_t window_bits,
24
                                     bool const_time);
25
26
      BigInt exponentiation(const BigInt& k, size_t max_k_bits) const;
27
28
      BigInt exponentiation_vartime(const BigInt& k) const;
29
   private:
30
      std::shared_ptr<const Montgomery_Params> m_params;
31
      std::vector<Montgomery_Int> m_g;
32
      size_t m_window_bits;
33
      bool m_const_time;
34
   };
35
36
Montgomery_Exponentation_State::Montgomery_Exponentation_State(std::shared_ptr<const Montgomery_Params> params,
37
                                                               const BigInt& g,
38
                                                               size_t window_bits,
39
                                                               bool const_time) :
40
   m_params(params),
41
   m_window_bits(window_bits == 0 ? 4 : window_bits),
42
   m_const_time(const_time)
43
83.8k
   {
44
83.8k
   BOTAN_ARG_CHECK(g < m_params->p(), "Montgomery base too big");
45
83.8k
46
83.8k
   if(m_window_bits < 1 || m_window_bits > 12) // really even 8 is too large ...
47
0
      throw Invalid_Argument("Invalid window bits for Montgomery exponentiation");
48
83.8k
49
83.8k
   const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
50
83.8k
51
83.8k
   m_g.reserve(window_size);
52
83.8k
53
83.8k
   m_g.push_back(Montgomery_Int(m_params, m_params->R1(), false));
54
83.8k
55
83.8k
   m_g.push_back(Montgomery_Int(m_params, g));
56
83.8k
57
1.17M
   for(size_t i = 2; i != window_size; ++i)
58
1.08M
      {
59
1.08M
      m_g.push_back(m_g[1] * m_g[i - 1]);
60
1.08M
      }
61
83.8k
62
83.8k
   // Resize each element to exactly p words
63
1.34M
   for(size_t i = 0; i != window_size; ++i)
64
1.25M
      {
65
1.25M
      m_g[i].fix_size();
66
1.25M
      if(const_time)
67
1.24M
         m_g[i].const_time_poison();
68
1.25M
      }
69
83.8k
   }
70
71
namespace {
72
73
void const_time_lookup(secure_vector<word>& output,
74
                       const std::vector<Montgomery_Int>& g,
75
                       size_t nibble)
76
1.99M
   {
77
1.99M
   BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
78
1.99M
79
1.99M
   const size_t words = output.size();
80
1.99M
81
1.99M
   clear_mem(output.data(), output.size());
82
1.99M
83
17.9M
   for(size_t i = 0; i != g.size(); i += 2)
84
15.9M
      {
85
15.9M
      const secure_vector<word>& vec_0 = g[i  ].repr().get_word_vector();
86
15.9M
      const secure_vector<word>& vec_1 = g[i+1].repr().get_word_vector();
87
15.9M
88
15.9M
      BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
89
15.9M
90
15.9M
      const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
91
15.9M
      const auto mask_1 = CT::Mask<word>::is_equal(nibble, i+1);
92
15.9M
93
191M
      for(size_t w = 0; w != words; ++w)
94
175M
         {
95
175M
         output[w] |= mask_0.if_set_return(vec_0[w]);
96
175M
         output[w] |= mask_1.if_set_return(vec_1[w]);
97
175M
         }
98
15.9M
      }
99
1.99M
   }
100
101
}
102
103
BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const
104
77.4k
   {
105
77.4k
   BOTAN_DEBUG_ASSERT(scalar.bits() <= max_k_bits);
106
77.4k
   // TODO add a const-time implementation of above assert and use it in release builds
107
77.4k
108
77.4k
   const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
109
77.4k
110
77.4k
   if(exp_nibbles == 0)
111
6
      return 1;
112
77.4k
113
77.4k
   secure_vector<word> e_bits(m_params->p_words());
114
77.4k
   secure_vector<word> ws;
115
77.4k
116
77.4k
   const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits));
117
77.4k
   Montgomery_Int x(m_params, e_bits.data(), e_bits.size(), false);
118
77.4k
119
1.99M
   for(size_t i = exp_nibbles - 1; i > 0; --i)
120
1.91M
      {
121
1.91M
      x.square_this_n_times(ws, m_window_bits);
122
1.91M
      const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(i-1), m_window_bits));
123
1.91M
      x.mul_by(e_bits, ws);
124
1.91M
      }
125
77.4k
126
77.4k
   x.const_time_unpoison();
127
77.4k
   return x.value();
128
77.4k
   }
129
130
BigInt Montgomery_Exponentation_State::exponentiation_vartime(const BigInt& scalar) const
131
5.99k
   {
132
5.99k
   BOTAN_ASSERT_NOMSG(m_const_time == false);
133
5.99k
134
5.99k
   const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
135
5.99k
136
5.99k
   secure_vector<word> ws;
137
5.99k
138
5.99k
   if(exp_nibbles == 0)
139
0
      return 1;
140
5.99k
141
5.99k
   Montgomery_Int x = m_g[scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits)];
142
5.99k
143
176k
   for(size_t i = exp_nibbles - 1; i > 0; --i)
144
170k
      {
145
170k
      x.square_this_n_times(ws, m_window_bits);
146
170k
147
170k
      const uint32_t nibble = scalar.get_substring(m_window_bits*(i-1), m_window_bits);
148
170k
      if(nibble > 0)
149
32.6k
         x.mul_by(m_g[nibble], ws);
150
170k
      }
151
5.99k
152
5.99k
   x.const_time_unpoison();
153
5.99k
   return x.value();
154
5.99k
   }
155
156
std::shared_ptr<const Montgomery_Exponentation_State>
157
monty_precompute(std::shared_ptr<const Montgomery_Params> params,
158
                 const BigInt& g,
159
                 size_t window_bits,
160
                 bool const_time)
161
83.8k
   {
162
83.8k
   return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
163
83.8k
   }
164
165
BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state,
166
                     const BigInt& k, size_t max_k_bits)
167
77.4k
   {
168
77.4k
   return precomputed_state.exponentiation(k, max_k_bits);
169
77.4k
   }
170
171
BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state,
172
                             const BigInt& k)
173
5.99k
   {
174
5.99k
   return precomputed_state.exponentiation_vartime(k);
175
5.99k
   }
176
177
BigInt monty_multi_exp(std::shared_ptr<const Montgomery_Params> params_p,
178
                       const BigInt& x_bn,
179
                       const BigInt& z1,
180
                       const BigInt& y_bn,
181
                       const BigInt& z2)
182
180
   {
183
180
   if(z1.is_negative() || z2.is_negative())
184
0
      throw Invalid_Argument("multi_exponentiate exponents must be positive");
185
180
186
180
   const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
187
180
188
180
   secure_vector<word> ws;
189
180
190
180
   const Montgomery_Int one(params_p, params_p->R1(), false);
191
180
   //const Montgomery_Int one(params_p, 1);
192
180
193
180
   const Montgomery_Int x1(params_p, x_bn);
194
180
   const Montgomery_Int x2 = x1.square(ws);
195
180
   const Montgomery_Int x3 = x2.mul(x1, ws);
196
180
197
180
   const Montgomery_Int y1(params_p, y_bn);
198
180
   const Montgomery_Int y2 = y1.square(ws);
199
180
   const Montgomery_Int y3 = y2.mul(y1, ws);
200
180
201
180
   const Montgomery_Int y1x1 = y1.mul(x1, ws);
202
180
   const Montgomery_Int y1x2 = y1.mul(x2, ws);
203
180
   const Montgomery_Int y1x3 = y1.mul(x3, ws);
204
180
205
180
   const Montgomery_Int y2x1 = y2.mul(x1, ws);
206
180
   const Montgomery_Int y2x2 = y2.mul(x2, ws);
207
180
   const Montgomery_Int y2x3 = y2.mul(x3, ws);
208
180
209
180
   const Montgomery_Int y3x1 = y3.mul(x1, ws);
210
180
   const Montgomery_Int y3x2 = y3.mul(x2, ws);
211
180
   const Montgomery_Int y3x3 = y3.mul(x3, ws);
212
180
213
180
   const Montgomery_Int* M[16] = {
214
180
      &one,
215
180
      &x1,                    // 0001
216
180
      &x2,                    // 0010
217
180
      &x3,                    // 0011
218
180
      &y1,                    // 0100
219
180
      &y1x1,
220
180
      &y1x2,
221
180
      &y1x3,
222
180
      &y2,                    // 1000
223
180
      &y2x1,
224
180
      &y2x2,
225
180
      &y2x3,
226
180
      &y3,                    // 1100
227
180
      &y3x1,
228
180
      &y3x2,
229
180
      &y3x3
230
180
   };
231
180
232
180
   Montgomery_Int H = one;
233
180
234
59.5k
   for(size_t i = 0; i != z_bits; i += 2)
235
59.3k
      {
236
59.3k
      if(i > 0)
237
59.2k
         {
238
59.2k
         H.square_this(ws);
239
59.2k
         H.square_this(ws);
240
59.2k
         }
241
59.3k
242
59.3k
      const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
243
59.3k
      const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
244
59.3k
245
59.3k
      const uint32_t z12 = (4*z2_b) + z1_b;
246
59.3k
247
59.3k
      H.mul_by(*M[z12], ws);
248
59.3k
      }
249
180
250
180
   return H.value();
251
180
   }
252
253
}
254