grpc-flamingo 1.11.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (452) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1150 -176
  3. data/etc/roots.pem +40 -196
  4. data/include/grpc/grpc.h +49 -8
  5. data/include/grpc/grpc_security.h +123 -2
  6. data/include/grpc/grpc_security_constants.h +6 -0
  7. data/include/grpc/impl/codegen/fork.h +4 -4
  8. data/include/grpc/impl/codegen/grpc_types.h +26 -5
  9. data/include/grpc/impl/codegen/log.h +112 -0
  10. data/include/grpc/impl/codegen/port_platform.h +55 -4
  11. data/include/grpc/module.modulemap +2 -0
  12. data/include/grpc/support/log.h +2 -80
  13. data/include/grpc/support/string_util.h +2 -0
  14. data/include/grpc/support/sync.h +0 -16
  15. data/src/boringssl/err_data.c +602 -588
  16. data/src/core/ext/{census → filters/census}/grpc_context.cc +0 -0
  17. data/src/core/ext/filters/client_channel/channel_connectivity.cc +1 -1
  18. data/src/core/ext/filters/client_channel/client_channel.cc +1234 -1070
  19. data/src/core/ext/filters/client_channel/client_channel.h +5 -0
  20. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +113 -0
  21. data/src/core/ext/filters/client_channel/client_channel_channelz.h +71 -0
  22. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +9 -0
  23. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +1 -1
  24. data/src/core/ext/filters/client_channel/http_proxy.cc +22 -5
  25. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -2
  26. data/src/core/ext/filters/client_channel/lb_policy.h +30 -10
  27. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +11 -9
  28. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +120 -127
  29. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h +36 -0
  30. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.cc +36 -102
  31. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +37 -32
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +25 -22
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +4 -2
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.c +19 -0
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h +54 -0
  36. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.c +19 -0
  37. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h +54 -0
  38. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +4 -17
  39. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +37 -63
  40. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +306 -239
  41. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +444 -392
  42. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +538 -98
  43. data/src/core/ext/filters/client_channel/lb_policy_factory.cc +8 -0
  44. data/src/core/ext/filters/client_channel/lb_policy_factory.h +4 -0
  45. data/src/core/ext/filters/client_channel/method_params.h +4 -0
  46. data/src/core/ext/filters/client_channel/resolver.h +10 -0
  47. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +36 -19
  48. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +320 -0
  49. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +62 -9
  50. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +49 -294
  51. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +537 -0
  52. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +112 -87
  53. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +17 -2
  54. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +6 -5
  55. data/src/core/ext/filters/{load_reporting/server_load_reporting_filter.h → client_channel/resolver/dns/c_ares/grpc_ares_wrapper_posix.cc} +7 -8
  56. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +29 -0
  57. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +32 -15
  58. data/src/core/ext/filters/client_channel/retry_throttle.h +4 -0
  59. data/src/core/ext/filters/client_channel/subchannel.cc +58 -15
  60. data/src/core/ext/filters/client_channel/subchannel.h +11 -0
  61. data/src/core/ext/filters/deadline/deadline_filter.cc +18 -15
  62. data/src/core/ext/filters/deadline/deadline_filter.h +5 -5
  63. data/src/core/ext/filters/http/client/http_client_filter.cc +10 -9
  64. data/src/core/ext/filters/http/client_authority_filter.cc +6 -5
  65. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +6 -6
  66. data/src/core/ext/filters/http/server/http_server_filter.cc +123 -131
  67. data/src/core/ext/filters/http/server/http_server_filter.h +1 -1
  68. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +1 -1
  69. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  70. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +3 -2
  71. data/src/core/ext/transport/chttp2/transport/bin_decoder.cc +9 -8
  72. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +97 -48
  73. data/src/core/ext/transport/chttp2/transport/flow_control.cc +10 -7
  74. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +3 -3
  75. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +12 -8
  76. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +4 -3
  77. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +2 -2
  78. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +2 -2
  79. data/src/core/ext/transport/chttp2/transport/parsing.cc +14 -12
  80. data/src/core/ext/transport/chttp2/transport/stream_lists.cc +3 -3
  81. data/src/core/ext/transport/chttp2/transport/writing.cc +32 -27
  82. data/src/core/ext/transport/inproc/inproc_transport.cc +87 -49
  83. data/src/core/lib/channel/channel_args.cc +28 -0
  84. data/src/core/lib/channel/channel_args.h +4 -0
  85. data/src/core/lib/channel/channel_stack.cc +22 -29
  86. data/src/core/lib/channel/channel_stack.h +2 -2
  87. data/src/core/lib/channel/channel_stack_builder.cc +0 -3
  88. data/src/core/lib/channel/channel_stack_builder.h +0 -2
  89. data/src/core/lib/channel/channel_trace.cc +28 -63
  90. data/src/core/lib/channel/channel_trace.h +13 -17
  91. data/src/core/lib/channel/channelz.cc +153 -0
  92. data/src/core/lib/channel/channelz.h +133 -0
  93. data/src/core/lib/channel/channelz_registry.cc +145 -0
  94. data/src/core/lib/channel/channelz_registry.h +120 -0
  95. data/src/core/lib/channel/connected_channel.cc +8 -1
  96. data/src/core/lib/channel/handshaker.cc +71 -0
  97. data/src/core/lib/channel/handshaker.h +4 -0
  98. data/src/core/lib/debug/stats.h +7 -0
  99. data/src/core/lib/debug/stats_data.cc +5 -0
  100. data/src/core/lib/debug/stats_data.h +120 -0
  101. data/src/core/lib/debug/trace.cc +2 -1
  102. data/src/core/lib/debug/trace.h +12 -1
  103. data/src/core/lib/gpr/alloc.h +28 -0
  104. data/src/core/lib/gpr/arena.cc +38 -45
  105. data/src/core/lib/gpr/log.cc +8 -2
  106. data/src/core/lib/gpr/log_android.cc +4 -0
  107. data/src/core/lib/gpr/log_linux.cc +4 -0
  108. data/src/core/lib/gpr/log_posix.cc +4 -0
  109. data/src/core/lib/gpr/log_windows.cc +5 -0
  110. data/src/core/lib/gpr/string.cc +28 -0
  111. data/src/core/lib/gpr/string.h +10 -0
  112. data/src/core/lib/gprpp/abstract.h +5 -2
  113. data/src/core/lib/gprpp/fork.cc +268 -0
  114. data/src/core/lib/gprpp/fork.h +88 -0
  115. data/src/core/lib/gprpp/inlined_vector.h +87 -37
  116. data/src/core/lib/gprpp/memory.h +12 -0
  117. data/src/core/lib/gprpp/mutex_lock.h +42 -0
  118. data/src/core/lib/gprpp/orphanable.h +10 -12
  119. data/src/core/lib/gprpp/ref_counted.h +10 -12
  120. data/src/core/lib/gprpp/ref_counted_ptr.h +65 -8
  121. data/src/core/lib/gprpp/thd.h +0 -3
  122. data/src/core/lib/gprpp/thd_posix.cc +5 -54
  123. data/src/core/lib/gprpp/thd_windows.cc +0 -7
  124. data/src/core/lib/http/httpcli_security_connector.cc +1 -3
  125. data/src/core/lib/iomgr/call_combiner.cc +13 -13
  126. data/src/core/lib/iomgr/call_combiner.h +84 -1
  127. data/src/core/lib/iomgr/closure.h +6 -5
  128. data/src/core/lib/iomgr/combiner.cc +30 -13
  129. data/src/core/lib/iomgr/combiner.h +1 -1
  130. data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
  131. data/src/core/lib/iomgr/error.cc +12 -0
  132. data/src/core/lib/iomgr/error.h +5 -0
  133. data/src/core/lib/iomgr/ev_epoll1_linux.cc +138 -51
  134. data/src/core/lib/iomgr/ev_epollex_linux.cc +276 -93
  135. data/src/core/lib/iomgr/ev_epollsig_linux.cc +58 -50
  136. data/src/core/lib/iomgr/ev_poll_posix.cc +163 -42
  137. data/src/core/lib/iomgr/ev_posix.cc +88 -24
  138. data/src/core/lib/iomgr/ev_posix.h +48 -12
  139. data/src/core/lib/iomgr/exec_ctx.cc +15 -9
  140. data/src/core/lib/iomgr/exec_ctx.h +48 -20
  141. data/src/core/lib/iomgr/executor.cc +274 -142
  142. data/src/core/lib/iomgr/executor.h +82 -16
  143. data/src/core/lib/iomgr/fork_posix.cc +42 -19
  144. data/src/core/lib/iomgr/iocp_windows.cc +9 -4
  145. data/src/core/lib/iomgr/iomgr.cc +2 -0
  146. data/src/core/lib/iomgr/iomgr.h +5 -0
  147. data/src/core/lib/iomgr/iomgr_posix.cc +2 -2
  148. data/src/core/lib/iomgr/is_epollexclusive_available.cc +1 -0
  149. data/src/core/lib/iomgr/lockfree_event.cc +5 -1
  150. data/src/core/lib/iomgr/polling_entity.cc +11 -2
  151. data/src/core/lib/iomgr/pollset_custom.cc +2 -2
  152. data/src/core/lib/iomgr/port.h +51 -1
  153. data/src/core/lib/iomgr/resolve_address.h +1 -1
  154. data/src/core/lib/iomgr/resolve_address_posix.cc +4 -3
  155. data/src/core/lib/iomgr/resolve_address_windows.cc +3 -2
  156. data/src/core/lib/iomgr/resource_quota.cc +89 -12
  157. data/src/core/lib/iomgr/resource_quota.h +16 -0
  158. data/src/core/lib/iomgr/sockaddr_posix.h +1 -1
  159. data/src/core/lib/iomgr/socket_factory_posix.cc +1 -1
  160. data/src/core/lib/iomgr/socket_mutator.cc +1 -1
  161. data/src/core/lib/iomgr/socket_mutator.h +1 -1
  162. data/src/core/lib/iomgr/socket_utils.h +9 -0
  163. data/src/core/lib/iomgr/socket_utils_common_posix.cc +29 -1
  164. data/src/core/lib/iomgr/socket_utils_linux.cc +0 -1
  165. data/src/core/lib/iomgr/socket_utils_posix.cc +2 -3
  166. data/src/core/lib/iomgr/socket_utils_posix.h +3 -0
  167. data/src/core/lib/iomgr/socket_utils_uv.cc +4 -0
  168. data/src/core/lib/iomgr/socket_utils_windows.cc +4 -0
  169. data/src/core/lib/iomgr/socket_windows.cc +33 -0
  170. data/src/core/lib/iomgr/socket_windows.h +6 -0
  171. data/src/core/lib/iomgr/tcp_client_custom.cc +5 -5
  172. data/src/core/lib/iomgr/tcp_client_posix.cc +10 -11
  173. data/src/core/lib/iomgr/tcp_custom.cc +11 -11
  174. data/src/core/lib/iomgr/tcp_posix.cc +49 -36
  175. data/src/core/lib/iomgr/tcp_server_custom.cc +5 -5
  176. data/src/core/lib/iomgr/tcp_server_posix.cc +16 -36
  177. data/src/core/lib/iomgr/tcp_server_utils_posix.h +1 -1
  178. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +17 -5
  179. data/src/core/lib/iomgr/tcp_server_windows.cc +1 -0
  180. data/src/core/lib/iomgr/tcp_uv.cc +3 -0
  181. data/src/core/lib/iomgr/tcp_windows.cc +18 -2
  182. data/src/core/lib/iomgr/tcp_windows.h +2 -0
  183. data/src/core/lib/iomgr/timer.h +4 -3
  184. data/src/core/lib/iomgr/timer_generic.cc +133 -51
  185. data/src/core/lib/iomgr/timer_manager.cc +12 -14
  186. data/src/core/lib/iomgr/timer_uv.cc +3 -0
  187. data/src/core/lib/iomgr/udp_server.cc +106 -52
  188. data/src/core/lib/iomgr/udp_server.h +8 -4
  189. data/src/core/lib/json/json.cc +12 -1
  190. data/src/core/lib/json/json.h +5 -0
  191. data/src/core/lib/profiling/basic_timers.cc +1 -0
  192. data/src/core/lib/security/context/security_context.cc +8 -8
  193. data/src/core/lib/security/context/security_context.h +6 -2
  194. data/src/core/lib/security/credentials/alts/alts_credentials.h +0 -20
  195. data/src/core/lib/security/credentials/alts/check_gcp_environment_linux.cc +3 -2
  196. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_client_options.cc +7 -7
  197. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h +1 -38
  198. data/src/core/lib/security/credentials/credentials.h +1 -0
  199. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +89 -115
  200. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +16 -0
  201. data/src/core/lib/security/credentials/jwt/json_token.h +2 -0
  202. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +2 -0
  203. data/src/core/lib/security/credentials/local/local_credentials.cc +77 -0
  204. data/src/core/lib/security/credentials/local/local_credentials.h +40 -0
  205. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +11 -7
  206. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -1
  207. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +17 -3
  208. data/src/core/lib/security/security_connector/alts_security_connector.cc +2 -1
  209. data/src/core/lib/security/security_connector/load_system_roots.h +29 -0
  210. data/src/core/lib/{gpr/fork.h → security/security_connector/load_system_roots_fallback.cc} +10 -13
  211. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +165 -0
  212. data/src/core/lib/security/security_connector/load_system_roots_linux.h +44 -0
  213. data/src/core/lib/security/security_connector/local_security_connector.cc +245 -0
  214. data/src/core/lib/security/security_connector/local_security_connector.h +58 -0
  215. data/src/core/lib/security/security_connector/security_connector.cc +79 -32
  216. data/src/core/lib/security/security_connector/security_connector.h +5 -3
  217. data/src/core/lib/security/transport/client_auth_filter.cc +5 -5
  218. data/src/core/lib/security/transport/secure_endpoint.cc +2 -2
  219. data/src/core/lib/security/transport/security_handshaker.cc +7 -2
  220. data/src/core/lib/security/transport/server_auth_filter.cc +4 -7
  221. data/src/core/lib/security/util/json_util.cc +4 -0
  222. data/src/core/lib/slice/slice.cc +6 -2
  223. data/src/core/lib/slice/slice_buffer.cc +27 -7
  224. data/src/core/lib/slice/slice_hash_table.h +4 -0
  225. data/src/core/lib/slice/slice_weak_hash_table.h +4 -0
  226. data/src/core/lib/surface/call.cc +119 -58
  227. data/src/core/lib/surface/call.h +7 -0
  228. data/src/core/lib/surface/channel.cc +50 -18
  229. data/src/core/lib/surface/channel.h +4 -0
  230. data/src/core/lib/surface/completion_queue.cc +153 -18
  231. data/src/core/lib/surface/completion_queue.h +20 -2
  232. data/src/core/lib/surface/completion_queue_factory.cc +13 -4
  233. data/src/core/lib/surface/init.cc +7 -8
  234. data/src/core/lib/surface/init.h +0 -1
  235. data/src/core/lib/surface/server.cc +16 -0
  236. data/src/core/lib/surface/version.cc +1 -1
  237. data/src/core/lib/transport/bdp_estimator.cc +3 -3
  238. data/src/core/lib/transport/bdp_estimator.h +2 -2
  239. data/src/core/lib/transport/byte_stream.cc +1 -1
  240. data/src/core/lib/transport/connectivity_state.cc +6 -7
  241. data/src/core/lib/transport/service_config.cc +2 -2
  242. data/src/core/lib/transport/service_config.h +3 -3
  243. data/src/core/lib/transport/transport.cc +22 -10
  244. data/src/core/lib/transport/transport.h +18 -18
  245. data/src/core/lib/transport/transport_op_string.cc +1 -8
  246. data/src/core/plugin_registry/grpc_plugin_registry.cc +0 -4
  247. data/src/core/tsi/alts/crypt/aes_gcm.cc +2 -0
  248. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +19 -7
  249. data/src/core/tsi/alts/handshaker/alts_handshaker_client.h +10 -0
  250. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h +2 -2
  251. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +38 -3
  252. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h +3 -0
  253. data/src/core/tsi/alts/handshaker/altscontext.pb.c +0 -1
  254. data/src/core/tsi/alts/handshaker/altscontext.pb.h +1 -2
  255. data/src/core/tsi/alts/handshaker/handshaker.pb.c +0 -1
  256. data/src/core/tsi/alts/handshaker/handshaker.pb.h +1 -2
  257. data/src/core/tsi/alts/handshaker/transport_security_common.pb.c +0 -1
  258. data/src/core/tsi/alts/handshaker/transport_security_common.pb.h +1 -1
  259. data/src/core/tsi/alts/handshaker/transport_security_common_api.h +2 -2
  260. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.cc +47 -1
  261. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.h +3 -1
  262. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -11
  263. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.h +7 -2
  264. data/src/core/tsi/fake_transport_security.cc +1 -0
  265. data/src/core/tsi/grpc_shadow_boringssl.h +3006 -0
  266. data/src/core/tsi/local_transport_security.cc +209 -0
  267. data/src/core/tsi/local_transport_security.h +51 -0
  268. data/src/core/tsi/ssl/session_cache/ssl_session.h +2 -0
  269. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +5 -5
  270. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +6 -0
  271. data/src/core/tsi/ssl_transport_security.cc +245 -116
  272. data/src/core/tsi/ssl_types.h +2 -0
  273. data/src/core/tsi/transport_security.cc +14 -0
  274. data/src/core/tsi/transport_security.h +2 -0
  275. data/src/core/tsi/transport_security_interface.h +11 -1
  276. data/src/ruby/bin/math_client.rb +17 -9
  277. data/src/ruby/ext/grpc/extconf.rb +1 -26
  278. data/src/ruby/ext/grpc/rb_channel_credentials.c +3 -3
  279. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +42 -16
  280. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +65 -26
  281. data/src/ruby/lib/grpc/generic/active_call.rb +19 -23
  282. data/src/ruby/lib/grpc/generic/rpc_server.rb +2 -1
  283. data/src/ruby/lib/grpc/version.rb +1 -1
  284. data/src/ruby/pb/generate_proto_ruby.sh +7 -1
  285. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +4 -1
  286. data/src/ruby/spec/call_credentials_spec.rb +1 -1
  287. data/src/ruby/spec/call_spec.rb +1 -1
  288. data/src/ruby/spec/channel_credentials_spec.rb +1 -1
  289. data/src/ruby/spec/channel_spec.rb +1 -1
  290. data/src/ruby/spec/client_auth_spec.rb +1 -12
  291. data/src/ruby/spec/client_server_spec.rb +1 -1
  292. data/src/ruby/spec/compression_options_spec.rb +1 -1
  293. data/src/ruby/spec/error_sanity_spec.rb +1 -1
  294. data/src/ruby/spec/generic/client_stub_spec.rb +16 -4
  295. data/src/ruby/spec/generic/rpc_desc_spec.rb +1 -1
  296. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +1 -1
  297. data/src/ruby/spec/generic/service_spec.rb +1 -1
  298. data/src/ruby/spec/google_rpc_status_utils_spec.rb +1 -12
  299. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +1 -0
  300. data/src/ruby/spec/pb/health/checker_spec.rb +1 -1
  301. data/src/ruby/spec/server_credentials_spec.rb +1 -1
  302. data/src/ruby/spec/server_spec.rb +1 -1
  303. data/src/ruby/spec/spec_helper.rb +1 -0
  304. data/src/ruby/spec/support/services.rb +1 -1
  305. data/src/ruby/spec/time_consts_spec.rb +1 -1
  306. data/third_party/address_sorting/address_sorting.c +17 -11
  307. data/third_party/address_sorting/address_sorting_windows.c +43 -3
  308. data/third_party/address_sorting/include/address_sorting/address_sorting.h +3 -0
  309. data/third_party/boringssl/crypto/asn1/a_int.c +33 -28
  310. data/third_party/boringssl/crypto/asn1/a_mbstr.c +24 -22
  311. data/third_party/boringssl/crypto/asn1/a_utf8.c +13 -11
  312. data/third_party/boringssl/crypto/asn1/asn1_locl.h +3 -0
  313. data/third_party/boringssl/crypto/asn1/tasn_dec.c +40 -19
  314. data/third_party/boringssl/crypto/bio/fd.c +1 -0
  315. data/third_party/boringssl/crypto/bio/file.c +2 -0
  316. data/third_party/boringssl/crypto/bn_extra/convert.c +6 -5
  317. data/third_party/boringssl/crypto/bytestring/ber.c +1 -4
  318. data/third_party/boringssl/crypto/bytestring/cbb.c +116 -16
  319. data/third_party/boringssl/crypto/bytestring/cbs.c +151 -20
  320. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +203 -0
  321. data/third_party/boringssl/crypto/cipher_extra/e_rc2.c +2 -0
  322. data/third_party/boringssl/crypto/cipher_extra/e_tls.c +1 -2
  323. data/third_party/boringssl/crypto/cpu-aarch64-fuchsia.c +55 -0
  324. data/third_party/boringssl/crypto/cpu-aarch64-linux.c +2 -1
  325. data/third_party/boringssl/crypto/dsa/dsa.c +16 -54
  326. data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +9 -10
  327. data/third_party/boringssl/crypto/ecdh/ecdh.c +4 -3
  328. data/third_party/boringssl/crypto/fipsmodule/bcm.c +11 -542
  329. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +57 -112
  330. data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +4 -3
  331. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +128 -70
  332. data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +32 -71
  333. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +64 -118
  334. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +284 -122
  335. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +31 -65
  336. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +274 -218
  337. data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +2 -1
  338. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +187 -27
  339. data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +1 -1
  340. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +124 -81
  341. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +8 -30
  342. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +321 -347
  343. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +326 -66
  344. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +77 -25
  345. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +199 -222
  346. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.h +27 -47
  347. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +155 -96
  348. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +1 -1
  349. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +10 -10
  350. data/third_party/boringssl/crypto/fipsmodule/des/internal.h +2 -0
  351. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +78 -47
  352. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +99 -163
  353. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +3 -10
  354. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +44 -23
  355. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +59 -90
  356. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +38 -65
  357. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +5378 -5418
  358. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +17 -26
  359. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.h +15 -11
  360. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +45 -51
  361. data/third_party/boringssl/crypto/fipsmodule/ec/{util-64.c → util.c} +0 -5
  362. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +144 -264
  363. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +78 -62
  364. data/third_party/boringssl/crypto/fipsmodule/modes/ccm.c +256 -0
  365. data/third_party/boringssl/crypto/fipsmodule/modes/internal.h +36 -32
  366. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +9 -7
  367. data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +16 -40
  368. data/third_party/boringssl/crypto/fipsmodule/rsa/internal.h +1 -6
  369. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +57 -39
  370. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +309 -142
  371. data/third_party/boringssl/crypto/fipsmodule/self_check/self_check.c +581 -0
  372. data/third_party/boringssl/crypto/fipsmodule/tls/internal.h +39 -0
  373. data/third_party/boringssl/crypto/fipsmodule/tls/kdf.c +165 -0
  374. data/third_party/boringssl/crypto/internal.h +65 -2
  375. data/third_party/boringssl/crypto/mem.c +0 -2
  376. data/third_party/boringssl/crypto/obj/obj.c +6 -73
  377. data/third_party/boringssl/crypto/thread_pthread.c +35 -5
  378. data/third_party/boringssl/crypto/x509/a_strex.c +11 -11
  379. data/third_party/boringssl/crypto/x509/vpm_int.h +1 -0
  380. data/third_party/boringssl/crypto/x509/x509_vfy.c +4 -0
  381. data/third_party/boringssl/crypto/x509/x509_vpm.c +44 -22
  382. data/third_party/boringssl/crypto/x509/x_name.c +13 -0
  383. data/third_party/boringssl/include/openssl/aead.h +10 -0
  384. data/third_party/boringssl/include/openssl/asn1.h +2 -3
  385. data/third_party/boringssl/include/openssl/base.h +5 -14
  386. data/third_party/boringssl/include/openssl/bio.h +1 -1
  387. data/third_party/boringssl/include/openssl/bn.h +62 -18
  388. data/third_party/boringssl/include/openssl/bytestring.h +53 -28
  389. data/third_party/boringssl/include/openssl/crypto.h +4 -0
  390. data/third_party/boringssl/include/openssl/ec.h +10 -4
  391. data/third_party/boringssl/include/openssl/ec_key.h +7 -6
  392. data/third_party/boringssl/include/openssl/err.h +9 -9
  393. data/third_party/boringssl/include/openssl/evp.h +1 -1
  394. data/third_party/boringssl/include/openssl/rsa.h +35 -10
  395. data/third_party/boringssl/include/openssl/ssl.h +167 -19
  396. data/third_party/boringssl/include/openssl/ssl3.h +0 -1
  397. data/third_party/boringssl/include/openssl/stack.h +1 -1
  398. data/third_party/boringssl/include/openssl/tls1.h +10 -2
  399. data/third_party/boringssl/include/openssl/x509.h +4 -0
  400. data/third_party/boringssl/include/openssl/x509v3.h +1 -0
  401. data/third_party/boringssl/ssl/d1_both.cc +16 -2
  402. data/third_party/boringssl/ssl/dtls_method.cc +1 -1
  403. data/third_party/boringssl/ssl/handoff.cc +285 -0
  404. data/third_party/boringssl/ssl/handshake.cc +26 -12
  405. data/third_party/boringssl/ssl/handshake_client.cc +101 -95
  406. data/third_party/boringssl/ssl/handshake_server.cc +14 -2
  407. data/third_party/boringssl/ssl/internal.h +132 -79
  408. data/third_party/boringssl/ssl/s3_both.cc +2 -2
  409. data/third_party/boringssl/ssl/s3_lib.cc +3 -1
  410. data/third_party/boringssl/ssl/s3_pkt.cc +0 -18
  411. data/third_party/boringssl/ssl/ssl_aead_ctx.cc +1 -4
  412. data/third_party/boringssl/ssl/ssl_asn1.cc +47 -43
  413. data/third_party/boringssl/ssl/ssl_cipher.cc +12 -8
  414. data/third_party/boringssl/ssl/ssl_key_share.cc +3 -1
  415. data/third_party/boringssl/ssl/ssl_lib.cc +83 -14
  416. data/third_party/boringssl/ssl/ssl_privkey.cc +6 -0
  417. data/third_party/boringssl/ssl/ssl_stat.cc +6 -6
  418. data/third_party/boringssl/ssl/ssl_versions.cc +12 -85
  419. data/third_party/boringssl/ssl/ssl_x509.cc +59 -61
  420. data/third_party/boringssl/ssl/t1_enc.cc +73 -124
  421. data/third_party/boringssl/ssl/t1_lib.cc +367 -41
  422. data/third_party/boringssl/ssl/tls13_both.cc +8 -0
  423. data/third_party/boringssl/ssl/tls13_client.cc +98 -184
  424. data/third_party/boringssl/ssl/tls13_enc.cc +88 -158
  425. data/third_party/boringssl/ssl/tls13_server.cc +91 -137
  426. data/third_party/boringssl/ssl/tls_method.cc +0 -17
  427. data/third_party/boringssl/ssl/tls_record.cc +1 -10
  428. data/third_party/boringssl/third_party/fiat/curve25519.c +921 -2753
  429. data/third_party/boringssl/third_party/fiat/curve25519_tables.h +7880 -0
  430. data/third_party/boringssl/third_party/fiat/internal.h +32 -20
  431. data/third_party/boringssl/third_party/fiat/p256.c +1824 -0
  432. metadata +86 -71
  433. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.cc +0 -253
  434. data/src/core/ext/filters/load_reporting/server_load_reporting_filter.cc +0 -222
  435. data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.cc +0 -71
  436. data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.h +0 -61
  437. data/src/core/lib/channel/channel_trace_registry.cc +0 -80
  438. data/src/core/lib/channel/channel_trace_registry.h +0 -43
  439. data/src/core/lib/gpr/fork.cc +0 -78
  440. data/src/core/tsi/transport_security_adapter.cc +0 -235
  441. data/src/core/tsi/transport_security_adapter.h +0 -41
  442. data/src/ruby/bin/apis/google/protobuf/empty.rb +0 -29
  443. data/src/ruby/bin/apis/pubsub_demo.rb +0 -241
  444. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub.rb +0 -159
  445. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub_services.rb +0 -88
  446. data/src/ruby/pb/test/client.rb +0 -764
  447. data/src/ruby/pb/test/server.rb +0 -252
  448. data/src/ruby/spec/pb/package_with_underscore/checker_spec.rb +0 -54
  449. data/src/ruby/spec/pb/package_with_underscore/data.proto +0 -23
  450. data/src/ruby/spec/pb/package_with_underscore/service.proto +0 -23
  451. data/third_party/boringssl/crypto/curve25519/x25519-x86_64.c +0 -247
  452. data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +0 -1674
