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
@@ -65,7 +65,7 @@
65
65
  #include "internal.h"
66
66
 
67
67
 
68
- namespace bssl {
68
+ BSSL_NAMESPACE_BEGIN
69
69
 
70
70
  static void ssl3_on_handshake_complete(SSL *ssl) {
71
71
  // The handshake should have released its final message.
@@ -95,6 +95,10 @@ static bool ssl3_set_read_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
95
95
  }
96
96
 
97
97
  static bool ssl3_set_write_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
98
+ if (!tls_flush_pending_hs_data(ssl)) {
99
+ return false;
100
+ }
101
+
98
102
  OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
99
103
  ssl->s3->aead_write_ctx = std::move(aead_ctx);
100
104
  return true;
@@ -115,16 +119,15 @@ static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
115
119
  ssl3_finish_message,
116
120
  ssl3_add_message,
117
121
  ssl3_add_change_cipher_spec,
118
- ssl3_add_alert,
119
122
  ssl3_flush_flight,
120
123
  ssl3_on_handshake_complete,
121
124
  ssl3_set_read_state,
122
125
  ssl3_set_write_state,
123
126
  };
124
127
 
125
- static int ssl_noop_x509_check_client_CA_names(
128
+ static bool ssl_noop_x509_check_client_CA_names(
126
129
  STACK_OF(CRYPTO_BUFFER) *names) {
127
- return 1;
130
+ return true;
128
131
  }
129
132
 
130
133
  static void ssl_noop_x509_clear(CERT *cert) {}
@@ -132,27 +135,29 @@ static void ssl_noop_x509_free(CERT *cert) {}
132
135
  static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
133
136
  static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
134
137
  static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
135
- static int ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
136
- return 1;
138
+ static bool ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
139
+ return true;
137
140
  }
138
- static int ssl_noop_x509_session_dup(SSL_SESSION *new_session,
139
- const SSL_SESSION *session) {
140
- return 1;
141
+ static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session,
142
+ const SSL_SESSION *session) {
143
+ return true;
141
144
  }
142
145
  static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
143
- static int ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
144
- SSL *ssl,
145
- uint8_t *out_alert) {
146
- return 0;
146
+ static bool ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
147
+ SSL_HANDSHAKE *hs,
148
+ uint8_t *out_alert) {
149
+ return false;
147
150
  }
148
151
 
149
152
  static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
150
- static int ssl_noop_x509_ssl_new(SSL *ctx) { return 1; }
151
- static void ssl_noop_x509_ssl_free(SSL *ctx) { }
152
- static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL *ssl) {}
153
- static int ssl_noop_x509_ssl_auto_chain_if_needed(SSL *ssl) { return 1; }
154
- static int ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return 1; }
155
- static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) { }
153
+ static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return true; }
154
+ static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg) {}
155
+ static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {}
156
+ static bool ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
157
+ return true;
158
+ }
159
+ static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return true; }
160
+ static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) {}
156
161
  static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
157
162
 
158
163
  const SSL_X509_METHOD ssl_noop_x509_method = {
@@ -168,7 +173,7 @@ const SSL_X509_METHOD ssl_noop_x509_method = {
168
173
  ssl_noop_x509_session_verify_cert_chain,
169
174
  ssl_noop_x509_hs_flush_cached_ca_names,
170
175
  ssl_noop_x509_ssl_new,
171
- ssl_noop_x509_ssl_free,
176
+ ssl_noop_x509_ssl_config_free,
172
177
  ssl_noop_x509_ssl_flush_cached_client_CA,
173
178
  ssl_noop_x509_ssl_auto_chain_if_needed,
174
179
  ssl_noop_x509_ssl_ctx_new,
@@ -176,7 +181,7 @@ const SSL_X509_METHOD ssl_noop_x509_method = {
176
181
  ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
177
182
  };
178
183
 
179
- } // namespace bssl
184
+ BSSL_NAMESPACE_END
180
185
 
181
186
  using namespace bssl;
182
187
 
@@ -119,7 +119,7 @@
119
119
  #include "../crypto/internal.h"
120
120
 
121
121
 
122
- namespace bssl {
122
+ BSSL_NAMESPACE_BEGIN
123
123
 
124
124
  // kMaxEmptyRecords is the number of consecutive, empty records that will be
125
125
  // processed. Without this limit an attacker could send empty records at a
@@ -140,26 +140,26 @@ static const uint8_t kMaxWarningAlerts = 4;
140
140
 
141
141
  // ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
142
142
  // state needs record-splitting and zero otherwise.
143
- static int ssl_needs_record_splitting(const SSL *ssl) {
143
+ static bool ssl_needs_record_splitting(const SSL *ssl) {
144
144
  #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
145
145
  return !ssl->s3->aead_write_ctx->is_null_cipher() &&
146
146
  ssl->s3->aead_write_ctx->ProtocolVersion() < TLS1_1_VERSION &&
147
147
  (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
148
148
  SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher());
149
149
  #else
150
- return 0;
150
+ return false;
151
151
  #endif
152
152
  }
153
153
 
154
- int ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
154
+ bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
155
155
  for (size_t i = seq_len - 1; i < seq_len; i--) {
156
156
  ++seq[i];
157
157
  if (seq[i] != 0) {
158
- return 1;
158
+ return true;
159
159
  }
160
160
  }
161
161
  OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
162
- return 0;
162
+ return false;
163
163
  }
164
164
 
165
165
  size_t ssl_record_prefix_len(const SSL *ssl) {
@@ -258,8 +258,8 @@ ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
258
258
  return ssl_open_record_partial;
259
259
  }
260
260
 
261
- ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER,
262
- in.subspan(0, SSL3_RT_HEADER_LENGTH));
261
+ Span<const uint8_t> header = in.subspan(0, SSL3_RT_HEADER_LENGTH);
262
+ ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, header);
263
263
 
264
264
  *out_consumed = in.size() - CBS_len(&cbs);
265
265
 
@@ -288,7 +288,7 @@ ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
288
288
 
289
289
  // Decrypt the body in-place.
290
290
  if (!ssl->s3->aead_read_ctx->Open(
291
- out, type, version, ssl->s3->read_sequence,
291
+ out, type, version, ssl->s3->read_sequence, header,
292
292
  MakeSpan(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) {
293
293
  if (ssl->s3->skip_early_data && !ssl->s3->aead_read_ctx->is_null_cipher()) {
294
294
  ERR_clear_error();
@@ -373,30 +373,25 @@ ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
373
373
  return ssl_open_record_success;
374
374
  }
375
375
 
376
- static int do_seal_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
377
- uint8_t *out_suffix, uint8_t type, const uint8_t *in,
378
- const size_t in_len) {
376
+ static bool do_seal_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
377
+ uint8_t *out_suffix, uint8_t type, const uint8_t *in,
378
+ const size_t in_len) {
379
+ SSLAEADContext *aead = ssl->s3->aead_write_ctx.get();
379
380
  uint8_t *extra_in = NULL;
380
381
  size_t extra_in_len = 0;
381
- if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
382
- ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) {
382
+ if (!aead->is_null_cipher() &&
383
+ aead->ProtocolVersion() >= TLS1_3_VERSION) {
383
384
  // TLS 1.3 hides the actual record type inside the encrypted data.
384
385
  extra_in = &type;
385
386
  extra_in_len = 1;
386
387
  }
387
388
 
388
- size_t suffix_len;
389
- if (!ssl->s3->aead_write_ctx->SuffixLen(&suffix_len, in_len, extra_in_len)) {
389
+ size_t suffix_len, ciphertext_len;
390
+ if (!aead->SuffixLen(&suffix_len, in_len, extra_in_len) ||
391
+ !aead->CiphertextLen(&ciphertext_len, in_len, extra_in_len)) {
390
392
  OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
391
- return 0;
392
- }
393
- size_t ciphertext_len =
394
- ssl->s3->aead_write_ctx->ExplicitNonceLen() + suffix_len;
395
- if (ciphertext_len + in_len < ciphertext_len) {
396
- OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
397
- return 0;
393
+ return false;
398
394
  }
399
- ciphertext_len += in_len;
400
395
 
401
396
  assert(in == out || !buffers_alias(in, in_len, out, in_len));
402
397
  assert(!buffers_alias(in, in_len, out_prefix, ssl_record_prefix_len(ssl)));
@@ -408,28 +403,27 @@ static int do_seal_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
408
403
  out_prefix[0] = type;
409
404
  }
410
405
 
411
- uint16_t record_version = ssl->s3->aead_write_ctx->RecordVersion();
406
+ uint16_t record_version = aead->RecordVersion();
412
407
 
413
408
  out_prefix[1] = record_version >> 8;
414
409
  out_prefix[2] = record_version & 0xff;
415
410
  out_prefix[3] = ciphertext_len >> 8;
416
411
  out_prefix[4] = ciphertext_len & 0xff;
412
+ Span<const uint8_t> header = MakeSpan(out_prefix, SSL3_RT_HEADER_LENGTH);
417
413
 
418
- if (!ssl->s3->aead_write_ctx->SealScatter(
419
- out_prefix + SSL3_RT_HEADER_LENGTH, out, out_suffix, type,
420
- record_version, ssl->s3->write_sequence, in, in_len, extra_in,
421
- extra_in_len) ||
414
+ if (!aead->SealScatter(out_prefix + SSL3_RT_HEADER_LENGTH, out, out_suffix,
415
+ out_prefix[0], record_version, ssl->s3->write_sequence,
416
+ header, in, in_len, extra_in, extra_in_len) ||
422
417
  !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
423
- return 0;
418
+ return false;
424
419
  }
425
420
 
426
- ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER,
427
- MakeSpan(out_prefix, SSL3_RT_HEADER_LENGTH));
428
- return 1;
421
+ ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, header);
422
+ return true;
429
423
  }
430
424
 
431
425
  static size_t tls_seal_scatter_prefix_len(const SSL *ssl, uint8_t type,
432
- size_t in_len) {
426
+ size_t in_len) {
433
427
  size_t ret = SSL3_RT_HEADER_LENGTH;
434
428
  if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
435
429
  ssl_needs_record_splitting(ssl)) {
@@ -470,7 +464,7 @@ static bool tls_seal_scatter_suffix_len(const SSL *ssl, size_t *out_suffix_len,
470
464
  // returns one on success and zero on error. If enabled,
471
465
  // |tls_seal_scatter_record| implements TLS 1.0 CBC 1/n-1 record splitting and
472
466
  // may write two records concatenated.
473
- static int tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
467
+ static bool tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
474
468
  uint8_t *out_suffix, uint8_t type,
475
469
  const uint8_t *in, size_t in_len) {
476
470
  if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
@@ -484,13 +478,13 @@ static int tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
484
478
 
485
479
  if (!do_seal_record(ssl, out_prefix, split_body, split_suffix, type, in,
486
480
  1)) {
487
- return 0;
481
+ return false;
488
482
  }
489
483
 
490
484
  size_t split_record_suffix_len;
491
485
  if (!ssl->s3->aead_write_ctx->SuffixLen(&split_record_suffix_len, 1, 0)) {
492
486
  assert(false);
493
- return 0;
487
+ return false;
494
488
  }
495
489
  const size_t split_record_len = prefix_len + 1 + split_record_suffix_len;
496
490
  assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
@@ -502,24 +496,25 @@ static int tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
502
496
  uint8_t tmp_prefix[SSL3_RT_HEADER_LENGTH];
503
497
  if (!do_seal_record(ssl, tmp_prefix, out + 1, out_suffix, type, in + 1,
504
498
  in_len - 1)) {
505
- return 0;
499
+ return false;
506
500
  }
507
501
  assert(tls_seal_scatter_prefix_len(ssl, type, in_len) ==
508
502
  split_record_len + SSL3_RT_HEADER_LENGTH - 1);
509
503
  OPENSSL_memcpy(out_prefix + split_record_len, tmp_prefix,
510
504
  SSL3_RT_HEADER_LENGTH - 1);
511
505
  OPENSSL_memcpy(out, tmp_prefix + SSL3_RT_HEADER_LENGTH - 1, 1);
512
- return 1;
506
+ return true;
513
507
  }
514
508
 
515
509
  return do_seal_record(ssl, out_prefix, out, out_suffix, type, in, in_len);
516
510
  }
517
511
 
518
- int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out_len,
519
- uint8_t type, const uint8_t *in, size_t in_len) {
512
+ bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
513
+ size_t max_out_len, uint8_t type, const uint8_t *in,
514
+ size_t in_len) {
520
515
  if (buffers_alias(in, in_len, out, max_out_len)) {
521
516
  OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
522
- return 0;
517
+ return false;
523
518
  }
524
519
 
525
520
  const size_t prefix_len = tls_seal_scatter_prefix_len(ssl, type, in_len);
@@ -530,22 +525,22 @@ int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out_len,
530
525
  if (in_len + prefix_len < in_len ||
531
526
  prefix_len + in_len + suffix_len < prefix_len + in_len) {
532
527
  OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
533
- return 0;
528
+ return false;
534
529
  }
535
530
  if (max_out_len < in_len + prefix_len + suffix_len) {
536
531
  OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
537
- return 0;
532
+ return false;
538
533
  }
539
534
 
540
535
  uint8_t *prefix = out;
541
536
  uint8_t *body = out + prefix_len;
542
537
  uint8_t *suffix = body + in_len;
543
538
  if (!tls_seal_scatter_record(ssl, prefix, body, suffix, type, in, in_len)) {
544
- return 0;
539
+ return false;
545
540
  }
546
541
 
547
542
  *out_len = prefix_len + in_len + suffix_len;
548
- return 1;
543
+ return true;
549
544
  }
550
545
 
551
546
  enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
@@ -680,7 +675,7 @@ bool SealRecord(SSL *ssl, const Span<uint8_t> out_prefix,
680
675
  in.data(), in.size());
681
676
  }
682
677
 
683
- } // namespace bssl
678
+ BSSL_NAMESPACE_END
684
679
 
685
680
  using namespace bssl;
686
681
 
@@ -45,8 +45,14 @@
45
45
  // Various pre-computed constants.
46
46
  #include "./curve25519_tables.h"
47
47
 
48
+ #if defined(BORINGSSL_CURVE25519_64BIT)
49
+ #include "./curve25519_64.h"
50
+ #else
51
+ #include "./curve25519_32.h"
52
+ #endif // BORINGSSL_CURVE25519_64BIT
53
+
48
54
 
49
- // Low-level intrinsic operations (hand-written).
55
+ // Low-level intrinsic operations
50
56
 
