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
@@ -77,7 +77,7 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
77
77
  }
78
78
 
79
79
  if (len == 0) {
80
- ret->top = 0;
80
+ ret->width = 0;
81
81
  return ret;
82
82
  }
83
83
 
@@ -93,7 +93,7 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
93
93
  // |bn_wexpand| must check bounds on |num_words| to write it into
94
94
  // |ret->dmax|.
95
95
  assert(num_words <= INT_MAX);
96
- ret->top = (int)num_words;
96
+ ret->width = (int)num_words;
97
97
  ret->neg = 0;
98
98
 
99
99
  while (len--) {
@@ -105,9 +105,6 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
105
105
  }
106
106
  }
107
107
 
108
- // need to call this due to clear byte at top if avoiding having the top bit
109
- // set (-ve number)
110
- bn_correct_top(ret);
111
108
  return ret;
112
109
  }
113
110
 
@@ -123,7 +120,7 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
123
120
  }
124
121
 
125
122
  if (len == 0) {
126
- ret->top = 0;
123
+ ret->width = 0;
127
124
  ret->neg = 0;
128
125
  return ret;
129
126
  }
@@ -134,7 +131,7 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
134
131
  BN_free(bn);
135
132
  return NULL;
136
133
  }
137
- ret->top = num_words;
134
+ ret->width = num_words;
138
135
 
139
136
  // Make sure the top bytes will be zeroed.
140
137
  ret->d[num_words - 1] = 0;
@@ -142,8 +139,6 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
142
139
  // We only support little-endian platforms, so we can simply memcpy the
143
140
  // internal representation.
144
141
  OPENSSL_memcpy(ret->d, in, len);
145
-
146
- bn_correct_top(ret);
147
142
  return ret;
148
143
  }
149
144
 
@@ -159,88 +154,54 @@ size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
159
154
  return n;
160
155
  }
161
156
 
157
+ static int fits_in_bytes(const uint8_t *bytes, size_t num_bytes, size_t len) {
158
+ uint8_t mask = 0;
159
+ for (size_t i = len; i < num_bytes; i++) {
160
+ mask |= bytes[i];
161
+ }
162
+ return mask == 0;
163
+ }
164
+
162
165
  int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
163
- // If we don't have enough space, fail out.
164
- size_t num_bytes = BN_num_bytes(in);
166
+ const uint8_t *bytes = (const uint8_t *)in->d;
167
+ size_t num_bytes = in->width * BN_BYTES;
165
168
  if (len < num_bytes) {
166
- return 0;
169
+ if (!fits_in_bytes(bytes, num_bytes, len)) {
170
+ return 0;
171
+ }
172
+ num_bytes = len;
167
173
  }
168
174
 
169
175
  // We only support little-endian platforms, so we can simply memcpy into the
170
176
  // internal representation.
171
- OPENSSL_memcpy(out, in->d, num_bytes);
172
-
177
+ OPENSSL_memcpy(out, bytes, num_bytes);
173
178
  // Pad out the rest of the buffer with zeroes.
174
179
  OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
175
-
176
180
  return 1;
177
181
  }
178
182
 
179
- // constant_time_select_ulong returns |x| if |v| is 1 and |y| if |v| is 0. Its
180
- // behavior is undefined if |v| takes any other value.
181
- static BN_ULONG constant_time_select_ulong(int v, BN_ULONG x, BN_ULONG y) {
182
- BN_ULONG mask = v;
183
- mask--;
184
-
185
- return (~mask & x) | (mask & y);
186
- }
187
-
188
- // constant_time_le_size_t returns 1 if |x| <= |y| and 0 otherwise. |x| and |y|
189
- // must not have their MSBs set.
190
- static int constant_time_le_size_t(size_t x, size_t y) {
191
- return ((x - y - 1) >> (sizeof(size_t) * 8 - 1)) & 1;
192
- }
193
-
194
- // read_word_padded returns the |i|'th word of |in|, if it is not out of
195
- // bounds. Otherwise, it returns 0. It does so without branches on the size of
196
- // |in|, however it necessarily does not have the same memory access pattern. If
197
- // the access would be out of bounds, it reads the last word of |in|. |in| must
198
- // not be zero.
199
- static BN_ULONG read_word_padded(const BIGNUM *in, size_t i) {
200
- // Read |in->d[i]| if valid. Otherwise, read the last word.
201
- BN_ULONG l = in->d[constant_time_select_ulong(
202
- constant_time_le_size_t(in->dmax, i), in->dmax - 1, i)];
203
-
204
- // Clamp to zero if above |d->top|.
205
- return constant_time_select_ulong(constant_time_le_size_t(in->top, i), 0, l);
206
- }
207
-
208
183
  int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
