grpc 1.24.0 → 1.25.0.pre1

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 (504) 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/exec_ctx.h +4 -3
  113. data/src/core/lib/iomgr/executor.cc +4 -2
  114. data/src/core/lib/iomgr/executor.h +3 -0
  115. data/src/core/lib/iomgr/executor/mpmcqueue.h +3 -6
  116. data/src/core/lib/iomgr/executor/threadpool.cc +1 -2
  117. data/src/core/lib/iomgr/executor/threadpool.h +7 -11
  118. data/src/core/lib/iomgr/resource_quota.cc +55 -51
  119. data/src/core/lib/iomgr/resource_quota.h +13 -9
  120. data/src/core/lib/iomgr/socket_utils_common_posix.cc +13 -0
  121. data/src/core/lib/iomgr/socket_utils_posix.h +4 -0
  122. data/src/core/lib/iomgr/tcp_client_posix.cc +4 -11
  123. data/src/core/lib/iomgr/tcp_custom.cc +9 -7
  124. data/src/core/lib/iomgr/tcp_posix.cc +20 -16
  125. data/src/core/lib/iomgr/tcp_server.h +1 -4
  126. data/src/core/lib/iomgr/tcp_server_custom.cc +5 -5
  127. data/src/core/lib/iomgr/tcp_server_posix.cc +1 -1
  128. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +2 -11
  129. data/src/core/lib/iomgr/timer_custom.cc +2 -2
  130. data/src/core/lib/iomgr/udp_server.cc +3 -2
  131. data/src/core/lib/iomgr/udp_server.h +6 -12
  132. data/src/core/lib/json/json.h +1 -1
  133. data/src/core/lib/json/json_string.cc +2 -2
  134. data/src/core/lib/profiling/basic_timers.cc +2 -2
  135. data/src/core/lib/security/credentials/alts/alts_credentials.cc +2 -2
  136. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_server_options.cc +1 -1
  137. data/src/core/lib/security/credentials/credentials.h +4 -20
  138. data/src/core/lib/security/credentials/fake/fake_credentials.cc +4 -4
  139. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -3
  140. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +64 -0
  141. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +4 -4
  142. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +9 -7
  143. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -0
  144. data/src/core/lib/security/security_connector/local/local_security_connector.cc +4 -4
  145. data/src/core/lib/security/security_connector/security_connector.cc +1 -0
  146. data/src/core/lib/security/security_connector/security_connector.h +19 -17
  147. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +8 -5
  148. data/src/core/lib/security/security_connector/ssl_utils.cc +2 -2
  149. data/src/core/lib/security/security_connector/ssl_utils.h +1 -1
  150. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +14 -6
  151. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +4 -2
  152. data/src/core/lib/security/transport/client_auth_filter.cc +17 -17
  153. data/src/core/lib/security/transport/security_handshaker.cc +29 -13
  154. data/src/core/lib/security/transport/security_handshaker.h +4 -2
  155. data/src/core/lib/security/transport/server_auth_filter.cc +14 -14
  156. data/src/core/lib/slice/slice.cc +2 -10
  157. data/src/core/lib/slice/slice_hash_table.h +4 -6
  158. data/src/core/lib/slice/slice_intern.cc +42 -39
  159. data/src/core/lib/slice/slice_internal.h +3 -3
  160. data/src/core/lib/slice/slice_utils.h +21 -4
  161. data/src/core/lib/slice/slice_weak_hash_table.h +4 -6
  162. data/src/core/lib/surface/call.cc +3 -3
  163. data/src/core/lib/surface/channel.cc +7 -0
  164. data/src/core/lib/surface/completion_queue.cc +12 -11
  165. data/src/core/lib/surface/completion_queue.h +4 -2
  166. data/src/core/lib/surface/init.cc +1 -0
  167. data/src/core/lib/surface/lame_client.cc +33 -18
  168. data/src/core/lib/surface/server.cc +77 -76
  169. data/src/core/lib/surface/version.cc +1 -1
  170. data/src/core/lib/transport/byte_stream.h +3 -7
  171. data/src/core/lib/transport/connectivity_state.cc +112 -98
  172. data/src/core/lib/transport/connectivity_state.h +100 -50
  173. data/src/core/lib/transport/static_metadata.cc +276 -288
  174. data/src/core/lib/transport/static_metadata.h +73 -76
  175. data/src/core/lib/transport/status_conversion.cc +1 -1
  176. data/src/core/lib/transport/status_metadata.cc +1 -1
  177. data/src/core/lib/transport/transport.cc +2 -2
  178. data/src/core/lib/transport/transport.h +12 -4
  179. data/src/core/lib/transport/transport_op_string.cc +14 -11
  180. data/src/core/tsi/alts/frame_protector/alts_unseal_privacy_integrity_crypter.cc +1 -1
  181. data/src/core/tsi/alts/handshaker/alts_shared_resource.cc +1 -1
  182. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +5 -5
  183. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -2
  184. data/src/core/tsi/fake_transport_security.cc +7 -5
  185. data/src/core/tsi/grpc_shadow_boringssl.h +2918 -2627
  186. data/src/core/tsi/local_transport_security.cc +8 -6
  187. data/src/core/tsi/ssl/session_cache/ssl_session.h +1 -3
  188. data/src/core/tsi/ssl/session_cache/ssl_session_boringssl.cc +1 -2
  189. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +7 -5
  190. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +4 -6
  191. data/src/core/tsi/ssl/session_cache/ssl_session_openssl.cc +1 -2
  192. data/src/core/tsi/ssl_transport_security.cc +12 -12
  193. data/src/core/tsi/ssl_transport_security.h +2 -2
  194. data/src/core/tsi/transport_security_grpc.cc +7 -0
  195. data/src/core/tsi/transport_security_grpc.h +6 -0
  196. data/src/ruby/ext/grpc/extconf.rb +1 -0
  197. data/src/ruby/ext/grpc/rb_call.c +1 -1
  198. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  199. data/src/ruby/lib/grpc/generic/bidi_call.rb +1 -1
  200. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  201. data/src/ruby/lib/grpc/version.rb +1 -1
  202. data/src/ruby/spec/google_rpc_status_utils_spec.rb +2 -2
  203. data/third_party/boringssl/crypto/asn1/a_bool.c +18 -5
  204. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +17 -221
  205. data/third_party/boringssl/crypto/asn1/a_dup.c +0 -24
  206. data/third_party/boringssl/crypto/asn1/a_enum.c +2 -2
  207. data/third_party/boringssl/crypto/asn1/a_i2d_fp.c +10 -72
  208. data/third_party/boringssl/crypto/asn1/a_int.c +12 -71
  209. data/third_party/boringssl/crypto/asn1/a_mbstr.c +110 -216
  210. data/third_party/boringssl/crypto/asn1/a_object.c +16 -5
  211. data/third_party/boringssl/crypto/asn1/a_strnid.c +1 -0
  212. data/third_party/boringssl/crypto/asn1/asn1_lib.c +5 -1
  213. data/third_party/boringssl/crypto/asn1/tasn_enc.c +3 -1
  214. data/third_party/boringssl/crypto/base64/base64.c +2 -2
  215. data/third_party/boringssl/crypto/bio/bio.c +73 -9
  216. data/third_party/boringssl/crypto/bio/connect.c +4 -0
  217. data/third_party/boringssl/crypto/bio/fd.c +4 -0
  218. data/third_party/boringssl/crypto/bio/file.c +5 -2
  219. data/third_party/boringssl/crypto/bio/socket.c +4 -0
  220. data/third_party/boringssl/crypto/bio/socket_helper.c +4 -0
  221. data/third_party/boringssl/crypto/bn_extra/convert.c +11 -7
  222. data/third_party/boringssl/crypto/bytestring/ber.c +8 -4
  223. data/third_party/boringssl/crypto/bytestring/cbb.c +19 -7
  224. data/third_party/boringssl/crypto/bytestring/cbs.c +28 -15
  225. data/third_party/boringssl/crypto/bytestring/internal.h +28 -7
  226. data/third_party/boringssl/crypto/bytestring/unicode.c +155 -0
  227. data/third_party/boringssl/crypto/chacha/chacha.c +36 -19
  228. data/third_party/boringssl/crypto/chacha/internal.h +45 -0
  229. data/third_party/boringssl/crypto/cipher_extra/cipher_extra.c +29 -0
  230. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +269 -25
  231. data/third_party/boringssl/crypto/cipher_extra/e_aesctrhmac.c +16 -14
  232. data/third_party/boringssl/crypto/cipher_extra/e_aesgcmsiv.c +54 -38
  233. data/third_party/boringssl/crypto/cipher_extra/e_chacha20poly1305.c +133 -41
  234. data/third_party/boringssl/crypto/cipher_extra/e_tls.c +23 -15
  235. data/third_party/boringssl/crypto/cipher_extra/tls_cbc.c +24 -15
  236. data/third_party/boringssl/crypto/cmac/cmac.c +62 -25
  237. data/third_party/boringssl/crypto/conf/conf.c +7 -0
  238. data/third_party/boringssl/crypto/cpu-arm-linux.c +4 -148
  239. data/third_party/boringssl/crypto/cpu-arm-linux.h +201 -0
  240. data/third_party/boringssl/crypto/cpu-intel.c +45 -51
  241. data/third_party/boringssl/crypto/crypto.c +39 -22
  242. data/third_party/boringssl/crypto/curve25519/spake25519.c +1 -1
  243. data/third_party/boringssl/crypto/dsa/dsa.c +77 -53
  244. data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +20 -8
  245. data/third_party/boringssl/crypto/ec_extra/ec_derive.c +96 -0
  246. data/third_party/boringssl/crypto/{ecdh/ecdh.c → ecdh_extra/ecdh_extra.c} +20 -58
  247. data/third_party/boringssl/crypto/ecdsa_extra/ecdsa_asn1.c +1 -9
  248. data/third_party/boringssl/crypto/engine/engine.c +2 -1
  249. data/third_party/boringssl/crypto/err/err.c +2 -0
  250. data/third_party/boringssl/crypto/err/internal.h +2 -2
  251. data/third_party/boringssl/crypto/evp/evp.c +89 -8
  252. data/third_party/boringssl/crypto/evp/evp_asn1.c +56 -5
  253. data/third_party/boringssl/crypto/evp/evp_ctx.c +52 -14
  254. data/third_party/boringssl/crypto/evp/internal.h +18 -1
  255. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +5 -0
  256. data/third_party/boringssl/crypto/evp/p_ec.c +51 -3
  257. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +6 -7
  258. data/third_party/boringssl/crypto/evp/p_ed25519.c +36 -3
  259. data/third_party/boringssl/crypto/evp/p_ed25519_asn1.c +76 -45
  260. data/third_party/boringssl/crypto/evp/p_rsa.c +3 -1
  261. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +5 -0
  262. data/third_party/boringssl/crypto/evp/p_x25519.c +110 -0
  263. data/third_party/boringssl/crypto/evp/p_x25519_asn1.c +249 -0
  264. data/third_party/boringssl/crypto/evp/scrypt.c +6 -2
  265. data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +34 -274
  266. data/third_party/boringssl/crypto/fipsmodule/aes/internal.h +161 -21
  267. data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +111 -13
  268. data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +17 -21
  269. data/third_party/boringssl/crypto/fipsmodule/bcm.c +119 -7
  270. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +19 -2
  271. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +2 -2
  272. data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +93 -160
  273. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +48 -57
  274. data/third_party/boringssl/crypto/fipsmodule/bn/div_extra.c +87 -0
  275. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +143 -211
  276. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +0 -305
  277. data/third_party/boringssl/crypto/fipsmodule/bn/gcd_extra.c +325 -0
  278. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +168 -50
  279. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +68 -92
  280. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +7 -6
  281. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +11 -14
  282. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +358 -443
  283. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +25 -35
  284. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +20 -25
  285. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.h +76 -5
  286. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +14 -14
  287. data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +7 -2
  288. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +383 -516
  289. data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +4 -0
  290. data/third_party/boringssl/crypto/fipsmodule/cipher/internal.h +3 -4
  291. data/third_party/boringssl/crypto/fipsmodule/delocate.h +3 -2
  292. data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +32 -17
  293. data/third_party/boringssl/crypto/fipsmodule/digest/md32_common.h +3 -3
  294. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +228 -122
  295. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +34 -8
  296. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +311 -98
  297. data/third_party/boringssl/crypto/fipsmodule/ec/felem.c +82 -0
  298. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +263 -97
  299. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +22 -59
  300. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +317 -234
  301. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +9473 -9475
  302. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +313 -109
  303. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.h +36 -0
  304. data/third_party/boringssl/crypto/fipsmodule/ec/scalar.c +96 -0
  305. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +126 -792
  306. data/third_party/boringssl/crypto/fipsmodule/ec/simple_mul.c +84 -0
  307. data/third_party/boringssl/crypto/fipsmodule/ec/util.c +163 -12
  308. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +84 -211
  309. data/third_party/boringssl/crypto/fipsmodule/ecdh/ecdh.c +122 -0
  310. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +60 -205
  311. data/third_party/boringssl/crypto/fipsmodule/fips_shared_support.c +32 -0
  312. data/third_party/boringssl/crypto/fipsmodule/is_fips.c +2 -0
  313. data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +3 -1
  314. data/third_party/boringssl/crypto/fipsmodule/md5/internal.h +37 -0
  315. data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +11 -8
  316. data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +35 -79
  317. data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +7 -39
  318. data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +7 -27
  319. data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +123 -309
  320. data/third_party/boringssl/crypto/fipsmodule/modes/internal.h +189 -126
  321. data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +3 -2
  322. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +2 -2
  323. data/third_party/boringssl/crypto/fipsmodule/rand/internal.h +35 -0
  324. data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +24 -19
  325. data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +256 -77
  326. data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +10 -7
  327. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +5 -1
  328. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +131 -14
  329. data/third_party/boringssl/crypto/fipsmodule/self_check/self_check.c +83 -10
  330. data/third_party/boringssl/crypto/fipsmodule/sha/internal.h +53 -0
  331. data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +9 -13
  332. data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +18 -12
  333. data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +95 -168
  334. data/third_party/boringssl/crypto/hrss/hrss.c +2201 -0
  335. data/third_party/boringssl/crypto/hrss/internal.h +62 -0
  336. data/third_party/boringssl/crypto/internal.h +95 -20
  337. data/third_party/boringssl/crypto/lhash/lhash.c +45 -33
  338. data/third_party/boringssl/crypto/mem.c +39 -2
  339. data/third_party/boringssl/crypto/obj/obj.c +4 -4
  340. data/third_party/boringssl/crypto/obj/obj_dat.h +6181 -875
  341. data/third_party/boringssl/crypto/pem/pem_all.c +2 -3
  342. data/third_party/boringssl/crypto/pem/pem_info.c +144 -162
  343. data/third_party/boringssl/crypto/pem/pem_lib.c +53 -52
  344. data/third_party/boringssl/crypto/pem/pem_pkey.c +13 -21
  345. data/third_party/boringssl/crypto/pkcs7/pkcs7.c +15 -22
  346. data/third_party/boringssl/crypto/pkcs7/pkcs7_x509.c +168 -16
  347. data/third_party/boringssl/crypto/pkcs8/internal.h +11 -0
  348. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +24 -15
  349. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +42 -25
  350. data/third_party/boringssl/crypto/pkcs8/pkcs8_x509.c +559 -43
  351. data/third_party/boringssl/crypto/pool/internal.h +1 -1
  352. data/third_party/boringssl/crypto/pool/pool.c +21 -0
  353. data/third_party/boringssl/crypto/rand_extra/deterministic.c +8 -0
  354. data/third_party/boringssl/crypto/rand_extra/fuchsia.c +1 -14
  355. data/third_party/boringssl/crypto/refcount_lock.c +2 -2
  356. data/third_party/boringssl/crypto/rsa_extra/rsa_print.c +22 -0
  357. data/third_party/boringssl/crypto/siphash/siphash.c +80 -0
  358. data/third_party/boringssl/crypto/stack/stack.c +83 -32
  359. data/third_party/boringssl/crypto/thread_none.c +2 -2
  360. data/third_party/boringssl/crypto/thread_pthread.c +2 -2
  361. data/third_party/boringssl/crypto/thread_win.c +38 -19
  362. data/third_party/boringssl/crypto/x509/a_strex.c +22 -2
  363. data/third_party/boringssl/crypto/x509/asn1_gen.c +2 -1
  364. data/third_party/boringssl/crypto/x509/by_dir.c +7 -0
  365. data/third_party/boringssl/crypto/x509/by_file.c +12 -10
  366. data/third_party/boringssl/crypto/x509/t_crl.c +5 -8
  367. data/third_party/boringssl/crypto/x509/t_req.c +1 -3
  368. data/third_party/boringssl/crypto/x509/t_x509.c +5 -8
  369. data/third_party/boringssl/crypto/x509/x509_cmp.c +1 -1
  370. data/third_party/boringssl/crypto/x509/x509_def.c +1 -1
  371. data/third_party/boringssl/crypto/x509/x509_lu.c +114 -5
  372. data/third_party/boringssl/crypto/x509/x509_req.c +20 -0
  373. data/third_party/boringssl/crypto/x509/x509_set.c +5 -0
  374. data/third_party/boringssl/crypto/x509/x509_trs.c +1 -0
  375. data/third_party/boringssl/crypto/x509/x509_txt.c +4 -5
  376. data/third_party/boringssl/crypto/x509/x509_vfy.c +145 -138
  377. data/third_party/boringssl/crypto/x509/x509_vpm.c +2 -0
  378. data/third_party/boringssl/crypto/x509/x509cset.c +40 -0
  379. data/third_party/boringssl/crypto/x509/x509name.c +2 -3
  380. data/third_party/boringssl/crypto/x509/x_all.c +109 -210
  381. data/third_party/boringssl/crypto/x509/x_x509.c +6 -0
  382. data/third_party/boringssl/crypto/x509v3/ext_dat.h +1 -3
  383. data/third_party/boringssl/crypto/x509v3/internal.h +56 -0
  384. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +2 -0
  385. data/third_party/boringssl/crypto/x509v3/pcy_node.c +1 -0
  386. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +4 -2
  387. data/third_party/boringssl/crypto/x509v3/v3_akey.c +5 -2
  388. data/third_party/boringssl/crypto/x509v3/v3_alt.c +19 -13
  389. data/third_party/boringssl/crypto/x509v3/v3_conf.c +2 -1
  390. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +3 -2
  391. data/third_party/boringssl/crypto/x509v3/v3_genn.c +1 -6
  392. data/third_party/boringssl/crypto/x509v3/v3_lib.c +1 -0
  393. data/third_party/boringssl/crypto/x509v3/v3_ocsp.c +68 -0
  394. data/third_party/boringssl/crypto/x509v3/v3_pci.c +2 -1
  395. data/third_party/boringssl/crypto/x509v3/v3_purp.c +47 -69
  396. data/third_party/boringssl/crypto/x509v3/v3_skey.c +5 -2
  397. data/third_party/boringssl/crypto/x509v3/v3_utl.c +69 -25
  398. data/third_party/boringssl/include/openssl/aead.h +45 -19
  399. data/third_party/boringssl/include/openssl/aes.h +32 -7
  400. data/third_party/boringssl/include/openssl/asn1.h +7 -77
  401. data/third_party/boringssl/include/openssl/base.h +120 -6
  402. data/third_party/boringssl/include/openssl/base64.h +4 -1
  403. data/third_party/boringssl/include/openssl/bio.h +112 -81
  404. data/third_party/boringssl/include/openssl/blowfish.h +3 -3
  405. data/third_party/boringssl/include/openssl/bn.h +55 -29
  406. data/third_party/boringssl/include/openssl/buf.h +2 -2
  407. data/third_party/boringssl/include/openssl/bytestring.h +54 -32
  408. data/third_party/boringssl/include/openssl/cast.h +2 -2
  409. data/third_party/boringssl/include/openssl/cipher.h +46 -16
  410. data/third_party/boringssl/include/openssl/cmac.h +6 -2
  411. data/third_party/boringssl/include/openssl/conf.h +3 -6
  412. data/third_party/boringssl/include/openssl/cpu.h +25 -9
  413. data/third_party/boringssl/include/openssl/crypto.h +32 -10
  414. data/third_party/boringssl/include/openssl/curve25519.h +4 -4
  415. data/third_party/boringssl/include/openssl/dh.h +3 -2
  416. data/third_party/boringssl/include/openssl/digest.h +21 -7
  417. data/third_party/boringssl/include/openssl/dsa.h +8 -2
  418. data/third_party/boringssl/include/openssl/e_os2.h +18 -0
  419. data/third_party/boringssl/include/openssl/ec.h +25 -21
  420. data/third_party/boringssl/include/openssl/ec_key.h +36 -8
  421. data/third_party/boringssl/include/openssl/ecdh.h +17 -0
  422. data/third_party/boringssl/include/openssl/ecdsa.h +3 -3
  423. data/third_party/boringssl/include/openssl/engine.h +4 -4
  424. data/third_party/boringssl/include/openssl/err.h +3 -0
  425. data/third_party/boringssl/include/openssl/evp.h +199 -42
  426. data/third_party/boringssl/include/openssl/hmac.h +4 -4
  427. data/third_party/boringssl/include/openssl/hrss.h +100 -0
  428. data/third_party/boringssl/include/openssl/lhash.h +131 -23
  429. data/third_party/boringssl/include/openssl/md4.h +6 -4
  430. data/third_party/boringssl/include/openssl/md5.h +6 -4
  431. data/third_party/boringssl/include/openssl/mem.h +6 -2
  432. data/third_party/boringssl/include/openssl/nid.h +3 -0
  433. data/third_party/boringssl/include/openssl/obj.h +3 -0
  434. data/third_party/boringssl/include/openssl/pem.h +102 -64
  435. data/third_party/boringssl/include/openssl/pkcs7.h +136 -3
  436. data/third_party/boringssl/include/openssl/pkcs8.h +42 -3
  437. data/third_party/boringssl/include/openssl/pool.h +13 -2
  438. data/third_party/boringssl/include/openssl/ripemd.h +5 -4
  439. data/third_party/boringssl/include/openssl/rsa.h +46 -15
  440. data/third_party/boringssl/include/openssl/sha.h +40 -28
  441. data/third_party/boringssl/include/openssl/siphash.h +37 -0
  442. data/third_party/boringssl/include/openssl/span.h +17 -9
  443. data/third_party/boringssl/include/openssl/ssl.h +766 -393
  444. data/third_party/boringssl/include/openssl/ssl3.h +4 -3
  445. data/third_party/boringssl/include/openssl/stack.h +134 -77
  446. data/third_party/boringssl/include/openssl/thread.h +1 -1
  447. data/third_party/boringssl/include/openssl/tls1.h +25 -9
  448. data/third_party/boringssl/include/openssl/type_check.h +14 -15
  449. data/third_party/boringssl/include/openssl/x509.h +28 -3
  450. data/third_party/boringssl/include/openssl/x509_vfy.h +98 -32
  451. data/third_party/boringssl/include/openssl/x509v3.h +17 -13
  452. data/third_party/boringssl/ssl/d1_both.cc +9 -18
  453. data/third_party/boringssl/ssl/d1_lib.cc +4 -3
  454. data/third_party/boringssl/ssl/d1_pkt.cc +4 -4
  455. data/third_party/boringssl/ssl/d1_srtp.cc +15 -15
  456. data/third_party/boringssl/ssl/dtls_method.cc +0 -1
  457. data/third_party/boringssl/ssl/dtls_record.cc +28 -28
  458. data/third_party/boringssl/ssl/handoff.cc +295 -91
  459. data/third_party/boringssl/ssl/handshake.cc +133 -72
  460. data/third_party/boringssl/ssl/handshake_client.cc +218 -189
  461. data/third_party/boringssl/ssl/handshake_server.cc +399 -272
  462. data/third_party/boringssl/ssl/internal.h +1413 -928
  463. data/third_party/boringssl/ssl/s3_both.cc +175 -36
  464. data/third_party/boringssl/ssl/s3_lib.cc +9 -13
  465. data/third_party/boringssl/ssl/s3_pkt.cc +63 -29
  466. data/third_party/boringssl/ssl/ssl_aead_ctx.cc +55 -35
  467. data/third_party/boringssl/ssl/ssl_asn1.cc +57 -73
  468. data/third_party/boringssl/ssl/ssl_buffer.cc +13 -12
  469. data/third_party/boringssl/ssl/ssl_cert.cc +313 -210
  470. data/third_party/boringssl/ssl/ssl_cipher.cc +159 -221
  471. data/third_party/boringssl/ssl/ssl_file.cc +2 -0
  472. data/third_party/boringssl/ssl/ssl_key_share.cc +164 -19
  473. data/third_party/boringssl/ssl/ssl_lib.cc +847 -555
  474. data/third_party/boringssl/ssl/ssl_privkey.cc +441 -111
  475. data/third_party/boringssl/ssl/ssl_session.cc +230 -178
  476. data/third_party/boringssl/ssl/ssl_transcript.cc +21 -142
  477. data/third_party/boringssl/ssl/ssl_versions.cc +88 -93
  478. data/third_party/boringssl/ssl/ssl_x509.cc +279 -218
  479. data/third_party/boringssl/ssl/t1_enc.cc +5 -96
  480. data/third_party/boringssl/ssl/t1_lib.cc +931 -678
  481. data/third_party/boringssl/ssl/tls13_both.cc +251 -121
  482. data/third_party/boringssl/ssl/tls13_client.cc +129 -73
  483. data/third_party/boringssl/ssl/tls13_enc.cc +350 -282
  484. data/third_party/boringssl/ssl/tls13_server.cc +259 -192
  485. data/third_party/boringssl/ssl/tls_method.cc +26 -21
  486. data/third_party/boringssl/ssl/tls_record.cc +42 -47
  487. data/third_party/boringssl/third_party/fiat/curve25519.c +261 -1324
  488. data/third_party/boringssl/third_party/fiat/curve25519_32.h +911 -0
  489. data/third_party/boringssl/third_party/fiat/curve25519_64.h +559 -0
  490. data/third_party/boringssl/third_party/fiat/p256.c +238 -999
  491. data/third_party/boringssl/third_party/fiat/p256_32.h +3226 -0
  492. data/third_party/boringssl/third_party/fiat/p256_64.h +1217 -0
  493. data/third_party/upb/upb/port_def.inc +1 -1
  494. data/third_party/upb/upb/table.c +2 -1
  495. metadata +71 -43
  496. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +0 -127
  497. data/src/core/lib/gpr/mpscq.cc +0 -117
  498. data/src/core/lib/gpr/mpscq.h +0 -88
  499. data/src/core/lib/gprpp/abstract.h +0 -47
  500. data/src/core/lib/gprpp/pair.h +0 -38
  501. data/third_party/boringssl/crypto/cipher_extra/e_ssl3.c +0 -460
  502. data/third_party/boringssl/crypto/fipsmodule/modes/ccm.c +0 -256
  503. data/third_party/boringssl/include/openssl/lhash_macros.h +0 -174
  504. data/third_party/boringssl/ssl/custom_extensions.cc +0 -265