51
57
  static uint64_t load_3(const uint8_t *in) {
52
58
  uint64_t result;
@@ -65,706 +71,111 @@ static uint64_t load_4(const uint8_t *in) {
65
71
  return result;
66
72
  }
67
73
 
68
- #if defined(BORINGSSL_CURVE25519_64BIT)
69
- static uint64_t load_8(const uint8_t *in) {
70
- uint64_t result;
71
- result = (uint64_t)in[0];
72
- result |= ((uint64_t)in[1]) << 8;
73
- result |= ((uint64_t)in[2]) << 16;
74
- result |= ((uint64_t)in[3]) << 24;
75
- result |= ((uint64_t)in[4]) << 32;
76
- result |= ((uint64_t)in[5]) << 40;
77
- result |= ((uint64_t)in[6]) << 48;
78
- result |= ((uint64_t)in[7]) << 56;
79
- return result;
80
- }
81
-
82
- static uint8_t /*bool*/ addcarryx_u51(uint8_t /*bool*/ c, uint64_t a,
83
- uint64_t b, uint64_t *low) {
84
- // This function extracts 51 bits of result and 1 bit of carry (52 total), so
85
- // a 64-bit intermediate is sufficient.
86
- uint64_t x = a + b + c;
87
- *low = x & ((UINT64_C(1) << 51) - 1);
88
- return (x >> 51) & 1;
89
- }
90
-
91
- static uint8_t /*bool*/ subborrow_u51(uint8_t /*bool*/ c, uint64_t a,
92
- uint64_t b, uint64_t *low) {
93
- // This function extracts 51 bits of result and 1 bit of borrow (52 total), so
94
- // a 64-bit intermediate is sufficient.
95
- uint64_t x = a - b - c;
96
- *low = x & ((UINT64_C(1) << 51) - 1);
97
- return x >> 63;
98
- }
99
-
100
- static uint64_t cmovznz64(uint64_t t, uint64_t z, uint64_t nz) {
101
- t = -!!t; // all set if nonzero, 0 if 0
102
- return (t&nz) | ((~t)&z);
103
- }
104
-
105
- #else
106
-
107
- static uint8_t /*bool*/ addcarryx_u25(uint8_t /*bool*/ c, uint32_t a,
108
- uint32_t b, uint32_t *low) {
109
- // This function extracts 25 bits of result and 1 bit of carry (26 total), so
110
- // a 32-bit intermediate is sufficient.
111
- uint32_t x = a + b + c;
112
- *low = x & ((1 << 25) - 1);
113
- return (x >> 25) & 1;
114
- }
115
-
116
- static uint8_t /*bool*/ addcarryx_u26(uint8_t /*bool*/ c, uint32_t a,
117
- uint32_t b, uint32_t *low) {
118
- // This function extracts 26 bits of result and 1 bit of carry (27 total), so
119
- // a 32-bit intermediate is sufficient.
120
- uint32_t x = a + b + c;
121
- *low = x & ((1 << 26) - 1);
122
- return (x >> 26) & 1;
123
- }
124
-
125
- static uint8_t /*bool*/ subborrow_u25(uint8_t /*bool*/ c, uint32_t a,
126
- uint32_t b, uint32_t *low) {
127
- // This function extracts 25 bits of result and 1 bit of borrow (26 total), so
128
- // a 32-bit intermediate is sufficient.
129
- uint32_t x = a - b - c;
130
- *low = x & ((1 << 25) - 1);
131
- return x >> 31;
132
- }
133
-
134
- static uint8_t /*bool*/ subborrow_u26(uint8_t /*bool*/ c, uint32_t a,
135
- uint32_t b, uint32_t *low) {
136
- // This function extracts 26 bits of result and 1 bit of borrow (27 total), so
137
- // a 32-bit intermediate is sufficient.
138
- uint32_t x = a - b - c;
139
- *low = x & ((1 << 26) - 1);
140
- return x >> 31;
141
- }
142
-
143
- static uint32_t cmovznz32(uint32_t t, uint32_t z, uint32_t nz) {
144
- t = -!!t; // all set if nonzero, 0 if 0
145
- return (t&nz) | ((~t)&z);
146
- }
147
-
148
- #endif
149
-
150
74
 
151
75
  // Field operations.
152
76
 
153
77
  #if defined(BORINGSSL_CURVE25519_64BIT)
154
78
 
155
- #define assert_fe(f) do { \
156
- for (unsigned _assert_fe_i = 0; _assert_fe_i< 5; _assert_fe_i++) { \
157
- assert(f[_assert_fe_i] < 1.125*(UINT64_C(1)<<51)); \
158
- } \
159
- } while (0)
160
-
161
- #define assert_fe_loose(f) do { \
162
- for (unsigned _assert_fe_i = 0; _assert_fe_i< 5; _assert_fe_i++) { \
163
- assert(f[_assert_fe_i] < 3.375*(UINT64_C(1)<<51)); \
164
- } \
165
- } while (0)
166
-
167
- #define assert_fe_frozen(f) do { \
168
- for (unsigned _assert_fe_i = 0; _assert_fe_i< 5; _assert_fe_i++) { \
169
- assert(f[_assert_fe_i] < (UINT64_C(1)<<51)); \
170
- } \
171
- } while (0)
172
-
173
- static void fe_frombytes_impl(uint64_t h[5], const uint8_t *s) {
174
- // Ignores top bit of s.
175
- uint64_t a0 = load_8(s);
176
- uint64_t a1 = load_8(s+8);
177
- uint64_t a2 = load_8(s+16);
178
- uint64_t a3 = load_8(s+24);
179
- // Use 51 bits, 64-51 = 13 left.
180
- h[0] = a0 & ((UINT64_C(1) << 51) - 1);
181
- // (64-51) + 38 = 13 + 38 = 51
182
- h[1] = (a0 >> 51) | ((a1 & ((UINT64_C(1) << 38) - 1)) << 13);
183
- // (64-38) + 25 = 26 + 25 = 51
184
- h[2] = (a1 >> 38) | ((a2 & ((UINT64_C(1) << 25) - 1)) << 26);
185
- // (64-25) + 12 = 39 + 12 = 51
186
- h[3] = (a2 >> 25) | ((a3 & ((UINT64_C(1) << 12) - 1)) << 39);
187
- // (64-12) = 52, ignore top bit
188
- h[4] = (a3 >> 12) & ((UINT64_C(1) << 51) - 1);
189
- assert_fe(h);
190
- }
191
-
192
- static void fe_frombytes(fe *h, const uint8_t *s) {
193
- fe_frombytes_impl(h->v, s);
194
- }
195
-
196
- static void fe_freeze(uint64_t out[5], const uint64_t in1[5]) {
197
- { const uint64_t x7 = in1[4];
198
- { const uint64_t x8 = in1[3];
199
- { const uint64_t x6 = in1[2];
200
- { const uint64_t x4 = in1[1];
201
- { const uint64_t x2 = in1[0];
202
- { uint64_t x10; uint8_t/*bool*/ x11 = subborrow_u51(0x0, x2, 0x7ffffffffffed, &x10);
203
- { uint64_t x13; uint8_t/*bool*/ x14 = subborrow_u51(x11, x4, 0x7ffffffffffff, &x13);
204
- { uint64_t x16; uint8_t/*bool*/ x17 = subborrow_u51(x14, x6, 0x7ffffffffffff, &x16);
205
- { uint64_t x19; uint8_t/*bool*/ x20 = subborrow_u51(x17, x8, 0x7ffffffffffff, &x19);
206
- { uint64_t x22; uint8_t/*bool*/ x23 = subborrow_u51(x20, x7, 0x7ffffffffffff, &x22);
207
- { uint64_t x24 = cmovznz64(x23, 0x0, 0xffffffffffffffffL);
208
- { uint64_t x25 = (x24 & 0x7ffffffffffed);
209
- { uint64_t x27; uint8_t/*bool*/ x28 = addcarryx_u51(0x0, x10, x25, &x27);
210
- { uint64_t x29 = (x24 & 0x7ffffffffffff);
211
- { uint64_t x31; uint8_t/*bool*/ x32 = addcarryx_u51(x28, x13, x29, &x31);
212
- { uint64_t x33 = (x24 & 0x7ffffffffffff);
213
- { uint64_t x35; uint8_t/*bool*/ x36 = addcarryx_u51(x32, x16, x33, &x35);
214
- { uint64_t x37 = (x24 & 0x7ffffffffffff);
215
- { uint64_t x39; uint8_t/*bool*/ x40 = addcarryx_u51(x36, x19, x37, &x39);
216
- { uint64_t x41 = (x24 & 0x7ffffffffffff);
217
- { uint64_t x43; addcarryx_u51(x40, x22, x41, &x43);
218
- out[0] = x27;
219
- out[1] = x31;
220
- out[2] = x35;
221
- out[3] = x39;
222
- out[4] = x43;
223
- }}}}}}}}}}}}}}}}}}}}}
224
- }
225
-
226
- static void fe_tobytes(uint8_t s[32], const fe *f) {
227
- assert_fe(f->v);
228
- uint64_t h[5];
229
- fe_freeze(h, f->v);
230
- assert_fe_frozen(h);
231
-
232
- s[0] = h[0] >> 0;
233
- s[1] = h[0] >> 8;
234
- s[2] = h[0] >> 16;
235
- s[3] = h[0] >> 24;
236
- s[4] = h[0] >> 32;
237
- s[5] = h[0] >> 40;
238
- s[6] = (h[0] >> 48) | (h[1] << 3);
239
- s[7] = h[1] >> 5;
240
- s[8] = h[1] >> 13;
241
- s[9] = h[1] >> 21;
242
- s[10] = h[1] >> 29;
243
- s[11] = h[1] >> 37;
244
- s[12] = (h[1] >> 45) | (h[2] << 6);
245
- s[13] = h[2] >> 2;
246
- s[14] = h[2] >> 10;
247
- s[15] = h[2] >> 18;
248
- s[16] = h[2] >> 26;
249
- s[17] = h[2] >> 34;
250
- s[18] = h[2] >> 42;
251
- s[19] = (h[2] >> 50) | (h[3] << 1);
252
- s[20] = h[3] >> 7;
253
- s[21] = h[3] >> 15;
254
- s[22] = h[3] >> 23;
255
- s[23] = h[3] >> 31;
256
- s[24] = h[3] >> 39;
257
- s[25] = (h[3] >> 47) | (h[4] << 4);
258
- s[26] = h[4] >> 4;
259
- s[27] = h[4] >> 12;
260
- s[28] = h[4] >> 20;
261
- s[29] = h[4] >> 28;
262
- s[30] = h[4] >> 36;
263
- s[31] = h[4] >> 44;
264
- }
265
-
266
- // h = 0
267
- static void fe_0(fe *h) {
268
- OPENSSL_memset(h, 0, sizeof(fe));
269
- }
270
-
271
- static void fe_loose_0(fe_loose *h) {
272
- OPENSSL_memset(h, 0, sizeof(fe_loose));
273
- }
79
+ typedef uint64_t fe_limb_t;
80
+ #define FE_NUM_LIMBS 5
274
81
 