209
- // Special case for |in| = 0. Just branch as the probability is negligible.
210
- if (BN_is_zero(in)) {
211
- OPENSSL_memset(out, 0, len);
212
- return 1;
213
- }
214
-
215
- // Check if the integer is too big. This case can exit early in non-constant
216
- // time.
217
- if ((size_t)in->top > (len + (BN_BYTES - 1)) / BN_BYTES) {
218
- return 0;
219
- }
220
- if ((len % BN_BYTES) != 0) {
221
- BN_ULONG l = read_word_padded(in, len / BN_BYTES);
222
- if (l >> (8 * (len % BN_BYTES)) != 0) {
184
+ const uint8_t *bytes = (const uint8_t *)in->d;
185
+ size_t num_bytes = in->width * BN_BYTES;
186
+ if (len < num_bytes) {
187
+ if (!fits_in_bytes(bytes, num_bytes, len)) {
223
188
  return 0;
224
189
  }
190
+ num_bytes = len;
225
191
  }
226
192
 
227
- // Write the bytes out one by one. Serialization is done without branching on
228
- // the bits of |in| or on |in->top|, but if the routine would otherwise read
229
- // out of bounds, the memory access pattern can't be fixed. However, for an
230
- // RSA key of size a multiple of the word size, the probability of BN_BYTES
231
- // leading zero octets is low.
232
- //
233
- // See Falko Stenzke, "Manger's Attack revisited", ICICS 2010.
234
- size_t i = len;
235
- while (i--) {
236
- BN_ULONG l = read_word_padded(in, i / BN_BYTES);
237
- *(out++) = (uint8_t)(l >> (8 * (i % BN_BYTES))) & 0xff;
193
+ // We only support little-endian platforms, so we can simply write the buffer
194
+ // in reverse.
195
+ for (size_t i = 0; i < num_bytes; i++) {
196
+ out[len - i - 1] = bytes[i];
238
197
  }
198
+ // Pad out the rest of the buffer with zeroes.
199
+ OPENSSL_memset(out, 0, len - num_bytes);
239
200
  return 1;
240
201
  }
241
202
 
242
203
  BN_ULONG BN_get_word(const BIGNUM *bn) {
243
- switch (bn->top) {
204
+ switch (bn_minimal_width(bn)) {
244
205
  case 0:
245
206
  return 0;
246
207
  case 1:
@@ -251,7 +212,7 @@ BN_ULONG BN_get_word(const BIGNUM *bn) {
251
212
  }
252
213
 
253
214
  int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
254
- switch (bn->top) {
215
+ switch (bn_minimal_width(bn)) {
255
216
  case 0:
256
217
  *out = 0;
257
218
  return 1;
@@ -63,33 +63,43 @@
63
63
  #include "../../internal.h"
64
64
 
65
65
 
66
- int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
67
- int i;
68
- BN_ULONG t1, t2, *ap, *bp;
69
-
70
- i = a->top - b->top;
71
- if (i != 0) {
72
- return i;
66
+ static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len,
67
+ const BN_ULONG *b, size_t b_len) {
68
+ OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
69
+ crypto_word_t_too_small);
70
+ int ret = 0;
71
+ // Process the common words in little-endian order.
72
+ size_t min = a_len < b_len ? a_len : b_len;
73
+ for (size_t i = 0; i < min; i++) {
74
+ crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
75
+ crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
76
+ ret =
77
+ constant_time_select_int(eq, ret, constant_time_select_int(lt, -1, 1));
73
78
  }
74
79
 
75
- ap = a->d;
76
- bp = b->d;
77
- for (i = a->top - 1; i >= 0; i--) {
78
- t1 = ap[i];
79
- t2 = bp[i];
80
- if (t1 != t2) {
81
- return (t1 > t2) ? 1 : -1;
80
+ // If |a| or |b| has non-zero words beyond |min|, they take precedence.
81
+ if (a_len < b_len) {
82
+ crypto_word_t mask = 0;
83
+ for (size_t i = a_len; i < b_len; i++) {
84
+ mask |= b[i];
85
+ }
86
+ ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, -1);
87
+ } else if (b_len < a_len) {
88
+ crypto_word_t mask = 0;
89
+ for (size_t i = b_len; i < a_len; i++) {
90
+ mask |= a[i];
82
91
  }
92
+ ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, 1);
83
93
  }
84
94
 
85
- return 0;
95
+ return ret;
86
96
  }
87
97
 
88
- int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
89
- int i;
90
- int gt, lt;
91
- BN_ULONG t1, t2;
98
+ int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
99
+ return bn_cmp_words_consttime(a->d, a->width, b->d, b->width);
100
+ }
92
101
 
102
+ int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
93
103
  if ((a == NULL) || (b == NULL)) {
94
104
  if (a != NULL) {
95
105
  return -1;
@@ -100,104 +110,32 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
100
110
  }
101
111
  }
102
112
 
113
+ // We do not attempt to process the sign bit in constant time. Negative
114
+ // |BIGNUM|s should never occur in crypto, only calculators.
103
115
  if (a->neg != b->neg) {
104
116
  if (a->neg) {
105
117
  return -1;
106
118
  }
107
119
  return 1;
108
120
  }
109
- if (a->neg == 0) {
110
- gt = 1;
111
- lt = -1;
112
- } else {
113
- gt = -1;
114
- lt = 1;
115
- }
116
-
117
- if (a->top > b->top) {
118
- return gt;
119
- }
120
- if (a->top < b->top) {
121
- return lt;
122
- }
123
-
124
- for (i = a->top - 1; i >= 0; i--) {
125
- t1 = a->d[i];
126
- t2 = b->d[i];
127
- if (t1 > t2) {
128
- return gt;
129
- } if (t1 < t2) {
130
- return lt;
131
- }
132
- }
133
-
134
- return 0;
135
- }
136
-
137
- int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
138
- int i;
139
- BN_ULONG aa, bb;
140
-
141
- aa = a[n - 1];
142
- bb = b[n - 1];
143
- if (aa != bb) {
144
- return (aa > bb) ? 1 : -1;
145
- }
146
-
147
- for (i = n - 2; i >= 0; i--) {
148
- aa = a[i];
149
- bb = b[i];
150
- if (aa != bb) {
151
- return (aa > bb) ? 1 : -1;
152
- }
153
- }
154
- return 0;
155
- }
156
-
157
- int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
158
- int n, i;
159
- n = cl - 1;
160
-
161
- if (dl < 0) {
162
- for (i = dl; i < 0; i++) {
163
- if (b[n - i] != 0) {
164
- return -1; // a < b
165
- }
166
- }
167
- }
168
- if (dl > 0) {
169
- for (i = dl; i > 0; i--) {
170
- if (a[n + i] != 0) {
171
- return 1; // a > b
172
- }
173
- }
174
- }
175
121
 
176
- return bn_cmp_words(a, b, cl);
122
+ int ret = BN_ucmp(a, b);
123
+ return a->neg ? -ret : ret;
177
124
  }
178
125
 
179
126
  int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
180
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
181
- crypto_word_t_too_small);
182
- int ret = 0;
183
- // Process the words in little-endian order.
184
- for (size_t i = 0; i < len; i++) {
185
- crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
186
- crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
187
- ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, 1, 0));
188
- }
189
- return ret;
127
+ return bn_cmp_words_consttime(a, len, b, len) < 0;
190
128
  }