@@ -66,38 +66,6 @@ extern "C" {
66
66
  #define STRICT_ALIGNMENT 0
67
67
  #endif
68
68
 
69
- #if defined(__GNUC__) && __GNUC__ >= 2
70
- static inline uint32_t CRYPTO_bswap4(uint32_t x) {
71
- return __builtin_bswap32(x);
72
- }
73
-
74
- static inline uint64_t CRYPTO_bswap8(uint64_t x) {
75
- return __builtin_bswap64(x);
76
- }
77
- #elif defined(_MSC_VER)
78
- OPENSSL_MSVC_PRAGMA(warning(push, 3))
79
- #include <intrin.h>
80
- OPENSSL_MSVC_PRAGMA(warning(pop))
81
- #pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
82
- static inline uint32_t CRYPTO_bswap4(uint32_t x) {
83
- return _byteswap_ulong(x);
84
- }
85
-
86
- static inline uint64_t CRYPTO_bswap8(uint64_t x) {
87
- return _byteswap_uint64(x);
88
- }
89
- #else
90
- static inline uint32_t CRYPTO_bswap4(uint32_t x) {
91
- x = (x >> 16) | (x << 16);
92
- x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
93
- return x;
94
- }
95
-
96
- static inline uint64_t CRYPTO_bswap8(uint64_t x) {
97
- return CRYPTO_bswap4(x >> 32) | (((uint64_t)CRYPTO_bswap4(x)) << 32);
98
- }
99
- #endif
100
-
101
69
  static inline uint32_t GETU32(const void *in) {
102
70
  uint32_t v;
103
71
  OPENSSL_memcpy(&v, in, sizeof(v));
@@ -281,6 +249,42 @@ OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag,
281
249
  size_t len);
282
250
 
283
251
 
252
+ // CCM.
253
+
254
+ typedef struct ccm128_context {
255
+ block128_f block;
256
+ ctr128_f ctr;
257
+ unsigned M, L;
258
+ } CCM128_CONTEXT;
259
+
260
+ // CRYPTO_ccm128_init initialises |ctx| to use |block| (typically AES) with the
261
+ // specified |M| and |L| parameters. It returns one on success and zero if |M|
262
+ // or |L| is invalid.
263
+ int CRYPTO_ccm128_init(CCM128_CONTEXT *ctx, const void *key, block128_f block,
264
+ ctr128_f ctr, unsigned M, unsigned L);
265
+
266
+ // CRYPTO_ccm128_max_input returns the maximum input length accepted by |ctx|.
267
+ size_t CRYPTO_ccm128_max_input(const CCM128_CONTEXT *ctx);
268
+
269
+ // CRYPTO_ccm128_encrypt encrypts |len| bytes from |in| to |out| writing the tag
270
+ // to |out_tag|. |key| must be the same key that was passed to
271
+ // |CRYPTO_ccm128_init|. It returns one on success and zero otherwise.
272
+ int CRYPTO_ccm128_encrypt(const CCM128_CONTEXT *ctx, const void *key,
273
+ uint8_t *out, uint8_t *out_tag, size_t tag_len,
274
+ const uint8_t *nonce, size_t nonce_len,
275
+ const uint8_t *in, size_t len, const uint8_t *aad,
276
+ size_t aad_len);
277
+
278
+ // CRYPTO_ccm128_decrypt decrypts |len| bytes from |in| to |out|, writing the
279
+ // expected tag to |out_tag|. |key| must be the same key that was passed to
280
+ // |CRYPTO_ccm128_init|. It returns one on success and zero otherwise.
281
+ int CRYPTO_ccm128_decrypt(const CCM128_CONTEXT *ctx, const void *key,
282
+ uint8_t *out, uint8_t *out_tag, size_t tag_len,
283
+ const uint8_t *nonce, size_t nonce_len,
284
+ const uint8_t *in, size_t len, const uint8_t *aad,
285
+ size_t aad_len);
286
+
287
+
284
288
  // CBC.
285
289
 
286
290
  // cbc128_f is the type of a function that performs CBC-mode encryption.
@@ -74,11 +74,11 @@ static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) {
74
74
  CRYPTO_bswap4(CRYPTO_bswap4(drbg->counter.words[3]) + n);
75
75
  }