@@ -22,17 +22,18 @@
22
22
 
23
23
  static uint64_t bn_neg_inv_mod_r_u64(uint64_t n);
24
24
 
25
- OPENSSL_COMPILE_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
26
- BN_MONT_CTX_N0_LIMBS_VALUE_INVALID_2);
27
- OPENSSL_COMPILE_ASSERT(sizeof(uint64_t) ==
28
- BN_MONT_CTX_N0_LIMBS * sizeof(BN_ULONG),
29
- BN_MONT_CTX_N0_LIMBS_DOES_NOT_MATCH_UINT64_T);
25
+ OPENSSL_STATIC_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
26
+ "BN_MONT_CTX_N0_LIMBS value is invalid");
27
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS ==
28
+ sizeof(uint64_t),
29
+ "uint64_t is insufficient precision for n0");
30
30
 
31
31
  // LG_LITTLE_R is log_2(r).
32
32
  #define LG_LITTLE_R (BN_MONT_CTX_N0_LIMBS * BN_BITS2)
33
33
 
34
34
  uint64_t bn_mont_n0(const BIGNUM *n) {
35
- // These conditions are checked by the caller, |BN_MONT_CTX_set|.
35
+ // These conditions are checked by the caller, |BN_MONT_CTX_set| or
36
+ // |BN_MONT_CTX_new_consttime|.
36
37
  assert(!BN_is_zero(n));
37
38
  assert(!BN_is_negative(n));
38
39
  assert(BN_is_odd(n));
@@ -57,6 +57,7 @@
57
57
  #include <openssl/bn.h>
58
58
 
59
59
  #include <assert.h>
60
+ #include <stdlib.h>
60
61
  #include <string.h>
61
62
 
62
63
  #include <openssl/err.h>
@@ -408,8 +409,8 @@ static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
408
409
  BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
409
410
  BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
410
411
  bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
411
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
412
- crypto_word_t_too_small);
412
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
413
+ "crypto_word_t is too small");
413
414
  c = constant_time_select_w(neg, c_neg, c_pos);
