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,239 @@
|
|
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 <openssl/mem.h>
|
60
|
+
|
61
|
+
#include "internal.h"
|
62
|
+
|
63
|
+
|
64
|
+
int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
|
65
|
+
int i;
|
66
|
+
BN_ULONG t1, t2, *ap, *bp;
|
67
|
+
|
68
|
+
i = a->top - b->top;
|
69
|
+
if (i != 0) {
|
70
|
+
return i;
|
71
|
+
}
|
72
|
+
|
73
|
+
ap = a->d;
|
74
|
+
bp = b->d;
|
75
|
+
for (i = a->top - 1; i >= 0; i--) {
|
76
|
+
t1 = ap[i];
|
77
|
+
t2 = bp[i];
|
78
|
+
if (t1 != t2) {
|
79
|
+
return (t1 > t2) ? 1 : -1;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
return 0;
|
84
|
+
}
|
85
|
+
|
86
|
+
int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
|
87
|
+
int i;
|
88
|
+
int gt, lt;
|
89
|
+
BN_ULONG t1, t2;
|
90
|
+
|
91
|
+
if ((a == NULL) || (b == NULL)) {
|
92
|
+
if (a != NULL) {
|
93
|
+
return -1;
|
94
|
+
} else if (b != NULL) {
|
95
|
+
return 1;
|
96
|
+
} else {
|
97
|
+
return 0;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
if (a->neg != b->neg) {
|
102
|
+
if (a->neg) {
|
103
|
+
return -1;
|
104
|
+
}
|
105
|
+
return 1;
|
106
|
+
}
|
107
|
+
if (a->neg == 0) {
|
108
|
+
gt = 1;
|
109
|
+
lt = -1;
|
110
|
+
} else {
|
111
|
+
gt = -1;
|
112
|
+
lt = 1;
|
113
|
+
}
|
114
|
+
|
115
|
+
if (a->top > b->top) {
|
116
|
+
return gt;
|
117
|
+
}
|
118
|
+
if (a->top < b->top) {
|
119
|
+
return lt;
|
120
|
+
}
|
121
|
+
|
122
|
+
for (i = a->top - 1; i >= 0; i--) {
|
123
|
+
t1 = a->d[i];
|
124
|
+
t2 = b->d[i];
|
125
|
+
if (t1 > t2) {
|
126
|
+
return gt;
|
127
|
+
} if (t1 < t2) {
|
128
|
+
return lt;
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
return 0;
|
133
|
+
}
|
134
|
+
|
135
|
+
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
|
136
|
+
int i;
|
137
|
+
BN_ULONG aa, bb;
|
138
|
+
|
139
|
+
aa = a[n - 1];
|
140
|
+
bb = b[n - 1];
|
141
|
+
if (aa != bb) {
|
142
|
+
return (aa > bb) ? 1 : -1;
|
143
|
+
}
|
144
|
+
|
145
|
+
for (i = n - 2; i >= 0; i--) {
|
146
|
+
aa = a[i];
|
147
|
+
bb = b[i];
|
148
|
+
if (aa != bb) {
|
149
|
+
return (aa > bb) ? 1 : -1;
|
150
|
+
}
|
151
|
+
}
|
152
|
+
return 0;
|
153
|
+
}
|
154
|
+
|
155
|
+
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
|
156
|
+
int n, i;
|
157
|
+
n = cl - 1;
|
158
|
+
|
159
|
+
if (dl < 0) {
|
160
|
+
for (i = dl; i < 0; i++) {
|
161
|
+
if (b[n - i] != 0) {
|
162
|
+
return -1; // a < b
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
if (dl > 0) {
|
167
|
+
for (i = dl; i > 0; i--) {
|
168
|
+
if (a[n + i] != 0) {
|
169
|
+
return 1; // a > b
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
return bn_cmp_words(a, b, cl);
|
175
|
+
}
|
176
|
+
|
177
|
+
int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
|
178
|
+
switch (bn->top) {
|
179
|
+
case 1:
|
180
|
+
return bn->d[0] == w;
|
181
|
+
case 0:
|
182
|
+
return w == 0;
|
183
|
+
default:
|
184
|
+
return 0;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
|
189
|
+
BIGNUM b_bn;
|
190
|
+
BN_init(&b_bn);
|
191
|
+
|
192
|
+
b_bn.d = &b;
|
193
|
+
b_bn.top = b > 0;
|
194
|
+
b_bn.dmax = 1;
|
195
|
+
b_bn.flags = BN_FLG_STATIC_DATA;
|
196
|
+
return BN_cmp(a, &b_bn);
|
197
|
+
}
|
198
|
+
|
199
|
+
int BN_is_zero(const BIGNUM *bn) {
|
200
|
+
return bn->top == 0;
|
201
|
+
}
|
202
|
+
|
203
|
+
int BN_is_one(const BIGNUM *bn) {
|
204
|
+
return bn->neg == 0 && BN_abs_is_word(bn, 1);
|
205
|
+
}
|
206
|
+
|
207
|
+
int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
|
208
|
+
return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
|
209
|
+
}
|
210
|
+
|
211
|
+
int BN_is_odd(const BIGNUM *bn) {
|
212
|
+
return bn->top > 0 && (bn->d[0] & 1) == 1;
|
213
|
+
}
|
214
|
+
|
215
|
+
int BN_is_pow2(const BIGNUM *bn) {
|
216
|
+
if (bn->top == 0 || bn->neg) {
|
217
|
+
return 0;
|
218
|
+
}
|
219
|
+
|
220
|
+
for (int i = 0; i < bn->top - 1; i++) {
|
221
|
+
if (bn->d[i] != 0) {
|
222
|
+
return 0;
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
return 0 == (bn->d[bn->top-1] & (bn->d[bn->top-1] - 1));
|
227
|
+
}
|
228
|
+
|
229
|
+
int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
|
230
|
+
if (a->top != b->top) {
|
231
|
+
return 0;
|
232
|
+
}
|
233
|
+
|
234
|
+
int limbs_are_equal =
|
235
|
+
CRYPTO_memcmp(a->d, b->d, (size_t)a->top * sizeof(a->d[0])) == 0;
|
236
|
+
|
237
|
+
return constant_time_select_int(constant_time_eq_int(a->neg, b->neg),
|
238
|
+
limbs_are_equal, 0);
|
239
|
+
}
|
@@ -0,0 +1,303 @@
|
|
1
|
+
/* Written by Ulf Moeller for the OpenSSL project. */
|
2
|
+
/* ====================================================================
|
3
|
+
* Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
|
4
|
+
*
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
6
|
+
* modification, are permitted provided that the following conditions
|
7
|
+
* are met:
|
8
|
+
*
|
9
|
+
* 1. Redistributions of source code must retain the above copyright
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
11
|
+
*
|
12
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
13
|
+
* notice, this list of conditions and the following disclaimer in
|
14
|
+
* the documentation and/or other materials provided with the
|
15
|
+
* distribution.
|
16
|
+
*
|
17
|
+
* 3. All advertising materials mentioning features or use of this
|
18
|
+
* software must display the following acknowledgment:
|
19
|
+
* "This product includes software developed by the OpenSSL Project
|
20
|
+
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
21
|
+
*
|
22
|
+
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
23
|
+
* endorse or promote products derived from this software without
|
24
|
+
* prior written permission. For written permission, please contact
|
25
|
+
* openssl-core@openssl.org.
|
26
|
+
*
|
27
|
+
* 5. Products derived from this software may not be called "OpenSSL"
|
28
|
+
* nor may "OpenSSL" appear in their names without prior written
|
29
|
+
* permission of the OpenSSL Project.
|
30
|
+
*
|
31
|
+
* 6. Redistributions of any form whatsoever must retain the following
|
32
|
+
* acknowledgment:
|
33
|
+
* "This product includes software developed by the OpenSSL Project
|
34
|
+
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
35
|
+
*
|
36
|
+
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
37
|
+
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
38
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
39
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
40
|
+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
41
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
42
|
+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
43
|
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
44
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
45
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
46
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
47
|
+
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
48
|
+
* ====================================================================
|
49
|
+
*
|
50
|
+
* This product includes cryptographic software written by Eric Young
|
51
|
+
* (eay@cryptsoft.com). This product includes software written by Tim
|
52
|
+
* Hudson (tjh@cryptsoft.com). */
|
53
|
+
|
54
|
+
|
55
|
+
#include <openssl/bn.h>
|
56
|
+
|
57
|
+
#include <string.h>
|
58
|
+
|
59
|
+
#include <openssl/err.h>
|
60
|
+
#include <openssl/mem.h>
|
61
|
+
|
62
|
+
#include "../../internal.h"
|
63
|
+
|
64
|
+
|
65
|
+
// How many bignums are in each "pool item";
|
66
|
+
#define BN_CTX_POOL_SIZE 16
|
67
|
+
// The stack frame info is resizing, set a first-time expansion size;
|
68
|
+
#define BN_CTX_START_FRAMES 32
|
69
|
+
|
70
|
+
// A bundle of bignums that can be linked with other bundles
|
71
|
+
typedef struct bignum_pool_item {
|
72
|
+
// The bignum values
|
73
|
+
BIGNUM vals[BN_CTX_POOL_SIZE];
|
74
|
+
// Linked-list admin
|
75
|
+
struct bignum_pool_item *prev, *next;
|
76
|
+
} BN_POOL_ITEM;
|
77
|
+
|
78
|
+
|
79
|
+
typedef struct bignum_pool {
|
80
|
+
// Linked-list admin
|
81
|
+
BN_POOL_ITEM *head, *current, *tail;
|
82
|
+
// Stack depth and allocation size
|
83
|
+
unsigned used, size;
|
84
|
+
} BN_POOL;
|
85
|
+
|
86
|
+
static void BN_POOL_init(BN_POOL *);
|
87
|
+
static void BN_POOL_finish(BN_POOL *);
|
88
|
+
static BIGNUM *BN_POOL_get(BN_POOL *);
|
89
|
+
static void BN_POOL_release(BN_POOL *, unsigned int);
|
90
|
+
|
91
|
+
|
92
|
+
// BN_STACK
|
93
|
+
|
94
|
+
// A wrapper to manage the "stack frames"
|
95
|
+
typedef struct bignum_ctx_stack {
|
96
|
+
// Array of indexes into the bignum stack
|
97
|
+
unsigned int *indexes;
|
98
|
+
// Number of stack frames, and the size of the allocated array
|
99
|
+
unsigned int depth, size;
|
100
|
+
} BN_STACK;
|
101
|
+
|
102
|
+
static void BN_STACK_init(BN_STACK *);
|
103
|
+
static void BN_STACK_finish(BN_STACK *);
|
104
|
+
static int BN_STACK_push(BN_STACK *, unsigned int);
|
105
|
+
static unsigned int BN_STACK_pop(BN_STACK *);
|
106
|
+
|
107
|
+
|
108
|
+
// BN_CTX
|
109
|
+
|
110
|
+
// The opaque BN_CTX type
|
111
|
+
struct bignum_ctx {
|
112
|
+
// The bignum bundles
|
113
|
+
BN_POOL pool;
|
114
|
+
// The "stack frames", if you will
|
115
|
+
BN_STACK stack;
|
116
|
+
// The number of bignums currently assigned
|
117
|
+
unsigned int used;
|
118
|
+
// Depth of stack overflow
|
119
|
+
int err_stack;
|
120
|
+
// Block "gets" until an "end" (compatibility behaviour)
|
121
|
+
int too_many;
|
122
|
+
};
|
123
|
+
|
124
|
+
BN_CTX *BN_CTX_new(void) {
|
125
|
+
BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
|
126
|
+
if (!ret) {
|
127
|
+
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
128
|
+
return NULL;
|
129
|
+
}
|
130
|
+
|
131
|
+
// Initialise the structure
|
132
|
+
BN_POOL_init(&ret->pool);
|
133
|
+
BN_STACK_init(&ret->stack);
|
134
|
+
ret->used = 0;
|
135
|
+
ret->err_stack = 0;
|
136
|
+
ret->too_many = 0;
|
137
|
+
return ret;
|
138
|
+
}
|
139
|
+
|
140
|
+
void BN_CTX_free(BN_CTX *ctx) {
|
141
|
+
if (ctx == NULL) {
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
|
145
|
+
BN_STACK_finish(&ctx->stack);
|
146
|
+
BN_POOL_finish(&ctx->pool);
|
147
|
+
OPENSSL_free(ctx);
|
148
|
+
}
|
149
|
+
|
150
|
+
void BN_CTX_start(BN_CTX *ctx) {
|
151
|
+
// If we're already overflowing ...
|
152
|
+
if (ctx->err_stack || ctx->too_many) {
|
153
|
+
ctx->err_stack++;
|
154
|
+
} else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
|
155
|
+
// (Try to) get a new frame pointer
|
156
|
+
OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
|
157
|
+
ctx->err_stack++;
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
BIGNUM *BN_CTX_get(BN_CTX *ctx) {
|
162
|
+
BIGNUM *ret;
|
163
|
+
if (ctx->err_stack || ctx->too_many) {
|
164
|
+
return NULL;
|
165
|
+
}
|
166
|
+
|
167
|
+
ret = BN_POOL_get(&ctx->pool);
|
168
|
+
if (ret == NULL) {
|
169
|
+
// Setting too_many prevents repeated "get" attempts from
|
170
|
+
// cluttering the error stack.
|
171
|
+
ctx->too_many = 1;
|
172
|
+
OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
|
173
|
+
return NULL;
|
174
|
+
}
|
175
|
+
|
176
|
+
// OK, make sure the returned bignum is "zero"
|
177
|
+
BN_zero(ret);
|
178
|
+
ctx->used++;
|
179
|
+
return ret;
|
180
|
+
}
|
181
|
+
|
182
|
+
void BN_CTX_end(BN_CTX *ctx) {
|
183
|
+
if (ctx->err_stack) {
|
184
|
+
ctx->err_stack--;
|
185
|
+
} else {
|
186
|
+
unsigned int fp = BN_STACK_pop(&ctx->stack);
|
187
|
+
// Does this stack frame have anything to release?
|
188
|
+
if (fp < ctx->used) {
|
189
|
+
BN_POOL_release(&ctx->pool, ctx->used - fp);
|
190
|
+
}
|
191
|
+
|
192
|
+
ctx->used = fp;
|
193
|
+
// Unjam "too_many" in case "get" had failed
|
194
|
+
ctx->too_many = 0;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
|
199
|
+
// BN_STACK
|
200
|
+
|
201
|
+
static void BN_STACK_init(BN_STACK *st) {
|
202
|
+
st->indexes = NULL;
|
203
|
+
st->depth = st->size = 0;
|
204
|
+
}
|
205
|
+
|
206
|
+
static void BN_STACK_finish(BN_STACK *st) {
|
207
|
+
OPENSSL_free(st->indexes);
|
208
|
+
}
|
209
|
+
|
210
|
+
static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
|
211
|
+
if (st->depth == st->size) {
|
212
|
+
// Need to expand
|
213
|
+
unsigned int newsize =
|
214
|
+
(st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES);
|
215
|
+
unsigned int *newitems = OPENSSL_malloc(newsize * sizeof(unsigned int));
|
216
|
+
if (!newitems) {
|
217
|
+
return 0;
|
218
|
+
}
|
219
|
+
if (st->depth) {
|
220
|
+
OPENSSL_memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
|
221
|
+
}
|
222
|
+
OPENSSL_free(st->indexes);
|
223
|
+
st->indexes = newitems;
|
224
|
+
st->size = newsize;
|
225
|
+
}
|
226
|
+
|
227
|
+
st->indexes[(st->depth)++] = idx;
|
228
|
+
return 1;
|
229
|
+
}
|
230
|
+
|
231
|
+
static unsigned int BN_STACK_pop(BN_STACK *st) {
|
232
|
+
return st->indexes[--(st->depth)];
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
static void BN_POOL_init(BN_POOL *p) {
|
237
|
+
p->head = p->current = p->tail = NULL;
|
238
|
+
p->used = p->size = 0;
|
239
|
+
}
|
240
|
+
|
241
|
+
static void BN_POOL_finish(BN_POOL *p) {
|
242
|
+
while (p->head) {
|
243
|
+
for (size_t i = 0; i < BN_CTX_POOL_SIZE; i++) {
|
244
|
+
BN_clear_free(&p->head->vals[i]);
|
245
|
+
}
|
246
|
+
|
247
|
+
p->current = p->head->next;
|
248
|
+
OPENSSL_free(p->head);
|
249
|
+
p->head = p->current;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
static BIGNUM *BN_POOL_get(BN_POOL *p) {
|
254
|
+
if (p->used == p->size) {
|
255
|
+
BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
|
256
|
+
if (!item) {
|
257
|
+
return NULL;
|
258
|
+
}
|
259
|
+
|
260
|
+
// Initialise the structure
|
261
|
+
for (size_t i = 0; i < BN_CTX_POOL_SIZE; i++) {
|
262
|
+
BN_init(&item->vals[i]);
|
263
|
+
}
|
264
|
+
|
265
|
+
item->prev = p->tail;
|
266
|
+
item->next = NULL;
|
267
|
+
// Link it in
|
268
|
+
if (!p->head) {
|
269
|
+
p->head = p->current = p->tail = item;
|
270
|
+
} else {
|
271
|
+
p->tail->next = item;
|
272
|
+
p->tail = item;
|
273
|
+
p->current = item;
|
274
|
+
}
|
275
|
+
|
276
|
+
p->size += BN_CTX_POOL_SIZE;
|
277
|
+
p->used++;
|
278
|
+
// Return the first bignum from the new pool
|
279
|
+
return item->vals;
|
280
|
+
}
|
281
|
+
|
282
|
+
if (!p->used) {
|
283
|
+
p->current = p->head;
|
284
|
+
} else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
|
285
|
+
p->current = p->current->next;
|
286
|
+
}
|
287
|
+
|
288
|
+
return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
|
289
|
+
}
|
290
|
+
|
291
|
+
static void BN_POOL_release(BN_POOL *p, unsigned int num) {
|
292
|
+
unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
|
293
|
+
p->used -= num;
|
294
|
+
|
295
|
+
while (num--) {
|
296
|
+
if (!offset) {
|
297
|
+
offset = BN_CTX_POOL_SIZE - 1;
|
298
|
+
p->current = p->current->prev;
|
299
|
+
} else {
|
300
|
+
offset--;
|
301
|
+
}
|
302
|
+
}
|
303
|
+
}
|