grpc 1.24.0 → 1.25.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (505) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +306 -243
  3. data/etc/roots.pem +0 -100
  4. data/include/grpc/grpc_security.h +44 -18
  5. data/include/grpc/impl/codegen/grpc_types.h +15 -0
  6. data/include/grpc/impl/codegen/port_platform.h +27 -11
  7. data/include/grpc/impl/codegen/sync_generic.h +1 -1
  8. data/src/boringssl/err_data.c +695 -650
  9. data/src/core/ext/filters/client_channel/client_channel.cc +257 -179
  10. data/src/core/ext/filters/client_channel/client_channel.h +24 -0
  11. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +2 -3
  12. data/src/core/ext/filters/client_channel/client_channel_factory.h +1 -5
  13. data/src/core/ext/filters/client_channel/health/health_check_client.cc +18 -45
  14. data/src/core/ext/filters/client_channel/health/health_check_client.h +5 -13
  15. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +1 -1
  16. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -3
  17. data/src/core/ext/filters/client_channel/lb_policy.h +65 -55
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +14 -14
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +113 -36
  20. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +14 -19
  21. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +36 -13
  22. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +3 -10
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +814 -1589
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.h +2 -5
  25. data/src/core/ext/filters/client_channel/lb_policy_factory.h +3 -6
  26. data/src/core/ext/filters/client_channel/resolver.cc +1 -2
  27. data/src/core/ext/filters/client_channel/resolver.h +8 -16
  28. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +25 -8
  29. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +46 -12
  30. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +10 -17
  31. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +7 -8
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +4 -4
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +111 -44
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +22 -14
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +1 -1
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +2 -2
  37. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +29 -10
  38. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +27 -36
  39. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +7 -10
  40. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +60 -16
  41. data/src/core/ext/filters/client_channel/resolver_factory.h +4 -8
  42. data/src/core/ext/filters/client_channel/resolver_registry.cc +1 -1
  43. data/src/core/ext/filters/client_channel/resolver_registry.h +1 -1
  44. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +7 -10
  45. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +7 -8
  46. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +1 -1
  47. data/src/core/ext/filters/client_channel/retry_throttle.cc +5 -5
  48. data/src/core/ext/filters/client_channel/retry_throttle.h +1 -4
  49. data/src/core/ext/filters/client_channel/service_config.h +8 -8
  50. data/src/core/ext/filters/client_channel/subchannel.cc +53 -86
  51. data/src/core/ext/filters/client_channel/subchannel.h +7 -9
  52. data/src/core/ext/filters/client_channel/subchannel_interface.h +9 -13
  53. data/src/core/ext/filters/client_channel/subchannel_pool_interface.h +3 -6
  54. data/src/core/ext/filters/client_channel/{lb_policy/xds/xds_load_balancer_api.cc → xds/xds_api.cc} +169 -52
  55. data/src/core/ext/filters/client_channel/xds/xds_api.h +171 -0
  56. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +450 -0
  57. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +99 -0
  58. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_channel.h +8 -6
  59. data/src/core/ext/filters/client_channel/xds/xds_channel_args.h +26 -0
  60. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_channel_secure.cc +28 -11
  61. data/src/core/ext/filters/client_channel/xds/xds_client.cc +1413 -0
  62. data/src/core/ext/filters/client_channel/xds/xds_client.h +221 -0
  63. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_client_stats.cc +1 -5
  64. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_client_stats.h +3 -4
  65. data/src/core/ext/filters/deadline/deadline_filter.cc +20 -20
  66. data/src/core/ext/filters/http/client/http_client_filter.cc +15 -15
  67. data/src/core/ext/filters/http/client_authority_filter.cc +14 -14
  68. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +12 -12
  69. data/src/core/ext/filters/max_age/max_age_filter.cc +59 -50
  70. data/src/core/ext/filters/message_size/message_size_filter.cc +18 -18
  71. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.cc +15 -14
  72. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +233 -175
  73. data/src/core/ext/transport/chttp2/transport/flow_control.h +21 -24
  74. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +253 -163
  75. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +24 -12
  76. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +2 -3
  77. data/src/core/ext/transport/chttp2/transport/internal.h +13 -15
  78. data/src/core/ext/transport/chttp2/transport/writing.cc +3 -0
  79. data/src/core/ext/transport/inproc/inproc_transport.cc +20 -13
  80. data/src/core/lib/channel/channel_args.cc +16 -0
  81. data/src/core/lib/channel/channel_args.h +22 -0
  82. data/src/core/lib/channel/channelz.cc +5 -6
  83. data/src/core/lib/channel/channelz.h +1 -1
  84. data/src/core/lib/channel/connected_channel.cc +20 -20
  85. data/src/core/lib/channel/handshaker.h +3 -4
  86. data/src/core/lib/channel/handshaker_factory.h +1 -3
  87. data/src/core/lib/debug/trace.h +3 -2
  88. data/src/core/lib/gprpp/arena.cc +3 -3
  89. data/src/core/lib/gprpp/arena.h +2 -3
  90. data/src/core/lib/gprpp/inlined_vector.h +9 -0
  91. data/src/core/lib/gprpp/map.h +3 -501
  92. data/src/core/lib/gprpp/memory.h +45 -41
  93. data/src/core/lib/gprpp/mpscq.cc +108 -0
  94. data/src/core/lib/gprpp/mpscq.h +98 -0
  95. data/src/core/lib/gprpp/orphanable.h +6 -11
  96. data/src/core/lib/gprpp/ref_counted.h +25 -19
  97. data/src/core/lib/gprpp/set.h +33 -0
  98. data/src/core/lib/gprpp/thd.h +2 -4
  99. data/src/core/lib/http/httpcli.cc +1 -1
  100. data/src/core/lib/http/httpcli_security_connector.cc +15 -11
  101. data/src/core/lib/http/parser.cc +1 -1
  102. data/src/core/lib/iomgr/buffer_list.cc +4 -5
  103. data/src/core/lib/iomgr/buffer_list.h +5 -6
  104. data/src/core/lib/iomgr/call_combiner.cc +4 -5
  105. data/src/core/lib/iomgr/call_combiner.h +2 -2
  106. data/src/core/lib/iomgr/cfstream_handle.h +3 -5
  107. data/src/core/lib/iomgr/closure.h +8 -3
  108. data/src/core/lib/iomgr/combiner.cc +45 -82
  109. data/src/core/lib/iomgr/combiner.h +32 -8
  110. data/src/core/lib/iomgr/endpoint_cfstream.cc +5 -3
  111. data/src/core/lib/iomgr/ev_epoll1_linux.cc +19 -15
  112. data/src/core/lib/iomgr/ev_poll_posix.cc +3 -1
  113. data/src/core/lib/iomgr/exec_ctx.h +4 -3
  114. data/src/core/lib/iomgr/executor.cc +4 -2
  115. data/src/core/lib/iomgr/executor.h +3 -0
  116. data/src/core/lib/iomgr/executor/mpmcqueue.h +3 -6
  117. data/src/core/lib/iomgr/executor/threadpool.cc +1 -2
  118. data/src/core/lib/iomgr/executor/threadpool.h +7 -11
  119. data/src/core/lib/iomgr/resource_quota.cc +55 -51
  120. data/src/core/lib/iomgr/resource_quota.h +13 -9
  121. data/src/core/lib/iomgr/socket_utils_common_posix.cc +13 -0
  122. data/src/core/lib/iomgr/socket_utils_posix.h +4 -0
  123. data/src/core/lib/iomgr/tcp_client_posix.cc +4 -11
  124. data/src/core/lib/iomgr/tcp_custom.cc +9 -7
  125. data/src/core/lib/iomgr/tcp_posix.cc +20 -16
  126. data/src/core/lib/iomgr/tcp_server.h +1 -4
  127. data/src/core/lib/iomgr/tcp_server_custom.cc +5 -5
  128. data/src/core/lib/iomgr/tcp_server_posix.cc +1 -1
  129. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +2 -11
  130. data/src/core/lib/iomgr/timer_custom.cc +2 -2
  131. data/src/core/lib/iomgr/udp_server.cc +3 -2
  132. data/src/core/lib/iomgr/udp_server.h +6 -12
  133. data/src/core/lib/json/json.h +1 -1
  134. data/src/core/lib/json/json_string.cc +2 -2
  135. data/src/core/lib/profiling/basic_timers.cc +2 -2
  136. data/src/core/lib/security/credentials/alts/alts_credentials.cc +2 -2
  137. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_server_options.cc +1 -1
  138. data/src/core/lib/security/credentials/credentials.h +4 -20
  139. data/src/core/lib/security/credentials/fake/fake_credentials.cc +4 -4
  140. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -3
  141. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +64 -0
  142. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +4 -4
  143. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +9 -7
  144. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -0
  145. data/src/core/lib/security/security_connector/local/local_security_connector.cc +4 -4
  146. data/src/core/lib/security/security_connector/security_connector.cc +1 -0
  147. data/src/core/lib/security/security_connector/security_connector.h +19 -17
  148. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +8 -5
  149. data/src/core/lib/security/security_connector/ssl_utils.cc +2 -2
  150. data/src/core/lib/security/security_connector/ssl_utils.h +1 -1
  151. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +14 -6
  152. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +4 -2
  153. data/src/core/lib/security/transport/client_auth_filter.cc +17 -17
  154. data/src/core/lib/security/transport/security_handshaker.cc +29 -13
  155. data/src/core/lib/security/transport/security_handshaker.h +4 -2
  156. data/src/core/lib/security/transport/server_auth_filter.cc +14 -14
  157. data/src/core/lib/slice/slice.cc +2 -10
  158. data/src/core/lib/slice/slice_hash_table.h +4 -6
  159. data/src/core/lib/slice/slice_intern.cc +42 -39
  160. data/src/core/lib/slice/slice_internal.h +3 -3
  161. data/src/core/lib/slice/slice_utils.h +21 -4
  162. data/src/core/lib/slice/slice_weak_hash_table.h +4 -6
  163. data/src/core/lib/surface/call.cc +3 -3
  164. data/src/core/lib/surface/channel.cc +7 -0
  165. data/src/core/lib/surface/completion_queue.cc +12 -11
  166. data/src/core/lib/surface/completion_queue.h +4 -2
  167. data/src/core/lib/surface/init.cc +1 -0
  168. data/src/core/lib/surface/lame_client.cc +33 -18
  169. data/src/core/lib/surface/server.cc +77 -76
  170. data/src/core/lib/surface/version.cc +1 -1
  171. data/src/core/lib/transport/byte_stream.h +3 -7
  172. data/src/core/lib/transport/connectivity_state.cc +112 -98
  173. data/src/core/lib/transport/connectivity_state.h +100 -50
  174. data/src/core/lib/transport/static_metadata.cc +276 -288
  175. data/src/core/lib/transport/static_metadata.h +73 -76
  176. data/src/core/lib/transport/status_conversion.cc +1 -1
  177. data/src/core/lib/transport/status_metadata.cc +1 -1
  178. data/src/core/lib/transport/transport.cc +2 -2
  179. data/src/core/lib/transport/transport.h +12 -4
  180. data/src/core/lib/transport/transport_op_string.cc +14 -11
  181. data/src/core/tsi/alts/frame_protector/alts_unseal_privacy_integrity_crypter.cc +1 -1
  182. data/src/core/tsi/alts/handshaker/alts_shared_resource.cc +1 -1
  183. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +5 -5
  184. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -2
  185. data/src/core/tsi/fake_transport_security.cc +7 -5
  186. data/src/core/tsi/grpc_shadow_boringssl.h +2918 -2627
  187. data/src/core/tsi/local_transport_security.cc +8 -6
  188. data/src/core/tsi/ssl/session_cache/ssl_session.h +1 -3
  189. data/src/core/tsi/ssl/session_cache/ssl_session_boringssl.cc +1 -2
  190. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +7 -5
  191. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +4 -6
  192. data/src/core/tsi/ssl/session_cache/ssl_session_openssl.cc +1 -2
  193. data/src/core/tsi/ssl_transport_security.cc +12 -12
  194. data/src/core/tsi/ssl_transport_security.h +2 -2
  195. data/src/core/tsi/transport_security_grpc.cc +7 -0
  196. data/src/core/tsi/transport_security_grpc.h +6 -0
  197. data/src/ruby/ext/grpc/extconf.rb +1 -0
  198. data/src/ruby/ext/grpc/rb_call.c +1 -1
  199. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  200. data/src/ruby/lib/grpc/generic/bidi_call.rb +1 -1
  201. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  202. data/src/ruby/lib/grpc/version.rb +1 -1
  203. data/src/ruby/spec/google_rpc_status_utils_spec.rb +2 -2
  204. data/third_party/boringssl/crypto/asn1/a_bool.c +18 -5
  205. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +17 -221
  206. data/third_party/boringssl/crypto/asn1/a_dup.c +0 -24
  207. data/third_party/boringssl/crypto/asn1/a_enum.c +2 -2
  208. data/third_party/boringssl/crypto/asn1/a_i2d_fp.c +10 -72
  209. data/third_party/boringssl/crypto/asn1/a_int.c +12 -71
  210. data/third_party/boringssl/crypto/asn1/a_mbstr.c +110 -216
  211. data/third_party/boringssl/crypto/asn1/a_object.c +16 -5
  212. data/third_party/boringssl/crypto/asn1/a_strnid.c +1 -0
  213. data/third_party/boringssl/crypto/asn1/asn1_lib.c +5 -1
  214. data/third_party/boringssl/crypto/asn1/tasn_enc.c +3 -1
  215. data/third_party/boringssl/crypto/base64/base64.c +2 -2
  216. data/third_party/boringssl/crypto/bio/bio.c +73 -9
  217. data/third_party/boringssl/crypto/bio/connect.c +4 -0
  218. data/third_party/boringssl/crypto/bio/fd.c +4 -0
  219. data/third_party/boringssl/crypto/bio/file.c +5 -2
  220. data/third_party/boringssl/crypto/bio/socket.c +4 -0
  221. data/third_party/boringssl/crypto/bio/socket_helper.c +4 -0
  222. data/third_party/boringssl/crypto/bn_extra/convert.c +11 -7
  223. data/third_party/boringssl/crypto/bytestring/ber.c +8 -4
  224. data/third_party/boringssl/crypto/bytestring/cbb.c +19 -7
  225. data/third_party/boringssl/crypto/bytestring/cbs.c +28 -15
  226. data/third_party/boringssl/crypto/bytestring/internal.h +28 -7
  227. data/third_party/boringssl/crypto/bytestring/unicode.c +155 -0
  228. data/third_party/boringssl/crypto/chacha/chacha.c +36 -19
  229. data/third_party/boringssl/crypto/chacha/internal.h +45 -0
  230. data/third_party/boringssl/crypto/cipher_extra/cipher_extra.c +29 -0
  231. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +269 -25
  232. data/third_party/boringssl/crypto/cipher_extra/e_aesctrhmac.c +16 -14
  233. data/third_party/boringssl/crypto/cipher_extra/e_aesgcmsiv.c +54 -38
  234. data/third_party/boringssl/crypto/cipher_extra/e_chacha20poly1305.c +133 -41
  235. data/third_party/boringssl/crypto/cipher_extra/e_tls.c +23 -15
  236. data/third_party/boringssl/crypto/cipher_extra/tls_cbc.c +24 -15
  237. data/third_party/boringssl/crypto/cmac/cmac.c +62 -25
  238. data/third_party/boringssl/crypto/conf/conf.c +7 -0
  239. data/third_party/boringssl/crypto/cpu-arm-linux.c +4 -148
  240. data/third_party/boringssl/crypto/cpu-arm-linux.h +201 -0
  241. data/third_party/boringssl/crypto/cpu-intel.c +45 -51
  242. data/third_party/boringssl/crypto/crypto.c +39 -22
  243. data/third_party/boringssl/crypto/curve25519/spake25519.c +1 -1
  244. data/third_party/boringssl/crypto/dsa/dsa.c +77 -53
  245. data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +20 -8
  246. data/third_party/boringssl/crypto/ec_extra/ec_derive.c +96 -0
  247. data/third_party/boringssl/crypto/{ecdh/ecdh.c → ecdh_extra/ecdh_extra.c} +20 -58
  248. data/third_party/boringssl/crypto/ecdsa_extra/ecdsa_asn1.c +1 -9
  249. data/third_party/boringssl/crypto/engine/engine.c +2 -1
  250. data/third_party/boringssl/crypto/err/err.c +2 -0
  251. data/third_party/boringssl/crypto/err/internal.h +2 -2
  252. data/third_party/boringssl/crypto/evp/evp.c +89 -8
  253. data/third_party/boringssl/crypto/evp/evp_asn1.c +56 -5
  254. data/third_party/boringssl/crypto/evp/evp_ctx.c +52 -14
  255. data/third_party/boringssl/crypto/evp/internal.h +18 -1
  256. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +5 -0
  257. data/third_party/boringssl/crypto/evp/p_ec.c +51 -3
  258. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +6 -7
  259. data/third_party/boringssl/crypto/evp/p_ed25519.c +36 -3
  260. data/third_party/boringssl/crypto/evp/p_ed25519_asn1.c +76 -45
  261. data/third_party/boringssl/crypto/evp/p_rsa.c +3 -1
  262. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +5 -0
  263. data/third_party/boringssl/crypto/evp/p_x25519.c +110 -0
  264. data/third_party/boringssl/crypto/evp/p_x25519_asn1.c +249 -0
  265. data/third_party/boringssl/crypto/evp/scrypt.c +6 -2
  266. data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +34 -274
  267. data/third_party/boringssl/crypto/fipsmodule/aes/internal.h +161 -21
  268. data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +111 -13
  269. data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +17 -21
  270. data/third_party/boringssl/crypto/fipsmodule/bcm.c +119 -7
  271. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +19 -2
  272. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +2 -2
  273. data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +93 -160
  274. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +48 -57
  275. data/third_party/boringssl/crypto/fipsmodule/bn/div_extra.c +87 -0
  276. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +143 -211
  277. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +0 -305
  278. data/third_party/boringssl/crypto/fipsmodule/bn/gcd_extra.c +325 -0
  279. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +168 -50
  280. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +68 -92
  281. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +7 -6
  282. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +11 -14
  283. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +358 -443
  284. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +25 -35
  285. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +20 -25
  286. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.h +76 -5
  287. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +14 -14
  288. data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +7 -2
  289. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +383 -516
  290. data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +4 -0
  291. data/third_party/boringssl/crypto/fipsmodule/cipher/internal.h +3 -4
  292. data/third_party/boringssl/crypto/fipsmodule/delocate.h +3 -2
  293. data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +32 -17
  294. data/third_party/boringssl/crypto/fipsmodule/digest/md32_common.h +3 -3
  295. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +228 -122
  296. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +34 -8
  297. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +311 -98
  298. data/third_party/boringssl/crypto/fipsmodule/ec/felem.c +82 -0
  299. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +263 -97
  300. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +22 -59
  301. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +317 -234
  302. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +9473 -9475
  303. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +313 -109
  304. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.h +36 -0
  305. data/third_party/boringssl/crypto/fipsmodule/ec/scalar.c +96 -0
  306. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +126 -792
  307. data/third_party/boringssl/crypto/fipsmodule/ec/simple_mul.c +84 -0
  308. data/third_party/boringssl/crypto/fipsmodule/ec/util.c +163 -12
  309. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +84 -211
  310. data/third_party/boringssl/crypto/fipsmodule/ecdh/ecdh.c +122 -0
  311. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +60 -205
  312. data/third_party/boringssl/crypto/fipsmodule/fips_shared_support.c +32 -0
  313. data/third_party/boringssl/crypto/fipsmodule/is_fips.c +2 -0
  314. data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +3 -1
  315. data/third_party/boringssl/crypto/fipsmodule/md5/internal.h +37 -0
  316. data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +11 -8
  317. data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +35 -79
  318. data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +7 -39
  319. data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +7 -27
  320. data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +123 -309
  321. data/third_party/boringssl/crypto/fipsmodule/modes/internal.h +189 -126
  322. data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +3 -2
  323. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +2 -2
  324. data/third_party/boringssl/crypto/fipsmodule/rand/internal.h +35 -0
  325. data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +24 -19
  326. data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +256 -77
  327. data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +10 -7
  328. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +5 -1
  329. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +131 -14
  330. data/third_party/boringssl/crypto/fipsmodule/self_check/self_check.c +83 -10
  331. data/third_party/boringssl/crypto/fipsmodule/sha/internal.h +53 -0
  332. data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +9 -13
  333. data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +18 -12
  334. data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +95 -168
  335. data/third_party/boringssl/crypto/hrss/hrss.c +2201 -0
  336. data/third_party/boringssl/crypto/hrss/internal.h +62 -0
  337. data/third_party/boringssl/crypto/internal.h +95 -20
  338. data/third_party/boringssl/crypto/lhash/lhash.c +45 -33
  339. data/third_party/boringssl/crypto/mem.c +39 -2
  340. data/third_party/boringssl/crypto/obj/obj.c +4 -4
  341. data/third_party/boringssl/crypto/obj/obj_dat.h +6181 -875
  342. data/third_party/boringssl/crypto/pem/pem_all.c +2 -3
  343. data/third_party/boringssl/crypto/pem/pem_info.c +144 -162
  344. data/third_party/boringssl/crypto/pem/pem_lib.c +53 -52
  345. data/third_party/boringssl/crypto/pem/pem_pkey.c +13 -21
  346. data/third_party/boringssl/crypto/pkcs7/pkcs7.c +15 -22
  347. data/third_party/boringssl/crypto/pkcs7/pkcs7_x509.c +168 -16
  348. data/third_party/boringssl/crypto/pkcs8/internal.h +11 -0
  349. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +24 -15
  350. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +42 -25
  351. data/third_party/boringssl/crypto/pkcs8/pkcs8_x509.c +559 -43
  352. data/third_party/boringssl/crypto/pool/internal.h +1 -1
  353. data/third_party/boringssl/crypto/pool/pool.c +21 -0
  354. data/third_party/boringssl/crypto/rand_extra/deterministic.c +8 -0
  355. data/third_party/boringssl/crypto/rand_extra/fuchsia.c +1 -14
  356. data/third_party/boringssl/crypto/refcount_lock.c +2 -2
  357. data/third_party/boringssl/crypto/rsa_extra/rsa_print.c +22 -0
  358. data/third_party/boringssl/crypto/siphash/siphash.c +80 -0
  359. data/third_party/boringssl/crypto/stack/stack.c +83 -32
  360. data/third_party/boringssl/crypto/thread_none.c +2 -2
  361. data/third_party/boringssl/crypto/thread_pthread.c +2 -2
  362. data/third_party/boringssl/crypto/thread_win.c +38 -19
  363. data/third_party/boringssl/crypto/x509/a_strex.c +22 -2
  364. data/third_party/boringssl/crypto/x509/asn1_gen.c +2 -1
  365. data/third_party/boringssl/crypto/x509/by_dir.c +7 -0
  366. data/third_party/boringssl/crypto/x509/by_file.c +12 -10
  367. data/third_party/boringssl/crypto/x509/t_crl.c +5 -8
  368. data/third_party/boringssl/crypto/x509/t_req.c +1 -3
  369. data/third_party/boringssl/crypto/x509/t_x509.c +5 -8
  370. data/third_party/boringssl/crypto/x509/x509_cmp.c +1 -1
  371. data/third_party/boringssl/crypto/x509/x509_def.c +1 -1
  372. data/third_party/boringssl/crypto/x509/x509_lu.c +114 -5
  373. data/third_party/boringssl/crypto/x509/x509_req.c +20 -0
  374. data/third_party/boringssl/crypto/x509/x509_set.c +5 -0
  375. data/third_party/boringssl/crypto/x509/x509_trs.c +1 -0
  376. data/third_party/boringssl/crypto/x509/x509_txt.c +4 -5
  377. data/third_party/boringssl/crypto/x509/x509_vfy.c +145 -138
  378. data/third_party/boringssl/crypto/x509/x509_vpm.c +2 -0
  379. data/third_party/boringssl/crypto/x509/x509cset.c +40 -0
  380. data/third_party/boringssl/crypto/x509/x509name.c +2 -3
  381. data/third_party/boringssl/crypto/x509/x_all.c +109 -210
  382. data/third_party/boringssl/crypto/x509/x_x509.c +6 -0
  383. data/third_party/boringssl/crypto/x509v3/ext_dat.h +1 -3
  384. data/third_party/boringssl/crypto/x509v3/internal.h +56 -0
  385. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +2 -0
  386. data/third_party/boringssl/crypto/x509v3/pcy_node.c +1 -0
  387. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +4 -2
  388. data/third_party/boringssl/crypto/x509v3/v3_akey.c +5 -2
  389. data/third_party/boringssl/crypto/x509v3/v3_alt.c +19 -13
  390. data/third_party/boringssl/crypto/x509v3/v3_conf.c +2 -1
  391. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +3 -2
  392. data/third_party/boringssl/crypto/x509v3/v3_genn.c +1 -6
  393. data/third_party/boringssl/crypto/x509v3/v3_lib.c +1 -0
  394. data/third_party/boringssl/crypto/x509v3/v3_ocsp.c +68 -0
  395. data/third_party/boringssl/crypto/x509v3/v3_pci.c +2 -1
  396. data/third_party/boringssl/crypto/x509v3/v3_purp.c +47 -69
  397. data/third_party/boringssl/crypto/x509v3/v3_skey.c +5 -2
  398. data/third_party/boringssl/crypto/x509v3/v3_utl.c +69 -25
  399. data/third_party/boringssl/include/openssl/aead.h +45 -19
  400. data/third_party/boringssl/include/openssl/aes.h +32 -7
  401. data/third_party/boringssl/include/openssl/asn1.h +7 -77
  402. data/third_party/boringssl/include/openssl/base.h +120 -6
  403. data/third_party/boringssl/include/openssl/base64.h +4 -1
  404. data/third_party/boringssl/include/openssl/bio.h +112 -81
  405. data/third_party/boringssl/include/openssl/blowfish.h +3 -3
  406. data/third_party/boringssl/include/openssl/bn.h +55 -29
  407. data/third_party/boringssl/include/openssl/buf.h +2 -2
  408. data/third_party/boringssl/include/openssl/bytestring.h +54 -32
  409. data/third_party/boringssl/include/openssl/cast.h +2 -2
  410. data/third_party/boringssl/include/openssl/cipher.h +46 -16
  411. data/third_party/boringssl/include/openssl/cmac.h +6 -2
  412. data/third_party/boringssl/include/openssl/conf.h +3 -6
  413. data/third_party/boringssl/include/openssl/cpu.h +25 -9
  414. data/third_party/boringssl/include/openssl/crypto.h +32 -10
  415. data/third_party/boringssl/include/openssl/curve25519.h +4 -4
  416. data/third_party/boringssl/include/openssl/dh.h +3 -2
  417. data/third_party/boringssl/include/openssl/digest.h +21 -7
  418. data/third_party/boringssl/include/openssl/dsa.h +8 -2
  419. data/third_party/boringssl/include/openssl/e_os2.h +18 -0
  420. data/third_party/boringssl/include/openssl/ec.h +25 -21
  421. data/third_party/boringssl/include/openssl/ec_key.h +36 -8
  422. data/third_party/boringssl/include/openssl/ecdh.h +17 -0
  423. data/third_party/boringssl/include/openssl/ecdsa.h +3 -3
  424. data/third_party/boringssl/include/openssl/engine.h +4 -4
  425. data/third_party/boringssl/include/openssl/err.h +3 -0
  426. data/third_party/boringssl/include/openssl/evp.h +199 -42
  427. data/third_party/boringssl/include/openssl/hmac.h +4 -4
  428. data/third_party/boringssl/include/openssl/hrss.h +100 -0
  429. data/third_party/boringssl/include/openssl/lhash.h +131 -23
  430. data/third_party/boringssl/include/openssl/md4.h +6 -4
  431. data/third_party/boringssl/include/openssl/md5.h +6 -4
  432. data/third_party/boringssl/include/openssl/mem.h +6 -2
  433. data/third_party/boringssl/include/openssl/nid.h +3 -0
  434. data/third_party/boringssl/include/openssl/obj.h +3 -0
  435. data/third_party/boringssl/include/openssl/pem.h +102 -64
  436. data/third_party/boringssl/include/openssl/pkcs7.h +136 -3
  437. data/third_party/boringssl/include/openssl/pkcs8.h +42 -3
  438. data/third_party/boringssl/include/openssl/pool.h +13 -2
  439. data/third_party/boringssl/include/openssl/ripemd.h +5 -4
  440. data/third_party/boringssl/include/openssl/rsa.h +46 -15
  441. data/third_party/boringssl/include/openssl/sha.h +40 -28
  442. data/third_party/boringssl/include/openssl/siphash.h +37 -0
  443. data/third_party/boringssl/include/openssl/span.h +17 -9
  444. data/third_party/boringssl/include/openssl/ssl.h +766 -393
  445. data/third_party/boringssl/include/openssl/ssl3.h +4 -3
  446. data/third_party/boringssl/include/openssl/stack.h +134 -77
  447. data/third_party/boringssl/include/openssl/thread.h +1 -1
  448. data/third_party/boringssl/include/openssl/tls1.h +25 -9
  449. data/third_party/boringssl/include/openssl/type_check.h +14 -15
  450. data/third_party/boringssl/include/openssl/x509.h +28 -3
  451. data/third_party/boringssl/include/openssl/x509_vfy.h +98 -32
  452. data/third_party/boringssl/include/openssl/x509v3.h +17 -13
  453. data/third_party/boringssl/ssl/d1_both.cc +9 -18
  454. data/third_party/boringssl/ssl/d1_lib.cc +4 -3
  455. data/third_party/boringssl/ssl/d1_pkt.cc +4 -4
  456. data/third_party/boringssl/ssl/d1_srtp.cc +15 -15
  457. data/third_party/boringssl/ssl/dtls_method.cc +0 -1
  458. data/third_party/boringssl/ssl/dtls_record.cc +28 -28
  459. data/third_party/boringssl/ssl/handoff.cc +295 -91
  460. data/third_party/boringssl/ssl/handshake.cc +133 -72
  461. data/third_party/boringssl/ssl/handshake_client.cc +218 -189
  462. data/third_party/boringssl/ssl/handshake_server.cc +399 -272
  463. data/third_party/boringssl/ssl/internal.h +1413 -928
  464. data/third_party/boringssl/ssl/s3_both.cc +175 -36
  465. data/third_party/boringssl/ssl/s3_lib.cc +9 -13
  466. data/third_party/boringssl/ssl/s3_pkt.cc +63 -29
  467. data/third_party/boringssl/ssl/ssl_aead_ctx.cc +55 -35
  468. data/third_party/boringssl/ssl/ssl_asn1.cc +57 -73
  469. data/third_party/boringssl/ssl/ssl_buffer.cc +13 -12
  470. data/third_party/boringssl/ssl/ssl_cert.cc +313 -210
  471. data/third_party/boringssl/ssl/ssl_cipher.cc +159 -221
  472. data/third_party/boringssl/ssl/ssl_file.cc +2 -0
  473. data/third_party/boringssl/ssl/ssl_key_share.cc +164 -19
  474. data/third_party/boringssl/ssl/ssl_lib.cc +847 -555
  475. data/third_party/boringssl/ssl/ssl_privkey.cc +441 -111
  476. data/third_party/boringssl/ssl/ssl_session.cc +230 -178
  477. data/third_party/boringssl/ssl/ssl_transcript.cc +21 -142
  478. data/third_party/boringssl/ssl/ssl_versions.cc +88 -93
  479. data/third_party/boringssl/ssl/ssl_x509.cc +279 -218
  480. data/third_party/boringssl/ssl/t1_enc.cc +5 -96
  481. data/third_party/boringssl/ssl/t1_lib.cc +931 -678
  482. data/third_party/boringssl/ssl/tls13_both.cc +251 -121
  483. data/third_party/boringssl/ssl/tls13_client.cc +129 -73
  484. data/third_party/boringssl/ssl/tls13_enc.cc +350 -282
  485. data/third_party/boringssl/ssl/tls13_server.cc +259 -192
  486. data/third_party/boringssl/ssl/tls_method.cc +26 -21
  487. data/third_party/boringssl/ssl/tls_record.cc +42 -47
  488. data/third_party/boringssl/third_party/fiat/curve25519.c +261 -1324
  489. data/third_party/boringssl/third_party/fiat/curve25519_32.h +911 -0
  490. data/third_party/boringssl/third_party/fiat/curve25519_64.h +559 -0
  491. data/third_party/boringssl/third_party/fiat/p256.c +238 -999
  492. data/third_party/boringssl/third_party/fiat/p256_32.h +3226 -0
  493. data/third_party/boringssl/third_party/fiat/p256_64.h +1217 -0
  494. data/third_party/upb/upb/port_def.inc +1 -1
  495. data/third_party/upb/upb/table.c +2 -1
  496. metadata +72 -44
  497. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +0 -127
  498. data/src/core/lib/gpr/mpscq.cc +0 -117
  499. data/src/core/lib/gpr/mpscq.h +0 -88
  500. data/src/core/lib/gprpp/abstract.h +0 -47
  501. data/src/core/lib/gprpp/pair.h +0 -38
  502. data/third_party/boringssl/crypto/cipher_extra/e_ssl3.c +0 -460
  503. data/third_party/boringssl/crypto/fipsmodule/modes/ccm.c +0 -256
  504. data/third_party/boringssl/include/openssl/lhash_macros.h +0 -174
  505. data/third_party/boringssl/ssl/custom_extensions.cc +0 -265
