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,370 @@
|
|
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
|
+
#include <openssl/bn.h>
|
58
|
+
|
59
|
+
#include <limits.h>
|
60
|
+
#include <string.h>
|
61
|
+
|
62
|
+
#include <openssl/err.h>
|
63
|
+
#include <openssl/mem.h>
|
64
|
+
|
65
|
+
#include "internal.h"
|
66
|
+
#include "../delocate.h"
|
67
|
+
|
68
|
+
|
69
|
+
BIGNUM *BN_new(void) {
|
70
|
+
BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
|
71
|
+
|
72
|
+
if (bn == NULL) {
|
73
|
+
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
74
|
+
return NULL;
|
75
|
+
}
|
76
|
+
|
77
|
+
OPENSSL_memset(bn, 0, sizeof(BIGNUM));
|
78
|
+
bn->flags = BN_FLG_MALLOCED;
|
79
|
+
|
80
|
+
return bn;
|
81
|
+
}
|
82
|
+
|
83
|
+
void BN_init(BIGNUM *bn) {
|
84
|
+
OPENSSL_memset(bn, 0, sizeof(BIGNUM));
|
85
|
+
}
|
86
|
+
|
87
|
+
void BN_free(BIGNUM *bn) {
|
88
|
+
if (bn == NULL) {
|
89
|
+
return;
|
90
|
+
}
|
91
|
+
|
92
|
+
if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
|
93
|
+
OPENSSL_free(bn->d);
|
94
|
+
}
|
95
|
+
|
96
|
+
if (bn->flags & BN_FLG_MALLOCED) {
|
97
|
+
OPENSSL_free(bn);
|
98
|
+
} else {
|
99
|
+
bn->d = NULL;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
void BN_clear_free(BIGNUM *bn) {
|
104
|
+
char should_free;
|
105
|
+
|
106
|
+
if (bn == NULL) {
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
|
110
|
+
if (bn->d != NULL) {
|
111
|
+
if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
|
112
|
+
OPENSSL_free(bn->d);
|
113
|
+
} else {
|
114
|
+
OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0]));
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
should_free = (bn->flags & BN_FLG_MALLOCED) != 0;
|
119
|
+
if (should_free) {
|
120
|
+
OPENSSL_free(bn);
|
121
|
+
} else {
|
122
|
+
OPENSSL_cleanse(bn, sizeof(BIGNUM));
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
BIGNUM *BN_dup(const BIGNUM *src) {
|
127
|
+
BIGNUM *copy;
|
128
|
+
|
129
|
+
if (src == NULL) {
|
130
|
+
return NULL;
|
131
|
+
}
|
132
|
+
|
133
|
+
copy = BN_new();
|
134
|
+
if (copy == NULL) {
|
135
|
+
return NULL;
|
136
|
+
}
|
137
|
+
|
138
|
+
if (!BN_copy(copy, src)) {
|
139
|
+
BN_free(copy);
|
140
|
+
return NULL;
|
141
|
+
}
|
142
|
+
|
143
|
+
return copy;
|
144
|
+
}
|
145
|
+
|
146
|
+
BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) {
|
147
|
+
if (src == dest) {
|
148
|
+
return dest;
|
149
|
+
}
|
150
|
+
|
151
|
+
if (!bn_wexpand(dest, src->top)) {
|
152
|
+
return NULL;
|
153
|
+
}
|
154
|
+
|
155
|
+
OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->top);
|
156
|
+
|
157
|
+
dest->top = src->top;
|
158
|
+
dest->neg = src->neg;
|
159
|
+
return dest;
|
160
|
+
}
|
161
|
+
|
162
|
+
void BN_clear(BIGNUM *bn) {
|
163
|
+
if (bn->d != NULL) {
|
164
|
+
OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0]));
|
165
|
+
}
|
166
|
+
|
167
|
+
bn->top = 0;
|
168
|
+
bn->neg = 0;
|
169
|
+
}
|
170
|
+
|
171
|
+
DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) {
|
172
|
+
static const BN_ULONG kOneLimbs[1] = { 1 };
|
173
|
+
out->d = (BN_ULONG*) kOneLimbs;
|
174
|
+
out->top = 1;
|
175
|
+
out->dmax = 1;
|
176
|
+
out->neg = 0;
|
177
|
+
out->flags = BN_FLG_STATIC_DATA;
|
178
|
+
}
|
179
|
+
|
180
|
+
// BN_num_bits_word returns the minimum number of bits needed to represent the
|
181
|
+
// value in |l|.
|
182
|
+
unsigned BN_num_bits_word(BN_ULONG l) {
|
183
|
+
static const unsigned char bits[256] = {
|
184
|
+
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
|
185
|
+
5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
186
|
+
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
|
187
|
+
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
188
|
+
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
189
|
+
7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
190
|
+
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
191
|
+
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
192
|
+
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
193
|
+
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
194
|
+
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
|
195
|
+
|
196
|
+
#if defined(OPENSSL_64_BIT)
|
197
|
+
if (l & 0xffffffff00000000L) {
|
198
|
+
if (l & 0xffff000000000000L) {
|
199
|
+
if (l & 0xff00000000000000L) {
|
200
|
+
return (bits[(int)(l >> 56)] + 56);
|
201
|
+
} else {
|
202
|
+
return (bits[(int)(l >> 48)] + 48);
|
203
|
+
}
|
204
|
+
} else {
|
205
|
+
if (l & 0x0000ff0000000000L) {
|
206
|
+
return (bits[(int)(l >> 40)] + 40);
|
207
|
+
} else {
|
208
|
+
return (bits[(int)(l >> 32)] + 32);
|
209
|
+
}
|
210
|
+
}
|
211
|
+
} else
|
212
|
+
#endif
|
213
|
+
{
|
214
|
+
if (l & 0xffff0000L) {
|
215
|
+
if (l & 0xff000000L) {
|
216
|
+
return (bits[(int)(l >> 24L)] + 24);
|
217
|
+
} else {
|
218
|
+
return (bits[(int)(l >> 16L)] + 16);
|
219
|
+
}
|
220
|
+
} else {
|
221
|
+
if (l & 0xff00L) {
|
222
|
+
return (bits[(int)(l >> 8)] + 8);
|
223
|
+
} else {
|
224
|
+
return (bits[(int)(l)]);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
}
|
229
|
+
|
230
|
+
unsigned BN_num_bits(const BIGNUM *bn) {
|
231
|
+
const int max = bn->top - 1;
|
232
|
+
|
233
|
+
if (BN_is_zero(bn)) {
|
234
|
+
return 0;
|
235
|
+
}
|
236
|
+
|
237
|
+
return max*BN_BITS2 + BN_num_bits_word(bn->d[max]);
|
238
|
+
}
|
239
|
+
|
240
|
+
unsigned BN_num_bytes(const BIGNUM *bn) {
|
241
|
+
return (BN_num_bits(bn) + 7) / 8;
|
242
|
+
}
|
243
|
+
|
244
|
+
void BN_zero(BIGNUM *bn) {
|
245
|
+
bn->top = bn->neg = 0;
|
246
|
+
}
|
247
|
+
|
248
|
+
int BN_one(BIGNUM *bn) {
|
249
|
+
return BN_set_word(bn, 1);
|
250
|
+
}
|
251
|
+
|
252
|
+
int BN_set_word(BIGNUM *bn, BN_ULONG value) {
|
253
|
+
if (value == 0) {
|
254
|
+
BN_zero(bn);
|
255
|
+
return 1;
|
256
|
+
}
|
257
|
+
|
258
|
+
if (!bn_wexpand(bn, 1)) {
|
259
|
+
return 0;
|
260
|
+
}
|
261
|
+
|
262
|
+
bn->neg = 0;
|
263
|
+
bn->d[0] = value;
|
264
|
+
bn->top = 1;
|
265
|
+
return 1;
|
266
|
+
}
|
267
|
+
|
268
|
+
int BN_set_u64(BIGNUM *bn, uint64_t value) {
|
269
|
+
#if BN_BITS2 == 64
|
270
|
+
return BN_set_word(bn, value);
|
271
|
+
#elif BN_BITS2 == 32
|
272
|
+
if (value <= BN_MASK2) {
|
273
|
+
return BN_set_word(bn, (BN_ULONG)value);
|
274
|
+
}
|
275
|
+
|
276
|
+
if (!bn_wexpand(bn, 2)) {
|
277
|
+
return 0;
|
278
|
+
}
|
279
|
+
|
280
|
+
bn->neg = 0;
|
281
|
+
bn->d[0] = (BN_ULONG)value;
|
282
|
+
bn->d[1] = (BN_ULONG)(value >> 32);
|
283
|
+
bn->top = 2;
|
284
|
+
return 1;
|
285
|
+
#else
|
286
|
+
#error "BN_BITS2 must be 32 or 64."
|
287
|
+
#endif
|
288
|
+
}
|
289
|
+
|
290
|
+
int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
|
291
|
+
if (!bn_wexpand(bn, num)) {
|
292
|
+
return 0;
|
293
|
+
}
|
294
|
+
OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG));
|
295
|
+
// |bn_wexpand| verified that |num| isn't too large.
|
296
|
+
bn->top = (int)num;
|
297
|
+
bn_correct_top(bn);
|
298
|
+
bn->neg = 0;
|
299
|
+
return 1;
|
300
|
+
}
|
301
|
+
|
302
|
+
int BN_is_negative(const BIGNUM *bn) {
|
303
|
+
return bn->neg != 0;
|
304
|
+
}
|
305
|
+
|
306
|
+
void BN_set_negative(BIGNUM *bn, int sign) {
|
307
|
+
if (sign && !BN_is_zero(bn)) {
|
308
|
+
bn->neg = 1;
|
309
|
+
} else {
|
310
|
+
bn->neg = 0;
|
311
|
+
}
|
312
|
+
}
|
313
|
+
|
314
|
+
int bn_wexpand(BIGNUM *bn, size_t words) {
|
315
|
+
BN_ULONG *a;
|
316
|
+
|
317
|
+
if (words <= (size_t)bn->dmax) {
|
318
|
+
return 1;
|
319
|
+
}
|
320
|
+
|
321
|
+
if (words > (INT_MAX / (4 * BN_BITS2))) {
|
322
|
+
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
|
323
|
+
return 0;
|
324
|
+
}
|
325
|
+
|
326
|
+
if (bn->flags & BN_FLG_STATIC_DATA) {
|
327
|
+
OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
|
328
|
+
return 0;
|
329
|
+
}
|
330
|
+
|
331
|
+
a = OPENSSL_malloc(sizeof(BN_ULONG) * words);
|
332
|
+
if (a == NULL) {
|
333
|
+
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
334
|
+
return 0;
|
335
|
+
}
|
336
|
+
|
337
|
+
OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->top);
|
338
|
+
|
339
|
+
OPENSSL_free(bn->d);
|
340
|
+
bn->d = a;
|
341
|
+
bn->dmax = (int)words;
|
342
|
+
|
343
|
+
return 1;
|
344
|
+
}
|
345
|
+
|
346
|
+
int bn_expand(BIGNUM *bn, size_t bits) {
|
347
|
+
if (bits + BN_BITS2 - 1 < bits) {
|
348
|
+
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
|
349
|
+
return 0;
|
350
|
+
}
|
351
|
+
return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
|
352
|
+
}
|
353
|
+
|
354
|
+
void bn_correct_top(BIGNUM *bn) {
|
355
|
+
BN_ULONG *ftl;
|
356
|
+
int tmp_top = bn->top;
|
357
|
+
|
358
|
+
if (tmp_top > 0) {
|
359
|
+
for (ftl = &(bn->d[tmp_top - 1]); tmp_top > 0; tmp_top--) {
|
360
|
+
if (*(ftl--)) {
|
361
|
+
break;
|
362
|
+
}
|
363
|
+
}
|
364
|
+
bn->top = tmp_top;
|
365
|
+
}
|
366
|
+
|
367
|
+
if (bn->top == 0) {
|
368
|
+
bn->neg = 0;
|
369
|
+
}
|
370
|
+
}
|
@@ -0,0 +1,269 @@
|
|
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
|
+
#include <openssl/bn.h>
|
58
|
+
|
59
|
+
#include <assert.h>
|
60
|
+
#include <limits.h>
|
61
|
+
|
62
|
+
#include "internal.h"
|
63
|
+
|
64
|
+
|
65
|
+
BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
|
66
|
+
size_t num_words;
|
67
|
+
unsigned m;
|
68
|
+
BN_ULONG word = 0;
|
69
|
+
BIGNUM *bn = NULL;
|
70
|
+
|
71
|
+
if (ret == NULL) {
|
72
|
+
ret = bn = BN_new();
|
73
|
+
}
|
74
|
+
|
75
|
+
if (ret == NULL) {
|
76
|
+
return NULL;
|
77
|
+
}
|
78
|
+
|
79
|
+
if (len == 0) {
|
80
|
+
ret->top = 0;
|
81
|
+
return ret;
|
82
|
+
}
|
83
|
+
|
84
|
+
num_words = ((len - 1) / BN_BYTES) + 1;
|
85
|
+
m = (len - 1) % BN_BYTES;
|
86
|
+
if (!bn_wexpand(ret, num_words)) {
|
87
|
+
if (bn) {
|
88
|
+
BN_free(bn);
|
89
|
+
}
|
90
|
+
return NULL;
|
91
|
+
}
|
92
|
+
|
93
|
+
// |bn_wexpand| must check bounds on |num_words| to write it into
|
94
|
+
// |ret->dmax|.
|
95
|
+
assert(num_words <= INT_MAX);
|
96
|
+
ret->top = (int)num_words;
|
97
|
+
ret->neg = 0;
|
98
|
+
|
99
|
+
while (len--) {
|
100
|
+
word = (word << 8) | *(in++);
|
101
|
+
if (m-- == 0) {
|
102
|
+
ret->d[--num_words] = word;
|
103
|
+
word = 0;
|
104
|
+
m = BN_BYTES - 1;
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
// need to call this due to clear byte at top if avoiding having the top bit
|
109
|
+
// set (-ve number)
|
110
|
+
bn_correct_top(ret);
|
111
|
+
return ret;
|
112
|
+
}
|
113
|
+
|
114
|
+
BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
|
115
|
+
BIGNUM *bn = NULL;
|
116
|
+
if (ret == NULL) {
|
117
|
+
bn = BN_new();
|
118
|
+
ret = bn;
|
119
|
+
}
|
120
|
+
|
121
|
+
if (ret == NULL) {
|
122
|
+
return NULL;
|
123
|
+
}
|
124
|
+
|
125
|
+
if (len == 0) {
|
126
|
+
ret->top = 0;
|
127
|
+
ret->neg = 0;
|
128
|
+
return ret;
|
129
|
+
}
|
130
|
+
|
131
|
+
// Reserve enough space in |ret|.
|
132
|
+
size_t num_words = ((len - 1) / BN_BYTES) + 1;
|
133
|
+
if (!bn_wexpand(ret, num_words)) {
|
134
|
+
BN_free(bn);
|
135
|
+
return NULL;
|
136
|
+
}
|
137
|
+
ret->top = num_words;
|
138
|
+
|
139
|
+
// Make sure the top bytes will be zeroed.
|
140
|
+
ret->d[num_words - 1] = 0;
|
141
|
+
|
142
|
+
// We only support little-endian platforms, so we can simply memcpy the
|
143
|
+
// internal representation.
|
144
|
+
OPENSSL_memcpy(ret->d, in, len);
|
145
|
+
|
146
|
+
bn_correct_top(ret);
|
147
|
+
return ret;
|
148
|
+
}
|
149
|
+
|
150
|
+
size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
|
151
|
+
size_t n, i;
|
152
|
+
BN_ULONG l;
|
153
|
+
|
154
|
+
n = i = BN_num_bytes(in);
|
155
|
+
while (i--) {
|
156
|
+
l = in->d[i / BN_BYTES];
|
157
|
+
*(out++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
|
158
|
+
}
|
159
|
+
return n;
|
160
|
+
}
|
161
|
+
|
162
|
+
int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
|
163
|
+
// If we don't have enough space, fail out.
|
164
|
+
size_t num_bytes = BN_num_bytes(in);
|
165
|
+
if (len < num_bytes) {
|
166
|
+
return 0;
|
167
|
+
}
|
168
|
+
|
169
|
+
// We only support little-endian platforms, so we can simply memcpy into the
|
170
|
+
// internal representation.
|
171
|
+
OPENSSL_memcpy(out, in->d, num_bytes);
|
172
|
+
|
173
|
+
// Pad out the rest of the buffer with zeroes.
|
174
|
+
OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
|
175
|
+
|
176
|
+
return 1;
|
177
|
+
}
|
178
|
+
|
179
|
+
// constant_time_select_ulong returns |x| if |v| is 1 and |y| if |v| is 0. Its
|
180
|
+
// behavior is undefined if |v| takes any other value.
|
181
|
+
static BN_ULONG constant_time_select_ulong(int v, BN_ULONG x, BN_ULONG y) {
|
182
|
+
BN_ULONG mask = v;
|
183
|
+
mask--;
|
184
|
+
|
185
|
+
return (~mask & x) | (mask & y);
|
186
|
+
}
|
187
|
+
|
188
|
+
// constant_time_le_size_t returns 1 if |x| <= |y| and 0 otherwise. |x| and |y|
|
189
|
+
// must not have their MSBs set.
|
190
|
+
static int constant_time_le_size_t(size_t x, size_t y) {
|
191
|
+
return ((x - y - 1) >> (sizeof(size_t) * 8 - 1)) & 1;
|
192
|
+
}
|
193
|
+
|
194
|
+
// read_word_padded returns the |i|'th word of |in|, if it is not out of
|
195
|
+
// bounds. Otherwise, it returns 0. It does so without branches on the size of
|
196
|
+
// |in|, however it necessarily does not have the same memory access pattern. If
|
197
|
+
// the access would be out of bounds, it reads the last word of |in|. |in| must
|
198
|
+
// not be zero.
|
199
|
+
static BN_ULONG read_word_padded(const BIGNUM *in, size_t i) {
|
200
|
+
// Read |in->d[i]| if valid. Otherwise, read the last word.
|
201
|
+
BN_ULONG l = in->d[constant_time_select_ulong(
|
202
|
+
constant_time_le_size_t(in->dmax, i), in->dmax - 1, i)];
|
203
|
+
|
204
|
+
// Clamp to zero if above |d->top|.
|
205
|
+
return constant_time_select_ulong(constant_time_le_size_t(in->top, i), 0, l);
|
206
|
+
}
|
207
|
+
|
208
|
+
int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
|
209
|
+
// Special case for |in| = 0. Just branch as the probability is negligible.
|
210
|
+
if (BN_is_zero(in)) {
|
211
|
+
OPENSSL_memset(out, 0, len);
|
212
|
+
return 1;
|
213
|
+
}
|
214
|
+
|
215
|
+
// Check if the integer is too big. This case can exit early in non-constant
|
216
|
+
// time.
|
217
|
+
if ((size_t)in->top > (len + (BN_BYTES - 1)) / BN_BYTES) {
|
218
|
+
return 0;
|
219
|
+
}
|
220
|
+
if ((len % BN_BYTES) != 0) {
|
221
|
+
BN_ULONG l = read_word_padded(in, len / BN_BYTES);
|
222
|
+
if (l >> (8 * (len % BN_BYTES)) != 0) {
|
223
|
+
return 0;
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
// Write the bytes out one by one. Serialization is done without branching on
|
228
|
+
// the bits of |in| or on |in->top|, but if the routine would otherwise read
|
229
|
+
// out of bounds, the memory access pattern can't be fixed. However, for an
|
230
|
+
// RSA key of size a multiple of the word size, the probability of BN_BYTES
|
231
|
+
// leading zero octets is low.
|
232
|
+
//
|
233
|
+
// See Falko Stenzke, "Manger's Attack revisited", ICICS 2010.
|
234
|
+
size_t i = len;
|
235
|
+
while (i--) {
|
236
|
+
BN_ULONG l = read_word_padded(in, i / BN_BYTES);
|
237
|
+
*(out++) = (uint8_t)(l >> (8 * (i % BN_BYTES))) & 0xff;
|
238
|
+
}
|
239
|
+
return 1;
|
240
|
+
}
|
241
|
+
|
242
|
+
BN_ULONG BN_get_word(const BIGNUM *bn) {
|
243
|
+
switch (bn->top) {
|
244
|
+
case 0:
|
245
|
+
return 0;
|
246
|
+
case 1:
|
247
|
+
return bn->d[0];
|
248
|
+
default:
|
249
|
+
return BN_MASK2;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
|
254
|
+
switch (bn->top) {
|
255
|
+
case 0:
|
256
|
+
*out = 0;
|
257
|
+
return 1;
|
258
|
+
case 1:
|
259
|
+
*out = bn->d[0];
|
260
|
+
return 1;
|
261
|
+
#if defined(OPENSSL_32_BIT)
|
262
|
+
case 2:
|
263
|
+
*out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32);
|
264
|
+
return 1;
|
265
|
+
#endif
|
266
|
+
default:
|
267
|
+
return 0;
|
268
|
+
}
|
269
|
+
}
|