414
415
 
415
416
  // We now have our three components. Add them together.
@@ -522,8 +523,8 @@ static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a,
522
523
  BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
523
524
  BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
524
525
  bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
525
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
526
- crypto_word_t_too_small);
526
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
527
+ "crypto_word_t is too small");
527
528
  c = constant_time_select_w(neg, c_neg, c_pos);
528
529
 
529
530
  // We now have our three components. Add them together.
@@ -558,7 +559,7 @@ static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
558
559
  BN_CTX_start(ctx);
559
560
  if (r == a || r == b) {
560
561
  rr = BN_CTX_get(ctx);
561
- if (r == NULL) {
562
+ if (rr == NULL) {
562
563
  goto err;
563
564
  }
564
565
  } else {
@@ -656,11 +657,10 @@ int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
656
657
  return bn_mul_impl(r, a, b, ctx);
657
658
  }
658
659
 
659
- int bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
660
- const BN_ULONG *b, size_t num_b) {
660
+ void bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
661
+ const BN_ULONG *b, size_t num_b) {
661
662
  if (num_r != num_a + num_b) {
662
- OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
663
- return 0;
663
+ abort();
664
664
  }
665
665
  // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not
666
666
  // hit that code.
@@ -669,7 +669,6 @@ int bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
669
669
  } else {
670
670
  bn_mul_normal(r, a, num_a, b, num_b);
671
671
  }
