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,95 @@
|
|
1
|
+
/* ====================================================================
|
2
|
+
* Copyright (c) 2008 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/type_check.h>
|
50
|
+
|
51
|
+
#include <assert.h>
|
52
|
+
#include <string.h>
|
53
|
+
|
54
|
+
#include "internal.h"
|
55
|
+
|
56
|
+
|
57
|
+
OPENSSL_COMPILE_ASSERT((16 % sizeof(size_t)) == 0, bad_size_t_size_ofb);
|
58
|
+
|
59
|
+
void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
|
60
|
+
const void *key, uint8_t ivec[16], unsigned *num,
|
61
|
+
block128_f block) {
|
62
|
+
assert(in && out && key && ivec && num);
|
63
|
+
|
64
|
+
unsigned n = *num;
|
65
|
+
|
66
|
+
while (n && len) {
|
67
|
+
*(out++) = *(in++) ^ ivec[n];
|
68
|
+
--len;
|
69
|
+
n = (n + 1) % 16;
|
70
|
+
}
|
71
|
+
|
72
|
+
while (len >= 16) {
|
73
|
+
(*block)(ivec, ivec, key);
|
74
|
+
for (; n < 16; n += sizeof(size_t)) {
|
75
|
+
size_t a, b;
|
76
|
+
OPENSSL_memcpy(&a, in + n, sizeof(size_t));
|
77
|
+
OPENSSL_memcpy(&b, ivec + n, sizeof(size_t));
|
78
|
+
|
79
|
+
const size_t c = a ^ b;
|
80
|
+
OPENSSL_memcpy(out + n, &c, sizeof(size_t));
|
81
|
+
}
|
82
|
+
len -= 16;
|
83
|
+
out += 16;
|
84
|
+
in += 16;
|
85
|
+
n = 0;
|
86
|
+
}
|
87
|
+
if (len) {
|
88
|
+
(*block)(ivec, ivec, key);
|
89
|
+
while (len--) {
|
90
|
+
out[n] = in[n] ^ ivec[n];
|
91
|
+
++n;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
*num = n;
|
95
|
+
}
|
@@ -0,0 +1,91 @@
|
|
1
|
+
/* Copyright (c) 2016, 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
|
+
#include <openssl/base.h>
|
16
|
+
|
17
|
+
#include <assert.h>
|
18
|
+
#include <string.h>
|
19
|
+
|
20
|
+
#include "internal.h"
|
21
|
+
#include "../../internal.h"
|
22
|
+
|
23
|
+
|
24
|
+
// byte_reverse reverses the order of the bytes in |b->c|.
|
25
|
+
static void byte_reverse(polyval_block *b) {
|
26
|
+
const uint64_t t = CRYPTO_bswap8(b->u[0]);
|
27
|
+
b->u[0] = CRYPTO_bswap8(b->u[1]);
|
28
|
+
b->u[1] = t;
|
29
|
+
}
|
30
|
+
|
31
|
+
// reverse_and_mulX_ghash interprets the bytes |b->c| as a reversed element of
|
32
|
+
// the GHASH field, multiplies that by 'x' and serialises the result back into
|
33
|
+
// |b|, but with GHASH's backwards bit ordering.
|
34
|
+
static void reverse_and_mulX_ghash(polyval_block *b) {
|
35
|
+
uint64_t hi = b->u[0];
|
36
|
+
uint64_t lo = b->u[1];
|
37
|
+
const crypto_word_t carry = constant_time_eq_w(hi & 1, 1);
|
38
|
+
hi >>= 1;
|
39
|
+
hi |= lo << 63;
|
40
|
+
lo >>= 1;
|
41
|
+
lo ^= ((uint64_t) constant_time_select_w(carry, 0xe1, 0)) << 56;
|
42
|
+
|
43
|
+
b->u[0] = CRYPTO_bswap8(lo);
|
44
|
+
b->u[1] = CRYPTO_bswap8(hi);
|
45
|
+
}
|
46
|
+
|
47
|
+
// POLYVAL(H, X_1, ..., X_n) =
|
48
|
+
// ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X_1), ...,
|
49
|
+
// ByteReverse(X_n))).
|
50
|
+
//
|
51
|
+
// See https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#appendix-A.
|
52
|
+
|
53
|
+
void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]) {
|
54
|
+
polyval_block H;
|
55
|
+
OPENSSL_memcpy(H.c, key, 16);
|
56
|
+
reverse_and_mulX_ghash(&H);
|
57
|
+
|
58
|
+
int is_avx;
|
59
|
+
CRYPTO_ghash_init(&ctx->gmult, &ctx->ghash, &ctx->H, ctx->Htable, &is_avx,
|
60
|
+
H.c);
|
61
|
+
OPENSSL_memset(&ctx->S, 0, sizeof(ctx->S));
|
62
|
+
}
|
63
|
+
|
64
|
+
void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in,
|
65
|
+
size_t in_len) {
|
66
|
+
assert((in_len & 15) == 0);
|
67
|
+
polyval_block reversed[32];
|
68
|
+
|
69
|
+
while (in_len > 0) {
|
70
|
+
size_t todo = in_len;
|
71
|
+
if (todo > sizeof(reversed)) {
|
72
|
+
todo = sizeof(reversed);
|
73
|
+
}
|
74
|
+
OPENSSL_memcpy(reversed, in, todo);
|
75
|
+
in += todo;
|
76
|
+
in_len -= todo;
|
77
|
+
|
78
|
+
size_t blocks = todo / sizeof(polyval_block);
|
79
|
+
for (size_t i = 0; i < blocks; i++) {
|
80
|
+
byte_reverse(&reversed[i]);
|
81
|
+
}
|
82
|
+
|
83
|
+
ctx->ghash(ctx->S.u, ctx->Htable, (const uint8_t *) reversed, todo);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]) {
|
88
|
+
polyval_block S = ctx->S;
|
89
|
+
byte_reverse(&S);
|
90
|
+
OPENSSL_memcpy(out, &S.c, sizeof(polyval_block));
|
91
|
+
}
|
@@ -0,0 +1,200 @@
|
|
1
|
+
/* Copyright (c) 2017, 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
|
+
#include <openssl/rand.h>
|
16
|
+
|
17
|
+
#include <openssl/type_check.h>
|
18
|
+
#include <openssl/mem.h>
|
19
|
+
|
20
|
+
#include "internal.h"
|
21
|
+
#include "../cipher/internal.h"
|
22
|
+
|
23
|
+
|
24
|
+
// Section references in this file refer to SP 800-90Ar1:
|
25
|
+
// http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf
|
26
|
+
|
27
|
+
// See table 3.
|
28
|
+
static const uint64_t kMaxReseedCount = UINT64_C(1) << 48;
|
29
|
+
|
30
|
+
int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
|
31
|
+
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
|
32
|
+
const uint8_t *personalization, size_t personalization_len) {
|
33
|
+
// Section 10.2.1.3.1
|
34
|
+
if (personalization_len > CTR_DRBG_ENTROPY_LEN) {
|
35
|
+
return 0;
|
36
|
+
}
|
37
|
+
|
38
|
+
uint8_t seed_material[CTR_DRBG_ENTROPY_LEN];
|
39
|
+
OPENSSL_memcpy(seed_material, entropy, CTR_DRBG_ENTROPY_LEN);
|
40
|
+
|
41
|
+
for (size_t i = 0; i < personalization_len; i++) {
|
42
|
+
seed_material[i] ^= personalization[i];
|
43
|
+
}
|
44
|
+
|
45
|
+
// Section 10.2.1.2
|
46
|
+
|
47
|
+
// kInitMask is the result of encrypting blocks with big-endian value 1, 2
|
48
|
+
// and 3 with the all-zero AES-256 key.
|
49
|
+
static const uint8_t kInitMask[CTR_DRBG_ENTROPY_LEN] = {
|
50
|
+
0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1,
|
51
|
+
0xc4, 0xcb, 0x73, 0x8b, 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
|
52
|
+
0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18, 0x72, 0x60, 0x03, 0xca,
|
53
|
+
0x37, 0xa6, 0x2a, 0x74, 0xd1, 0xa2, 0xf5, 0x8e, 0x75, 0x06, 0x35, 0x8e,
|
54
|
+
};
|
55
|
+
|
56
|
+
for (size_t i = 0; i < sizeof(kInitMask); i++) {
|
57
|
+
seed_material[i] ^= kInitMask[i];
|
58
|
+
}
|
59
|
+
|
60
|
+
drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, seed_material, 32);
|
61
|
+
OPENSSL_memcpy(drbg->counter.bytes, seed_material + 32, 16);
|
62
|
+
drbg->reseed_counter = 1;
|
63
|
+
|
64
|
+
return 1;
|
65
|
+
}
|
66
|
+
|
67
|
+
OPENSSL_COMPILE_ASSERT(CTR_DRBG_ENTROPY_LEN % AES_BLOCK_SIZE == 0,
|
68
|
+
not_a_multiple_of_block_size);
|
69
|
+
|
70
|
+
// ctr_inc adds |n| to the last four bytes of |drbg->counter|, treated as a
|
71
|
+
// big-endian number.
|
72
|
+
static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) {
|
73
|
+
drbg->counter.words[3] =
|
74
|
+
CRYPTO_bswap4(CRYPTO_bswap4(drbg->counter.words[3]) + n);
|
75
|
+
}
|
76
|
+
|
77
|
+
static int CTR_DRBG_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
|
78
|
+
size_t data_len) {
|
79
|
+
// Section 10.2.1.2. A value of |data_len| which less than
|
80
|
+
// |CTR_DRBG_ENTROPY_LEN| is permitted and acts the same as right-padding
|
81
|
+
// with zeros. This can save a copy.
|
82
|
+
if (data_len > CTR_DRBG_ENTROPY_LEN) {
|
83
|
+
return 0;
|
84
|
+
}
|
85
|
+
|
86
|
+
uint8_t temp[CTR_DRBG_ENTROPY_LEN];
|
87
|
+
for (size_t i = 0; i < CTR_DRBG_ENTROPY_LEN; i += AES_BLOCK_SIZE) {
|
88
|
+
ctr32_add(drbg, 1);
|
89
|
+
drbg->block(drbg->counter.bytes, temp + i, &drbg->ks);
|
90
|
+
}
|
91
|
+
|
92
|
+
for (size_t i = 0; i < data_len; i++) {
|
93
|
+
temp[i] ^= data[i];
|
94
|
+
}
|
95
|
+
|
96
|
+
drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, temp, 32);
|
97
|
+
OPENSSL_memcpy(drbg->counter.bytes, temp + 32, 16);
|
98
|
+
|
99
|
+
return 1;
|
100
|
+
}
|
101
|
+
|
102
|
+
int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
|
103
|
+
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
|
104
|
+
const uint8_t *additional_data,
|
105
|
+
size_t additional_data_len) {
|
106
|
+
// Section 10.2.1.4
|
107
|
+
uint8_t entropy_copy[CTR_DRBG_ENTROPY_LEN];
|
108
|
+
|
109
|
+
if (additional_data_len > 0) {
|
110
|
+
if (additional_data_len > CTR_DRBG_ENTROPY_LEN) {
|
111
|
+
return 0;
|
112
|
+
}
|
113
|
+
|
114
|
+
OPENSSL_memcpy(entropy_copy, entropy, CTR_DRBG_ENTROPY_LEN);
|
115
|
+
for (size_t i = 0; i < additional_data_len; i++) {
|
116
|
+
entropy_copy[i] ^= additional_data[i];
|
117
|
+
}
|
118
|
+
|
119
|
+
entropy = entropy_copy;
|
120
|
+
}
|
121
|
+
|
122
|
+
if (!CTR_DRBG_update(drbg, entropy, CTR_DRBG_ENTROPY_LEN)) {
|
123
|
+
return 0;
|
124
|
+
}
|
125
|
+
|
126
|
+
drbg->reseed_counter = 1;
|
127
|
+
|
128
|
+
return 1;
|
129
|
+
}
|
130
|
+
|
131
|
+
int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
|
132
|
+
const uint8_t *additional_data,
|
133
|
+
size_t additional_data_len) {
|
134
|
+
// See 9.3.1
|
135
|
+
if (out_len > CTR_DRBG_MAX_GENERATE_LENGTH) {
|
136
|
+
return 0;
|
137
|
+
}
|
138
|
+
|
139
|
+
// See 10.2.1.5.1
|
140
|
+
if (drbg->reseed_counter > kMaxReseedCount) {
|
141
|
+
return 0;
|
142
|
+
}
|
143
|
+
|
144
|
+
if (additional_data_len != 0 &&
|
145
|
+
!CTR_DRBG_update(drbg, additional_data, additional_data_len)) {
|
146
|
+
return 0;
|
147
|
+
}
|
148
|
+
|
149
|
+
// kChunkSize is used to interact better with the cache. Since the AES-CTR
|
150
|
+
// code assumes that it's encrypting rather than just writing keystream, the
|
151
|
+
// buffer has to be zeroed first. Without chunking, large reads would zero
|
152
|
+
// the whole buffer, flushing the L1 cache, and then do another pass (missing
|
153
|
+
// the cache every time) to “encrypt” it. The code can avoid this by
|
154
|
+
// chunking.
|
155
|
+
static const size_t kChunkSize = 8 * 1024;
|
156
|
+
|
157
|
+
while (out_len >= AES_BLOCK_SIZE) {
|
158
|
+
size_t todo = kChunkSize;
|
159
|
+
if (todo > out_len) {
|
160
|
+
todo = out_len;
|
161
|
+
}
|
162
|
+
|
163
|
+
todo &= ~(AES_BLOCK_SIZE-1);
|
164
|
+
const size_t num_blocks = todo / AES_BLOCK_SIZE;
|
165
|
+
|
166
|
+
if (drbg->ctr) {
|
167
|
+
OPENSSL_memset(out, 0, todo);
|
168
|
+
ctr32_add(drbg, 1);
|
169
|
+
drbg->ctr(out, out, num_blocks, &drbg->ks, drbg->counter.bytes);
|
170
|
+
ctr32_add(drbg, num_blocks - 1);
|
171
|
+
} else {
|
172
|
+
for (size_t i = 0; i < todo; i += AES_BLOCK_SIZE) {
|
173
|
+
ctr32_add(drbg, 1);
|
174
|
+
drbg->block(drbg->counter.bytes, out + i, &drbg->ks);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
out += todo;
|
179
|
+
out_len -= todo;
|
180
|
+
}
|
181
|
+
|
182
|
+
if (out_len > 0) {
|
183
|
+
uint8_t block[AES_BLOCK_SIZE];
|
184
|
+
ctr32_add(drbg, 1);
|
185
|
+
drbg->block(drbg->counter.bytes, block, &drbg->ks);
|
186
|
+
|
187
|
+
OPENSSL_memcpy(out, block, out_len);
|
188
|
+
}
|
189
|
+
|
190
|
+
if (!CTR_DRBG_update(drbg, additional_data, additional_data_len)) {
|
191
|
+
return 0;
|
192
|
+
}
|
193
|
+
|
194
|
+
drbg->reseed_counter++;
|
195
|
+
return 1;
|
196
|
+
}
|
197
|
+
|
198
|
+
void CTR_DRBG_clear(CTR_DRBG_STATE *drbg) {
|
199
|
+
OPENSSL_cleanse(drbg, sizeof(CTR_DRBG_STATE));
|
200
|
+
}
|
@@ -0,0 +1,358 @@
|
|
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
|
+
#include <openssl/rand.h>
|
16
|
+
|
17
|
+
#include <assert.h>
|
18
|
+
#include <limits.h>
|
19
|
+
#include <string.h>
|
20
|
+
|
21
|
+
#if defined(BORINGSSL_FIPS)
|
22
|
+
#include <unistd.h>
|
23
|
+
#endif
|
24
|
+
|
25
|
+
#include <openssl/chacha.h>
|
26
|
+
#include <openssl/cpu.h>
|
27
|
+
#include <openssl/mem.h>
|
28
|
+
|
29
|
+
#include "internal.h"
|
30
|
+
#include "../../internal.h"
|
31
|
+
#include "../delocate.h"
|
32
|
+
|
33
|
+
|
34
|
+
// It's assumed that the operating system always has an unfailing source of
|
35
|
+
// entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
|
36
|
+
// entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
|
37
|
+
// don't try to handle it.)
|
38
|
+
//
|
39
|
+
// In addition, the hardware may provide a low-latency RNG. Intel's rdrand
|
40
|
+
// instruction is the canonical example of this. When a hardware RNG is
|
41
|
+
// available we don't need to worry about an RNG failure arising from fork()ing
|
42
|
+
// the process or moving a VM, so we can keep thread-local RNG state and use it
|
43
|
+
// as an additional-data input to CTR-DRBG.
|
44
|
+
//
|
45
|
+
// (We assume that the OS entropy is safe from fork()ing and VM duplication.
|
46
|
+
// This might be a bit of a leap of faith, esp on Windows, but there's nothing
|
47
|
+
// that we can do about it.)
|
48
|
+
|
49
|
+
// kReseedInterval is the number of generate calls made to CTR-DRBG before
|
50
|
+
// reseeding.
|
51
|
+
static const unsigned kReseedInterval = 4096;
|
52
|
+
|
53
|
+
// CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the
|
54
|
+
// continuous random number generator test in FIPS 140-2, section 4.9.2.
|
55
|
+
#define CRNGT_BLOCK_SIZE 16
|
56
|
+
|
57
|
+
// rand_thread_state contains the per-thread state for the RNG.
|
58
|
+
struct rand_thread_state {
|
59
|
+
CTR_DRBG_STATE drbg;
|
60
|
+
// calls is the number of generate calls made on |drbg| since it was last
|
61
|
+
// (re)seeded. This is bound by |kReseedInterval|.
|
62
|
+
unsigned calls;
|
63
|
+
// last_block_valid is non-zero iff |last_block| contains data from
|
64
|
+
// |CRYPTO_sysrand|.
|
65
|
+
int last_block_valid;
|
66
|
+
|
67
|
+
#if defined(BORINGSSL_FIPS)
|
68
|
+
// last_block contains the previous block from |CRYPTO_sysrand|.
|
69
|
+
uint8_t last_block[CRNGT_BLOCK_SIZE];
|
70
|
+
// next and prev form a NULL-terminated, double-linked list of all states in
|
71
|
+
// a process.
|
72
|
+
struct rand_thread_state *next, *prev;
|
73
|
+
#endif
|
74
|
+
};
|
75
|
+
|
76
|
+
#if defined(BORINGSSL_FIPS)
|
77
|
+
// thread_states_list is the head of a linked-list of all |rand_thread_state|
|
78
|
+
// objects in the process, one per thread. This is needed because FIPS requires
|
79
|
+
// that they be zeroed on process exit, but thread-local destructors aren't
|
80
|
+
// called when the whole process is exiting.
|
81
|
+
DEFINE_BSS_GET(struct rand_thread_state *, thread_states_list);
|
82
|
+
DEFINE_STATIC_MUTEX(thread_states_list_lock);
|
83
|
+
|
84
|
+
static void rand_thread_state_clear_all(void) __attribute__((destructor));
|
85
|
+
static void rand_thread_state_clear_all(void) {
|
86
|
+
CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
|
87
|
+
for (struct rand_thread_state *cur = *thread_states_list_bss_get();
|
88
|
+
cur != NULL; cur = cur->next) {
|
89
|
+
CTR_DRBG_clear(&cur->drbg);
|
90
|
+
}
|
91
|
+
// |thread_states_list_lock is deliberately left locked so that any threads
|
92
|
+
// that are still running will hang if they try to call |RAND_bytes|.
|
93
|
+
}
|
94
|
+
#endif
|
95
|
+
|
96
|
+
// rand_thread_state_free frees a |rand_thread_state|. This is called when a
|
97
|
+
// thread exits.
|
98
|
+
static void rand_thread_state_free(void *state_in) {
|
99
|
+
struct rand_thread_state *state = state_in;
|
100
|
+
|
101
|
+
if (state_in == NULL) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
|
105
|
+
#if defined(BORINGSSL_FIPS)
|
106
|
+
CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
|
107
|
+
|
108
|
+
if (state->prev != NULL) {
|
109
|
+
state->prev->next = state->next;
|
110
|
+
} else {
|
111
|
+
*thread_states_list_bss_get() = state->next;
|
112
|
+
}
|
113
|
+
|
114
|
+
if (state->next != NULL) {
|
115
|
+
state->next->prev = state->prev;
|
116
|
+
}
|
117
|
+
|
118
|
+
CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
|
119
|
+
|
120
|
+
CTR_DRBG_clear(&state->drbg);
|
121
|
+
#endif
|
122
|
+
|
123
|
+
OPENSSL_free(state);
|
124
|
+
}
|
125
|
+
|
126
|
+
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
|
127
|
+
!defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
|
128
|
+
|
129
|
+
// These functions are defined in asm/rdrand-x86_64.pl
|
130
|
+
extern int CRYPTO_rdrand(uint8_t out[8]);
|
131
|
+
extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
|
132
|
+
|
133
|
+
static int have_rdrand(void) {
|
134
|
+
return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0;
|
135
|
+
}
|
136
|
+
|
137
|
+
static int hwrand(uint8_t *buf, const size_t len) {
|
138
|
+
if (!have_rdrand()) {
|
139
|
+
return 0;
|
140
|
+
}
|
141
|
+
|
142
|
+
const size_t len_multiple8 = len & ~7;
|
143
|
+
if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
|
144
|
+
return 0;
|
145
|
+
}
|
146
|
+
const size_t remainder = len - len_multiple8;
|
147
|
+
|
148
|
+
if (remainder != 0) {
|
149
|
+
assert(remainder < 8);
|
150
|
+
|
151
|
+
uint8_t rand_buf[8];
|
152
|
+
if (!CRYPTO_rdrand(rand_buf)) {
|
153
|
+
return 0;
|
154
|
+
}
|
155
|
+
OPENSSL_memcpy(buf + len_multiple8, rand_buf, remainder);
|
156
|
+
}
|
157
|
+
|
158
|
+
#if defined(BORINGSSL_FIPS_BREAK_CRNG)
|
159
|
+
// This breaks the "continuous random number generator test" defined in FIPS
|
160
|
+
// 140-2, section 4.9.2, and implemented in rand_get_seed().
|
161
|
+
OPENSSL_memset(buf, 0, len);
|
162
|
+
#endif
|
163
|
+
|
164
|
+
return 1;
|
165
|
+
}
|
166
|
+
|
167
|
+
#else
|
168
|
+
|
169
|
+
static int hwrand(uint8_t *buf, size_t len) {
|
170
|
+
return 0;
|
171
|
+
}
|
172
|
+
|
173
|
+
#endif
|
174
|
+
|
175
|
+
#if defined(BORINGSSL_FIPS)
|
176
|
+
|
177
|
+
static void rand_get_seed(struct rand_thread_state *state,
|
178
|
+
uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
|
179
|
+
if (!state->last_block_valid) {
|
180
|
+
if (!hwrand(state->last_block, sizeof(state->last_block))) {
|
181
|
+
CRYPTO_sysrand(state->last_block, sizeof(state->last_block));
|
182
|
+
}
|
183
|
+
state->last_block_valid = 1;
|
184
|
+
}
|
185
|
+
|
186
|
+
// We overread from /dev/urandom or RDRAND by a factor of 10 and XOR to
|
187
|
+
// whiten.
|
188
|
+
#define FIPS_OVERREAD 10
|
189
|
+
uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD];
|
190
|
+
|
191
|
+
if (!hwrand(entropy, sizeof(entropy))) {
|
192
|
+
CRYPTO_sysrand(entropy, sizeof(entropy));
|
193
|
+
}
|
194
|
+
|
195
|
+
// See FIPS 140-2, section 4.9.2. This is the “continuous random number
|
196
|
+
// generator test” which causes the program to randomly abort. Hopefully the
|
197
|
+
// rate of failure is small enough not to be a problem in practice.
|
198
|
+
if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
|
199
|
+
printf("CRNGT failed.\n");
|
200
|
+
BORINGSSL_FIPS_abort();
|
201
|
+
}
|
202
|
+
|
203
|
+
for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
|
204
|
+
i += CRNGT_BLOCK_SIZE) {
|
205
|
+
if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
|
206
|
+
CRNGT_BLOCK_SIZE) == 0) {
|
207
|
+
printf("CRNGT failed.\n");
|
208
|
+
BORINGSSL_FIPS_abort();
|
209
|
+
}
|
210
|
+
}
|
211
|
+
OPENSSL_memcpy(state->last_block,
|
212
|
+
entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
|
213
|
+
CRNGT_BLOCK_SIZE);
|
214
|
+
|
215
|
+
OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
|
216
|
+
|
217
|
+
for (size_t i = 1; i < FIPS_OVERREAD; i++) {
|
218
|
+
for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) {
|
219
|
+
seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
#else
|
225
|
+
|
226
|
+
static void rand_get_seed(struct rand_thread_state *state,
|
227
|
+
uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
|
228
|
+
// If not in FIPS mode, we don't overread from the system entropy source and
|
229
|
+
// we don't depend only on the hardware RDRAND.
|
230
|
+
CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN);
|
231
|
+
}
|
232
|
+
|
233
|
+
#endif
|
234
|
+
|
235
|
+
void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
|
236
|
+
const uint8_t user_additional_data[32]) {
|
237
|
+
if (out_len == 0) {
|
238
|
+
return;
|
239
|
+
}
|
240
|
+
|
241
|
+
// Additional data is mixed into every CTR-DRBG call to protect, as best we
|
242
|
+
// can, against forks & VM clones. We do not over-read this information and
|
243
|
+
// don't reseed with it so, from the point of view of FIPS, this doesn't
|
244
|
+
// provide “prediction resistance”. But, in practice, it does.
|
245
|
+
uint8_t additional_data[32];
|
246
|
+
if (!hwrand(additional_data, sizeof(additional_data))) {
|
247
|
+
// Without a hardware RNG to save us from address-space duplication, the OS
|
248
|
+
// entropy is used. This can be expensive (one read per |RAND_bytes| call)
|
249
|
+
// and so can be disabled by applications that we have ensured don't fork
|
250
|
+
// and aren't at risk of VM cloning.
|
251
|
+
if (!rand_fork_unsafe_buffering_enabled()) {
|
252
|
+
CRYPTO_sysrand(additional_data, sizeof(additional_data));
|
253
|
+
} else {
|
254
|
+
OPENSSL_memset(additional_data, 0, sizeof(additional_data));
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
for (size_t i = 0; i < sizeof(additional_data); i++) {
|
259
|
+
additional_data[i] ^= user_additional_data[i];
|
260
|
+
}
|
261
|
+
|
262
|
+
struct rand_thread_state stack_state;
|
263
|
+
struct rand_thread_state *state =
|
264
|
+
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
|
265
|
+
|
266
|
+
if (state == NULL) {
|
267
|
+
state = OPENSSL_malloc(sizeof(struct rand_thread_state));
|
268
|
+
if (state == NULL ||
|
269
|
+
!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
|
270
|
+
rand_thread_state_free)) {
|
271
|
+
// If the system is out of memory, use an ephemeral state on the
|
272
|
+
// stack.
|
273
|
+
state = &stack_state;
|
274
|
+
}
|
275
|
+
|
276
|
+
state->last_block_valid = 0;
|
277
|
+
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
|
278
|
+
rand_get_seed(state, seed);
|
279
|
+
if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) {
|
280
|
+
abort();
|
281
|
+
}
|
282
|
+
state->calls = 0;
|
283
|
+
|
284
|
+
#if defined(BORINGSSL_FIPS)
|
285
|
+
if (state != &stack_state) {
|
286
|
+
CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
|
287
|
+
struct rand_thread_state **states_list = thread_states_list_bss_get();
|
288
|
+
state->next = *states_list;
|
289
|
+
if (state->next != NULL) {
|
290
|
+
state->next->prev = state;
|
291
|
+
}
|
292
|
+
state->prev = NULL;
|
293
|
+
*states_list = state;
|
294
|
+
CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
|
295
|
+
}
|
296
|
+
#endif
|
297
|
+
}
|
298
|
+
|
299
|
+
if (state->calls >= kReseedInterval) {
|
300
|
+
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
|
301
|
+
rand_get_seed(state, seed);
|
302
|
+
#if defined(BORINGSSL_FIPS)
|
303
|
+
// Take a read lock around accesses to |state->drbg|. This is needed to
|
304
|
+
// avoid returning bad entropy if we race with
|
305
|
+
// |rand_thread_state_clear_all|.
|
306
|
+
//
|
307
|
+
// This lock must be taken after any calls to |CRYPTO_sysrand| to avoid a
|
308
|
+
// bug on ppc64le. glibc may implement pthread locks by wrapping user code
|
309
|
+
// in a hardware transaction, but, on some older versions of glibc and the
|
310
|
+
// kernel, syscalls made with |syscall| did not abort the transaction.
|
311
|
+
CRYPTO_STATIC_MUTEX_lock_read(thread_states_list_lock_bss_get());
|
312
|
+
#endif
|
313
|
+
if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
|
314
|
+
abort();
|
315
|
+
}
|
316
|
+
state->calls = 0;
|
317
|
+
} else {
|
318
|
+
#if defined(BORINGSSL_FIPS)
|
319
|
+
CRYPTO_STATIC_MUTEX_lock_read(thread_states_list_lock_bss_get());
|
320
|
+
#endif
|
321
|
+
}
|
322
|
+
|
323
|
+
int first_call = 1;
|
324
|
+
while (out_len > 0) {
|
325
|
+
size_t todo = out_len;
|
326
|
+
if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
|
327
|
+
todo = CTR_DRBG_MAX_GENERATE_LENGTH;
|
328
|
+
}
|
329
|
+
|
330
|
+
if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
|
331
|
+
first_call ? sizeof(additional_data) : 0)) {
|
332
|
+
abort();
|
333
|
+
}
|
334
|
+
|
335
|
+
out += todo;
|
336
|
+
out_len -= todo;
|
337
|
+
state->calls++;
|
338
|
+
first_call = 0;
|
339
|
+
}
|
340
|
+
|
341
|
+
if (state == &stack_state) {
|
342
|
+
CTR_DRBG_clear(&state->drbg);
|
343
|
+
}
|
344
|
+
|
345
|
+
#if defined(BORINGSSL_FIPS)
|
346
|
+
CRYPTO_STATIC_MUTEX_unlock_read(thread_states_list_lock_bss_get());
|
347
|
+
#endif
|
348
|
+
}
|
349
|
+
|
350
|
+
int RAND_bytes(uint8_t *out, size_t out_len) {
|
351
|
+
static const uint8_t kZeroAdditionalData[32] = {0};
|
352
|
+
RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData);
|
353
|
+
return 1;
|
354
|
+
}
|
355
|
+
|
356
|
+
int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
|
357
|
+
return RAND_bytes(buf, len);
|
358
|
+
}
|