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.

Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +403 -153
  3. data/include/grpc/grpc.h +0 -8
  4. data/include/grpc/grpc_security.h +59 -2
  5. data/include/grpc/impl/codegen/grpc_types.h +8 -2
  6. data/include/grpc/impl/codegen/log.h +112 -0
  7. data/include/grpc/module.modulemap +2 -0
  8. data/include/grpc/support/log.h +2 -88
  9. data/include/grpc/support/string_util.h +2 -0
  10. data/src/boringssl/err_data.c +597 -593
  11. data/src/core/ext/filters/client_channel/client_channel.cc +715 -770
  12. data/src/core/ext/filters/client_channel/client_channel.h +5 -0
  13. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +111 -0
  14. data/src/core/ext/filters/client_channel/client_channel_channelz.h +69 -0
  15. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +9 -0
  16. data/src/core/ext/filters/client_channel/http_proxy.cc +22 -5
  17. data/src/core/ext/filters/client_channel/lb_policy.h +15 -0
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +3 -0
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +3 -3
  20. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +3 -1
  21. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.c +19 -0
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h +54 -0
  23. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.c +19 -0
  24. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h +54 -0
  25. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +4 -17
  26. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +37 -63
  27. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +79 -0
  28. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +5 -2
  29. data/src/core/ext/filters/client_channel/lb_policy_factory.cc +8 -0
  30. data/src/core/ext/filters/client_channel/lb_policy_factory.h +4 -0
  31. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +2 -2
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +317 -0
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +48 -9
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +40 -293
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +106 -84
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +6 -2
  37. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +6 -5
  38. data/src/core/ext/filters/client_channel/subchannel.cc +36 -6
  39. data/src/core/ext/filters/client_channel/subchannel.h +4 -0
  40. data/src/core/ext/filters/deadline/deadline_filter.cc +18 -15
  41. data/src/core/ext/filters/deadline/deadline_filter.h +5 -5
  42. data/src/core/ext/filters/http/client/http_client_filter.cc +10 -9
  43. data/src/core/ext/filters/http/server/http_server_filter.h +1 -1
  44. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +1 -1
  45. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +3 -2
  46. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +33 -22
  47. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +1 -1
  48. data/src/core/ext/transport/chttp2/transport/internal.h +10 -3
  49. data/src/core/ext/transport/chttp2/transport/stream_lists.cc +17 -0
  50. data/src/core/ext/transport/chttp2/transport/writing.cc +21 -16
  51. data/src/core/ext/transport/inproc/inproc_transport.cc +46 -6
  52. data/src/core/lib/channel/channel_stack.cc +22 -24
  53. data/src/core/lib/channel/channel_trace.cc +28 -63
  54. data/src/core/lib/channel/channel_trace.h +13 -17
  55. data/src/core/lib/channel/channelz.cc +143 -0
  56. data/src/core/lib/channel/channelz.h +124 -0
  57. data/src/core/lib/channel/channelz_registry.cc +7 -24
  58. data/src/core/lib/channel/channelz_registry.h +12 -8
  59. data/src/core/lib/channel/connected_channel.cc +8 -1
  60. data/src/core/{ext/filters/load_reporting/server_load_reporting_filter.h → lib/gpr/alloc.h} +7 -9
  61. data/src/core/lib/gpr/arena.cc +8 -8
  62. data/src/core/lib/gpr/string.cc +28 -0
  63. data/src/core/lib/gpr/string.h +10 -0
  64. data/src/core/lib/gprpp/abstract.h +5 -2
  65. data/src/core/lib/gprpp/inlined_vector.h +57 -3
  66. data/src/core/lib/gprpp/memory.h +2 -2
  67. data/src/core/lib/gprpp/ref_counted_ptr.h +5 -0
  68. data/src/core/lib/gprpp/thd_posix.cc +1 -1
  69. data/src/core/lib/iomgr/call_combiner.h +80 -0
  70. data/src/core/lib/iomgr/closure.h +3 -2
  71. data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
  72. data/src/core/lib/iomgr/error.cc +12 -0
  73. data/src/core/lib/iomgr/error.h +5 -0
  74. data/src/core/lib/iomgr/ev_epoll1_linux.cc +36 -9
  75. data/src/core/lib/iomgr/ev_epollex_linux.cc +172 -46
  76. data/src/core/lib/iomgr/ev_epollsig_linux.cc +47 -21
  77. data/src/core/lib/iomgr/ev_poll_posix.cc +10 -4
  78. data/src/core/lib/iomgr/ev_posix.cc +17 -9
  79. data/src/core/lib/iomgr/ev_posix.h +20 -4
  80. data/src/core/lib/iomgr/executor.cc +196 -140
  81. data/src/core/lib/iomgr/executor.h +47 -14
  82. data/src/core/lib/iomgr/iomgr.cc +2 -0
  83. data/src/core/lib/iomgr/iomgr.h +5 -0
  84. data/src/core/lib/iomgr/is_epollexclusive_available.cc +1 -0
  85. data/src/core/lib/iomgr/socket_utils.h +9 -0
  86. data/src/core/lib/iomgr/socket_utils_common_posix.cc +4 -0
  87. data/src/core/lib/iomgr/socket_utils_uv.cc +4 -0
  88. data/src/core/lib/iomgr/socket_utils_windows.cc +4 -0
  89. data/src/core/lib/iomgr/tcp_client_posix.cc +3 -5
  90. data/src/core/lib/iomgr/tcp_posix.cc +6 -1
  91. data/src/core/lib/iomgr/tcp_server_posix.cc +3 -3
  92. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +1 -1
  93. data/src/core/lib/iomgr/timer_manager.cc +0 -1
  94. data/src/core/lib/iomgr/udp_server.cc +2 -3
  95. data/src/core/lib/json/json.cc +10 -0
  96. data/src/core/lib/json/json.h +5 -0
  97. data/src/core/lib/security/context/security_context.cc +8 -8
  98. data/src/core/lib/security/context/security_context.h +6 -2
  99. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +2 -1
  100. data/src/core/lib/security/credentials/local/local_credentials.cc +77 -0
  101. data/src/core/lib/security/credentials/local/local_credentials.h +40 -0
  102. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +17 -3
  103. data/src/core/lib/security/security_connector/local_security_connector.cc +245 -0
  104. data/src/core/lib/security/security_connector/local_security_connector.h +58 -0
  105. data/src/core/lib/security/security_connector/security_connector.cc +30 -5
  106. data/src/core/lib/security/security_connector/security_connector.h +1 -0
  107. data/src/core/lib/security/transport/client_auth_filter.cc +5 -1
  108. data/src/core/lib/security/transport/server_auth_filter.cc +4 -5
  109. data/src/core/lib/surface/call.cc +75 -32
  110. data/src/core/lib/surface/call.h +2 -0
  111. data/src/core/lib/surface/channel.cc +32 -13
  112. data/src/core/lib/surface/channel.h +4 -0
  113. data/src/core/lib/surface/version.cc +1 -1
  114. data/src/core/lib/transport/transport.cc +20 -9
  115. data/src/core/lib/transport/transport.h +12 -10
  116. data/src/core/lib/transport/transport_op_string.cc +0 -7
  117. data/src/core/plugin_registry/grpc_plugin_registry.cc +0 -4
  118. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h +2 -2
  119. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +2 -1
  120. data/src/core/tsi/alts/handshaker/altscontext.pb.c +0 -1
  121. data/src/core/tsi/alts/handshaker/altscontext.pb.h +1 -2
  122. data/src/core/tsi/alts/handshaker/handshaker.pb.c +0 -1
  123. data/src/core/tsi/alts/handshaker/handshaker.pb.h +1 -2
  124. data/src/core/tsi/alts/handshaker/transport_security_common.pb.c +0 -1
  125. data/src/core/tsi/alts/handshaker/transport_security_common.pb.h +1 -1
  126. data/src/core/tsi/alts/handshaker/transport_security_common_api.h +2 -2
  127. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.cc +47 -1
  128. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.h +3 -1
  129. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -11
  130. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.h +7 -2
  131. data/src/core/tsi/local_transport_security.cc +209 -0
  132. data/src/core/tsi/local_transport_security.h +51 -0
  133. data/src/core/tsi/ssl_transport_security.cc +2 -3
  134. data/src/{core/ext → cpp/ext/filters}/census/grpc_context.cc +0 -0
  135. data/src/ruby/ext/grpc/rb_channel_credentials.c +3 -3
  136. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +18 -18
  137. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +29 -29
  138. data/src/ruby/lib/grpc/generic/active_call.rb +19 -23
  139. data/src/ruby/lib/grpc/version.rb +1 -1
  140. data/src/ruby/spec/call_credentials_spec.rb +1 -1
  141. data/src/ruby/spec/call_spec.rb +1 -1
  142. data/src/ruby/spec/channel_credentials_spec.rb +1 -1
  143. data/src/ruby/spec/channel_spec.rb +1 -1
  144. data/src/ruby/spec/client_auth_spec.rb +1 -12
  145. data/src/ruby/spec/client_server_spec.rb +1 -1
  146. data/src/ruby/spec/compression_options_spec.rb +1 -1
  147. data/src/ruby/spec/error_sanity_spec.rb +1 -1
  148. data/src/ruby/spec/generic/client_stub_spec.rb +13 -1
  149. data/src/ruby/spec/generic/rpc_desc_spec.rb +1 -1
  150. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +1 -1
  151. data/src/ruby/spec/generic/service_spec.rb +1 -1
  152. data/src/ruby/spec/google_rpc_status_utils_spec.rb +1 -12
  153. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +1 -0
  154. data/src/ruby/spec/pb/health/checker_spec.rb +1 -1
  155. data/src/ruby/spec/server_credentials_spec.rb +1 -1
  156. data/src/ruby/spec/server_spec.rb +1 -1
  157. data/src/ruby/spec/spec_helper.rb +1 -0
  158. data/src/ruby/spec/support/services.rb +1 -1
  159. data/src/ruby/spec/time_consts_spec.rb +1 -1
  160. data/third_party/boringssl/crypto/asn1/tasn_dec.c +40 -19
  161. data/third_party/boringssl/crypto/bytestring/cbs.c +1 -0
  162. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +47 -15
  163. data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +9 -10
  164. data/third_party/boringssl/crypto/ecdh/ecdh.c +4 -3
  165. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +30 -54
  166. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +7 -1
  167. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +8 -8
  168. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +97 -11
  169. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +274 -218
  170. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +111 -34
  171. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +2 -2
  172. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +1 -1
  173. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +24 -6
  174. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +324 -63
  175. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +74 -21
  176. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +128 -86
  177. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +1 -1
  178. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +67 -112
  179. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +8 -1
  180. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +5 -5
  181. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +9 -17
  182. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +5378 -5418
  183. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +32 -32
  184. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +5 -11
  185. data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +16 -40
  186. data/third_party/boringssl/crypto/fipsmodule/rsa/internal.h +1 -6
  187. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +41 -29
  188. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +63 -49
  189. data/third_party/boringssl/crypto/x509/vpm_int.h +1 -0
  190. data/third_party/boringssl/crypto/x509/x509_vfy.c +4 -0
  191. data/third_party/boringssl/crypto/x509/x509_vpm.c +44 -22
  192. data/third_party/boringssl/include/openssl/aead.h +8 -2
  193. data/third_party/boringssl/include/openssl/asn1.h +1 -0
  194. data/third_party/boringssl/include/openssl/base.h +4 -0
  195. data/third_party/boringssl/include/openssl/bn.h +13 -3
  196. data/third_party/boringssl/include/openssl/bytestring.h +4 -4
  197. data/third_party/boringssl/include/openssl/ec.h +10 -4
  198. data/third_party/boringssl/include/openssl/ec_key.h +0 -3
  199. data/third_party/boringssl/include/openssl/rsa.h +1 -0
  200. data/third_party/boringssl/include/openssl/ssl.h +8 -3
  201. data/third_party/boringssl/include/openssl/ssl3.h +0 -1
  202. data/third_party/boringssl/include/openssl/x509.h +1 -0
  203. data/third_party/boringssl/include/openssl/x509v3.h +1 -0
  204. data/third_party/boringssl/ssl/handshake_client.cc +36 -64
  205. data/third_party/boringssl/ssl/ssl_cipher.cc +4 -0
  206. data/third_party/boringssl/ssl/ssl_lib.cc +1 -1
  207. metadata +45 -38
  208. data/src/core/ext/filters/load_reporting/server_load_reporting_filter.cc +0 -222
  209. data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.cc +0 -71
  210. data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.h +0 -61
  211. data/src/ruby/spec/pb/package_with_underscore/checker_spec.rb +0 -51
  212. data/src/ruby/spec/pb/package_with_underscore/data.proto +0 -23
  213. data/src/ruby/spec/pb/package_with_underscore/service.proto +0 -23