76
76
 
77
- static int CTR_DRBG_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
77
+ static int ctr_drbg_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
78
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.
79
+ // Per section 10.2.1.2, |data_len| must be |CTR_DRBG_ENTROPY_LEN|. Here, we
80
+ // allow shorter inputs and right-pad them with zeros. This is equivalent to
81
+ // the specified algorithm but saves a copy in |CTR_DRBG_generate|.
82
82
  if (data_len > CTR_DRBG_ENTROPY_LEN) {
83
83
  return 0;
84
84
  }
@@ -119,7 +119,7 @@ int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
119
119
  entropy = entropy_copy;
120
120
  }
121
121
 
122
- if (!CTR_DRBG_update(drbg, entropy, CTR_DRBG_ENTROPY_LEN)) {
122
+ if (!ctr_drbg_update(drbg, entropy, CTR_DRBG_ENTROPY_LEN)) {
123
123
  return 0;
124
124
  }
125
125
 
@@ -142,7 +142,7 @@ int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
142
142
  }
143
143
 
144
144
  if (additional_data_len != 0 &&
145
- !CTR_DRBG_update(drbg, additional_data, additional_data_len)) {
145
+ !ctr_drbg_update(drbg, additional_data, additional_data_len)) {
146
146
  return 0;
147
147
  }