191
129
 
192
130
  int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
193
- switch (bn->top) {
194
- case 1:
195
- return bn->d[0] == w;
196
- case 0:
197
- return w == 0;
198
- default:
199
- return 0;
131
+ if (bn->width == 0) {
132
+ return w == 0;
133
+ }
134
+ BN_ULONG mask = bn->d[0] ^ w;
135
+ for (int i = 1; i < bn->width; i++) {
136
+ mask |= bn->d[i];
200
137
  }
138
+ return mask == 0;
201
139
  }
202
140
 
203
141
  int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
@@ -205,14 +143,14 @@ int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
205
143
  BN_init(&b_bn);
206
144
 
207
145
  b_bn.d = &b;
208
- b_bn.top = b > 0;
146
+ b_bn.width = b > 0;
209
147
  b_bn.dmax = 1;
210
148
  b_bn.flags = BN_FLG_STATIC_DATA;
211
149
  return BN_cmp(a, &b_bn);
212
150
  }
213
151
 
214
152
  int BN_is_zero(const BIGNUM *bn) {
215
- return bn->top == 0;
153
+ return bn_fits_in_words(bn, 0);
216
154
  }
217
155
 
218
156
  int BN_is_one(const BIGNUM *bn) {
@@ -224,31 +162,39 @@ int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
224
162
  }
225
163
 
226
164
  int BN_is_odd(const BIGNUM *bn) {
227
- return bn->top > 0 && (bn->d[0] & 1) == 1;
165
+ return bn->width > 0 && (bn->d[0] & 1) == 1;
228
166
  }
229
167
 