@@ -0,0 +1,87 @@
1
+ /* Copyright (c) 2018, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include <openssl/bn.h>
16
+
17
+ #include <assert.h>
18
+
19
+ #include "internal.h"
20
+
21
+
22
+ // The following functions use a Barrett reduction variant to avoid leaking the
23
+ // numerator. See http://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
24
+ //
25
+ // We use 32-bit numerator and 16-bit divisor for simplicity. This allows
26
+ // computing |m| and |q| without architecture-specific code.
27
+
28
+ // mod_u16 returns |n| mod |d|. |p| and |m| are the "magic numbers" for |d| (see
29
+ // reference). For proof of correctness in Coq, see
30
+ // https://github.com/davidben/fiat-crypto/blob/barrett/src/Arithmetic/BarrettReduction/RidiculousFish.v
31
+ // Note the Coq version of |mod_u16| additionally includes the computation of
32
+ // |p| and |m| from |bn_mod_u16_consttime| below.
33
+ static uint16_t mod_u16(uint32_t n, uint16_t d, uint32_t p, uint32_t m) {
34
+ // Compute floor(n/d) per steps 3 through 5.
35
+ uint32_t q = ((uint64_t)m * n) >> 32;
36
+ // Note there is a typo in the reference. We right-shift by one, not two.
37
+ uint32_t t = ((n - q) >> 1) + q;
38
+ t = t >> (p - 1);
39
+
40
+ // Multiply and subtract to get the remainder.
41
+ n -= d * t;
42
+ assert(n < d);
43
+ return n;
44
+ }
45
+
46
+ // shift_and_add_mod_u16 returns |r| * 2^32 + |a| mod |d|. |p| and |m| are the
47
+ // "magic numbers" for |d| (see reference).
48
+ static uint16_t shift_and_add_mod_u16(uint16_t r, uint32_t a, uint16_t d,
49
+ uint32_t p, uint32_t m) {
50
+ // Incorporate |a| in two 16-bit chunks.
51
+ uint32_t t = r;
52
+ t <<= 16;
53
+ t |= a >> 16;
54
+ t = mod_u16(t, d, p, m);
55
+
56
+ t <<= 16;
57
+ t |= a & 0xffff;
58
+ t = mod_u16(t, d, p, m);
59
+ return t;
60
+ }
61
+
62
+ uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d) {
63
+ if (d <= 1) {
64
+ return 0;
65
+ }
66
+
67
+ // Compute the "magic numbers" for |d|. See steps 1 and 2.
68
+ // This computes p = ceil(log_2(d)).
69
+ uint32_t p = BN_num_bits_word(d - 1);
70
+ // This operation is not constant-time, but |p| and |d| are public values.
71
+ // Note that |p| is at most 16, so the computation fits in |uint64_t|.
72
+ assert(p <= 16);
73
+ uint32_t m = ((UINT64_C(1) << (32 + p)) + d - 1) / d;
74
+
75
+ uint16_t ret = 0;
76
+ for (int i = bn->width - 1; i >= 0; i--) {
77
+ #if BN_BITS2 == 32
78
+ ret = shift_and_add_mod_u16(ret, bn->d[i], d, p, m);
79
+ #elif BN_BITS2 == 64
80
+ ret = shift_and_add_mod_u16(ret, bn->d[i] >> 32, d, p, m);
81
+ ret = shift_and_add_mod_u16(ret, bn->d[i] & 0xffffffff, d, p, m);
82
+ #else
83
+ #error "Unknown BN_ULONG size"
84
+ #endif
85
+ }
86
+ return ret;
87
+ }
@@ -109,6 +109,7 @@
109
109
  #include <openssl/bn.h>
110
110
 
111
111
  #include <assert.h>
112
+ #include <stdlib.h>
112
113
  #include <string.h>
113
114
 
114
115
  #include <openssl/cpu.h>
@@ -116,25 +117,8 @@
116
117
  #include <openssl/mem.h>
117
118
 
118
119
  #include "internal.h"
119
-
120
-
121
- #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
122
- #define OPENSSL_BN_ASM_MONT5
123
- #define RSAZ_ENABLED
124
-
125
120
  #include "rsaz_exp.h"
126
121
 
127
- void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap, const void *table,
128
- const BN_ULONG *np, const BN_ULONG *n0, int num,
129
- int power);
130
- void bn_scatter5(const BN_ULONG *inp, size_t num, void *table, size_t power);
131
- void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);
132
- void bn_power5(BN_ULONG *rp, const BN_ULONG *ap, const void *table,
133
- const BN_ULONG *np, const BN_ULONG *n0, int num, int power);
134
- int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,
135
- const BN_ULONG *not_used, const BN_ULONG *np,
136
- const BN_ULONG *n0, int num);
137
- #endif
138
122
 
139
123
  int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
140
124
  int i, bits, ret = 0;
@@ -445,21 +429,18 @@ static int BN_window_bits_for_exponent_size(int b) {
445
429
 
446
430
  static int mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
447
431
  const BIGNUM *m, BN_CTX *ctx) {
448
- int i, j, bits, ret = 0, wstart, window;
432
+ int i, j, ret = 0, wstart, window;
449
433
  int start = 1;
450
434
  BIGNUM *aa;
451
435
  // Table of variables obtained from 'ctx'
452
436
  BIGNUM *val[TABLE_SIZE];
453
437
  BN_RECP_CTX recp;
454
438
 
455
- bits = BN_num_bits(p);
439
+ // This function is only called on even moduli.
440
+ assert(!BN_is_odd(m));
456
441
 
442
+ int bits = BN_num_bits(p);
457
443
  if (bits == 0) {
458
- // x**0 mod 1 is still zero.
459
- if (BN_is_one(m)) {
460
- BN_zero(r);
461
- return 1;
462
- }
463
444
  return BN_one(r);
464
445
  }
465
446
 
@@ -585,6 +566,17 @@ err:
585
566
 
586
567
  int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
587
568
  BN_CTX *ctx) {
569
+ if (m->neg) {
570
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
571
+ return 0;
572
+ }
573
+ if (a->neg || BN_ucmp(a, m) >= 0) {
574
+ if (!BN_nnmod(r, a, m, ctx)) {
575
+ return 0;
576
+ }
577
+ a = r;
578
+ }
579
+
588
580
  if (BN_is_odd(m)) {
589
581
  return BN_mod_exp_mont(r, a, p, m, ctx, NULL);
590
582
  }
@@ -598,10 +590,19 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
598
590
  OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
599
591
  return 0;
600
592
  }
593
+ if (m->neg) {
594
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
595
+ return 0;
596
+ }
597
+ if (a->neg || BN_ucmp(a, m) >= 0) {
598
+ OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
599
+ return 0;
600
+ }
601
+
601
602
  int bits = BN_num_bits(p);
602
603
  if (bits == 0) {
603
604
  // x**0 mod 1 is still zero.
604
- if (BN_is_one(m)) {
605
+ if (BN_abs_is_word(m, 1)) {
605
606
  BN_zero(rr);
606
607
  return 1;
607
608
  }
@@ -613,48 +614,33 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
613
614
  BN_MONT_CTX *new_mont = NULL;
614
615
 
615
616
  BN_CTX_start(ctx);
616
- BIGNUM *d = BN_CTX_get(ctx);
617
617
  BIGNUM *r = BN_CTX_get(ctx);
618
618
  val[0] = BN_CTX_get(ctx);
619
- if (!d || !r || !val[0]) {
619
+ if (r == NULL || val[0] == NULL) {
620
620
  goto err;
621
621
  }
622
622
 
623
623
  // Allocate a montgomery context if it was not supplied by the caller.
624
624
  if (mont == NULL) {
625
- new_mont = BN_MONT_CTX_new_for_modulus(m, ctx);
625
+ new_mont = BN_MONT_CTX_new_consttime(m, ctx);
626
626
  if (new_mont == NULL) {
627
627
  goto err;
628
628
  }
629
629
  mont = new_mont;
630
630
  }
631
631
 
632
- const BIGNUM *aa;
633
- if (a->neg || BN_ucmp(a, m) >= 0) {
634
- if (!BN_nnmod(val[0], a, m, ctx)) {
635
- goto err;
636
- }
637
- aa = val[0];
638
- } else {
639
- aa = a;
640
- }
641
-
642
- if (BN_is_zero(aa)) {
643
- BN_zero(rr);
644
- ret = 1;
645
- goto err;
646
- }
647
-
648
632
  // We exponentiate by looking at sliding windows of the exponent and
649
- // precomputing powers of |aa|. Windows may be shifted so they always end on a
650
- // set bit, so only precompute odd powers. We compute val[i] = aa^(2*i + 1)
633
+ // precomputing powers of |a|. Windows may be shifted so they always end on a
634
+ // set bit, so only precompute odd powers. We compute val[i] = a^(2*i + 1)
651
635
  // for i = 0 to 2^(window-1), all in Montgomery form.
652
636
  int window = BN_window_bits_for_exponent_size(bits);
653
- if (!BN_to_montgomery(val[0], aa, mont, ctx)) {
637
+ if (!BN_to_montgomery(val[0], a, mont, ctx)) {
654
638
  goto err;
655
639
  }
656
640
  if (window > 1) {
657
- if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx)) {
641
+ BIGNUM *d = BN_CTX_get(ctx);
642
+ if (d == NULL ||
643
+ !BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx)) {
658
644
  goto err;
659
645
  }
660
646
  for (int i = 1; i < 1 << (window - 1); i++) {
@@ -666,10 +652,8 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
666
652
  }
667
653
  }
668
654
 
669
- if (!bn_one_to_montgomery(r, mont, ctx)) {
670
- goto err;
671
- }
672
-
655
+ // |p| is non-zero, so at least one window is non-zero. To save some
656
+ // multiplications, defer initializing |r| until then.
673
657
  int r_is_one = 1;
674
658
  int wstart = bits - 1; // The top bit of the window.
675
659
  for (;;) {
@@ -706,7 +690,11 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
706
690
 
707
691
  assert(wvalue & 1);
708
692
  assert(wvalue < (1 << window));
709
- if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx)) {
693
+ if (r_is_one) {
694
+ if (!BN_copy(r, val[wvalue >> 1])) {
695
+ goto err;
696
+ }
697
+ } else if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx)) {
710
698
  goto err;
711
699
  }
712
700
 
@@ -717,6 +705,9 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
717
705
  wstart -= wsize + 1;
718
706
  }
719
707
 
708
+ // |p| is non-zero, so |r_is_one| must be cleared at some point.
709
+ assert(!r_is_one);
710
+
720
711
  if (!BN_from_montgomery(rr, r, mont, ctx)) {
721
712
  goto err;
722
713
  }
@@ -728,29 +719,24 @@ err:
728
719
  return ret;
729
720
  }
730
721
 
731
- int bn_mod_exp_mont_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
732
- size_t num_a, const BN_ULONG *p, size_t num_p,
733
- const BN_MONT_CTX *mont) {
734
- size_t num_n = mont->N.width;
735
- if (num_n != num_a || num_n != num_r || num_n > BN_SMALL_MAX_WORDS) {
736
- OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
737
- return 0;
738
- }
739
- if (!BN_is_odd(&mont->N)) {
740
- OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
741
- return 0;
722
+ void bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num,
723
+ const BN_ULONG *p, size_t num_p,
724
+ const BN_MONT_CTX *mont) {
725
+ if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) {
726
+ abort();
742
727
  }
743
- unsigned bits = 0;
744
- if (num_p != 0) {
745
- bits = BN_num_bits_word(p[num_p - 1]) + (num_p - 1) * BN_BITS2;
728
+ assert(BN_is_odd(&mont->N));
729
+
730
+ // Count the number of bits in |p|. Note this function treats |p| as public.
731
+ while (num_p != 0 && p[num_p - 1] == 0) {
732
+ num_p--;
746
733
  }
747
- if (bits == 0) {
748
- OPENSSL_memset(r, 0, num_r * sizeof(BN_ULONG));
749
- if (!BN_is_one(&mont->N)) {
750
- r[0] = 1;
751
- }
752
- return 1;
734
+ if (num_p == 0) {
735
+ bn_from_montgomery_small(r, mont->RR.d, num, mont);
736
+ return;
753
737
  }
738
+ unsigned bits = BN_num_bits_word(p[num_p - 1]) + (num_p - 1) * BN_BITS2;
739
+ assert(bits != 0);
754
740
 
755
741
  // We exponentiate by looking at sliding windows of the exponent and
756
742
  // precomputing powers of |a|. Windows may be shifted so they always end on a
@@ -760,34 +746,24 @@ int bn_mod_exp_mont_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
760
746
  if (window > TABLE_BITS_SMALL) {
761
747
  window = TABLE_BITS_SMALL; // Tolerate excessively large |p|.
762
748
  }
763
- int ret = 0;
764
749
  BN_ULONG val[TABLE_SIZE_SMALL][BN_SMALL_MAX_WORDS];
765
- OPENSSL_memcpy(val[0], a, num_n * sizeof(BN_ULONG));
750
+ OPENSSL_memcpy(val[0], a, num * sizeof(BN_ULONG));
766
751
  if (window > 1) {
767
752
  BN_ULONG d[BN_SMALL_MAX_WORDS];
768
- if (!bn_mod_mul_montgomery_small(d, num_n, val[0], num_n, val[0], num_n,
769
- mont)) {
770
- goto err;
771
- }
753
+ bn_mod_mul_montgomery_small(d, val[0], val[0], num, mont);
772
754
  for (unsigned i = 1; i < 1u << (window - 1); i++) {
773
- if (!bn_mod_mul_montgomery_small(val[i], num_n, val[i - 1], num_n, d,
774
- num_n, mont)) {
775
- goto err;
776
- }
755
+ bn_mod_mul_montgomery_small(val[i], val[i - 1], d, num, mont);
777
756
  }
778
757
  }
779
758
 
780
- if (!bn_one_to_montgomery_small(r, num_r, mont)) {
781
- goto err;
782
- }
783
-
759
+ // |p| is non-zero, so at least one window is non-zero. To save some
760
+ // multiplications, defer initializing |r| until then.
784
761
  int r_is_one = 1;
785
762
  unsigned wstart = bits - 1; // The top bit of the window.
786
763
  for (;;) {
787
764
  if (!bn_is_bit_set_words(p, num_p, wstart)) {
788
- if (!r_is_one &&
789
- !bn_mod_mul_montgomery_small(r, num_r, r, num_r, r, num_r, mont)) {
790
- goto err;
765
+ if (!r_is_one) {
766
+ bn_mod_mul_montgomery_small(r, r, r, num, mont);
791
767
  }
792
768
  if (wstart == 0) {
793
769
  break;
@@ -810,19 +786,17 @@ int bn_mod_exp_mont_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
810
786
  // Shift |r| to the end of the window.
811
787
  if (!r_is_one) {
812
788
  for (unsigned i = 0; i < wsize + 1; i++) {
813
- if (!bn_mod_mul_montgomery_small(r, num_r, r, num_r, r, num_r, mont)) {
814
- goto err;
815
- }
789
+ bn_mod_mul_montgomery_small(r, r, r, num, mont);
816
790
  }
817
791
  }
818
792
 
819
793
  assert(wvalue & 1);
820
794
  assert(wvalue < (1u << window));
821
- if (!bn_mod_mul_montgomery_small(r, num_r, r, num_r, val[wvalue >> 1],
822
- num_n, mont)) {
823
- goto err;
795
+ if (r_is_one) {
796
+ OPENSSL_memcpy(r, val[wvalue >> 1], num * sizeof(BN_ULONG));
797
+ } else {
798
+ bn_mod_mul_montgomery_small(r, r, val[wvalue >> 1], num, mont);
824
799
  }
825
-
826
800
  r_is_one = 0;
827
801
  if (wstart == wsize) {
828
802
  break;
@@ -830,103 +804,54 @@ int bn_mod_exp_mont_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
830
804
  wstart -= wsize + 1;
831
805
  }
832
806
 
833
- ret = 1;
834
-
835
- err:
807
+ // |p| is non-zero, so |r_is_one| must be cleared at some point.
808
+ assert(!r_is_one);
836
809
  OPENSSL_cleanse(val, sizeof(val));
837
- return ret;
838
810
  }
839
811
 
840
- int bn_mod_inverse_prime_mont_small(BN_ULONG *r, size_t num_r,
841
- const BN_ULONG *a, size_t num_a,
842
- const BN_MONT_CTX *mont) {
843
- const BN_ULONG *p = mont->N.d;
844
- size_t num_p = mont->N.width;
845
- if (num_p > BN_SMALL_MAX_WORDS || num_p == 0) {
846
- OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
847
- return 0;
812
+ void bn_mod_inverse_prime_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num,
813
+ const BN_MONT_CTX *mont) {
814
+ if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) {
815
+ abort();
848
816
  }
849
817
 
850
818
  // Per Fermat's Little Theorem, a^-1 = a^(p-2) (mod p) for p prime.
851
819
  BN_ULONG p_minus_two[BN_SMALL_MAX_WORDS];
852
- OPENSSL_memcpy(p_minus_two, p, num_p * sizeof(BN_ULONG));
820
+ const BN_ULONG *p = mont->N.d;
821
+ OPENSSL_memcpy(p_minus_two, p, num * sizeof(BN_ULONG));
853
822
  if (p_minus_two[0] >= 2) {
854
823
  p_minus_two[0] -= 2;
855
824
  } else {
856
825
  p_minus_two[0] -= 2;
857
- for (size_t i = 1; i < num_p; i++) {
826
+ for (size_t i = 1; i < num; i++) {
858
827
  if (p_minus_two[i]-- != 0) {
859
828
  break;
860
829
  }
861
830
  }
862
831
  }
863
832
 
864
- return bn_mod_exp_mont_small(r, num_r, a, num_a, p_minus_two, num_p, mont);
833
+ bn_mod_exp_mont_small(r, a, num, p_minus_two, num, mont);
865
834
  }
866
835
 
867
-
868
- // |BN_mod_exp_mont_consttime| stores the precomputed powers in a specific
869
- // layout so that accessing any of these table values shows the same access
870
- // pattern as far as cache lines are concerned. The following functions are
871
- // used to transfer a BIGNUM from/to that table.
872
-
873
- static void copy_to_prebuf(const BIGNUM *b, int top, unsigned char *buf,
874
- int idx, int window) {
875
- int i, j;
876
- const int width = 1 << window;
877
- BN_ULONG *table = (BN_ULONG *) buf;
878
-
879
- if (top > b->width) {
880
- top = b->width; // this works because 'buf' is explicitly zeroed
881
- }
882
-
883
- for (i = 0, j = idx; i < top; i++, j += width) {
884
- table[j] = b->d[i];
885
- }
836
+ static void copy_to_prebuf(const BIGNUM *b, int top, BN_ULONG *table, int idx,
837
+ int window) {
838
+ int ret = bn_copy_words(table + idx * top, top, b);
839
+ assert(ret); // |b| is guaranteed to fit.
840
+ (void)ret;
886
841
  }
887
842
 
888
- static int copy_from_prebuf(BIGNUM *b, int top, unsigned char *buf, int idx,
843
+ static int copy_from_prebuf(BIGNUM *b, int top, const BN_ULONG *table, int idx,
889
844
  int window) {
890
- int i, j;
891
- const int width = 1 << window;
892
- volatile BN_ULONG *table = (volatile BN_ULONG *)buf;
893
-
894
845
  if (!bn_wexpand(b, top)) {
895
846
  return 0;
896
847
  }
897
848
 
898
- if (window <= 3) {
899
- for (i = 0; i < top; i++, table += width) {
900
- BN_ULONG acc = 0;
901
-
902
- for (j = 0; j < width; j++) {
903
- acc |= table[j] & ((BN_ULONG)0 - (constant_time_eq_int(j, idx) & 1));
904
- }
905
-
906
- b->d[i] = acc;
907
- }
908
- } else {
909
- int xstride = 1 << (window - 2);
910
- BN_ULONG y0, y1, y2, y3;
911
-
912
- i = idx >> (window - 2); // equivalent of idx / xstride
913
- idx &= xstride - 1; // equivalent of idx % xstride
914
-
915
- y0 = (BN_ULONG)0 - (constant_time_eq_int(i, 0) & 1);
916
- y1 = (BN_ULONG)0 - (constant_time_eq_int(i, 1) & 1);
917
- y2 = (BN_ULONG)0 - (constant_time_eq_int(i, 2) & 1);
918
- y3 = (BN_ULONG)0 - (constant_time_eq_int(i, 3) & 1);
919
-
920
- for (i = 0; i < top; i++, table += width) {
921
- BN_ULONG acc = 0;
922
-
923
- for (j = 0; j < xstride; j++) {
924
- acc |= ((table[j + 0 * xstride] & y0) | (table[j + 1 * xstride] & y1) |
925
- (table[j + 2 * xstride] & y2) | (table[j + 3 * xstride] & y3)) &
926
- ((BN_ULONG)0 - (constant_time_eq_int(j, idx) & 1));
927
- }
928
-
929
- b->d[i] = acc;
849
+ OPENSSL_memset(b->d, 0, sizeof(BN_ULONG) * top);
850
+ const int width = 1 << window;
851
+ for (int i = 0; i < width; i++, table += top) {
852
+ BN_ULONG mask = constant_time_eq_int(i, idx);
853
+ for (int j = 0; j < top; j++) {
854
+ b->d[j] |= table[j] & mask;
930
855
  }
931
856
  }
932
857
 
@@ -934,9 +859,6 @@ static int copy_from_prebuf(BIGNUM *b, int top, unsigned char *buf, int idx,
934
859
  return 1;
935
860
  }
936
861
 
937
- // BN_mod_exp_mont_conttime is based on the assumption that the L1 data cache
938
- // line width of the target processor is at least the following value.
939
- #define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH (64)
940
862
  #define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK \
941
863
  (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
942
864
 
@@ -972,9 +894,8 @@ static int copy_from_prebuf(BIGNUM *b, int top, unsigned char *buf, int idx,
972
894
  (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - \
973
895
  (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
974
896
 
975
- // This variant of BN_mod_exp_mont() uses fixed windows and the special
976
- // precomputation memory layout to limit data-dependency to a minimum
977
- // to protect secret exponents (cf. the hyper-threading timing attacks
897
+ // This variant of |BN_mod_exp_mont| uses fixed windows and fixed memory access
898
+ // patterns to protect secret exponents (cf. the hyper-threading timing attacks
978
899
  // pointed out by Colin Percival,
979
900
  // http://www.daemonology.net/hyperthreading-considered-harmful/)
980
901
  int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
@@ -986,14 +907,21 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
986
907
  int numPowers;
987
908
  unsigned char *powerbufFree = NULL;
988
909
  int powerbufLen = 0;
989
- unsigned char *powerbuf = NULL;
910
+ BN_ULONG *powerbuf = NULL;
990
911
  BIGNUM tmp, am;
991
- BIGNUM *new_a = NULL;
992
912
 
993
913
  if (!BN_is_odd(m)) {
994
914
  OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
995
915
  return 0;
996
916
  }
917
+ if (m->neg) {
918
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
919
+ return 0;
920
+ }
921
+ if (a->neg || BN_ucmp(a, m) >= 0) {
922
+ OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
923
+ return 0;
924
+ }
997
925
 
998
926
  // Use all bits stored in |p|, rather than |BN_num_bits|, so we do not leak
999
927
  // whether the top bits are zero.
@@ -1001,7 +929,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
1001
929
  int bits = max_bits;
1002
930
  if (bits == 0) {
1003
931
  // x**0 mod 1 is still zero.
1004
- if (BN_is_one(m)) {
932
+ if (BN_abs_is_word(m, 1)) {
1005
933
  BN_zero(rr);
1006
934
  return 1;
1007
935
  }
@@ -1010,7 +938,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
1010
938
 
1011
939
  // Allocate a montgomery context if it was not supplied by the caller.
1012
940
  if (mont == NULL) {
1013
- new_mont = BN_MONT_CTX_new_for_modulus(m, ctx);
941
+ new_mont = BN_MONT_CTX_new_consttime(m, ctx);
1014
942
  if (new_mont == NULL) {
1015
943
  goto err;
1016
944
  }
@@ -1021,25 +949,25 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
1021
949
  // implementation assumes it can use |top| to size R.
1022
950
  int top = mont->N.width;
1023
951
 
1024
- if (a->neg || BN_ucmp(a, m) >= 0) {
1025
- new_a = BN_new();
1026
- if (new_a == NULL ||
1027
- !BN_nnmod(new_a, a, m, ctx)) {
1028
- goto err;
1029
- }
1030
- a = new_a;
1031
- }
1032
-
1033
- #ifdef RSAZ_ENABLED
1034
- // If the size of the operands allow it, perform the optimized
1035
- // RSAZ exponentiation. For further information see
1036
- // crypto/bn/rsaz_exp.c and accompanying assembly modules.
1037
- if ((16 == a->width) && (16 == p->width) && (BN_num_bits(m) == 1024) &&
1038
- rsaz_avx2_eligible()) {
952
+ #if defined(OPENSSL_BN_ASM_MONT5) || defined(RSAZ_ENABLED)
953
+ // Share one large stack-allocated buffer between the RSAZ and non-RSAZ code
954
+ // paths. If we were to use separate static buffers for each then there is
955
+ // some chance that both large buffers would be allocated on the stack,
956
+ // causing the stack space requirement to be truly huge (~10KB).
957
+ alignas(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH) BN_ULONG
958
+ storage[MOD_EXP_CTIME_STORAGE_LEN];
959
+ #endif
960
+ #if defined(RSAZ_ENABLED)
961
+ // If the size of the operands allow it, perform the optimized RSAZ
962
+ // exponentiation. For further information see crypto/fipsmodule/bn/rsaz_exp.c
963
+ // and accompanying assembly modules.
964
+ if (a->width == 16 && p->width == 16 && BN_num_bits(m) == 1024 &&
965
+ rsaz_avx2_preferred()) {
1039
966
  if (!bn_wexpand(rr, 16)) {
1040
967
  goto err;
1041
968
  }
1042
- RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d, mont->n0[0]);
969
+ RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d, mont->n0[0],
970
+ storage);
1043
971
  rr->width = 16;
1044
972
  rr->neg = 0;
1045
973
  ret = 1;
@@ -1063,29 +991,26 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
1063
991
  powerbufLen +=
1064
992
  sizeof(m->d[0]) *
1065
993
  (top * numPowers + ((2 * top) > numPowers ? (2 * top) : numPowers));
1066
- #ifdef alloca
1067
- if (powerbufLen < 3072) {
1068
- powerbufFree = alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
1069
- } else
994
+
995
+ #if defined(OPENSSL_BN_ASM_MONT5)
996
+ if ((size_t)powerbufLen <= sizeof(storage)) {
997
+ powerbuf = storage;
998
+ }
999
+ // |storage| is more than large enough to handle 1024-bit inputs.
1000
+ assert(powerbuf != NULL || top * BN_BITS2 > 1024);
1070
1001
  #endif
1071
- {
1072
- if ((powerbufFree = OPENSSL_malloc(
1073
- powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL) {
1002
+ if (powerbuf == NULL) {
1003
+ powerbufFree =
1004
+ OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
1005
+ if (powerbufFree == NULL) {
1074
1006
  goto err;
1075
1007
  }
1008
+ powerbuf = (BN_ULONG *)MOD_EXP_CTIME_ALIGN(powerbufFree);
1076
1009
  }
1077
-
1078
- powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
1079
1010
  OPENSSL_memset(powerbuf, 0, powerbufLen);
1080
1011
 
1081
- #ifdef alloca
1082
- if (powerbufLen < 3072) {
1083
- powerbufFree = NULL;
1084
- }
1085
- #endif
1086
-
1087
1012
  // lay down tmp and am right after powers table
1088
- tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);
1013
+ tmp.d = powerbuf + top * numPowers;
1089
1014
  am.d = tmp.d + top;
1090
1015
  tmp.width = am.width = 0;
1091
1016
  tmp.dmax = am.dmax = top;
@@ -1290,7 +1215,9 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
1290
1215
 
1291
1216
  err:
1292
1217
  BN_MONT_CTX_free(new_mont);
1293
- BN_clear_free(new_a);
1218
+ if (powerbuf != NULL && powerbufFree == NULL) {
1219
+ OPENSSL_cleanse(powerbuf, powerbufLen);
1220
+ }
1294
1221
  OPENSSL_free(powerbufFree);
1295
1222
  return (ret);
1296
1223
  }
@@ -1303,6 +1230,11 @@ int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
1303
1230
 
1304
1231
  int ret = 0;
1305
1232
 
1233
+ // BN_mod_exp_mont requires reduced inputs.
1234
+ if (bn_minimal_width(m) == 1) {
1235
+ a %= m->d[0];
1236
+ }
1237
+
1306
1238
  if (!BN_set_word(&a_bignum, a)) {
1307
1239
  OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
1308
1240
  goto err;