grpc 1.13.0 → 1.14.0
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 +403 -153
- data/include/grpc/grpc.h +0 -8
- data/include/grpc/grpc_security.h +59 -2
- data/include/grpc/impl/codegen/grpc_types.h +8 -2
- data/include/grpc/impl/codegen/log.h +112 -0
- data/include/grpc/module.modulemap +2 -0
- data/include/grpc/support/log.h +2 -88
- data/include/grpc/support/string_util.h +2 -0
- data/src/boringssl/err_data.c +597 -593
- data/src/core/ext/filters/client_channel/client_channel.cc +715 -770
- data/src/core/ext/filters/client_channel/client_channel.h +5 -0
- data/src/core/ext/filters/client_channel/client_channel_channelz.cc +111 -0
- data/src/core/ext/filters/client_channel/client_channel_channelz.h +69 -0
- data/src/core/ext/filters/client_channel/client_channel_plugin.cc +9 -0
- data/src/core/ext/filters/client_channel/http_proxy.cc +22 -5
- data/src/core/ext/filters/client_channel/lb_policy.h +15 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +3 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +3 -3
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +3 -1
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.c +19 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h +54 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.c +19 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h +54 -0
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +4 -17
- data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +37 -63
- data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +79 -0
- data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +5 -2
- data/src/core/ext/filters/client_channel/lb_policy_factory.cc +8 -0
- data/src/core/ext/filters/client_channel/lb_policy_factory.h +4 -0
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +2 -2
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +317 -0
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +48 -9
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +40 -293
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +106 -84
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +6 -2
- data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +6 -5
- data/src/core/ext/filters/client_channel/subchannel.cc +36 -6
- data/src/core/ext/filters/client_channel/subchannel.h +4 -0
- data/src/core/ext/filters/deadline/deadline_filter.cc +18 -15
- data/src/core/ext/filters/deadline/deadline_filter.h +5 -5
- data/src/core/ext/filters/http/client/http_client_filter.cc +10 -9
- data/src/core/ext/filters/http/server/http_server_filter.h +1 -1
- data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +1 -1
- data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +3 -2
- data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +33 -22
- data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +1 -1
- data/src/core/ext/transport/chttp2/transport/internal.h +10 -3
- data/src/core/ext/transport/chttp2/transport/stream_lists.cc +17 -0
- data/src/core/ext/transport/chttp2/transport/writing.cc +21 -16
- data/src/core/ext/transport/inproc/inproc_transport.cc +46 -6
- data/src/core/lib/channel/channel_stack.cc +22 -24
- data/src/core/lib/channel/channel_trace.cc +28 -63
- data/src/core/lib/channel/channel_trace.h +13 -17
- data/src/core/lib/channel/channelz.cc +143 -0
- data/src/core/lib/channel/channelz.h +124 -0
- data/src/core/lib/channel/channelz_registry.cc +7 -24
- data/src/core/lib/channel/channelz_registry.h +12 -8
- data/src/core/lib/channel/connected_channel.cc +8 -1
- data/src/core/{ext/filters/load_reporting/server_load_reporting_filter.h → lib/gpr/alloc.h} +7 -9
- data/src/core/lib/gpr/arena.cc +8 -8
- data/src/core/lib/gpr/string.cc +28 -0
- data/src/core/lib/gpr/string.h +10 -0
- data/src/core/lib/gprpp/abstract.h +5 -2
- data/src/core/lib/gprpp/inlined_vector.h +57 -3
- data/src/core/lib/gprpp/memory.h +2 -2
- data/src/core/lib/gprpp/ref_counted_ptr.h +5 -0
- data/src/core/lib/gprpp/thd_posix.cc +1 -1
- data/src/core/lib/iomgr/call_combiner.h +80 -0
- data/src/core/lib/iomgr/closure.h +3 -2
- data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
- data/src/core/lib/iomgr/error.cc +12 -0
- data/src/core/lib/iomgr/error.h +5 -0
- data/src/core/lib/iomgr/ev_epoll1_linux.cc +36 -9
- data/src/core/lib/iomgr/ev_epollex_linux.cc +172 -46
- data/src/core/lib/iomgr/ev_epollsig_linux.cc +47 -21
- data/src/core/lib/iomgr/ev_poll_posix.cc +10 -4
- data/src/core/lib/iomgr/ev_posix.cc +17 -9
- data/src/core/lib/iomgr/ev_posix.h +20 -4
- data/src/core/lib/iomgr/executor.cc +196 -140
- data/src/core/lib/iomgr/executor.h +47 -14
- data/src/core/lib/iomgr/iomgr.cc +2 -0
- data/src/core/lib/iomgr/iomgr.h +5 -0
- data/src/core/lib/iomgr/is_epollexclusive_available.cc +1 -0
- data/src/core/lib/iomgr/socket_utils.h +9 -0
- data/src/core/lib/iomgr/socket_utils_common_posix.cc +4 -0
- data/src/core/lib/iomgr/socket_utils_uv.cc +4 -0
- data/src/core/lib/iomgr/socket_utils_windows.cc +4 -0
- data/src/core/lib/iomgr/tcp_client_posix.cc +3 -5
- data/src/core/lib/iomgr/tcp_posix.cc +6 -1
- data/src/core/lib/iomgr/tcp_server_posix.cc +3 -3
- data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +1 -1
- data/src/core/lib/iomgr/timer_manager.cc +0 -1
- data/src/core/lib/iomgr/udp_server.cc +2 -3
- data/src/core/lib/json/json.cc +10 -0
- data/src/core/lib/json/json.h +5 -0
- data/src/core/lib/security/context/security_context.cc +8 -8
- data/src/core/lib/security/context/security_context.h +6 -2
- data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +2 -1
- data/src/core/lib/security/credentials/local/local_credentials.cc +77 -0
- data/src/core/lib/security/credentials/local/local_credentials.h +40 -0
- data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +17 -3
- data/src/core/lib/security/security_connector/local_security_connector.cc +245 -0
- data/src/core/lib/security/security_connector/local_security_connector.h +58 -0
- data/src/core/lib/security/security_connector/security_connector.cc +30 -5
- data/src/core/lib/security/security_connector/security_connector.h +1 -0
- data/src/core/lib/security/transport/client_auth_filter.cc +5 -1
- data/src/core/lib/security/transport/server_auth_filter.cc +4 -5
- data/src/core/lib/surface/call.cc +75 -32
- data/src/core/lib/surface/call.h +2 -0
- data/src/core/lib/surface/channel.cc +32 -13
- data/src/core/lib/surface/channel.h +4 -0
- data/src/core/lib/surface/version.cc +1 -1
- data/src/core/lib/transport/transport.cc +20 -9
- data/src/core/lib/transport/transport.h +12 -10
- data/src/core/lib/transport/transport_op_string.cc +0 -7
- data/src/core/plugin_registry/grpc_plugin_registry.cc +0 -4
- data/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h +2 -2
- data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +2 -1
- data/src/core/tsi/alts/handshaker/altscontext.pb.c +0 -1
- data/src/core/tsi/alts/handshaker/altscontext.pb.h +1 -2
- data/src/core/tsi/alts/handshaker/handshaker.pb.c +0 -1
- data/src/core/tsi/alts/handshaker/handshaker.pb.h +1 -2
- data/src/core/tsi/alts/handshaker/transport_security_common.pb.c +0 -1
- data/src/core/tsi/alts/handshaker/transport_security_common.pb.h +1 -1
- data/src/core/tsi/alts/handshaker/transport_security_common_api.h +2 -2
- data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.cc +47 -1
- data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.h +3 -1
- data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -11
- data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.h +7 -2
- data/src/core/tsi/local_transport_security.cc +209 -0
- data/src/core/tsi/local_transport_security.h +51 -0
- data/src/core/tsi/ssl_transport_security.cc +2 -3
- data/src/{core/ext → cpp/ext/filters}/census/grpc_context.cc +0 -0
- data/src/ruby/ext/grpc/rb_channel_credentials.c +3 -3
- data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +18 -18
- data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +29 -29
- data/src/ruby/lib/grpc/generic/active_call.rb +19 -23
- data/src/ruby/lib/grpc/version.rb +1 -1
- data/src/ruby/spec/call_credentials_spec.rb +1 -1
- data/src/ruby/spec/call_spec.rb +1 -1
- data/src/ruby/spec/channel_credentials_spec.rb +1 -1
- data/src/ruby/spec/channel_spec.rb +1 -1
- data/src/ruby/spec/client_auth_spec.rb +1 -12
- data/src/ruby/spec/client_server_spec.rb +1 -1
- data/src/ruby/spec/compression_options_spec.rb +1 -1
- data/src/ruby/spec/error_sanity_spec.rb +1 -1
- data/src/ruby/spec/generic/client_stub_spec.rb +13 -1
- data/src/ruby/spec/generic/rpc_desc_spec.rb +1 -1
- data/src/ruby/spec/generic/rpc_server_pool_spec.rb +1 -1
- data/src/ruby/spec/generic/service_spec.rb +1 -1
- data/src/ruby/spec/google_rpc_status_utils_spec.rb +1 -12
- data/src/ruby/spec/pb/duplicate/codegen_spec.rb +1 -0
- data/src/ruby/spec/pb/health/checker_spec.rb +1 -1
- data/src/ruby/spec/server_credentials_spec.rb +1 -1
- data/src/ruby/spec/server_spec.rb +1 -1
- data/src/ruby/spec/spec_helper.rb +1 -0
- data/src/ruby/spec/support/services.rb +1 -1
- data/src/ruby/spec/time_consts_spec.rb +1 -1
- data/third_party/boringssl/crypto/asn1/tasn_dec.c +40 -19
- data/third_party/boringssl/crypto/bytestring/cbs.c +1 -0
- data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +47 -15
- data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +9 -10
- data/third_party/boringssl/crypto/ecdh/ecdh.c +4 -3
- data/third_party/boringssl/crypto/fipsmodule/bn/add.c +30 -54
- data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +7 -1
- data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +8 -8
- data/third_party/boringssl/crypto/fipsmodule/bn/div.c +97 -11
- data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +274 -218
- data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +111 -34
- data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +2 -2
- data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +1 -1
- data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +24 -6
- data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +324 -63
- data/third_party/boringssl/crypto/fipsmodule/bn/random.c +74 -21
- data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +128 -86
- data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +1 -1
- data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +67 -112
- data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +8 -1
- data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +5 -5
- data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +9 -17
- data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +5378 -5418
- data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +32 -32
- data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +5 -11
- data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +16 -40
- data/third_party/boringssl/crypto/fipsmodule/rsa/internal.h +1 -6
- data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +41 -29
- data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +63 -49
- data/third_party/boringssl/crypto/x509/vpm_int.h +1 -0
- data/third_party/boringssl/crypto/x509/x509_vfy.c +4 -0
- data/third_party/boringssl/crypto/x509/x509_vpm.c +44 -22
- data/third_party/boringssl/include/openssl/aead.h +8 -2
- data/third_party/boringssl/include/openssl/asn1.h +1 -0
- data/third_party/boringssl/include/openssl/base.h +4 -0
- data/third_party/boringssl/include/openssl/bn.h +13 -3
- data/third_party/boringssl/include/openssl/bytestring.h +4 -4
- data/third_party/boringssl/include/openssl/ec.h +10 -4
- data/third_party/boringssl/include/openssl/ec_key.h +0 -3
- data/third_party/boringssl/include/openssl/rsa.h +1 -0
- data/third_party/boringssl/include/openssl/ssl.h +8 -3
- data/third_party/boringssl/include/openssl/ssl3.h +0 -1
- data/third_party/boringssl/include/openssl/x509.h +1 -0
- data/third_party/boringssl/include/openssl/x509v3.h +1 -0
- data/third_party/boringssl/ssl/handshake_client.cc +36 -64
- data/third_party/boringssl/ssl/ssl_cipher.cc +4 -0
- data/third_party/boringssl/ssl/ssl_lib.cc +1 -1
- metadata +45 -38
- data/src/core/ext/filters/load_reporting/server_load_reporting_filter.cc +0 -222
- data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.cc +0 -71
- data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.h +0 -61
- data/src/ruby/spec/pb/package_with_underscore/checker_spec.rb +0 -51
- data/src/ruby/spec/pb/package_with_underscore/data.proto +0 -23
- data/src/ruby/spec/pb/package_with_underscore/service.proto +0 -23
@@ -285,10 +285,8 @@ void bn_sqr_comba4(BN_ULONG r[8], const BN_ULONG a[4]);
|
|
285
285
|
int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len);
|
286
286
|
|
287
287
|
// bn_in_range_words returns one if |min_inclusive| <= |a| < |max_exclusive|,
|
288
|
-
// where |a| and |max_exclusive| both are |len| words long.
|
289
|
-
//
|
290
|
-
// [max_exclusive, 2^(BN_BITS2*len)) contains |a|, but otherwise the value of
|
291
|
-
// |a| is secret.
|
288
|
+
// where |a| and |max_exclusive| both are |len| words long. |a| and
|
289
|
+
// |max_exclusive| are treated as secret.
|
292
290
|
int bn_in_range_words(const BN_ULONG *a, BN_ULONG min_inclusive,
|
293
291
|
const BN_ULONG *max_exclusive, size_t len);
|
294
292
|
|
@@ -303,6 +301,27 @@ int bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive,
|
|
303
301
|
const BN_ULONG *max_exclusive, size_t len,
|
304
302
|
const uint8_t additional_data[32]);
|
305
303
|
|
304
|
+
// bn_range_secret_range behaves like |BN_rand_range_ex|, but treats
|
305
|
+
// |max_exclusive| as secret. Because of this constraint, the distribution of
|
306
|
+
// values returned is more complex.
|
307
|
+
//
|
308
|
+
// Rather than repeatedly generating values until one is in range, which would
|
309
|
+
// leak information, it generates one value. If the value is in range, it sets
|
310
|
+
// |*out_is_uniform| to one. Otherwise, it sets |*out_is_uniform| to zero,
|
311
|
+
// fixing up the value to force it in range.
|
312
|
+
//
|
313
|
+
// The subset of calls to |bn_rand_secret_range| which set |*out_is_uniform| to
|
314
|
+
// one are uniformly distributed in the target range. Calls overall are not.
|
315
|
+
// This function is intended for use in situations where the extra values are
|
316
|
+
// still usable and where the number of iterations needed to reach the target
|
317
|
+
// number of uniform outputs may be blinded for negligible probabilities of
|
318
|
+
// timing leaks.
|
319
|
+
//
|
320
|
+
// Although this function treats |max_exclusive| as secret, it treats the number
|
321
|
+
// of bits in |max_exclusive| as public.
|
322
|
+
int bn_rand_secret_range(BIGNUM *r, int *out_is_uniform, BN_ULONG min_inclusive,
|
323
|
+
const BIGNUM *max_exclusive);
|
324
|
+
|
306
325
|
int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
|
307
326
|
const BN_ULONG *np, const BN_ULONG *n0, int num);
|
308
327
|
|
@@ -323,18 +342,6 @@ int bn_mod_exp_base_2_consttime(BIGNUM *r, unsigned p, const BIGNUM *n,
|
|
323
342
|
#error "Either BN_ULLONG or BN_UMULT_LOHI must be defined on every platform."
|
324
343
|
#endif
|
325
344
|
|
326
|
-
// bn_mod_inverse_prime sets |out| to the modular inverse of |a| modulo |p|,
|
327
|
-
// computed with Fermat's Little Theorem. It returns one on success and zero on
|
328
|
-
// error. If |mont_p| is NULL, one will be computed temporarily.
|
329
|
-
int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
|
330
|
-
BN_CTX *ctx, const BN_MONT_CTX *mont_p);
|
331
|
-
|
332
|
-
// bn_mod_inverse_secret_prime behaves like |bn_mod_inverse_prime| but uses
|
333
|
-
// |BN_mod_exp_mont_consttime| instead of |BN_mod_exp_mont| in hopes of
|
334
|
-
// protecting the exponent.
|
335
|
-
int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
|
336
|
-
BN_CTX *ctx, const BN_MONT_CTX *mont_p);
|
337
|
-
|
338
345
|
// bn_jacobi returns the Jacobi symbol of |a| and |b| (which is -1, 0 or 1), or
|
339
346
|
// -2 on error.
|
340
347
|
int bn_jacobi(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
|
@@ -352,8 +359,24 @@ int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx);
|
|
352
359
|
// value for |mont| and zero otherwise.
|
353
360
|
int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont);
|
354
361
|
|
362
|
+
// bn_mod_u16_consttime returns |bn| mod |d|, ignoring |bn|'s sign bit. It runs
|
363
|
+
// in time independent of the value of |bn|, but it treats |d| as public.
|
364
|
+
OPENSSL_EXPORT uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d);
|
365
|
+
|
366
|
+
// bn_odd_number_is_obviously_composite returns one if |bn| is divisible by one
|
367
|
+
// of the first several odd primes and zero otherwise.
|
368
|
+
int bn_odd_number_is_obviously_composite(const BIGNUM *bn);
|
369
|
+
|
370
|
+
// bn_rshift1_words sets |r| to |a| >> 1, where both arrays are |num| bits wide.
|
371
|
+
void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num);
|
372
|
+
|
373
|
+
// bn_rshift_secret_shift behaves like |BN_rshift| but runs in time independent
|
374
|
+
// of both |a| and |n|.
|
375
|
+
OPENSSL_EXPORT int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a,
|
376
|
+
unsigned n, BN_CTX *ctx);
|
377
|
+
|
355
378
|
|
356
|
-
//
|
379
|
+
// Constant-time non-modular arithmetic.
|
357
380
|
//
|
358
381
|
// The following functions implement non-modular arithmetic in constant-time
|
359
382
|
// and pessimally set |r->width| to the largest possible word size.
|
@@ -362,42 +385,96 @@ int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont);
|
|
362
385
|
// to increase without bound. The corresponding public API functions minimize
|
363
386
|
// their outputs to avoid regressing calculator consumers.
|
364
387
|
|
365
|
-
//
|
388
|
+
// bn_uadd_consttime behaves like |BN_uadd|, but it pessimally sets
|
366
389
|
// |r->width| = |a->width| + |b->width| + 1.
|
367
|
-
int
|
390
|
+
int bn_uadd_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
|
368
391
|
|
369
|
-
//
|
392
|
+
// bn_usub_consttime behaves like |BN_usub|, but it pessimally sets
|
393
|
+
// |r->width| = |a->width|.
|
394
|
+
int bn_usub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
|
395
|
+
|
396
|
+
// bn_abs_sub_consttime sets |r| to the absolute value of |a| - |b|, treating
|
397
|
+
// both inputs as secret. It returns one on success and zero on error.
|
398
|
+
OPENSSL_EXPORT int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a,
|
399
|
+
const BIGNUM *b, BN_CTX *ctx);
|
400
|
+
|
401
|
+
// bn_mul_consttime behaves like |BN_mul|, but it rejects negative inputs and
|
370
402
|
// pessimally sets |r->width| to |a->width| + |b->width|, to avoid leaking
|
371
403
|
// information about |a| and |b|.
|
372
|
-
int
|
404
|
+
int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
|
405
|
+
|
406
|
+
// bn_sqrt_consttime behaves like |BN_sqrt|, but it pessimally sets |r->width|
|
407
|
+
// to 2*|a->width|, to avoid leaking information about |a| and |b|.
|
408
|
+
int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
|
409
|
+
|
410
|
+
// bn_div_consttime behaves like |BN_div|, but it rejects negative inputs and
|
411
|
+
// treats both inputs, including their magnitudes, as secret. It is, as a
|
412
|
+
// result, much slower than |BN_div| and should only be used for rare operations
|
413
|
+
// where Montgomery reduction is not available.
|
414
|
+
//
|
415
|
+
// Note that |quotient->width| will be set pessimally to |numerator->width|.
|
416
|
+
OPENSSL_EXPORT int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
417
|
+
const BIGNUM *numerator,
|
418
|
+
const BIGNUM *divisor, BN_CTX *ctx);
|
373
419
|
|
374
|
-
//
|
375
|
-
//
|
376
|
-
|
420
|
+
// bn_is_relatively_prime checks whether GCD(|x|, |y|) is one. On success, it
|
421
|
+
// returns one and sets |*out_relatively_prime| to one if the GCD was one and
|
422
|
+
// zero otherwise. On error, it returns zero.
|
423
|
+
OPENSSL_EXPORT int bn_is_relatively_prime(int *out_relatively_prime,
|
424
|
+
const BIGNUM *x, const BIGNUM *y,
|
425
|
+
BN_CTX *ctx);
|
426
|
+
|
427
|
+
// bn_lcm_consttime sets |r| to LCM(|a|, |b|). It returns one and success and
|
428
|
+
// zero on error. |a| and |b| are both treated as secret.
|
429
|
+
OPENSSL_EXPORT int bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
430
|
+
BN_CTX *ctx);
|
377
431
|
|
378
432
|
|
379
433
|
// Constant-time modular arithmetic.
|
380
434
|
//
|
381
|
-
// The following functions implement basic constant-time modular
|
382
|
-
// word arrays.
|
435
|
+
// The following functions implement basic constant-time modular arithmetic.
|
383
436
|
|
384
|
-
//
|
385
|
-
int
|
437
|
+
// bn_mod_add_consttime acts like |BN_mod_add_quick| but takes a |BN_CTX|.
|
438
|
+
int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
386
439
|
const BIGNUM *m, BN_CTX *ctx);
|
387
440
|
|
388
|
-
//
|
389
|
-
int
|
441
|
+
// bn_mod_sub_consttime acts like |BN_mod_sub_quick| but takes a |BN_CTX|.
|
442
|
+
int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
390
443
|
const BIGNUM *m, BN_CTX *ctx);
|
391
444
|
|
392
|
-
//
|
445
|
+
// bn_mod_lshift1_consttime acts like |BN_mod_lshift1_quick| but takes a
|
393
446
|
// |BN_CTX|.
|
394
|
-
int
|
447
|
+
int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
|
395
448
|
BN_CTX *ctx);
|
396
449
|
|
397
|
-
//
|
398
|
-
int
|
450
|
+
// bn_mod_lshift_consttime acts like |BN_mod_lshift_quick| but takes a |BN_CTX|.
|
451
|
+
int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
|
399
452
|
BN_CTX *ctx);
|
400
453
|
|
454
|
+
// bn_mod_inverse_consttime sets |r| to |a|^-1, mod |n|. |a| must be non-
|
455
|
+
// negative and less than |n|. It returns one on success and zero on error. On
|
456
|
+
// failure, if the failure was caused by |a| having no inverse mod |n| then
|
457
|
+
// |*out_no_inverse| will be set to one; otherwise it will be set to zero.
|
458
|
+
//
|
459
|
+
// This function treats both |a| and |n| as secret, provided they are both non-
|
460
|
+
// zero and the inverse exists. It should only be used for even moduli where
|
461
|
+
// none of the less general implementations are applicable.
|
462
|
+
OPENSSL_EXPORT int bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse,
|
463
|
+
const BIGNUM *a, const BIGNUM *n,
|
464
|
+
BN_CTX *ctx);
|
465
|
+
|
466
|
+
// bn_mod_inverse_prime sets |out| to the modular inverse of |a| modulo |p|,
|
467
|
+
// computed with Fermat's Little Theorem. It returns one on success and zero on
|
468
|
+
// error. If |mont_p| is NULL, one will be computed temporarily.
|
469
|
+
int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
|
470
|
+
BN_CTX *ctx, const BN_MONT_CTX *mont_p);
|
471
|
+
|
472
|
+
// bn_mod_inverse_secret_prime behaves like |bn_mod_inverse_prime| but uses
|
473
|
+
// |BN_mod_exp_mont_consttime| instead of |BN_mod_exp_mont| in hopes of
|
474
|
+
// protecting the exponent.
|
475
|
+
int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
|
476
|
+
BN_CTX *ctx, const BN_MONT_CTX *mont_p);
|
477
|
+
|
401
478
|
|
402
479
|
// Low-level operations for small numbers.
|
403
480
|
//
|
@@ -381,11 +381,11 @@ static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a,
|
|
381
381
|
}
|
382
382
|
|
383
383
|
if (a == b) {
|
384
|
-
if (!
|
384
|
+
if (!bn_sqr_consttime(tmp, a, ctx)) {
|
385
385
|
goto err;
|
386
386
|
}
|
387
387
|
} else {
|
388
|
-
if (!
|
388
|
+
if (!bn_mul_consttime(tmp, a, b, ctx)) {
|
389
389
|
goto err;
|
390
390
|
}
|
391
391
|
}
|
@@ -177,7 +177,7 @@ int bn_mod_exp_base_2_consttime(BIGNUM *r, unsigned p, const BIGNUM *n,
|
|
177
177
|
// Set |r| to the larger power of two smaller than |n|, then shift with
|
178
178
|
// reductions the rest of the way.
|
179
179
|
if (!BN_set_bit(r, n_bits - 1) ||
|
180
|
-
!
|
180
|
+
!bn_mod_lshift_consttime(r, r, p - (n_bits - 1), n, ctx)) {
|
181
181
|
return 0;
|
182
182
|
}
|
183
183
|
|
@@ -306,6 +306,24 @@ static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
|
|
306
306
|
return borrow;
|
307
307
|
}
|
308
308
|
|
309
|
+
int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
310
|
+
BN_CTX *ctx) {
|
311
|
+
int cl = a->width < b->width ? a->width : b->width;
|
312
|
+
int dl = a->width - b->width;
|
313
|
+
int r_len = a->width < b->width ? b->width : a->width;
|
314
|
+
BN_CTX_start(ctx);
|
315
|
+
BIGNUM *tmp = BN_CTX_get(ctx);
|
316
|
+
int ok = tmp != NULL &&
|
317
|
+
bn_wexpand(r, r_len) &&
|
318
|
+
bn_wexpand(tmp, r_len);
|
319
|
+
if (ok) {
|
320
|
+
bn_abs_sub_part_words(r->d, a->d, b->d, cl, dl, tmp->d);
|
321
|
+
r->width = r_len;
|
322
|
+
}
|
323
|
+
BN_CTX_end(ctx);
|
324
|
+
return ok;
|
325
|
+
}
|
326
|
+
|
309
327
|
// Karatsuba recursive multiplication algorithm
|
310
328
|
// (cf. Knuth, The Art of Computer Programming, Vol. 2)
|
311
329
|
|
@@ -523,9 +541,9 @@ static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a,
|
|
523
541
|
assert(c == 0);
|
524
542
|
}
|
525
543
|
|
526
|
-
// bn_mul_impl implements |BN_mul| and |
|
527
|
-
// |BIGNUM| invariants and may return a negative zero. This is handled by
|
528
|
-
// callers.
|
544
|
+
// bn_mul_impl implements |BN_mul| and |bn_mul_consttime|. Note this function
|
545
|
+
// breaks |BIGNUM| invariants and may return a negative zero. This is handled by
|
546
|
+
// the callers.
|
529
547
|
static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
530
548
|
BN_CTX *ctx) {
|
531
549
|
int al = a->width;
|
@@ -628,7 +646,7 @@ int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
|
|
628
646
|
return 1;
|
629
647
|
}
|
630
648
|
|
631
|
-
int
|
649
|
+
int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
|
632
650
|
// Prevent negative zeros.
|
633
651
|
if (a->neg || b->neg) {
|
634
652
|
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
|
@@ -773,7 +791,7 @@ int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
|
|
773
791
|
return 1;
|
774
792
|
}
|
775
793
|
|
776
|
-
int
|
794
|
+
int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
|
777
795
|
int al = a->width;
|
778
796
|
if (al <= 0) {
|
779
797
|
r->width = 0;
|
@@ -832,7 +850,7 @@ err:
|
|
832
850
|
}
|
833
851
|
|
834
852
|
int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
|
835
|
-
if (!
|
853
|
+
if (!bn_sqr_consttime(r, a, ctx)) {
|
836
854
|
return 0;
|
837
855
|
}
|
838
856
|
|
@@ -112,6 +112,8 @@
|
|
112
112
|
#include <openssl/mem.h>
|
113
113
|
|
114
114
|
#include "internal.h"
|
115
|
+
#include "../../internal.h"
|
116
|
+
|
115
117
|
|
116
118
|
// The quick sieve algorithm approach to weeding out primes is Philip
|
117
119
|
// Zimmermann's, as implemented in PGP. I have had a read of his comments and
|
@@ -341,6 +343,67 @@ static int BN_prime_checks_for_size(int bits) {
|
|
341
343
|
return 28;
|
342
344
|
}
|
343
345
|
|
346
|
+
// BN_PRIME_CHECKS_BLINDED is the iteration count for blinding the constant-time
|
347
|
+
// primality test. See |BN_primality_test| for details. This number is selected
|
348
|
+
// so that, for a candidate N-bit RSA prime, picking |BN_PRIME_CHECKS_BLINDED|
|
349
|
+
// random N-bit numbers will have at least |BN_prime_checks_for_size(N)| values
|
350
|
+
// in range with high probability.
|
351
|
+
//
|
352
|
+
// The following Python script computes the blinding factor needed for the
|
353
|
+
// corresponding iteration count.
|
354
|
+
/*
|
355
|
+
import math
|
356
|
+
|
357
|
+
# We choose candidate RSA primes between sqrt(2)/2 * 2^N and 2^N and select
|
358
|
+
# witnesses by generating random N-bit numbers. Thus the probability of
|
359
|
+
# selecting one in range is at least sqrt(2)/2.
|
360
|
+
p = math.sqrt(2) / 2
|
361
|
+
|
362
|
+
# Target around 2^-8 probability of the blinding being insufficient given that
|
363
|
+
# key generation is a one-time, noisy operation.
|
364
|
+
epsilon = 2**-8
|
365
|
+
|
366
|
+
def choose(a, b):
|
367
|
+
r = 1
|
368
|
+
for i in xrange(b):
|
369
|
+
r *= a - i
|
370
|
+
r /= (i + 1)
|
371
|
+
return r
|
372
|
+
|
373
|
+
def failure_rate(min_uniform, iterations):
|
374
|
+
""" Returns the probability that, for |iterations| candidate witnesses, fewer
|
375
|
+
than |min_uniform| of them will be uniform. """
|
376
|
+
prob = 0.0
|
377
|
+
for i in xrange(min_uniform):
|
378
|
+
prob += (choose(iterations, i) *
|
379
|
+
p**i * (1-p)**(iterations - i))
|
380
|
+
return prob
|
381
|
+
|
382
|
+
for min_uniform in (3, 4, 5, 6, 8, 13, 19, 28):
|
383
|
+
# Find the smallest number of iterations under the target failure rate.
|
384
|
+
iterations = min_uniform
|
385
|
+
while True:
|
386
|
+
prob = failure_rate(min_uniform, iterations)
|
387
|
+
if prob < epsilon:
|
388
|
+
print min_uniform, iterations, prob
|
389
|
+
break
|
390
|
+
iterations += 1
|
391
|
+
|
392
|
+
Output:
|
393
|
+
3 9 0.00368894873911
|
394
|
+
4 11 0.00363319494662
|
395
|
+
5 13 0.00336215573898
|
396
|
+
6 15 0.00300145783158
|
397
|
+
8 19 0.00225214119331
|
398
|
+
13 27 0.00385610026955
|
399
|
+
19 38 0.0021410539126
|
400
|
+
28 52 0.00325405801769
|
401
|
+
|
402
|
+
16 iterations suffices for 400-bit primes and larger (6 uniform samples needed),
|
403
|
+
which is already well below the minimum acceptable key size for RSA.
|
404
|
+
*/
|
405
|
+
#define BN_PRIME_CHECKS_BLINDED 16
|
406
|
+
|
344
407
|
static int probable_prime(BIGNUM *rnd, int bits);
|
345
408
|
static int probable_prime_dh(BIGNUM *rnd, int bits, const BIGNUM *add,
|
346
409
|
const BIGNUM *rem, BN_CTX *ctx);
|
@@ -461,81 +524,291 @@ err:
|
|
461
524
|
return found;
|
462
525
|
}
|
463
526
|
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
527
|
+
// The following functions use a Barrett reduction variant to avoid leaking the
|
528
|
+
// numerator. See http://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
|
529
|
+
//
|
530
|
+
// We use 32-bit numerator and 16-bit divisor for simplicity. This allows
|
531
|
+
// computing |m| and |q| without architecture-specific code.
|
532
|
+
|
533
|
+
// mod_u16 returns |n| mod |d|. |p| and |m| are the "magic numbers" for |d| (see
|
534
|
+
// reference). For proof of correctness in Coq, see
|
535
|
+
// https://github.com/davidben/fiat-crypto/blob/barrett/src/Arithmetic/BarrettReduction/RidiculousFish.v
|
536
|
+
// Note the Coq version of |mod_u16| additionally includes the computation of
|
537
|
+
// |p| and |m| from |bn_mod_u16_consttime| below.
|
538
|
+
static uint16_t mod_u16(uint32_t n, uint16_t d, uint32_t p, uint32_t m) {
|
539
|
+
// Compute floor(n/d) per steps 3 through 5.
|
540
|
+
uint32_t q = ((uint64_t)m * n) >> 32;
|
541
|
+
// Note there is a typo in the reference. We right-shift by one, not two.
|
542
|
+
uint32_t t = ((n - q) >> 1) + q;
|
543
|
+
t = t >> (p - 1);
|
544
|
+
|
545
|
+
// Multiply and subtract to get the remainder.
|
546
|
+
n -= d * t;
|
547
|
+
assert(n < d);
|
548
|
+
return n;
|
549
|
+
}
|
550
|
+
|
551
|
+
// shift_and_add_mod_u16 returns |r| * 2^32 + |a| mod |d|. |p| and |m| are the
|
552
|
+
// "magic numbers" for |d| (see reference).
|
553
|
+
static uint16_t shift_and_add_mod_u16(uint16_t r, uint32_t a, uint16_t d,
|
554
|
+
uint32_t p, uint32_t m) {
|
555
|
+
// Incorporate |a| in two 16-bit chunks.
|
556
|
+
uint32_t t = r;
|
557
|
+
t <<= 16;
|
558
|
+
t |= a >> 16;
|
559
|
+
t = mod_u16(t, d, p, m);
|
560
|
+
|
561
|
+
t <<= 16;
|
562
|
+
t |= a & 0xffff;
|
563
|
+
t = mod_u16(t, d, p, m);
|
564
|
+
return t;
|
565
|
+
}
|
566
|
+
|
567
|
+
uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d) {
|
568
|
+
if (d <= 1) {
|
569
|
+
return 0;
|
570
|
+
}
|
571
|
+
|
572
|
+
// Compute the "magic numbers" for |d|. See steps 1 and 2.
|
573
|
+
// This computes p = ceil(log_2(d)).
|
574
|
+
uint32_t p = BN_num_bits_word(d - 1);
|
575
|
+
// This operation is not constant-time, but |p| and |d| are public values.
|
576
|
+
// Note that |p| is at most 16, so the computation fits in |uint64_t|.
|
577
|
+
assert(p <= 16);
|
578
|
+
uint32_t m = ((UINT64_C(1) << (32 + p)) + d - 1) / d;
|
579
|
+
|
580
|
+
uint16_t ret = 0;
|
581
|
+
for (int i = bn->width - 1; i >= 0; i--) {
|
582
|
+
#if BN_BITS2 == 32
|
583
|
+
ret = shift_and_add_mod_u16(ret, bn->d[i], d, p, m);
|
584
|
+
#elif BN_BITS2 == 64
|
585
|
+
ret = shift_and_add_mod_u16(ret, bn->d[i] >> 32, d, p, m);
|
586
|
+
ret = shift_and_add_mod_u16(ret, bn->d[i] & 0xffffffff, d, p, m);
|
587
|
+
#else
|
588
|
+
#error "Unknown BN_ULONG size"
|
589
|
+
#endif
|
590
|
+
}
|
591
|
+
return ret;
|
592
|
+
}
|
593
|
+
|
594
|
+
static int bn_trial_division(uint16_t *out, const BIGNUM *bn) {
|
595
|
+
for (int i = 1; i < NUMPRIMES; i++) {
|
596
|
+
if (bn_mod_u16_consttime(bn, primes[i]) == 0) {
|
597
|
+
*out = primes[i];
|
473
598
|
return 1;
|
474
|
-
|
475
|
-
*is_probably_prime = 0;
|
476
|
-
return 0;
|
599
|
+
}
|
477
600
|
}
|
601
|
+
return 0;
|
478
602
|
}
|
479
603
|
|
480
|
-
int
|
481
|
-
|
604
|
+
int bn_odd_number_is_obviously_composite(const BIGNUM *bn) {
|
605
|
+
uint16_t prime;
|
606
|
+
return bn_trial_division(&prime, bn) && !BN_is_word(bn, prime);
|
482
607
|
}
|
483
608
|
|
484
|
-
int
|
485
|
-
|
486
|
-
|
487
|
-
|
609
|
+
int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
|
610
|
+
int iterations, BN_CTX *ctx, int do_trial_division,
|
611
|
+
BN_GENCB *cb) {
|
612
|
+
*is_probably_prime = 0;
|
613
|
+
|
614
|
+
// To support RSA key generation, this function should treat |w| as secret if
|
615
|
+
// it is a large prime. Composite numbers are discarded, so they may return
|
616
|
+
// early.
|
617
|
+
|
618
|
+
if (BN_cmp(w, BN_value_one()) <= 0) {
|
619
|
+
return 1;
|
488
620
|
}
|
489
621
|
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
return
|
622
|
+
if (!BN_is_odd(w)) {
|
623
|
+
// The only even prime is two.
|
624
|
+
*is_probably_prime = BN_is_word(w, 2);
|
625
|
+
return 1;
|
494
626
|
}
|
495
627
|
|
496
|
-
//
|
497
|
-
if (BN_is_word(
|
628
|
+
// Miller-Rabin does not work for three.
|
629
|
+
if (BN_is_word(w, 3)) {
|
630
|
+
*is_probably_prime = 1;
|
498
631
|
return 1;
|
499
632
|
}
|
500
633
|
|
501
634
|
if (do_trial_division) {
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
if (mod == 0) {
|
508
|
-
return BN_is_word(a, primes[i]);
|
509
|
-
}
|
635
|
+
// Perform additional trial division checks to discard small primes.
|
636
|
+
uint16_t prime;
|
637
|
+
if (bn_trial_division(&prime, w)) {
|
638
|
+
*is_probably_prime = BN_is_word(w, prime);
|
639
|
+
return 1;
|
510
640
|
}
|
511
|
-
|
512
641
|
if (!BN_GENCB_call(cb, 1, -1)) {
|
513
|
-
return
|
642
|
+
return 0;
|
514
643
|
}
|
515
644
|
}
|
516
645
|
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
646
|
+
if (iterations == BN_prime_checks) {
|
647
|
+
iterations = BN_prime_checks_for_size(BN_num_bits(w));
|
648
|
+
}
|
649
|
+
|
650
|
+
// See C.3.1 from FIPS 186-4.
|
651
|
+
int ret = 0;
|
652
|
+
BN_MONT_CTX *mont = NULL;
|
653
|
+
BN_CTX_start(ctx);
|
654
|
+
BIGNUM *w1 = BN_CTX_get(ctx);
|
655
|
+
if (w1 == NULL ||
|
656
|
+
!bn_usub_consttime(w1, w, BN_value_one())) {
|
657
|
+
goto err;
|
658
|
+
}
|
659
|
+
|
660
|
+
// Write w1 as m * 2^a (Steps 1 and 2).
|
661
|
+
int w_len = BN_num_bits(w);
|
662
|
+
int a = BN_count_low_zero_bits(w1);
|
663
|
+
BIGNUM *m = BN_CTX_get(ctx);
|
664
|
+
if (m == NULL ||
|
665
|
+
!bn_rshift_secret_shift(m, w1, a, ctx)) {
|
666
|
+
goto err;
|
525
667
|
}
|
526
668
|
|
527
|
-
|
528
|
-
|
669
|
+
// Montgomery setup for computations mod w. Additionally, compute 1 and w - 1
|
670
|
+
// in the Montgomery domain for later comparisons.
|
671
|
+
BIGNUM *b = BN_CTX_get(ctx);
|
672
|
+
BIGNUM *z = BN_CTX_get(ctx);
|
673
|
+
BIGNUM *one_mont = BN_CTX_get(ctx);
|
674
|
+
BIGNUM *w1_mont = BN_CTX_get(ctx);
|
675
|
+
mont = BN_MONT_CTX_new_for_modulus(w, ctx);
|
676
|
+
if (b == NULL || z == NULL || one_mont == NULL || w1_mont == NULL ||
|
677
|
+
mont == NULL ||
|
678
|
+
!bn_one_to_montgomery(one_mont, mont, ctx) ||
|
679
|
+
// w - 1 is -1 mod w, so we can compute it in the Montgomery domain, -R,
|
680
|
+
// with a subtraction. (|one_mont| cannot be zero.)
|
681
|
+
!bn_usub_consttime(w1_mont, w, one_mont)) {
|
529
682
|
goto err;
|
530
683
|
}
|
531
684
|
|
532
|
-
|
685
|
+
// The following loop performs in inner iteration of the Miller-Rabin
|
686
|
+
// Primality test (Step 4).
|
687
|
+
//
|
688
|
+
// The algorithm as specified in FIPS 186-4 leaks information on |w|, the RSA
|
689
|
+
// private key. Instead, we run through each iteration unconditionally,
|
690
|
+
// performing modular multiplications, masking off any effects to behave
|
691
|
+
// equivalently to the specified algorithm.
|
692
|
+
//
|
693
|
+
// We also blind the number of values of |b| we try. Steps 4.1–4.2 say to
|
694
|
+
// discard out-of-range values. To avoid leaking information on |w|, we use
|
695
|
+
// |bn_rand_secret_range| which, rather than discarding bad values, adjusts
|
696
|
+
// them to be in range. Though not uniformly selected, these adjusted values
|
697
|
+
// are still usable as Rabin-Miller checks.
|
698
|
+
//
|
699
|
+
// Rabin-Miller is already probabilistic, so we could reach the desired
|
700
|
+
// confidence levels by just suitably increasing the iteration count. However,
|
701
|
+
// to align with FIPS 186-4, we use a more pessimal analysis: we do not count
|
702
|
+
// the non-uniform values towards the iteration count. As a result, this
|
703
|
+
// function is more complex and has more timing risk than necessary.
|
704
|
+
//
|
705
|
+
// We count both total iterations and uniform ones and iterate until we've
|
706
|
+
// reached at least |BN_PRIME_CHECKS_BLINDED| and |iterations|, respectively.
|
707
|
+
// If the latter is large enough, it will be the limiting factor with high
|
708
|
+
// probability and we won't leak information.
|
709
|
+
//
|
710
|
+
// Note this blinding does not impact most calls when picking primes because
|
711
|
+
// composites are rejected early. Only the two secret primes see extra work.
|
712
|
+
|
713
|
+
crypto_word_t uniform_iterations = 0;
|
714
|
+
// Using |constant_time_lt_w| seems to prevent the compiler from optimizing
|
715
|
+
// this into two jumps.
|
716
|
+
for (int i = 1; (i <= BN_PRIME_CHECKS_BLINDED) |
|
717
|
+
constant_time_lt_w(uniform_iterations, iterations);
|
718
|
+
i++) {
|
719
|
+
int is_uniform;
|
720
|
+
if (// Step 4.1-4.2
|
721
|
+
!bn_rand_secret_range(b, &is_uniform, 2, w1) ||
|
722
|
+
// Step 4.3
|
723
|
+
!BN_mod_exp_mont_consttime(z, b, m, w, ctx, mont)) {
|
724
|
+
goto err;
|
725
|
+
}
|
726
|
+
uniform_iterations += is_uniform;
|
727
|
+
|
728
|
+
// loop_done is all ones if the loop has completed and all zeros otherwise.
|
729
|
+
crypto_word_t loop_done = 0;
|
730
|
+
// next_iteration is all ones if we should continue to the next iteration
|
731
|
+
// (|b| is not a composite witness for |w|). This is equivalent to going to
|
732
|
+
// step 4.7 in the original algorithm.
|
733
|
+
crypto_word_t next_iteration = 0;
|
734
|
+
|
735
|
+
// Step 4.4. If z = 1 or z = w-1, mask off the loop and continue to the next
|
736
|
+
// iteration (go to step 4.7).
|
737
|
+
loop_done = BN_equal_consttime(z, BN_value_one()) |
|
738
|
+
BN_equal_consttime(z, w1);
|
739
|
+
loop_done = 0 - loop_done; // Make it all zeros or all ones.
|
740
|
+
next_iteration = loop_done; // Go to step 4.7 if |loop_done|.
|
741
|
+
|
742
|
+
// Step 4.5. We use Montgomery-encoding for better performance and to avoid
|
743
|
+
// timing leaks.
|
744
|
+
if (!BN_to_montgomery(z, z, mont, ctx)) {
|
745
|
+
goto err;
|
746
|
+
}
|
747
|
+
|
748
|
+
// To avoid leaking |a|, we run the loop to |w_len| and mask off all
|
749
|
+
// iterations once |j| = |a|.
|
750
|
+
for (int j = 1; j < w_len; j++) {
|
751
|
+
loop_done |= constant_time_eq_int(j, a);
|
752
|
+
|
753
|
+
// Step 4.5.1.
|
754
|
+
if (!BN_mod_mul_montgomery(z, z, z, mont, ctx)) {
|
755
|
+
goto err;
|
756
|
+
}
|
757
|
+
|
758
|
+
// Step 4.5.2. If z = w-1 and the loop is not done, run through the next
|
759
|
+
// iteration.
|
760
|
+
crypto_word_t z_is_w1_mont = BN_equal_consttime(z, w1_mont) & ~loop_done;
|
761
|
+
z_is_w1_mont = 0 - z_is_w1_mont; // Make it all zeros or all ones.
|
762
|
+
loop_done |= z_is_w1_mont;
|
763
|
+
next_iteration |= z_is_w1_mont; // Go to step 4.7 if |z_is_w1_mont|.
|
764
|
+
|
765
|
+
// Step 4.5.3. If z = 1 and the loop is not done, w is composite and we
|
766
|
+
// may exit in variable time.
|
767
|
+
if (BN_equal_consttime(z, one_mont) & ~loop_done) {
|
768
|
+
assert(!next_iteration);
|
769
|
+
break;
|
770
|
+
}
|
771
|
+
}
|
772
|
+
|
773
|
+
if (!next_iteration) {
|
774
|
+
// Step 4.6. We did not see z = w-1 before z = 1, so w must be composite.
|
775
|
+
// (For any prime, the value of z immediately preceding 1 must be -1.
|
776
|
+
// There are no non-trivial square roots of 1 modulo a prime.)
|
777
|
+
*is_probably_prime = 0;
|
778
|
+
ret = 1;
|
779
|
+
goto err;
|
780
|
+
}
|
781
|
+
|
782
|
+
// Step 4.7
|
783
|
+
if (!BN_GENCB_call(cb, 1, i)) {
|
784
|
+
goto err;
|
785
|
+
}
|
786
|
+
}
|
787
|
+
|
788
|
+
assert(uniform_iterations >= (crypto_word_t)iterations);
|
789
|
+
*is_probably_prime = 1;
|
790
|
+
ret = 1;
|
533
791
|
|
534
792
|
err:
|
535
|
-
|
793
|
+
BN_MONT_CTX_free(mont);
|
794
|
+
BN_CTX_end(ctx);
|
536
795
|
return ret;
|
537
796
|
}
|
538
797
|
|
798
|
+
int BN_is_prime_ex(const BIGNUM *candidate, int checks, BN_CTX *ctx, BN_GENCB *cb) {
|
799
|
+
return BN_is_prime_fasttest_ex(candidate, checks, ctx, 0, cb);
|
800
|
+
}
|
801
|
+
|
802
|
+
int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx,
|
803
|
+
int do_trial_division, BN_GENCB *cb) {
|
804
|
+
int is_probably_prime;
|
805
|
+
if (!BN_primality_test(&is_probably_prime, a, checks, ctx, do_trial_division,
|
806
|
+
cb)) {
|
807
|
+
return -1;
|
808
|
+
}
|
809
|
+
return is_probably_prime;
|
810
|
+
}
|
811
|
+
|
539
812
|
int BN_enhanced_miller_rabin_primality_test(
|
540
813
|
enum bn_primality_result_t *out_result, const BIGNUM *w, int iterations,
|
541
814
|
BN_CTX *ctx, BN_GENCB *cb) {
|
@@ -585,7 +858,7 @@ int BN_enhanced_miller_rabin_primality_test(
|
|
585
858
|
goto err;
|
586
859
|
}
|
587
860
|
|
588
|
-
// Montgomery setup for computations mod
|
861
|
+
// Montgomery setup for computations mod w
|
589
862
|
mont = BN_MONT_CTX_new_for_modulus(w, ctx);
|
590
863
|
if (mont == NULL) {
|
591
864
|
goto err;
|
@@ -689,11 +962,7 @@ again:
|
|
689
962
|
|
690
963
|
// we now have a random number 'rnd' to test.
|
691
964
|
for (i = 1; i < NUMPRIMES; i++) {
|
692
|
-
|
693
|
-
if (mod == (BN_ULONG)-1) {
|
694
|
-
return 0;
|
695
|
-
}
|
696
|
-
mods[i] = (uint16_t)mod;
|
965
|
+
mods[i] = bn_mod_u16_consttime(rnd, primes[i]);
|
697
966
|
}
|
698
967
|
// If bits is so small that it fits into a single word then we
|
699
968
|
// additionally don't want to exceed that many bits.
|
@@ -793,11 +1062,7 @@ static int probable_prime_dh(BIGNUM *rnd, int bits, const BIGNUM *add,
|
|
793
1062
|
loop:
|
794
1063
|
for (i = 1; i < NUMPRIMES; i++) {
|
795
1064
|
// check that rnd is a prime
|
796
|
-
|
797
|
-
if (mod == (BN_ULONG)-1) {
|
798
|
-
goto err;
|
799
|
-
}
|
800
|
-
if (mod <= 1) {
|
1065
|
+
if (bn_mod_u16_consttime(rnd, primes[i]) <= 1) {
|
801
1066
|
if (!BN_add(rnd, rnd, add)) {
|
802
1067
|
goto err;
|
803
1068
|
}
|
@@ -869,12 +1134,8 @@ loop:
|
|
869
1134
|
// check that p and q are prime
|
870
1135
|
// check that for p and q
|
871
1136
|
// gcd(p-1,primes) == 1 (except for 2)
|
872
|
-
|
873
|
-
|
874
|
-
if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1) {
|
875
|
-
goto err;
|
876
|
-
}
|
877
|
-
if (pmod == 0 || qmod == 0) {
|
1137
|
+
if (bn_mod_u16_consttime(p, primes[i]) == 0 ||
|
1138
|
+
bn_mod_u16_consttime(q, primes[i]) == 0) {
|
878
1139
|
if (!BN_add(p, p, padd)) {
|
879
1140
|
goto err;
|
880
1141
|
}
|