148
148
 
@@ -187,7 +187,9 @@ int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
187
187
  OPENSSL_memcpy(out, block, out_len);
188
188
  }
189
189
 
190
- if (!CTR_DRBG_update(drbg, additional_data, additional_data_len)) {
190
+ // Right-padding |additional_data| in step 2.2 is handled implicitly by
191
+ // |ctr_drbg_update|, to save a copy.
192
+ if (!ctr_drbg_update(drbg, additional_data, additional_data_len)) {
191
193
  return 0;
192
194
  }
193
195
 
@@ -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
@@ -132,17 +132,21 @@ void RSA_free(RSA *rsa) {
132
132
 
133
133
  CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
134
134
 
135
- BN_clear_free(rsa->n);
136
- BN_clear_free(rsa->e);
137
- BN_clear_free(rsa->d);
138
- BN_clear_free(rsa->p);
139
- BN_clear_free(rsa->q);
140
- BN_clear_free(rsa->dmp1);
141
- BN_clear_free(rsa->dmq1);
142
- BN_clear_free(rsa->iqmp);
135
+ BN_free(rsa->n);
136
+ BN_free(rsa->e);
137
+ BN_free(rsa->d);
138
+ BN_free(rsa->p);
139
+ BN_free(rsa->q);
140
+ BN_free(rsa->dmp1);
141
+ BN_free(rsa->dmq1);
142
+ BN_free(rsa->iqmp);
143
143
  BN_MONT_CTX_free(rsa->mont_n);
144
144
  BN_MONT_CTX_free(rsa->mont_p);
145
145
  BN_MONT_CTX_free(rsa->mont_q);
146
+ BN_free(rsa->d_fixed);
147
+ BN_free(rsa->dmp1_fixed);
148
+ BN_free(rsa->dmq1_fixed);
149
+ BN_free(rsa->inv_small_mod_large_mont);
146
150
  for (u = 0; u < rsa->num_blindings; u++) {
147
151
  BN_BLINDING_free(rsa->blindings[u]);
148
152
  }
@@ -630,8 +634,25 @@ err:
630
634
  return ret;
631
635
  }
632
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
+
633
654
  int RSA_check_key(const RSA *key) {
634
- BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
655
+ BIGNUM n, pm1, qm1, lcm, dmp1, dmq1, iqmp_times_q;
635
656
  BN_CTX *ctx;
636
657
  int ok = 0, has_crt_values;
637
658
 
@@ -666,26 +687,20 @@ int RSA_check_key(const RSA *key) {
666
687
  BN_init(&pm1);
667
688
  BN_init(&qm1);
668
689
  BN_init(&lcm);
669
- BN_init(&gcd);
670
- BN_init(&de);
671
690
  BN_init(&dmp1);
672
691
  BN_init(&dmq1);
673
692
  BN_init(&iqmp_times_q);
674
693
 
675
- if (!BN_mul(&n, key->p, key->q, ctx) ||
694
+ int d_ok;
695
+ if (!bn_mul_consttime(&n, key->p, key->q, ctx) ||
676
696
  // lcm = lcm(p, q)
677
- !BN_sub(&pm1, key->p, BN_value_one()) ||
678
- !BN_sub(&qm1, key->q, BN_value_one()) ||
679
- !BN_mul(&lcm, &pm1, &qm1, ctx) ||
680
- !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
681
- OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
682
- goto out;
683
- }
684
-
685
- if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
686
- !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
687
- // de = d*e mod lcm(p, q).
688
- !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)) {
689
704
  OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
690
705
  goto out;
691
706
  }
@@ -695,11 +710,16 @@ int RSA_check_key(const RSA *key) {
695
710
  goto out;
696
711
  }
697
712
 
698
- if (!BN_is_one(&de)) {
713
+ if (!d_ok) {
699
714
  OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
700
715
  goto out;
701
716
  }
702
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
+
703
723
  has_crt_values = key->dmp1 != NULL;
704
724
  if (has_crt_values != (key->dmq1 != NULL) ||
705
725
  has_crt_values != (key->iqmp != NULL)) {
@@ -708,20 +728,18 @@ int RSA_check_key(const RSA *key) {
708
728
  }
709
729
 
710
730
  if (has_crt_values) {
711
- if (// dmp1 = d mod (p-1)
712
- !BN_mod(&dmp1, key->d, &pm1, ctx) ||
713
- // dmq1 = d mod (q-1)
714
- !BN_mod(&dmq1, key->d, &qm1, ctx) ||
715
- // iqmp = q^-1 mod p
716
- !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)) {
717
738
  OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
718
739
  goto out;
719
740
  }
720
741
 
721
- if (BN_cmp(&dmp1, key->dmp1) != 0 ||
722
- BN_cmp(&dmq1, key->dmq1) != 0 ||
723
- BN_cmp(key->iqmp, key->p) >= 0 ||
724
- !BN_is_one(&iqmp_times_q)) {
742
+ if (!dmp1_ok || !dmq1_ok || !iqmp_ok) {
725
743
  OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
726
744
  goto out;
727
745
  }
@@ -734,8 +752,6 @@ out:
734
752
  BN_free(&pm1);
735
753
  BN_free(&qm1);
736
754
  BN_free(&lcm);
737
- BN_free(&gcd);
738
- BN_free(&de);
739
755
  BN_free(&dmp1);
740
756
  BN_free(&dmq1);
741
757
  BN_free(&iqmp_times_q);
@@ -760,8 +776,8 @@ static const BN_ULONG kSmallFactorsLimbs[] = {
760
776
 
761
777
  DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) {
762
778
  out->d = (BN_ULONG *) kSmallFactorsLimbs;
763
- out->top = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs);
764
- out->dmax = out->top;
779
+ out->width = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs);
780
+ out->dmax = out->width;
765
781
  out->neg = 0;
766
782
  out->flags = BN_FLG_STATIC_DATA;
767
783
  }
@@ -852,6 +868,8 @@ int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
852
868
  return rsa_default_private_transform(rsa, out, in, len);
853
869
  }
854
870
 
871
+ int RSA_flags(const RSA *rsa) { return rsa->flags; }
872
+
855
873
  int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
856
874
  return 1;
857
875
  }
@@ -109,6 +109,133 @@ static int check_modulus_and_exponent_sizes(const RSA *rsa) {
109
109
  return 1;
110
110
  }
111
111
 
112
+ static int ensure_fixed_copy(BIGNUM **out, const BIGNUM *in, int width) {
113
+ if (*out != NULL) {
114
+ return 1;
115
+ }
116
+ BIGNUM *copy = BN_dup(in);
117
+ if (copy == NULL ||
118
+ !bn_resize_words(copy, width)) {
119
+ BN_free(copy);
120
+ return 0;
121
+ }
122
+ *out = copy;
123
+ return 1;
124
+ }
125
+
126
+ // freeze_private_key finishes initializing |rsa|'s private key components.
127
+ // After this function has returned, |rsa| may not be changed. This is needed
128
+ // because |RSA| is a public struct and, additionally, OpenSSL 1.1.0 opaquified
129
+ // it wrong (see https://github.com/openssl/openssl/issues/5158).
130
+ static int freeze_private_key(RSA *rsa, BN_CTX *ctx) {
131
+ CRYPTO_MUTEX_lock_read(&rsa->lock);
132
+ int frozen = rsa->private_key_frozen;
133
+ CRYPTO_MUTEX_unlock_read(&rsa->lock);
134
+ if (frozen) {
135
+ return 1;
136
+ }
137
+
138
+ int ret = 0;
139
+ CRYPTO_MUTEX_lock_write(&rsa->lock);
140
+ if (rsa->private_key_frozen) {
141
+ ret = 1;
142
+ goto err;
143
+ }
144
+
145
+ // Pre-compute various intermediate values, as well as copies of private
146
+ // exponents with correct widths. Note that other threads may concurrently
147
+ // read from |rsa->n|, |rsa->e|, etc., so any fixes must be in separate
148
+ // copies. We use |mont_n->N|, |mont_p->N|, and |mont_q->N| as copies of |n|,
149
+ // |p|, and |q| with the correct minimal widths.
150
+
151
+ if (rsa->mont_n == NULL) {
152
+ rsa->mont_n = BN_MONT_CTX_new_for_modulus(rsa->n, ctx);
153
+ if (rsa->mont_n == NULL) {
154
+ goto err;
155
+ }
156
+ }
157
+ const BIGNUM *n_fixed = &rsa->mont_n->N;
158
+
159
+ // The only public upper-bound of |rsa->d| is the bit length of |rsa->n|. The
160
+ // ASN.1 serialization of RSA private keys unfortunately leaks the byte length
161
+ // of |rsa->d|, but normalize it so we only leak it once, rather than per
162
+ // operation.
163
+ if (rsa->d != NULL &&
164
+ !ensure_fixed_copy(&rsa->d_fixed, rsa->d, n_fixed->width)) {
165
+ goto err;
166
+ }
167
+
168
+ if (rsa->p != NULL && rsa->q != NULL) {
169
+ if (rsa->mont_p == NULL) {
170
+ rsa->mont_p = BN_MONT_CTX_new_for_modulus(rsa->p, ctx);
171
+ if (rsa->mont_p == NULL) {
172
+ goto err;
173
+ }
174
+ }
175
+ const BIGNUM *p_fixed = &rsa->mont_p->N;
176
+
177
+ if (rsa->mont_q == NULL) {
178
+ rsa->mont_q = BN_MONT_CTX_new_for_modulus(rsa->q, ctx);
179
+ if (rsa->mont_q == NULL) {
180
+ goto err;
181
+ }
182
+ }
183
+ const BIGNUM *q_fixed = &rsa->mont_q->N;
184
+
185
+ if (rsa->dmp1 != NULL && rsa->dmq1 != NULL) {
186
+ // Key generation relies on this function to compute |iqmp|.
187
+ if (rsa->iqmp == NULL) {
188
+ BIGNUM *iqmp = BN_new();
189
+ if (iqmp == NULL ||
190
+ !bn_mod_inverse_secret_prime(iqmp, rsa->q, rsa->p, ctx,
191
+ rsa->mont_p)) {
192
+ BN_free(iqmp);
193
+ goto err;
194
+ }
195
+ rsa->iqmp = iqmp;
196
+ }
197
+
198
+ // CRT components are only publicly bounded by their corresponding
199
+ // moduli's bit lengths. |rsa->iqmp| is unused outside of this one-time
200
+ // setup, so we do not compute a fixed-width version of it.
201
+ if (!ensure_fixed_copy(&rsa->dmp1_fixed, rsa->dmp1, p_fixed->width) ||
202
+ !ensure_fixed_copy(&rsa->dmq1_fixed, rsa->dmq1, q_fixed->width)) {
203
+ goto err;
204
+ }
205
+
206
+ // Compute |inv_small_mod_large_mont|. Note that it is always modulo the
207
+ // larger prime, independent of what is stored in |rsa->iqmp|.
208
+ if (rsa->inv_small_mod_large_mont == NULL) {
209
+ BIGNUM *inv_small_mod_large_mont = BN_new();
210
+ int ok;
211
+ if (BN_cmp(rsa->p, rsa->q) < 0) {
212
+ ok = inv_small_mod_large_mont != NULL &&
213
+ bn_mod_inverse_secret_prime(inv_small_mod_large_mont, rsa->p,
214
+ rsa->q, ctx, rsa->mont_q) &&
215
+ BN_to_montgomery(inv_small_mod_large_mont,
216
+ inv_small_mod_large_mont, rsa->mont_q, ctx);
217
+ } else {
218
+ ok = inv_small_mod_large_mont != NULL &&
219
+ BN_to_montgomery(inv_small_mod_large_mont, rsa->iqmp,
220
+ rsa->mont_p, ctx);
221
+ }
222
+ if (!ok) {
223
+ BN_free(inv_small_mod_large_mont);
224
+ goto err;
225
+ }
226
+ rsa->inv_small_mod_large_mont = inv_small_mod_large_mont;
227
+ }
228
+ }
229
+ }
230
+
231
+ rsa->private_key_frozen = 1;
232
+ ret = 1;
233
+
234
+ err:
235
+ CRYPTO_MUTEX_unlock_write(&rsa->lock);
236
+ return ret;
237
+ }
238
+
112
239
  size_t rsa_default_size(const RSA *rsa) {
113
240
  return BN_num_bytes(rsa->n);
114
241
  }
@@ -181,7 +308,7 @@ int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
181
308
  }