230
168
  int BN_is_pow2(const BIGNUM *bn) {
231
- if (bn->top == 0 || bn->neg) {
169
+ int width = bn_minimal_width(bn);
170
+ if (width == 0 || bn->neg) {
232
171
  return 0;
233
172
  }
234
173
 
235
- for (int i = 0; i < bn->top - 1; i++) {
174
+ for (int i = 0; i < width - 1; i++) {
236
175
  if (bn->d[i] != 0) {
237
176
  return 0;
238
177
  }
239
178
  }
240
179
 
241
- return 0 == (bn->d[bn->top-1] & (bn->d[bn->top-1] - 1));
180
+ return 0 == (bn->d[width-1] & (bn->d[width-1] - 1));
242
181
  }
243
182
 
244
183
  int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
245
- if (a->top != b->top) {
246
- return 0;
247
- }
248
-
249
- int limbs_are_equal =
250
- CRYPTO_memcmp(a->d, b->d, (size_t)a->top * sizeof(a->d[0])) == 0;
251
-
252
- return constant_time_select_int(constant_time_eq_int(a->neg, b->neg),
253
- limbs_are_equal, 0);
184
+ BN_ULONG mask = 0;
185
+ // If |a| or |b| has more words than the other, all those words must be zero.
186
+ for (int i = a->width; i < b->width; i++) {
187
+ mask |= b->d[i];
188
+ }
189
+ for (int i = b->width; i < a->width; i++) {
190
+ mask |= a->d[i];
191
+ }
192
+ // Common words must match.
193
+ int min = a->width < b->width ? a->width : b->width;
194
+ for (int i = 0; i < min; i++) {
195
+ mask |= (a->d[i] ^ b->d[i]);
196
+ }
197
+ // The sign bit must match.
198
+ mask |= (a->neg ^ b->neg);
199
+ return mask == 0;
254
200
  }
@@ -155,18 +155,18 @@ static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
155
155
  //
156
156
  // These issues aren't specific to x86 and x86_64, so it might be worthwhile
157
157
  // to add more assembly language implementations.
158
- #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__GNUC__)
159
- __asm__ volatile (
160
- "divl %4"
161
- : "=a"(*quotient_out), "=d"(*rem_out)
162
- : "a"(n1), "d"(n0), "rm"(d0)
163
- : "cc" );
164
- #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__GNUC__)
165
- __asm__ volatile (
166
- "divq %4"
167
- : "=a"(*quotient_out), "=d"(*rem_out)
168
- : "a"(n1), "d"(n0), "rm"(d0)
169
- : "cc" );
158
+ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && \
159
+ (defined(__GNUC__) || defined(__clang__))
160
+ __asm__ volatile("divl %4"
161
+ : "=a"(*quotient_out), "=d"(*rem_out)
162
+ : "a"(n1), "d"(n0), "rm"(d0)
163
+ : "cc");
164
+ #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
165
+ (defined(__GNUC__) || defined(__clang__))
166
+ __asm__ volatile("divq %4"
167
+ : "=a"(*quotient_out), "=d"(*rem_out)
168
+ : "a"(n1), "d"(n0), "rm"(d0)
169
+ : "cc");
170
170
  #else
171
171
  #if defined(BN_ULLONG)
172
172
  BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
@@ -202,10 +202,16 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
202
202
  BN_ULONG d0, d1;
203
203
  int num_n, div_n;
204
204
 
205
- // Invalid zero-padding would have particularly bad consequences
206
- // so don't just rely on bn_check_top() here
207
- if ((numerator->top > 0 && numerator->d[numerator->top - 1] == 0) ||
208
- (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
205
+ // This function relies on the historical minimal-width |BIGNUM| invariant.
206
+ // It is already not constant-time (constant-time reductions should use
207
+ // Montgomery logic), so we shrink all inputs and intermediate values to
208
+ // retain the previous behavior.
209
+
210
+ // Invalid zero-padding would have particularly bad consequences.
211
+ int numerator_width = bn_minimal_width(numerator);
212
+ int divisor_width = bn_minimal_width(divisor);
213
+ if ((numerator_width > 0 && numerator->d[numerator_width - 1] == 0) ||
214
+ (divisor_width > 0 && divisor->d[divisor_width - 1] == 0)) {
209
215
  OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
210
216
  return 0;
211
217
  }
@@ -234,46 +240,48 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
234
240
  if (!BN_lshift(sdiv, divisor, norm_shift)) {
235
241
  goto err;
236
242
  }
243
+ bn_set_minimal_width(sdiv);
237
244
  sdiv->neg = 0;
238
245
  norm_shift += BN_BITS2;
239
246
  if (!BN_lshift(snum, numerator, norm_shift)) {
240
247
  goto err;
241
248
  }
249
+ bn_set_minimal_width(snum);
242
250
  snum->neg = 0;
243
251
 
244
252
  // Since we don't want to have special-case logic for the case where snum is
245
253
  // larger than sdiv, we pad snum with enough zeroes without changing its
246
254
  // value.
247
- if (snum->top <= sdiv->top + 1) {
248
- if (!bn_wexpand(snum, sdiv->top + 2)) {
255
+ if (snum->width <= sdiv->width + 1) {
256
+ if (!bn_wexpand(snum, sdiv->width + 2)) {
249
257
  goto err;
250
258
  }
251
- for (int i = snum->top; i < sdiv->top + 2; i++) {
259
+ for (int i = snum->width; i < sdiv->width + 2; i++) {
252
260
  snum->d[i] = 0;
253
261
  }
254
- snum->top = sdiv->top + 2;
262
+ snum->width = sdiv->width + 2;
255
263
  } else {
256
- if (!bn_wexpand(snum, snum->top + 1)) {
264
+ if (!bn_wexpand(snum, snum->width + 1)) {
257
265
  goto err;
258
266
  }
259
- snum->d[snum->top] = 0;
260
- snum->top++;
267
+ snum->d[snum->width] = 0;
268
+ snum->width++;
261
269
  }
262
270
 
263
- div_n = sdiv->top;
264
- num_n = snum->top;
271
+ div_n = sdiv->width;
272
+ num_n = snum->width;
265
273
  loop = num_n - div_n;
266
274
  // Lets setup a 'window' into snum
267
275
  // This is the part that corresponds to the current
268
276
  // 'area' being divided
269
277
  wnum.neg = 0;
270
278
  wnum.d = &(snum->d[loop]);
271
- wnum.top = div_n;
272
- // only needed when BN_ucmp messes up the values between top and max
279
+ wnum.width = div_n;
280
+ // only needed when BN_ucmp messes up the values between width and max
273
281
  wnum.dmax = snum->dmax - loop; // so we don't step out of bounds
274
282
 
275
283
  // Get the top 2 words of sdiv
276
- // div_n=sdiv->top;
284
+ // div_n=sdiv->width;
277
285
  d0 = sdiv->d[div_n - 1];
278
286
  d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
279
287
 
@@ -285,7 +293,7 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
285
293
  if (!bn_wexpand(res, loop + 1)) {
286
294
  goto err;
287
295
  }
288
- res->top = loop - 1;
296
+ res->width = loop - 1;
289
297
  resp = &(res->d[loop - 1]);
290
298
 
291
299
  // space for temp
@@ -293,9 +301,9 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
293
301
  goto err;
294
302
  }
295
303
 
296
- // if res->top == 0 then clear the neg value otherwise decrease
304
+ // if res->width == 0 then clear the neg value otherwise decrease
297
305
  // the resp pointer
298
- if (res->top == 0) {
306
+ if (res->width == 0) {
299
307
  res->neg = 0;
300
308
  } else {
301
309
  resp--;
@@ -371,7 +379,7 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
371
379
  *resp = q;
372
380
  }
373
381
 
374
- bn_correct_top(snum);
382
+ bn_set_minimal_width(snum);
375
383
 
376
384
  if (rem != NULL) {
377
385
  // Keep a copy of the neg flag in numerator because if |rem| == |numerator|
@@ -385,7 +393,7 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
385
393
  }
386
394
  }
387
395
 
388
- bn_correct_top(res);
396
+ bn_set_minimal_width(res);
389
397
  BN_CTX_end(ctx);
390
398
  return 1;
391
399
 
@@ -406,6 +414,164 @@ int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
406
414
  return (d->neg ? BN_sub : BN_add)(r, r, d);
407
415
  }
408
416
 
417
+ // bn_mod_sub_words sets |r| to |a| - |b| (mod |m|), using |tmp| as scratch
418
+ // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
419
+ // |r|, |a|, and |b| may alias.
420
+ static void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
421
+ const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
422
+ // r = a - b
423
+ BN_ULONG borrow = bn_sub_words(r, a, b, num);
424
+ // tmp = a - b + m
425
+ bn_add_words(tmp, r, m, num);
426
+ bn_select_words(r, 0 - borrow, tmp /* r < 0 */, r /* r >= 0 */, num);
427
+ }
428
+
429
+ // bn_mod_add_words sets |r| to |a| + |b| (mod |m|), using |tmp| as scratch
430
+ // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
431
+ // |r|, |a|, and |b| may alias.
432
+ static void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
433
+ const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
434
+ // tmp = a + b. Note the result fits in |num|+1 words. We store the extra word
435
+ // in |carry|.
436
+ BN_ULONG carry = bn_add_words(tmp, a, b, num);
437
+ // r = a + b - m. We use |bn_sub_words| to perform the bulk of the
438
+ // subtraction, and then apply the borrow to |carry|.
439
+ carry -= bn_sub_words(r, tmp, m, num);
440
+ // |a| and |b| were both fully-reduced, so we know:
441
+ //
442
+ // 0 + 0 - m <= r < m + m - m
443
+ // -m <= r < m
444
+ //
445
+ // If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then
446
+ // wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to
447
+ // return |r| + |m|, or |tmp|. |carry| must then be -1 or all ones. In both
448
+ // cases, |carry| is a suitable input to |bn_select_words|.
449
+ //
450
+ // Although |carry| may be one if |bn_add_words| returns one and
451
+ // |bn_sub_words| returns zero, this would give |r| > |m|, which violates are
452
+ // input assumptions.
453
+ assert(carry == 0 || carry == (BN_ULONG)-1);
454
+ bn_select_words(r, carry, tmp /* r < 0 */, r /* r >= 0 */, num);
455
+ }
456
+
457
+ int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
458
+ const BIGNUM *numerator, const BIGNUM *divisor,
459
+ BN_CTX *ctx) {
460
+ if (BN_is_negative(numerator) || BN_is_negative(divisor)) {
461
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
462
+ return 0;
463
+ }
464
+ if (BN_is_zero(divisor)) {
465
+ OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
466
+ return 0;
467
+ }
468
+
469
+ // This function implements long division in binary. It is not very efficient,
470
+ // but it is simple, easy to make constant-time, and performant enough for RSA
471
+ // key generation.
472
+
473
+ int ret = 0;
474
+ BN_CTX_start(ctx);
475
+ BIGNUM *q = quotient, *r = remainder;
476
+ if (quotient == NULL || quotient == numerator || quotient == divisor) {
477
+ q = BN_CTX_get(ctx);
478
+ }
479
+ if (remainder == NULL || remainder == numerator || remainder == divisor) {
480
+ r = BN_CTX_get(ctx);
481
+ }
482
+ BIGNUM *tmp = BN_CTX_get(ctx);
483
+ if (q == NULL || r == NULL || tmp == NULL ||
484
+ !bn_wexpand(q, numerator->width) ||
485
+ !bn_wexpand(r, divisor->width) ||
486
+ !bn_wexpand(tmp, divisor->width)) {
487
+ goto err;
488
+ }
489
+
490
+ OPENSSL_memset(q->d, 0, numerator->width * sizeof(BN_ULONG));
491
+ q->width = numerator->width;
492
+ q->neg = 0;
493
+
494
+ OPENSSL_memset(r->d, 0, divisor->width * sizeof(BN_ULONG));
495
+ r->width = divisor->width;
496
+ r->neg = 0;
497
+
498
+ // Incorporate |numerator| into |r|, one bit at a time, reducing after each
499
+ // step. At the start of each loop iteration, |r| < |divisor|
500
+ for (int i = numerator->width - 1; i >= 0; i--) {
501
+ for (int bit = BN_BITS2 - 1; bit >= 0; bit--) {
502
+ // Incorporate the next bit of the numerator, by computing
503
+ // r = 2*r or 2*r + 1. Note the result fits in one more word. We store the
504
+ // extra word in |carry|.
505
+ BN_ULONG carry = bn_add_words(r->d, r->d, r->d, divisor->width);
506
+ r->d[0] |= (numerator->d[i] >> bit) & 1;
507
+ // tmp = r - divisor. We use |bn_sub_words| to perform the bulk of the
508
+ // subtraction, and then apply the borrow to |carry|.
509
+ carry -= bn_sub_words(tmp->d, r->d, divisor->d, divisor->width);
510
+ // |r| was previously fully-reduced, so we know:
511
+ //
512
+ // 2*0 - divisor <= tmp <= 2*(divisor-1) + 1 - divisor
513
+ // -divisor <= tmp < divisor
514
+ //
515
+ // If 0 <= |tmp| < |divisor|, |tmp| fits in |divisor->width| and |carry|
516
+ // is zero. We then wish to select |tmp|. Otherwise,
517
+ // -|divisor| <= |tmp| < 0 and we wish to select |tmp| + |divisor|, which
518
+ // is |r|. |carry| must then be -1 (all ones). In both cases, |carry| is a
519
+ // suitable input to |bn_select_words|.
520
+ //
521
+ // Although |carry| may be one if |bn_add_words| returns one and
522
+ // |bn_sub_words| returns zero, this would give |r| > |d|, which violates
523
+ // the loop invariant.
524
+ bn_select_words(r->d, carry, r->d /* tmp < 0 */, tmp->d /* tmp >= 0 */,
525
+ divisor->width);
526
+ // The corresponding bit of the quotient is set iff we needed to subtract.
527
+ q->d[i] |= (~carry & 1) << bit;
528
+ }
529
+ }
530
+
531
+ if ((quotient != NULL && !BN_copy(quotient, q)) ||
532
+ (remainder != NULL && !BN_copy(remainder, r))) {
533
+ goto err;
534
+ }
535
+
536
+ ret = 1;
537
+
538
+ err:
539
+ BN_CTX_end(ctx);
540
+ return ret;
541
+ }
542
+
543
+ static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) {
544
+ BIGNUM *ret = BN_CTX_get(ctx);
545
+ if (ret == NULL ||
546
+ !bn_wexpand(ret, width)) {
547
+ return NULL;
548
+ }
549
+ ret->neg = 0;
550
+ ret->width = width;
551
+ return ret;
552
+ }
553
+
554
+ // bn_resized_from_ctx returns |bn| with width at least |width| or NULL on
555
+ // error. This is so it may be used with low-level "words" functions. If
556
+ // necessary, it allocates a new |BIGNUM| with a lifetime of the current scope
557
+ // in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in
558
+ // |width| words.
559
+ static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width,
560
+ BN_CTX *ctx) {
561
+ if ((size_t)bn->width >= width) {
562
+ // Any excess words must be zero.
563
+ assert(bn_fits_in_words(bn, width));
564
+ return bn;
565
+ }
566
+ BIGNUM *ret = bn_scratch_space_from_ctx(width, ctx);
567
+ if (ret == NULL ||
568
+ !BN_copy(ret, bn) ||
569
+ !bn_resize_words(ret, width)) {
570
+ return NULL;
571
+ }
572
+ return ret;
573
+ }
574
+
409
575
  int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
