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.

Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -3
  3. data/src/core/ext/filters/max_age/max_age_filter.cc +3 -0
  4. data/src/core/lib/surface/version.cc +1 -1
  5. data/src/ruby/lib/grpc/version.rb +1 -1
  6. data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +1100 -0
  7. data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +138 -0
  8. data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +112 -0
  9. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +375 -0
  10. data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +537 -0
  11. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +370 -0
  12. data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +269 -0
  13. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +239 -0
  14. data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +303 -0
  15. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +726 -0
  16. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +1233 -0
  17. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +627 -0
  18. data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +715 -0
  19. data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +146 -0
  20. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +430 -0
  21. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +207 -0
  22. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +869 -0
  23. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +894 -0
  24. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +283 -0
  25. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +254 -0
  26. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +307 -0
  27. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +502 -0
  28. data/third_party/boringssl/crypto/fipsmodule/cipher/aead.c +284 -0
  29. data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +613 -0
  30. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +1437 -0
  31. data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +233 -0
  32. data/third_party/boringssl/crypto/fipsmodule/des/des.c +785 -0
  33. data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +256 -0
  34. data/third_party/boringssl/crypto/fipsmodule/digest/digests.c +280 -0
  35. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +842 -0
  36. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +517 -0
  37. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +304 -0
  38. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +404 -0
  39. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +1165 -0
  40. data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +1708 -0
  41. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +561 -0
  42. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +1113 -0
  43. data/third_party/boringssl/crypto/fipsmodule/ec/util-64.c +109 -0
  44. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +456 -0
  45. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +444 -0
  46. data/third_party/boringssl/crypto/fipsmodule/hmac/hmac.c +228 -0
  47. data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +254 -0
  48. data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +298 -0
  49. data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +212 -0
  50. data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +230 -0
  51. data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +219 -0
  52. data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +1074 -0
  53. data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +95 -0
  54. data/third_party/boringssl/crypto/fipsmodule/modes/polyval.c +91 -0
  55. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +200 -0
  56. data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +358 -0
  57. data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +302 -0
  58. data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +263 -0
  59. data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +692 -0
  60. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +855 -0
  61. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +1061 -0
  62. data/third_party/boringssl/crypto/fipsmodule/sha/sha1-altivec.c +361 -0
  63. data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +375 -0
  64. data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +337 -0
  65. data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +608 -0
  66. metadata +62 -2