182
309
 
183
310
  if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
184
- !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
311
+ !BN_mod_exp_mont(result, f, rsa->e, &rsa->mont_n->N, ctx, rsa->mont_n)) {
185
312
  goto err;
186
313
  }
187
314
 
@@ -487,7 +614,7 @@ int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
487
614
  }
488
615
 
489
616
  if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
490
- !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
617
+ !BN_mod_exp_mont(result, f, rsa->e, &rsa->mont_n->N, ctx, rsa->mont_n)) {
491
618
  goto err;
492
619
  }
493
620
 
@@ -560,7 +687,7 @@ int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
560
687
  goto err;
561
688
  }
562
689
 
563
- if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
690
+ if (!freeze_private_key(rsa, ctx)) {
564
691
  OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
565
692
  goto err;
566
693
  }
@@ -592,7 +719,7 @@ int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
592
719
  if (!mod_exp(result, f, rsa, ctx)) {
593
720
  goto err;
594
721
  }
595
- } else if (!BN_mod_exp_mont_consttime(result, f, rsa->d, rsa->n, ctx,
722
+ } else if (!BN_mod_exp_mont_consttime(result, f, rsa->d_fixed, rsa->n, ctx,
596
723
  rsa->mont_n)) {
597
724
  goto err;
598
725
  }
@@ -622,6 +749,12 @@ int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
622
749
  goto err;
623
750
  }