410
576
  BN_CTX *ctx) {
411
577
  if (!BN_add(r, a, b)) {
@@ -416,13 +582,27 @@ int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
416
582
 
417
583
  int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
418
584
  const BIGNUM *m) {
419
- if (!BN_uadd(r, a, b)) {
420
- return 0;
421
- }
422
- if (BN_ucmp(r, m) >= 0) {
423
- return BN_usub(r, r, m);
585
+ BN_CTX *ctx = BN_CTX_new();
586
+ int ok = ctx != NULL &&
587
+ bn_mod_add_consttime(r, a, b, m, ctx);
588
+ BN_CTX_free(ctx);
589
+ return ok;
590
+ }
591
+
592
+ int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
593
+ const BIGNUM *m, BN_CTX *ctx) {
594
+ BN_CTX_start(ctx);
595
+ a = bn_resized_from_ctx(a, m->width, ctx);
596
+ b = bn_resized_from_ctx(b, m->width, ctx);
597
+ BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
598
+ int ok = a != NULL && b != NULL && tmp != NULL &&
599
+ bn_wexpand(r, m->width);
600
+ if (ok) {
601
+ bn_mod_add_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
602
+ r->width = m->width;
424
603
  }
425
- return 1;
604
+ BN_CTX_end(ctx);
605
+ return ok;
426
606
  }
427
607
 
428
608
  int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
@@ -433,17 +613,29 @@ int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
433
613
  return BN_nnmod(r, r, m, ctx);
434
614
  }