@@ -395,8 +395,8 @@ int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
395
395
  }
396
396
 
397
397
  // n5, n6
398
- if (!bn_mod_sub_quick_ctx(n5, n1, n3, p, ctx) ||
399
- !bn_mod_sub_quick_ctx(n6, n2, n4, p, ctx)) {
398
+ if (!bn_mod_sub_consttime(n5, n1, n3, p, ctx) ||
399
+ !bn_mod_sub_consttime(n6, n2, n4, p, ctx)) {
400
400
  goto end;
401
401
  }
402
402
  // n5 = n1 - n3
@@ -418,8 +418,8 @@ int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
418
418
  }
419
419
 
420
420
  // 'n7', 'n8'
421
- if (!bn_mod_add_quick_ctx(n1, n1, n3, p, ctx) ||
422
- !bn_mod_add_quick_ctx(n2, n2, n4, p, ctx)) {
421
+ if (!bn_mod_add_consttime(n1, n1, n3, p, ctx) ||
422
+ !bn_mod_add_consttime(n2, n2, n4, p, ctx)) {
423
423
  goto end;
424
424
  }
425
425
  // 'n7' = n1 + n3
@@ -453,14 +453,14 @@ int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
453
453
  if (!field_sqr(group, n0, n6, ctx) ||
454
454
  !field_sqr(group, n4, n5, ctx) ||
455
455
  !field_mul(group, n3, n1, n4, ctx) ||
456
- !bn_mod_sub_quick_ctx(&r->X, n0, n3, p, ctx)) {
456
+ !bn_mod_sub_consttime(&r->X, n0, n3, p, ctx)) {
457
457
  goto end;
458
458
  }
459
459
  // X_r = n6^2 - n5^2 * 'n7'
460
460
 
461
461
  // 'n9'
462
- if (!bn_mod_lshift1_quick_ctx(n0, &r->X, p, ctx) ||
463
- !bn_mod_sub_quick_ctx(n0, n3, n0, p, ctx)) {
462
+ if (!bn_mod_lshift1_consttime(n0, &r->X, p, ctx) ||
463
+ !bn_mod_sub_consttime(n0, n3, n0, p, ctx)) {
464
464
  goto end;
465
465
  }
466
466
  // n9 = n5^2 * 'n7' - 2 * X_r
@@ -471,7 +471,7 @@ int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
471
471
  goto end; // now n5 is n5^3
472
472
  }
473
473
  if (!field_mul(group, n1, n2, n5, ctx) ||
474
- !bn_mod_sub_quick_ctx(n0, n0, n1, p, ctx)) {
474
+ !bn_mod_sub_consttime(n0, n0, n1, p, ctx)) {
475
475
  goto end;
476
476
  }
477
477
  if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
@@ -536,31 +536,31 @@ int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
536
536
  // n1
537
537
  if (BN_cmp(&a->Z, &group->one) == 0) {
538
538
  if (!field_sqr(group, n0, &a->X, ctx) ||
539
- !bn_mod_lshift1_quick_ctx(n1, n0, p, ctx) ||
540
- !bn_mod_add_quick_ctx(n0, n0, n1, p, ctx) ||
541
- !bn_mod_add_quick_ctx(n1, n0, &group->a, p, ctx)) {
539
+ !bn_mod_lshift1_consttime(n1, n0, p, ctx) ||
540
+ !bn_mod_add_consttime(n0, n0, n1, p, ctx) ||
541
+ !bn_mod_add_consttime(n1, n0, &group->a, p, ctx)) {
542
542
  goto err;
543
543
  }
544
544
  // n1 = 3 * X_a^2 + a_curve
545
545
  } else if (group->a_is_minus3) {
546
546
  if (!field_sqr(group, n1, &a->Z, ctx) ||
547
- !bn_mod_add_quick_ctx(n0, &a->X, n1, p, ctx) ||
548
- !bn_mod_sub_quick_ctx(n2, &a->X, n1, p, ctx) ||
547
+ !bn_mod_add_consttime(n0, &a->X, n1, p, ctx) ||
548
+ !bn_mod_sub_consttime(n2, &a->X, n1, p, ctx) ||
549
549
  !field_mul(group, n1, n0, n2, ctx) ||
550
- !bn_mod_lshift1_quick_ctx(n0, n1, p, ctx) ||
551
- !bn_mod_add_quick_ctx(n1, n0, n1, p, ctx)) {
550
+ !bn_mod_lshift1_consttime(n0, n1, p, ctx) ||
551
+ !bn_mod_add_consttime(n1, n0, n1, p, ctx)) {
552
552
  goto err;
553
553
  }
554
554
  // n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
555
555
  // = 3 * X_a^2 - 3 * Z_a^4
556
556
  } else {
557
557
  if (!field_sqr(group, n0, &a->X, ctx) ||
558
- !bn_mod_lshift1_quick_ctx(n1, n0, p, ctx) ||
559
- !bn_mod_add_quick_ctx(n0, n0, n1, p, ctx) ||
558
+ !bn_mod_lshift1_consttime(n1, n0, p, ctx) ||
559
+ !bn_mod_add_consttime(n0, n0, n1, p, ctx) ||
560
560
  !field_sqr(group, n1, &a->Z, ctx) ||
561
561
  !field_sqr(group, n1, n1, ctx) ||
562
562
  !field_mul(group, n1, n1, &group->a, ctx) ||
563
- !bn_mod_add_quick_ctx(n1, n1, n0, p, ctx)) {
563
+ !bn_mod_add_consttime(n1, n1, n0, p, ctx)) {
564
564
  goto err;
565
565
  }
566
566
  // n1 = 3 * X_a^2 + a_curve * Z_a^4
@@ -574,7 +574,7 @@ int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
574
574
  } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
575
575
  goto err;
576
576
  }