624
751
 
752
+ // The computation should have left |result| as a maximally-wide number, so
753
+ // that it and serializing does not leak information about the magnitude of
754
+ // the result.
755
+ //
756
+ // See Falko Stenzke, "Manger's Attack revisited", ICICS 2010.
757
+ assert(result->width == rsa->mont_n->N.width);
625
758
  if (!BN_bn2bin_padded(out, len, result)) {
626
759
  OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
627
760
  goto err;
@@ -641,6 +774,37 @@ err:
641
774
  return ret;
642
775
  }
643
776
 
777
+ // mod_montgomery sets |r| to |I| mod |p|. |I| must already be fully reduced
778
+ // modulo |p| times |q|. It returns one on success and zero on error.
779
+ static int mod_montgomery(BIGNUM *r, const BIGNUM *I, const BIGNUM *p,
780
+ const BN_MONT_CTX *mont_p, const BIGNUM *q,
781
+ BN_CTX *ctx) {
782
+ // Reducing in constant-time with Montgomery reduction requires I <= p * R. We
783
+ // have I < p * q, so this follows if q < R. In particular, this always holds
784
+ // if p and q are the same size, which is true for any RSA keys we or anyone
785
+ // sane generates. For other keys, we fall back to |BN_mod|.
786
+ if (!bn_less_than_montgomery_R(q, mont_p)) {
787
+ return BN_mod(r, I, p, ctx);
788
+ }
789
+
790
+ if (// Reduce mod p with Montgomery reduction. This computes I * R^-1 mod p.
791
+ !BN_from_montgomery(r, I, mont_p, ctx) ||
792
+ // Multiply by R^2 and do another Montgomery reduction to compute
793
+ // I * R^-1 * R^2 * R^-1 = I mod p.
794
+ !BN_to_montgomery(r, r, mont_p, ctx)) {
795
+ return 0;
796
+ }
797
+
798
+ // By precomputing R^3 mod p (normally |BN_MONT_CTX| only uses R^2 mod p) and
799
+ // adjusting the API for |BN_mod_exp_mont_consttime|, we could instead compute
800
+ // I * R mod p here and save a reduction per prime. But this would require
801
+ // changing the RSAZ code and may not be worth it. Note that the RSAZ code
802
+ // uses a different radix, so it uses R' = 2^1044. There we'd actually want
803
+ // R^2 * R', and would futher benefit from a precomputed R'^2. It currently
804
+ // converts |mont_p->RR| to R'^2.
805
+ return 1;
806
+ }
807
+
644
808
  static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
645
809
  assert(ctx != NULL);
646
810
 
@@ -653,82 +817,67 @@ static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
653
817
  assert(rsa->dmq1 != NULL);
654
818
  assert(rsa->iqmp != NULL);
655
819
 
656
- BIGNUM *r1, *m1, *vrfy;
820
+ BIGNUM *r1, *m1;
657
821
  int ret = 0;
658
822
 
659
823
  BN_CTX_start(ctx);
660
824
  r1 = BN_CTX_get(ctx);
661
825
  m1 = BN_CTX_get(ctx);
662
- vrfy = BN_CTX_get(ctx);
663
826
  if (r1 == NULL ||
664
- m1 == NULL ||
665
- vrfy == NULL) {
666
- goto err;
667
- }
668
-
669
- if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
670
- !BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, rsa->q, ctx)) {
827
+ m1 == NULL) {
671
828
  goto err;
672
829
  }