435
615
 
436
- // BN_mod_sub variant that may be used if both a and b are non-negative
437
- // and less than m
616
+ int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
617
+ const BIGNUM *m, BN_CTX *ctx) {
618
+ BN_CTX_start(ctx);
619
+ a = bn_resized_from_ctx(a, m->width, ctx);
620
+ b = bn_resized_from_ctx(b, m->width, ctx);
621
+ BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
622
+ int ok = a != NULL && b != NULL && tmp != NULL &&
623
+ bn_wexpand(r, m->width);
624
+ if (ok) {
625
+ bn_mod_sub_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
626
+ r->width = m->width;
627
+ }
628
+ BN_CTX_end(ctx);
629
+ return ok;
630
+ }
631
+
438
632
  int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
439
633
  const BIGNUM *m) {
440
- if (!BN_sub(r, a, b)) {
441
- return 0;
442
- }
443
- if (r->neg) {
444
- return BN_add(r, r, m);
445
- }
446
- return 1;
634
+ BN_CTX *ctx = BN_CTX_new();
635
+ int ok = ctx != NULL &&
636
+ bn_mod_sub_consttime(r, a, b, m, ctx);
637
+ BN_CTX_free(ctx);
638
+ return ok;
447
639
  }
448
640
 
449
641
  int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