577
- if (!bn_mod_lshift1_quick_ctx(&r->Z, n0, p, ctx)) {
577
+ if (!bn_mod_lshift1_consttime(&r->Z, n0, p, ctx)) {
578
578
  goto err;
579
579
  }
580
580
  // Z_r = 2 * Y_a * Z_a
@@ -582,30 +582,30 @@ int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
582
582
  // n2
583
583
  if (!field_sqr(group, n3, &a->Y, ctx) ||
584
584
  !field_mul(group, n2, &a->X, n3, ctx) ||
585
- !bn_mod_lshift_quick_ctx(n2, n2, 2, p, ctx)) {
585
+ !bn_mod_lshift_consttime(n2, n2, 2, p, ctx)) {
586
586
  goto err;
587
587
  }
588
588
  // n2 = 4 * X_a * Y_a^2
589
589
 
590
590
  // X_r
591
- if (!bn_mod_lshift1_quick_ctx(n0, n2, p, ctx) ||
591
+ if (!bn_mod_lshift1_consttime(n0, n2, p, ctx) ||
592
592
  !field_sqr(group, &r->X, n1, ctx) ||
593
- !bn_mod_sub_quick_ctx(&r->X, &r->X, n0, p, ctx)) {
593
+ !bn_mod_sub_consttime(&r->X, &r->X, n0, p, ctx)) {
594
594
  goto err;
595
595
  }
596
596
  // X_r = n1^2 - 2 * n2
597
597
 
598
598
  // n3
599
599
  if (!field_sqr(group, n0, n3, ctx) ||
600
- !bn_mod_lshift_quick_ctx(n3, n0, 3, p, ctx)) {
600
+ !bn_mod_lshift_consttime(n3, n0, 3, p, ctx)) {
601
601
  goto err;
602
602
  }
603
603
  // n3 = 8 * Y_a^4
604
604
 
605
605
  // Y_r
606
- if (!bn_mod_sub_quick_ctx(n0, n2, &r->X, p, ctx) ||
606
+ if (!bn_mod_sub_consttime(n0, n2, &r->X, p, ctx) ||
607
607
  !field_mul(group, n0, n1, n0, ctx) ||
608
- !bn_mod_sub_quick_ctx(&r->Y, n0, n3, p, ctx)) {
608
+ !bn_mod_sub_consttime(&r->Y, n0, n3, p, ctx)) {
609
609
  goto err;
610
610
  }
611
611
  // Y_r = n1 * (n2 - X_r) - n3
@@ -688,15 +688,15 @@ int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
688
688
 
689
689
  // rh := (rh + a*Z^4)*X
690
690
  if (group->a_is_minus3) {
691
- if (!bn_mod_lshift1_quick_ctx(tmp, Z4, p, ctx) ||
692
- !bn_mod_add_quick_ctx(tmp, tmp, Z4, p, ctx) ||
693
- !bn_mod_sub_quick_ctx(rh, rh, tmp, p, ctx) ||
691
+ if (!bn_mod_lshift1_consttime(tmp, Z4, p, ctx) ||
692
+ !bn_mod_add_consttime(tmp, tmp, Z4, p, ctx) ||
693
+ !bn_mod_sub_consttime(rh, rh, tmp, p, ctx) ||
694
694
  !field_mul(group, rh, rh, &point->X, ctx)) {
695
695
  goto err;
696
696
  }
697
697
  } else {
698
698
  if (!field_mul(group, tmp, Z4, &group->a, ctx) ||
699
- !bn_mod_add_quick_ctx(rh, rh, tmp, p, ctx) ||
699
+ !bn_mod_add_consttime(rh, rh, tmp, p, ctx) ||
700
700
  !field_mul(group, rh, rh, &point->X, ctx)) {
701
701
  goto err;
702
702
  }
@@ -704,17 +704,17 @@ int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
704
704
 
705
705
  // rh := rh + b*Z^6
706
706
  if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
707
- !bn_mod_add_quick_ctx(rh, rh, tmp, p, ctx)) {
707
+ !bn_mod_add_consttime(rh, rh, tmp, p, ctx)) {
708
708
  goto err;
709
709
  }
710
710
  } else {
711
711
  // rh := (rh + a)*X
712
- if (!bn_mod_add_quick_ctx(rh, rh, &group->a, p, ctx) ||
712
+ if (!bn_mod_add_consttime(rh, rh, &group->a, p, ctx) ||
713
713
  !field_mul(group, rh, rh, &point->X, ctx)) {
714
714
  goto err;
715
715
  }
716
716
  // rh := rh + b
717
- if (!bn_mod_add_quick_ctx(rh, rh, &group->b, p, ctx)) {
717
+ if (!bn_mod_add_consttime(rh, rh, &group->b, p, ctx)) {
718
718
  goto err;
719
719
  }
720
720
  }
@@ -392,17 +392,17 @@ ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
392
392
  }
393
393
 
394
394
  const EC_GROUP *group = EC_KEY_get0_group(eckey);
395
- const BIGNUM *priv_key_bn = EC_KEY_get0_private_key(eckey);
396
- if (group == NULL || priv_key_bn == NULL) {
395
+ if (group == NULL || eckey->priv_key == NULL) {
397
396
  OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
398
397
  return NULL;
399
398
  }
400
399
  const BIGNUM *order = EC_GROUP_get0_order(group);
400
+ const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
401
401
 
402
402
  int ok = 0;
403
403
  ECDSA_SIG *ret = ECDSA_SIG_new();
404
404
  BN_CTX *ctx = BN_CTX_new();
405
- EC_SCALAR kinv_mont, priv_key, r_mont, s;
405
+ EC_SCALAR kinv_mont, r_mont, s;
406
406
  EC_LOOSE_SCALAR m, tmp;
407
407
  if (ret == NULL || ctx == NULL) {
408
408
  OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
@@ -410,14 +410,9 @@ ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
410
410
  }
411
411
 
412
412
  digest_to_scalar(group, &m, digest, digest_len);
413
- // TODO(davidben): Store the private key as an |EC_SCALAR| so this is obvious
414
- // via the type system.
415
- if (!ec_bignum_to_scalar_unchecked(group, &priv_key, priv_key_bn)) {
416
- goto err;
417
- }
418
413
  for (;;) {
419
414
  if (!ecdsa_sign_setup(eckey, ctx, &kinv_mont, &ret->r, digest, digest_len,
420
- &priv_key)) {
415
+ priv_key)) {
421
416
  goto err;
422
417
  }
423
418
 
@@ -427,7 +422,7 @@ ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
427
422
  if (!ec_bignum_to_scalar(group, &r_mont, ret->r) ||
428
423
  !bn_to_montgomery_small(r_mont.words, order->width, r_mont.words,
429
424
  order->width, group->order_mont) ||
430
- !scalar_mod_mul_montgomery(group, &s, &priv_key, &r_mont)) {
425
+ !scalar_mod_mul_montgomery(group, &s, priv_key, &r_mont)) {
431
426
  goto err;
432
427
  }
433
428
 
@@ -455,7 +450,6 @@ err:
455
450
  }
456
451
  BN_CTX_free(ctx);
457
452
  OPENSSL_cleanse(&kinv_mont, sizeof(kinv_mont));
458
- OPENSSL_cleanse(&priv_key, sizeof(priv_key));
459
453
  OPENSSL_cleanse(&r_mont, sizeof(r_mont));
460
454
  OPENSSL_cleanse(&s, sizeof(s));
461
455
  OPENSSL_cleanse(&tmp, sizeof(tmp));
@@ -215,46 +215,22 @@ int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_MONT_CTX *mont,
215
215
 
216
216
  static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e,
217
217
  const BN_MONT_CTX *mont, BN_CTX *ctx) {
218
- int retry_counter = 32;
219
-
220
- do {
221
- if (!BN_rand_range_ex(b->A, 1, &mont->N)) {
222
- OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
223
- return 0;
224
- }
225
-
226
- // |BN_from_montgomery| + |BN_mod_inverse_blinded| is equivalent to, but
227
- // more efficient than, |BN_mod_inverse_blinded| + |BN_to_montgomery|.
228
- if (!BN_from_montgomery(b->Ai, b->A, mont, ctx)) {
229
- OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
230
- return 0;
231
- }
232
-
233
- int no_inverse;
234
- if (BN_mod_inverse_blinded(b->Ai, &no_inverse, b->Ai, mont, ctx)) {
235
- break;
236
- }
237
-
238
- if (!no_inverse) {
239
- OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
240
- return 0;
241
- }
242
-
243
- // For reasonably-sized RSA keys, it should almost never be the case that a
244
- // random value doesn't have an inverse.
245
- if (retry_counter-- == 0) {
246
- OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
247
- return 0;
248
- }
249
- ERR_clear_error();
250
- } while (1);
251
-
252
- if (!BN_mod_exp_mont(b->A, b->A, e, &mont->N, ctx, mont)) {
253
- OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
254
- return 0;
255
- }
256
-
257
- if (!BN_to_montgomery(b->A, b->A, mont, ctx)) {
218
+ int no_inverse;
219
+ if (!BN_rand_range_ex(b->A, 1, &mont->N) ||
220
+ // Compute |b->A|^-1 in Montgomery form. Note |BN_from_montgomery| +
221
+ // |BN_mod_inverse_blinded| is equivalent to, but more efficient than,
222
+ // |BN_mod_inverse_blinded| + |BN_to_montgomery|.
223
+ //
224
+ // We do not retry if |b->A| has no inverse. Finding a non-invertible
225
+ // value of |b->A| is equivalent to factoring |mont->N|. There is
226
+ // negligible probability of stumbling on one at random.
227
+ !BN_from_montgomery(b->Ai, b->A, mont, ctx) ||
228
+ !BN_mod_inverse_blinded(b->Ai, &no_inverse, b->Ai, mont, ctx) ||
229
+ // TODO(davidben): |BN_mod_exp_mont| internally computes the result in
230
+ // Montgomery form. Save a pair of Montgomery reductions and a
231
+ // multiplication by returning that value directly.
232
+ !BN_mod_exp_mont(b->A, b->A, e, &mont->N, ctx, mont) ||
233
+ !BN_to_montgomery(b->A, b->A, mont, ctx)) {
258
234
  OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
259
235
  return 0;
260
236
  }
@@ -114,15 +114,10 @@ int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
114
114
  size_t len);
115
115
 
116
116
 
117
- // The following utility functions are exported for test purposes.
118
-
117
+ // This constant is exported for test purposes.
119
118
  extern const BN_ULONG kBoringSSLRSASqrtTwo[];
120
119
  extern const size_t kBoringSSLRSASqrtTwoLen;
121
120
 
122
- // rsa_greater_than_pow2 returns one if |b| is greater than 2^|n| and zero
123
- // otherwise.
124
- int rsa_greater_than_pow2(const BIGNUM *b, int n);
125
-
126
121
 
127
122
  #if defined(__cplusplus)
128
123
  } // extern C
@@ -634,8 +634,25 @@ err:
634
634
  return ret;
635
635
  }
636
636
 
637
+ static int check_mod_inverse(int *out_ok, const BIGNUM *a, const BIGNUM *ainv,
638
+ const BIGNUM *m, int check_reduced, BN_CTX *ctx) {
639
+ BN_CTX_start(ctx);
640
+ BIGNUM *tmp = BN_CTX_get(ctx);
641
+ int ret = tmp != NULL &&
642
+ bn_mul_consttime(tmp, a, ainv, ctx) &&
643
+ bn_div_consttime(NULL, tmp, tmp, m, ctx);
644
+ if (ret) {
645
+ *out_ok = BN_is_one(tmp);
646
+ if (check_reduced && (BN_is_negative(ainv) || BN_cmp(ainv, m) >= 0)) {
647
+ *out_ok = 0;
648
+ }
649
+ }
650
+ BN_CTX_end(ctx);
651
+ return ret;
652
+ }
653
+
637
654
  int RSA_check_key(const RSA *key) {
638
- BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
655
+ BIGNUM n, pm1, qm1, lcm, dmp1, dmq1, iqmp_times_q;
639
656
  BN_CTX *ctx;
640
657
  int ok = 0, has_crt_values;
641
658
 
@@ -670,26 +687,20 @@ int RSA_check_key(const RSA *key) {
670
687
  BN_init(&pm1);
671
688
  BN_init(&qm1);
672
689
  BN_init(&lcm);
673
- BN_init(&gcd);
674
- BN_init(&de);
675
690
  BN_init(&dmp1);
676
691
  BN_init(&dmq1);
677
692
  BN_init(&iqmp_times_q);
678
693
 
679
- if (!BN_mul(&n, key->p, key->q, ctx) ||
694
+ int d_ok;
695
+ if (!bn_mul_consttime(&n, key->p, key->q, ctx) ||
680
696
  // lcm = lcm(p, q)
681
- !BN_sub(&pm1, key->p, BN_value_one()) ||
682
- !BN_sub(&qm1, key->q, BN_value_one()) ||
683
- !BN_mul(&lcm, &pm1, &qm1, ctx) ||
684
- !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
685
- OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
686
- goto out;
687
- }
688
-
689
- if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
690
- !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
691
- // de = d*e mod lcm(p, q).
692
- !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
697
+ !bn_usub_consttime(&pm1, key->p, BN_value_one()) ||
698
+ !bn_usub_consttime(&qm1, key->q, BN_value_one()) ||
699
+ !bn_lcm_consttime(&lcm, &pm1, &qm1, ctx) ||
700
+ // Other implementations use the Euler totient rather than the Carmichael
701
+ // totient, so allow unreduced |key->d|.
702
+ !check_mod_inverse(&d_ok, key->e, key->d, &lcm,
703
+ 0 /* don't require reduced */, ctx)) {
693
704
  OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
694
705
  goto out;
695
706
  }
@@ -699,11 +710,16 @@ int RSA_check_key(const RSA *key) {
699
710
  goto out;
700
711
  }
701
712
 
702
- if (!BN_is_one(&de)) {
713
+ if (!d_ok) {
703
714
  OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
704
715
  goto out;
705
716
  }
706
717
 
718
+ if (BN_is_negative(key->d) || BN_cmp(key->d, key->n) >= 0) {
719
+ OPENSSL_PUT_ERROR(RSA, RSA_R_D_OUT_OF_RANGE);
720
+ goto out;
721
+ }
722
+
707
723
  has_crt_values = key->dmp1 != NULL;
708
724
  if (has_crt_values != (key->dmq1 != NULL) ||
709
725
  has_crt_values != (key->iqmp != NULL)) {
@@ -712,20 +728,18 @@ int RSA_check_key(const RSA *key) {
712
728
  }
713
729
 
714
730
  if (has_crt_values) {
715
- if (// dmp1 = d mod (p-1)
716
- !BN_mod(&dmp1, key->d, &pm1, ctx) ||
717
- // dmq1 = d mod (q-1)
718
- !BN_mod(&dmq1, key->d, &qm1, ctx) ||
719
- // iqmp = q^-1 mod p
720
- !BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
731
+ int dmp1_ok, dmq1_ok, iqmp_ok;
732
+ if (!check_mod_inverse(&dmp1_ok, key->e, key->dmp1, &pm1,
733
+ 1 /* check reduced */, ctx) ||
734
+ !check_mod_inverse(&dmq1_ok, key->e, key->dmq1, &qm1,
735
+ 1 /* check reduced */, ctx) ||
736
+ !check_mod_inverse(&iqmp_ok, key->q, key->iqmp, key->p,
737
+ 1 /* check reduced */, ctx)) {
721
738
  OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
722
739
  goto out;
723
740
  }
724
741
 
725
- if (BN_cmp(&dmp1, key->dmp1) != 0 ||
726
- BN_cmp(&dmq1, key->dmq1) != 0 ||
727
- BN_cmp(key->iqmp, key->p) >= 0 ||
728
- !BN_is_one(&iqmp_times_q)) {
742
+ if (!dmp1_ok || !dmq1_ok || !iqmp_ok) {
729
743
  OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
730
744
  goto out;
731
745
  }
@@ -738,8 +752,6 @@ out:
738
752
  BN_free(&pm1);
739
753
  BN_free(&qm1);
740
754
  BN_free(&lcm);
741
- BN_free(&gcd);
742
- BN_free(&de);
743
755
  BN_free(&dmp1);
744
756
  BN_free(&dmq1);
745
757
  BN_free(&iqmp_times_q);
@@ -862,7 +862,7 @@ static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
862
862
  !BN_mod_exp_mont_consttime(r0, r1, dmp1, p, ctx, mont_p) ||
863
863
  // Compute r0 = r0 - m1 mod p. |p| is the larger prime, so |m1| is already
864
864
  // fully reduced mod |p|.
865
- !bn_mod_sub_quick_ctx(r0, r0, m1, p, ctx) ||
865
+ !bn_mod_sub_consttime(r0, r0, m1, p, ctx) ||
866
866
  // r0 = r0 * iqmp mod p. We use Montgomery multiplication to compute this
867
867
  // in constant time. |inv_small_mod_large_mont| is in Montgomery form and
868
868
  // r0 is not, so the result is taken out of Montgomery form.
@@ -873,8 +873,8 @@ static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
873
873
  // so it is correct mod q. Finally, the result is bounded by [m1, n + m1),
874
874
  // and the result is at least |m1|, so this must be the unique answer in
875
875
  // [0, n).
876
- !bn_mul_fixed(r0, r0, q, ctx) ||
877
- !bn_uadd_fixed(r0, r0, m1) ||
876
+ !bn_mul_consttime(r0, r0, q, ctx) ||
877
+ !bn_uadd_consttime(r0, r0, m1) ||
878
878
  // The result should be bounded by |n|, but fixed-width operations may
879
879
  // bound the width slightly higher, so fix it.
880
880
  !bn_resize_words(r0, n->width)) {
@@ -924,25 +924,20 @@ const BN_ULONG kBoringSSLRSASqrtTwo[] = {
924
924
  };
925
925
  const size_t kBoringSSLRSASqrtTwoLen = OPENSSL_ARRAY_SIZE(kBoringSSLRSASqrtTwo);
926
926
 
927
- int rsa_greater_than_pow2(const BIGNUM *b, int n) {
928
- if (BN_is_negative(b) || n == INT_MAX) {
929
- return 0;
930
- }
931
-
932
- int b_bits = BN_num_bits(b);
933
- return b_bits > n + 1 || (b_bits == n + 1 && !BN_is_pow2(b));
934
- }
935
-
936
927
  // generate_prime sets |out| to a prime with length |bits| such that |out|-1 is
937
928
  // relatively prime to |e|. If |p| is non-NULL, |out| will also not be close to
938
- // |p|.
929
+ // |p|. |sqrt2| must be ⌊2^(bits-1)×√2⌋ (or a slightly overestimate for large
930
+ // sizes), and |pow2_bits_100| must be 2^(bits-100).
939
931
  static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
940
- const BIGNUM *p, const BIGNUM *sqrt2, BN_CTX *ctx,
932
+ const BIGNUM *p, const BIGNUM *sqrt2,
933
+ const BIGNUM *pow2_bits_100, BN_CTX *ctx,
941
934
  BN_GENCB *cb) {
942
935
  if (bits < 128 || (bits % BN_BITS2) != 0) {
943
936
  OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
944
937
  return 0;
945
938
  }
939
+ assert(BN_is_pow2(pow2_bits_100));
940
+ assert(BN_is_bit_set(pow2_bits_100, bits - 100));
946
941
 
947
942
  // See FIPS 186-4 appendix B.3.3, steps 4 and 5. Note |bits| here is nlen/2.
948
943
 
@@ -973,11 +968,10 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
973
968
 
974
969
  if (p != NULL) {
975
970
  // If |p| and |out| are too close, try again (step 5.4).
976
- if (!BN_sub(tmp, out, p)) {
971
+ if (!bn_abs_sub_consttime(tmp, out, p, ctx)) {
977
972
  goto err;
978
973
  }
979
- BN_set_negative(tmp, 0);
980
- if (!rsa_greater_than_pow2(tmp, bits - 100)) {
974
+ if (BN_cmp(tmp, pow2_bits_100) <= 0) {
981
975
  continue;
982
976
  }
983
977
  }
@@ -993,21 +987,26 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
993
987
  continue;
994
988
  }
995
989
 
996
- // Check gcd(out-1, e) is one (steps 4.5 and 5.6).
997
- if (!BN_sub(tmp, out, BN_value_one()) ||
998
- !BN_gcd(tmp, tmp, e, ctx)) {
999
- goto err;
1000
- }
1001
- if (BN_is_one(tmp)) {
1002
- // Test |out| for primality (steps 4.5.1 and 5.6.1).
1003
- int is_probable_prime;
1004
- if (!BN_primality_test(&is_probable_prime, out, BN_prime_checks, ctx, 1,
1005
- cb)) {
990
+ // RSA key generation's bottleneck is discarding composites. If it fails
991
+ // trial division, do not bother computing a GCD or performing Rabin-Miller.
992
+ if (!bn_odd_number_is_obviously_composite(out)) {
993
+ // Check gcd(out-1, e) is one (steps 4.5 and 5.6).
994
+ int relatively_prime;
995
+ if (!BN_sub(tmp, out, BN_value_one()) ||
996
+ !bn_is_relatively_prime(&relatively_prime, tmp, e, ctx)) {
1006
997
  goto err;
1007
998
  }
1008
- if (is_probable_prime) {
1009
- ret = 1;
1010
- goto err;
999
+ if (relatively_prime) {
1000
+ // Test |out| for primality (steps 4.5.1 and 5.6.1).
1001
+ int is_probable_prime;
1002
+ if (!BN_primality_test(&is_probable_prime, out, BN_prime_checks, ctx, 0,
1003
+ cb)) {
1004
+ goto err;
1005
+ }
1006
+ if (is_probable_prime) {
1007
+ ret = 1;
1008
+ goto err;
1009
+ }
1011
1010
  }
1012
1011
  }
1013
1012
 
@@ -1043,7 +1042,19 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1043
1042
  return 0;
1044
1043
  }
1045
1044
 
1045
+ // Reject excessively large public exponents. Windows CryptoAPI and Go don't
1046
+ // support values larger than 32 bits, so match their limits for generating
1047
+ // keys. (|check_modulus_and_exponent_sizes| uses a slightly more conservative
1048
+ // value, but we don't need to support generating such keys.)
1049
+ // https://github.com/golang/go/issues/3161
1050
+ // https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx
1051
+ if (BN_num_bits(e_value) > 32) {
1052
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
1053
+ return 0;
1054
+ }
1055
+
1046
1056
  int ret = 0;
1057
+ int prime_bits = bits / 2;
1047
1058
  BN_CTX *ctx = BN_CTX_new();
1048
1059
  if (ctx == NULL) {
1049
1060
  goto bn_err;
@@ -1052,10 +1063,13 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1052
1063
  BIGNUM *totient = BN_CTX_get(ctx);
1053
1064
  BIGNUM *pm1 = BN_CTX_get(ctx);
1054
1065
  BIGNUM *qm1 = BN_CTX_get(ctx);
1055
- BIGNUM *gcd = BN_CTX_get(ctx);
1056
1066
  BIGNUM *sqrt2 = BN_CTX_get(ctx);
1057
- if (totient == NULL || pm1 == NULL || qm1 == NULL || gcd == NULL ||
1058
- sqrt2 == NULL) {
1067
+ BIGNUM *pow2_prime_bits_100 = BN_CTX_get(ctx);
1068
+ BIGNUM *pow2_prime_bits = BN_CTX_get(ctx);
1069
+ if (totient == NULL || pm1 == NULL || qm1 == NULL || sqrt2 == NULL ||
1070
+ pow2_prime_bits_100 == NULL || pow2_prime_bits == NULL ||
1071
+ !BN_set_bit(pow2_prime_bits_100, prime_bits - 100) ||
1072
+ !BN_set_bit(pow2_prime_bits, prime_bits)) {
1059
1073
  goto bn_err;
1060
1074
  }
1061
1075
 
@@ -1074,8 +1088,6 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1074
1088
  goto bn_err;
1075
1089
  }
1076
1090
 
1077
- int prime_bits = bits / 2;
1078
-
1079
1091
  // Compute sqrt2 >= ⌊2^(prime_bits-1)×√2⌋.
1080
1092
  if (!bn_set_words(sqrt2, kBoringSSLRSASqrtTwo, kBoringSSLRSASqrtTwoLen)) {
1081
1093
  goto bn_err;
@@ -1101,9 +1113,11 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1101
1113
  do {
1102
1114
  // Generate p and q, each of size |prime_bits|, using the steps outlined in
1103
1115
  // appendix FIPS 186-4 appendix B.3.3.
1104
- if (!generate_prime(rsa->p, prime_bits, rsa->e, NULL, sqrt2, ctx, cb) ||
1116
+ if (!generate_prime(rsa->p, prime_bits, rsa->e, NULL, sqrt2,
1117
+ pow2_prime_bits_100, ctx, cb) ||
1105
1118
  !BN_GENCB_call(cb, 3, 0) ||
1106
- !generate_prime(rsa->q, prime_bits, rsa->e, rsa->p, sqrt2, ctx, cb) ||
1119
+ !generate_prime(rsa->q, prime_bits, rsa->e, rsa->p, sqrt2,
1120
+ pow2_prime_bits_100, ctx, cb) ||
1107
1121
  !BN_GENCB_call(cb, 3, 1)) {
1108
1122
  goto bn_err;
1109
1123
  }
@@ -1121,27 +1135,27 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1121
1135
  // q-1. However, we do operations with Chinese Remainder Theorem, so we only
1122
1136
  // use d (mod p-1) and d (mod q-1) as exponents. Using a minimal totient
1123
1137
  // does not affect those two values.
1124
- if (!BN_sub(pm1, rsa->p, BN_value_one()) ||
1125
- !BN_sub(qm1, rsa->q, BN_value_one()) ||
1126
- !BN_mul(totient, pm1, qm1, ctx) ||
1127
- !BN_gcd(gcd, pm1, qm1, ctx) ||
1128
- !BN_div(totient, NULL, totient, gcd, ctx) ||
1129
- !BN_mod_inverse(rsa->d, rsa->e, totient, ctx)) {
1138
+ int no_inverse;
1139
+ if (!bn_usub_consttime(pm1, rsa->p, BN_value_one()) ||
1140
+ !bn_usub_consttime(qm1, rsa->q, BN_value_one()) ||
1141
+ !bn_lcm_consttime(totient, pm1, qm1, ctx) ||
1142
+ !bn_mod_inverse_consttime(rsa->d, &no_inverse, rsa->e, totient, ctx)) {
1130
1143
  goto bn_err;
1131
1144
  }
1132
1145
 
1133
- // Check that |rsa->d| > 2^|prime_bits| and try again if it fails. See
1134
- // appendix B.3.1's guidance on values for d.
1135
- } while (!rsa_greater_than_pow2(rsa->d, prime_bits));
1146
+ // Retry if |rsa->d| <= 2^|prime_bits|. See appendix B.3.1's guidance on
1147
+ // values for d.
1148
+ } while (BN_cmp(rsa->d, pow2_prime_bits) <= 0);
1136
1149
 
1137
1150
  if (// Calculate n.
1138
- !BN_mul(rsa->n, rsa->p, rsa->q, ctx) ||
1151
+ !bn_mul_consttime(rsa->n, rsa->p, rsa->q, ctx) ||
1139
1152
  // Calculate d mod (p-1).
1140
- !BN_mod(rsa->dmp1, rsa->d, pm1, ctx) ||
1153
+ !bn_div_consttime(NULL, rsa->dmp1, rsa->d, pm1, ctx) ||
1141
1154
  // Calculate d mod (q-1)
1142
- !BN_mod(rsa->dmq1, rsa->d, qm1, ctx)) {
1155
+ !bn_div_consttime(NULL, rsa->dmq1, rsa->d, qm1, ctx)) {
1143
1156
  goto bn_err;
1144
1157
  }
1158
+ bn_set_minimal_width(rsa->n);
1145
1159
 
1146
1160
  // Sanity-check that |rsa->n| has the specified size. This is implied by
1147
1161
  // |generate_prime|'s bounds.