673
830
 
674
- if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
831
+ if (!freeze_private_key(rsa, ctx)) {
675
832
  goto err;
676
833
  }
677
834
 
678
- // compute I mod q
679
- if (!BN_mod(r1, I, rsa->q, ctx)) {
680
- goto err;
681
- }
682
-
683
- // compute r1^dmq1 mod q
684
- if (!BN_mod_exp_mont_consttime(m1, r1, rsa->dmq1, rsa->q, ctx, rsa->mont_q)) {
685
- goto err;
686
- }
687
-
688
- // compute I mod p
689
- if (!BN_mod(r1, I, rsa->p, ctx)) {
690
- goto err;
691
- }
692
-
693
- // compute r1^dmp1 mod p
694
- if (!BN_mod_exp_mont_consttime(r0, r1, rsa->dmp1, rsa->p, ctx, rsa->mont_p)) {
695
- goto err;
696
- }
697
-
698
- if (!BN_sub(r0, r0, m1)) {
699
- goto err;
700
- }
701
- // This will help stop the size of r0 increasing, which does
702
- // affect the multiply if it optimised for a power of 2 size
703
- if (BN_is_negative(r0)) {
704
- if (!BN_add(r0, r0, rsa->p)) {
705
- goto err;
706
- }
707
- }
708
-
709
- if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
710
- goto err;
711
- }
712
-
713
- if (!BN_mod(r0, r1, rsa->p, ctx)) {
714
- goto err;
715
- }
716
-
717
- // If p < q it is occasionally possible for the correction of
718
- // adding 'p' if r0 is negative above to leave the result still
719
- // negative. This can break the private key operations: the following
720
- // second correction should *always* correct this rare occurrence.
721
- // This will *never* happen with OpenSSL generated keys because
722
- // they ensure p > q [steve]
723
- if (BN_is_negative(r0)) {
724
- if (!BN_add(r0, r0, rsa->p)) {
725
- goto err;
726
- }
727
- }
728
- if (!BN_mul(r1, r0, rsa->q, ctx)) {
729
- goto err;
730
- }
731
- if (!BN_add(r0, r1, m1)) {
835
+ // Implementing RSA with CRT in constant-time is sensitive to which prime is
836
+ // larger. Canonicalize fields so that |p| is the larger prime.
837
+ const BIGNUM *dmp1 = rsa->dmp1_fixed, *dmq1 = rsa->dmq1_fixed;
838
+ const BN_MONT_CTX *mont_p = rsa->mont_p, *mont_q = rsa->mont_q;
839
+ if (BN_cmp(rsa->p, rsa->q) < 0) {
840
+ mont_p = rsa->mont_q;
841
+ mont_q = rsa->mont_p;
842
+ dmp1 = rsa->dmq1_fixed;
843
+ dmq1 = rsa->dmp1_fixed;
844
+ }
845
+
846
+ // Use the minimal-width versions of |n|, |p|, and |q|. Either works, but if
847
+ // someone gives us non-minimal values, these will be slightly more efficient
848
+ // on the non-Montgomery operations.
849
+ const BIGNUM *n = &rsa->mont_n->N;
850
+ const BIGNUM *p = &mont_p->N;
851
+ const BIGNUM *q = &mont_q->N;
852
+
853
+ // This is a pre-condition for |mod_montgomery|. It was already checked by the
854
+ // caller.
855
+ assert(BN_ucmp(I, n) < 0);
856
+
857
+ if (// |m1| is the result modulo |q|.
858
+ !mod_montgomery(r1, I, q, mont_q, p, ctx) ||
859
+ !BN_mod_exp_mont_consttime(m1, r1, dmq1, q, ctx, mont_q) ||
860
+ // |r0| is the result modulo |p|.
861
+ !mod_montgomery(r1, I, p, mont_p, q, ctx) ||
862
+ !BN_mod_exp_mont_consttime(r0, r1, dmp1, p, ctx, mont_p) ||
863
+ // Compute r0 = r0 - m1 mod p. |p| is the larger prime, so |m1| is already
864
+ // fully reduced mod |p|.
865
+ !bn_mod_sub_consttime(r0, r0, m1, p, ctx) ||
866
+ // r0 = r0 * iqmp mod p. We use Montgomery multiplication to compute this
867
+ // in constant time. |inv_small_mod_large_mont| is in Montgomery form and
868
+ // r0 is not, so the result is taken out of Montgomery form.
869
+ !BN_mod_mul_montgomery(r0, r0, rsa->inv_small_mod_large_mont, mont_p,
870
+ ctx) ||
871
+ // r0 = r0 * q + m1 gives the final result. Reducing modulo q gives m1, so
872
+ // it is correct mod p. Reducing modulo p gives (r0-m1)*iqmp*q + m1 = r0,
873
+ // so it is correct mod q. Finally, the result is bounded by [m1, n + m1),
874
+ // and the result is at least |m1|, so this must be the unique answer in
875
+ // [0, n).
876
+ !bn_mul_consttime(r0, r0, q, ctx) ||
877
+ !bn_uadd_consttime(r0, r0, m1) ||
878
+ // The result should be bounded by |n|, but fixed-width operations may
879
+ // bound the width slightly higher, so fix it.
880
+ !bn_resize_words(r0, n->width)) {
732
881
  goto err;
733
882
  }
734
883
 
@@ -775,24 +924,20 @@ const BN_ULONG kBoringSSLRSASqrtTwo[] = {
775
924
  };
776
925
  const size_t kBoringSSLRSASqrtTwoLen = OPENSSL_ARRAY_SIZE(kBoringSSLRSASqrtTwo);
777
926
 
778
- int rsa_greater_than_pow2(const BIGNUM *b, int n) {
779
- if (BN_is_negative(b) || n == INT_MAX) {
780
- return 0;
781
- }
782
-
783
- int b_bits = BN_num_bits(b);
784
- return b_bits > n + 1 || (b_bits == n + 1 && !BN_is_pow2(b));
785
- }
786
-
787
927
  // generate_prime sets |out| to a prime with length |bits| such that |out|-1 is
788
928
  // relatively prime to |e|. If |p| is non-NULL, |out| will also not be close to
789
- // |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).
790
931
  static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
791
- const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb) {
932
+ const BIGNUM *p, const BIGNUM *sqrt2,
933
+ const BIGNUM *pow2_bits_100, BN_CTX *ctx,
934
+ BN_GENCB *cb) {
792
935
  if (bits < 128 || (bits % BN_BITS2) != 0) {
793
936
  OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
794
937
  return 0;
795
938
  }
939
+ assert(BN_is_pow2(pow2_bits_100));
940
+ assert(BN_is_bit_set(pow2_bits_100, bits - 100));
796
941
 
797
942
  // See FIPS 186-4 appendix B.3.3, steps 4 and 5. Note |bits| here is nlen/2.
798
943
 
@@ -823,57 +968,45 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
823
968
 
824
969
  if (p != NULL) {
825
970
  // If |p| and |out| are too close, try again (step 5.4).
826
- if (!BN_sub(tmp, out, p)) {
971
+ if (!bn_abs_sub_consttime(tmp, out, p, ctx)) {
827
972
  goto err;
828
973
  }
829
- BN_set_negative(tmp, 0);
830
- if (!rsa_greater_than_pow2(tmp, bits - 100)) {
974
+ if (BN_cmp(tmp, pow2_bits_100) <= 0) {
831
975
  continue;
832
976
  }
833
977
  }
834
978
 
835
- // If out < 2^(bits-1)×√2, try again (steps 4.4 and 5.5).
836
- //
837
- // We check the most significant words, so we retry if ⌊out/2^k⌋ <= ⌊b/2^k⌋,
838
- // where b = 2^(bits-1)×√2 and k = max(0, bits - 1536). For key sizes up to
839
- // 3072 (bits = 1536), k = 0, so we are testing that ⌊out⌋ <= ⌊b⌋. out is an
840
- // integer and b is not, so this is equivalent to out < b. That is, the
841
- // comparison is exact for FIPS key sizes.
979
+ // If out < 2^(bits-1)×√2, try again (steps 4.4 and 5.5). This is equivalent
980
+ // to out <= ⌊2^(bits-1)×√2⌋, or out <= sqrt2 for FIPS key sizes.
842
981
  //
843
982
  // For larger keys, the comparison is approximate, leaning towards
844
983
  // retrying. That is, we reject a negligible fraction of primes that are
845
984
  // within the FIPS bound, but we will never accept a prime outside the
846
- // bound, ensuring the resulting RSA key is the right size. Specifically, if
847
- // the FIPS bound holds, we have ⌊out/2^k⌋ < out/2^k < b/2^k. This implies
848
- // ⌊out/2^k⌋ <= ⌊b/2^k⌋. That is, the FIPS bound implies our bound and so we
849
- // are slightly tighter.
850
- size_t out_len = (size_t)out->top;
851
- assert(out_len == (size_t)bits / BN_BITS2);
852
- size_t to_check = kBoringSSLRSASqrtTwoLen;
853
- if (to_check > out_len) {
854
- to_check = out_len;
855
- }
856
- if (!bn_less_than_words(
857
- kBoringSSLRSASqrtTwo + kBoringSSLRSASqrtTwoLen - to_check,
858
- out->d + out_len - to_check, to_check)) {
985
+ // bound, ensuring the resulting RSA key is the right size.
986
+ if (BN_cmp(out, sqrt2) <= 0) {
859
987
  continue;
860
988
  }
861
989
 
862
- // Check gcd(out-1, e) is one (steps 4.5 and 5.6).
863
- if (!BN_sub(tmp, out, BN_value_one()) ||
864
- !BN_gcd(tmp, tmp, e, ctx)) {
865
- goto err;
866
- }
867
- if (BN_is_one(tmp)) {
868
- // Test |out| for primality (steps 4.5.1 and 5.6.1).
869
- int is_probable_prime;
870
- if (!BN_primality_test(&is_probable_prime, out, BN_prime_checks, ctx, 1,
871
- 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)) {
872
997
  goto err;
873
998
  }
874
- if (is_probable_prime) {
875
- ret = 1;
876
- 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
+ }
877
1010
  }
878
1011
  }
879
1012
 
@@ -909,7 +1042,19 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
909
1042
  return 0;
910
1043
  }
911
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
+
912
1056
  int ret = 0;
1057
+ int prime_bits = bits / 2;
913
1058
  BN_CTX *ctx = BN_CTX_new();
914
1059
  if (ctx == NULL) {
915
1060
  goto bn_err;
@@ -918,8 +1063,13 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
918
1063
  BIGNUM *totient = BN_CTX_get(ctx);
919
1064
  BIGNUM *pm1 = BN_CTX_get(ctx);
920
1065
  BIGNUM *qm1 = BN_CTX_get(ctx);
921
- BIGNUM *gcd = BN_CTX_get(ctx);
922
- if (totient == NULL || pm1 == NULL || qm1 == NULL || gcd == NULL) {
1066
+ BIGNUM *sqrt2 = BN_CTX_get(ctx);
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)) {
923
1073
  goto bn_err;
924
1074
  }
925
1075
 
@@ -930,8 +1080,7 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
930
1080
  !ensure_bignum(&rsa->p) ||
931
1081
  !ensure_bignum(&rsa->q) ||
932
1082
  !ensure_bignum(&rsa->dmp1) ||
933
- !ensure_bignum(&rsa->dmq1) ||
934
- !ensure_bignum(&rsa->iqmp)) {
1083
+ !ensure_bignum(&rsa->dmq1)) {
935
1084
  goto bn_err;
936
1085
  }
937
1086
 
@@ -939,13 +1088,36 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
939
1088
  goto bn_err;
940
1089
  }