@@ -504,58 +696,33 @@ int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
504
696
  abs_m->neg = 0;
505
697
  }
506
698
 
507
- ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
699
+ ret = bn_mod_lshift_consttime(r, r, n, (abs_m ? abs_m : m), ctx);
508
700
 
509
701
  BN_free(abs_m);
510
702
  return ret;
511
703
  }
512
704
 
513
- int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
514
- if (r != a) {
515
- if (BN_copy(r, a) == NULL) {
516
- return 0;
517
- }
705
+ int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
706
+ BN_CTX *ctx) {
707
+ if (!BN_copy(r, a)) {
708
+ return 0;
518
709
  }
519
-
520
- while (n > 0) {
521
- int max_shift;
522
-
523
- // 0 < r < m
524
- max_shift = BN_num_bits(m) - BN_num_bits(r);
525
- // max_shift >= 0
526
-
527
- if (max_shift < 0) {
528
- OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
710
+ for (int i = 0; i < n; i++) {
711
+ if (!bn_mod_lshift1_consttime(r, r, m, ctx)) {
529
712
  return 0;
530
713
  }
531
-
532
- if (max_shift > n) {
533
- max_shift = n;
534
- }
535
-
536
- if (max_shift) {
537
- if (!BN_lshift(r, r, max_shift)) {
538
- return 0;
539
- }
540
- n -= max_shift;
541
- } else {
542
- if (!BN_lshift1(r, r)) {
543
- return 0;
544
- }
545
- --n;
546
- }
547
-
548
- // BN_num_bits(r) <= BN_num_bits(m)
549
- if (BN_cmp(r, m) >= 0) {
550
- if (!BN_sub(r, r, m)) {
551
- return 0;
552
- }
553
- }
554
714
  }
555
-
556
715
  return 1;
557
716
  }
558
717
 
718
+ int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
719
+ BN_CTX *ctx = BN_CTX_new();
720
+ int ok = ctx != NULL &&
721
+ bn_mod_lshift_consttime(r, a, n, m, ctx);
722
+ BN_CTX_free(ctx);
723
+ return ok;
724
+ }
725
+
559
726
  int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
560
727
  if (!BN_lshift1(r, a)) {
561
728
  return 0;
@@ -564,15 +731,17 @@ int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
564
731
  return BN_nnmod(r, r, m, ctx);
565
732
  }
566
733
 
567
- int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
568
- if (!BN_lshift1(r, a)) {
569
- return 0;
570
- }
571
- if (BN_cmp(r, m) >= 0) {
572
- return BN_sub(r, r, m);
573
- }
734
+ int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
735
+ BN_CTX *ctx) {
736
+ return bn_mod_add_consttime(r, a, a, m, ctx);
737
+ }
574
738
 
575
- return 1;
739
+ int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
740
+ BN_CTX *ctx = BN_CTX_new();
741
+ int ok = ctx != NULL &&
742
+ bn_mod_lshift1_consttime(r, a, m, ctx);
743
+ BN_CTX_free(ctx);
744
+ return ok;
576
745
  }
