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,138 @@
|
|
1
|
+
/* ====================================================================
|
2
|
+
* Copyright (c) 2001-2011 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
|
+
#include <openssl/aes.h>
|
50
|
+
|
51
|
+
#include <limits.h>
|
52
|
+
#include <string.h>
|
53
|
+
|
54
|
+
#include <openssl/mem.h>
|
55
|
+
|
56
|
+
#include "../../internal.h"
|
57
|
+
|
58
|
+
|
59
|
+
// kDefaultIV is the default IV value given in RFC 3394, 2.2.3.1.
|
60
|
+
static const uint8_t kDefaultIV[] = {
|
61
|
+
0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6,
|
62
|
+
};
|
63
|
+
|
64
|
+
static const unsigned kBound = 6;
|
65
|
+
|
66
|
+
int AES_wrap_key(const AES_KEY *key, const uint8_t *iv, uint8_t *out,
|
67
|
+
const uint8_t *in, size_t in_len) {
|
68
|
+
// See RFC 3394, section 2.2.1.
|
69
|
+
|
70
|
+
if (in_len > INT_MAX - 8 || in_len < 8 || in_len % 8 != 0) {
|
71
|
+
return -1;
|
72
|
+
}
|
73
|
+
|
74
|
+
if (iv == NULL) {
|
75
|
+
iv = kDefaultIV;
|
76
|
+
}
|
77
|
+
|
78
|
+
OPENSSL_memmove(out + 8, in, in_len);
|
79
|
+
uint8_t A[AES_BLOCK_SIZE];
|
80
|
+
OPENSSL_memcpy(A, iv, 8);
|
81
|
+
|
82
|
+
size_t n = in_len / 8;
|
83
|
+
|
84
|
+
for (unsigned j = 0; j < kBound; j++) {
|
85
|
+
for (size_t i = 1; i <= n; i++) {
|
86
|
+
OPENSSL_memcpy(A + 8, out + 8 * i, 8);
|
87
|
+
AES_encrypt(A, A, key);
|
88
|
+
|
89
|
+
uint32_t t = (uint32_t)(n * j + i);
|
90
|
+
A[7] ^= t & 0xff;
|
91
|
+
A[6] ^= (t >> 8) & 0xff;
|
92
|
+
A[5] ^= (t >> 16) & 0xff;
|
93
|
+
A[4] ^= (t >> 24) & 0xff;
|
94
|
+
OPENSSL_memcpy(out + 8 * i, A + 8, 8);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
OPENSSL_memcpy(out, A, 8);
|
99
|
+
return (int)in_len + 8;
|
100
|
+
}
|
101
|
+
|
102
|
+
int AES_unwrap_key(const AES_KEY *key, const uint8_t *iv, uint8_t *out,
|
103
|
+
const uint8_t *in, size_t in_len) {
|
104
|
+
// See RFC 3394, section 2.2.2.
|
105
|
+
|
106
|
+
if (in_len > INT_MAX || in_len < 16 || in_len % 8 != 0) {
|
107
|
+
return -1;
|
108
|
+
}
|
109
|
+
|
110
|
+
if (iv == NULL) {
|
111
|
+
iv = kDefaultIV;
|
112
|
+
}
|
113
|
+
|
114
|
+
uint8_t A[AES_BLOCK_SIZE];
|
115
|
+
OPENSSL_memcpy(A, in, 8);
|
116
|
+
OPENSSL_memmove(out, in + 8, in_len - 8);
|
117
|
+
|
118
|
+
size_t n = (in_len / 8) - 1;
|
119
|
+
|
120
|
+
for (unsigned j = kBound - 1; j < kBound; j--) {
|
121
|
+
for (size_t i = n; i > 0; i--) {
|
122
|
+
uint32_t t = (uint32_t)(n * j + i);
|
123
|
+
A[7] ^= t & 0xff;
|
124
|
+
A[6] ^= (t >> 8) & 0xff;
|
125
|
+
A[5] ^= (t >> 16) & 0xff;
|
126
|
+
A[4] ^= (t >> 24) & 0xff;
|
127
|
+
OPENSSL_memcpy(A + 8, out + 8 * (i - 1), 8);
|
128
|
+
AES_decrypt(A, A, key);
|
129
|
+
OPENSSL_memcpy(out + 8 * (i - 1), A + 8, 8);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
if (CRYPTO_memcmp(A, iv, 8) != 0) {
|
134
|
+
return -1;
|
135
|
+
}
|
136
|
+
|
137
|
+
return (int)in_len - 8;
|
138
|
+
}
|
@@ -0,0 +1,112 @@
|
|
1
|
+
/* ====================================================================
|
2
|
+
* Copyright (c) 2002-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
|
+
#include <openssl/aes.h>
|
50
|
+
|
51
|
+
#include <assert.h>
|
52
|
+
|
53
|
+
#include "../modes/internal.h"
|
54
|
+
|
55
|
+
|
56
|
+
void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
|
57
|
+
const AES_KEY *key, uint8_t ivec[AES_BLOCK_SIZE],
|
58
|
+
uint8_t ecount_buf[AES_BLOCK_SIZE], unsigned int *num) {
|
59
|
+
CRYPTO_ctr128_encrypt(in, out, len, key, ivec, ecount_buf, num,
|
60
|
+
(block128_f)AES_encrypt);
|
61
|
+
}
|
62
|
+
|
63
|
+
void AES_ecb_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key,
|
64
|
+
const int enc) {
|
65
|
+
assert(in && out && key);
|
66
|
+
assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
|
67
|
+
|
68
|
+
if (AES_ENCRYPT == enc) {
|
69
|
+
AES_encrypt(in, out, key);
|
70
|
+
} else {
|
71
|
+
AES_decrypt(in, out, key);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
#if defined(OPENSSL_NO_ASM) || \
|
76
|
+
(!defined(OPENSSL_X86_64) && !defined(OPENSSL_X86))
|
77
|
+
void AES_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
|
78
|
+
const AES_KEY *key, uint8_t *ivec, const int enc) {
|
79
|
+
|
80
|
+
if (enc) {
|
81
|
+
CRYPTO_cbc128_encrypt(in, out, len, key, ivec, (block128_f)AES_encrypt);
|
82
|
+
} else {
|
83
|
+
CRYPTO_cbc128_decrypt(in, out, len, key, ivec, (block128_f)AES_decrypt);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
#else
|
87
|
+
|
88
|
+
void asm_AES_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
|
89
|
+
const AES_KEY *key, uint8_t *ivec, const int enc);
|
90
|
+
void AES_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
|
91
|
+
const AES_KEY *key, uint8_t *ivec, const int enc) {
|
92
|
+
asm_AES_cbc_encrypt(in, out, len, key, ivec, enc);
|
93
|
+
}
|
94
|
+
|
95
|
+
#endif // OPENSSL_NO_ASM || (!OPENSSL_X86_64 && !OPENSSL_X86)
|
96
|
+
|
97
|
+
void AES_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t length,
|
98
|
+
const AES_KEY *key, uint8_t *ivec, int *num) {
|
99
|
+
unsigned num_u = (unsigned)(*num);
|
100
|
+
CRYPTO_ofb128_encrypt(in, out, length, key, ivec, &num_u,
|
101
|
+
(block128_f)AES_encrypt);
|
102
|
+
*num = (int)num_u;
|
103
|
+
}
|
104
|
+
|
105
|
+
void AES_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t length,
|
106
|
+
const AES_KEY *key, uint8_t *ivec, int *num,
|
107
|
+
int enc) {
|
108
|
+
unsigned num_u = (unsigned)(*num);
|
109
|
+
CRYPTO_cfb128_encrypt(in, out, length, key, ivec, &num_u, enc,
|
110
|
+
(block128_f)AES_encrypt);
|
111
|
+
*num = (int)num_u;
|
112
|
+
}
|
@@ -0,0 +1,375 @@
|
|
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 <string.h>
|
60
|
+
|
61
|
+
#include <openssl/err.h>
|
62
|
+
#include <openssl/mem.h>
|
63
|
+
|
64
|
+
#include "internal.h"
|
65
|
+
|
66
|
+
|
67
|
+
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
|
68
|
+
const BIGNUM *tmp;
|
69
|
+
int a_neg = a->neg, ret;
|
70
|
+
|
71
|
+
// a + b a+b
|
72
|
+
// a + -b a-b
|
73
|
+
// -a + b b-a
|
74
|
+
// -a + -b -(a+b)
|
75
|
+
if (a_neg ^ b->neg) {
|
76
|
+
// only one is negative
|
77
|
+
if (a_neg) {
|
78
|
+
tmp = a;
|
79
|
+
a = b;
|
80
|
+
b = tmp;
|
81
|
+
}
|
82
|
+
|
83
|
+
// we are now a - b
|
84
|
+
if (BN_ucmp(a, b) < 0) {
|
85
|
+
if (!BN_usub(r, b, a)) {
|
86
|
+
return 0;
|
87
|
+
}
|
88
|
+
r->neg = 1;
|
89
|
+
} else {
|
90
|
+
if (!BN_usub(r, a, b)) {
|
91
|
+
return 0;
|
92
|
+
}
|
93
|
+
r->neg = 0;
|
94
|
+
}
|
95
|
+
return 1;
|
96
|
+
}
|
97
|
+
|
98
|
+
ret = BN_uadd(r, a, b);
|
99
|
+
r->neg = a_neg;
|
100
|
+
return ret;
|
101
|
+
}
|
102
|
+
|
103
|
+
int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
|
104
|
+
int max, min, dif;
|
105
|
+
BN_ULONG *ap, *bp, *rp, carry, t1, t2;
|
106
|
+
const BIGNUM *tmp;
|
107
|
+
|
108
|
+
if (a->top < b->top) {
|
109
|
+
tmp = a;
|
110
|
+
a = b;
|
111
|
+
b = tmp;
|
112
|
+
}
|
113
|
+
max = a->top;
|
114
|
+
min = b->top;
|
115
|
+
dif = max - min;
|
116
|
+
|
117
|
+
if (!bn_wexpand(r, max + 1)) {
|
118
|
+
return 0;
|
119
|
+
}
|
120
|
+
|
121
|
+
r->top = max;
|
122
|
+
|
123
|
+
ap = a->d;
|
124
|
+
bp = b->d;
|
125
|
+
rp = r->d;
|
126
|
+
|
127
|
+
carry = bn_add_words(rp, ap, bp, min);
|
128
|
+
rp += min;
|
129
|
+
ap += min;
|
130
|
+
bp += min;
|
131
|
+
|
132
|
+
if (carry) {
|
133
|
+
while (dif) {
|
134
|
+
dif--;
|
135
|
+
t1 = *(ap++);
|
136
|
+
t2 = (t1 + 1) & BN_MASK2;
|
137
|
+
*(rp++) = t2;
|
138
|
+
if (t2) {
|
139
|
+
carry = 0;
|
140
|
+
break;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
if (carry) {
|
144
|
+
// carry != 0 => dif == 0
|
145
|
+
*rp = 1;
|
146
|
+
r->top++;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
if (dif && rp != ap) {
|
151
|
+
while (dif--) {
|
152
|
+
// copy remaining words if ap != rp
|
153
|
+
*(rp++) = *(ap++);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
r->neg = 0;
|
158
|
+
return 1;
|
159
|
+
}
|
160
|
+
|
161
|
+
int BN_add_word(BIGNUM *a, BN_ULONG w) {
|
162
|
+
BN_ULONG l;
|
163
|
+
int i;
|
164
|
+
|
165
|
+
w &= BN_MASK2;
|
166
|
+
|
167
|
+
// degenerate case: w is zero
|
168
|
+
if (!w) {
|
169
|
+
return 1;
|
170
|
+
}
|
171
|
+
|
172
|
+
// degenerate case: a is zero
|
173
|
+
if (BN_is_zero(a)) {
|
174
|
+
return BN_set_word(a, w);
|
175
|
+
}
|
176
|
+
|
177
|
+
// handle 'a' when negative
|
178
|
+
if (a->neg) {
|
179
|
+
a->neg = 0;
|
180
|
+
i = BN_sub_word(a, w);
|
181
|
+
if (!BN_is_zero(a)) {
|
182
|
+
a->neg = !(a->neg);
|
183
|
+
}
|
184
|
+
return i;
|
185
|
+
}
|
186
|
+
|
187
|
+
for (i = 0; w != 0 && i < a->top; i++) {
|
188
|
+
a->d[i] = l = (a->d[i] + w) & BN_MASK2;
|
189
|
+
w = (w > l) ? 1 : 0;
|
190
|
+
}
|
191
|
+
|
192
|
+
if (w && i == a->top) {
|
193
|
+
if (!bn_wexpand(a, a->top + 1)) {
|
194
|
+
return 0;
|
195
|
+
}
|
196
|
+
a->top++;
|
197
|
+
a->d[i] = w;
|
198
|
+
}
|
199
|
+
|
200
|
+
return 1;
|
201
|
+
}
|
202
|
+
|
203
|
+
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
|
204
|
+
int max;
|
205
|
+
int add = 0, neg = 0;
|
206
|
+
const BIGNUM *tmp;
|
207
|
+
|
208
|
+
// a - b a-b
|
209
|
+
// a - -b a+b
|
210
|
+
// -a - b -(a+b)
|
211
|
+
// -a - -b b-a
|
212
|
+
if (a->neg) {
|
213
|
+
if (b->neg) {
|
214
|
+
tmp = a;
|
215
|
+
a = b;
|
216
|
+
b = tmp;
|
217
|
+
} else {
|
218
|
+
add = 1;
|
219
|
+
neg = 1;
|
220
|
+
}
|
221
|
+
} else {
|
222
|
+
if (b->neg) {
|
223
|
+
add = 1;
|
224
|
+
neg = 0;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
|
228
|
+
if (add) {
|
229
|
+
if (!BN_uadd(r, a, b)) {
|
230
|
+
return 0;
|
231
|
+
}
|
232
|
+
|
233
|
+
r->neg = neg;
|
234
|
+
return 1;
|
235
|
+
}
|
236
|
+
|
237
|
+
// We are actually doing a - b :-)
|
238
|
+
|
239
|
+
max = (a->top > b->top) ? a->top : b->top;
|
240
|
+
if (!bn_wexpand(r, max)) {
|
241
|
+
return 0;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (BN_ucmp(a, b) < 0) {
|
245
|
+
if (!BN_usub(r, b, a)) {
|
246
|
+
return 0;
|
247
|
+
}
|
248
|
+
r->neg = 1;
|
249
|
+
} else {
|
250
|
+
if (!BN_usub(r, a, b)) {
|
251
|
+
return 0;
|
252
|
+
}
|
253
|
+
r->neg = 0;
|
254
|
+
}
|
255
|
+
|
256
|
+
return 1;
|
257
|
+
}
|
258
|
+
|
259
|
+
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
|
260
|
+
int max, min, dif;
|
261
|
+
register BN_ULONG t1, t2, *ap, *bp, *rp;
|
262
|
+
int i, carry;
|
263
|
+
|
264
|
+
max = a->top;
|
265
|
+
min = b->top;
|
266
|
+
dif = max - min;
|
267
|
+
|
268
|
+
if (dif < 0) // hmm... should not be happening
|
269
|
+
{
|
270
|
+
OPENSSL_PUT_ERROR(BN, BN_R_ARG2_LT_ARG3);
|
271
|
+
return 0;
|
272
|
+
}
|
273
|
+
|
274
|
+
if (!bn_wexpand(r, max)) {
|
275
|
+
return 0;
|
276
|
+
}
|
277
|
+
|
278
|
+
ap = a->d;
|
279
|
+
bp = b->d;
|
280
|
+
rp = r->d;
|
281
|
+
|
282
|
+
carry = 0;
|
283
|
+
for (i = min; i != 0; i--) {
|
284
|
+
t1 = *(ap++);
|
285
|
+
t2 = *(bp++);
|
286
|
+
if (carry) {
|
287
|
+
carry = (t1 <= t2);
|
288
|
+
t1 = (t1 - t2 - 1) & BN_MASK2;
|
289
|
+
} else {
|
290
|
+
carry = (t1 < t2);
|
291
|
+
t1 = (t1 - t2) & BN_MASK2;
|
292
|
+
}
|
293
|
+
*(rp++) = t1 & BN_MASK2;
|
294
|
+
}
|
295
|
+
|
296
|
+
if (carry) // subtracted
|
297
|
+
{
|
298
|
+
if (!dif) {
|
299
|
+
// error: a < b
|
300
|
+
return 0;
|
301
|
+
}
|
302
|
+
|
303
|
+
while (dif) {
|
304
|
+
dif--;
|
305
|
+
t1 = *(ap++);
|
306
|
+
t2 = (t1 - 1) & BN_MASK2;
|
307
|
+
*(rp++) = t2;
|
308
|
+
if (t1) {
|
309
|
+
break;
|
310
|
+
}
|
311
|
+
}
|
312
|
+
}
|
313
|
+
|
314
|
+
if (dif > 0 && rp != ap) {
|
315
|
+
OPENSSL_memcpy(rp, ap, sizeof(*rp) * dif);
|
316
|
+
}
|
317
|
+
|
318
|
+
r->top = max;
|
319
|
+
r->neg = 0;
|
320
|
+
bn_correct_top(r);
|
321
|
+
|
322
|
+
return 1;
|
323
|
+
}
|
324
|
+
|
325
|
+
int BN_sub_word(BIGNUM *a, BN_ULONG w) {
|
326
|
+
int i;
|
327
|
+
|
328
|
+
w &= BN_MASK2;
|
329
|
+
|
330
|
+
// degenerate case: w is zero
|
331
|
+
if (!w) {
|
332
|
+
return 1;
|
333
|
+
}
|
334
|
+
|
335
|
+
// degenerate case: a is zero
|
336
|
+
if (BN_is_zero(a)) {
|
337
|
+
i = BN_set_word(a, w);
|
338
|
+
if (i != 0) {
|
339
|
+
BN_set_negative(a, 1);
|
340
|
+
}
|
341
|
+
return i;
|
342
|
+
}
|
343
|
+
|
344
|
+
// handle 'a' when negative
|
345
|
+
if (a->neg) {
|
346
|
+
a->neg = 0;
|
347
|
+
i = BN_add_word(a, w);
|
348
|
+
a->neg = 1;
|
349
|
+
return i;
|
350
|
+
}
|
351
|
+
|
352
|
+
if ((a->top == 1) && (a->d[0] < w)) {
|
353
|
+
a->d[0] = w - a->d[0];
|
354
|
+
a->neg = 1;
|
355
|
+
return 1;
|
356
|
+
}
|
357
|
+
|
358
|
+
i = 0;
|
359
|
+
for (;;) {
|
360
|
+
if (a->d[i] >= w) {
|
361
|
+
a->d[i] -= w;
|
362
|
+
break;
|
363
|
+
} else {
|
364
|
+
a->d[i] = (a->d[i] - w) & BN_MASK2;
|
365
|
+
i++;
|
366
|
+
w = 1;
|
367
|
+
}
|
368
|
+
}
|
369
|
+
|
370
|
+
if ((a->d[i] == 0) && (i == (a->top - 1))) {
|
371
|
+
a->top--;
|
372
|
+
}
|
373
|
+
|
374
|
+
return 1;
|
375
|
+
}
|