@@ -0,0 +1,302 @@
1
+ /* Copyright (c) 2014, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #if !defined(_GNU_SOURCE)
16
+ #define _GNU_SOURCE // needed for syscall() on Linux.
17
+ #endif
18
+
19
+ #include <openssl/rand.h>
20
+
21
+ #if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_FUCHSIA) && \
22
+ !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && !defined(OPENSSL_TRUSTY)
23
+
24
+ #include <assert.h>
25
+ #include <errno.h>
26
+ #include <fcntl.h>
27
+ #include <stdio.h>
28
+ #include <string.h>
29
+ #include <unistd.h>
30
+
31
+ #if defined(OPENSSL_LINUX)
32
+ #if defined(BORINGSSL_FIPS)
33
+ #include <linux/random.h>
34
+ #include <sys/ioctl.h>
35
+ #endif
36
+ #include <sys/syscall.h>
37
+ #endif
38
+
39
+ #include <openssl/thread.h>
40
+ #include <openssl/mem.h>
41
+
42
+ #include "internal.h"
43
+ #include "../delocate.h"
44
+ #include "../../internal.h"
45
+
46
+
47
+ #if defined(OPENSSL_LINUX)
48
+
49
+ #if defined(OPENSSL_X86_64)
50
+ #define EXPECTED_NR_getrandom 318
51
+ #elif defined(OPENSSL_X86)
52
+ #define EXPECTED_NR_getrandom 355
53
+ #elif defined(OPENSSL_AARCH64)
54
+ #define EXPECTED_NR_getrandom 278
55
+ #elif defined(OPENSSL_ARM)
56
+ #define EXPECTED_NR_getrandom 384
57
+ #elif defined(OPENSSL_PPC64LE)
58
+ #define EXPECTED_NR_getrandom 359
59
+ #endif
60
+
61
+ #if defined(EXPECTED_NR_getrandom)
62
+ #define USE_NR_getrandom
63
+
64
+ #if defined(__NR_getrandom)
65
+
66
+ #if __NR_getrandom != EXPECTED_NR_getrandom
67
+ #error "system call number for getrandom is not the expected value"
68
+ #endif
69
+
70
+ #else // __NR_getrandom
71
+
72
+ #define __NR_getrandom EXPECTED_NR_getrandom
73
+
74
+ #endif // __NR_getrandom
75
+
76
+ #endif // EXPECTED_NR_getrandom
77
+
78
+ #if !defined(GRND_NONBLOCK)
79
+ #define GRND_NONBLOCK 1
80
+ #endif
81
+
82
+ #endif // OPENSSL_LINUX
83
+
84
+ // rand_lock is used to protect the |*_requested| variables.
85
+ DEFINE_STATIC_MUTEX(rand_lock);
86
+
87
+ // The following constants are magic values of |urandom_fd|.
88
+ static const int kUnset = 0;
89
+ static const int kHaveGetrandom = -3;
90
+
91
+ // urandom_fd_requested is set by |RAND_set_urandom_fd|. It's protected by
92
+ // |rand_lock|.
93
+ DEFINE_BSS_GET(int, urandom_fd_requested);
94
+
95
+ // urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|.
96
+ DEFINE_BSS_GET(int, urandom_fd);
97
+
98
+ DEFINE_STATIC_ONCE(rand_once);
99
+
100
+ #if defined(USE_NR_getrandom) || defined(BORINGSSL_FIPS)
101
+ // message writes |msg| to stderr. We use this because referencing |stderr|
102
+ // with |fprintf| generates relocations, which is a problem inside the FIPS
103
+ // module.
104
+ static void message(const char *msg) {
105
+ ssize_t r;
106
+ do {
107
+ r = write(2, msg, strlen(msg));
108
+ } while (r == -1 && errno == EINTR);
109
+ }
110
+ #endif
111
+
112
+ // init_once initializes the state of this module to values previously
113
+ // requested. This is the only function that modifies |urandom_fd| and
114
+ // |urandom_buffering|, whose values may be read safely after calling the
115
+ // once.
116
+ static void init_once(void) {
117
+ CRYPTO_STATIC_MUTEX_lock_read(rand_lock_bss_get());
118
+ int fd = *urandom_fd_requested_bss_get();
119
+ CRYPTO_STATIC_MUTEX_unlock_read(rand_lock_bss_get());
120
+
121
+ #if defined(USE_NR_getrandom)
122
+ uint8_t dummy;
123
+ long getrandom_ret =
124
+ syscall(__NR_getrandom, &dummy, sizeof(dummy), GRND_NONBLOCK);
125
+
126
+ if (getrandom_ret == 1) {
127
+ *urandom_fd_bss_get() = kHaveGetrandom;
128
+ return;
129
+ } else if (getrandom_ret == -1 && errno == EAGAIN) {
130
+ message(
131
+ "getrandom indicates that the entropy pool has not been initialized. "
132
+ "Rather than continue with poor entropy, this process will block until "
133
+ "entropy is available.\n");
134
+
135
+ do {
136
+ getrandom_ret =
137
+ syscall(__NR_getrandom, &dummy, sizeof(dummy), 0 /* no flags */);
138
+ } while (getrandom_ret == -1 && errno == EINTR);
139
+
140
+ if (getrandom_ret == 1) {
141
+ *urandom_fd_bss_get() = kHaveGetrandom;
142
+ return;
143
+ }
144
+ }
145
+ #endif // USE_NR_getrandom
146
+
147
+ if (fd == kUnset) {
148
+ do {
149
+ fd = open("/dev/urandom", O_RDONLY);
150
+ } while (fd == -1 && errno == EINTR);
151
+ }
152
+
153
+ if (fd < 0) {
154
+ abort();
155
+ }
156
+
157
+ assert(kUnset == 0);
158
+ if (fd == kUnset) {
159
+ // Because we want to keep |urandom_fd| in the BSS, we have to initialise
160
+ // it to zero. But zero is a valid file descriptor too. Thus if open
161
+ // returns zero for /dev/urandom, we dup it to get a non-zero number.
162
+ fd = dup(fd);
163
+ close(kUnset);
164
+
165
+ if (fd <= 0) {
166
+ abort();
167
+ }
168
+ }
169
+
170
+ #if defined(BORINGSSL_FIPS)
171
+ // In FIPS mode we ensure that the kernel has sufficient entropy before
172
+ // continuing. This is automatically handled by getrandom, which requires
173
+ // that the entropy pool has been initialised, but for urandom we have to
174
+ // poll.
175
+ for (;;) {
176
+ int entropy_bits;
177
+ if (ioctl(fd, RNDGETENTCNT, &entropy_bits)) {
178
+ message(
179
+ "RNDGETENTCNT on /dev/urandom failed. We cannot continue in this "
180
+ "case when in FIPS mode.\n");
181
+ abort();
182
+ }
183
+
184
+ static const int kBitsNeeded = 256;
185
+ if (entropy_bits >= kBitsNeeded) {
186
+ break;
187
+ }
188
+
189
+ usleep(250000);
190
+ }
191
+ #endif
192
+
193
+ int flags = fcntl(fd, F_GETFD);
194
+ if (flags == -1) {
195
+ // Native Client doesn't implement |fcntl|.
196
+ if (errno != ENOSYS) {
197
+ abort();
198
+ }
199
+ } else {
200
+ flags |= FD_CLOEXEC;
201
+ if (fcntl(fd, F_SETFD, flags) == -1) {
202
+ abort();
203
+ }
204
+ }
205
+ *urandom_fd_bss_get() = fd;
206
+ }
207
+
208
+ void RAND_set_urandom_fd(int fd) {
209
+ fd = dup(fd);
210
+ if (fd < 0) {
211
+ abort();
212
+ }
213
+
214
+ assert(kUnset == 0);
215
+ if (fd == kUnset) {
216
+ // Because we want to keep |urandom_fd| in the BSS, we have to initialise
217
+ // it to zero. But zero is a valid file descriptor too. Thus if dup
218
+ // returned zero we dup it again to get a non-zero number.
219
+ fd = dup(fd);
220
+ close(kUnset);
221
+
222
+ if (fd <= 0) {
223
+ abort();
224
+ }
225
+ }
226
+
227
+ CRYPTO_STATIC_MUTEX_lock_write(rand_lock_bss_get());
228
+ *urandom_fd_requested_bss_get() = fd;
229
+ CRYPTO_STATIC_MUTEX_unlock_write(rand_lock_bss_get());
230
+
231
+ CRYPTO_once(rand_once_bss_get(), init_once);
232
+ if (*urandom_fd_bss_get() == kHaveGetrandom) {
233
+ close(fd);
234
+ } else if (*urandom_fd_bss_get() != fd) {
235
+ abort(); // Already initialized.
236
+ }
237
+ }
238
+
239
+ #if defined(USE_NR_getrandom) && defined(OPENSSL_MSAN)
240
+ void __msan_unpoison(void *, size_t);
241
+ #endif
242
+
243
+ // fill_with_entropy writes |len| bytes of entropy into |out|. It returns one
244
+ // on success and zero on error.
245
+ static char fill_with_entropy(uint8_t *out, size_t len) {
246
+ while (len > 0) {
247
+ ssize_t r;
248
+
249
+ if (*urandom_fd_bss_get() == kHaveGetrandom) {
250
+ #if defined(USE_NR_getrandom)
251
+ do {
252
+ r = syscall(__NR_getrandom, out, len, 0 /* no flags */);
253
+ } while (r == -1 && errno == EINTR);
254
+
255
+ #if defined(OPENSSL_MSAN)
256
+ if (r > 0) {
257
+ // MSAN doesn't recognise |syscall| and thus doesn't notice that we
258
+ // have initialised the output buffer.
259
+ __msan_unpoison(out, r);
260
+ }
261
+ #endif // OPENSSL_MSAN
262
+
263
+ #else // USE_NR_getrandom
264
+ abort();
265
+ #endif
266
+ } else {
267
+ do {
268
+ r = read(*urandom_fd_bss_get(), out, len);
269
+ } while (r == -1 && errno == EINTR);
270
+ }
271
+
272
+ if (r <= 0) {
273
+ return 0;
274
+ }
275
+ out += r;
276
+ len -= r;
277
+ }
278
+
279
+ return 1;
280
+ }
281
+
282
+ // CRYPTO_sysrand puts |requested| random bytes into |out|.
283
+ void CRYPTO_sysrand(uint8_t *out, size_t requested) {
284
+ if (requested == 0) {
285
+ return;
286
+ }
287
+
288
+ CRYPTO_once(rand_once_bss_get(), init_once);
289
+
290
+ if (!fill_with_entropy(out, requested)) {
291
+ abort();
292
+ }
293
+
294
+ #if defined(BORINGSSL_FIPS_BREAK_CRNG)
295
+ // This breaks the "continuous random number generator test" defined in FIPS
296
+ // 140-2, section 4.9.2, and implemented in rand_get_seed().
297
+ OPENSSL_memset(out, 0, requested);
298
+ #endif
299
+ }
300
+
301
+ #endif /* !OPENSSL_WINDOWS && !defined(OPENSSL_FUCHSIA) && \
302
+ !BORINGSSL_UNSAFE_DETERMINISTIC_MODE && !OPENSSL_TRUSTY */
@@ -0,0 +1,263 @@
1
+ /* ====================================================================
2
+ * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. All advertising materials mentioning features or use of this
17
+ * software must display the following acknowledgment:
18
+ * "This product includes software developed by the OpenSSL Project
19
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20
+ *
21
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22
+ * endorse or promote products derived from this software without
23
+ * prior written permission. For written permission, please contact
24
+ * openssl-core@openssl.org.
25
+ *
26
+ * 5. Products derived from this software may not be called "OpenSSL"
27
+ * nor may "OpenSSL" appear in their names without prior written
28
+ * permission of the OpenSSL Project.
29
+ *
30
+ * 6. Redistributions of any form whatsoever must retain the following
31
+ * acknowledgment:
32
+ * "This product includes software developed by the OpenSSL Project
33
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34
+ *
35
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
47
+ * ====================================================================
48
+ *
49
+ * This product includes cryptographic software written by Eric Young
50
+ * (eay@cryptsoft.com). This product includes software written by Tim
51
+ * Hudson (tjh@cryptsoft.com).
52
+ *
53
+ * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
54
+ * All rights reserved.
55
+ *
56
+ * This package is an SSL implementation written
57
+ * by Eric Young (eay@cryptsoft.com).
58
+ * The implementation was written so as to conform with Netscapes SSL.
59
+ *
60
+ * This library is free for commercial and non-commercial use as long as
61
+ * the following conditions are aheared to. The following conditions
62
+ * apply to all code found in this distribution, be it the RC4, RSA,
63
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
64
+ * included with this distribution is covered by the same copyright terms
65
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
66
+ *
67
+ * Copyright remains Eric Young's, and as such any Copyright notices in
68
+ * the code are not to be removed.
69
+ * If this package is used in a product, Eric Young should be given attribution
70
+ * as the author of the parts of the library used.
71
+ * This can be in the form of a textual message at program startup or
72
+ * in documentation (online or textual) provided with the package.
73
+ *
74
+ * Redistribution and use in source and binary forms, with or without
75
+ * modification, are permitted provided that the following conditions
76
+ * are met:
77
+ * 1. Redistributions of source code must retain the copyright
78
+ * notice, this list of conditions and the following disclaimer.
79
+ * 2. Redistributions in binary form must reproduce the above copyright
80
+ * notice, this list of conditions and the following disclaimer in the
81
+ * documentation and/or other materials provided with the distribution.
82
+ * 3. All advertising materials mentioning features or use of this software
83
+ * must display the following acknowledgement:
84
+ * "This product includes cryptographic software written by
85
+ * Eric Young (eay@cryptsoft.com)"
86
+ * The word 'cryptographic' can be left out if the rouines from the library
87
+ * being used are not cryptographic related :-).
88
+ * 4. If you include any Windows specific code (or a derivative thereof) from
89
+ * the apps directory (application code) you must include an acknowledgement:
90
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
91
+ *
92
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
93
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
96
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102
+ * SUCH DAMAGE.
103
+ *
104
+ * The licence and distribution terms for any publically available version or
105
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
106
+ * copied and put under another distribution licence
107
+ * [including the GNU Public Licence.] */
108
+
109
+ #include <openssl/rsa.h>
110
+
111
+ #include <string.h>
112
+
113
+ #include <openssl/bn.h>
114
+ #include <openssl/mem.h>
115
+ #include <openssl/err.h>
116
+
117
+ #include "internal.h"
118
+ #include "../../internal.h"
119
+
120
+
121
+ #define BN_BLINDING_COUNTER 32
122
+
123
+ struct bn_blinding_st {
124
+ BIGNUM *A; // The base blinding factor, Montgomery-encoded.
125
+ BIGNUM *Ai; // The inverse of the blinding factor, Montgomery-encoded.
126
+ unsigned counter;
127
+ };
128
+
129
+ static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e,
130
+ const BN_MONT_CTX *mont, BN_CTX *ctx);
131
+
132
+ BN_BLINDING *BN_BLINDING_new(void) {
133
+ BN_BLINDING *ret = OPENSSL_malloc(sizeof(BN_BLINDING));
134
+ if (ret == NULL) {
135
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
136
+ return NULL;
137
+ }
138
+ OPENSSL_memset(ret, 0, sizeof(BN_BLINDING));
139
+
140
+ ret->A = BN_new();
141
+ if (ret->A == NULL) {
142
+ goto err;
143
+ }
144
+
145
+ ret->Ai = BN_new();
146
+ if (ret->Ai == NULL) {
147
+ goto err;
148
+ }
149
+
150
+ // The blinding values need to be created before this blinding can be used.
151
+ ret->counter = BN_BLINDING_COUNTER - 1;
152
+
153
+ return ret;
154
+
155
+ err:
156
+ BN_BLINDING_free(ret);
157
+ return NULL;
158
+ }
159
+
160
+ void BN_BLINDING_free(BN_BLINDING *r) {
161
+ if (r == NULL) {
162
+ return;
163
+ }
164
+
165
+ BN_free(r->A);
166
+ BN_free(r->Ai);
167
+ OPENSSL_free(r);
168
+ }
169
+
170
+ static int bn_blinding_update(BN_BLINDING *b, const BIGNUM *e,
171
+ const BN_MONT_CTX *mont, BN_CTX *ctx) {
172
+ if (++b->counter == BN_BLINDING_COUNTER) {
173
+ // re-create blinding parameters
174
+ if (!bn_blinding_create_param(b, e, mont, ctx)) {
175
+ goto err;
176
+ }
177
+ b->counter = 0;
178
+ } else {
179
+ if (!BN_mod_mul_montgomery(b->A, b->A, b->A, mont, ctx) ||
180
+ !BN_mod_mul_montgomery(b->Ai, b->Ai, b->Ai, mont, ctx)) {
181
+ goto err;
182
+ }
183
+ }
184
+
185
+ return 1;
186
+
187
+ err:
188
+ // |A| and |Ai| may be in an inconsistent state so they both need to be
189
+ // replaced the next time this blinding is used. Note that this is only
190
+ // sufficient because support for |BN_BLINDING_NO_UPDATE| and
191
+ // |BN_BLINDING_NO_RECREATE| was previously dropped.
192
+ b->counter = BN_BLINDING_COUNTER - 1;
193
+
194
+ return 0;
195
+ }
196
+
197
+ int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, const BIGNUM *e,
198
+ const BN_MONT_CTX *mont, BN_CTX *ctx) {
199
+ // |n| is not Montgomery-encoded and |b->A| is. |BN_mod_mul_montgomery|
200
+ // cancels one Montgomery factor, so the resulting value of |n| is unencoded.
201
+ if (!bn_blinding_update(b, e, mont, ctx) ||
202
+ !BN_mod_mul_montgomery(n, n, b->A, mont, ctx)) {
203
+ return 0;
204
+ }
205
+
206
+ return 1;
207
+ }
208
+
209
+ int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_MONT_CTX *mont,
210
+ BN_CTX *ctx) {
211
+ // |n| is not Montgomery-encoded and |b->A| is. |BN_mod_mul_montgomery|
212
+ // cancels one Montgomery factor, so the resulting value of |n| is unencoded.
213
+ return BN_mod_mul_montgomery(n, n, b->Ai, mont, ctx);
214
+ }
215
+
216
+ static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e,
217
+ const BN_MONT_CTX *mont, BN_CTX *ctx) {
218
+ int retry_counter = 32;
219
+
220
+ do {
221
+ if (!BN_rand_range_ex(b->A, 1, &mont->N)) {
222
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
223
+ return 0;
224
+ }
225
+
226
+ // |BN_from_montgomery| + |BN_mod_inverse_blinded| is equivalent to, but
227
+ // more efficient than, |BN_mod_inverse_blinded| + |BN_to_montgomery|.
228
+ if (!BN_from_montgomery(b->Ai, b->A, mont, ctx)) {
229
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
230
+ return 0;
231
+ }
232
+
233
+ int no_inverse;
234
+ if (BN_mod_inverse_blinded(b->Ai, &no_inverse, b->Ai, mont, ctx)) {
235
+ break;
236
+ }
237
+
238
+ if (!no_inverse) {
239
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
240
+ return 0;
241
+ }
242
+
243
+ // For reasonably-sized RSA keys, it should almost never be the case that a
244
+ // random value doesn't have an inverse.
245
+ if (retry_counter-- == 0) {
246
+ OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
247
+ return 0;
248
+ }
249
+ ERR_clear_error();
250
+ } while (1);
251
+
252
+ if (!BN_mod_exp_mont(b->A, b->A, e, &mont->N, ctx, mont)) {
253
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
254
+ return 0;
255
+ }
256
+
257
+ if (!BN_to_montgomery(b->A, b->A, mont, ctx)) {
258
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
259
+ return 0;
260
+ }
261
+
262
+ return 1;
263
+ }