577
746
 
578
747
  BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
@@ -584,7 +753,7 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
584
753
  return (BN_ULONG) - 1;
585
754
  }
586
755
 
587
- if (a->top == 0) {
756
+ if (a->width == 0) {
588
757
  return 0;
589
758
  }
590
759
 
@@ -595,7 +764,7 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
595
764
  return (BN_ULONG) - 1;
596
765
  }
597
766
 
598
- for (i = a->top - 1; i >= 0; i--) {
767
+ for (i = a->width - 1; i >= 0; i--) {
599
768
  BN_ULONG l = a->d[i];
600
769
  BN_ULONG d;
601
770
  BN_ULONG unused_rem;
@@ -604,20 +773,13 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
604
773
  a->d[i] = d;
605
774
  }
606
775
 
607
- if ((a->top > 0) && (a->d[a->top - 1] == 0)) {
608
- a->top--;
609
- }
610
-
611
- if (a->top == 0) {
612
- a->neg = 0;
613
- }
614
-
776
+ bn_set_minimal_width(a);
615
777
  ret >>= j;
616
778
  return ret;
617
779
  }
618
780
 
619
781
  BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
620
- #ifndef BN_ULLONG
782
+ #ifndef BN_CAN_DIVIDE_ULLONG
621
783
  BN_ULONG ret = 0;
622
784
  #else
623
785
  BN_ULLONG ret = 0;
@@ -628,9 +790,9 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
628
790
  return (BN_ULONG) -1;
629
791
  }
630
792
 
631
- #ifndef BN_ULLONG
632
- // If |w| is too long and we don't have |BN_ULLONG| then we need to fall back
633
- // to using |BN_div_word|.
793
+ #ifndef BN_CAN_DIVIDE_ULLONG
794
+ // If |w| is too long and we don't have |BN_ULLONG| division then we need to
795
+ // fall back to using |BN_div_word|.
634
796
  if (w > ((BN_ULONG)1 << BN_BITS4)) {
635
797
  BIGNUM *tmp = BN_dup(a);
636
798
  if (tmp == NULL) {
@@ -642,8 +804,8 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
642
804
  }
643
805
  #endif
644
806
 
645
- for (i = a->top - 1; i >= 0; i--) {
646
- #ifndef BN_ULLONG
807
+ for (i = a->width - 1; i >= 0; i--) {
808
+ #ifndef BN_CAN_DIVIDE_ULLONG
647
809
  ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
648
810
  ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
649
811
  #else
@@ -654,7 +816,7 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
654
816
  }
655
817
 
656
818
  int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
657
- if (e == 0 || a->top == 0) {
819
+ if (e == 0 || a->width == 0) {
658
820
  BN_zero(r);
659
821
  return 1;
660
822
  }
@@ -662,7 +824,7 @@ int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
662
824
  size_t num_words = 1 + ((e - 1) / BN_BITS2);
663
825
 
664
826
  // If |a| definitely has less than |e| bits, just BN_copy.
665
- if ((size_t) a->top < num_words) {
827
+ if ((size_t) a->width < num_words) {
666
828
  return BN_copy(r, a) != NULL;
667
829
  }
668
830
 
@@ -683,8 +845,8 @@ int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
683
845
 
684
846
  // Fill in the remaining fields of |r|.
685
847
  r->neg = a->neg;
686
- r->top = (int) num_words;
687
- bn_correct_top(r);
848
+ r->width = (int) num_words;
849
+ bn_set_minimal_width(r);
688
850
  return 1;
689
851
  }
690
852
 
@@ -706,27 +868,27 @@ int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
706
868
  }
707
869
 
708
870
  // Clear the upper words of |r|.
709
- OPENSSL_memset(&r->d[r->top], 0, (num_words - r->top) * BN_BYTES);
871
+ OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES);
710
872
 
711
873
  // Set parameters of |r|.
712
874
  r->neg = 0;
713
- r->top = (int) num_words;
875
+ r->width = (int) num_words;
714
876
 
715
877
  // Now, invert every word. The idea here is that we want to compute 2^e-|x|,
716
878
  // which is actually equivalent to the twos-complement representation of |x|
717
879
  // in |e| bits, which is -x = ~x + 1.
718
- for (int i = 0; i < r->top; i++) {
880
+ for (int i = 0; i < r->width; i++) {
719
881
  r->d[i] = ~r->d[i];
720
882
  }
721
883
 
722
884
  // If our exponent doesn't span the top word, we have to mask the rest.
723
885
  size_t top_word_exponent = e % BN_BITS2;
724
886
  if (top_word_exponent != 0) {
725
- r->d[r->top - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
887
+ r->d[r->width - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
726
888
  }
727
889
 
728
- // Keep the correct_top invariant for BN_add.
729
- bn_correct_top(r);
890
+ // Keep the minimal-width invariant for |BIGNUM|.
891
+ bn_set_minimal_width(r);
730
892
 
731
893
  // Finally, add one, for the reason described above.
732
894
  return BN_add(r, r, BN_value_one());