672
- return 1;
673
672
  }
674
673
 
675
674
  // tmp must have 2*n words
@@ -858,10 +857,9 @@ int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
858
857
  return 1;
859
858
  }
860
859
 
861
- int bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) {
860
+ void bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) {
862
861
  if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) {
863
- OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
864
- return 0;
862
+ abort();
865
863
  }
866
864
  if (num_a == 4) {
867
865
  bn_sqr_comba4(r, a);
@@ -872,5 +870,4 @@ int bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) {
872
870
  bn_sqr_normal(r, a, num_a, tmp);
873
871
  OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG));
874
872
  }
875
- return 1;
876
873
  }
@@ -119,205 +119,148 @@
119
119
  // Zimmermann's, as implemented in PGP. I have had a read of his comments and
120
120
  // implemented my own version.
121
121
 
122
- #define NUMPRIMES 2048
123
-
124
- // primes contains all the primes that fit into a uint16_t.
125
- static const uint16_t primes[NUMPRIMES] = {
126
- 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
127
- 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
128
- 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
129
- 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
130
- 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
131
- 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
132
- 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389,
133
- 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457,
134
- 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523,
135
- 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
136
- 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
137
- 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
138
- 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
139
- 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
140
- 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
141
- 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049,
142
- 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117,
143
- 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
144
- 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289,
145
- 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373,
146
- 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453,
147
- 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531,
148
- 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607,
149
- 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
150
- 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777,
151
- 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
152
- 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951,
153
- 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029,
154
- 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113,
155
- 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213,
156
- 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293,
157
- 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377,
158
- 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447,
159
- 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
160
- 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659,
161
- 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713,
162
- 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797,
163
- 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887,
164
- 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971,
165
- 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079,
166
- 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
167
- 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
168
- 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359,
169
- 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461,
170
- 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539,
171
- 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617,
172
- 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701,
173
- 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797,
174
- 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889,
175
- 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
176
- 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073,
177
- 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157,
178
- 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253,
179
- 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349,
180
- 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451,
181
- 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547,
182
- 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643,
183
- 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
184
- 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817,
185
- 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937,
186
- 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009,
187
- 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101,
188
- 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209,
189
- 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309,
190
- 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417,
191
- 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501,
192
- 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581,
193
- 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683,
194
- 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783,
195
- 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857,
196
- 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953,
197
- 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073,
198
- 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163,
199
- 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263,
200
- 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337,
201
- 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427,
202
- 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553,
203
- 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659,
204
- 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737,
205
- 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833,
206
- 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947,
207
- 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013,
208
- 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127,
209
- 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229,
210
- 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333,
211
- 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477,
212
- 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547,
213
- 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621,
214
- 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717,
215
- 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829,
216
- 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927,
217
- 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053,
218
- 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147,
219
- 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237,
220
- 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329,
221
- 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443,
222
- 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563,
223
- 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663,
224
- 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737,
225
- 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831,
226
- 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933,
227
- 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029,
228
- 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137,
229
- 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227,
230
- 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337,
231
- 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421,
232
- 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497,
233
- 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623,
234
- 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721,
235
- 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811,
236
- 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901,
237
- 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037,
238
- 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133,
239
- 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223,
240
- 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313,
241
- 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429,
242
- 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529,
243
- 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639,
244
- 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733,
245
- 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859,
246
- 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957,
247
- 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071,
248
- 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171,
249
- 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279,
250
- 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393,
251
- 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491,
252
- 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617,
253
- 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731,
254
- 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831,
255
- 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933,
256
- 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037,
257
- 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119,
258
- 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241,
259
- 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343,
260
- 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437,
261
- 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527,
262
- 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613,
263
- 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713,
264
- 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823,
265
- 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923,
266
- 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009,
267
- 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127,
268
- 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229,
269
- 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337,
270
- 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457,
271
- 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577,
272
- 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687,
273
- 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759,
274
- 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877,
275
- 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967,
276
- 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083,
277
- 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221,
278
- 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347,
279
- 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447,
280
- 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551,
281
- 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653,
282
- 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747,
283
- 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831,
284
- 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939,
285
- 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073,
286
- 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161,
287
- 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269,
288
- 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349,
289
- 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443,
290
- 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559,
291
- 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649,
292
- 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749,
293
- 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859,
294
- 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959,
295
- 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069,
296
- 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187,
297
- 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301,
298
- 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421,
299
- 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529,
300
- 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649,
301
- 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747,
302
- 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883,
303
- 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981,
304
- 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077,
305
- 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191,
306
- 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321,
307
- 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401,
308
- 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491,
309
- 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599,
310
- 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729,
311
- 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839,
312
- 17851, 17863,
122
+ // kPrimes contains the first 1024 primes.
123
+ static const uint16_t kPrimes[] = {
124
+ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
125
+ 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
126
+ 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
127
+ 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
128
+ 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
129
+ 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
130
+ 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
131
+ 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
132
+ 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593,
133
+ 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
134
+ 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
135
+ 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827,
136
+ 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
137
+ 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,
138
+ 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069,
139
+ 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163,
140
+ 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249,
141
+ 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321,
142
+ 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439,
143
+ 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511,
144
+ 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601,
145
+ 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
146
+ 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783,
147
+ 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877,
148
+ 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987,
149
+ 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069,
150
+ 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143,
151
+ 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267,
152
+ 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347,
153
+ 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423,
154
+ 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543,
155
+ 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657,
156
+ 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713,
157
+ 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801,
158
+ 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903,
159
+ 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011,
160
+ 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119,
161
+ 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221,
162
+ 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323,
163
+ 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413,
164
+ 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527,
165
+ 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607,
166
+ 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697,
167
+ 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797,
168
+ 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907,
169
+ 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003,
170
+ 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093,
171
+ 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211,
172
+ 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283,
173
+ 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409,
174
+ 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513,
175
+ 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621,
176
+ 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721,
177
+ 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813,
178
+ 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937,
179
+ 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011,
180
+ 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113,
181
+ 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233,
182
+ 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351,
183
+ 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443,
184
+ 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531,
185
+ 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653,
186
+ 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743,
187
+ 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849,
188
+ 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939,
189
+ 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073,
190
+ 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173,
191
+ 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271,
192
+ 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359,
193
+ 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473,
194
+ 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581,
195
+ 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701,
196
+ 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803,
197
+ 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907,
198
+ 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997,
199
+ 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121,
200
+ 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229,
201
+ 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349,
202
+ 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487,
203
+ 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561,
204
+ 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669,
205
+ 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757,
206
+ 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879,
207
+ 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009,
208
+ 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111,
209
+ 8117, 8123, 8147, 8161,
313
210
  };
314
211
 
315
212
  // BN_prime_checks_for_size returns the number of Miller-Rabin iterations
316
- // necessary for a 'bits'-bit prime, in order to maintain an error rate greater
317
- // than the security level for an RSA prime of that many bits (calculated using
318
- // the FIPS SP 800-57 security level and 186-4 Section F.1; original paper:
319
- // Damgaard, Landrock, Pomerance: Average case error estimates for the strong
320
- // probable prime test. -- Math. Comp. 61 (1993) 177-194)
213
+ // necessary for a 'bits'-bit prime.
214
+ //
215
+ //
216
+ // This table is generated using the algorithm of FIPS PUB 186-4
217
+ // Digital Signature Standard (DSS), section F.1, page 117.
218
+ // (https://doi.org/10.6028/NIST.FIPS.186-4)
219
+ // The following magma script was used to generate the output:
220
+ // securitybits:=125;
221
+ // k:=1024;
222
+ // for t:=1 to 65 do
223
+ // for M:=3 to Floor(2*Sqrt(k-1)-1) do
224
+ // S:=0;
225
+ // // Sum over m
226
+ // for m:=3 to M do
227
+ // s:=0;
228
+ // // Sum over j
229
+ // for j:=2 to m do
230
+ // s+:=(RealField(32)!2)^-(j+(k-1)/j);
231
+ // end for;
232
+ // S+:=2^(m-(m-1)*t)*s;
233
+ // end for;
234
+ // A:=2^(k-2-M*t);
235
+ // B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S;
236
+ // pkt:=2.00743*Log(2)*k*2^-k*(A+B);
237
+ // seclevel:=Floor(-Log(2,pkt));
238
+ // if seclevel ge securitybits then
239
+ // printf "k: %5o, security: %o bits (t: %o, M: %o)\n",k,seclevel,t,M;
240
+ // break;
241
+ // end if;
242
+ // end for;
243
+ // if seclevel ge securitybits then break; end if;
244
+ // end for;
245
+ //
246
+ // It can be run online at: http://magma.maths.usyd.edu.au/calc
247
+ // And will output:
248
+ // k: 1024, security: 129 bits (t: 6, M: 23)
249
+ // k is the number of bits of the prime, securitybits is the level we want to
250
+ // reach.
251
+ // prime length | RSA key size | # MR tests | security level
252
+ // -------------+--------------|------------+---------------
253
+ // (b) >= 6394 | >= 12788 | 3 | 256 bit
254
+ // (b) >= 3747 | >= 7494 | 3 | 192 bit
255
+ // (b) >= 1345 | >= 2690 | 4 | 128 bit
256
+ // (b) >= 1080 | >= 2160 | 5 | 128 bit
257
+ // (b) >= 852 | >= 1704 | 5 | 112 bit
258
+ // (b) >= 476 | >= 952 | 5 | 80 bit
259
+ // (b) >= 400 | >= 800 | 6 | 80 bit
260
+ // (b) >= 347 | >= 694 | 7 | 80 bit
261
+ // (b) >= 308 | >= 616 | 8 | 80 bit
262
+ // (b) >= 55 | >= 110 | 27 | 64 bit
263
+ // (b) >= 6 | >= 12 | 34 | 64 bit
321
264
  static int BN_prime_checks_for_size(int bits) {
322
265
  if (bits >= 3747) {
323
266
  return 3;
@@ -331,16 +274,26 @@ static int BN_prime_checks_for_size(int bits) {
331
274
  if (bits >= 400) {
332
275
  return 6;
333
276
  }
277
+ if (bits >= 347) {
278
+ return 7;
279
+ }
334
280
  if (bits >= 308) {
335
281
  return 8;
336
282
  }
337
- if (bits >= 205) {
338
- return 13;
283
+ if (bits >= 55) {
284
+ return 27;
339
285
  }
340
- if (bits >= 155) {
341
- return 19;
286
+ return 34;
287
+ }
288
+
289
+ // num_trial_division_primes returns the number of primes to try with trial
290
+ // division before using more expensive checks. For larger numbers, the value
291
+ // of excluding a candidate with trial division is larger.
292
+ static size_t num_trial_division_primes(const BIGNUM *n) {
293
+ if (n->width * BN_BITS2 > 1024) {
294
+ return OPENSSL_ARRAY_SIZE(kPrimes);
342
295
  }
343
- return 28;
296
+ return OPENSSL_ARRAY_SIZE(kPrimes) / 2;
344
297
  }
345
298
 
346
299
  // BN_PRIME_CHECKS_BLINDED is the iteration count for blinding the constant-time
@@ -490,6 +443,11 @@ loop:
490
443
  goto err;
491
444
  }
492
445
 
446
+ // Interleave |ret| and |t|'s primality tests to avoid paying the full
447
+ // iteration count on |ret| only to quickly discover |t| is composite.
448
+ //
449
+ // TODO(davidben): This doesn't quite work because an iteration count of 1
450
+ // still runs the blinding mechanism.
493
451
  for (i = 0; i < checks; i++) {
494
452
  j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, NULL);
495
453
  if (j == -1) {
@@ -505,7 +463,7 @@ loop:
505
463
  goto loop;
506
464
  }
507
465
 
508
- if (!BN_GENCB_call(cb, i, c1 - 1)) {
466
+ if (!BN_GENCB_call(cb, BN_GENCB_PRIME_TEST, i)) {
509
467
  goto err;
510
468
  }
511
469
  // We have a safe prime test pass
@@ -524,96 +482,174 @@ err:
524
482
  return found;
525
483
  }
526
484
 
527
- // The following functions use a Barrett reduction variant to avoid leaking the
528
- // numerator. See http://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
529
- //
530
- // We use 32-bit numerator and 16-bit divisor for simplicity. This allows
531
- // computing |m| and |q| without architecture-specific code.
532
-
533
- // mod_u16 returns |n| mod |d|. |p| and |m| are the "magic numbers" for |d| (see
534
- // reference). For proof of correctness in Coq, see
535
- // https://github.com/davidben/fiat-crypto/blob/barrett/src/Arithmetic/BarrettReduction/RidiculousFish.v
536
- // Note the Coq version of |mod_u16| additionally includes the computation of
537
- // |p| and |m| from |bn_mod_u16_consttime| below.
538
- static uint16_t mod_u16(uint32_t n, uint16_t d, uint32_t p, uint32_t m) {
539
- // Compute floor(n/d) per steps 3 through 5.
540
- uint32_t q = ((uint64_t)m * n) >> 32;
541
- // Note there is a typo in the reference. We right-shift by one, not two.
542
- uint32_t t = ((n - q) >> 1) + q;
543
- t = t >> (p - 1);
544
-
545
- // Multiply and subtract to get the remainder.
546
- n -= d * t;
547
- assert(n < d);
548
- return n;
485
+ static int bn_trial_division(uint16_t *out, const BIGNUM *bn) {
486
+ const size_t num_primes = num_trial_division_primes(bn);
487
+ for (size_t i = 1; i < num_primes; i++) {
488
+ if (bn_mod_u16_consttime(bn, kPrimes[i]) == 0) {
489
+ *out = kPrimes[i];
490
+ return 1;
491
+ }
492
+ }
493
+ return 0;
549
494
  }
550
495
 
551
- // shift_and_add_mod_u16 returns |r| * 2^32 + |a| mod |d|. |p| and |m| are the
552
- // "magic numbers" for |d| (see reference).
553
- static uint16_t shift_and_add_mod_u16(uint16_t r, uint32_t a, uint16_t d,
554
- uint32_t p, uint32_t m) {
555
- // Incorporate |a| in two 16-bit chunks.
556
- uint32_t t = r;
557
- t <<= 16;
558
- t |= a >> 16;
559
- t = mod_u16(t, d, p, m);
560
-
561
- t <<= 16;
562
- t |= a & 0xffff;
563
- t = mod_u16(t, d, p, m);
564
- return t;
496
+ int bn_odd_number_is_obviously_composite(const BIGNUM *bn) {
497
+ uint16_t prime;
498
+ return bn_trial_division(&prime, bn) && !BN_is_word(bn, prime);
565
499
  }
566
500
 
567
- uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d) {
568
- if (d <= 1) {
501
+ int bn_miller_rabin_init(BN_MILLER_RABIN *miller_rabin, const BN_MONT_CTX *mont,
502
+ BN_CTX *ctx) {
503
+ // This function corresponds to steps 1 through 3 of FIPS 186-4, C.3.1.
504
+ const BIGNUM *w = &mont->N;
505
+ // Note we do not call |BN_CTX_start| in this function. We intentionally
506
+ // allocate values in the containing scope so they outlive this function.
507
+ miller_rabin->w1 = BN_CTX_get(ctx);
508
+ miller_rabin->m = BN_CTX_get(ctx);
509
+ miller_rabin->one_mont = BN_CTX_get(ctx);
510
+ miller_rabin->w1_mont = BN_CTX_get(ctx);
511
+ if (miller_rabin->w1 == NULL ||
512
+ miller_rabin->m == NULL ||
513
+ miller_rabin->one_mont == NULL ||
514
+ miller_rabin->w1_mont == NULL) {
569
515
  return 0;
570
516
  }
571
517
 
572
- // Compute the "magic numbers" for |d|. See steps 1 and 2.
573
- // This computes p = ceil(log_2(d)).
574
- uint32_t p = BN_num_bits_word(d - 1);
575
- // This operation is not constant-time, but |p| and |d| are public values.
576
- // Note that |p| is at most 16, so the computation fits in |uint64_t|.
577
- assert(p <= 16);
578
- uint32_t m = ((UINT64_C(1) << (32 + p)) + d - 1) / d;
579
-
580
- uint16_t ret = 0;
581
- for (int i = bn->width - 1; i >= 0; i--) {
582
- #if BN_BITS2 == 32
583
- ret = shift_and_add_mod_u16(ret, bn->d[i], d, p, m);
584
- #elif BN_BITS2 == 64
585
- ret = shift_and_add_mod_u16(ret, bn->d[i] >> 32, d, p, m);
586
- ret = shift_and_add_mod_u16(ret, bn->d[i] & 0xffffffff, d, p, m);
587
- #else
588
- #error "Unknown BN_ULONG size"
589
- #endif
518
+ // See FIPS 186-4, C.3.1, steps 1 through 3.
519
+ if (!bn_usub_consttime(miller_rabin->w1, w, BN_value_one())) {
520
+ return 0;
590
521
  }
591
- return ret;
522
+ miller_rabin->a = BN_count_low_zero_bits(miller_rabin->w1);
523
+ if (!bn_rshift_secret_shift(miller_rabin->m, miller_rabin->w1,
524
+ miller_rabin->a, ctx)) {
525
+ return 0;
526
+ }
527
+ miller_rabin->w_bits = BN_num_bits(w);
528
+
529
+ // Precompute some values in Montgomery form.
530
+ if (!bn_one_to_montgomery(miller_rabin->one_mont, mont, ctx) ||
531
+ // w - 1 is -1 mod w, so we can compute it in the Montgomery domain, -R,
532
+ // with a subtraction. (|one_mont| cannot be zero.)
533
+ !bn_usub_consttime(miller_rabin->w1_mont, w, miller_rabin->one_mont)) {
534
+ return 0;
535
+ }
536
+
537
+ return 1;
592
538
  }
593
539
 
594
- static int bn_trial_division(uint16_t *out, const BIGNUM *bn) {
595
- for (int i = 1; i < NUMPRIMES; i++) {
596
- if (bn_mod_u16_consttime(bn, primes[i]) == 0) {
597
- *out = primes[i];
598
- return 1;
540
+ int bn_miller_rabin_iteration(const BN_MILLER_RABIN *miller_rabin,
541
+ int *out_is_possibly_prime, const BIGNUM *b,
542
+ const BN_MONT_CTX *mont, BN_CTX *ctx) {
543
+ // This function corresponds to steps 4.3 through 4.5 of FIPS 186-4, C.3.1.
544
+ int ret = 0;
545
+ BN_CTX_start(ctx);
546
+
547
+ // Step 4.3. We use Montgomery-encoding for better performance and to avoid
548
+ // timing leaks.
549
+ const BIGNUM *w = &mont->N;
550
+ BIGNUM *z = BN_CTX_get(ctx);
551
+ if (z == NULL ||
552
+ !BN_mod_exp_mont_consttime(z, b, miller_rabin->m, w, ctx, mont) ||
553
+ !BN_to_montgomery(z, z, mont, ctx)) {
554
+ goto err;
555
+ }
556
+
557
+ // is_possibly_prime is all ones if we have determined |b| is not a composite
558
+ // witness for |w|. This is equivalent to going to step 4.7 in the original
559
+ // algorithm. To avoid timing leaks, we run the algorithm to the end for prime
560
+ // inputs.
561
+ crypto_word_t is_possibly_prime = 0;
562
+
563
+ // Step 4.4. If z = 1 or z = w-1, b is not a composite witness and w is still
564
+ // possibly prime.
565
+ is_possibly_prime = BN_equal_consttime(z, miller_rabin->one_mont) |
566
+ BN_equal_consttime(z, miller_rabin->w1_mont);
567
+ is_possibly_prime = 0 - is_possibly_prime; // Make it all zeros or all ones.
568
+
569
+ // Step 4.5.
570
+ //
571
+ // To avoid leaking |a|, we run the loop to |w_bits| and mask off all
572
+ // iterations once |j| = |a|.
573
+ for (int j = 1; j < miller_rabin->w_bits; j++) {
574
+ if (constant_time_eq_int(j, miller_rabin->a) & ~is_possibly_prime) {
575
+ // If the loop is done and we haven't seen z = 1 or z = w-1 yet, the
576
+ // value is composite and we can break in variable time.
577
+ break;
578
+ }
579
+
580
+ // Step 4.5.1.
581
+ if (!BN_mod_mul_montgomery(z, z, z, mont, ctx)) {
582
+ goto err;
583
+ }
584
+
585
+ // Step 4.5.2. If z = w-1 and the loop is not done, this is not a composite
586
+ // witness.
587
+ crypto_word_t z_is_w1_mont = BN_equal_consttime(z, miller_rabin->w1_mont);
588
+ z_is_w1_mont = 0 - z_is_w1_mont; // Make it all zeros or all ones.
589
+ is_possibly_prime |= z_is_w1_mont; // Go to step 4.7 if |z_is_w1_mont|.
590
+
591
+ // Step 4.5.3. If z = 1 and the loop is not done, the previous value of z
592
+ // was not -1. There are no non-trivial square roots of 1 modulo a prime, so
593
+ // w is composite and we may exit in variable time.
594
+ if (BN_equal_consttime(z, miller_rabin->one_mont) & ~is_possibly_prime) {
595
+ break;
599
596
  }
600
597
  }
601
- return 0;
602
- }
603
598
 
604
- int bn_odd_number_is_obviously_composite(const BIGNUM *bn) {
605
- uint16_t prime;
606
- return bn_trial_division(&prime, bn) && !BN_is_word(bn, prime);
599
+ *out_is_possibly_prime = is_possibly_prime & 1;
600
+ ret = 1;
601
+
602
+ err:
603
+ BN_CTX_end(ctx);
604
+ return ret;
607
605
  }
608
606
 
609
- int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
607
+ int BN_primality_test(int *out_is_probably_prime, const BIGNUM *w,
610
608
  int iterations, BN_CTX *ctx, int do_trial_division,
611
609
  BN_GENCB *cb) {
612
- *is_probably_prime = 0;
610
+ // This function's secrecy and performance requirements come from RSA key
611
+ // generation. We generate RSA keys by selecting two large, secret primes with
612
+ // rejection sampling.
613
+ //
614
+ // We thus treat |w| as secret if turns out to be a large prime. However, if
615
+ // |w| is composite, we treat this and |w| itself as public. (Conversely, if
616
+ // |w| is prime, that it is prime is public. Only the value is secret.) This
617
+ // is fine for RSA key generation, but note it is important that we use
618
+ // rejection sampling, with each candidate prime chosen independently. This
619
+ // would not work for, e.g., an algorithm which looked for primes in
620
+ // consecutive integers. These assumptions allow us to discard composites
621
+ // quickly. We additionally treat |w| as public when it is a small prime to
622
+ // simplify trial decryption and some edge cases.
623
+ //
624
+ // One RSA key generation will call this function on exactly two primes and
625
+ // many more composites. The overall cost is a combination of several factors:
626
+ //
627
+ // 1. Checking if |w| is divisible by a small prime is much faster than
628
+ // learning it is composite by Miller-Rabin (see below for details on that
629
+ // cost). Trial division by p saves 1/p of Miller-Rabin calls, so this is
630
+ // worthwhile until p exceeds the ratio of the two costs.
631
+ //
632
+ // 2. For a random (i.e. non-adversarial) candidate large prime and candidate
633
+ // witness, the probability of false witness is very low. (This is why FIPS
634
+ // 186-4 only requires a few iterations.) Thus composites not discarded by
635
+ // trial decryption, in practice, cost one Miller-Rabin iteration. Only the
636
+ // two actual primes cost the full iteration count.
637
+ //
638
+ // 3. A Miller-Rabin iteration is a modular exponentiation plus |a| additional
639
+ // modular squares, where |a| is the number of factors of two in |w-1|. |a|
640
+ // is likely small (the distribution falls exponentially), but it is also
641
+ // potentially secret, so we loop up to its log(w) upper bound when |w| is
642
+ // prime. When |w| is composite, we break early, so only two calls pay this
643
+ // cost. (Note that all calls pay the modular exponentiation which is,
644
+ // itself, log(w) modular multiplications and squares.)
645
+ //
646
+ // 4. While there are only two prime calls, they multiplicatively pay the full
647
+ // costs of (2) and (3).
648
+ //
649
+ // 5. After the primes are chosen, RSA keys derive some values from the
650
+ // primes, but this cost is negligible in comparison.
613
651
 
614
- // To support RSA key generation, this function should treat |w| as secret if
615
- // it is a large prime. Composite numbers are discarded, so they may return
616
- // early.
652
+ *out_is_probably_prime = 0;
617
653
 
618
654
  if (BN_cmp(w, BN_value_one()) <= 0) {
619
655
  return 1;
@@ -621,13 +657,13 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
621
657
 
622
658
  if (!BN_is_odd(w)) {
623
659
  // The only even prime is two.
624
- *is_probably_prime = BN_is_word(w, 2);
660
+ *out_is_probably_prime = BN_is_word(w, 2);
625
661
  return 1;
626
662
  }
627
663
 
628
664
  // Miller-Rabin does not work for three.
629
665
  if (BN_is_word(w, 3)) {
630
- *is_probably_prime = 1;
666
+ *out_is_probably_prime = 1;
631
667
  return 1;
632
668
  }
633
669
 
@@ -635,10 +671,10 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
635
671
  // Perform additional trial division checks to discard small primes.
636
672
  uint16_t prime;
637
673
  if (bn_trial_division(&prime, w)) {
638
- *is_probably_prime = BN_is_word(w, prime);
674
+ *out_is_probably_prime = BN_is_word(w, prime);
639
675
  return 1;
640
676
  }
641
- if (!BN_GENCB_call(cb, 1, -1)) {
677
+ if (!BN_GENCB_call(cb, BN_GENCB_PRIME_TEST, -1)) {
642
678
  return 0;
643
679
  }
644
680
  }
@@ -647,38 +683,24 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
647
683
  iterations = BN_prime_checks_for_size(BN_num_bits(w));
648
684
  }
649
685
 
686
+ BN_CTX *new_ctx = NULL;
687
+ if (ctx == NULL) {
688
+ new_ctx = BN_CTX_new();
689
+ if (new_ctx == NULL) {
690
+ return 0;
691
+ }
692
+ ctx = new_ctx;
693
+ }
694
+
650
695
  // See C.3.1 from FIPS 186-4.
651
696
  int ret = 0;
652
- BN_MONT_CTX *mont = NULL;
653
697
  BN_CTX_start(ctx);
654
- BIGNUM *w1 = BN_CTX_get(ctx);
655
- if (w1 == NULL ||
656
- !bn_usub_consttime(w1, w, BN_value_one())) {
657
- goto err;
658
- }
659
-
660
- // Write w1 as m * 2^a (Steps 1 and 2).
661
- int w_len = BN_num_bits(w);
662
- int a = BN_count_low_zero_bits(w1);
663
- BIGNUM *m = BN_CTX_get(ctx);
664
- if (m == NULL ||
665
- !bn_rshift_secret_shift(m, w1, a, ctx)) {
666
- goto err;
667
- }
668
-
669
- // Montgomery setup for computations mod w. Additionally, compute 1 and w - 1
670
- // in the Montgomery domain for later comparisons.
671
698
  BIGNUM *b = BN_CTX_get(ctx);
672
- BIGNUM *z = BN_CTX_get(ctx);
673
- BIGNUM *one_mont = BN_CTX_get(ctx);
674
- BIGNUM *w1_mont = BN_CTX_get(ctx);
675
- mont = BN_MONT_CTX_new_for_modulus(w, ctx);
676
- if (b == NULL || z == NULL || one_mont == NULL || w1_mont == NULL ||
677
- mont == NULL ||
678
- !bn_one_to_montgomery(one_mont, mont, ctx) ||
679
- // w - 1 is -1 mod w, so we can compute it in the Montgomery domain, -R,
680
- // with a subtraction. (|one_mont| cannot be zero.)
681
- !bn_usub_consttime(w1_mont, w, one_mont)) {
699
+ BN_MONT_CTX *mont = BN_MONT_CTX_new_consttime(w, ctx);
700
+ BN_MILLER_RABIN miller_rabin;
701
+ if (b == NULL || mont == NULL ||
702
+ // Steps 1-3.
703
+ !bn_miller_rabin_init(&miller_rabin, mont, ctx)) {
682
704
  goto err;
683
705
  }
684
706
 
@@ -694,9 +716,9 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
694
716
  // discard out-of-range values. To avoid leaking information on |w|, we use
695
717
  // |bn_rand_secret_range| which, rather than discarding bad values, adjusts
696
718
  // them to be in range. Though not uniformly selected, these adjusted values
697
- // are still usable as Rabin-Miller checks.
719
+ // are still usable as Miller-Rabin checks.
698
720
  //
699
- // Rabin-Miller is already probabilistic, so we could reach the desired
721
+ // Miller-Rabin is already probabilistic, so we could reach the desired
700
722
  // confidence levels by just suitably increasing the iteration count. However,
701
723
  // to align with FIPS 186-4, we use a more pessimal analysis: we do not count
702
724
  // the non-uniform values towards the iteration count. As a result, this
@@ -716,86 +738,46 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w,
716
738
  for (int i = 1; (i <= BN_PRIME_CHECKS_BLINDED) |
717
739
  constant_time_lt_w(uniform_iterations, iterations);
718
740
  i++) {
741
+ // Step 4.1-4.2
719
742
  int is_uniform;
720
- if (// Step 4.1-4.2
721
- !bn_rand_secret_range(b, &is_uniform, 2, w1) ||
722
- // Step 4.3
723
- !BN_mod_exp_mont_consttime(z, b, m, w, ctx, mont)) {
724
- goto err;
743
+ if (!bn_rand_secret_range(b, &is_uniform, 2, miller_rabin.w1)) {
744
+ goto err;
725
745
  }
726
746
  uniform_iterations += is_uniform;
727
747
 
728
- // loop_done is all ones if the loop has completed and all zeros otherwise.
729
- crypto_word_t loop_done = 0;
730
- // next_iteration is all ones if we should continue to the next iteration
731
- // (|b| is not a composite witness for |w|). This is equivalent to going to
732
- // step 4.7 in the original algorithm.
733
- crypto_word_t next_iteration = 0;
734
-
735
- // Step 4.4. If z = 1 or z = w-1, mask off the loop and continue to the next
736
- // iteration (go to step 4.7).
737
- loop_done = BN_equal_consttime(z, BN_value_one()) |
738
- BN_equal_consttime(z, w1);
739
- loop_done = 0 - loop_done; // Make it all zeros or all ones.
740
- next_iteration = loop_done; // Go to step 4.7 if |loop_done|.
741
-
742
- // Step 4.5. We use Montgomery-encoding for better performance and to avoid
743
- // timing leaks.
744
- if (!BN_to_montgomery(z, z, mont, ctx)) {
748
+ // Steps 4.3-4.5
749
+ int is_possibly_prime = 0;
750
+ if (!bn_miller_rabin_iteration(&miller_rabin, &is_possibly_prime, b, mont,
751
+ ctx)) {
745
752
  goto err;
746
753
  }
747
754
 
748
- // To avoid leaking |a|, we run the loop to |w_len| and mask off all
749
- // iterations once |j| = |a|.
750
- for (int j = 1; j < w_len; j++) {
751
- loop_done |= constant_time_eq_int(j, a);
752
-
753
- // Step 4.5.1.
754
- if (!BN_mod_mul_montgomery(z, z, z, mont, ctx)) {
755
- goto err;
756
- }
757
-
758
- // Step 4.5.2. If z = w-1 and the loop is not done, run through the next
759
- // iteration.
760
- crypto_word_t z_is_w1_mont = BN_equal_consttime(z, w1_mont) & ~loop_done;
761
- z_is_w1_mont = 0 - z_is_w1_mont; // Make it all zeros or all ones.
762
- loop_done |= z_is_w1_mont;
763
- next_iteration |= z_is_w1_mont; // Go to step 4.7 if |z_is_w1_mont|.
764
-
765
- // Step 4.5.3. If z = 1 and the loop is not done, w is composite and we
766
- // may exit in variable time.
767
- if (BN_equal_consttime(z, one_mont) & ~loop_done) {
768
- assert(!next_iteration);
769
- break;
770
- }
771
- }
772
-
773
- if (!next_iteration) {
755
+ if (!is_possibly_prime) {
774
756
  // Step 4.6. We did not see z = w-1 before z = 1, so w must be composite.
775
- // (For any prime, the value of z immediately preceding 1 must be -1.
776
- // There are no non-trivial square roots of 1 modulo a prime.)
777
- *is_probably_prime = 0;
757
+ *out_is_probably_prime = 0;
778
758
  ret = 1;
779
759
  goto err;
780
760
  }
781
761
 
782
762
  // Step 4.7
783
- if (!BN_GENCB_call(cb, 1, i)) {
763
+ if (!BN_GENCB_call(cb, BN_GENCB_PRIME_TEST, i - 1)) {
784
764
  goto err;
785
765
  }
786
766
  }
787
767
 
788
768
  assert(uniform_iterations >= (crypto_word_t)iterations);
789
- *is_probably_prime = 1;
769
+ *out_is_probably_prime = 1;
790
770
  ret = 1;
791
771
 
792
772
  err:
793
773
  BN_MONT_CTX_free(mont);
794
774
  BN_CTX_end(ctx);
775
+ BN_CTX_free(new_ctx);
795
776
  return ret;
796
777
  }
797
778
 
798
- int BN_is_prime_ex(const BIGNUM *candidate, int checks, BN_CTX *ctx, BN_GENCB *cb) {
779
+ int BN_is_prime_ex(const BIGNUM *candidate, int checks, BN_CTX *ctx,
780
+ BN_GENCB *cb) {
799
781
  return BN_is_prime_fasttest_ex(candidate, checks, ctx, 0, cb);
800
782
  }
801
783
 
@@ -933,7 +915,7 @@ int BN_enhanced_miller_rabin_primality_test(
933
915
 
934
916
  loop:
935
917
  // Step 4.15
936
- if (!BN_GENCB_call(cb, 1, i)) {
918
+ if (!BN_GENCB_call(cb, BN_GENCB_PRIME_TEST, i - 1)) {
937
919
  goto err;
938
920
  }
939
921
  }
@@ -949,86 +931,17 @@ err:
949
931
  }
950
932
 
951
933
  static int probable_prime(BIGNUM *rnd, int bits) {
952
- int i;
953
- uint16_t mods[NUMPRIMES];
954
- BN_ULONG delta;
955
- BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
956
- char is_single_word = bits <= BN_BITS2;
957
-
958
- again:
959
- if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD)) {
960
- return 0;
961
- }
962
-
963
- // we now have a random number 'rnd' to test.
964
- for (i = 1; i < NUMPRIMES; i++) {
965
- mods[i] = bn_mod_u16_consttime(rnd, primes[i]);
966
- }
967
- // If bits is so small that it fits into a single word then we
968
- // additionally don't want to exceed that many bits.
969
- if (is_single_word) {
970
- BN_ULONG size_limit;
971
- if (bits == BN_BITS2) {
972
- // Avoid undefined behavior.
973
- size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
974
- } else {
975
- size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
976
- }
977
- if (size_limit < maxdelta) {
978
- maxdelta = size_limit;
979
- }
980
- }
981
- delta = 0;
982
-
983
- loop:
984
- if (is_single_word) {
985
- BN_ULONG rnd_word = BN_get_word(rnd);
986
-
987
- // In the case that the candidate prime is a single word then
988
- // we check that:
989
- // 1) It's greater than primes[i] because we shouldn't reject
990
- // 3 as being a prime number because it's a multiple of
991
- // three.
992
- // 2) That it's not a multiple of a known prime. We don't
993
- // check that rnd-1 is also coprime to all the known
994
- // primes because there aren't many small primes where
995
- // that's true.
996
- for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
997
- if ((mods[i] + delta) % primes[i] == 0) {
998
- delta += 2;
999
- if (delta > maxdelta) {
1000
- goto again;
1001
- }
1002
- goto loop;
1003
- }
1004
- }
1005
- } else {
1006
- for (i = 1; i < NUMPRIMES; i++) {
1007
- // check that rnd is not a prime and also
1008
- // that gcd(rnd-1,primes) == 1 (except for 2)
1009
- if (((mods[i] + delta) % primes[i]) <= 1) {
1010
- delta += 2;
1011
- if (delta > maxdelta) {
1012
- goto again;
1013
- }
1014
- goto loop;
1015
- }
934
+ do {
935
+ if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD)) {
936
+ return 0;
1016
937
  }
1017
- }
1018
-
1019
- if (!BN_add_word(rnd, delta)) {
1020
- return 0;
1021
- }
1022
- if (BN_num_bits(rnd) != (unsigned)bits) {
1023
- goto again;
1024
- }
1025
-
938
+ } while (bn_odd_number_is_obviously_composite(rnd));
1026
939
  return 1;
1027
940
  }
1028
941
 
1029
942
  static int probable_prime_dh(BIGNUM *rnd, int bits, const BIGNUM *add,
1030
943
  const BIGNUM *rem, BN_CTX *ctx) {
1031
- int i, ret = 0;
944
+ int ret = 0;
1032
945
  BIGNUM *t1;
1033
946
 
1034
947
  BN_CTX_start(ctx);
@@ -1059,10 +972,11 @@ static int probable_prime_dh(BIGNUM *rnd, int bits, const BIGNUM *add,
1059
972
  }
1060
973
  // we now have a random number 'rand' to test.
1061
974
 
975
+ const size_t num_primes = num_trial_division_primes(rnd);
1062
976
  loop:
1063
- for (i = 1; i < NUMPRIMES; i++) {
977
+ for (size_t i = 1; i < num_primes; i++) {
1064
978
  // check that rnd is a prime
1065
- if (bn_mod_u16_consttime(rnd, primes[i]) <= 1) {
979
+ if (bn_mod_u16_consttime(rnd, kPrimes[i]) <= 1) {
1066
980
  if (!BN_add(rnd, rnd, add)) {
1067
981
  goto err;
1068
982
  }
@@ -1079,7 +993,7 @@ err:
1079
993
 
1080
994
  static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
1081
995
  const BIGNUM *rem, BN_CTX *ctx) {
1082
- int i, ret = 0;
996
+ int ret = 0;
1083
997
  BIGNUM *t1, *qadd, *q;
1084
998
 
1085
999
  bits--;
@@ -1129,13 +1043,14 @@ static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
1129
1043
  goto err;
1130
1044
  }
1131
1045
 
1046
+ const size_t num_primes = num_trial_division_primes(p);
1132
1047
  loop:
1133
- for (i = 1; i < NUMPRIMES; i++) {
1048
+ for (size_t i = 1; i < num_primes; i++) {
1134
1049
  // check that p and q are prime
1135
1050
  // check that for p and q
1136
1051
  // gcd(p-1,primes) == 1 (except for 2)
1137
- if (bn_mod_u16_consttime(p, primes[i]) == 0 ||
1138
- bn_mod_u16_consttime(q, primes[i]) == 0) {
1052
+ if (bn_mod_u16_consttime(p, kPrimes[i]) == 0 ||
1053
+ bn_mod_u16_consttime(q, kPrimes[i]) == 0) {
1139
1054
  if (!BN_add(p, p, padd)) {
1140
1055
  goto err;
1141
1056
  }