941
1090
 
942
- int prime_bits = bits / 2;
1091
+ // Compute sqrt2 >= 2^(prime_bits-1)×√2⌋.
1092
+ if (!bn_set_words(sqrt2, kBoringSSLRSASqrtTwo, kBoringSSLRSASqrtTwoLen)) {
1093
+ goto bn_err;
1094
+ }
1095
+ int sqrt2_bits = kBoringSSLRSASqrtTwoLen * BN_BITS2;
1096
+ assert(sqrt2_bits == (int)BN_num_bits(sqrt2));
1097
+ if (sqrt2_bits > prime_bits) {
1098
+ // For key sizes up to 3072 (prime_bits = 1536), this is exactly
1099
+ // ⌊2^(prime_bits-1)×√2⌋.
1100
+ if (!BN_rshift(sqrt2, sqrt2, sqrt2_bits - prime_bits)) {
1101
+ goto bn_err;
1102
+ }
1103
+ } else if (prime_bits > sqrt2_bits) {
1104
+ // For key sizes beyond 3072, this is approximate. We err towards retrying
1105
+ // to ensure our key is the right size and round up.
1106
+ if (!BN_add_word(sqrt2, 1) ||
1107
+ !BN_lshift(sqrt2, sqrt2, prime_bits - sqrt2_bits)) {
1108
+ goto bn_err;
1109
+ }
1110
+ }
1111
+ assert(prime_bits == (int)BN_num_bits(sqrt2));
1112
+
943
1113
  do {
944
1114
  // Generate p and q, each of size |prime_bits|, using the steps outlined in
945
1115
  // appendix FIPS 186-4 appendix B.3.3.
946
- if (!generate_prime(rsa->p, prime_bits, rsa->e, NULL, ctx, cb) ||
1116
+ if (!generate_prime(rsa->p, prime_bits, rsa->e, NULL, sqrt2,
1117
+ pow2_prime_bits_100, ctx, cb) ||
947
1118
  !BN_GENCB_call(cb, 3, 0) ||
948
- !generate_prime(rsa->q, prime_bits, rsa->e, rsa->p, ctx, cb) ||
1119
+ !generate_prime(rsa->q, prime_bits, rsa->e, rsa->p, sqrt2,
1120
+ pow2_prime_bits_100, ctx, cb) ||
949
1121
  !BN_GENCB_call(cb, 3, 1)) {
950
1122
  goto bn_err;
951
1123
  }
@@ -963,27 +1135,27 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
963
1135
  // q-1. However, we do operations with Chinese Remainder Theorem, so we only
964
1136
  // use d (mod p-1) and d (mod q-1) as exponents. Using a minimal totient
965
1137
  // does not affect those two values.
966
- if (!BN_sub(pm1, rsa->p, BN_value_one()) ||
967
- !BN_sub(qm1, rsa->q, BN_value_one()) ||
968
- !BN_mul(totient, pm1, qm1, ctx) ||
969
- !BN_gcd(gcd, pm1, qm1, ctx) ||
970
- !BN_div(totient, NULL, totient, gcd, ctx) ||
971
- !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)) {
972
1143
  goto bn_err;
973
1144
  }
974
1145
 
975
- // Check that |rsa->d| > 2^|prime_bits| and try again if it fails. See
976
- // appendix B.3.1's guidance on values for d.
977
- } 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);
978
1149
 
979
1150
  if (// Calculate n.
980
- !BN_mul(rsa->n, rsa->p, rsa->q, ctx) ||
1151
+ !bn_mul_consttime(rsa->n, rsa->p, rsa->q, ctx) ||
981
1152
  // Calculate d mod (p-1).
982
- !BN_mod(rsa->dmp1, rsa->d, pm1, ctx) ||
1153
+ !bn_div_consttime(NULL, rsa->dmp1, rsa->d, pm1, ctx) ||
983
1154
  // Calculate d mod (q-1)
984
- !BN_mod(rsa->dmq1, rsa->d, qm1, ctx)) {
1155
+ !bn_div_consttime(NULL, rsa->dmq1, rsa->d, qm1, ctx)) {
985
1156
  goto bn_err;
986
1157
  }
1158
+ bn_set_minimal_width(rsa->n);
987
1159
 
988
1160
  // Sanity-check that |rsa->n| has the specified size. This is implied by
989
1161
  // |generate_prime|'s bounds.
@@ -992,13 +1164,9 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
992
1164
  goto err;
993
1165
  }
994
1166
 
995
- // Calculate inverse of q mod p. Note that although RSA key generation is far
996
- // from constant-time, |bn_mod_inverse_secret_prime| uses the same modular
997
- // exponentation logic as in RSA private key operations and, if the RSAZ-1024
998
- // code is enabled, will be optimized for common RSA prime sizes.
999
- if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
1000
- !bn_mod_inverse_secret_prime(rsa->iqmp, rsa->q, rsa->p, ctx,
1001
- rsa->mont_p)) {
1167
+ // Call |freeze_private_key| to compute the inverse of q mod p, by way of
1168
+ // |rsa->mont_p|.
1169
+ if (!freeze_private_key(rsa, ctx)) {
1002
1170
  goto bn_err;
1003
1171
  }
1004
1172
 
@@ -1047,5 +1215,4 @@ DEFINE_METHOD_FUNCTION(RSA_METHOD, RSA_default_method) {
1047
1215
  // |rsa_default_*| implementation.
1048
1216
  OPENSSL_memset(out, 0, sizeof(RSA_METHOD));
1049
1217
  out->common.is_static = 1;
1050
- out->flags = RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1051
1218
  }