grpc 1.10.0.pre1 → 1.10.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grpc might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Makefile +3 -3
- data/src/core/ext/filters/max_age/max_age_filter.cc +3 -0
- data/src/core/lib/surface/version.cc +1 -1
- data/src/ruby/lib/grpc/version.rb +1 -1
- data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +1100 -0
- data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +138 -0
- data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +112 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/add.c +375 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +537 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +370 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +269 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +239 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +303 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/div.c +726 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +1233 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +627 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +715 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +146 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +430 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +207 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +869 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +894 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/random.c +283 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +254 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +307 -0
- data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +502 -0
- data/third_party/boringssl/crypto/fipsmodule/cipher/aead.c +284 -0
- data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +613 -0
- data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +1437 -0
- data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +233 -0
- data/third_party/boringssl/crypto/fipsmodule/des/des.c +785 -0
- data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +256 -0
- data/third_party/boringssl/crypto/fipsmodule/digest/digests.c +280 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +842 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +517 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +304 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +404 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +1165 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +1708 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +561 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +1113 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/util-64.c +109 -0
- data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +456 -0
- data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +444 -0
- data/third_party/boringssl/crypto/fipsmodule/hmac/hmac.c +228 -0
- data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +254 -0
- data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +298 -0
- data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +212 -0
- data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +230 -0
- data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +219 -0
- data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +1074 -0
- data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +95 -0
- data/third_party/boringssl/crypto/fipsmodule/modes/polyval.c +91 -0
- data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +200 -0
- data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +358 -0
- data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +302 -0
- data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +263 -0
- data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +692 -0
- data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +855 -0
- data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +1061 -0
- data/third_party/boringssl/crypto/fipsmodule/sha/sha1-altivec.c +361 -0
- data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +375 -0
- data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +337 -0
- data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +608 -0
- metadata +62 -2
@@ -0,0 +1,1233 @@
|
|
1
|
+
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
2
|
+
* All rights reserved.
|
3
|
+
*
|
4
|
+
* This package is an SSL implementation written
|
5
|
+
* by Eric Young (eay@cryptsoft.com).
|
6
|
+
* The implementation was written so as to conform with Netscapes SSL.
|
7
|
+
*
|
8
|
+
* This library is free for commercial and non-commercial use as long as
|
9
|
+
* the following conditions are aheared to. The following conditions
|
10
|
+
* apply to all code found in this distribution, be it the RC4, RSA,
|
11
|
+
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
12
|
+
* included with this distribution is covered by the same copyright terms
|
13
|
+
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
14
|
+
*
|
15
|
+
* Copyright remains Eric Young's, and as such any Copyright notices in
|
16
|
+
* the code are not to be removed.
|
17
|
+
* If this package is used in a product, Eric Young should be given attribution
|
18
|
+
* as the author of the parts of the library used.
|
19
|
+
* This can be in the form of a textual message at program startup or
|
20
|
+
* in documentation (online or textual) provided with the package.
|
21
|
+
*
|
22
|
+
* Redistribution and use in source and binary forms, with or without
|
23
|
+
* modification, are permitted provided that the following conditions
|
24
|
+
* are met:
|
25
|
+
* 1. Redistributions of source code must retain the copyright
|
26
|
+
* notice, this list of conditions and the following disclaimer.
|
27
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
28
|
+
* notice, this list of conditions and the following disclaimer in the
|
29
|
+
* documentation and/or other materials provided with the distribution.
|
30
|
+
* 3. All advertising materials mentioning features or use of this software
|
31
|
+
* must display the following acknowledgement:
|
32
|
+
* "This product includes cryptographic software written by
|
33
|
+
* Eric Young (eay@cryptsoft.com)"
|
34
|
+
* The word 'cryptographic' can be left out if the rouines from the library
|
35
|
+
* being used are not cryptographic related :-).
|
36
|
+
* 4. If you include any Windows specific code (or a derivative thereof) from
|
37
|
+
* the apps directory (application code) you must include an acknowledgement:
|
38
|
+
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
39
|
+
*
|
40
|
+
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
41
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
42
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
43
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
44
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
45
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
46
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
47
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
48
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
49
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
50
|
+
* SUCH DAMAGE.
|
51
|
+
*
|
52
|
+
* The licence and distribution terms for any publically available version or
|
53
|
+
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
54
|
+
* copied and put under another distribution licence
|
55
|
+
* [including the GNU Public Licence.]
|
56
|
+
*/
|
57
|
+
/* ====================================================================
|
58
|
+
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
|
59
|
+
*
|
60
|
+
* Redistribution and use in source and binary forms, with or without
|
61
|
+
* modification, are permitted provided that the following conditions
|
62
|
+
* are met:
|
63
|
+
*
|
64
|
+
* 1. Redistributions of source code must retain the above copyright
|
65
|
+
* notice, this list of conditions and the following disclaimer.
|
66
|
+
*
|
67
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
68
|
+
* notice, this list of conditions and the following disclaimer in
|
69
|
+
* the documentation and/or other materials provided with the
|
70
|
+
* distribution.
|
71
|
+
*
|
72
|
+
* 3. All advertising materials mentioning features or use of this
|
73
|
+
* software must display the following acknowledgment:
|
74
|
+
* "This product includes software developed by the OpenSSL Project
|
75
|
+
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
76
|
+
*
|
77
|
+
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
78
|
+
* endorse or promote products derived from this software without
|
79
|
+
* prior written permission. For written permission, please contact
|
80
|
+
* openssl-core@openssl.org.
|
81
|
+
*
|
82
|
+
* 5. Products derived from this software may not be called "OpenSSL"
|
83
|
+
* nor may "OpenSSL" appear in their names without prior written
|
84
|
+
* permission of the OpenSSL Project.
|
85
|
+
*
|
86
|
+
* 6. Redistributions of any form whatsoever must retain the following
|
87
|
+
* acknowledgment:
|
88
|
+
* "This product includes software developed by the OpenSSL Project
|
89
|
+
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
90
|
+
*
|
91
|
+
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
92
|
+
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
93
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
94
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
95
|
+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
96
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
97
|
+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
98
|
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
99
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
100
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
101
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
102
|
+
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
103
|
+
* ====================================================================
|
104
|
+
*
|
105
|
+
* This product includes cryptographic software written by Eric Young
|
106
|
+
* (eay@cryptsoft.com). This product includes software written by Tim
|
107
|
+
* Hudson (tjh@cryptsoft.com). */
|
108
|
+
|
109
|
+
#include <openssl/bn.h>
|
110
|
+
|
111
|
+
#include <assert.h>
|
112
|
+
#include <string.h>
|
113
|
+
|
114
|
+
#include <openssl/cpu.h>
|
115
|
+
#include <openssl/err.h>
|
116
|
+
#include <openssl/mem.h>
|
117
|
+
|
118
|
+
#include "internal.h"
|
119
|
+
|
120
|
+
|
121
|
+
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
|
122
|
+
#define OPENSSL_BN_ASM_MONT5
|
123
|
+
#define RSAZ_ENABLED
|
124
|
+
|
125
|
+
#include "rsaz_exp.h"
|
126
|
+
|
127
|
+
void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap, const void *table,
|
128
|
+
const BN_ULONG *np, const BN_ULONG *n0, int num,
|
129
|
+
int power);
|
130
|
+
void bn_scatter5(const BN_ULONG *inp, size_t num, void *table, size_t power);
|
131
|
+
void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);
|
132
|
+
void bn_power5(BN_ULONG *rp, const BN_ULONG *ap, const void *table,
|
133
|
+
const BN_ULONG *np, const BN_ULONG *n0, int num, int power);
|
134
|
+
int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,
|
135
|
+
const BN_ULONG *not_used, const BN_ULONG *np,
|
136
|
+
const BN_ULONG *n0, int num);
|
137
|
+
#endif
|
138
|
+
|
139
|
+
int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
|
140
|
+
int i, bits, ret = 0;
|
141
|
+
BIGNUM *v, *rr;
|
142
|
+
|
143
|
+
BN_CTX_start(ctx);
|
144
|
+
if (r == a || r == p) {
|
145
|
+
rr = BN_CTX_get(ctx);
|
146
|
+
} else {
|
147
|
+
rr = r;
|
148
|
+
}
|
149
|
+
|
150
|
+
v = BN_CTX_get(ctx);
|
151
|
+
if (rr == NULL || v == NULL) {
|
152
|
+
goto err;
|
153
|
+
}
|
154
|
+
|
155
|
+
if (BN_copy(v, a) == NULL) {
|
156
|
+
goto err;
|
157
|
+
}
|
158
|
+
bits = BN_num_bits(p);
|
159
|
+
|
160
|
+
if (BN_is_odd(p)) {
|
161
|
+
if (BN_copy(rr, a) == NULL) {
|
162
|
+
goto err;
|
163
|
+
}
|
164
|
+
} else {
|
165
|
+
if (!BN_one(rr)) {
|
166
|
+
goto err;
|
167
|
+
}
|
168
|
+
}
|
169
|
+
|
170
|
+
for (i = 1; i < bits; i++) {
|
171
|
+
if (!BN_sqr(v, v, ctx)) {
|
172
|
+
goto err;
|
173
|
+
}
|
174
|
+
if (BN_is_bit_set(p, i)) {
|
175
|
+
if (!BN_mul(rr, rr, v, ctx)) {
|
176
|
+
goto err;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
if (r != rr && !BN_copy(r, rr)) {
|
182
|
+
goto err;
|
183
|
+
}
|
184
|
+
ret = 1;
|
185
|
+
|
186
|
+
err:
|
187
|
+
BN_CTX_end(ctx);
|
188
|
+
return ret;
|
189
|
+
}
|
190
|
+
|
191
|
+
// maximum precomputation table size for *variable* sliding windows
|
192
|
+
#define TABLE_SIZE 32
|
193
|
+
|
194
|
+
typedef struct bn_recp_ctx_st {
|
195
|
+
BIGNUM N; // the divisor
|
196
|
+
BIGNUM Nr; // the reciprocal
|
197
|
+
int num_bits;
|
198
|
+
int shift;
|
199
|
+
int flags;
|
200
|
+
} BN_RECP_CTX;
|
201
|
+
|
202
|
+
static void BN_RECP_CTX_init(BN_RECP_CTX *recp) {
|
203
|
+
BN_init(&recp->N);
|
204
|
+
BN_init(&recp->Nr);
|
205
|
+
recp->num_bits = 0;
|
206
|
+
recp->shift = 0;
|
207
|
+
recp->flags = 0;
|
208
|
+
}
|
209
|
+
|
210
|
+
static void BN_RECP_CTX_free(BN_RECP_CTX *recp) {
|
211
|
+
if (recp == NULL) {
|
212
|
+
return;
|
213
|
+
}
|
214
|
+
|
215
|
+
BN_free(&recp->N);
|
216
|
+
BN_free(&recp->Nr);
|
217
|
+
}
|
218
|
+
|
219
|
+
static int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) {
|
220
|
+
if (!BN_copy(&(recp->N), d)) {
|
221
|
+
return 0;
|
222
|
+
}
|
223
|
+
BN_zero(&recp->Nr);
|
224
|
+
recp->num_bits = BN_num_bits(d);
|
225
|
+
recp->shift = 0;
|
226
|
+
|
227
|
+
return 1;
|
228
|
+
}
|
229
|
+
|
230
|
+
// len is the expected size of the result We actually calculate with an extra
|
231
|
+
// word of precision, so we can do faster division if the remainder is not
|
232
|
+
// required.
|
233
|
+
// r := 2^len / m
|
234
|
+
static int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) {
|
235
|
+
int ret = -1;
|
236
|
+
BIGNUM *t;
|
237
|
+
|
238
|
+
BN_CTX_start(ctx);
|
239
|
+
t = BN_CTX_get(ctx);
|
240
|
+
if (t == NULL) {
|
241
|
+
goto err;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (!BN_set_bit(t, len)) {
|
245
|
+
goto err;
|
246
|
+
}
|
247
|
+
|
248
|
+
if (!BN_div(r, NULL, t, m, ctx)) {
|
249
|
+
goto err;
|
250
|
+
}
|
251
|
+
|
252
|
+
ret = len;
|
253
|
+
|
254
|
+
err:
|
255
|
+
BN_CTX_end(ctx);
|
256
|
+
return ret;
|
257
|
+
}
|
258
|
+
|
259
|
+
static int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
|
260
|
+
BN_RECP_CTX *recp, BN_CTX *ctx) {
|
261
|
+
int i, j, ret = 0;
|
262
|
+
BIGNUM *a, *b, *d, *r;
|
263
|
+
|
264
|
+
BN_CTX_start(ctx);
|
265
|
+
a = BN_CTX_get(ctx);
|
266
|
+
b = BN_CTX_get(ctx);
|
267
|
+
if (dv != NULL) {
|
268
|
+
d = dv;
|
269
|
+
} else {
|
270
|
+
d = BN_CTX_get(ctx);
|
271
|
+
}
|
272
|
+
|
273
|
+
if (rem != NULL) {
|
274
|
+
r = rem;
|
275
|
+
} else {
|
276
|
+
r = BN_CTX_get(ctx);
|
277
|
+
}
|
278
|
+
|
279
|
+
if (a == NULL || b == NULL || d == NULL || r == NULL) {
|
280
|
+
goto err;
|
281
|
+
}
|
282
|
+
|
283
|
+
if (BN_ucmp(m, &recp->N) < 0) {
|
284
|
+
BN_zero(d);
|
285
|
+
if (!BN_copy(r, m)) {
|
286
|
+
goto err;
|
287
|
+
}
|
288
|
+
BN_CTX_end(ctx);
|
289
|
+
return 1;
|
290
|
+
}
|
291
|
+
|
292
|
+
// We want the remainder
|
293
|
+
// Given input of ABCDEF / ab
|
294
|
+
// we need multiply ABCDEF by 3 digests of the reciprocal of ab
|
295
|
+
|
296
|
+
// i := max(BN_num_bits(m), 2*BN_num_bits(N))
|
297
|
+
i = BN_num_bits(m);
|
298
|
+
j = recp->num_bits << 1;
|
299
|
+
if (j > i) {
|
300
|
+
i = j;
|
301
|
+
}
|
302
|
+
|
303
|
+
// Nr := round(2^i / N)
|
304
|
+
if (i != recp->shift) {
|
305
|
+
recp->shift =
|
306
|
+
BN_reciprocal(&(recp->Nr), &(recp->N), i,
|
307
|
+
ctx); // BN_reciprocal returns i, or -1 for an error
|
308
|
+
}
|
309
|
+
|
310
|
+
if (recp->shift == -1) {
|
311
|
+
goto err;
|
312
|
+
}
|
313
|
+
|
314
|
+
// d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i -
|
315
|
+
// BN_num_bits(N)))|
|
316
|
+
// = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i -
|
317
|
+
// BN_num_bits(N)))|
|
318
|
+
// <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
|
319
|
+
// = |m/N|
|
320
|
+
if (!BN_rshift(a, m, recp->num_bits)) {
|
321
|
+
goto err;
|
322
|
+
}
|
323
|
+
if (!BN_mul(b, a, &(recp->Nr), ctx)) {
|
324
|
+
goto err;
|
325
|
+
}
|
326
|
+
if (!BN_rshift(d, b, i - recp->num_bits)) {
|
327
|
+
goto err;
|
328
|
+
}
|
329
|
+
d->neg = 0;
|
330
|
+
|
331
|
+
if (!BN_mul(b, &(recp->N), d, ctx)) {
|
332
|
+
goto err;
|
333
|
+
}
|
334
|
+
if (!BN_usub(r, m, b)) {
|
335
|
+
goto err;
|
336
|
+
}
|
337
|
+
r->neg = 0;
|
338
|
+
|
339
|
+
j = 0;
|
340
|
+
while (BN_ucmp(r, &(recp->N)) >= 0) {
|
341
|
+
if (j++ > 2) {
|
342
|
+
OPENSSL_PUT_ERROR(BN, BN_R_BAD_RECIPROCAL);
|
343
|
+
goto err;
|
344
|
+
}
|
345
|
+
if (!BN_usub(r, r, &(recp->N))) {
|
346
|
+
goto err;
|
347
|
+
}
|
348
|
+
if (!BN_add_word(d, 1)) {
|
349
|
+
goto err;
|
350
|
+
}
|
351
|
+
}
|
352
|
+
|
353
|
+
r->neg = BN_is_zero(r) ? 0 : m->neg;
|
354
|
+
d->neg = m->neg ^ recp->N.neg;
|
355
|
+
ret = 1;
|
356
|
+
|
357
|
+
err:
|
358
|
+
BN_CTX_end(ctx);
|
359
|
+
return ret;
|
360
|
+
}
|
361
|
+
|
362
|
+
static int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
|
363
|
+
BN_RECP_CTX *recp, BN_CTX *ctx) {
|
364
|
+
int ret = 0;
|
365
|
+
BIGNUM *a;
|
366
|
+
const BIGNUM *ca;
|
367
|
+
|
368
|
+
BN_CTX_start(ctx);
|
369
|
+
a = BN_CTX_get(ctx);
|
370
|
+
if (a == NULL) {
|
371
|
+
goto err;
|
372
|
+
}
|
373
|
+
|
374
|
+
if (y != NULL) {
|
375
|
+
if (x == y) {
|
376
|
+
if (!BN_sqr(a, x, ctx)) {
|
377
|
+
goto err;
|
378
|
+
}
|
379
|
+
} else {
|
380
|
+
if (!BN_mul(a, x, y, ctx)) {
|
381
|
+
goto err;
|
382
|
+
}
|
383
|
+
}
|
384
|
+
ca = a;
|
385
|
+
} else {
|
386
|
+
ca = x; // Just do the mod
|
387
|
+
}
|
388
|
+
|
389
|
+
ret = BN_div_recp(NULL, r, ca, recp, ctx);
|
390
|
+
|
391
|
+
err:
|
392
|
+
BN_CTX_end(ctx);
|
393
|
+
return ret;
|
394
|
+
}
|
395
|
+
|
396
|
+
// BN_window_bits_for_exponent_size -- macro for sliding window mod_exp
|
397
|
+
// functions
|
398
|
+
//
|
399
|
+
// For window size 'w' (w >= 2) and a random 'b' bits exponent, the number of
|
400
|
+
// multiplications is a constant plus on average
|
401
|
+
//
|
402
|
+
// 2^(w-1) + (b-w)/(w+1);
|
403
|
+
//
|
404
|
+
// here 2^(w-1) is for precomputing the table (we actually need entries only
|
405
|
+
// for windows that have the lowest bit set), and (b-w)/(w+1) is an
|
406
|
+
// approximation for the expected number of w-bit windows, not counting the
|
407
|
+
// first one.
|
408
|
+
//
|
409
|
+
// Thus we should use
|
410
|
+
//
|
411
|
+
// w >= 6 if b > 671
|
412
|
+
// w = 5 if 671 > b > 239
|
413
|
+
// w = 4 if 239 > b > 79
|
414
|
+
// w = 3 if 79 > b > 23
|
415
|
+
// w <= 2 if 23 > b
|
416
|
+
//
|
417
|
+
// (with draws in between). Very small exponents are often selected
|
418
|
+
// with low Hamming weight, so we use w = 1 for b <= 23.
|
419
|
+
#define BN_window_bits_for_exponent_size(b) \
|
420
|
+
((b) > 671 ? 6 : \
|
421
|
+
(b) > 239 ? 5 : \
|
422
|
+
(b) > 79 ? 4 : \
|
423
|
+
(b) > 23 ? 3 : 1)
|
424
|
+
|
425
|
+
static int mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
426
|
+
const BIGNUM *m, BN_CTX *ctx) {
|
427
|
+
int i, j, bits, ret = 0, wstart, window;
|
428
|
+
int start = 1;
|
429
|
+
BIGNUM *aa;
|
430
|
+
// Table of variables obtained from 'ctx'
|
431
|
+
BIGNUM *val[TABLE_SIZE];
|
432
|
+
BN_RECP_CTX recp;
|
433
|
+
|
434
|
+
bits = BN_num_bits(p);
|
435
|
+
|
436
|
+
if (bits == 0) {
|
437
|
+
// x**0 mod 1 is still zero.
|
438
|
+
if (BN_is_one(m)) {
|
439
|
+
BN_zero(r);
|
440
|
+
return 1;
|
441
|
+
}
|
442
|
+
return BN_one(r);
|
443
|
+
}
|
444
|
+
|
445
|
+
BN_CTX_start(ctx);
|
446
|
+
aa = BN_CTX_get(ctx);
|
447
|
+
val[0] = BN_CTX_get(ctx);
|
448
|
+
if (!aa || !val[0]) {
|
449
|
+
goto err;
|
450
|
+
}
|
451
|
+
|
452
|
+
BN_RECP_CTX_init(&recp);
|
453
|
+
if (m->neg) {
|
454
|
+
// ignore sign of 'm'
|
455
|
+
if (!BN_copy(aa, m)) {
|
456
|
+
goto err;
|
457
|
+
}
|
458
|
+
aa->neg = 0;
|
459
|
+
if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0) {
|
460
|
+
goto err;
|
461
|
+
}
|
462
|
+
} else {
|
463
|
+
if (BN_RECP_CTX_set(&recp, m, ctx) <= 0) {
|
464
|
+
goto err;
|
465
|
+
}
|
466
|
+
}
|
467
|
+
|
468
|
+
if (!BN_nnmod(val[0], a, m, ctx)) {
|
469
|
+
goto err; // 1
|
470
|
+
}
|
471
|
+
if (BN_is_zero(val[0])) {
|
472
|
+
BN_zero(r);
|
473
|
+
ret = 1;
|
474
|
+
goto err;
|
475
|
+
}
|
476
|
+
|
477
|
+
window = BN_window_bits_for_exponent_size(bits);
|
478
|
+
if (window > 1) {
|
479
|
+
if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx)) {
|
480
|
+
goto err; // 2
|
481
|
+
}
|
482
|
+
j = 1 << (window - 1);
|
483
|
+
for (i = 1; i < j; i++) {
|
484
|
+
if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
|
485
|
+
!BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx)) {
|
486
|
+
goto err;
|
487
|
+
}
|
488
|
+
}
|
489
|
+
}
|
490
|
+
|
491
|
+
start = 1; // This is used to avoid multiplication etc
|
492
|
+
// when there is only the value '1' in the
|
493
|
+
// buffer.
|
494
|
+
wstart = bits - 1; // The top bit of the window
|
495
|
+
|
496
|
+
if (!BN_one(r)) {
|
497
|
+
goto err;
|
498
|
+
}
|
499
|
+
|
500
|
+
for (;;) {
|
501
|
+
int wvalue; // The 'value' of the window
|
502
|
+
int wend; // The bottom bit of the window
|
503
|
+
|
504
|
+
if (BN_is_bit_set(p, wstart) == 0) {
|
505
|
+
if (!start) {
|
506
|
+
if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) {
|
507
|
+
goto err;
|
508
|
+
}
|
509
|
+
}
|
510
|
+
if (wstart == 0) {
|
511
|
+
break;
|
512
|
+
}
|
513
|
+
wstart--;
|
514
|
+
continue;
|
515
|
+
}
|
516
|
+
|
517
|
+
// We now have wstart on a 'set' bit, we now need to work out
|
518
|
+
// how bit a window to do. To do this we need to scan
|
519
|
+
// forward until the last set bit before the end of the
|
520
|
+
// window
|
521
|
+
wvalue = 1;
|
522
|
+
wend = 0;
|
523
|
+
for (i = 1; i < window; i++) {
|
524
|
+
if (wstart - i < 0) {
|
525
|
+
break;
|
526
|
+
}
|
527
|
+
if (BN_is_bit_set(p, wstart - i)) {
|
528
|
+
wvalue <<= (i - wend);
|
529
|
+
wvalue |= 1;
|
530
|
+
wend = i;
|
531
|
+
}
|
532
|
+
}
|
533
|
+
|
534
|
+
// wend is the size of the current window
|
535
|
+
j = wend + 1;
|
536
|
+
// add the 'bytes above'
|
537
|
+
if (!start) {
|
538
|
+
for (i = 0; i < j; i++) {
|
539
|
+
if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) {
|
540
|
+
goto err;
|
541
|
+
}
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
// wvalue will be an odd number < 2^window
|
546
|
+
if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx)) {
|
547
|
+
goto err;
|
548
|
+
}
|
549
|
+
|
550
|
+
// move the 'window' down further
|
551
|
+
wstart -= wend + 1;
|
552
|
+
start = 0;
|
553
|
+
if (wstart < 0) {
|
554
|
+
break;
|
555
|
+
}
|
556
|
+
}
|
557
|
+
ret = 1;
|
558
|
+
|
559
|
+
err:
|
560
|
+
BN_CTX_end(ctx);
|
561
|
+
BN_RECP_CTX_free(&recp);
|
562
|
+
return ret;
|
563
|
+
}
|
564
|
+
|
565
|
+
int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
|
566
|
+
BN_CTX *ctx) {
|
567
|
+
if (BN_is_odd(m)) {
|
568
|
+
return BN_mod_exp_mont(r, a, p, m, ctx, NULL);
|
569
|
+
}
|
570
|
+
|
571
|
+
return mod_exp_recp(r, a, p, m, ctx);
|
572
|
+
}
|
573
|
+
|
574
|
+
int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
|
575
|
+
const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont) {
|
576
|
+
int i, j, bits, ret = 0, wstart, window;
|
577
|
+
int start = 1;
|
578
|
+
BIGNUM *d, *r;
|
579
|
+
const BIGNUM *aa;
|
580
|
+
// Table of variables obtained from 'ctx'
|
581
|
+
BIGNUM *val[TABLE_SIZE];
|
582
|
+
BN_MONT_CTX *new_mont = NULL;
|
583
|
+
|
584
|
+
if (!BN_is_odd(m)) {
|
585
|
+
OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
|
586
|
+
return 0;
|
587
|
+
}
|
588
|
+
bits = BN_num_bits(p);
|
589
|
+
if (bits == 0) {
|
590
|
+
// x**0 mod 1 is still zero.
|
591
|
+
if (BN_is_one(m)) {
|
592
|
+
BN_zero(rr);
|
593
|
+
return 1;
|
594
|
+
}
|
595
|
+
return BN_one(rr);
|
596
|
+
}
|
597
|
+
|
598
|
+
BN_CTX_start(ctx);
|
599
|
+
d = BN_CTX_get(ctx);
|
600
|
+
r = BN_CTX_get(ctx);
|
601
|
+
val[0] = BN_CTX_get(ctx);
|
602
|
+
if (!d || !r || !val[0]) {
|
603
|
+
goto err;
|
604
|
+
}
|
605
|
+
|
606
|
+
// Allocate a montgomery context if it was not supplied by the caller.
|
607
|
+
if (mont == NULL) {
|
608
|
+
new_mont = BN_MONT_CTX_new();
|
609
|
+
if (new_mont == NULL || !BN_MONT_CTX_set(new_mont, m, ctx)) {
|
610
|
+
goto err;
|
611
|
+
}
|
612
|
+
mont = new_mont;
|
613
|
+
}
|
614
|
+
|
615
|
+
if (a->neg || BN_ucmp(a, m) >= 0) {
|
616
|
+
if (!BN_nnmod(val[0], a, m, ctx)) {
|
617
|
+
goto err;
|
618
|
+
}
|
619
|
+
aa = val[0];
|
620
|
+
} else {
|
621
|
+
aa = a;
|
622
|
+
}
|
623
|
+
|
624
|
+
if (BN_is_zero(aa)) {
|
625
|
+
BN_zero(rr);
|
626
|
+
ret = 1;
|
627
|
+
goto err;
|
628
|
+
}
|
629
|
+
if (!BN_to_montgomery(val[0], aa, mont, ctx)) {
|
630
|
+
goto err; // 1
|
631
|
+
}
|
632
|
+
|
633
|
+
window = BN_window_bits_for_exponent_size(bits);
|
634
|
+
if (window > 1) {
|
635
|
+
if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx)) {
|
636
|
+
goto err; // 2
|
637
|
+
}
|
638
|
+
j = 1 << (window - 1);
|
639
|
+
for (i = 1; i < j; i++) {
|
640
|
+
if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
|
641
|
+
!BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx)) {
|
642
|
+
goto err;
|
643
|
+
}
|
644
|
+
}
|
645
|
+
}
|
646
|
+
|
647
|
+
start = 1; // This is used to avoid multiplication etc
|
648
|
+
// when there is only the value '1' in the
|
649
|
+
// buffer.
|
650
|
+
wstart = bits - 1; // The top bit of the window
|
651
|
+
|
652
|
+
j = m->top; // borrow j
|
653
|
+
if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
|
654
|
+
if (!bn_wexpand(r, j)) {
|
655
|
+
goto err;
|
656
|
+
}
|
657
|
+
// 2^(top*BN_BITS2) - m
|
658
|
+
r->d[0] = (0 - m->d[0]) & BN_MASK2;
|
659
|
+
for (i = 1; i < j; i++) {
|
660
|
+
r->d[i] = (~m->d[i]) & BN_MASK2;
|
661
|
+
}
|
662
|
+
r->top = j;
|
663
|
+
// Upper words will be zero if the corresponding words of 'm'
|
664
|
+
// were 0xfff[...], so decrement r->top accordingly.
|
665
|
+
bn_correct_top(r);
|
666
|
+
} else if (!BN_to_montgomery(r, BN_value_one(), mont, ctx)) {
|
667
|
+
goto err;
|
668
|
+
}
|
669
|
+
|
670
|
+
for (;;) {
|
671
|
+
int wvalue; // The 'value' of the window
|
672
|
+
int wend; // The bottom bit of the window
|
673
|
+
|
674
|
+
if (BN_is_bit_set(p, wstart) == 0) {
|
675
|
+
if (!start && !BN_mod_mul_montgomery(r, r, r, mont, ctx)) {
|
676
|
+
goto err;
|
677
|
+
}
|
678
|
+
if (wstart == 0) {
|
679
|
+
break;
|
680
|
+
}
|
681
|
+
wstart--;
|
682
|
+
continue;
|
683
|
+
}
|
684
|
+
|
685
|
+
// We now have wstart on a 'set' bit, we now need to work out how bit a
|
686
|
+
// window to do. To do this we need to scan forward until the last set bit
|
687
|
+
// before the end of the window
|
688
|
+
wvalue = 1;
|
689
|
+
wend = 0;
|
690
|
+
for (i = 1; i < window; i++) {
|
691
|
+
if (wstart - i < 0) {
|
692
|
+
break;
|
693
|
+
}
|
694
|
+
if (BN_is_bit_set(p, wstart - i)) {
|
695
|
+
wvalue <<= (i - wend);
|
696
|
+
wvalue |= 1;
|
697
|
+
wend = i;
|
698
|
+
}
|
699
|
+
}
|
700
|
+
|
701
|
+
// wend is the size of the current window
|
702
|
+
j = wend + 1;
|
703
|
+
// add the 'bytes above'
|
704
|
+
if (!start) {
|
705
|
+
for (i = 0; i < j; i++) {
|
706
|
+
if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) {
|
707
|
+
goto err;
|
708
|
+
}
|
709
|
+
}
|
710
|
+
}
|
711
|
+
|
712
|
+
// wvalue will be an odd number < 2^window
|
713
|
+
if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx)) {
|
714
|
+
goto err;
|
715
|
+
}
|
716
|
+
|
717
|
+
// move the 'window' down further
|
718
|
+
wstart -= wend + 1;
|
719
|
+
start = 0;
|
720
|
+
if (wstart < 0) {
|
721
|
+
break;
|
722
|
+
}
|
723
|
+
}
|
724
|
+
|
725
|
+
if (!BN_from_montgomery(rr, r, mont, ctx)) {
|
726
|
+
goto err;
|
727
|
+
}
|
728
|
+
ret = 1;
|
729
|
+
|
730
|
+
err:
|
731
|
+
BN_MONT_CTX_free(new_mont);
|
732
|
+
BN_CTX_end(ctx);
|
733
|
+
return ret;
|
734
|
+
}
|
735
|
+
|
736
|
+
// BN_mod_exp_mont_consttime() stores the precomputed powers in a specific
|
737
|
+
// layout so that accessing any of these table values shows the same access
|
738
|
+
// pattern as far as cache lines are concerned. The following functions are
|
739
|
+
// used to transfer a BIGNUM from/to that table.
|
740
|
+
static int copy_to_prebuf(const BIGNUM *b, int top, unsigned char *buf, int idx,
|
741
|
+
int window) {
|
742
|
+
int i, j;
|
743
|
+
const int width = 1 << window;
|
744
|
+
BN_ULONG *table = (BN_ULONG *) buf;
|
745
|
+
|
746
|
+
if (top > b->top) {
|
747
|
+
top = b->top; // this works because 'buf' is explicitly zeroed
|
748
|
+
}
|
749
|
+
|
750
|
+
for (i = 0, j = idx; i < top; i++, j += width) {
|
751
|
+
table[j] = b->d[i];
|
752
|
+
}
|
753
|
+
|
754
|
+
return 1;
|
755
|
+
}
|
756
|
+
|
757
|
+
static int copy_from_prebuf(BIGNUM *b, int top, unsigned char *buf, int idx,
|
758
|
+
int window) {
|
759
|
+
int i, j;
|
760
|
+
const int width = 1 << window;
|
761
|
+
volatile BN_ULONG *table = (volatile BN_ULONG *)buf;
|
762
|
+
|
763
|
+
if (!bn_wexpand(b, top)) {
|
764
|
+
return 0;
|
765
|
+
}
|
766
|
+
|
767
|
+
if (window <= 3) {
|
768
|
+
for (i = 0; i < top; i++, table += width) {
|
769
|
+
BN_ULONG acc = 0;
|
770
|
+
|
771
|
+
for (j = 0; j < width; j++) {
|
772
|
+
acc |= table[j] & ((BN_ULONG)0 - (constant_time_eq_int(j, idx) & 1));
|
773
|
+
}
|
774
|
+
|
775
|
+
b->d[i] = acc;
|
776
|
+
}
|
777
|
+
} else {
|
778
|
+
int xstride = 1 << (window - 2);
|
779
|
+
BN_ULONG y0, y1, y2, y3;
|
780
|
+
|
781
|
+
i = idx >> (window - 2); // equivalent of idx / xstride
|
782
|
+
idx &= xstride - 1; // equivalent of idx % xstride
|
783
|
+
|
784
|
+
y0 = (BN_ULONG)0 - (constant_time_eq_int(i, 0) & 1);
|
785
|
+
y1 = (BN_ULONG)0 - (constant_time_eq_int(i, 1) & 1);
|
786
|
+
y2 = (BN_ULONG)0 - (constant_time_eq_int(i, 2) & 1);
|
787
|
+
y3 = (BN_ULONG)0 - (constant_time_eq_int(i, 3) & 1);
|
788
|
+
|
789
|
+
for (i = 0; i < top; i++, table += width) {
|
790
|
+
BN_ULONG acc = 0;
|
791
|
+
|
792
|
+
for (j = 0; j < xstride; j++) {
|
793
|
+
acc |= ((table[j + 0 * xstride] & y0) | (table[j + 1 * xstride] & y1) |
|
794
|
+
(table[j + 2 * xstride] & y2) | (table[j + 3 * xstride] & y3)) &
|
795
|
+
((BN_ULONG)0 - (constant_time_eq_int(j, idx) & 1));
|
796
|
+
}
|
797
|
+
|
798
|
+
b->d[i] = acc;
|
799
|
+
}
|
800
|
+
}
|
801
|
+
|
802
|
+
b->top = top;
|
803
|
+
bn_correct_top(b);
|
804
|
+
return 1;
|
805
|
+
}
|
806
|
+
|
807
|
+
// BN_mod_exp_mont_conttime is based on the assumption that the L1 data cache
|
808
|
+
// line width of the target processor is at least the following value.
|
809
|
+
#define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH (64)
|
810
|
+
#define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK \
|
811
|
+
(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
|
812
|
+
|
813
|
+
// Window sizes optimized for fixed window size modular exponentiation
|
814
|
+
// algorithm (BN_mod_exp_mont_consttime).
|
815
|
+
//
|
816
|
+
// To achieve the security goals of BN_mode_exp_mont_consttime, the maximum
|
817
|
+
// size of the window must not exceed
|
818
|
+
// log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH).
|
819
|
+
//
|
820
|
+
// Window size thresholds are defined for cache line sizes of 32 and 64, cache
|
821
|
+
// line sizes where log_2(32)=5 and log_2(64)=6 respectively. A window size of
|
822
|
+
// 7 should only be used on processors that have a 128 byte or greater cache
|
823
|
+
// line size.
|
824
|
+
#if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
|
825
|
+
|
826
|
+
#define BN_window_bits_for_ctime_exponent_size(b) \
|
827
|
+
((b) > 937 ? 6 : (b) > 306 ? 5 : (b) > 89 ? 4 : (b) > 22 ? 3 : 1)
|
828
|
+
#define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6)
|
829
|
+
|
830
|
+
#elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
|
831
|
+
|
832
|
+
#define BN_window_bits_for_ctime_exponent_size(b) \
|
833
|
+
((b) > 306 ? 5 : (b) > 89 ? 4 : (b) > 22 ? 3 : 1)
|
834
|
+
#define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5)
|
835
|
+
|
836
|
+
#endif
|
837
|
+
|
838
|
+
// Given a pointer value, compute the next address that is a cache line
|
839
|
+
// multiple.
|
840
|
+
#define MOD_EXP_CTIME_ALIGN(x_) \
|
841
|
+
((unsigned char *)(x_) + \
|
842
|
+
(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - \
|
843
|
+
(((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
|
844
|
+
|
845
|
+
// This variant of BN_mod_exp_mont() uses fixed windows and the special
|
846
|
+
// precomputation memory layout to limit data-dependency to a minimum
|
847
|
+
// to protect secret exponents (cf. the hyper-threading timing attacks
|
848
|
+
// pointed out by Colin Percival,
|
849
|
+
// http://www.daemonology.net/hyperthreading-considered-harmful/)
|
850
|
+
int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
|
851
|
+
const BIGNUM *m, BN_CTX *ctx,
|
852
|
+
const BN_MONT_CTX *mont) {
|
853
|
+
int i, bits, ret = 0, window, wvalue;
|
854
|
+
int top;
|
855
|
+
BN_MONT_CTX *new_mont = NULL;
|
856
|
+
|
857
|
+
int numPowers;
|
858
|
+
unsigned char *powerbufFree = NULL;
|
859
|
+
int powerbufLen = 0;
|
860
|
+
unsigned char *powerbuf = NULL;
|
861
|
+
BIGNUM tmp, am;
|
862
|
+
BIGNUM *new_a = NULL;
|
863
|
+
|
864
|
+
if (!BN_is_odd(m)) {
|
865
|
+
OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
|
866
|
+
return 0;
|
867
|
+
}
|
868
|
+
|
869
|
+
top = m->top;
|
870
|
+
|
871
|
+
bits = BN_num_bits(p);
|
872
|
+
if (bits == 0) {
|
873
|
+
// x**0 mod 1 is still zero.
|
874
|
+
if (BN_is_one(m)) {
|
875
|
+
BN_zero(rr);
|
876
|
+
return 1;
|
877
|
+
}
|
878
|
+
return BN_one(rr);
|
879
|
+
}
|
880
|
+
|
881
|
+
// Allocate a montgomery context if it was not supplied by the caller.
|
882
|
+
if (mont == NULL) {
|
883
|
+
new_mont = BN_MONT_CTX_new();
|
884
|
+
if (new_mont == NULL || !BN_MONT_CTX_set(new_mont, m, ctx)) {
|
885
|
+
goto err;
|
886
|
+
}
|
887
|
+
mont = new_mont;
|
888
|
+
}
|
889
|
+
|
890
|
+
if (a->neg || BN_ucmp(a, m) >= 0) {
|
891
|
+
new_a = BN_new();
|
892
|
+
if (new_a == NULL ||
|
893
|
+
!BN_nnmod(new_a, a, m, ctx)) {
|
894
|
+
goto err;
|
895
|
+
}
|
896
|
+
a = new_a;
|
897
|
+
}
|
898
|
+
|
899
|
+
#ifdef RSAZ_ENABLED
|
900
|
+
// If the size of the operands allow it, perform the optimized
|
901
|
+
// RSAZ exponentiation. For further information see
|
902
|
+
// crypto/bn/rsaz_exp.c and accompanying assembly modules.
|
903
|
+
if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024) &&
|
904
|
+
rsaz_avx2_eligible()) {
|
905
|
+
if (!bn_wexpand(rr, 16)) {
|
906
|
+
goto err;
|
907
|
+
}
|
908
|
+
RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d, mont->n0[0]);
|
909
|
+
rr->top = 16;
|
910
|
+
rr->neg = 0;
|
911
|
+
bn_correct_top(rr);
|
912
|
+
ret = 1;
|
913
|
+
goto err;
|
914
|
+
}
|
915
|
+
#endif
|
916
|
+
|
917
|
+
// Get the window size to use with size of p.
|
918
|
+
window = BN_window_bits_for_ctime_exponent_size(bits);
|
919
|
+
#if defined(OPENSSL_BN_ASM_MONT5)
|
920
|
+
if (window >= 5) {
|
921
|
+
window = 5; // ~5% improvement for RSA2048 sign, and even for RSA4096
|
922
|
+
// reserve space for mont->N.d[] copy
|
923
|
+
powerbufLen += top * sizeof(mont->N.d[0]);
|
924
|
+
}
|
925
|
+
#endif
|
926
|
+
|
927
|
+
// Allocate a buffer large enough to hold all of the pre-computed
|
928
|
+
// powers of am, am itself and tmp.
|
929
|
+
numPowers = 1 << window;
|
930
|
+
powerbufLen +=
|
931
|
+
sizeof(m->d[0]) *
|
932
|
+
(top * numPowers + ((2 * top) > numPowers ? (2 * top) : numPowers));
|
933
|
+
#ifdef alloca
|
934
|
+
if (powerbufLen < 3072) {
|
935
|
+
powerbufFree = alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
|
936
|
+
} else
|
937
|
+
#endif
|
938
|
+
{
|
939
|
+
if ((powerbufFree = OPENSSL_malloc(
|
940
|
+
powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL) {
|
941
|
+
goto err;
|
942
|
+
}
|
943
|
+
}
|
944
|
+
|
945
|
+
powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
|
946
|
+
OPENSSL_memset(powerbuf, 0, powerbufLen);
|
947
|
+
|
948
|
+
#ifdef alloca
|
949
|
+
if (powerbufLen < 3072) {
|
950
|
+
powerbufFree = NULL;
|
951
|
+
}
|
952
|
+
#endif
|
953
|
+
|
954
|
+
// lay down tmp and am right after powers table
|
955
|
+
tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);
|
956
|
+
am.d = tmp.d + top;
|
957
|
+
tmp.top = am.top = 0;
|
958
|
+
tmp.dmax = am.dmax = top;
|
959
|
+
tmp.neg = am.neg = 0;
|
960
|
+
tmp.flags = am.flags = BN_FLG_STATIC_DATA;
|
961
|
+
|
962
|
+
// prepare a^0 in Montgomery domain
|
963
|
+
// by Shay Gueron's suggestion
|
964
|
+
if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
|
965
|
+
// 2^(top*BN_BITS2) - m
|
966
|
+
tmp.d[0] = (0 - m->d[0]) & BN_MASK2;
|
967
|
+
for (i = 1; i < top; i++) {
|
968
|
+
tmp.d[i] = (~m->d[i]) & BN_MASK2;
|
969
|
+
}
|
970
|
+
tmp.top = top;
|
971
|
+
} else if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx)) {
|
972
|
+
goto err;
|
973
|
+
}
|
974
|
+
|
975
|
+
// prepare a^1 in Montgomery domain
|
976
|
+
assert(!a->neg);
|
977
|
+
assert(BN_ucmp(a, m) < 0);
|
978
|
+
if (!BN_to_montgomery(&am, a, mont, ctx)) {
|
979
|
+
goto err;
|
980
|
+
}
|
981
|
+
|
982
|
+
#if defined(OPENSSL_BN_ASM_MONT5)
|
983
|
+
// This optimization uses ideas from http://eprint.iacr.org/2011/239,
|
984
|
+
// specifically optimization of cache-timing attack countermeasures
|
985
|
+
// and pre-computation optimization.
|
986
|
+
|
987
|
+
// Dedicated window==4 case improves 512-bit RSA sign by ~15%, but as
|
988
|
+
// 512-bit RSA is hardly relevant, we omit it to spare size...
|
989
|
+
if (window == 5 && top > 1) {
|
990
|
+
const BN_ULONG *n0 = mont->n0;
|
991
|
+
BN_ULONG *np;
|
992
|
+
|
993
|
+
// BN_to_montgomery can contaminate words above .top
|
994
|
+
// [in BN_DEBUG[_DEBUG] build]...
|
995
|
+
for (i = am.top; i < top; i++) {
|
996
|
+
am.d[i] = 0;
|
997
|
+
}
|
998
|
+
for (i = tmp.top; i < top; i++) {
|
999
|
+
tmp.d[i] = 0;
|
1000
|
+
}
|
1001
|
+
|
1002
|
+
// copy mont->N.d[] to improve cache locality
|
1003
|
+
for (np = am.d + top, i = 0; i < top; i++) {
|
1004
|
+
np[i] = mont->N.d[i];
|
1005
|
+
}
|
1006
|
+
|
1007
|
+
bn_scatter5(tmp.d, top, powerbuf, 0);
|
1008
|
+
bn_scatter5(am.d, am.top, powerbuf, 1);
|
1009
|
+
bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);
|
1010
|
+
bn_scatter5(tmp.d, top, powerbuf, 2);
|
1011
|
+
|
1012
|
+
// same as above, but uses squaring for 1/2 of operations
|
1013
|
+
for (i = 4; i < 32; i *= 2) {
|
1014
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1015
|
+
bn_scatter5(tmp.d, top, powerbuf, i);
|
1016
|
+
}
|
1017
|
+
for (i = 3; i < 8; i += 2) {
|
1018
|
+
int j;
|
1019
|
+
bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
|
1020
|
+
bn_scatter5(tmp.d, top, powerbuf, i);
|
1021
|
+
for (j = 2 * i; j < 32; j *= 2) {
|
1022
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1023
|
+
bn_scatter5(tmp.d, top, powerbuf, j);
|
1024
|
+
}
|
1025
|
+
}
|
1026
|
+
for (; i < 16; i += 2) {
|
1027
|
+
bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
|
1028
|
+
bn_scatter5(tmp.d, top, powerbuf, i);
|
1029
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1030
|
+
bn_scatter5(tmp.d, top, powerbuf, 2 * i);
|
1031
|
+
}
|
1032
|
+
for (; i < 32; i += 2) {
|
1033
|
+
bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
|
1034
|
+
bn_scatter5(tmp.d, top, powerbuf, i);
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
bits--;
|
1038
|
+
for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--) {
|
1039
|
+
wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
|
1040
|
+
}
|
1041
|
+
bn_gather5(tmp.d, top, powerbuf, wvalue);
|
1042
|
+
|
1043
|
+
// At this point |bits| is 4 mod 5 and at least -1. (|bits| is the first bit
|
1044
|
+
// that has not been read yet.)
|
1045
|
+
assert(bits >= -1 && (bits == -1 || bits % 5 == 4));
|
1046
|
+
|
1047
|
+
// Scan the exponent one window at a time starting from the most
|
1048
|
+
// significant bits.
|
1049
|
+
if (top & 7) {
|
1050
|
+
while (bits >= 0) {
|
1051
|
+
for (wvalue = 0, i = 0; i < 5; i++, bits--) {
|
1052
|
+
wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
|
1053
|
+
}
|
1054
|
+
|
1055
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1056
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1057
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1058
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1059
|
+
bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
|
1060
|
+
bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);
|
1061
|
+
}
|
1062
|
+
} else {
|
1063
|
+
const uint8_t *p_bytes = (const uint8_t *)p->d;
|
1064
|
+
int max_bits = p->top * BN_BITS2;
|
1065
|
+
assert(bits < max_bits);
|
1066
|
+
// |p = 0| has been handled as a special case, so |max_bits| is at least
|
1067
|
+
// one word.
|
1068
|
+
assert(max_bits >= 64);
|
1069
|
+
|
1070
|
+
// If the first bit to be read lands in the last byte, unroll the first
|
1071
|
+
// iteration to avoid reading past the bounds of |p->d|. (After the first
|
1072
|
+
// iteration, we are guaranteed to be past the last byte.) Note |bits|
|
1073
|
+
// here is the top bit, inclusive.
|
1074
|
+
if (bits - 4 >= max_bits - 8) {
|
1075
|
+
// Read five bits from |bits-4| through |bits|, inclusive.
|
1076
|
+
wvalue = p_bytes[p->top * BN_BYTES - 1];
|
1077
|
+
wvalue >>= (bits - 4) & 7;
|
1078
|
+
wvalue &= 0x1f;
|
1079
|
+
bits -= 5;
|
1080
|
+
bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);
|
1081
|
+
}
|
1082
|
+
while (bits >= 0) {
|
1083
|
+
// Read five bits from |bits-4| through |bits|, inclusive.
|
1084
|
+
int first_bit = bits - 4;
|
1085
|
+
uint16_t val;
|
1086
|
+
OPENSSL_memcpy(&val, p_bytes + (first_bit >> 3), sizeof(val));
|
1087
|
+
val >>= first_bit & 7;
|
1088
|
+
val &= 0x1f;
|
1089
|
+
bits -= 5;
|
1090
|
+
bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, val);
|
1091
|
+
}
|
1092
|
+
}
|
1093
|
+
|
1094
|
+
ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);
|
1095
|
+
tmp.top = top;
|
1096
|
+
bn_correct_top(&tmp);
|
1097
|
+
if (ret) {
|
1098
|
+
if (!BN_copy(rr, &tmp)) {
|
1099
|
+
ret = 0;
|
1100
|
+
}
|
1101
|
+
goto err; // non-zero ret means it's not error
|
1102
|
+
}
|
1103
|
+
} else
|
1104
|
+
#endif
|
1105
|
+
{
|
1106
|
+
if (!copy_to_prebuf(&tmp, top, powerbuf, 0, window) ||
|
1107
|
+
!copy_to_prebuf(&am, top, powerbuf, 1, window)) {
|
1108
|
+
goto err;
|
1109
|
+
}
|
1110
|
+
|
1111
|
+
// If the window size is greater than 1, then calculate
|
1112
|
+
// val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1)
|
1113
|
+
// (even powers could instead be computed as (a^(i/2))^2
|
1114
|
+
// to use the slight performance advantage of sqr over mul).
|
1115
|
+
if (window > 1) {
|
1116
|
+
if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx) ||
|
1117
|
+
!copy_to_prebuf(&tmp, top, powerbuf, 2, window)) {
|
1118
|
+
goto err;
|
1119
|
+
}
|
1120
|
+
for (i = 3; i < numPowers; i++) {
|
1121
|
+
// Calculate a^i = a^(i-1) * a
|
1122
|
+
if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx) ||
|
1123
|
+
!copy_to_prebuf(&tmp, top, powerbuf, i, window)) {
|
1124
|
+
goto err;
|
1125
|
+
}
|
1126
|
+
}
|
1127
|
+
}
|
1128
|
+
|
1129
|
+
bits--;
|
1130
|
+
for (wvalue = 0, i = bits % window; i >= 0; i--, bits--) {
|
1131
|
+
wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
|
1132
|
+
}
|
1133
|
+
if (!copy_from_prebuf(&tmp, top, powerbuf, wvalue, window)) {
|
1134
|
+
goto err;
|
1135
|
+
}
|
1136
|
+
|
1137
|
+
// Scan the exponent one window at a time starting from the most
|
1138
|
+
// significant bits.
|
1139
|
+
while (bits >= 0) {
|
1140
|
+
wvalue = 0; // The 'value' of the window
|
1141
|
+
|
1142
|
+
// Scan the window, squaring the result as we go
|
1143
|
+
for (i = 0; i < window; i++, bits--) {
|
1144
|
+
if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx)) {
|
1145
|
+
goto err;
|
1146
|
+
}
|
1147
|
+
wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
|
1148
|
+
}
|
1149
|
+
|
1150
|
+
// Fetch the appropriate pre-computed value from the pre-buf
|
1151
|
+
if (!copy_from_prebuf(&am, top, powerbuf, wvalue, window)) {
|
1152
|
+
goto err;
|
1153
|
+
}
|
1154
|
+
|
1155
|
+
// Multiply the result into the intermediate result
|
1156
|
+
if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx)) {
|
1157
|
+
goto err;
|
1158
|
+
}
|
1159
|
+
}
|
1160
|
+
}
|
1161
|
+
|
1162
|
+
// Convert the final result from montgomery to standard format
|
1163
|
+
if (!BN_from_montgomery(rr, &tmp, mont, ctx)) {
|
1164
|
+
goto err;
|
1165
|
+
}
|
1166
|
+
ret = 1;
|
1167
|
+
|
1168
|
+
err:
|
1169
|
+
BN_MONT_CTX_free(new_mont);
|
1170
|
+
BN_clear_free(new_a);
|
1171
|
+
OPENSSL_free(powerbufFree);
|
1172
|
+
return (ret);
|
1173
|
+
}
|
1174
|
+
|
1175
|
+
int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
|
1176
|
+
const BIGNUM *m, BN_CTX *ctx,
|
1177
|
+
const BN_MONT_CTX *mont) {
|
1178
|
+
BIGNUM a_bignum;
|
1179
|
+
BN_init(&a_bignum);
|
1180
|
+
|
1181
|
+
int ret = 0;
|
1182
|
+
|
1183
|
+
if (!BN_set_word(&a_bignum, a)) {
|
1184
|
+
OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
|
1185
|
+
goto err;
|
1186
|
+
}
|
1187
|
+
|
1188
|
+
ret = BN_mod_exp_mont(rr, &a_bignum, p, m, ctx, mont);
|
1189
|
+
|
1190
|
+
err:
|
1191
|
+
BN_free(&a_bignum);
|
1192
|
+
|
1193
|
+
return ret;
|
1194
|
+
}
|
1195
|
+
|
1196
|
+
#define TABLE_SIZE 32
|
1197
|
+
|
1198
|
+
int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
|
1199
|
+
const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
|
1200
|
+
BN_CTX *ctx, const BN_MONT_CTX *mont) {
|
1201
|
+
BIGNUM tmp;
|
1202
|
+
BN_init(&tmp);
|
1203
|
+
|
1204
|
+
int ret = 0;
|
1205
|
+
BN_MONT_CTX *new_mont = NULL;
|
1206
|
+
|
1207
|
+
// Allocate a montgomery context if it was not supplied by the caller.
|
1208
|
+
if (mont == NULL) {
|
1209
|
+
new_mont = BN_MONT_CTX_new();
|
1210
|
+
if (new_mont == NULL || !BN_MONT_CTX_set(new_mont, m, ctx)) {
|
1211
|
+
goto err;
|
1212
|
+
}
|
1213
|
+
mont = new_mont;
|
1214
|
+
}
|
1215
|
+
|
1216
|
+
// BN_mod_mul_montgomery removes one Montgomery factor, so passing one
|
1217
|
+
// Montgomery-encoded and one non-Montgomery-encoded value gives a
|
1218
|
+
// non-Montgomery-encoded result.
|
1219
|
+
if (!BN_mod_exp_mont(rr, a1, p1, m, ctx, mont) ||
|
1220
|
+
!BN_mod_exp_mont(&tmp, a2, p2, m, ctx, mont) ||
|
1221
|
+
!BN_to_montgomery(rr, rr, mont, ctx) ||
|
1222
|
+
!BN_mod_mul_montgomery(rr, rr, &tmp, mont, ctx)) {
|
1223
|
+
goto err;
|
1224
|
+
}
|
1225
|
+
|
1226
|
+
ret = 1;
|
1227
|
+
|
1228
|
+
err:
|
1229
|
+
BN_MONT_CTX_free(new_mont);
|
1230
|
+
BN_free(&tmp);
|
1231
|
+
|
1232
|
+
return ret;
|
1233
|
+
}
|