275
- // h = 1
276
- static void fe_1(fe *h) {
277
- OPENSSL_memset(h, 0, sizeof(fe));
278
- h->v[0] = 1;
279
- }
280
-
281
- static void fe_loose_1(fe_loose *h) {
282
- OPENSSL_memset(h, 0, sizeof(fe_loose));
283
- h->v[0] = 1;
284
- }
285
-
286
- static void fe_add_impl(uint64_t out[5], const uint64_t in1[5], const uint64_t in2[5]) {
287
- { const uint64_t x10 = in1[4];
288
- { const uint64_t x11 = in1[3];
289
- { const uint64_t x9 = in1[2];
290
- { const uint64_t x7 = in1[1];
291
- { const uint64_t x5 = in1[0];
292
- { const uint64_t x18 = in2[4];
293
- { const uint64_t x19 = in2[3];
294
- { const uint64_t x17 = in2[2];
295
- { const uint64_t x15 = in2[1];
296
- { const uint64_t x13 = in2[0];
297
- out[0] = (x5 + x13);
298
- out[1] = (x7 + x15);
299
- out[2] = (x9 + x17);
300
- out[3] = (x11 + x19);
301
- out[4] = (x10 + x18);
302
- }}}}}}}}}}
303
- }
304
-
305
- // h = f + g
306
- // Can overlap h with f or g.
307
- static void fe_add(fe_loose *h, const fe *f, const fe *g) {
308
- assert_fe(f->v);
309
- assert_fe(g->v);
310
- fe_add_impl(h->v, f->v, g->v);
311
- assert_fe_loose(h->v);
312
- }
313
-
314
- static void fe_sub_impl(uint64_t out[5], const uint64_t in1[5], const uint64_t in2[5]) {
315
- { const uint64_t x10 = in1[4];
316
- { const uint64_t x11 = in1[3];
317
- { const uint64_t x9 = in1[2];
318
- { const uint64_t x7 = in1[1];
319
- { const uint64_t x5 = in1[0];
320
- { const uint64_t x18 = in2[4];
321
- { const uint64_t x19 = in2[3];
322
- { const uint64_t x17 = in2[2];
323
- { const uint64_t x15 = in2[1];
324
- { const uint64_t x13 = in2[0];
325
- out[0] = ((0xfffffffffffda + x5) - x13);
326
- out[1] = ((0xffffffffffffe + x7) - x15);
327
- out[2] = ((0xffffffffffffe + x9) - x17);
328
- out[3] = ((0xffffffffffffe + x11) - x19);
329
- out[4] = ((0xffffffffffffe + x10) - x18);
330
- }}}}}}}}}}
331
- }
332
-
333
- // h = f - g
334
- // Can overlap h with f or g.
335
- static void fe_sub(fe_loose *h, const fe *f, const fe *g) {
336
- assert_fe(f->v);
337
- assert_fe(g->v);
338
- fe_sub_impl(h->v, f->v, g->v);
339
- assert_fe_loose(h->v);
340
- }
341
-
342
- static void fe_carry_impl(uint64_t out[5], const uint64_t in1[5]) {
343
- { const uint64_t x7 = in1[4];
344
- { const uint64_t x8 = in1[3];
345
- { const uint64_t x6 = in1[2];
346
- { const uint64_t x4 = in1[1];
347
- { const uint64_t x2 = in1[0];
348
- { uint64_t x9 = (x2 >> 0x33);
349
- { uint64_t x10 = (x2 & 0x7ffffffffffff);
350
- { uint64_t x11 = (x9 + x4);
351
- { uint64_t x12 = (x11 >> 0x33);
352
- { uint64_t x13 = (x11 & 0x7ffffffffffff);
353
- { uint64_t x14 = (x12 + x6);
354
- { uint64_t x15 = (x14 >> 0x33);
355
- { uint64_t x16 = (x14 & 0x7ffffffffffff);
356
- { uint64_t x17 = (x15 + x8);
357
- { uint64_t x18 = (x17 >> 0x33);
358
- { uint64_t x19 = (x17 & 0x7ffffffffffff);
359
- { uint64_t x20 = (x18 + x7);
360
- { uint64_t x21 = (x20 >> 0x33);
361
- { uint64_t x22 = (x20 & 0x7ffffffffffff);
362
- { uint64_t x23 = (x10 + (0x13 * x21));
363
- { uint64_t x24 = (x23 >> 0x33);
364
- { uint64_t x25 = (x23 & 0x7ffffffffffff);
365
- { uint64_t x26 = (x24 + x13);
366
- { uint64_t x27 = (x26 >> 0x33);
367
- { uint64_t x28 = (x26 & 0x7ffffffffffff);
368
- out[0] = x25;
369
- out[1] = x28;
370
- out[2] = (x27 + x16);
371
- out[3] = x19;
372
- out[4] = x22;
373
- }}}}}}}}}}}}}}}}}}}}}}}}}
374
- }
375
-
376
- static void fe_carry(fe *h, const fe_loose* f) {
377
- assert_fe_loose(f->v);
378
- fe_carry_impl(h->v, f->v);
379
- assert_fe(h->v);
380
- }
381
-
382
- static void fe_mul_impl(uint64_t out[5], const uint64_t in1[5], const uint64_t in2[5]) {
383
- assert_fe_loose(in1);
384
- assert_fe_loose(in2);
385
- { const uint64_t x10 = in1[4];
386
- { const uint64_t x11 = in1[3];
387
- { const uint64_t x9 = in1[2];
388
- { const uint64_t x7 = in1[1];
389
- { const uint64_t x5 = in1[0];
390
- { const uint64_t x18 = in2[4];
391
- { const uint64_t x19 = in2[3];
392
- { const uint64_t x17 = in2[2];
393
- { const uint64_t x15 = in2[1];
394
- { const uint64_t x13 = in2[0];
395
- { uint128_t x20 = ((uint128_t)x5 * x13);
396
- { uint128_t x21 = (((uint128_t)x5 * x15) + ((uint128_t)x7 * x13));
397
- { uint128_t x22 = ((((uint128_t)x5 * x17) + ((uint128_t)x9 * x13)) + ((uint128_t)x7 * x15));
398
- { uint128_t x23 = (((((uint128_t)x5 * x19) + ((uint128_t)x11 * x13)) + ((uint128_t)x7 * x17)) + ((uint128_t)x9 * x15));
399
- { uint128_t x24 = ((((((uint128_t)x5 * x18) + ((uint128_t)x10 * x13)) + ((uint128_t)x11 * x15)) + ((uint128_t)x7 * x19)) + ((uint128_t)x9 * x17));
400
- { uint64_t x25 = (x10 * 0x13);
401
- { uint64_t x26 = (x7 * 0x13);
402
- { uint64_t x27 = (x9 * 0x13);
403
- { uint64_t x28 = (x11 * 0x13);
404
- { uint128_t x29 = ((((x20 + ((uint128_t)x25 * x15)) + ((uint128_t)x26 * x18)) + ((uint128_t)x27 * x19)) + ((uint128_t)x28 * x17));
405
- { uint128_t x30 = (((x21 + ((uint128_t)x25 * x17)) + ((uint128_t)x27 * x18)) + ((uint128_t)x28 * x19));
406
- { uint128_t x31 = ((x22 + ((uint128_t)x25 * x19)) + ((uint128_t)x28 * x18));
407
- { uint128_t x32 = (x23 + ((uint128_t)x25 * x18));
408
- { uint64_t x33 = (uint64_t) (x29 >> 0x33);
409
- { uint64_t x34 = ((uint64_t)x29 & 0x7ffffffffffff);
410
- { uint128_t x35 = (x33 + x30);
411
- { uint64_t x36 = (uint64_t) (x35 >> 0x33);
412
- { uint64_t x37 = ((uint64_t)x35 & 0x7ffffffffffff);
413
- { uint128_t x38 = (x36 + x31);
414
- { uint64_t x39 = (uint64_t) (x38 >> 0x33);
415
- { uint64_t x40 = ((uint64_t)x38 & 0x7ffffffffffff);
416
- { uint128_t x41 = (x39 + x32);
417
- { uint64_t x42 = (uint64_t) (x41 >> 0x33);
418
- { uint64_t x43 = ((uint64_t)x41 & 0x7ffffffffffff);
419
- { uint128_t x44 = (x42 + x24);
420
- { uint64_t x45 = (uint64_t) (x44 >> 0x33);
421
- { uint64_t x46 = ((uint64_t)x44 & 0x7ffffffffffff);
422
- { uint64_t x47 = (x34 + (0x13 * x45));
423
- { uint64_t x48 = (x47 >> 0x33);
424
- { uint64_t x49 = (x47 & 0x7ffffffffffff);
425
- { uint64_t x50 = (x48 + x37);
426
- { uint64_t x51 = (x50 >> 0x33);
427
- { uint64_t x52 = (x50 & 0x7ffffffffffff);
428
- out[0] = x49;
429
- out[1] = x52;
430
- out[2] = (x51 + x40);
431
- out[3] = x43;
432
- out[4] = x46;
433
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
434
- assert_fe(out);
435
- }
436
-
437
- static void fe_mul_ltt(fe_loose *h, const fe *f, const fe *g) {
438
- fe_mul_impl(h->v, f->v, g->v);
439
- }
440
-
441
- static void fe_mul_llt(fe_loose *h, const fe_loose *f, const fe *g) {
442
- fe_mul_impl(h->v, f->v, g->v);
443
- }
444
-
445
- static void fe_mul_ttt(fe *h, const fe *f, const fe *g) {
446
- fe_mul_impl(h->v, f->v, g->v);
447
- }
448
-
449
- static void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) {
450
- fe_mul_impl(h->v, f->v, g->v);
451
- }
452
-
453
- static void fe_mul_ttl(fe *h, const fe *f, const fe_loose *g) {
454
- fe_mul_impl(h->v, f->v, g->v);
455
- }
456
-
457
- static void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) {
458
- fe_mul_impl(h->v, f->v, g->v);
459
- }
460
-
461
- static void fe_sqr_impl(uint64_t out[5], const uint64_t in1[5]) {
462
- assert_fe_loose(in1);
463
- { const uint64_t x7 = in1[4];
464
- { const uint64_t x8 = in1[3];
465
- { const uint64_t x6 = in1[2];
466
- { const uint64_t x4 = in1[1];
467
- { const uint64_t x2 = in1[0];
468
- { uint64_t x9 = (x2 * 0x2);
469
- { uint64_t x10 = (x4 * 0x2);
470
- { uint64_t x11 = ((x6 * 0x2) * 0x13);
471
- { uint64_t x12 = (x7 * 0x13);
472
- { uint64_t x13 = (x12 * 0x2);
473
- { uint128_t x14 = ((((uint128_t)x2 * x2) + ((uint128_t)x13 * x4)) + ((uint128_t)x11 * x8));
474
- { uint128_t x15 = ((((uint128_t)x9 * x4) + ((uint128_t)x13 * x6)) + ((uint128_t)x8 * (x8 * 0x13)));
475
- { uint128_t x16 = ((((uint128_t)x9 * x6) + ((uint128_t)x4 * x4)) + ((uint128_t)x13 * x8));
476
- { uint128_t x17 = ((((uint128_t)x9 * x8) + ((uint128_t)x10 * x6)) + ((uint128_t)x7 * x12));
477
- { uint128_t x18 = ((((uint128_t)x9 * x7) + ((uint128_t)x10 * x8)) + ((uint128_t)x6 * x6));
478
- { uint64_t x19 = (uint64_t) (x14 >> 0x33);
479
- { uint64_t x20 = ((uint64_t)x14 & 0x7ffffffffffff);
480
- { uint128_t x21 = (x19 + x15);
481
- { uint64_t x22 = (uint64_t) (x21 >> 0x33);
482
- { uint64_t x23 = ((uint64_t)x21 & 0x7ffffffffffff);
483
- { uint128_t x24 = (x22 + x16);
484
- { uint64_t x25 = (uint64_t) (x24 >> 0x33);
485
- { uint64_t x26 = ((uint64_t)x24 & 0x7ffffffffffff);
486
- { uint128_t x27 = (x25 + x17);
487
- { uint64_t x28 = (uint64_t) (x27 >> 0x33);
488
- { uint64_t x29 = ((uint64_t)x27 & 0x7ffffffffffff);
489
- { uint128_t x30 = (x28 + x18);
490
- { uint64_t x31 = (uint64_t) (x30 >> 0x33);
491
- { uint64_t x32 = ((uint64_t)x30 & 0x7ffffffffffff);
492
- { uint64_t x33 = (x20 + (0x13 * x31));
493
- { uint64_t x34 = (x33 >> 0x33);
494
- { uint64_t x35 = (x33 & 0x7ffffffffffff);
495
- { uint64_t x36 = (x34 + x23);
496
- { uint64_t x37 = (x36 >> 0x33);
497
- { uint64_t x38 = (x36 & 0x7ffffffffffff);
498
- out[0] = x35;
499
- out[1] = x38;
500
- out[2] = (x37 + x26);
501
- out[3] = x29;
502
- out[4] = x32;
503
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
504
- assert_fe(out);
505
- }
82
+ // assert_fe asserts that |f| satisfies bounds:
83
+ //
84
+ // [[0x0 ~> 0x8cccccccccccc],
85
+ // [0x0 ~> 0x8cccccccccccc],
86
+ // [0x0 ~> 0x8cccccccccccc],
87
+ // [0x0 ~> 0x8cccccccccccc],
88
+ // [0x0 ~> 0x8cccccccccccc]]
89
+ //
90
+ // See comments in curve25519_64.h for which functions use these bounds for
91
+ // inputs or outputs.
92
+ #define assert_fe(f) \
93
+ do { \
94
+ for (unsigned _assert_fe_i = 0; _assert_fe_i < 5; _assert_fe_i++) { \
95
+ assert(f[_assert_fe_i] <= UINT64_C(0x8cccccccccccc)); \
96
+ } \
97
+ } while (0)
98
+
99
+ // assert_fe_loose asserts that |f| satisfies bounds:
100
+ //
101
+ // [[0x0 ~> 0x1a666666666664],
102
+ // [0x0 ~> 0x1a666666666664],
103
+ // [0x0 ~> 0x1a666666666664],
104
+ // [0x0 ~> 0x1a666666666664],
105
+ // [0x0 ~> 0x1a666666666664]]
106
+ //
107
+ // See comments in curve25519_64.h for which functions use these bounds for
108
+ // inputs or outputs.
109
+ #define assert_fe_loose(f) \
110
+ do { \
111
+ for (unsigned _assert_fe_i = 0; _assert_fe_i < 5; _assert_fe_i++) { \
112
+ assert(f[_assert_fe_i] <= UINT64_C(0x1a666666666664)); \
113
+ } \
114
+ } while (0)
506
115
 
507
- static void fe_sq_tl(fe *h, const fe_loose *f) {
508
- fe_sqr_impl(h->v, f->v);
509
- }
116
+ #else
510
117
 
511
- static void fe_sq_tt(fe *h, const fe *f) {
512
- fe_sqr_impl(h->v, f->v);
513
- }
118
+ typedef uint32_t fe_limb_t;
119
+ #define FE_NUM_LIMBS 10
514
120
 
515
- // Replace (f,g) with (g,f) if b == 1;
516
- // replace (f,g) with (f,g) if b == 0.
121
+ // assert_fe asserts that |f| satisfies bounds:
517
122
  //
518
- // Preconditions: b in {0,1}.
519
- static void fe_cswap(fe *f, fe *g, uint64_t b) {
520
- b = 0-b;
521
- for (unsigned i = 0; i < 5; i++) {
522
- uint64_t x = f->v[i] ^ g->v[i];
523
- x &= b;
524
- f->v[i] ^= x;
525
- g->v[i] ^= x;
526
- }
527
- }
528
-
529
- // NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0..
530
- static void fe_mul_121666_impl(uint64_t out[5], const uint64_t in1[5]) {
531
- { const uint64_t x10 = in1[4];
532
- { const uint64_t x11 = in1[3];
533
- { const uint64_t x9 = in1[2];
534
- { const uint64_t x7 = in1[1];
535
- { const uint64_t x5 = in1[0];
536
- { const uint64_t x18 = 0;
537
- { const uint64_t x19 = 0;
538
- { const uint64_t x17 = 0;
539
- { const uint64_t x15 = 0;
540
- { const uint64_t x13 = 121666;
541
- { uint128_t x20 = ((uint128_t)x5 * x13);
542
- { uint128_t x21 = (((uint128_t)x5 * x15) + ((uint128_t)x7 * x13));
543
- { uint128_t x22 = ((((uint128_t)x5 * x17) + ((uint128_t)x9 * x13)) + ((uint128_t)x7 * x15));
544
- { uint128_t x23 = (((((uint128_t)x5 * x19) + ((uint128_t)x11 * x13)) + ((uint128_t)x7 * x17)) + ((uint128_t)x9 * x15));
545
- { uint128_t x24 = ((((((uint128_t)x5 * x18) + ((uint128_t)x10 * x13)) + ((uint128_t)x11 * x15)) + ((uint128_t)x7 * x19)) + ((uint128_t)x9 * x17));
546
- { uint64_t x25 = (x10 * 0x13);
547
- { uint64_t x26 = (x7 * 0x13);
548
- { uint64_t x27 = (x9 * 0x13);
549
- { uint64_t x28 = (x11 * 0x13);
550
- { uint128_t x29 = ((((x20 + ((uint128_t)x25 * x15)) + ((uint128_t)x26 * x18)) + ((uint128_t)x27 * x19)) + ((uint128_t)x28 * x17));
551
- { uint128_t x30 = (((x21 + ((uint128_t)x25 * x17)) + ((uint128_t)x27 * x18)) + ((uint128_t)x28 * x19));
552
- { uint128_t x31 = ((x22 + ((uint128_t)x25 * x19)) + ((uint128_t)x28 * x18));
553
- { uint128_t x32 = (x23 + ((uint128_t)x25 * x18));
554
- { uint64_t x33 = (uint64_t) (x29 >> 0x33);
555
- { uint64_t x34 = ((uint64_t)x29 & 0x7ffffffffffff);
556
- { uint128_t x35 = (x33 + x30);
557
- { uint64_t x36 = (uint64_t) (x35 >> 0x33);
558
- { uint64_t x37 = ((uint64_t)x35 & 0x7ffffffffffff);
559
- { uint128_t x38 = (x36 + x31);
560
- { uint64_t x39 = (uint64_t) (x38 >> 0x33);
561
- { uint64_t x40 = ((uint64_t)x38 & 0x7ffffffffffff);
562
- { uint128_t x41 = (x39 + x32);
563
- { uint64_t x42 = (uint64_t) (x41 >> 0x33);
564
- { uint64_t x43 = ((uint64_t)x41 & 0x7ffffffffffff);
565
- { uint128_t x44 = (x42 + x24);
566
- { uint64_t x45 = (uint64_t) (x44 >> 0x33);
567
- { uint64_t x46 = ((uint64_t)x44 & 0x7ffffffffffff);
568
- { uint64_t x47 = (x34 + (0x13 * x45));
569
- { uint64_t x48 = (x47 >> 0x33);
570
- { uint64_t x49 = (x47 & 0x7ffffffffffff);
571
- { uint64_t x50 = (x48 + x37);
572
- { uint64_t x51 = (x50 >> 0x33);
573
- { uint64_t x52 = (x50 & 0x7ffffffffffff);
574
- out[0] = x49;
575
- out[1] = x52;
576
- out[2] = (x51 + x40);
577
- out[3] = x43;
578
- out[4] = x46;
579
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
580
- }
581
-
582
- static void fe_mul121666(fe *h, const fe_loose *f) {
583
- assert_fe_loose(f->v);
584
- fe_mul_121666_impl(h->v, f->v);
585
- assert_fe(h->v);
586
- }
123
+ // [[0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
124
+ // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
125
+ // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
126
+ // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
127
+ // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333]]
128
+ //
129
+ // See comments in curve25519_32.h for which functions use these bounds for
130
+ // inputs or outputs.
131
+ #define assert_fe(f) \
132
+ do { \
133
+ for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \
134
+ assert(f[_assert_fe_i] <= \
135
+ ((_assert_fe_i & 1) ? 0x2333333u : 0x4666666u)); \
136
+ } \
137
+ } while (0)
138
+
139
+ // assert_fe_loose asserts that |f| satisfies bounds:
140
+ //
141
+ // [[0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
142
+ // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
143
+ // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
144
+ // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
145
+ // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999]]
146
+ //
147
+ // See comments in curve25519_32.h for which functions use these bounds for
148
+ // inputs or outputs.
149
+ #define assert_fe_loose(f) \
150
+ do { \
151
+ for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \
152
+ assert(f[_assert_fe_i] <= \
153
+ ((_assert_fe_i & 1) ? 0x6999999u : 0xd333332u)); \
154
+ } \
155
+ } while (0)
587
156
 
588
- // Adapted from Fiat-synthesized |fe_sub_impl| with |out| = 0.
589
- static void fe_neg_impl(uint64_t out[5], const uint64_t in2[5]) {
590
- { const uint64_t x10 = 0;
591
- { const uint64_t x11 = 0;
592
- { const uint64_t x9 = 0;
593
- { const uint64_t x7 = 0;
594
- { const uint64_t x5 = 0;
595
- { const uint64_t x18 = in2[4];
596
- { const uint64_t x19 = in2[3];
597
- { const uint64_t x17 = in2[2];
598
- { const uint64_t x15 = in2[1];
599
- { const uint64_t x13 = in2[0];
600
- out[0] = ((0xfffffffffffda + x5) - x13);
601
- out[1] = ((0xffffffffffffe + x7) - x15);
602
- out[2] = ((0xffffffffffffe + x9) - x17);
603
- out[3] = ((0xffffffffffffe + x11) - x19);
604
- out[4] = ((0xffffffffffffe + x10) - x18);
605
- }}}}}}}}}}
606
- }
157
+ #endif // BORINGSSL_CURVE25519_64BIT
607
158
 
608
- // h = -f
609
- static void fe_neg(fe_loose *h, const fe *f) {
610
- assert_fe(f->v);
611
- fe_neg_impl(h->v, f->v);
612
- assert_fe_loose(h->v);
613
- }
159
+ OPENSSL_STATIC_ASSERT(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS,
160
+ "fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe");
614
161
 
615
- // Replace (f,g) with (g,g) if b == 1;
616
- // replace (f,g) with (f,g) if b == 0.
617
- //
618
- // Preconditions: b in {0,1}.
619
- static void fe_cmov(fe_loose *f, const fe_loose *g, uint64_t b) {
620
- b = 0-b;
621
- for (unsigned i = 0; i < 5; i++) {
622
- uint64_t x = f->v[i] ^ g->v[i];
623
- x &= b;
624
- f->v[i] ^= x;
625
- }
162
+ static void fe_frombytes_strict(fe *h, const uint8_t s[32]) {
163
+ // |fiat_25519_from_bytes| requires the top-most bit be clear.
164
+ assert((s[31] & 0x80) == 0);
165
+ fiat_25519_from_bytes(h->v, s);
166
+ assert_fe(h->v);
626
167
  }
627
168
 
628
- #else
629
-
630
- #define assert_fe(f) do { \
631
- for (unsigned _assert_fe_i = 0; _assert_fe_i< 10; _assert_fe_i++) { \
632
- assert(f[_assert_fe_i] < 1.125*(1<<(26-(_assert_fe_i&1)))); \
633
- } \
634
- } while (0)
635
-
636
- #define assert_fe_loose(f) do { \
637
- for (unsigned _assert_fe_i = 0; _assert_fe_i< 10; _assert_fe_i++) { \
638
- assert(f[_assert_fe_i] < 3.375*(1<<(26-(_assert_fe_i&1)))); \
639
- } \
640
- } while (0)
641
-
642
- #define assert_fe_frozen(f) do { \
643
- for (unsigned _assert_fe_i = 0; _assert_fe_i< 10; _assert_fe_i++) { \
644
- assert(f[_assert_fe_i] < (1u<<(26-(_assert_fe_i&1)))); \
645
- } \
646
- } while (0)
647
-
648
- static void fe_frombytes_impl(uint32_t h[10], const uint8_t *s) {
649
- // Ignores top bit of s.
650
- uint32_t a0 = load_4(s);
651
- uint32_t a1 = load_4(s+4);
652
- uint32_t a2 = load_4(s+8);
653
- uint32_t a3 = load_4(s+12);
654
- uint32_t a4 = load_4(s+16);
655
- uint32_t a5 = load_4(s+20);
656
- uint32_t a6 = load_4(s+24);
657
- uint32_t a7 = load_4(s+28);
658
- h[0] = a0&((1<<26)-1); // 26 used, 32-26 left. 26
659
- h[1] = (a0>>26) | ((a1&((1<<19)-1))<< 6); // (32-26) + 19 = 6+19 = 25
660
- h[2] = (a1>>19) | ((a2&((1<<13)-1))<<13); // (32-19) + 13 = 13+13 = 26
661
- h[3] = (a2>>13) | ((a3&((1<< 6)-1))<<19); // (32-13) + 6 = 19+ 6 = 25
662
- h[4] = (a3>> 6); // (32- 6) = 26
663
- h[5] = a4&((1<<25)-1); // 25
664
- h[6] = (a4>>25) | ((a5&((1<<19)-1))<< 7); // (32-25) + 19 = 7+19 = 26
665
- h[7] = (a5>>19) | ((a6&((1<<12)-1))<<13); // (32-19) + 12 = 13+12 = 25
666
- h[8] = (a6>>12) | ((a7&((1<< 6)-1))<<20); // (32-12) + 6 = 20+ 6 = 26
667
- h[9] = (a7>> 6)&((1<<25)-1); // 25
668
- assert_fe(h);
669
- }
670
-
671
- static void fe_frombytes(fe *h, const uint8_t *s) {
672
- fe_frombytes_impl(h->v, s);
673
- }
674
-
675
- static void fe_freeze(uint32_t out[10], const uint32_t in1[10]) {
676
- { const uint32_t x17 = in1[9];
677
- { const uint32_t x18 = in1[8];
678
- { const uint32_t x16 = in1[7];
679
- { const uint32_t x14 = in1[6];
680
- { const uint32_t x12 = in1[5];
681
- { const uint32_t x10 = in1[4];
682
- { const uint32_t x8 = in1[3];
683
- { const uint32_t x6 = in1[2];
684
- { const uint32_t x4 = in1[1];
685
- { const uint32_t x2 = in1[0];
686
- { uint32_t x20; uint8_t/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20);
687
- { uint32_t x23; uint8_t/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23);
688
- { uint32_t x26; uint8_t/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26);
689
- { uint32_t x29; uint8_t/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29);
690
- { uint32_t x32; uint8_t/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32);
691
- { uint32_t x35; uint8_t/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35);
692
- { uint32_t x38; uint8_t/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38);
693
- { uint32_t x41; uint8_t/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41);
694
- { uint32_t x44; uint8_t/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44);
695
- { uint32_t x47; uint8_t/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47);
696
- { uint32_t x49 = cmovznz32(x48, 0x0, 0xffffffff);
697
- { uint32_t x50 = (x49 & 0x3ffffed);
698
- { uint32_t x52; uint8_t/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52);
699
- { uint32_t x54 = (x49 & 0x1ffffff);
700
- { uint32_t x56; uint8_t/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56);
701
- { uint32_t x58 = (x49 & 0x3ffffff);
702
- { uint32_t x60; uint8_t/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60);
703
- { uint32_t x62 = (x49 & 0x1ffffff);
704
- { uint32_t x64; uint8_t/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64);
705
- { uint32_t x66 = (x49 & 0x3ffffff);
706
- { uint32_t x68; uint8_t/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68);
707
- { uint32_t x70 = (x49 & 0x1ffffff);
708
- { uint32_t x72; uint8_t/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72);
709
- { uint32_t x74 = (x49 & 0x3ffffff);
710
- { uint32_t x76; uint8_t/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76);
711
- { uint32_t x78 = (x49 & 0x1ffffff);
712
- { uint32_t x80; uint8_t/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80);
713
- { uint32_t x82 = (x49 & 0x3ffffff);
714
- { uint32_t x84; uint8_t/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84);
715
- { uint32_t x86 = (x49 & 0x1ffffff);
716
- { uint32_t x88; addcarryx_u25(x85, x47, x86, &x88);
717
- out[0] = x52;
718
- out[1] = x56;
719
- out[2] = x60;
720
- out[3] = x64;
721
- out[4] = x68;
722
- out[5] = x72;
723
- out[6] = x76;
724
- out[7] = x80;
725
- out[8] = x84;
726
- out[9] = x88;
727
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
169
+ static void fe_frombytes(fe *h, const uint8_t s[32]) {
170
+ uint8_t s_copy[32];
171
+ OPENSSL_memcpy(s_copy, s, 32);
172
+ s_copy[31] &= 0x7f;
173
+ fe_frombytes_strict(h, s_copy);
728
174
  }
729
175
 
730
176
  static void fe_tobytes(uint8_t s[32], const fe *f) {
731
177
  assert_fe(f->v);
732
- uint32_t h[10];
733
- fe_freeze(h, f->v);
734
- assert_fe_frozen(h);
735
-
736
- s[0] = h[0] >> 0;
737
- s[1] = h[0] >> 8;
738
- s[2] = h[0] >> 16;
739
- s[3] = (h[0] >> 24) | (h[1] << 2);
740
- s[4] = h[1] >> 6;
741
- s[5] = h[1] >> 14;
742
- s[6] = (h[1] >> 22) | (h[2] << 3);
743
- s[7] = h[2] >> 5;
744
- s[8] = h[2] >> 13;
745
- s[9] = (h[2] >> 21) | (h[3] << 5);
746
- s[10] = h[3] >> 3;
747
- s[11] = h[3] >> 11;
748
- s[12] = (h[3] >> 19) | (h[4] << 6);
749
- s[13] = h[4] >> 2;
750
- s[14] = h[4] >> 10;
751
- s[15] = h[4] >> 18;
752
- s[16] = h[5] >> 0;
753
- s[17] = h[5] >> 8;
754
- s[18] = h[5] >> 16;
755
- s[19] = (h[5] >> 24) | (h[6] << 1);
756
- s[20] = h[6] >> 7;
757
- s[21] = h[6] >> 15;
758
- s[22] = (h[6] >> 23) | (h[7] << 3);
759
- s[23] = h[7] >> 5;
760
- s[24] = h[7] >> 13;
761
- s[25] = (h[7] >> 21) | (h[8] << 4);
762
- s[26] = h[8] >> 4;
763
- s[27] = h[8] >> 12;
764
- s[28] = (h[8] >> 20) | (h[9] << 6);
765
- s[29] = h[9] >> 2;
766
- s[30] = h[9] >> 10;
767
- s[31] = h[9] >> 18;
178
+ fiat_25519_to_bytes(s, f->v);
768
179
  }
769
180
 
770
181
  // h = 0
@@ -787,272 +198,36 @@ static void fe_loose_1(fe_loose *h) {
787
198
  h->v[0] = 1;
788
199
  }
789
200
 
790
- static void fe_add_impl(uint32_t out[10], const uint32_t in1[10], const uint32_t in2[10]) {
791
- { const uint32_t x20 = in1[9];
792
- { const uint32_t x21 = in1[8];
793
- { const uint32_t x19 = in1[7];
794
- { const uint32_t x17 = in1[6];
795
- { const uint32_t x15 = in1[5];
796
- { const uint32_t x13 = in1[4];
797
- { const uint32_t x11 = in1[3];
798
- { const uint32_t x9 = in1[2];
799
- { const uint32_t x7 = in1[1];
800
- { const uint32_t x5 = in1[0];
801
- { const uint32_t x38 = in2[9];
802
- { const uint32_t x39 = in2[8];
803
- { const uint32_t x37 = in2[7];
804
- { const uint32_t x35 = in2[6];
805
- { const uint32_t x33 = in2[5];
806
- { const uint32_t x31 = in2[4];
807
- { const uint32_t x29 = in2[3];
808
- { const uint32_t x27 = in2[2];
809
- { const uint32_t x25 = in2[1];
810
- { const uint32_t x23 = in2[0];
811
- out[0] = (x5 + x23);
812
- out[1] = (x7 + x25);
813
- out[2] = (x9 + x27);
814
- out[3] = (x11 + x29);
815
- out[4] = (x13 + x31);
816
- out[5] = (x15 + x33);
817
- out[6] = (x17 + x35);
818
- out[7] = (x19 + x37);
819
- out[8] = (x21 + x39);
820
- out[9] = (x20 + x38);
821
- }}}}}}}}}}}}}}}}}}}}
822
- }
823
-
824
201
  // h = f + g
825
202
  // Can overlap h with f or g.
826
203
  static void fe_add(fe_loose *h, const fe *f, const fe *g) {
827
204
  assert_fe(f->v);
828
205
  assert_fe(g->v);
829
- fe_add_impl(h->v, f->v, g->v);
206
+ fiat_25519_add(h->v, f->v, g->v);
830
207
  assert_fe_loose(h->v);
831
208
  }
832
209
 
833
- static void fe_sub_impl(uint32_t out[10], const uint32_t in1[10], const uint32_t in2[10]) {
834
- { const uint32_t x20 = in1[9];
835
- { const uint32_t x21 = in1[8];
836
- { const uint32_t x19 = in1[7];
837
- { const uint32_t x17 = in1[6];
838
- { const uint32_t x15 = in1[5];
839
- { const uint32_t x13 = in1[4];
840
- { const uint32_t x11 = in1[3];
841
- { const uint32_t x9 = in1[2];
842
- { const uint32_t x7 = in1[1];
843
- { const uint32_t x5 = in1[0];
844
- { const uint32_t x38 = in2[9];
845
- { const uint32_t x39 = in2[8];
846
- { const uint32_t x37 = in2[7];
847
- { const uint32_t x35 = in2[6];
848
- { const uint32_t x33 = in2[5];
849
- { const uint32_t x31 = in2[4];
850
- { const uint32_t x29 = in2[3];
851
- { const uint32_t x27 = in2[2];
852
- { const uint32_t x25 = in2[1];
853
- { const uint32_t x23 = in2[0];
854
- out[0] = ((0x7ffffda + x5) - x23);
855
- out[1] = ((0x3fffffe + x7) - x25);
856
- out[2] = ((0x7fffffe + x9) - x27);
857
- out[3] = ((0x3fffffe + x11) - x29);
858
- out[4] = ((0x7fffffe + x13) - x31);
859
- out[5] = ((0x3fffffe + x15) - x33);
860
- out[6] = ((0x7fffffe + x17) - x35);
861
- out[7] = ((0x3fffffe + x19) - x37);
862
- out[8] = ((0x7fffffe + x21) - x39);
863
- out[9] = ((0x3fffffe + x20) - x38);
864
- }}}}}}}}}}}}}}}}}}}}
865
- }
866
-
867
210
  // h = f - g
868
211
  // Can overlap h with f or g.
869
212
  static void fe_sub(fe_loose *h, const fe *f, const fe *g) {
870
213
  assert_fe(f->v);
871
214
  assert_fe(g->v);
872
- fe_sub_impl(h->v, f->v, g->v);
215
+ fiat_25519_sub(h->v, f->v, g->v);
873
216
  assert_fe_loose(h->v);
874
217
  }
875
218
 
876
- static void fe_carry_impl(uint32_t out[10], const uint32_t in1[10]) {
877
- { const uint32_t x17 = in1[9];
878
- { const uint32_t x18 = in1[8];
879
- { const uint32_t x16 = in1[7];
880
- { const uint32_t x14 = in1[6];
881
- { const uint32_t x12 = in1[5];
882
- { const uint32_t x10 = in1[4];
883
- { const uint32_t x8 = in1[3];
884
- { const uint32_t x6 = in1[2];
885
- { const uint32_t x4 = in1[1];
886
- { const uint32_t x2 = in1[0];
887
- { uint32_t x19 = (x2 >> 0x1a);
888
- { uint32_t x20 = (x2 & 0x3ffffff);
889
- { uint32_t x21 = (x19 + x4);
890
- { uint32_t x22 = (x21 >> 0x19);
891
- { uint32_t x23 = (x21 & 0x1ffffff);
892
- { uint32_t x24 = (x22 + x6);
893
- { uint32_t x25 = (x24 >> 0x1a);
894
- { uint32_t x26 = (x24 & 0x3ffffff);
895
- { uint32_t x27 = (x25 + x8);
896
- { uint32_t x28 = (x27 >> 0x19);
897
- { uint32_t x29 = (x27 & 0x1ffffff);
898
- { uint32_t x30 = (x28 + x10);
899
- { uint32_t x31 = (x30 >> 0x1a);
900
- { uint32_t x32 = (x30 & 0x3ffffff);
901
- { uint32_t x33 = (x31 + x12);
902
- { uint32_t x34 = (x33 >> 0x19);
903
- { uint32_t x35 = (x33 & 0x1ffffff);
904
- { uint32_t x36 = (x34 + x14);
905
- { uint32_t x37 = (x36 >> 0x1a);
906
- { uint32_t x38 = (x36 & 0x3ffffff);
907
- { uint32_t x39 = (x37 + x16);
908
- { uint32_t x40 = (x39 >> 0x19);
909
- { uint32_t x41 = (x39 & 0x1ffffff);
910
- { uint32_t x42 = (x40 + x18);
911
- { uint32_t x43 = (x42 >> 0x1a);
912
- { uint32_t x44 = (x42 & 0x3ffffff);
913
- { uint32_t x45 = (x43 + x17);
914
- { uint32_t x46 = (x45 >> 0x19);
915
- { uint32_t x47 = (x45 & 0x1ffffff);
916
- { uint32_t x48 = (x20 + (0x13 * x46));
917
- { uint32_t x49 = (x48 >> 0x1a);
918
- { uint32_t x50 = (x48 & 0x3ffffff);
919
- { uint32_t x51 = (x49 + x23);
920
- { uint32_t x52 = (x51 >> 0x19);
921
- { uint32_t x53 = (x51 & 0x1ffffff);
922
- out[0] = x50;
923
- out[1] = x53;
924
- out[2] = (x52 + x26);
925
- out[3] = x29;
926
- out[4] = x32;
927
- out[5] = x35;
928
- out[6] = x38;
929
- out[7] = x41;
930
- out[8] = x44;
931
- out[9] = x47;
932
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
933
- }
934
-
935
219
  static void fe_carry(fe *h, const fe_loose* f) {
936
220
  assert_fe_loose(f->v);
937
- fe_carry_impl(h->v, f->v);
221
+ fiat_25519_carry(h->v, f->v);
938
222
  assert_fe(h->v);
939
223
  }
940
224
 
941
- static void fe_mul_impl(uint32_t out[10], const uint32_t in1[10], const uint32_t in2[10]) {
225
+ static void fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS],
226
+ const fe_limb_t in1[FE_NUM_LIMBS],
227
+ const fe_limb_t in2[FE_NUM_LIMBS]) {
942
228
  assert_fe_loose(in1);
943
229
  assert_fe_loose(in2);
944
- { const uint32_t x20 = in1[9];
945
- { const uint32_t x21 = in1[8];
946
- { const uint32_t x19 = in1[7];
947
- { const uint32_t x17 = in1[6];
948
- { const uint32_t x15 = in1[5];
949
- { const uint32_t x13 = in1[4];
950
- { const uint32_t x11 = in1[3];
951
- { const uint32_t x9 = in1[2];
952
- { const uint32_t x7 = in1[1];
953
- { const uint32_t x5 = in1[0];
954
- { const uint32_t x38 = in2[9];
955
- { const uint32_t x39 = in2[8];
956
- { const uint32_t x37 = in2[7];
957
- { const uint32_t x35 = in2[6];
958
- { const uint32_t x33 = in2[5];
959
- { const uint32_t x31 = in2[4];
960
- { const uint32_t x29 = in2[3];
961
- { const uint32_t x27 = in2[2];
962
- { const uint32_t x25 = in2[1];
963
- { const uint32_t x23 = in2[0];
964
- { uint64_t x40 = ((uint64_t)x23 * x5);
965
- { uint64_t x41 = (((uint64_t)x23 * x7) + ((uint64_t)x25 * x5));
966
- { uint64_t x42 = ((((uint64_t)(0x2 * x25) * x7) + ((uint64_t)x23 * x9)) + ((uint64_t)x27 * x5));
967
- { uint64_t x43 = (((((uint64_t)x25 * x9) + ((uint64_t)x27 * x7)) + ((uint64_t)x23 * x11)) + ((uint64_t)x29 * x5));
968
- { uint64_t x44 = (((((uint64_t)x27 * x9) + (0x2 * (((uint64_t)x25 * x11) + ((uint64_t)x29 * x7)))) + ((uint64_t)x23 * x13)) + ((uint64_t)x31 * x5));
969
- { uint64_t x45 = (((((((uint64_t)x27 * x11) + ((uint64_t)x29 * x9)) + ((uint64_t)x25 * x13)) + ((uint64_t)x31 * x7)) + ((uint64_t)x23 * x15)) + ((uint64_t)x33 * x5));
970
- { uint64_t x46 = (((((0x2 * ((((uint64_t)x29 * x11) + ((uint64_t)x25 * x15)) + ((uint64_t)x33 * x7))) + ((uint64_t)x27 * x13)) + ((uint64_t)x31 * x9)) + ((uint64_t)x23 * x17)) + ((uint64_t)x35 * x5));
971
- { uint64_t x47 = (((((((((uint64_t)x29 * x13) + ((uint64_t)x31 * x11)) + ((uint64_t)x27 * x15)) + ((uint64_t)x33 * x9)) + ((uint64_t)x25 * x17)) + ((uint64_t)x35 * x7)) + ((uint64_t)x23 * x19)) + ((uint64_t)x37 * x5));
972
- { uint64_t x48 = (((((((uint64_t)x31 * x13) + (0x2 * (((((uint64_t)x29 * x15) + ((uint64_t)x33 * x11)) + ((uint64_t)x25 * x19)) + ((uint64_t)x37 * x7)))) + ((uint64_t)x27 * x17)) + ((uint64_t)x35 * x9)) + ((uint64_t)x23 * x21)) + ((uint64_t)x39 * x5));
973
- { uint64_t x49 = (((((((((((uint64_t)x31 * x15) + ((uint64_t)x33 * x13)) + ((uint64_t)x29 * x17)) + ((uint64_t)x35 * x11)) + ((uint64_t)x27 * x19)) + ((uint64_t)x37 * x9)) + ((uint64_t)x25 * x21)) + ((uint64_t)x39 * x7)) + ((uint64_t)x23 * x20)) + ((uint64_t)x38 * x5));
974
- { uint64_t x50 = (((((0x2 * ((((((uint64_t)x33 * x15) + ((uint64_t)x29 * x19)) + ((uint64_t)x37 * x11)) + ((uint64_t)x25 * x20)) + ((uint64_t)x38 * x7))) + ((uint64_t)x31 * x17)) + ((uint64_t)x35 * x13)) + ((uint64_t)x27 * x21)) + ((uint64_t)x39 * x9));
975
- { uint64_t x51 = (((((((((uint64_t)x33 * x17) + ((uint64_t)x35 * x15)) + ((uint64_t)x31 * x19)) + ((uint64_t)x37 * x13)) + ((uint64_t)x29 * x21)) + ((uint64_t)x39 * x11)) + ((uint64_t)x27 * x20)) + ((uint64_t)x38 * x9));
976
- { uint64_t x52 = (((((uint64_t)x35 * x17) + (0x2 * (((((uint64_t)x33 * x19) + ((uint64_t)x37 * x15)) + ((uint64_t)x29 * x20)) + ((uint64_t)x38 * x11)))) + ((uint64_t)x31 * x21)) + ((uint64_t)x39 * x13));
977
- { uint64_t x53 = (((((((uint64_t)x35 * x19) + ((uint64_t)x37 * x17)) + ((uint64_t)x33 * x21)) + ((uint64_t)x39 * x15)) + ((uint64_t)x31 * x20)) + ((uint64_t)x38 * x13));
978
- { uint64_t x54 = (((0x2 * ((((uint64_t)x37 * x19) + ((uint64_t)x33 * x20)) + ((uint64_t)x38 * x15))) + ((uint64_t)x35 * x21)) + ((uint64_t)x39 * x17));
979
- { uint64_t x55 = (((((uint64_t)x37 * x21) + ((uint64_t)x39 * x19)) + ((uint64_t)x35 * x20)) + ((uint64_t)x38 * x17));
980
- { uint64_t x56 = (((uint64_t)x39 * x21) + (0x2 * (((uint64_t)x37 * x20) + ((uint64_t)x38 * x19))));
981
- { uint64_t x57 = (((uint64_t)x39 * x20) + ((uint64_t)x38 * x21));
982
- { uint64_t x58 = ((uint64_t)(0x2 * x38) * x20);
983
- { uint64_t x59 = (x48 + (x58 << 0x4));
984
- { uint64_t x60 = (x59 + (x58 << 0x1));
985
- { uint64_t x61 = (x60 + x58);
986
- { uint64_t x62 = (x47 + (x57 << 0x4));
987
- { uint64_t x63 = (x62 + (x57 << 0x1));
988
- { uint64_t x64 = (x63 + x57);
989
- { uint64_t x65 = (x46 + (x56 << 0x4));
990
- { uint64_t x66 = (x65 + (x56 << 0x1));
991
- { uint64_t x67 = (x66 + x56);
992
- { uint64_t x68 = (x45 + (x55 << 0x4));
993
- { uint64_t x69 = (x68 + (x55 << 0x1));
994
- { uint64_t x70 = (x69 + x55);
995
- { uint64_t x71 = (x44 + (x54 << 0x4));
996
- { uint64_t x72 = (x71 + (x54 << 0x1));
997
- { uint64_t x73 = (x72 + x54);
998
- { uint64_t x74 = (x43 + (x53 << 0x4));
999
- { uint64_t x75 = (x74 + (x53 << 0x1));
1000
- { uint64_t x76 = (x75 + x53);
1001
- { uint64_t x77 = (x42 + (x52 << 0x4));
1002
- { uint64_t x78 = (x77 + (x52 << 0x1));
1003
- { uint64_t x79 = (x78 + x52);
1004
- { uint64_t x80 = (x41 + (x51 << 0x4));
1005
- { uint64_t x81 = (x80 + (x51 << 0x1));
1006
- { uint64_t x82 = (x81 + x51);
1007
- { uint64_t x83 = (x40 + (x50 << 0x4));
1008
- { uint64_t x84 = (x83 + (x50 << 0x1));
1009
- { uint64_t x85 = (x84 + x50);
1010
- { uint64_t x86 = (x85 >> 0x1a);
1011
- { uint32_t x87 = ((uint32_t)x85 & 0x3ffffff);
1012
- { uint64_t x88 = (x86 + x82);
1013
- { uint64_t x89 = (x88 >> 0x19);
1014
- { uint32_t x90 = ((uint32_t)x88 & 0x1ffffff);
1015
- { uint64_t x91 = (x89 + x79);
1016
- { uint64_t x92 = (x91 >> 0x1a);
1017
- { uint32_t x93 = ((uint32_t)x91 & 0x3ffffff);
1018
- { uint64_t x94 = (x92 + x76);
1019
- { uint64_t x95 = (x94 >> 0x19);
1020
- { uint32_t x96 = ((uint32_t)x94 & 0x1ffffff);
1021
- { uint64_t x97 = (x95 + x73);
1022
- { uint64_t x98 = (x97 >> 0x1a);
1023
- { uint32_t x99 = ((uint32_t)x97 & 0x3ffffff);
1024
- { uint64_t x100 = (x98 + x70);
1025
- { uint64_t x101 = (x100 >> 0x19);
1026
- { uint32_t x102 = ((uint32_t)x100 & 0x1ffffff);
1027
- { uint64_t x103 = (x101 + x67);
1028
- { uint64_t x104 = (x103 >> 0x1a);
1029
- { uint32_t x105 = ((uint32_t)x103 & 0x3ffffff);
1030
- { uint64_t x106 = (x104 + x64);
1031
- { uint64_t x107 = (x106 >> 0x19);
1032
- { uint32_t x108 = ((uint32_t)x106 & 0x1ffffff);
1033
- { uint64_t x109 = (x107 + x61);
1034
- { uint64_t x110 = (x109 >> 0x1a);
1035
- { uint32_t x111 = ((uint32_t)x109 & 0x3ffffff);
1036
- { uint64_t x112 = (x110 + x49);
1037
- { uint64_t x113 = (x112 >> 0x19);
1038
- { uint32_t x114 = ((uint32_t)x112 & 0x1ffffff);
1039
- { uint64_t x115 = (x87 + (0x13 * x113));
1040
- { uint32_t x116 = (uint32_t) (x115 >> 0x1a);
1041
- { uint32_t x117 = ((uint32_t)x115 & 0x3ffffff);
1042
- { uint32_t x118 = (x116 + x90);
1043
- { uint32_t x119 = (x118 >> 0x19);
1044
- { uint32_t x120 = (x118 & 0x1ffffff);
1045
- out[0] = x117;
1046
- out[1] = x120;
1047
- out[2] = (x119 + x93);
1048
- out[3] = x96;
1049
- out[4] = x99;
1050
- out[5] = x102;
1051
- out[6] = x105;
1052
- out[7] = x108;
1053
- out[8] = x111;
1054
- out[9] = x114;
1055
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
230
+ fiat_25519_carry_mul(out, in1, in2);
1056
231
  assert_fe(out);
1057
232
  }
1058
233
 
@@ -1080,297 +255,42 @@ static void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) {
1080
255
  fe_mul_impl(h->v, f->v, g->v);
1081
256
  }
1082
257
 
1083
- static void fe_sqr_impl(uint32_t out[10], const uint32_t in1[10]) {
1084
- assert_fe_loose(in1);
1085
- { const uint32_t x17 = in1[9];
1086
- { const uint32_t x18 = in1[8];
1087
- { const uint32_t x16 = in1[7];
1088
- { const uint32_t x14 = in1[6];
1089
- { const uint32_t x12 = in1[5];
1090
- { const uint32_t x10 = in1[4];
1091
- { const uint32_t x8 = in1[3];
1092
- { const uint32_t x6 = in1[2];
1093
- { const uint32_t x4 = in1[1];
1094
- { const uint32_t x2 = in1[0];
1095
- { uint64_t x19 = ((uint64_t)x2 * x2);
1096
- { uint64_t x20 = ((uint64_t)(0x2 * x2) * x4);
1097
- { uint64_t x21 = (0x2 * (((uint64_t)x4 * x4) + ((uint64_t)x2 * x6)));
1098
- { uint64_t x22 = (0x2 * (((uint64_t)x4 * x6) + ((uint64_t)x2 * x8)));
1099
- { uint64_t x23 = ((((uint64_t)x6 * x6) + ((uint64_t)(0x4 * x4) * x8)) + ((uint64_t)(0x2 * x2) * x10));
1100
- { uint64_t x24 = (0x2 * ((((uint64_t)x6 * x8) + ((uint64_t)x4 * x10)) + ((uint64_t)x2 * x12)));
1101
- { uint64_t x25 = (0x2 * (((((uint64_t)x8 * x8) + ((uint64_t)x6 * x10)) + ((uint64_t)x2 * x14)) + ((uint64_t)(0x2 * x4) * x12)));
1102
- { uint64_t x26 = (0x2 * (((((uint64_t)x8 * x10) + ((uint64_t)x6 * x12)) + ((uint64_t)x4 * x14)) + ((uint64_t)x2 * x16)));
1103
- { uint64_t x27 = (((uint64_t)x10 * x10) + (0x2 * ((((uint64_t)x6 * x14) + ((uint64_t)x2 * x18)) + (0x2 * (((uint64_t)x4 * x16) + ((uint64_t)x8 * x12))))));
1104
- { uint64_t x28 = (0x2 * ((((((uint64_t)x10 * x12) + ((uint64_t)x8 * x14)) + ((uint64_t)x6 * x16)) + ((uint64_t)x4 * x18)) + ((uint64_t)x2 * x17)));
1105
- { uint64_t x29 = (0x2 * (((((uint64_t)x12 * x12) + ((uint64_t)x10 * x14)) + ((uint64_t)x6 * x18)) + (0x2 * (((uint64_t)x8 * x16) + ((uint64_t)x4 * x17)))));
1106
- { uint64_t x30 = (0x2 * (((((uint64_t)x12 * x14) + ((uint64_t)x10 * x16)) + ((uint64_t)x8 * x18)) + ((uint64_t)x6 * x17)));
1107
- { uint64_t x31 = (((uint64_t)x14 * x14) + (0x2 * (((uint64_t)x10 * x18) + (0x2 * (((uint64_t)x12 * x16) + ((uint64_t)x8 * x17))))));
1108
- { uint64_t x32 = (0x2 * ((((uint64_t)x14 * x16) + ((uint64_t)x12 * x18)) + ((uint64_t)x10 * x17)));
1109
- { uint64_t x33 = (0x2 * ((((uint64_t)x16 * x16) + ((uint64_t)x14 * x18)) + ((uint64_t)(0x2 * x12) * x17)));
1110
- { uint64_t x34 = (0x2 * (((uint64_t)x16 * x18) + ((uint64_t)x14 * x17)));
1111
- { uint64_t x35 = (((uint64_t)x18 * x18) + ((uint64_t)(0x4 * x16) * x17));
1112
- { uint64_t x36 = ((uint64_t)(0x2 * x18) * x17);
1113
- { uint64_t x37 = ((uint64_t)(0x2 * x17) * x17);
1114
- { uint64_t x38 = (x27 + (x37 << 0x4));
1115
- { uint64_t x39 = (x38 + (x37 << 0x1));
1116
- { uint64_t x40 = (x39 + x37);
1117
- { uint64_t x41 = (x26 + (x36 << 0x4));
1118
- { uint64_t x42 = (x41 + (x36 << 0x1));
1119
- { uint64_t x43 = (x42 + x36);
1120
- { uint64_t x44 = (x25 + (x35 << 0x4));
1121
- { uint64_t x45 = (x44 + (x35 << 0x1));
1122
- { uint64_t x46 = (x45 + x35);
1123
- { uint64_t x47 = (x24 + (x34 << 0x4));
1124
- { uint64_t x48 = (x47 + (x34 << 0x1));
1125
- { uint64_t x49 = (x48 + x34);
1126
- { uint64_t x50 = (x23 + (x33 << 0x4));
1127
- { uint64_t x51 = (x50 + (x33 << 0x1));
1128
- { uint64_t x52 = (x51 + x33);
1129
- { uint64_t x53 = (x22 + (x32 << 0x4));
1130
- { uint64_t x54 = (x53 + (x32 << 0x1));
1131
- { uint64_t x55 = (x54 + x32);
1132
- { uint64_t x56 = (x21 + (x31 << 0x4));
1133
- { uint64_t x57 = (x56 + (x31 << 0x1));
1134
- { uint64_t x58 = (x57 + x31);
1135
- { uint64_t x59 = (x20 + (x30 << 0x4));
1136
- { uint64_t x60 = (x59 + (x30 << 0x1));
1137
- { uint64_t x61 = (x60 + x30);
1138
- { uint64_t x62 = (x19 + (x29 << 0x4));
1139
- { uint64_t x63 = (x62 + (x29 << 0x1));
1140
- { uint64_t x64 = (x63 + x29);
1141
- { uint64_t x65 = (x64 >> 0x1a);
1142
- { uint32_t x66 = ((uint32_t)x64 & 0x3ffffff);
1143
- { uint64_t x67 = (x65 + x61);
1144
- { uint64_t x68 = (x67 >> 0x19);
1145
- { uint32_t x69 = ((uint32_t)x67 & 0x1ffffff);
1146
- { uint64_t x70 = (x68 + x58);
1147
- { uint64_t x71 = (x70 >> 0x1a);
1148
- { uint32_t x72 = ((uint32_t)x70 & 0x3ffffff);
1149
- { uint64_t x73 = (x71 + x55);
1150
- { uint64_t x74 = (x73 >> 0x19);
1151
- { uint32_t x75 = ((uint32_t)x73 & 0x1ffffff);
1152
- { uint64_t x76 = (x74 + x52);
1153
- { uint64_t x77 = (x76 >> 0x1a);
1154
- { uint32_t x78 = ((uint32_t)x76 & 0x3ffffff);
1155
- { uint64_t x79 = (x77 + x49);
1156
- { uint64_t x80 = (x79 >> 0x19);
1157
- { uint32_t x81 = ((uint32_t)x79 & 0x1ffffff);
1158
- { uint64_t x82 = (x80 + x46);
1159
- { uint64_t x83 = (x82 >> 0x1a);
1160
- { uint32_t x84 = ((uint32_t)x82 & 0x3ffffff);
1161
- { uint64_t x85 = (x83 + x43);
1162
- { uint64_t x86 = (x85 >> 0x19);
1163
- { uint32_t x87 = ((uint32_t)x85 & 0x1ffffff);
1164
- { uint64_t x88 = (x86 + x40);
1165
- { uint64_t x89 = (x88 >> 0x1a);
1166
- { uint32_t x90 = ((uint32_t)x88 & 0x3ffffff);
1167
- { uint64_t x91 = (x89 + x28);
1168
- { uint64_t x92 = (x91 >> 0x19);
1169
- { uint32_t x93 = ((uint32_t)x91 & 0x1ffffff);
1170
- { uint64_t x94 = (x66 + (0x13 * x92));
1171
- { uint32_t x95 = (uint32_t) (x94 >> 0x1a);
1172
- { uint32_t x96 = ((uint32_t)x94 & 0x3ffffff);
1173
- { uint32_t x97 = (x95 + x69);
1174
- { uint32_t x98 = (x97 >> 0x19);
1175
- { uint32_t x99 = (x97 & 0x1ffffff);
1176
- out[0] = x96;
1177
- out[1] = x99;
1178
- out[2] = (x98 + x72);
1179
- out[3] = x75;
1180
- out[4] = x78;
1181
- out[5] = x81;
1182
- out[6] = x84;
1183
- out[7] = x87;
1184
- out[8] = x90;
1185
- out[9] = x93;
1186
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1187
- assert_fe(out);
1188
- }
1189
-
1190
258
  static void fe_sq_tl(fe *h, const fe_loose *f) {
1191
- fe_sqr_impl(h->v, f->v);
259
+ assert_fe_loose(f->v);
260
+ fiat_25519_carry_square(h->v, f->v);
261
+ assert_fe(h->v);
1192
262
  }
1193
263
 
1194
264
  static void fe_sq_tt(fe *h, const fe *f) {
1195
- fe_sqr_impl(h->v, f->v);
265
+ assert_fe_loose(f->v);
266
+ fiat_25519_carry_square(h->v, f->v);
267
+ assert_fe(h->v);
1196
268
  }
1197
269
 
1198
270
  // Replace (f,g) with (g,f) if b == 1;
1199
271
  // replace (f,g) with (f,g) if b == 0.
1200
272
  //
1201
273
  // Preconditions: b in {0,1}.
1202
- static void fe_cswap(fe *f, fe *g, unsigned int b) {
274
+ static void fe_cswap(fe *f, fe *g, fe_limb_t b) {
1203
275
  b = 0-b;
1204
- unsigned i;
1205
- for (i = 0; i < 10; i++) {
1206
- uint32_t x = f->v[i] ^ g->v[i];
276
+ for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
277
+ fe_limb_t x = f->v[i] ^ g->v[i];
1207
278
  x &= b;
1208
279
  f->v[i] ^= x;
1209
280
  g->v[i] ^= x;
1210
281
  }
1211
282
  }
1212
283
 
1213
- // NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0..
1214
- static void fe_mul_121666_impl(uint32_t out[10], const uint32_t in1[10]) {
1215
- { const uint32_t x20 = in1[9];
1216
- { const uint32_t x21 = in1[8];
1217
- { const uint32_t x19 = in1[7];
1218
- { const uint32_t x17 = in1[6];
1219
- { const uint32_t x15 = in1[5];
1220
- { const uint32_t x13 = in1[4];
1221
- { const uint32_t x11 = in1[3];
1222
- { const uint32_t x9 = in1[2];
1223
- { const uint32_t x7 = in1[1];
1224
- { const uint32_t x5 = in1[0];
1225
- { const uint32_t x38 = 0;
1226
- { const uint32_t x39 = 0;
1227
- { const uint32_t x37 = 0;
1228
- { const uint32_t x35 = 0;
1229
- { const uint32_t x33 = 0;
1230
- { const uint32_t x31 = 0;
1231
- { const uint32_t x29 = 0;
1232
- { const uint32_t x27 = 0;
1233
- { const uint32_t x25 = 0;
1234
- { const uint32_t x23 = 121666;
1235
- { uint64_t x40 = ((uint64_t)x23 * x5);
1236
- { uint64_t x41 = (((uint64_t)x23 * x7) + ((uint64_t)x25 * x5));
1237
- { uint64_t x42 = ((((uint64_t)(0x2 * x25) * x7) + ((uint64_t)x23 * x9)) + ((uint64_t)x27 * x5));
1238
- { uint64_t x43 = (((((uint64_t)x25 * x9) + ((uint64_t)x27 * x7)) + ((uint64_t)x23 * x11)) + ((uint64_t)x29 * x5));
1239
- { uint64_t x44 = (((((uint64_t)x27 * x9) + (0x2 * (((uint64_t)x25 * x11) + ((uint64_t)x29 * x7)))) + ((uint64_t)x23 * x13)) + ((uint64_t)x31 * x5));
1240
- { uint64_t x45 = (((((((uint64_t)x27 * x11) + ((uint64_t)x29 * x9)) + ((uint64_t)x25 * x13)) + ((uint64_t)x31 * x7)) + ((uint64_t)x23 * x15)) + ((uint64_t)x33 * x5));
1241
- { uint64_t x46 = (((((0x2 * ((((uint64_t)x29 * x11) + ((uint64_t)x25 * x15)) + ((uint64_t)x33 * x7))) + ((uint64_t)x27 * x13)) + ((uint64_t)x31 * x9)) + ((uint64_t)x23 * x17)) + ((uint64_t)x35 * x5));
1242
- { uint64_t x47 = (((((((((uint64_t)x29 * x13) + ((uint64_t)x31 * x11)) + ((uint64_t)x27 * x15)) + ((uint64_t)x33 * x9)) + ((uint64_t)x25 * x17)) + ((uint64_t)x35 * x7)) + ((uint64_t)x23 * x19)) + ((uint64_t)x37 * x5));
1243
- { uint64_t x48 = (((((((uint64_t)x31 * x13) + (0x2 * (((((uint64_t)x29 * x15) + ((uint64_t)x33 * x11)) + ((uint64_t)x25 * x19)) + ((uint64_t)x37 * x7)))) + ((uint64_t)x27 * x17)) + ((uint64_t)x35 * x9)) + ((uint64_t)x23 * x21)) + ((uint64_t)x39 * x5));
1244
- { uint64_t x49 = (((((((((((uint64_t)x31 * x15) + ((uint64_t)x33 * x13)) + ((uint64_t)x29 * x17)) + ((uint64_t)x35 * x11)) + ((uint64_t)x27 * x19)) + ((uint64_t)x37 * x9)) + ((uint64_t)x25 * x21)) + ((uint64_t)x39 * x7)) + ((uint64_t)x23 * x20)) + ((uint64_t)x38 * x5));
1245
- { uint64_t x50 = (((((0x2 * ((((((uint64_t)x33 * x15) + ((uint64_t)x29 * x19)) + ((uint64_t)x37 * x11)) + ((uint64_t)x25 * x20)) + ((uint64_t)x38 * x7))) + ((uint64_t)x31 * x17)) + ((uint64_t)x35 * x13)) + ((uint64_t)x27 * x21)) + ((uint64_t)x39 * x9));
1246
- { uint64_t x51 = (((((((((uint64_t)x33 * x17) + ((uint64_t)x35 * x15)) + ((uint64_t)x31 * x19)) + ((uint64_t)x37 * x13)) + ((uint64_t)x29 * x21)) + ((uint64_t)x39 * x11)) + ((uint64_t)x27 * x20)) + ((uint64_t)x38 * x9));
1247
- { uint64_t x52 = (((((uint64_t)x35 * x17) + (0x2 * (((((uint64_t)x33 * x19) + ((uint64_t)x37 * x15)) + ((uint64_t)x29 * x20)) + ((uint64_t)x38 * x11)))) + ((uint64_t)x31 * x21)) + ((uint64_t)x39 * x13));
1248
- { uint64_t x53 = (((((((uint64_t)x35 * x19) + ((uint64_t)x37 * x17)) + ((uint64_t)x33 * x21)) + ((uint64_t)x39 * x15)) + ((uint64_t)x31 * x20)) + ((uint64_t)x38 * x13));
1249
- { uint64_t x54 = (((0x2 * ((((uint64_t)x37 * x19) + ((uint64_t)x33 * x20)) + ((uint64_t)x38 * x15))) + ((uint64_t)x35 * x21)) + ((uint64_t)x39 * x17));
1250
- { uint64_t x55 = (((((uint64_t)x37 * x21) + ((uint64_t)x39 * x19)) + ((uint64_t)x35 * x20)) + ((uint64_t)x38 * x17));
1251
- { uint64_t x56 = (((uint64_t)x39 * x21) + (0x2 * (((uint64_t)x37 * x20) + ((uint64_t)x38 * x19))));
1252
- { uint64_t x57 = (((uint64_t)x39 * x20) + ((uint64_t)x38 * x21));
1253
- { uint64_t x58 = ((uint64_t)(0x2 * x38) * x20);
1254
- { uint64_t x59 = (x48 + (x58 << 0x4));
1255
- { uint64_t x60 = (x59 + (x58 << 0x1));
1256
- { uint64_t x61 = (x60 + x58);
1257
- { uint64_t x62 = (x47 + (x57 << 0x4));
1258
- { uint64_t x63 = (x62 + (x57 << 0x1));
1259
- { uint64_t x64 = (x63 + x57);
1260
- { uint64_t x65 = (x46 + (x56 << 0x4));
1261
- { uint64_t x66 = (x65 + (x56 << 0x1));
1262
- { uint64_t x67 = (x66 + x56);
1263
- { uint64_t x68 = (x45 + (x55 << 0x4));
1264
- { uint64_t x69 = (x68 + (x55 << 0x1));
1265
- { uint64_t x70 = (x69 + x55);
1266
- { uint64_t x71 = (x44 + (x54 << 0x4));
1267
- { uint64_t x72 = (x71 + (x54 << 0x1));
1268
- { uint64_t x73 = (x72 + x54);
1269
- { uint64_t x74 = (x43 + (x53 << 0x4));
1270
- { uint64_t x75 = (x74 + (x53 << 0x1));
1271
- { uint64_t x76 = (x75 + x53);
1272
- { uint64_t x77 = (x42 + (x52 << 0x4));
1273
- { uint64_t x78 = (x77 + (x52 << 0x1));
1274
- { uint64_t x79 = (x78 + x52);
1275
- { uint64_t x80 = (x41 + (x51 << 0x4));
1276
- { uint64_t x81 = (x80 + (x51 << 0x1));
1277
- { uint64_t x82 = (x81 + x51);
1278
- { uint64_t x83 = (x40 + (x50 << 0x4));
1279
- { uint64_t x84 = (x83 + (x50 << 0x1));
1280
- { uint64_t x85 = (x84 + x50);
1281
- { uint64_t x86 = (x85 >> 0x1a);
1282
- { uint32_t x87 = ((uint32_t)x85 & 0x3ffffff);
1283
- { uint64_t x88 = (x86 + x82);
1284
- { uint64_t x89 = (x88 >> 0x19);
1285
- { uint32_t x90 = ((uint32_t)x88 & 0x1ffffff);
1286
- { uint64_t x91 = (x89 + x79);
1287
- { uint64_t x92 = (x91 >> 0x1a);
1288
- { uint32_t x93 = ((uint32_t)x91 & 0x3ffffff);
1289
- { uint64_t x94 = (x92 + x76);
1290
- { uint64_t x95 = (x94 >> 0x19);
1291
- { uint32_t x96 = ((uint32_t)x94 & 0x1ffffff);
1292
- { uint64_t x97 = (x95 + x73);
1293
- { uint64_t x98 = (x97 >> 0x1a);
1294
- { uint32_t x99 = ((uint32_t)x97 & 0x3ffffff);
1295
- { uint64_t x100 = (x98 + x70);
1296
- { uint64_t x101 = (x100 >> 0x19);
1297
- { uint32_t x102 = ((uint32_t)x100 & 0x1ffffff);
1298
- { uint64_t x103 = (x101 + x67);
1299
- { uint64_t x104 = (x103 >> 0x1a);
1300
- { uint32_t x105 = ((uint32_t)x103 & 0x3ffffff);
1301
- { uint64_t x106 = (x104 + x64);
1302
- { uint64_t x107 = (x106 >> 0x19);
1303
- { uint32_t x108 = ((uint32_t)x106 & 0x1ffffff);
1304
- { uint64_t x109 = (x107 + x61);
1305
- { uint64_t x110 = (x109 >> 0x1a);
1306
- { uint32_t x111 = ((uint32_t)x109 & 0x3ffffff);
1307
- { uint64_t x112 = (x110 + x49);
1308
- { uint64_t x113 = (x112 >> 0x19);
1309
- { uint32_t x114 = ((uint32_t)x112 & 0x1ffffff);
1310
- { uint64_t x115 = (x87 + (0x13 * x113));
1311
- { uint32_t x116 = (uint32_t) (x115 >> 0x1a);
1312
- { uint32_t x117 = ((uint32_t)x115 & 0x3ffffff);
1313
- { uint32_t x118 = (x116 + x90);
1314
- { uint32_t x119 = (x118 >> 0x19);
1315
- { uint32_t x120 = (x118 & 0x1ffffff);
1316
- out[0] = x117;
1317
- out[1] = x120;
1318
- out[2] = (x119 + x93);
1319
- out[3] = x96;
1320
- out[4] = x99;
1321
- out[5] = x102;
1322
- out[6] = x105;
1323
- out[7] = x108;
1324
- out[8] = x111;
1325
- out[9] = x114;
1326
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1327
- }
1328
-
1329
284
  static void fe_mul121666(fe *h, const fe_loose *f) {
1330
285
  assert_fe_loose(f->v);
1331
- fe_mul_121666_impl(h->v, f->v);
286
+ fiat_25519_carry_scmul_121666(h->v, f->v);
1332
287
  assert_fe(h->v);
1333
288
  }
1334
289
 
1335
- // Adapted from Fiat-synthesized |fe_sub_impl| with |out| = 0.
1336
- static void fe_neg_impl(uint32_t out[10], const uint32_t in2[10]) {
1337
- { const uint32_t x20 = 0;
1338
- { const uint32_t x21 = 0;
1339
- { const uint32_t x19 = 0;
1340
- { const uint32_t x17 = 0;
1341
- { const uint32_t x15 = 0;
1342
- { const uint32_t x13 = 0;
1343
- { const uint32_t x11 = 0;
1344
- { const uint32_t x9 = 0;
1345
- { const uint32_t x7 = 0;
1346
- { const uint32_t x5 = 0;
1347
- { const uint32_t x38 = in2[9];
1348
- { const uint32_t x39 = in2[8];
1349
- { const uint32_t x37 = in2[7];
1350
- { const uint32_t x35 = in2[6];
1351
- { const uint32_t x33 = in2[5];
1352
- { const uint32_t x31 = in2[4];
1353
- { const uint32_t x29 = in2[3];
1354
- { const uint32_t x27 = in2[2];
1355
- { const uint32_t x25 = in2[1];
1356
- { const uint32_t x23 = in2[0];
1357
- out[0] = ((0x7ffffda + x5) - x23);
1358
- out[1] = ((0x3fffffe + x7) - x25);
1359
- out[2] = ((0x7fffffe + x9) - x27);
1360
- out[3] = ((0x3fffffe + x11) - x29);
1361
- out[4] = ((0x7fffffe + x13) - x31);
1362
- out[5] = ((0x3fffffe + x15) - x33);
1363
- out[6] = ((0x7fffffe + x17) - x35);
1364
- out[7] = ((0x3fffffe + x19) - x37);
1365
- out[8] = ((0x7fffffe + x21) - x39);
1366
- out[9] = ((0x3fffffe + x20) - x38);
1367
- }}}}}}}}}}}}}}}}}}}}
1368
- }
1369
-
1370
290
  // h = -f
1371
291
  static void fe_neg(fe_loose *h, const fe *f) {
1372
292
  assert_fe(f->v);
1373
- fe_neg_impl(h->v, f->v);
293
+ fiat_25519_opp(h->v, f->v);
1374
294
  assert_fe_loose(h->v);
1375
295
  }
1376
296
 
@@ -1378,26 +298,30 @@ static void fe_neg(fe_loose *h, const fe *f) {
1378
298
  // replace (f,g) with (f,g) if b == 0.
1379
299
  //
1380
300
  // Preconditions: b in {0,1}.
1381
- static void fe_cmov(fe_loose *f, const fe_loose *g, unsigned b) {
301
+ static void fe_cmov(fe_loose *f, const fe_loose *g, fe_limb_t b) {
302
+ // Silence an unused function warning. |fiat_25519_selectznz| isn't quite the
303
+ // calling convention the rest of this code wants, so implement it by hand.
304
+ //
305
+ // TODO(davidben): Switch to fiat's calling convention, or ask fiat to emit a
306
+ // different one.
307
+ (void)fiat_25519_selectznz;
308
+
1382
309
  b = 0-b;
1383
- unsigned i;
1384
- for (i = 0; i < 10; i++) {
1385
- uint32_t x = f->v[i] ^ g->v[i];
310
+ for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
311
+ fe_limb_t x = f->v[i] ^ g->v[i];
1386
312
  x &= b;
1387
313
  f->v[i] ^= x;
1388
314
  }
1389
315
  }
1390
316
 
1391
- #endif // BORINGSSL_CURVE25519_64BIT
1392
-
1393
317
  // h = f
1394
318
  static void fe_copy(fe *h, const fe *f) {
1395
319
  OPENSSL_memmove(h, f, sizeof(fe));
1396
320
  }
1397
321
 
1398
322
  static void fe_copy_lt(fe_loose *h, const fe *f) {
1399
- OPENSSL_COMPILE_ASSERT(sizeof(fe_loose) == sizeof(fe),
1400
- fe_and_fe_loose_mismatch);
323
+ OPENSSL_STATIC_ASSERT(sizeof(fe_loose) == sizeof(fe),
324
+ "fe and fe_loose mismatch");
1401
325
  OPENSSL_memmove(h, f, sizeof(fe));
1402
326
  }
1403
327
  #if !defined(OPENSSL_SMALL)
@@ -1584,7 +508,7 @@ static void ge_p3_tobytes(uint8_t s[32], const ge_p3 *h) {
1584
508
  s[31] ^= fe_isnegative(&x) << 7;
1585
509
  }
1586
510
 
1587
- int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s) {
511
+ int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) {
1588
512
  fe u;
1589
513
  fe_loose v;
1590
514
  fe v3;
@@ -1615,7 +539,7 @@ int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s) {
1615
539
  if (fe_isnonzero(&check)) {
1616
540
  fe_add(&check, &vxx, &u);
1617
541
  if (fe_isnonzero(&check)) {
1618
- return -1;
542
+ return 0;
1619
543
  }
1620
544
  fe_mul_ttt(&h->X, &h->X, &sqrtm1);
1621
545
  }
@@ -1627,7 +551,7 @@ int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s) {
1627
551
  }
1628
552
 
1629
553
  fe_mul_ttt(&h->T, &h->X, &h->Y);
1630
- return 0;
554
+ return 1;
1631
555
  }
1632
556
 
1633
557
  static void ge_p2_0(ge_p2 *h) {
@@ -1813,10 +737,12 @@ void x25519_ge_scalarmult_small_precomp(
1813
737
 
1814
738
  unsigned i;
1815
739
  for (i = 0; i < 15; i++) {
740
+ // The precomputed table is assumed to already clear the top bit, so
741
+ // |fe_frombytes_strict| may be used directly.
1816
742
  const uint8_t *bytes = &precomp_table[i*(2 * 32)];
1817
743
  fe x, y;
1818
- fe_frombytes(&x, bytes);
1819
- fe_frombytes(&y, bytes + 32);
744
+ fe_frombytes_strict(&x, bytes);
745
+ fe_frombytes_strict(&y, bytes + 32);
1820
746
 
1821
747
  ge_precomp *out = &multiples[i];
1822
748
  fe_add(&out->yplusx, &y, &x);
@@ -2120,6 +1046,12 @@ static void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a,
2120
1046
  }
2121
1047
  }
2122
1048
 
1049
+ // int64_lshift21 returns |a << 21| but is defined when shifting bits into the
1050
+ // sign bit. This works around a language flaw in C.
1051
+ static inline int64_t int64_lshift21(int64_t a) {
1052
+ return (int64_t)((uint64_t)a << 21);
1053
+ }
1054
+
2123
1055
  // The set of scalars is \Z/l
2124
1056
  // where l = 2^252 + 27742317777372353535851937790883648493.
2125
1057
 
@@ -2223,38 +1155,38 @@ void x25519_sc_reduce(uint8_t s[64]) {
2223
1155
 
2224
1156
  carry6 = (s6 + (1 << 20)) >> 21;
2225
1157
  s7 += carry6;
2226
- s6 -= carry6 << 21;
1158
+ s6 -= int64_lshift21(carry6);
2227
1159
  carry8 = (s8 + (1 << 20)) >> 21;
2228
1160
  s9 += carry8;
2229
- s8 -= carry8 << 21;
1161
+ s8 -= int64_lshift21(carry8);
2230
1162
  carry10 = (s10 + (1 << 20)) >> 21;
2231
1163
  s11 += carry10;
2232
- s10 -= carry10 << 21;
1164
+ s10 -= int64_lshift21(carry10);
2233
1165
  carry12 = (s12 + (1 << 20)) >> 21;
2234
1166
  s13 += carry12;
2235
- s12 -= carry12 << 21;
1167
+ s12 -= int64_lshift21(carry12);
2236
1168
  carry14 = (s14 + (1 << 20)) >> 21;
2237
1169
  s15 += carry14;
2238
- s14 -= carry14 << 21;
1170
+ s14 -= int64_lshift21(carry14);
2239
1171
  carry16 = (s16 + (1 << 20)) >> 21;
2240
1172
  s17 += carry16;
2241
- s16 -= carry16 << 21;
1173
+ s16 -= int64_lshift21(carry16);
2242
1174
 
2243
1175
  carry7 = (s7 + (1 << 20)) >> 21;
2244
1176
  s8 += carry7;
2245
- s7 -= carry7 << 21;
1177
+ s7 -= int64_lshift21(carry7);
2246
1178
  carry9 = (s9 + (1 << 20)) >> 21;
2247
1179
  s10 += carry9;
2248
- s9 -= carry9 << 21;
1180
+ s9 -= int64_lshift21(carry9);
2249
1181
  carry11 = (s11 + (1 << 20)) >> 21;
2250
1182
  s12 += carry11;
2251
- s11 -= carry11 << 21;
1183
+ s11 -= int64_lshift21(carry11);
2252
1184
  carry13 = (s13 + (1 << 20)) >> 21;
2253
1185
  s14 += carry13;
2254
- s13 -= carry13 << 21;
1186
+ s13 -= int64_lshift21(carry13);
2255
1187
  carry15 = (s15 + (1 << 20)) >> 21;
2256
1188
  s16 += carry15;
2257
- s15 -= carry15 << 21;
1189
+ s15 -= int64_lshift21(carry15);
2258
1190
 
2259
1191
  s5 += s17 * 666643;
2260
1192
  s6 += s17 * 470296;
@@ -2306,41 +1238,41 @@ void x25519_sc_reduce(uint8_t s[64]) {
2306
1238
 
2307
1239
  carry0 = (s0 + (1 << 20)) >> 21;
2308
1240
  s1 += carry0;
2309
- s0 -= carry0 << 21;
1241
+ s0 -= int64_lshift21(carry0);
2310
1242
  carry2 = (s2 + (1 << 20)) >> 21;
2311
1243
  s3 += carry2;
2312
- s2 -= carry2 << 21;
1244
+ s2 -= int64_lshift21(carry2);
2313
1245
  carry4 = (s4 + (1 << 20)) >> 21;
2314
1246
  s5 += carry4;
2315
- s4 -= carry4 << 21;
1247
+ s4 -= int64_lshift21(carry4);
2316
1248
  carry6 = (s6 + (1 << 20)) >> 21;
2317
1249
  s7 += carry6;
2318
- s6 -= carry6 << 21;
1250
+ s6 -= int64_lshift21(carry6);
2319
1251
  carry8 = (s8 + (1 << 20)) >> 21;
2320
1252
  s9 += carry8;
2321
- s8 -= carry8 << 21;
1253
+ s8 -= int64_lshift21(carry8);
2322
1254
  carry10 = (s10 + (1 << 20)) >> 21;
2323
1255
  s11 += carry10;
2324
- s10 -= carry10 << 21;
1256
+ s10 -= int64_lshift21(carry10);
2325
1257
 
2326
1258
  carry1 = (s1 + (1 << 20)) >> 21;
2327
1259
  s2 += carry1;
2328
- s1 -= carry1 << 21;
1260
+ s1 -= int64_lshift21(carry1);
2329
1261
  carry3 = (s3 + (1 << 20)) >> 21;
2330
1262
  s4 += carry3;
2331
- s3 -= carry3 << 21;
1263
+ s3 -= int64_lshift21(carry3);
2332
1264
  carry5 = (s5 + (1 << 20)) >> 21;
2333
1265
  s6 += carry5;
2334
- s5 -= carry5 << 21;
1266
+ s5 -= int64_lshift21(carry5);
2335
1267
  carry7 = (s7 + (1 << 20)) >> 21;
2336
1268
  s8 += carry7;
2337
- s7 -= carry7 << 21;
1269
+ s7 -= int64_lshift21(carry7);
2338
1270
  carry9 = (s9 + (1 << 20)) >> 21;
2339
1271
  s10 += carry9;
2340
- s9 -= carry9 << 21;
1272
+ s9 -= int64_lshift21(carry9);
2341
1273
  carry11 = (s11 + (1 << 20)) >> 21;
2342
1274
  s12 += carry11;
2343
- s11 -= carry11 << 21;
1275
+ s11 -= int64_lshift21(carry11);
2344
1276
 
2345
1277
  s0 += s12 * 666643;
2346
1278
  s1 += s12 * 470296;
@@ -2352,40 +1284,40 @@ void x25519_sc_reduce(uint8_t s[64]) {
2352
1284
 
2353
1285
  carry0 = s0 >> 21;
2354
1286
  s1 += carry0;
2355
- s0 -= carry0 << 21;
1287
+ s0 -= int64_lshift21(carry0);
2356
1288
  carry1 = s1 >> 21;
2357
1289
  s2 += carry1;
2358
- s1 -= carry1 << 21;
1290
+ s1 -= int64_lshift21(carry1);
2359
1291
  carry2 = s2 >> 21;
2360
1292
  s3 += carry2;
2361
- s2 -= carry2 << 21;
1293
+ s2 -= int64_lshift21(carry2);
2362
1294
  carry3 = s3 >> 21;
2363
1295
  s4 += carry3;
2364
- s3 -= carry3 << 21;
1296
+ s3 -= int64_lshift21(carry3);
2365
1297
  carry4 = s4 >> 21;
2366
1298
  s5 += carry4;
2367
- s4 -= carry4 << 21;
1299
+ s4 -= int64_lshift21(carry4);
2368
1300
  carry5 = s5 >> 21;
2369
1301
  s6 += carry5;
2370
- s5 -= carry5 << 21;
1302
+ s5 -= int64_lshift21(carry5);
2371
1303
  carry6 = s6 >> 21;
2372
1304
  s7 += carry6;
2373
- s6 -= carry6 << 21;
1305
+ s6 -= int64_lshift21(carry6);
2374
1306
  carry7 = s7 >> 21;
2375
1307
  s8 += carry7;
2376
- s7 -= carry7 << 21;
1308
+ s7 -= int64_lshift21(carry7);
2377
1309
  carry8 = s8 >> 21;
2378
1310
  s9 += carry8;
2379
- s8 -= carry8 << 21;
1311
+ s8 -= int64_lshift21(carry8);
2380
1312
  carry9 = s9 >> 21;
2381
1313
  s10 += carry9;
2382
- s9 -= carry9 << 21;
1314
+ s9 -= int64_lshift21(carry9);
2383
1315
  carry10 = s10 >> 21;
2384
1316
  s11 += carry10;
2385
- s10 -= carry10 << 21;
1317
+ s10 -= int64_lshift21(carry10);
2386
1318
  carry11 = s11 >> 21;
2387
1319
  s12 += carry11;
2388
- s11 -= carry11 << 21;
1320
+ s11 -= int64_lshift21(carry11);
2389
1321
 
2390
1322
  s0 += s12 * 666643;
2391
1323
  s1 += s12 * 470296;
@@ -2397,37 +1329,37 @@ void x25519_sc_reduce(uint8_t s[64]) {
2397
1329
 
2398
1330
  carry0 = s0 >> 21;
2399
1331
  s1 += carry0;
2400
- s0 -= carry0 << 21;
1332
+ s0 -= int64_lshift21(carry0);
2401
1333
  carry1 = s1 >> 21;
2402
1334
  s2 += carry1;
2403
- s1 -= carry1 << 21;
1335
+ s1 -= int64_lshift21(carry1);
2404
1336
  carry2 = s2 >> 21;
2405
1337
  s3 += carry2;
2406
- s2 -= carry2 << 21;
1338
+ s2 -= int64_lshift21(carry2);
2407
1339
  carry3 = s3 >> 21;
2408
1340
  s4 += carry3;
2409
- s3 -= carry3 << 21;
1341
+ s3 -= int64_lshift21(carry3);
2410
1342
  carry4 = s4 >> 21;
2411
1343
  s5 += carry4;
2412
- s4 -= carry4 << 21;
1344
+ s4 -= int64_lshift21(carry4);
2413
1345
  carry5 = s5 >> 21;
2414
1346
  s6 += carry5;
2415
- s5 -= carry5 << 21;
1347
+ s5 -= int64_lshift21(carry5);
2416
1348
  carry6 = s6 >> 21;
2417
1349
  s7 += carry6;
2418
- s6 -= carry6 << 21;
1350
+ s6 -= int64_lshift21(carry6);
2419
1351
  carry7 = s7 >> 21;
2420
1352
  s8 += carry7;
2421
- s7 -= carry7 << 21;
1353
+ s7 -= int64_lshift21(carry7);
2422
1354
  carry8 = s8 >> 21;
2423
1355
  s9 += carry8;
2424
- s8 -= carry8 << 21;
1356
+ s8 -= int64_lshift21(carry8);
2425
1357
  carry9 = s9 >> 21;
2426
1358
  s10 += carry9;
2427
- s9 -= carry9 << 21;
1359
+ s9 -= int64_lshift21(carry9);
2428
1360
  carry10 = s10 >> 21;
2429
1361
  s11 += carry10;
2430
- s10 -= carry10 << 21;
1362
+ s10 -= int64_lshift21(carry10);
2431
1363
 
2432
1364
  s[0] = s0 >> 0;
2433
1365
  s[1] = s0 >> 8;
@@ -2593,74 +1525,74 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
2593
1525
 
2594
1526
  carry0 = (s0 + (1 << 20)) >> 21;
2595
1527
  s1 += carry0;
2596
- s0 -= carry0 << 21;
1528
+ s0 -= int64_lshift21(carry0);
2597
1529
  carry2 = (s2 + (1 << 20)) >> 21;
2598
1530
  s3 += carry2;
2599
- s2 -= carry2 << 21;
1531
+ s2 -= int64_lshift21(carry2);
2600
1532
  carry4 = (s4 + (1 << 20)) >> 21;
2601
1533
  s5 += carry4;
2602
- s4 -= carry4 << 21;
1534
+ s4 -= int64_lshift21(carry4);
2603
1535
  carry6 = (s6 + (1 << 20)) >> 21;
2604
1536
  s7 += carry6;
2605
- s6 -= carry6 << 21;
1537
+ s6 -= int64_lshift21(carry6);
2606
1538
  carry8 = (s8 + (1 << 20)) >> 21;
2607
1539
  s9 += carry8;
2608
- s8 -= carry8 << 21;
1540
+ s8 -= int64_lshift21(carry8);
2609
1541
  carry10 = (s10 + (1 << 20)) >> 21;
2610
1542
  s11 += carry10;
2611
- s10 -= carry10 << 21;
1543
+ s10 -= int64_lshift21(carry10);
2612
1544
  carry12 = (s12 + (1 << 20)) >> 21;
2613
1545
  s13 += carry12;
2614
- s12 -= carry12 << 21;
1546
+ s12 -= int64_lshift21(carry12);
2615
1547
  carry14 = (s14 + (1 << 20)) >> 21;
2616
1548
  s15 += carry14;
2617
- s14 -= carry14 << 21;
1549
+ s14 -= int64_lshift21(carry14);
2618
1550
  carry16 = (s16 + (1 << 20)) >> 21;
2619
1551
  s17 += carry16;
2620
- s16 -= carry16 << 21;
1552
+ s16 -= int64_lshift21(carry16);
2621
1553
  carry18 = (s18 + (1 << 20)) >> 21;
2622
1554
  s19 += carry18;
2623
- s18 -= carry18 << 21;
1555
+ s18 -= int64_lshift21(carry18);
2624
1556
  carry20 = (s20 + (1 << 20)) >> 21;
2625
1557
  s21 += carry20;
2626
- s20 -= carry20 << 21;
1558
+ s20 -= int64_lshift21(carry20);
2627
1559
  carry22 = (s22 + (1 << 20)) >> 21;
2628
1560
  s23 += carry22;
2629
- s22 -= carry22 << 21;
1561
+ s22 -= int64_lshift21(carry22);
2630
1562
 
2631
1563
  carry1 = (s1 + (1 << 20)) >> 21;
2632
1564
  s2 += carry1;
2633
- s1 -= carry1 << 21;
1565
+ s1 -= int64_lshift21(carry1);
2634
1566
  carry3 = (s3 + (1 << 20)) >> 21;
2635
1567
  s4 += carry3;
2636
- s3 -= carry3 << 21;
1568
+ s3 -= int64_lshift21(carry3);
2637
1569
  carry5 = (s5 + (1 << 20)) >> 21;
2638
1570
  s6 += carry5;
2639
- s5 -= carry5 << 21;
1571
+ s5 -= int64_lshift21(carry5);
2640
1572
  carry7 = (s7 + (1 << 20)) >> 21;
2641
1573
  s8 += carry7;
2642
- s7 -= carry7 << 21;
1574
+ s7 -= int64_lshift21(carry7);
2643
1575
  carry9 = (s9 + (1 << 20)) >> 21;
2644
1576
  s10 += carry9;
2645
- s9 -= carry9 << 21;
1577
+ s9 -= int64_lshift21(carry9);
2646
1578
  carry11 = (s11 + (1 << 20)) >> 21;
2647
1579
  s12 += carry11;
2648
- s11 -= carry11 << 21;
1580
+ s11 -= int64_lshift21(carry11);
2649
1581
  carry13 = (s13 + (1 << 20)) >> 21;
2650
1582
  s14 += carry13;
2651
- s13 -= carry13 << 21;
1583
+ s13 -= int64_lshift21(carry13);
2652
1584
  carry15 = (s15 + (1 << 20)) >> 21;
2653
1585
  s16 += carry15;
2654
- s15 -= carry15 << 21;
1586
+ s15 -= int64_lshift21(carry15);
2655
1587
  carry17 = (s17 + (1 << 20)) >> 21;
2656
1588
  s18 += carry17;
2657
- s17 -= carry17 << 21;
1589
+ s17 -= int64_lshift21(carry17);
2658
1590
  carry19 = (s19 + (1 << 20)) >> 21;
2659
1591
  s20 += carry19;
2660
- s19 -= carry19 << 21;
1592
+ s19 -= int64_lshift21(carry19);
2661
1593
  carry21 = (s21 + (1 << 20)) >> 21;
2662
1594
  s22 += carry21;
2663
- s21 -= carry21 << 21;
1595
+ s21 -= int64_lshift21(carry21);
2664
1596
 
2665
1597
  s11 += s23 * 666643;
2666
1598
  s12 += s23 * 470296;
@@ -2712,38 +1644,38 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
2712
1644
 
2713
1645
  carry6 = (s6 + (1 << 20)) >> 21;
2714
1646
  s7 += carry6;
2715
- s6 -= carry6 << 21;
1647
+ s6 -= int64_lshift21(carry6);
2716
1648
  carry8 = (s8 + (1 << 20)) >> 21;
2717
1649
  s9 += carry8;
2718
- s8 -= carry8 << 21;
1650
+ s8 -= int64_lshift21(carry8);
2719
1651
  carry10 = (s10 + (1 << 20)) >> 21;
2720
1652
  s11 += carry10;
2721
- s10 -= carry10 << 21;
1653
+ s10 -= int64_lshift21(carry10);
2722
1654
  carry12 = (s12 + (1 << 20)) >> 21;
2723
1655
  s13 += carry12;
2724
- s12 -= carry12 << 21;
1656
+ s12 -= int64_lshift21(carry12);
2725
1657
  carry14 = (s14 + (1 << 20)) >> 21;
2726
1658
  s15 += carry14;
2727
- s14 -= carry14 << 21;
1659
+ s14 -= int64_lshift21(carry14);
2728
1660
  carry16 = (s16 + (1 << 20)) >> 21;
2729
1661
  s17 += carry16;
2730
- s16 -= carry16 << 21;
1662
+ s16 -= int64_lshift21(carry16);
2731
1663
 
2732
1664
  carry7 = (s7 + (1 << 20)) >> 21;
2733
1665
  s8 += carry7;
2734
- s7 -= carry7 << 21;
1666
+ s7 -= int64_lshift21(carry7);
2735
1667
  carry9 = (s9 + (1 << 20)) >> 21;
2736
1668
  s10 += carry9;
2737
- s9 -= carry9 << 21;
1669
+ s9 -= int64_lshift21(carry9);
2738
1670
  carry11 = (s11 + (1 << 20)) >> 21;
2739
1671
  s12 += carry11;
2740
- s11 -= carry11 << 21;
1672
+ s11 -= int64_lshift21(carry11);
2741
1673
  carry13 = (s13 + (1 << 20)) >> 21;
2742
1674
  s14 += carry13;
2743
- s13 -= carry13 << 21;
1675
+ s13 -= int64_lshift21(carry13);
2744
1676
  carry15 = (s15 + (1 << 20)) >> 21;
2745
1677
  s16 += carry15;
2746
- s15 -= carry15 << 21;
1678
+ s15 -= int64_lshift21(carry15);
2747
1679
 
2748
1680
  s5 += s17 * 666643;
2749
1681
  s6 += s17 * 470296;
@@ -2795,41 +1727,41 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
2795
1727
 
2796
1728
  carry0 = (s0 + (1 << 20)) >> 21;
2797
1729
  s1 += carry0;
2798
- s0 -= carry0 << 21;
1730
+ s0 -= int64_lshift21(carry0);
2799
1731
  carry2 = (s2 + (1 << 20)) >> 21;
2800
1732
  s3 += carry2;
2801
- s2 -= carry2 << 21;
1733
+ s2 -= int64_lshift21(carry2);
2802
1734
  carry4 = (s4 + (1 << 20)) >> 21;
2803
1735
  s5 += carry4;
2804
- s4 -= carry4 << 21;
1736
+ s4 -= int64_lshift21(carry4);
2805
1737
  carry6 = (s6 + (1 << 20)) >> 21;
2806
1738
  s7 += carry6;
2807
- s6 -= carry6 << 21;
1739
+ s6 -= int64_lshift21(carry6);
2808
1740
  carry8 = (s8 + (1 << 20)) >> 21;
2809
1741
  s9 += carry8;
2810
- s8 -= carry8 << 21;
1742
+ s8 -= int64_lshift21(carry8);
2811
1743
  carry10 = (s10 + (1 << 20)) >> 21;
2812
1744
  s11 += carry10;
2813
- s10 -= carry10 << 21;
1745
+ s10 -= int64_lshift21(carry10);
2814
1746
 
2815
1747
  carry1 = (s1 + (1 << 20)) >> 21;
2816
1748
  s2 += carry1;
2817
- s1 -= carry1 << 21;
1749
+ s1 -= int64_lshift21(carry1);
2818
1750
  carry3 = (s3 + (1 << 20)) >> 21;
2819
1751
  s4 += carry3;
2820
- s3 -= carry3 << 21;
1752
+ s3 -= int64_lshift21(carry3);
2821
1753
  carry5 = (s5 + (1 << 20)) >> 21;
2822
1754
  s6 += carry5;
2823
- s5 -= carry5 << 21;
1755
+ s5 -= int64_lshift21(carry5);
2824
1756
  carry7 = (s7 + (1 << 20)) >> 21;
2825
1757
  s8 += carry7;
2826
- s7 -= carry7 << 21;
1758
+ s7 -= int64_lshift21(carry7);
2827
1759
  carry9 = (s9 + (1 << 20)) >> 21;
2828
1760
  s10 += carry9;
2829
- s9 -= carry9 << 21;
1761
+ s9 -= int64_lshift21(carry9);
2830
1762
  carry11 = (s11 + (1 << 20)) >> 21;
2831
1763
  s12 += carry11;
2832
- s11 -= carry11 << 21;
1764
+ s11 -= int64_lshift21(carry11);
2833
1765
 
2834
1766
  s0 += s12 * 666643;
2835
1767
  s1 += s12 * 470296;
@@ -2841,40 +1773,40 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
2841
1773
 
2842
1774
  carry0 = s0 >> 21;
2843
1775
  s1 += carry0;
2844
- s0 -= carry0 << 21;
1776
+ s0 -= int64_lshift21(carry0);
2845
1777
  carry1 = s1 >> 21;
2846
1778
  s2 += carry1;
2847
- s1 -= carry1 << 21;
1779
+ s1 -= int64_lshift21(carry1);
2848
1780
  carry2 = s2 >> 21;
2849
1781
  s3 += carry2;
2850
- s2 -= carry2 << 21;
1782
+ s2 -= int64_lshift21(carry2);
2851
1783
  carry3 = s3 >> 21;
2852
1784
  s4 += carry3;
2853
- s3 -= carry3 << 21;
1785
+ s3 -= int64_lshift21(carry3);
2854
1786
  carry4 = s4 >> 21;
2855
1787
  s5 += carry4;
2856
- s4 -= carry4 << 21;
1788
+ s4 -= int64_lshift21(carry4);
2857
1789
  carry5 = s5 >> 21;
2858
1790
  s6 += carry5;
2859
- s5 -= carry5 << 21;
1791
+ s5 -= int64_lshift21(carry5);
2860
1792
  carry6 = s6 >> 21;
2861
1793
  s7 += carry6;
2862
- s6 -= carry6 << 21;
1794
+ s6 -= int64_lshift21(carry6);
2863
1795
  carry7 = s7 >> 21;
2864
1796
  s8 += carry7;
2865
- s7 -= carry7 << 21;
1797
+ s7 -= int64_lshift21(carry7);
2866
1798
  carry8 = s8 >> 21;
2867
1799
  s9 += carry8;
2868
- s8 -= carry8 << 21;
1800
+ s8 -= int64_lshift21(carry8);
2869
1801
  carry9 = s9 >> 21;
2870
1802
  s10 += carry9;
2871
- s9 -= carry9 << 21;
1803
+ s9 -= int64_lshift21(carry9);
2872
1804
  carry10 = s10 >> 21;
2873
1805
  s11 += carry10;
2874
- s10 -= carry10 << 21;
1806
+ s10 -= int64_lshift21(carry10);
2875
1807
  carry11 = s11 >> 21;
2876
1808
  s12 += carry11;
2877
- s11 -= carry11 << 21;
1809
+ s11 -= int64_lshift21(carry11);
2878
1810
 
2879
1811
  s0 += s12 * 666643;
2880
1812
  s1 += s12 * 470296;
@@ -2886,37 +1818,37 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
2886
1818
 
2887
1819
  carry0 = s0 >> 21;
2888
1820
  s1 += carry0;
2889
- s0 -= carry0 << 21;
1821
+ s0 -= int64_lshift21(carry0);
2890
1822
  carry1 = s1 >> 21;
2891
1823
  s2 += carry1;
2892
- s1 -= carry1 << 21;
1824
+ s1 -= int64_lshift21(carry1);
2893
1825
  carry2 = s2 >> 21;
2894
1826
  s3 += carry2;
2895
- s2 -= carry2 << 21;
1827
+ s2 -= int64_lshift21(carry2);
2896
1828
  carry3 = s3 >> 21;
2897
1829
  s4 += carry3;
2898
- s3 -= carry3 << 21;
1830
+ s3 -= int64_lshift21(carry3);
2899
1831
  carry4 = s4 >> 21;
2900
1832
  s5 += carry4;
2901
- s4 -= carry4 << 21;
1833
+ s4 -= int64_lshift21(carry4);
2902
1834
  carry5 = s5 >> 21;
2903
1835
  s6 += carry5;
2904
- s5 -= carry5 << 21;
1836
+ s5 -= int64_lshift21(carry5);
2905
1837
  carry6 = s6 >> 21;
2906
1838
  s7 += carry6;
2907
- s6 -= carry6 << 21;
1839
+ s6 -= int64_lshift21(carry6);
2908
1840
  carry7 = s7 >> 21;
2909
1841
  s8 += carry7;
2910
- s7 -= carry7 << 21;
1842
+ s7 -= int64_lshift21(carry7);
2911
1843
  carry8 = s8 >> 21;
2912
1844
  s9 += carry8;
2913
- s8 -= carry8 << 21;
1845
+ s8 -= int64_lshift21(carry8);
2914
1846
  carry9 = s9 >> 21;
2915
1847
  s10 += carry9;
2916
- s9 -= carry9 << 21;
1848
+ s9 -= int64_lshift21(carry9);
2917
1849
  carry10 = s10 >> 21;
2918
1850
  s11 += carry10;
2919
- s10 -= carry10 << 21;
1851
+ s10 -= int64_lshift21(carry10);
2920
1852
 
2921
1853
  s[0] = s0 >> 0;
2922
1854
  s[1] = s0 >> 8;
@@ -2960,6 +1892,11 @@ void ED25519_keypair(uint8_t out_public_key[32], uint8_t out_private_key[64]) {
2960
1892
 
2961
1893
  int ED25519_sign(uint8_t out_sig[64], const uint8_t *message,
2962
1894
  size_t message_len, const uint8_t private_key[64]) {
1895
+ // NOTE: The documentation on this function says that it returns zero on
1896
+ // allocation failure. While that can't happen with the current
1897
+ // implementation, we want to reserve the ability to allocate in this
1898
+ // implementation in the future.
1899
+
2963
1900
  uint8_t az[SHA512_DIGEST_LENGTH];
2964
1901
  SHA512(private_key, 32, az);
2965
1902
 
@@ -2996,7 +1933,7 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
2996
1933
  const uint8_t signature[64], const uint8_t public_key[32]) {
2997
1934
  ge_p3 A;
2998
1935
  if ((signature[63] & 224) != 0 ||
2999
- x25519_ge_frombytes_vartime(&A, public_key) != 0) {
1936
+ !x25519_ge_frombytes_vartime(&A, public_key)) {
3000
1937
  return 0;
3001
1938
  }
3002
1939
 
@@ -3062,7 +1999,7 @@ void ED25519_keypair_from_seed(uint8_t out_public_key[32],
3062
1999
  SHA512(seed, 32, az);
3063
2000
 
3064
2001
  az[0] &= 248;
3065
- az[31] &= 63;
2002
+ az[31] &= 127;
3066
2003
  az[31] |= 64;
3067
2004
 
3068
2005
  ge_p3 A;
@@ -3184,9 +2121,9 @@ void X25519_keypair(uint8_t out_public_value[32], uint8_t out_private_key[32]) {
3184
2121
  // This does not affect security because, although we're throwing away
3185
2122
  // entropy, a valid implementation of scalarmult should throw away the exact
3186
2123
  // same bits anyway.
3187
- out_private_key[0] |= 7;
3188
- out_private_key[31] &= 63;
3189
- out_private_key[31] |= 128;
2124
+ out_private_key[0] |= ~248;
2125
+ out_private_key[31] &= ~64;
2126
+ out_private_key[31] |= ~127;
3190
2127
 
3191
2128
  X25519_public_from_private(out_public_value, out_private_key);
3192
2129
  }