grpc 1.66.0 → 1.67.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (547) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +19 -10
  3. data/include/grpc/credentials.h +1 -1
  4. data/include/grpc/event_engine/README.md +1 -1
  5. data/include/grpc/event_engine/internal/slice_cast.h +1 -1
  6. data/include/grpc/event_engine/slice.h +0 -1
  7. data/include/grpc/event_engine/slice_buffer.h +0 -1
  8. data/include/grpc/grpc_crl_provider.h +1 -1
  9. data/include/grpc/impl/channel_arg_names.h +1 -1
  10. data/include/grpc/support/log.h +34 -32
  11. data/include/grpc/support/sync_generic.h +2 -4
  12. data/src/core/channelz/channelz.cc +0 -1
  13. data/src/core/channelz/channelz_registry.cc +0 -1
  14. data/src/core/client_channel/client_channel.cc +10 -7
  15. data/src/core/client_channel/client_channel.h +1 -1
  16. data/src/core/client_channel/client_channel_filter.cc +21 -18
  17. data/src/core/client_channel/client_channel_filter.h +1 -1
  18. data/src/core/client_channel/client_channel_internal.h +0 -2
  19. data/src/core/client_channel/config_selector.h +0 -1
  20. data/src/core/client_channel/dynamic_filters.cc +0 -2
  21. data/src/core/client_channel/local_subchannel_pool.cc +0 -2
  22. data/src/core/client_channel/retry_filter.h +0 -1
  23. data/src/core/client_channel/retry_filter_legacy_call_data.cc +175 -257
  24. data/src/core/client_channel/subchannel.cc +21 -27
  25. data/src/core/client_channel/subchannel_stream_client.cc +1 -1
  26. data/src/core/ext/filters/backend_metrics/backend_metric_filter.cc +8 -9
  27. data/src/core/ext/filters/channel_idle/legacy_channel_idle_filter.cc +0 -1
  28. data/src/core/ext/filters/fault_injection/fault_injection_filter.cc +3 -4
  29. data/src/core/ext/filters/gcp_authentication/gcp_authentication_filter.cc +167 -0
  30. data/src/core/ext/filters/gcp_authentication/gcp_authentication_filter.h +82 -0
  31. data/src/core/ext/filters/gcp_authentication/gcp_authentication_service_config_parser.cc +81 -0
  32. data/src/core/ext/filters/gcp_authentication/gcp_authentication_service_config_parser.h +87 -0
  33. data/src/core/ext/filters/http/message_compress/compression_filter.cc +7 -9
  34. data/src/core/ext/filters/http/server/http_server_filter.cc +2 -4
  35. data/src/core/ext/filters/message_size/message_size_filter.cc +6 -7
  36. data/src/core/ext/filters/stateful_session/stateful_session_filter.cc +0 -2
  37. data/src/core/ext/transport/chttp2/alpn/alpn.cc +0 -1
  38. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +6 -8
  39. data/src/core/ext/transport/chttp2/transport/bin_encoder.cc +0 -1
  40. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +288 -265
  41. data/src/core/ext/transport/chttp2/transport/flow_control.cc +3 -4
  42. data/src/core/ext/transport/chttp2/transport/flow_control.h +0 -1
  43. data/src/core/ext/transport/chttp2/transport/frame.cc +0 -1
  44. data/src/core/ext/transport/chttp2/transport/frame_data.cc +0 -1
  45. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +0 -1
  46. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +6 -6
  47. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +3 -4
  48. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +5 -6
  49. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +0 -1
  50. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +2 -3
  51. data/src/core/ext/transport/chttp2/transport/hpack_encoder_table.cc +0 -1
  52. data/src/core/ext/transport/chttp2/transport/hpack_parse_result.h +0 -1
  53. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +3 -3
  54. data/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc +37 -5
  55. data/src/core/ext/transport/chttp2/transport/hpack_parser_table.h +27 -6
  56. data/src/core/ext/transport/chttp2/transport/internal.h +2 -3
  57. data/src/core/ext/transport/chttp2/transport/parsing.cc +21 -32
  58. data/src/core/ext/transport/chttp2/transport/ping_callbacks.cc +0 -1
  59. data/src/core/ext/transport/chttp2/transport/stream_lists.cc +6 -8
  60. data/src/core/ext/transport/chttp2/transport/varint.h +0 -1
  61. data/src/core/ext/transport/chttp2/transport/write_size_policy.cc +0 -1
  62. data/src/core/ext/transport/chttp2/transport/writing.cc +22 -22
  63. data/src/core/ext/upb-gen/envoy/extensions/filters/http/gcp_authn/v3/gcp_authn.upb.h +431 -0
  64. data/src/core/ext/upb-gen/envoy/extensions/filters/http/gcp_authn/v3/gcp_authn.upb_minitable.c +129 -0
  65. data/src/core/ext/upb-gen/envoy/extensions/filters/http/gcp_authn/v3/gcp_authn.upb_minitable.h +33 -0
  66. data/src/core/ext/upb-gen/google/api/expr/v1alpha1/checked.upb.h +16 -0
  67. data/src/core/ext/upb-gen/google/api/expr/v1alpha1/checked.upb_minitable.c +13 -2
  68. data/src/core/ext/upb-gen/google/api/expr/v1alpha1/syntax.upb.h +397 -22
  69. data/src/core/ext/upb-gen/google/api/expr/v1alpha1/syntax.upb_minitable.c +94 -20
  70. data/src/core/ext/upb-gen/google/api/expr/v1alpha1/syntax.upb_minitable.h +2 -0
  71. data/src/core/ext/upbdefs-gen/envoy/extensions/filters/http/gcp_authn/v3/gcp_authn.upbdefs.c +86 -0
  72. data/src/core/ext/upbdefs-gen/envoy/extensions/filters/http/gcp_authn/v3/gcp_authn.upbdefs.h +47 -0
  73. data/src/core/ext/upbdefs-gen/google/api/expr/v1alpha1/checked.upbdefs.c +108 -107
  74. data/src/core/ext/upbdefs-gen/google/api/expr/v1alpha1/syntax.upbdefs.c +101 -78
  75. data/src/core/ext/upbdefs-gen/google/api/expr/v1alpha1/syntax.upbdefs.h +10 -0
  76. data/src/core/handshaker/handshaker.cc +21 -29
  77. data/src/core/handshaker/security/secure_endpoint.cc +3 -3
  78. data/src/core/handshaker/security/security_handshaker.cc +60 -72
  79. data/src/core/handshaker/tcp_connect/tcp_connect_handshaker.cc +0 -1
  80. data/src/core/lib/backoff/backoff.cc +7 -10
  81. data/src/core/lib/backoff/backoff.h +4 -6
  82. data/src/core/lib/channel/channel_stack.cc +0 -1
  83. data/src/core/lib/channel/channel_stack.h +0 -1
  84. data/src/core/lib/channel/channel_stack_builder_impl.cc +0 -1
  85. data/src/core/lib/channel/connected_channel.cc +0 -1
  86. data/src/core/lib/channel/promise_based_filter.cc +146 -194
  87. data/src/core/lib/channel/promise_based_filter.h +1 -1
  88. data/src/core/lib/compression/compression_internal.cc +0 -1
  89. data/src/core/lib/config/config_vars.cc +11 -1
  90. data/src/core/lib/config/config_vars.h +8 -0
  91. data/src/core/lib/config/core_configuration.cc +0 -1
  92. data/src/core/lib/config/core_configuration.h +0 -1
  93. data/src/core/lib/debug/event_log.cc +0 -1
  94. data/src/core/lib/debug/trace_flags.cc +4 -18
  95. data/src/core/lib/debug/trace_flags.h +2 -5
  96. data/src/core/lib/debug/trace_impl.h +6 -0
  97. data/src/core/lib/event_engine/ares_resolver.cc +89 -56
  98. data/src/core/lib/event_engine/ares_resolver.h +0 -9
  99. data/src/core/lib/event_engine/cf_engine/cfstream_endpoint.cc +14 -1
  100. data/src/core/lib/event_engine/cf_engine/cftype_unique_ref.h +1 -1
  101. data/src/core/lib/event_engine/forkable.cc +0 -1
  102. data/src/core/lib/event_engine/forkable.h +0 -1
  103. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.cc +1 -1
  104. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.h +4 -4
  105. data/src/core/lib/event_engine/posix_engine/ev_poll_posix.cc +0 -1
  106. data/src/core/lib/event_engine/posix_engine/lockfree_event.cc +0 -1
  107. data/src/core/lib/event_engine/posix_engine/posix_endpoint.cc +9 -1
  108. data/src/core/lib/event_engine/posix_engine/posix_endpoint.h +0 -1
  109. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.h +2 -2
  110. data/src/core/lib/event_engine/posix_engine/tcp_socket_utils.h +1 -2
  111. data/src/core/lib/event_engine/posix_engine/timer_manager.cc +4 -9
  112. data/src/core/lib/event_engine/posix_engine/traced_buffer_list.cc +0 -1
  113. data/src/core/lib/event_engine/resolved_address.cc +0 -1
  114. data/src/core/lib/event_engine/slice.cc +0 -1
  115. data/src/core/lib/event_engine/thread_pool/thread_count.cc +0 -1
  116. data/src/core/lib/event_engine/thread_pool/work_stealing_thread_pool.cc +3 -5
  117. data/src/core/lib/event_engine/windows/grpc_polled_fd_windows.cc +121 -93
  118. data/src/core/lib/experiments/config.cc +12 -10
  119. data/src/core/lib/experiments/experiments.cc +45 -66
  120. data/src/core/lib/experiments/experiments.h +22 -27
  121. data/src/core/lib/gprpp/chunked_vector.h +0 -1
  122. data/src/core/lib/gprpp/down_cast.h +0 -1
  123. data/src/core/lib/gprpp/host_port.cc +0 -1
  124. data/src/core/lib/gprpp/load_file.cc +0 -1
  125. data/src/core/lib/gprpp/mpscq.h +0 -1
  126. data/src/core/lib/gprpp/single_set_ptr.h +0 -1
  127. data/src/core/lib/gprpp/status_helper.cc +0 -1
  128. data/src/core/lib/gprpp/sync.h +0 -1
  129. data/src/core/lib/gprpp/table.h +28 -0
  130. data/src/core/lib/gprpp/thd.h +0 -1
  131. data/src/core/lib/gprpp/time.h +0 -1
  132. data/src/core/lib/gprpp/time_util.cc +0 -1
  133. data/src/core/lib/gprpp/windows/directory_reader.cc +0 -2
  134. data/src/core/lib/gprpp/windows/thd.cc +0 -1
  135. data/src/core/lib/gprpp/work_serializer.cc +23 -34
  136. data/src/core/lib/iomgr/buffer_list.cc +0 -1
  137. data/src/core/lib/iomgr/call_combiner.h +6 -8
  138. data/src/core/lib/iomgr/cfstream_handle.cc +6 -8
  139. data/src/core/lib/iomgr/closure.h +5 -8
  140. data/src/core/lib/iomgr/combiner.cc +6 -8
  141. data/src/core/lib/iomgr/endpoint_cfstream.cc +17 -22
  142. data/src/core/lib/iomgr/endpoint_pair_posix.cc +0 -1
  143. data/src/core/lib/iomgr/error.h +0 -1
  144. data/src/core/lib/iomgr/ev_apple.cc +13 -18
  145. data/src/core/lib/iomgr/ev_epoll1_linux.cc +47 -85
  146. data/src/core/lib/iomgr/ev_poll_posix.cc +17 -24
  147. data/src/core/lib/iomgr/ev_posix.cc +55 -44
  148. data/src/core/lib/iomgr/ev_posix.h +0 -5
  149. data/src/core/lib/iomgr/event_engine_shims/closure.cc +7 -9
  150. data/src/core/lib/iomgr/event_engine_shims/endpoint.cc +3 -4
  151. data/src/core/lib/iomgr/exec_ctx.cc +6 -9
  152. data/src/core/lib/iomgr/exec_ctx.h +26 -16
  153. data/src/core/lib/iomgr/executor.cc +43 -33
  154. data/src/core/lib/iomgr/fork_windows.cc +0 -1
  155. data/src/core/lib/iomgr/internal_errqueue.cc +0 -1
  156. data/src/core/lib/iomgr/iocp_windows.cc +0 -1
  157. data/src/core/lib/iomgr/iomgr_windows.cc +0 -2
  158. data/src/core/lib/iomgr/lockfree_event.cc +7 -11
  159. data/src/core/lib/iomgr/polling_entity.cc +10 -3
  160. data/src/core/lib/iomgr/pollset_windows.cc +0 -2
  161. data/src/core/lib/iomgr/resolve_address.cc +0 -1
  162. data/src/core/lib/iomgr/resolve_address_posix.cc +0 -1
  163. data/src/core/lib/iomgr/resolve_address_windows.cc +0 -1
  164. data/src/core/lib/iomgr/sockaddr_utils_posix.cc +0 -1
  165. data/src/core/lib/iomgr/socket_mutator.cc +0 -1
  166. data/src/core/lib/iomgr/socket_utils_linux.cc +0 -2
  167. data/src/core/lib/iomgr/socket_utils_posix.cc +0 -1
  168. data/src/core/lib/iomgr/socket_utils_windows.cc +0 -2
  169. data/src/core/lib/iomgr/tcp_client_cfstream.cc +7 -12
  170. data/src/core/lib/iomgr/tcp_client_posix.cc +8 -12
  171. data/src/core/lib/iomgr/tcp_client_windows.cc +0 -1
  172. data/src/core/lib/iomgr/tcp_posix.cc +32 -68
  173. data/src/core/lib/iomgr/tcp_server_posix.cc +7 -11
  174. data/src/core/lib/iomgr/tcp_windows.cc +4 -12
  175. data/src/core/lib/iomgr/timer_generic.cc +46 -65
  176. data/src/core/lib/iomgr/timer_manager.cc +4 -5
  177. data/src/core/lib/iomgr/unix_sockets_posix.cc +0 -1
  178. data/src/core/lib/iomgr/unix_sockets_posix_noop.cc +0 -2
  179. data/src/core/lib/iomgr/vsock.cc +0 -1
  180. data/src/core/lib/iomgr/wakeup_fd_eventfd.cc +0 -2
  181. data/src/core/lib/promise/activity.h +0 -1
  182. data/src/core/lib/promise/context.h +0 -1
  183. data/src/core/lib/promise/detail/join_state.h +44 -44
  184. data/src/core/lib/promise/detail/seq_state.h +1101 -1356
  185. data/src/core/lib/promise/for_each.h +8 -15
  186. data/src/core/lib/promise/interceptor_list.h +17 -27
  187. data/src/core/lib/promise/latch.h +16 -24
  188. data/src/core/lib/promise/map.h +1 -1
  189. data/src/core/lib/promise/party.cc +238 -114
  190. data/src/core/lib/promise/party.h +105 -308
  191. data/src/core/lib/promise/pipe.h +3 -4
  192. data/src/core/lib/promise/poll.h +0 -1
  193. data/src/core/lib/promise/status_flag.h +0 -1
  194. data/src/core/lib/resource_quota/connection_quota.cc +0 -1
  195. data/src/core/lib/resource_quota/memory_quota.cc +11 -19
  196. data/src/core/lib/resource_quota/memory_quota.h +2 -4
  197. data/src/core/lib/resource_quota/periodic_update.cc +2 -3
  198. data/src/core/lib/resource_quota/thread_quota.cc +0 -1
  199. data/src/core/lib/security/authorization/audit_logging.cc +0 -1
  200. data/src/core/lib/security/authorization/grpc_authorization_engine.cc +0 -1
  201. data/src/core/lib/security/authorization/grpc_server_authz_filter.cc +14 -19
  202. data/src/core/lib/security/authorization/stdout_logger.cc +0 -1
  203. data/src/core/lib/security/credentials/alts/check_gcp_environment.cc +0 -1
  204. data/src/core/lib/security/credentials/alts/check_gcp_environment_windows.cc +0 -1
  205. data/src/core/lib/security/credentials/call_creds_util.cc +0 -1
  206. data/src/core/lib/security/credentials/composite/composite_credentials.cc +0 -1
  207. data/src/core/lib/security/credentials/composite/composite_credentials.h +2 -0
  208. data/src/core/lib/security/credentials/credentials.h +1 -2
  209. data/src/core/lib/security/credentials/external/aws_external_account_credentials.cc +322 -324
  210. data/src/core/lib/security/credentials/external/aws_external_account_credentials.h +53 -42
  211. data/src/core/lib/security/credentials/external/external_account_credentials.cc +391 -353
  212. data/src/core/lib/security/credentials/external/external_account_credentials.h +121 -51
  213. data/src/core/lib/security/credentials/external/file_external_account_credentials.cc +83 -44
  214. data/src/core/lib/security/credentials/external/file_external_account_credentials.h +27 -7
  215. data/src/core/lib/security/credentials/external/url_external_account_credentials.cc +91 -116
  216. data/src/core/lib/security/credentials/external/url_external_account_credentials.h +14 -17
  217. data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -0
  218. data/src/core/lib/security/credentials/gcp_service_account_identity/gcp_service_account_identity_credentials.cc +196 -0
  219. data/src/core/lib/security/credentials/gcp_service_account_identity/gcp_service_account_identity_credentials.h +90 -0
  220. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +27 -41
  221. data/src/core/lib/security/credentials/iam/iam_credentials.cc +0 -1
  222. data/src/core/lib/security/credentials/iam/iam_credentials.h +2 -0
  223. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +2 -0
  224. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +163 -259
  225. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +34 -56
  226. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +12 -16
  227. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  228. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +0 -1
  229. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.cc +0 -1
  230. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h +0 -1
  231. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_verifier.cc +0 -1
  232. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_verifier.h +0 -1
  233. data/src/core/lib/security/credentials/token_fetcher/token_fetcher_credentials.cc +298 -0
  234. data/src/core/lib/security/credentials/token_fetcher/token_fetcher_credentials.h +176 -0
  235. data/src/core/lib/security/credentials/xds/xds_credentials.cc +0 -1
  236. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +0 -1
  237. data/src/core/lib/security/security_connector/insecure/insecure_security_connector.cc +0 -1
  238. data/src/core/lib/security/security_connector/load_system_roots_windows.cc +0 -1
  239. data/src/core/lib/security/transport/server_auth_filter.cc +4 -6
  240. data/src/core/lib/slice/percent_encoding.cc +0 -1
  241. data/src/core/lib/slice/slice.cc +0 -1
  242. data/src/core/lib/slice/slice.h +0 -1
  243. data/src/core/lib/slice/slice_buffer.cc +0 -1
  244. data/src/core/lib/slice/slice_internal.h +0 -1
  245. data/src/core/lib/slice/slice_refcount.h +6 -8
  246. data/src/core/lib/surface/byte_buffer_reader.cc +0 -1
  247. data/src/core/lib/surface/call.cc +3 -5
  248. data/src/core/lib/surface/call_utils.h +0 -1
  249. data/src/core/lib/surface/channel.cc +0 -1
  250. data/src/core/lib/surface/channel_create.cc +0 -1
  251. data/src/core/lib/surface/channel_init.h +0 -1
  252. data/src/core/lib/surface/client_call.cc +0 -1
  253. data/src/core/lib/surface/client_call.h +0 -1
  254. data/src/core/lib/surface/completion_queue.cc +28 -4
  255. data/src/core/lib/surface/completion_queue_factory.cc +0 -1
  256. data/src/core/lib/surface/filter_stack_call.cc +9 -9
  257. data/src/core/lib/surface/filter_stack_call.h +0 -1
  258. data/src/core/lib/surface/lame_client.cc +0 -1
  259. data/src/core/lib/surface/server_call.cc +0 -1
  260. data/src/core/lib/surface/server_call.h +0 -1
  261. data/src/core/lib/surface/validate_metadata.h +0 -1
  262. data/src/core/lib/surface/version.cc +2 -2
  263. data/src/core/lib/transport/bdp_estimator.cc +9 -12
  264. data/src/core/lib/transport/bdp_estimator.h +6 -8
  265. data/src/core/lib/transport/call_arena_allocator.cc +2 -16
  266. data/src/core/lib/transport/call_arena_allocator.h +20 -5
  267. data/src/core/lib/transport/call_filters.cc +6 -9
  268. data/src/core/lib/transport/call_spine.h +24 -13
  269. data/src/core/lib/transport/connectivity_state.cc +34 -42
  270. data/src/core/lib/transport/metadata_batch.h +41 -1
  271. data/src/core/lib/transport/timeout_encoding.cc +0 -1
  272. data/src/core/lib/transport/transport.h +6 -8
  273. data/src/core/lib/transport/transport_op_string.cc +0 -1
  274. data/src/core/lib/uri/uri_parser.cc +0 -1
  275. data/src/core/load_balancing/grpclb/grpclb.cc +55 -71
  276. data/src/core/load_balancing/health_check_client.cc +31 -42
  277. data/src/core/load_balancing/oob_backend_metric.cc +2 -4
  278. data/src/core/load_balancing/outlier_detection/outlier_detection.cc +99 -129
  279. data/src/core/load_balancing/pick_first/pick_first.cc +168 -228
  280. data/src/core/load_balancing/priority/priority.cc +77 -106
  281. data/src/core/load_balancing/ring_hash/ring_hash.cc +32 -46
  282. data/src/core/load_balancing/rls/rls.cc +142 -187
  283. data/src/core/load_balancing/round_robin/round_robin.cc +36 -55
  284. data/src/core/load_balancing/weighted_round_robin/static_stride_scheduler.cc +0 -1
  285. data/src/core/load_balancing/weighted_round_robin/weighted_round_robin.cc +85 -110
  286. data/src/core/load_balancing/weighted_target/weighted_target.cc +52 -75
  287. data/src/core/load_balancing/xds/cds.cc +26 -43
  288. data/src/core/load_balancing/xds/xds_cluster_impl.cc +57 -54
  289. data/src/core/load_balancing/xds/xds_cluster_manager.cc +36 -50
  290. data/src/core/load_balancing/xds/xds_override_host.cc +95 -131
  291. data/src/core/load_balancing/xds/xds_wrr_locality.cc +15 -23
  292. data/src/core/plugin_registry/grpc_plugin_registry_extra.cc +3 -0
  293. data/src/core/resolver/binder/binder_resolver.cc +0 -2
  294. data/src/core/resolver/dns/c_ares/dns_resolver_ares.cc +62 -44
  295. data/src/core/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +0 -2
  296. data/src/core/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +110 -89
  297. data/src/core/resolver/dns/c_ares/grpc_ares_wrapper.cc +132 -96
  298. data/src/core/resolver/dns/c_ares/grpc_ares_wrapper.h +0 -7
  299. data/src/core/resolver/dns/dns_resolver_plugin.cc +0 -1
  300. data/src/core/resolver/dns/event_engine/event_engine_client_channel_resolver.cc +40 -39
  301. data/src/core/resolver/dns/native/dns_resolver.cc +8 -14
  302. data/src/core/resolver/endpoint_addresses.cc +0 -1
  303. data/src/core/resolver/fake/fake_resolver.cc +0 -1
  304. data/src/core/resolver/polling_resolver.cc +6 -15
  305. data/src/core/resolver/polling_resolver.h +1 -1
  306. data/src/core/resolver/xds/xds_config.cc +96 -0
  307. data/src/core/resolver/xds/xds_config.h +109 -0
  308. data/src/core/resolver/xds/xds_dependency_manager.cc +59 -154
  309. data/src/core/resolver/xds/xds_dependency_manager.h +1 -69
  310. data/src/core/resolver/xds/xds_resolver.cc +51 -55
  311. data/src/core/server/server.cc +2 -2
  312. data/src/core/server/server_config_selector_filter.cc +0 -1
  313. data/src/core/server/xds_server_config_fetcher.cc +4 -6
  314. data/src/core/service_config/service_config_call_data.h +2 -3
  315. data/src/core/service_config/service_config_channel_arg_filter.cc +0 -1
  316. data/src/core/service_config/service_config_impl.h +0 -1
  317. data/src/core/telemetry/call_tracer.cc +0 -1
  318. data/src/core/telemetry/metrics.h +0 -1
  319. data/src/core/telemetry/stats_data.cc +67 -0
  320. data/src/core/telemetry/stats_data.h +48 -0
  321. data/src/core/tsi/alts/handshaker/alts_shared_resource.cc +0 -1
  322. data/src/core/tsi/alts/handshaker/transport_security_common_api.h +0 -1
  323. data/src/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol.cc +0 -1
  324. data/src/core/tsi/fake_transport_security.cc +6 -5
  325. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +0 -1
  326. data/src/core/util/alloc.cc +0 -1
  327. data/src/core/util/gcp_metadata_query.cc +0 -1
  328. data/src/core/util/http_client/httpcli.cc +12 -15
  329. data/src/core/util/http_client/httpcli.h +16 -11
  330. data/src/core/util/http_client/parser.cc +3 -4
  331. data/src/core/util/json/json_reader.cc +0 -1
  332. data/src/core/util/latent_see.cc +29 -9
  333. data/src/core/util/latent_see.h +122 -27
  334. data/src/core/util/log.cc +36 -55
  335. data/src/core/util/lru_cache.h +104 -0
  336. data/src/core/util/msys/tmpfile.cc +0 -1
  337. data/src/core/util/posix/sync.cc +0 -1
  338. data/src/core/util/posix/time.cc +0 -1
  339. data/src/core/util/ring_buffer.h +123 -0
  340. data/src/core/util/spinlock.h +1 -2
  341. data/src/core/util/string.cc +7 -7
  342. data/src/core/util/sync.cc +0 -1
  343. data/src/core/util/sync_abseil.cc +0 -1
  344. data/src/core/util/time.cc +0 -1
  345. data/src/core/util/unique_ptr_with_bitset.h +86 -0
  346. data/src/core/util/useful.h +0 -24
  347. data/src/core/util/windows/cpu.cc +0 -1
  348. data/src/core/util/windows/sync.cc +0 -1
  349. data/src/core/util/windows/time.cc +0 -1
  350. data/src/core/util/windows/tmpfile.cc +0 -1
  351. data/src/core/xds/grpc/xds_bootstrap_grpc.cc +0 -32
  352. data/src/core/xds/grpc/xds_bootstrap_grpc.h +0 -5
  353. data/src/core/xds/grpc/xds_certificate_provider.cc +0 -1
  354. data/src/core/xds/grpc/xds_client_grpc.cc +11 -16
  355. data/src/core/xds/grpc/xds_cluster.cc +2 -8
  356. data/src/core/xds/grpc/xds_cluster.h +4 -4
  357. data/src/core/xds/grpc/xds_cluster_parser.cc +58 -96
  358. data/src/core/xds/grpc/xds_cluster_specifier_plugin.cc +0 -1
  359. data/src/core/xds/grpc/xds_common_types_parser.cc +4 -4
  360. data/src/core/xds/grpc/xds_common_types_parser.h +17 -0
  361. data/src/core/xds/grpc/xds_endpoint_parser.cc +14 -14
  362. data/src/core/xds/grpc/xds_http_fault_filter.cc +15 -6
  363. data/src/core/xds/grpc/xds_http_fault_filter.h +5 -1
  364. data/src/core/xds/grpc/xds_http_filter.h +11 -1
  365. data/src/core/xds/grpc/xds_http_filter_registry.cc +7 -1
  366. data/src/core/xds/grpc/xds_http_filter_registry.h +8 -1
  367. data/src/core/xds/grpc/xds_http_gcp_authn_filter.cc +142 -0
  368. data/src/core/xds/grpc/xds_http_gcp_authn_filter.h +61 -0
  369. data/src/core/xds/grpc/xds_http_rbac_filter.cc +14 -6
  370. data/src/core/xds/grpc/xds_http_rbac_filter.h +5 -1
  371. data/src/core/xds/grpc/xds_http_stateful_session_filter.cc +9 -1
  372. data/src/core/xds/grpc/xds_http_stateful_session_filter.h +5 -1
  373. data/src/core/xds/grpc/xds_lb_policy_registry.cc +14 -16
  374. data/src/core/xds/grpc/xds_listener_parser.cc +10 -11
  375. data/src/core/xds/grpc/xds_metadata.cc +62 -0
  376. data/src/core/xds/grpc/xds_metadata.h +127 -0
  377. data/src/core/xds/grpc/xds_metadata_parser.cc +143 -0
  378. data/src/core/xds/grpc/xds_metadata_parser.h +36 -0
  379. data/src/core/xds/grpc/xds_route_config_parser.cc +12 -17
  380. data/src/core/xds/grpc/xds_routing.cc +57 -22
  381. data/src/core/xds/grpc/xds_routing.h +10 -2
  382. data/src/core/xds/grpc/xds_transport_grpc.cc +0 -1
  383. data/src/core/xds/xds_client/xds_client.cc +124 -165
  384. data/src/core/xds/xds_client/xds_client_stats.cc +20 -27
  385. data/src/ruby/ext/grpc/rb_call.c +1 -1
  386. data/src/ruby/ext/grpc/rb_call_credentials.c +34 -27
  387. data/src/ruby/ext/grpc/rb_channel.c +22 -16
  388. data/src/ruby/ext/grpc/rb_event_thread.c +3 -2
  389. data/src/ruby/ext/grpc/rb_grpc.c +9 -8
  390. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +6 -10
  391. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +9 -15
  392. data/src/ruby/ext/grpc/rb_server.c +10 -8
  393. data/src/ruby/lib/grpc/generic/active_call.rb +8 -5
  394. data/src/ruby/lib/grpc/version.rb +1 -1
  395. data/src/ruby/spec/call_spec.rb +53 -40
  396. data/src/ruby/spec/channel_spec.rb +4 -2
  397. data/src/ruby/spec/client_server_spec.rb +148 -507
  398. data/src/ruby/spec/generic/active_call_spec.rb +64 -86
  399. data/src/ruby/spec/support/services.rb +3 -0
  400. data/third_party/boringssl-with-bazel/src/crypto/{fipsmodule/rand/fork_detect.h → bcm_support.h} +51 -6
  401. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/internal.h +43 -0
  402. data/third_party/boringssl-with-bazel/src/crypto/cpu_intel.c +72 -23
  403. data/third_party/boringssl-with-bazel/src/crypto/crypto.c +1 -1
  404. data/third_party/boringssl-with-bazel/src/crypto/ecdsa_extra/ecdsa_asn1.c +160 -14
  405. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/internal.h +2 -0
  406. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bcm.c +79 -78
  407. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bcm_interface.h +89 -0
  408. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{div.c → div.c.inc} +146 -179
  409. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{random.c → random.c.inc} +6 -8
  410. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{sqrt.c → sqrt.c.inc} +1 -1
  411. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/{e_aes.c → e_aes.c.inc} +9 -8
  412. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{ec_key.c → ec_key.c.inc} +11 -7
  413. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{p256-nistz.c → p256-nistz.c.inc} +104 -12
  414. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256-nistz.h +65 -8
  415. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/{ecdsa.c → ecdsa.c.inc} +52 -107
  416. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/internal.h +28 -11
  417. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h +1 -80
  418. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/{rand.c → rand.c.inc} +26 -40
  419. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/{padding.c → padding.c.inc} +2 -5
  420. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/{rsa_impl.c → rsa_impl.c.inc} +1 -1
  421. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/{self_check.c → self_check.c.inc} +9 -35
  422. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/service_indicator/internal.h +2 -2
  423. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/service_indicator/{service_indicator.c → service_indicator.c.inc} +2 -2
  424. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +293 -2
  425. data/third_party/boringssl-with-bazel/src/crypto/internal.h +69 -14
  426. data/third_party/boringssl-with-bazel/src/crypto/mem.c +7 -3
  427. data/third_party/boringssl-with-bazel/src/crypto/mldsa/internal.h +73 -0
  428. data/third_party/boringssl-with-bazel/src/crypto/mldsa/mldsa.c +1687 -0
  429. data/third_party/boringssl-with-bazel/src/crypto/mlkem/internal.h +90 -0
  430. data/third_party/boringssl-with-bazel/src/crypto/mlkem/mlkem.cc +1097 -0
  431. data/third_party/boringssl-with-bazel/src/crypto/obj/obj_dat.h +4 -1
  432. data/third_party/boringssl-with-bazel/src/crypto/pem/pem_lib.c +4 -5
  433. data/third_party/boringssl-with-bazel/src/crypto/pem/pem_pk8.c +2 -3
  434. data/third_party/boringssl-with-bazel/src/crypto/pem/pem_pkey.c +1 -1
  435. data/third_party/boringssl-with-bazel/src/crypto/pkcs8/internal.h +1 -0
  436. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/deterministic.c +9 -1
  437. data/third_party/boringssl-with-bazel/src/crypto/{fipsmodule/rand → rand_extra}/fork_detect.c +26 -28
  438. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/getentropy.c +9 -1
  439. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/ios.c +9 -1
  440. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/passive.c +19 -3
  441. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/rand_extra.c +26 -23
  442. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/sysrand_internal.h +37 -0
  443. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/trusty.c +9 -1
  444. data/third_party/boringssl-with-bazel/src/crypto/{fipsmodule/rand → rand_extra}/urandom.c +19 -19
  445. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/windows.c +8 -1
  446. data/third_party/boringssl-with-bazel/src/crypto/rsa_extra/internal.h +2 -0
  447. data/third_party/boringssl-with-bazel/src/include/openssl/asn1.h +14 -1
  448. data/third_party/boringssl-with-bazel/src/include/openssl/bn.h +14 -9
  449. data/third_party/boringssl-with-bazel/src/include/openssl/experimental/dilithium.h +13 -15
  450. data/third_party/boringssl-with-bazel/src/include/openssl/hpke.h +8 -6
  451. data/third_party/boringssl-with-bazel/src/include/openssl/mldsa.h +136 -0
  452. data/third_party/boringssl-with-bazel/src/include/openssl/mlkem.h +246 -0
  453. data/third_party/boringssl-with-bazel/src/include/openssl/nid.h +3 -0
  454. data/third_party/boringssl-with-bazel/src/include/openssl/pem.h +3 -4
  455. data/third_party/boringssl-with-bazel/src/include/openssl/service_indicator.h +2 -2
  456. data/third_party/boringssl-with-bazel/src/include/openssl/span.h +22 -0
  457. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +35 -5
  458. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +5 -6
  459. data/third_party/boringssl-with-bazel/src/ssl/d1_lib.cc +6 -0
  460. data/third_party/boringssl-with-bazel/src/ssl/d1_pkt.cc +6 -1
  461. data/third_party/boringssl-with-bazel/src/ssl/dtls_method.cc +13 -1
  462. data/third_party/boringssl-with-bazel/src/ssl/dtls_record.cc +289 -55
  463. data/third_party/boringssl-with-bazel/src/ssl/extensions.cc +2 -0
  464. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +69 -38
  465. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +14 -3
  466. data/third_party/boringssl-with-bazel/src/ssl/internal.h +107 -14
  467. data/third_party/boringssl-with-bazel/src/ssl/s3_both.cc +44 -16
  468. data/third_party/boringssl-with-bazel/src/ssl/s3_pkt.cc +21 -1
  469. data/third_party/boringssl-with-bazel/src/ssl/ssl_aead_ctx.cc +86 -1
  470. data/third_party/boringssl-with-bazel/src/ssl/ssl_buffer.cc +7 -4
  471. data/third_party/boringssl-with-bazel/src/ssl/ssl_key_share.cc +97 -3
  472. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +31 -2
  473. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +6 -0
  474. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +18 -4
  475. data/third_party/boringssl-with-bazel/src/ssl/tls13_enc.cc +96 -34
  476. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +15 -5
  477. data/third_party/boringssl-with-bazel/src/ssl/tls_record.cc +3 -23
  478. metadata +113 -87
  479. data/src/core/ext/transport/chttp2/transport/max_concurrent_streams_policy.cc +0 -45
  480. data/src/core/ext/transport/chttp2/transport/max_concurrent_streams_policy.h +0 -67
  481. data/src/core/util/android/log.cc +0 -48
  482. data/src/core/util/linux/log.cc +0 -69
  483. data/src/core/util/posix/log.cc +0 -69
  484. data/src/core/util/windows/log.cc +0 -73
  485. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/{aes.c → aes.c.inc} +0 -0
  486. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/{aes_nohw.c → aes_nohw.c.inc} +0 -0
  487. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/{key_wrap.c → key_wrap.c.inc} +0 -0
  488. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/{mode_wrappers.c → mode_wrappers.c.inc} +0 -0
  489. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{add.c → add.c.inc} +0 -0
  490. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/asm/{x86_64-gcc.c → x86_64-gcc.c.inc} +0 -0
  491. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{bn.c → bn.c.inc} +0 -0
  492. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{bytes.c → bytes.c.inc} +0 -0
  493. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{cmp.c → cmp.c.inc} +0 -0
  494. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{ctx.c → ctx.c.inc} +0 -0
  495. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{div_extra.c → div_extra.c.inc} +0 -0
  496. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{exponentiation.c → exponentiation.c.inc} +0 -0
  497. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{gcd.c → gcd.c.inc} +0 -0
  498. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{gcd_extra.c → gcd_extra.c.inc} +0 -0
  499. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{generic.c → generic.c.inc} +0 -0
  500. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{jacobi.c → jacobi.c.inc} +0 -0
  501. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{montgomery.c → montgomery.c.inc} +0 -0
  502. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{montgomery_inv.c → montgomery_inv.c.inc} +0 -0
  503. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{mul.c → mul.c.inc} +0 -0
  504. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{prime.c → prime.c.inc} +0 -0
  505. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{rsaz_exp.c → rsaz_exp.c.inc} +0 -0
  506. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/{shift.c → shift.c.inc} +0 -0
  507. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/{aead.c → aead.c.inc} +0 -0
  508. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/{cipher.c → cipher.c.inc} +0 -0
  509. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/{e_aesccm.c → e_aesccm.c.inc} +0 -0
  510. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cmac/{cmac.c → cmac.c.inc} +0 -0
  511. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/dh/{check.c → check.c.inc} +0 -0
  512. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/dh/{dh.c → dh.c.inc} +0 -0
  513. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/{digest.c → digest.c.inc} +0 -0
  514. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/{digests.c → digests.c.inc} +0 -0
  515. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digestsign/{digestsign.c → digestsign.c.inc} +0 -0
  516. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{ec.c → ec.c.inc} +0 -0
  517. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{ec_montgomery.c → ec_montgomery.c.inc} +0 -0
  518. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{felem.c → felem.c.inc} +0 -0
  519. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{oct.c → oct.c.inc} +0 -0
  520. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{p224-64.c → p224-64.c.inc} +0 -0
  521. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{p256.c → p256.c.inc} +0 -0
  522. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{scalar.c → scalar.c.inc} +0 -0
  523. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{simple.c → simple.c.inc} +0 -0
  524. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{simple_mul.c → simple_mul.c.inc} +0 -0
  525. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{util.c → util.c.inc} +0 -0
  526. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/{wnaf.c → wnaf.c.inc} +0 -0
  527. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdh/{ecdh.c → ecdh.c.inc} +0 -0
  528. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/hkdf/{hkdf.c → hkdf.c.inc} +0 -0
  529. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/hmac/{hmac.c → hmac.c.inc} +0 -0
  530. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md4/{md4.c → md4.c.inc} +0 -0
  531. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md5/{md5.c → md5.c.inc} +0 -0
  532. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{cbc.c → cbc.c.inc} +0 -0
  533. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{cfb.c → cfb.c.inc} +0 -0
  534. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{ctr.c → ctr.c.inc} +0 -0
  535. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{gcm.c → gcm.c.inc} +0 -0
  536. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{gcm_nohw.c → gcm_nohw.c.inc} +0 -0
  537. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{ofb.c → ofb.c.inc} +0 -0
  538. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/{polyval.c → polyval.c.inc} +0 -0
  539. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/{ctrdrbg.c → ctrdrbg.c.inc} +0 -0
  540. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/{blinding.c → blinding.c.inc} +0 -0
  541. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/{rsa.c → rsa.c.inc} +0 -0
  542. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/{fips.c → fips.c.inc} +0 -0
  543. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/{sha1.c → sha1.c.inc} +0 -0
  544. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/{sha256.c → sha256.c.inc} +0 -0
  545. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/{sha512.c → sha512.c.inc} +0 -0
  546. /data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/tls/{kdf.c → kdf.c.inc} +0 -0
  547. /data/third_party/boringssl-with-bazel/src/crypto/{fipsmodule/rand → rand_extra}/getrandom_fillin.h +0 -0
@@ -0,0 +1,1097 @@
1
+ /* Copyright (c) 2024, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include <openssl/mlkem.h>
16
+
17
+ #include <assert.h>
18
+ #include <stdint.h>
19
+ #include <stdlib.h>
20
+ #include <string.h>
21
+
22
+ #include <openssl/base.h>
23
+ #include <openssl/bytestring.h>
24
+ #include <openssl/mem.h>
25
+ #include <openssl/rand.h>
26
+
27
+ #include "../internal.h"
28
+ #include "../keccak/internal.h"
29
+ #include "./internal.h"
30
+
31
+
32
+ // See
33
+ // https://csrc.nist.gov/pubs/fips/203/final
34
+
35
+ static void prf(uint8_t *out, size_t out_len, const uint8_t in[33]) {
36
+ BORINGSSL_keccak(out, out_len, in, 33, boringssl_shake256);
37
+ }
38
+
39
+ // Section 4.1
40
+ static void hash_h(uint8_t out[32], const uint8_t *in, size_t len) {
41
+ BORINGSSL_keccak(out, 32, in, len, boringssl_sha3_256);
42
+ }
43
+
44
+ static void hash_g(uint8_t out[64], const uint8_t *in, size_t len) {
45
+ BORINGSSL_keccak(out, 64, in, len, boringssl_sha3_512);
46
+ }
47
+
48
+ // This is called `J` in the spec.
49
+ static void kdf(uint8_t out[MLKEM_SHARED_SECRET_BYTES],
50
+ const uint8_t failure_secret[32], const uint8_t *ciphertext,
51
+ size_t ciphertext_len) {
52
+ struct BORINGSSL_keccak_st st;
53
+ BORINGSSL_keccak_init(&st, boringssl_shake256);
54
+ BORINGSSL_keccak_absorb(&st, failure_secret, 32);
55
+ BORINGSSL_keccak_absorb(&st, ciphertext, ciphertext_len);
56
+ BORINGSSL_keccak_squeeze(&st, out, MLKEM_SHARED_SECRET_BYTES);
57
+ }
58
+
59
+ // Constants that are common across all sizes.
60
+ #define DEGREE 256
61
+ static const size_t kBarrettMultiplier = 5039;
62
+ static const unsigned kBarrettShift = 24;
63
+ static const uint16_t kPrime = 3329;
64
+ static const int kLog2Prime = 12;
65
+ static const uint16_t kHalfPrime = (/*kPrime=*/3329 - 1) / 2;
66
+ // kInverseDegree is 128^-1 mod 3329; 128 because kPrime does not have a 512th
67
+ // root of unity.
68
+ static const uint16_t kInverseDegree = 3303;
69
+
70
+ // Rank-specific constants.
71
+ #define RANK768 3
72
+ static const int kDU768 = 10;
73
+ static const int kDV768 = 4;
74
+ #define RANK1024 4
75
+ static const int kDU1024 = 11;
76
+ static const int kDV1024 = 5;
77
+
78
+ constexpr size_t encoded_vector_size(int rank) {
79
+ return (kLog2Prime * DEGREE / 8) * static_cast<size_t>(rank);
80
+ }
81
+
82
+ constexpr size_t encoded_public_key_size(int rank) {
83
+ return encoded_vector_size(rank) + /*sizeof(rho)=*/32;
84
+ }
85
+
86
+ static_assert(encoded_public_key_size(RANK768) == MLKEM768_PUBLIC_KEY_BYTES,
87
+ "");
88
+ static_assert(encoded_public_key_size(RANK1024) == MLKEM1024_PUBLIC_KEY_BYTES,
89
+ "");
90
+
91
+ constexpr size_t compressed_vector_size(int rank) {
92
+ // `if constexpr` isn't available in C++17.
93
+ return (rank == RANK768 ? kDU768 : kDU1024) * static_cast<size_t>(rank) *
94
+ DEGREE / 8;
95
+ }
96
+
97
+ constexpr size_t ciphertext_size(int rank) {
98
+ return compressed_vector_size(rank) +
99
+ (rank == RANK768 ? kDV768 : kDV1024) * DEGREE / 8;
100
+ }
101
+
102
+ static_assert(ciphertext_size(RANK768) == MLKEM768_CIPHERTEXT_BYTES, "");
103
+ static_assert(ciphertext_size(RANK1024) == MLKEM1024_CIPHERTEXT_BYTES, "");
104
+
105
+ typedef struct scalar {
106
+ // On every function entry and exit, 0 <= c < kPrime.
107
+ uint16_t c[DEGREE];
108
+ } scalar;
109
+
110
+ template <int RANK>
111
+ struct vector {
112
+ scalar v[RANK];
113
+ };
114
+
115
+ template <int RANK>
116
+ struct matrix {
117
+ scalar v[RANK][RANK];
118
+ };
119
+
120
+ // This bit of Python will be referenced in some of the following comments:
121
+ //
122
+ // p = 3329
123
+ //
124
+ // def bitreverse(i):
125
+ // ret = 0
126
+ // for n in range(7):
127
+ // bit = i & 1
128
+ // ret <<= 1
129
+ // ret |= bit
130
+ // i >>= 1
131
+ // return ret
132
+
133
+ // kNTTRoots = [pow(17, bitreverse(i), p) for i in range(128)]
134
+ static const uint16_t kNTTRoots[128] = {
135
+ 1, 1729, 2580, 3289, 2642, 630, 1897, 848, 1062, 1919, 193, 797,
136
+ 2786, 3260, 569, 1746, 296, 2447, 1339, 1476, 3046, 56, 2240, 1333,
137
+ 1426, 2094, 535, 2882, 2393, 2879, 1974, 821, 289, 331, 3253, 1756,
138
+ 1197, 2304, 2277, 2055, 650, 1977, 2513, 632, 2865, 33, 1320, 1915,
139
+ 2319, 1435, 807, 452, 1438, 2868, 1534, 2402, 2647, 2617, 1481, 648,
140
+ 2474, 3110, 1227, 910, 17, 2761, 583, 2649, 1637, 723, 2288, 1100,
141
+ 1409, 2662, 3281, 233, 756, 2156, 3015, 3050, 1703, 1651, 2789, 1789,
142
+ 1847, 952, 1461, 2687, 939, 2308, 2437, 2388, 733, 2337, 268, 641,
143
+ 1584, 2298, 2037, 3220, 375, 2549, 2090, 1645, 1063, 319, 2773, 757,
144
+ 2099, 561, 2466, 2594, 2804, 1092, 403, 1026, 1143, 2150, 2775, 886,
145
+ 1722, 1212, 1874, 1029, 2110, 2935, 885, 2154,
146
+ };
147
+
148
+ // kInverseNTTRoots = [pow(17, -bitreverse(i), p) for i in range(128)]
149
+ static const uint16_t kInverseNTTRoots[128] = {
150
+ 1, 1600, 40, 749, 2481, 1432, 2699, 687, 1583, 2760, 69, 543,
151
+ 2532, 3136, 1410, 2267, 2508, 1355, 450, 936, 447, 2794, 1235, 1903,
152
+ 1996, 1089, 3273, 283, 1853, 1990, 882, 3033, 2419, 2102, 219, 855,
153
+ 2681, 1848, 712, 682, 927, 1795, 461, 1891, 2877, 2522, 1894, 1010,
154
+ 1414, 2009, 3296, 464, 2697, 816, 1352, 2679, 1274, 1052, 1025, 2132,
155
+ 1573, 76, 2998, 3040, 1175, 2444, 394, 1219, 2300, 1455, 2117, 1607,
156
+ 2443, 554, 1179, 2186, 2303, 2926, 2237, 525, 735, 863, 2768, 1230,
157
+ 2572, 556, 3010, 2266, 1684, 1239, 780, 2954, 109, 1292, 1031, 1745,
158
+ 2688, 3061, 992, 2596, 941, 892, 1021, 2390, 642, 1868, 2377, 1482,
159
+ 1540, 540, 1678, 1626, 279, 314, 1173, 2573, 3096, 48, 667, 1920,
160
+ 2229, 1041, 2606, 1692, 680, 2746, 568, 3312,
161
+ };
162
+
163
+ // kModRoots = [pow(17, 2*bitreverse(i) + 1, p) for i in range(128)]
164
+ static const uint16_t kModRoots[128] = {
165
+ 17, 3312, 2761, 568, 583, 2746, 2649, 680, 1637, 1692, 723, 2606,
166
+ 2288, 1041, 1100, 2229, 1409, 1920, 2662, 667, 3281, 48, 233, 3096,
167
+ 756, 2573, 2156, 1173, 3015, 314, 3050, 279, 1703, 1626, 1651, 1678,
168
+ 2789, 540, 1789, 1540, 1847, 1482, 952, 2377, 1461, 1868, 2687, 642,
169
+ 939, 2390, 2308, 1021, 2437, 892, 2388, 941, 733, 2596, 2337, 992,
170
+ 268, 3061, 641, 2688, 1584, 1745, 2298, 1031, 2037, 1292, 3220, 109,
171
+ 375, 2954, 2549, 780, 2090, 1239, 1645, 1684, 1063, 2266, 319, 3010,
172
+ 2773, 556, 757, 2572, 2099, 1230, 561, 2768, 2466, 863, 2594, 735,
173
+ 2804, 525, 1092, 2237, 403, 2926, 1026, 2303, 1143, 2186, 2150, 1179,
174
+ 2775, 554, 886, 2443, 1722, 1607, 1212, 2117, 1874, 1455, 1029, 2300,
175
+ 2110, 1219, 2935, 394, 885, 2444, 2154, 1175,
176
+ };
177
+
178
+ // reduce_once reduces 0 <= x < 2*kPrime, mod kPrime.
179
+ static uint16_t reduce_once(uint16_t x) {
180
+ assert(x < 2 * kPrime);
181
+ const uint16_t subtracted = x - kPrime;
182
+ uint16_t mask = 0u - (subtracted >> 15);
183
+ // On Aarch64, omitting a |value_barrier_u16| results in a 2x speedup of
184
+ // ML-KEM overall and Clang still produces constant-time code using `csel`. On
185
+ // other platforms & compilers on godbolt that we care about, this code also
186
+ // produces constant-time output.
187
+ return (mask & x) | (~mask & subtracted);
188
+ }
189
+
190
+ // constant time reduce x mod kPrime using Barrett reduction. x must be less
191
+ // than kPrime + 2×kPrime².
192
+ static uint16_t reduce(uint32_t x) {
193
+ assert(x < kPrime + 2u * kPrime * kPrime);
194
+ uint64_t product = (uint64_t)x * kBarrettMultiplier;
195
+ uint32_t quotient = (uint32_t)(product >> kBarrettShift);
196
+ uint32_t remainder = x - quotient * kPrime;
197
+ return reduce_once(remainder);
198
+ }
199
+
200
+ static void scalar_zero(scalar *out) { OPENSSL_memset(out, 0, sizeof(*out)); }
201
+
202
+ template <int RANK>
203
+ static void vector_zero(vector<RANK> *out) {
204
+ OPENSSL_memset(out->v, 0, sizeof(scalar) * RANK);
205
+ }
206
+
207
+ // In place number theoretic transform of a given scalar.
208
+ // Note that MLKEM's kPrime 3329 does not have a 512th root of unity, so this
209
+ // transform leaves off the last iteration of the usual FFT code, with the 128
210
+ // relevant roots of unity being stored in |kNTTRoots|. This means the output
211
+ // should be seen as 128 elements in GF(3329^2), with the coefficients of the
212
+ // elements being consecutive entries in |s->c|.
213
+ static void scalar_ntt(scalar *s) {
214
+ int offset = DEGREE;
215
+ // `int` is used here because using `size_t` throughout caused a ~5% slowdown
216
+ // with Clang 14 on Aarch64.
217
+ for (int step = 1; step < DEGREE / 2; step <<= 1) {
218
+ offset >>= 1;
219
+ int k = 0;
220
+ for (int i = 0; i < step; i++) {
221
+ const uint32_t step_root = kNTTRoots[i + step];
222
+ for (int j = k; j < k + offset; j++) {
223
+ uint16_t odd = reduce(step_root * s->c[j + offset]);
224
+ uint16_t even = s->c[j];
225
+ s->c[j] = reduce_once(odd + even);
226
+ s->c[j + offset] = reduce_once(even - odd + kPrime);
227
+ }
228
+ k += 2 * offset;
229
+ }
230
+ }
231
+ }
232
+
233
+ template <int RANK>
234
+ static void vector_ntt(vector<RANK> *a) {
235
+ for (int i = 0; i < RANK; i++) {
236
+ scalar_ntt(&a->v[i]);
237
+ }
238
+ }
239
+
240
+ // In place inverse number theoretic transform of a given scalar, with pairs of
241
+ // entries of s->v being interpreted as elements of GF(3329^2). Just as with the
242
+ // number theoretic transform, this leaves off the first step of the normal iFFT
243
+ // to account for the fact that 3329 does not have a 512th root of unity, using
244
+ // the precomputed 128 roots of unity stored in |kInverseNTTRoots|.
245
+ static void scalar_inverse_ntt(scalar *s) {
246
+ int step = DEGREE / 2;
247
+ // `int` is used here because using `size_t` throughout caused a ~5% slowdown
248
+ // with Clang 14 on Aarch64.
249
+ for (int offset = 2; offset < DEGREE; offset <<= 1) {
250
+ step >>= 1;
251
+ int k = 0;
252
+ for (int i = 0; i < step; i++) {
253
+ uint32_t step_root = kInverseNTTRoots[i + step];
254
+ for (int j = k; j < k + offset; j++) {
255
+ uint16_t odd = s->c[j + offset];
256
+ uint16_t even = s->c[j];
257
+ s->c[j] = reduce_once(odd + even);
258
+ s->c[j + offset] = reduce(step_root * (even - odd + kPrime));
259
+ }
260
+ k += 2 * offset;
261
+ }
262
+ }
263
+ for (int i = 0; i < DEGREE; i++) {
264
+ s->c[i] = reduce(s->c[i] * kInverseDegree);
265
+ }
266
+ }
267
+
268
+ template <int RANK>
269
+ static void vector_inverse_ntt(vector<RANK> *a) {
270
+ for (int i = 0; i < RANK; i++) {
271
+ scalar_inverse_ntt(&a->v[i]);
272
+ }
273
+ }
274
+
275
+ static void scalar_add(scalar *lhs, const scalar *rhs) {
276
+ for (int i = 0; i < DEGREE; i++) {
277
+ lhs->c[i] = reduce_once(lhs->c[i] + rhs->c[i]);
278
+ }
279
+ }
280
+
281
+ static void scalar_sub(scalar *lhs, const scalar *rhs) {
282
+ for (int i = 0; i < DEGREE; i++) {
283
+ lhs->c[i] = reduce_once(lhs->c[i] - rhs->c[i] + kPrime);
284
+ }
285
+ }
286
+
287
+ // Multiplying two scalars in the number theoretically transformed state. Since
288
+ // 3329 does not have a 512th root of unity, this means we have to interpret
289
+ // the 2*ith and (2*i+1)th entries of the scalar as elements of GF(3329)[X]/(X^2
290
+ // - 17^(2*bitreverse(i)+1)) The value of 17^(2*bitreverse(i)+1) mod 3329 is
291
+ // stored in the precomputed |kModRoots| table. Note that our Barrett transform
292
+ // only allows us to multipy two reduced numbers together, so we need some
293
+ // intermediate reduction steps, even if an uint64_t could hold 3 multiplied
294
+ // numbers.
295
+ static void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) {
296
+ for (int i = 0; i < DEGREE / 2; i++) {
297
+ uint32_t real_real = (uint32_t)lhs->c[2 * i] * rhs->c[2 * i];
298
+ uint32_t img_img = (uint32_t)lhs->c[2 * i + 1] * rhs->c[2 * i + 1];
299
+ uint32_t real_img = (uint32_t)lhs->c[2 * i] * rhs->c[2 * i + 1];
300
+ uint32_t img_real = (uint32_t)lhs->c[2 * i + 1] * rhs->c[2 * i];
301
+ out->c[2 * i] =
302
+ reduce(real_real + (uint32_t)reduce(img_img) * kModRoots[i]);
303
+ out->c[2 * i + 1] = reduce(img_real + real_img);
304
+ }
305
+ }
306
+
307
+ template <int RANK>
308
+ static void vector_add(vector<RANK> *lhs, const vector<RANK> *rhs) {
309
+ for (int i = 0; i < RANK; i++) {
310
+ scalar_add(&lhs->v[i], &rhs->v[i]);
311
+ }
312
+ }
313
+
314
+ template <int RANK>
315
+ static void matrix_mult(vector<RANK> *out, const matrix<RANK> *m,
316
+ const vector<RANK> *a) {
317
+ vector_zero(out);
318
+ for (int i = 0; i < RANK; i++) {
319
+ for (int j = 0; j < RANK; j++) {
320
+ scalar product;
321
+ scalar_mult(&product, &m->v[i][j], &a->v[j]);
322
+ scalar_add(&out->v[i], &product);
323
+ }
324
+ }
325
+ }
326
+
327
+ template <int RANK>
328
+ static void matrix_mult_transpose(vector<RANK> *out, const matrix<RANK> *m,
329
+ const vector<RANK> *a) {
330
+ vector_zero(out);
331
+ for (int i = 0; i < RANK; i++) {
332
+ for (int j = 0; j < RANK; j++) {
333
+ scalar product;
334
+ scalar_mult(&product, &m->v[j][i], &a->v[j]);
335
+ scalar_add(&out->v[i], &product);
336
+ }
337
+ }
338
+ }
339
+
340
+ template <int RANK>
341
+ static void scalar_inner_product(scalar *out, const vector<RANK> *lhs,
342
+ const vector<RANK> *rhs) {
343
+ scalar_zero(out);
344
+ for (int i = 0; i < RANK; i++) {
345
+ scalar product;
346
+ scalar_mult(&product, &lhs->v[i], &rhs->v[i]);
347
+ scalar_add(out, &product);
348
+ }
349
+ }
350
+
351
+ // Algorithm 6 from the spec. Rejection samples a Keccak stream to get
352
+ // uniformly distributed elements. This is used for matrix expansion and only
353
+ // operates on public inputs.
354
+ static void scalar_from_keccak_vartime(scalar *out,
355
+ struct BORINGSSL_keccak_st *keccak_ctx) {
356
+ assert(keccak_ctx->squeeze_offset == 0);
357
+ assert(keccak_ctx->rate_bytes == 168);
358
+ static_assert(168 % 3 == 0, "block and coefficient boundaries do not align");
359
+
360
+ int done = 0;
361
+ while (done < DEGREE) {
362
+ uint8_t block[168];
363
+ BORINGSSL_keccak_squeeze(keccak_ctx, block, sizeof(block));
364
+ for (size_t i = 0; i < sizeof(block) && done < DEGREE; i += 3) {
365
+ uint16_t d1 = block[i] + 256 * (block[i + 1] % 16);
366
+ uint16_t d2 = block[i + 1] / 16 + 16 * block[i + 2];
367
+ if (d1 < kPrime) {
368
+ out->c[done++] = d1;
369
+ }
370
+ if (d2 < kPrime && done < DEGREE) {
371
+ out->c[done++] = d2;
372
+ }
373
+ }
374
+ }
375
+ }
376
+
377
+ // Algorithm 7 from the spec, with eta fixed to two and the PRF call
378
+ // included. Creates binominally distributed elements by sampling 2*|eta| bits,
379
+ // and setting the coefficient to the count of the first bits minus the count of
380
+ // the second bits, resulting in a centered binomial distribution. Since eta is
381
+ // two this gives -2/2 with a probability of 1/16, -1/1 with probability 1/4,
382
+ // and 0 with probability 3/8.
383
+ static void scalar_centered_binomial_distribution_eta_2_with_prf(
384
+ scalar *out, const uint8_t input[33]) {
385
+ uint8_t entropy[128];
386
+ static_assert(sizeof(entropy) == 2 * /*kEta=*/2 * DEGREE / 8, "");
387
+ prf(entropy, sizeof(entropy), input);
388
+
389
+ for (int i = 0; i < DEGREE; i += 2) {
390
+ uint8_t byte = entropy[i / 2];
391
+
392
+ uint16_t value = kPrime;
393
+ value += (byte & 1) + ((byte >> 1) & 1);
394
+ value -= ((byte >> 2) & 1) + ((byte >> 3) & 1);
395
+ out->c[i] = reduce_once(value);
396
+
397
+ byte >>= 4;
398
+ value = kPrime;
399
+ value += (byte & 1) + ((byte >> 1) & 1);
400
+ value -= ((byte >> 2) & 1) + ((byte >> 3) & 1);
401
+ out->c[i + 1] = reduce_once(value);
402
+ }
403
+ }
404
+
405
+ // Generates a secret vector by using
406
+ // |scalar_centered_binomial_distribution_eta_2_with_prf|, using the given seed
407
+ // appending and incrementing |counter| for entry of the vector.
408
+ template <int RANK>
409
+ static void vector_generate_secret_eta_2(vector<RANK> *out, uint8_t *counter,
410
+ const uint8_t seed[32]) {
411
+ uint8_t input[33];
412
+ OPENSSL_memcpy(input, seed, 32);
413
+ for (int i = 0; i < RANK; i++) {
414
+ input[32] = (*counter)++;
415
+ scalar_centered_binomial_distribution_eta_2_with_prf(&out->v[i], input);
416
+ }
417
+ }
418
+
419
+ // Expands the matrix of a seed for key generation and for encaps-CPA.
420
+ template <int RANK>
421
+ static void matrix_expand(matrix<RANK> *out, const uint8_t rho[32]) {
422
+ uint8_t input[34];
423
+ OPENSSL_memcpy(input, rho, 32);
424
+ for (int i = 0; i < RANK; i++) {
425
+ for (int j = 0; j < RANK; j++) {
426
+ input[32] = i;
427
+ input[33] = j;
428
+ struct BORINGSSL_keccak_st keccak_ctx;
429
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake128);
430
+ BORINGSSL_keccak_absorb(&keccak_ctx, input, sizeof(input));
431
+ scalar_from_keccak_vartime(&out->v[i][j], &keccak_ctx);
432
+ }
433
+ }
434
+ }
435
+
436
+ static const uint8_t kMasks[8] = {0x01, 0x03, 0x07, 0x0f,
437
+ 0x1f, 0x3f, 0x7f, 0xff};
438
+
439
+ static void scalar_encode(uint8_t *out, const scalar *s, int bits) {
440
+ assert(bits <= (int)sizeof(*s->c) * 8 && bits != 1);
441
+
442
+ uint8_t out_byte = 0;
443
+ int out_byte_bits = 0;
444
+
445
+ for (int i = 0; i < DEGREE; i++) {
446
+ uint16_t element = s->c[i];
447
+ int element_bits_done = 0;
448
+
449
+ while (element_bits_done < bits) {
450
+ int chunk_bits = bits - element_bits_done;
451
+ int out_bits_remaining = 8 - out_byte_bits;
452
+ if (chunk_bits >= out_bits_remaining) {
453
+ chunk_bits = out_bits_remaining;
454
+ out_byte |= (element & kMasks[chunk_bits - 1]) << out_byte_bits;
455
+ *out = out_byte;
456
+ out++;
457
+ out_byte_bits = 0;
458
+ out_byte = 0;
459
+ } else {
460
+ out_byte |= (element & kMasks[chunk_bits - 1]) << out_byte_bits;
461
+ out_byte_bits += chunk_bits;
462
+ }
463
+
464
+ element_bits_done += chunk_bits;
465
+ element >>= chunk_bits;
466
+ }
467
+ }
468
+
469
+ if (out_byte_bits > 0) {
470
+ *out = out_byte;
471
+ }
472
+ }
473
+
474
+ // scalar_encode_1 is |scalar_encode| specialised for |bits| == 1.
475
+ static void scalar_encode_1(uint8_t out[32], const scalar *s) {
476
+ for (int i = 0; i < DEGREE; i += 8) {
477
+ uint8_t out_byte = 0;
478
+ for (int j = 0; j < 8; j++) {
479
+ out_byte |= (s->c[i + j] & 1) << j;
480
+ }
481
+ *out = out_byte;
482
+ out++;
483
+ }
484
+ }
485
+
486
+ // Encodes an entire vector into 32*|RANK|*|bits| bytes. Note that since 256
487
+ // (DEGREE) is divisible by 8, the individual vector entries will always fill a
488
+ // whole number of bytes, so we do not need to worry about bit packing here.
489
+ template <int RANK>
490
+ static void vector_encode(uint8_t *out, const vector<RANK> *a, int bits) {
491
+ for (int i = 0; i < RANK; i++) {
492
+ scalar_encode(out + i * bits * DEGREE / 8, &a->v[i], bits);
493
+ }
494
+ }
495
+
496
+ // scalar_decode parses |DEGREE * bits| bits from |in| into |DEGREE| values in
497
+ // |out|. It returns one on success and zero if any parsed value is >=
498
+ // |kPrime|.
499
+ static int scalar_decode(scalar *out, const uint8_t *in, int bits) {
500
+ assert(bits <= (int)sizeof(*out->c) * 8 && bits != 1);
501
+
502
+ uint8_t in_byte = 0;
503
+ int in_byte_bits_left = 0;
504
+
505
+ for (int i = 0; i < DEGREE; i++) {
506
+ uint16_t element = 0;
507
+ int element_bits_done = 0;
508
+
509
+ while (element_bits_done < bits) {
510
+ if (in_byte_bits_left == 0) {
511
+ in_byte = *in;
512
+ in++;
513
+ in_byte_bits_left = 8;
514
+ }
515
+
516
+ int chunk_bits = bits - element_bits_done;
517
+ if (chunk_bits > in_byte_bits_left) {
518
+ chunk_bits = in_byte_bits_left;
519
+ }
520
+
521
+ element |= (in_byte & kMasks[chunk_bits - 1]) << element_bits_done;
522
+ in_byte_bits_left -= chunk_bits;
523
+ in_byte >>= chunk_bits;
524
+
525
+ element_bits_done += chunk_bits;
526
+ }
527
+
528
+ if (element >= kPrime) {
529
+ return 0;
530
+ }
531
+ out->c[i] = element;
532
+ }
533
+
534
+ return 1;
535
+ }
536
+
537
+ // scalar_decode_1 is |scalar_decode| specialised for |bits| == 1.
538
+ static void scalar_decode_1(scalar *out, const uint8_t in[32]) {
539
+ for (int i = 0; i < DEGREE; i += 8) {
540
+ uint8_t in_byte = *in;
541
+ in++;
542
+ for (int j = 0; j < 8; j++) {
543
+ out->c[i + j] = in_byte & 1;
544
+ in_byte >>= 1;
545
+ }
546
+ }
547
+ }
548
+
549
+ // Decodes 32*|RANK|*|bits| bytes from |in| into |out|. It returns one on
550
+ // success or zero if any parsed value is >= |kPrime|.
551
+ template <int RANK>
552
+ static int vector_decode(vector<RANK> *out, const uint8_t *in, int bits) {
553
+ for (int i = 0; i < RANK; i++) {
554
+ if (!scalar_decode(&out->v[i], in + i * bits * DEGREE / 8, bits)) {
555
+ return 0;
556
+ }
557
+ }
558
+ return 1;
559
+ }
560
+
561
+ // Compresses (lossily) an input |x| mod 3329 into |bits| many bits by grouping
562
+ // numbers close to each other together. The formula used is
563
+ // round(2^|bits|/kPrime*x) mod 2^|bits|.
564
+ // Uses Barrett reduction to achieve constant time. Since we need both the
565
+ // remainder (for rounding) and the quotient (as the result), we cannot use
566
+ // |reduce| here, but need to do the Barrett reduction directly.
567
+ static uint16_t compress(uint16_t x, int bits) {
568
+ uint32_t shifted = (uint32_t)x << bits;
569
+ uint64_t product = (uint64_t)shifted * kBarrettMultiplier;
570
+ uint32_t quotient = (uint32_t)(product >> kBarrettShift);
571
+ uint32_t remainder = shifted - quotient * kPrime;
572
+
573
+ // Adjust the quotient to round correctly:
574
+ // 0 <= remainder <= kHalfPrime round to 0
575
+ // kHalfPrime < remainder <= kPrime + kHalfPrime round to 1
576
+ // kPrime + kHalfPrime < remainder < 2 * kPrime round to 2
577
+ assert(remainder < 2u * kPrime);
578
+ quotient += 1 & constant_time_lt_w(kHalfPrime, remainder);
579
+ quotient += 1 & constant_time_lt_w(kPrime + kHalfPrime, remainder);
580
+ return quotient & ((1 << bits) - 1);
581
+ }
582
+
583
+ // Decompresses |x| by using an equi-distant representative. The formula is
584
+ // round(kPrime/2^|bits|*x). Note that 2^|bits| being the divisor allows us to
585
+ // implement this logic using only bit operations.
586
+ static uint16_t decompress(uint16_t x, int bits) {
587
+ uint32_t product = (uint32_t)x * kPrime;
588
+ uint32_t power = 1 << bits;
589
+ // This is |product| % power, since |power| is a power of 2.
590
+ uint32_t remainder = product & (power - 1);
591
+ // This is |product| / power, since |power| is a power of 2.
592
+ uint32_t lower = product >> bits;
593
+ // The rounding logic works since the first half of numbers mod |power| have a
594
+ // 0 as first bit, and the second half has a 1 as first bit, since |power| is
595
+ // a power of 2. As a 12 bit number, |remainder| is always positive, so we
596
+ // will shift in 0s for a right shift.
597
+ return lower + (remainder >> (bits - 1));
598
+ }
599
+
600
+ static void scalar_compress(scalar *s, int bits) {
601
+ for (int i = 0; i < DEGREE; i++) {
602
+ s->c[i] = compress(s->c[i], bits);
603
+ }
604
+ }
605
+
606
+ static void scalar_decompress(scalar *s, int bits) {
607
+ for (int i = 0; i < DEGREE; i++) {
608
+ s->c[i] = decompress(s->c[i], bits);
609
+ }
610
+ }
611
+
612
+ template <int RANK>
613
+ static void vector_compress(vector<RANK> *a, int bits) {
614
+ for (int i = 0; i < RANK; i++) {
615
+ scalar_compress(&a->v[i], bits);
616
+ }
617
+ }
618
+
619
+ template <int RANK>
620
+ static void vector_decompress(vector<RANK> *a, int bits) {
621
+ for (int i = 0; i < RANK; i++) {
622
+ scalar_decompress(&a->v[i], bits);
623
+ }
624
+ }
625
+
626
+ template <int RANK>
627
+ struct public_key {
628
+ vector<RANK> t;
629
+ uint8_t rho[32];
630
+ uint8_t public_key_hash[32];
631
+ matrix<RANK> m;
632
+ };
633
+
634
+ static struct public_key<RANK768> *public_key_768_from_external(
635
+ const struct MLKEM768_public_key *external) {
636
+ static_assert(sizeof(struct MLKEM768_public_key) >=
637
+ sizeof(struct public_key<RANK768>),
638
+ "MLKEM public key is too small");
639
+ static_assert(alignof(struct MLKEM768_public_key) >=
640
+ alignof(struct public_key<RANK768>),
641
+ "MLKEM public key alignment incorrect");
642
+ return (struct public_key<RANK768> *)external;
643
+ }
644
+
645
+ static struct public_key<RANK1024> *
646
+ public_key_1024_from_external(const struct MLKEM1024_public_key *external) {
647
+ static_assert(sizeof(struct MLKEM1024_public_key) >=
648
+ sizeof(struct public_key<RANK1024>),
649
+ "MLKEM1024 public key is too small");
650
+ static_assert(alignof(struct MLKEM1024_public_key) >=
651
+ alignof(struct public_key<RANK1024>),
652
+ "MLKEM1024 public key alignment incorrect");
653
+ return (struct public_key<RANK1024> *)external;
654
+ }
655
+
656
+ template <int RANK>
657
+ struct private_key {
658
+ struct public_key<RANK> pub;
659
+ vector<RANK> s;
660
+ uint8_t fo_failure_secret[32];
661
+ };
662
+
663
+ static struct private_key<RANK768> *private_key_768_from_external(
664
+ const struct MLKEM768_private_key *external) {
665
+ static_assert(sizeof(struct MLKEM768_private_key) >=
666
+ sizeof(struct private_key<RANK768>),
667
+ "MLKEM private key too small");
668
+ static_assert(alignof(struct MLKEM768_private_key) >=
669
+ alignof(struct private_key<RANK768>),
670
+ "MLKEM private key alignment incorrect");
671
+ return (struct private_key<RANK768> *)external;
672
+ }
673
+
674
+ static struct private_key<RANK1024> *
675
+ private_key_1024_from_external(const struct MLKEM1024_private_key *external) {
676
+ static_assert(sizeof(struct MLKEM1024_private_key) >=
677
+ sizeof(struct private_key<RANK1024>),
678
+ "MLKEM1024 private key too small");
679
+ static_assert(alignof(struct MLKEM1024_private_key) >=
680
+ alignof(struct private_key<RANK1024>),
681
+ "MLKEM1024 private key alignment incorrect");
682
+ return (struct private_key<RANK1024> *)external;
683
+ }
684
+
685
+ void MLKEM768_generate_key(uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES],
686
+ uint8_t optional_out_seed[MLKEM_SEED_BYTES],
687
+ struct MLKEM768_private_key *out_private_key) {
688
+ uint8_t seed[MLKEM_SEED_BYTES];
689
+ RAND_bytes(seed, sizeof(seed));
690
+ if (optional_out_seed) {
691
+ OPENSSL_memcpy(optional_out_seed, seed, sizeof(seed));
692
+ }
693
+ MLKEM768_generate_key_external_seed(out_encoded_public_key, out_private_key,
694
+ seed);
695
+ }
696
+
697
+ int MLKEM768_private_key_from_seed(struct MLKEM768_private_key *out_private_key,
698
+ const uint8_t *seed, size_t seed_len) {
699
+ if (seed_len != MLKEM_SEED_BYTES) {
700
+ return 0;
701
+ }
702
+ uint8_t public_key_bytes[MLKEM768_PUBLIC_KEY_BYTES];
703
+ MLKEM768_generate_key_external_seed(public_key_bytes, out_private_key, seed);
704
+ return 1;
705
+ }
706
+
707
+ void MLKEM1024_generate_key(
708
+ uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES],
709
+ uint8_t optional_out_seed[MLKEM_SEED_BYTES],
710
+ struct MLKEM1024_private_key *out_private_key) {
711
+ uint8_t seed[MLKEM_SEED_BYTES];
712
+ RAND_bytes(seed, sizeof(seed));
713
+ if (optional_out_seed) {
714
+ OPENSSL_memcpy(optional_out_seed, seed, sizeof(seed));
715
+ }
716
+ MLKEM1024_generate_key_external_seed(out_encoded_public_key, out_private_key,
717
+ seed);
718
+ }
719
+
720
+ int MLKEM1024_private_key_from_seed(
721
+ struct MLKEM1024_private_key *out_private_key, const uint8_t *seed,
722
+ size_t seed_len) {
723
+ if (seed_len != MLKEM_SEED_BYTES) {
724
+ return 0;
725
+ }
726
+ uint8_t public_key_bytes[MLKEM1024_PUBLIC_KEY_BYTES];
727
+ MLKEM1024_generate_key_external_seed(public_key_bytes, out_private_key, seed);
728
+ return 1;
729
+ }
730
+
731
+ template <int RANK>
732
+ static int mlkem_marshal_public_key(CBB *out,
733
+ const struct public_key<RANK> *pub) {
734
+ uint8_t *vector_output;
735
+ if (!CBB_add_space(out, &vector_output, encoded_vector_size(RANK))) {
736
+ return 0;
737
+ }
738
+ vector_encode(vector_output, &pub->t, kLog2Prime);
739
+ if (!CBB_add_bytes(out, pub->rho, sizeof(pub->rho))) {
740
+ return 0;
741
+ }
742
+ return 1;
743
+ }
744
+
745
+ template <int RANK>
746
+ void mlkem_generate_key_external_seed(uint8_t *out_encoded_public_key,
747
+ private_key<RANK> *priv,
748
+ const uint8_t seed[MLKEM_SEED_BYTES]) {
749
+ uint8_t augmented_seed[33];
750
+ OPENSSL_memcpy(augmented_seed, seed, 32);
751
+ augmented_seed[32] = RANK;
752
+
753
+ uint8_t hashed[64];
754
+ hash_g(hashed, augmented_seed, sizeof(augmented_seed));
755
+ const uint8_t *const rho = hashed;
756
+ const uint8_t *const sigma = hashed + 32;
757
+ OPENSSL_memcpy(priv->pub.rho, hashed, sizeof(priv->pub.rho));
758
+ matrix_expand(&priv->pub.m, rho);
759
+ uint8_t counter = 0;
760
+ vector_generate_secret_eta_2(&priv->s, &counter, sigma);
761
+ vector_ntt(&priv->s);
762
+ vector<RANK> error;
763
+ vector_generate_secret_eta_2(&error, &counter, sigma);
764
+ vector_ntt(&error);
765
+ matrix_mult_transpose(&priv->pub.t, &priv->pub.m, &priv->s);
766
+ vector_add(&priv->pub.t, &error);
767
+
768
+ CBB cbb;
769
+ CBB_init_fixed(&cbb, out_encoded_public_key, encoded_public_key_size(RANK));
770
+ if (!mlkem_marshal_public_key(&cbb, &priv->pub)) {
771
+ abort();
772
+ }
773
+
774
+ hash_h(priv->pub.public_key_hash, out_encoded_public_key,
775
+ encoded_public_key_size(RANK));
776
+ OPENSSL_memcpy(priv->fo_failure_secret, seed + 32, 32);
777
+ }
778
+
779
+ void MLKEM768_generate_key_external_seed(
780
+ uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES],
781
+ struct MLKEM768_private_key *out_private_key,
782
+ const uint8_t seed[MLKEM_SEED_BYTES]) {
783
+ private_key<RANK768> *priv = private_key_768_from_external(out_private_key);
784
+ mlkem_generate_key_external_seed(out_encoded_public_key, priv, seed);
785
+ }
786
+
787
+ void MLKEM1024_generate_key_external_seed(
788
+ uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES],
789
+ struct MLKEM1024_private_key *out_private_key,
790
+ const uint8_t seed[MLKEM_SEED_BYTES]) {
791
+ private_key<RANK1024> *priv = private_key_1024_from_external(out_private_key);
792
+ mlkem_generate_key_external_seed(out_encoded_public_key, priv, seed);
793
+ }
794
+
795
+ void MLKEM768_public_from_private(
796
+ struct MLKEM768_public_key *out_public_key,
797
+ const struct MLKEM768_private_key *private_key) {
798
+ struct public_key<RANK768> *const pub =
799
+ public_key_768_from_external(out_public_key);
800
+ const struct ::private_key<RANK768> *const priv =
801
+ private_key_768_from_external(private_key);
802
+ *pub = priv->pub;
803
+ }
804
+
805
+ void MLKEM1024_public_from_private(
806
+ struct MLKEM1024_public_key *out_public_key,
807
+ const struct MLKEM1024_private_key *private_key) {
808
+ struct public_key<RANK1024> *const pub =
809
+ public_key_1024_from_external(out_public_key);
810
+ const struct ::private_key<RANK1024> *const priv =
811
+ private_key_1024_from_external(private_key);
812
+ *pub = priv->pub;
813
+ }
814
+
815
+ // Encrypts a message with given randomness to
816
+ // the ciphertext in |out|. Without applying the Fujisaki-Okamoto transform this
817
+ // would not result in a CCA secure scheme, since lattice schemes are vulnerable
818
+ // to decryption failure oracles.
819
+ template <int RANK>
820
+ static void encrypt_cpa(uint8_t *out, const struct public_key<RANK> *pub,
821
+ const uint8_t message[32],
822
+ const uint8_t randomness[32]) {
823
+ constexpr int du = RANK == RANK768 ? kDU768 : kDU1024;
824
+ constexpr int dv = RANK == RANK768 ? kDV768 : kDV1024;
825
+
826
+ uint8_t counter = 0;
827
+ vector<RANK> secret;
828
+ vector_generate_secret_eta_2(&secret, &counter, randomness);
829
+ vector_ntt(&secret);
830
+ vector<RANK> error;
831
+ vector_generate_secret_eta_2(&error, &counter, randomness);
832
+ uint8_t input[33];
833
+ OPENSSL_memcpy(input, randomness, 32);
834
+ input[32] = counter;
835
+ scalar scalar_error;
836
+ scalar_centered_binomial_distribution_eta_2_with_prf(&scalar_error, input);
837
+ vector<RANK> u;
838
+ matrix_mult(&u, &pub->m, &secret);
839
+ vector_inverse_ntt(&u);
840
+ vector_add(&u, &error);
841
+ scalar v;
842
+ scalar_inner_product(&v, &pub->t, &secret);
843
+ scalar_inverse_ntt(&v);
844
+ scalar_add(&v, &scalar_error);
845
+ scalar expanded_message;
846
+ scalar_decode_1(&expanded_message, message);
847
+ scalar_decompress(&expanded_message, 1);
848
+ scalar_add(&v, &expanded_message);
849
+ vector_compress(&u, du);
850
+ vector_encode(out, &u, du);
851
+ scalar_compress(&v, dv);
852
+ scalar_encode(out + compressed_vector_size(RANK), &v, dv);
853
+ }
854
+
855
+ // Calls |MLKEM768_encap_external_entropy| with random bytes from |RAND_bytes|
856
+ void MLKEM768_encap(uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES],
857
+ uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
858
+ const struct MLKEM768_public_key *public_key) {
859
+ uint8_t entropy[MLKEM_ENCAP_ENTROPY];
860
+ RAND_bytes(entropy, MLKEM_ENCAP_ENTROPY);
861
+ MLKEM768_encap_external_entropy(out_ciphertext, out_shared_secret, public_key,
862
+ entropy);
863
+ }
864
+
865
+ void MLKEM1024_encap(uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES],
866
+ uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
867
+ const struct MLKEM1024_public_key *public_key) {
868
+ uint8_t entropy[MLKEM_ENCAP_ENTROPY];
869
+ RAND_bytes(entropy, MLKEM_ENCAP_ENTROPY);
870
+ MLKEM1024_encap_external_entropy(out_ciphertext, out_shared_secret,
871
+ public_key, entropy);
872
+ }
873
+
874
+ // See section 6.2.
875
+ template <int RANK>
876
+ static void mlkem_encap_external_entropy(
877
+ uint8_t *out_ciphertext,
878
+ uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
879
+ const struct public_key<RANK> *pub,
880
+ const uint8_t entropy[MLKEM_ENCAP_ENTROPY]) {
881
+ uint8_t input[64];
882
+ OPENSSL_memcpy(input, entropy, MLKEM_ENCAP_ENTROPY);
883
+ OPENSSL_memcpy(input + MLKEM_ENCAP_ENTROPY, pub->public_key_hash,
884
+ sizeof(input) - MLKEM_ENCAP_ENTROPY);
885
+ uint8_t key_and_randomness[64];
886
+ hash_g(key_and_randomness, input, sizeof(input));
887
+ encrypt_cpa(out_ciphertext, pub, entropy, key_and_randomness + 32);
888
+ static_assert(MLKEM_SHARED_SECRET_BYTES == 32, "");
889
+ memcpy(out_shared_secret, key_and_randomness, 32);
890
+ }
891
+
892
+ void MLKEM768_encap_external_entropy(
893
+ uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES],
894
+ uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
895
+ const struct MLKEM768_public_key *public_key,
896
+ const uint8_t entropy[MLKEM_ENCAP_ENTROPY]) {
897
+ const struct ::public_key<RANK768> *pub =
898
+ public_key_768_from_external(public_key);
899
+ mlkem_encap_external_entropy(out_ciphertext, out_shared_secret, pub, entropy);
900
+ }
901
+
902
+ void MLKEM1024_encap_external_entropy(
903
+ uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES],
904
+ uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
905
+ const struct MLKEM1024_public_key *public_key,
906
+ const uint8_t entropy[MLKEM_ENCAP_ENTROPY]) {
907
+ const struct ::public_key<RANK1024> *pub =
908
+ public_key_1024_from_external(public_key);
909
+ mlkem_encap_external_entropy(out_ciphertext, out_shared_secret, pub, entropy);
910
+ }
911
+
912
+ template <int RANK>
913
+ static void decrypt_cpa(uint8_t out[32], const struct private_key<RANK> *priv,
914
+ const uint8_t ciphertext[MLKEM768_CIPHERTEXT_BYTES]) {
915
+ constexpr int du = RANK == RANK768 ? kDU768 : kDU1024;
916
+ constexpr int dv = RANK == RANK768 ? kDV768 : kDV1024;
917
+
918
+ vector<RANK> u;
919
+ vector_decode(&u, ciphertext, du);
920
+ vector_decompress(&u, du);
921
+ vector_ntt(&u);
922
+ scalar v;
923
+ scalar_decode(&v, ciphertext + compressed_vector_size(RANK), dv);
924
+ scalar_decompress(&v, dv);
925
+ scalar mask;
926
+ scalar_inner_product(&mask, &priv->s, &u);
927
+ scalar_inverse_ntt(&mask);
928
+ scalar_sub(&v, &mask);
929
+ scalar_compress(&v, 1);
930
+ scalar_encode_1(out, &v);
931
+ }
932
+
933
+ // See section 6.3
934
+ template <int RANK>
935
+ static void mlkem_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
936
+ const uint8_t *ciphertext,
937
+ const struct private_key<RANK> *priv) {
938
+ uint8_t decrypted[64];
939
+ decrypt_cpa(decrypted, priv, ciphertext);
940
+ OPENSSL_memcpy(decrypted + 32, priv->pub.public_key_hash,
941
+ sizeof(decrypted) - 32);
942
+ uint8_t key_and_randomness[64];
943
+ hash_g(key_and_randomness, decrypted, sizeof(decrypted));
944
+ constexpr size_t ciphertext_len = ciphertext_size(RANK);
945
+ uint8_t expected_ciphertext[MLKEM1024_CIPHERTEXT_BYTES];
946
+ static_assert(ciphertext_len <= sizeof(expected_ciphertext), "");
947
+ encrypt_cpa(expected_ciphertext, &priv->pub, decrypted,
948
+ key_and_randomness + 32);
949
+
950
+ uint8_t failure_key[32];
951
+ kdf(failure_key, priv->fo_failure_secret, ciphertext, ciphertext_len);
952
+
953
+ uint8_t mask = constant_time_eq_int_8(
954
+ CRYPTO_memcmp(ciphertext, expected_ciphertext, ciphertext_len), 0);
955
+ for (int i = 0; i < MLKEM_SHARED_SECRET_BYTES; i++) {
956
+ out_shared_secret[i] =
957
+ constant_time_select_8(mask, key_and_randomness[i], failure_key[i]);
958
+ }
959
+ }
960
+
961
+ int MLKEM768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
962
+ const uint8_t *ciphertext, size_t ciphertext_len,
963
+ const struct MLKEM768_private_key *private_key) {
964
+ if (ciphertext_len != MLKEM768_CIPHERTEXT_BYTES) {
965
+ RAND_bytes(out_shared_secret, MLKEM_SHARED_SECRET_BYTES);
966
+ return 0;
967
+ }
968
+ const struct ::private_key<RANK768> *priv =
969
+ private_key_768_from_external(private_key);
970
+ mlkem_decap(out_shared_secret, ciphertext, priv);
971
+ return 1;
972
+ }
973
+
974
+ int MLKEM1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
975
+ const uint8_t *ciphertext, size_t ciphertext_len,
976
+ const struct MLKEM1024_private_key *private_key) {
977
+ if (ciphertext_len != MLKEM1024_CIPHERTEXT_BYTES) {
978
+ RAND_bytes(out_shared_secret, MLKEM_SHARED_SECRET_BYTES);
979
+ return 0;
980
+ }
981
+ const struct ::private_key<RANK1024> *priv =
982
+ private_key_1024_from_external(private_key);
983
+ mlkem_decap(out_shared_secret, ciphertext, priv);
984
+ return 1;
985
+ }
986
+
987
+ int MLKEM768_marshal_public_key(CBB *out,
988
+ const struct MLKEM768_public_key *public_key) {
989
+ return mlkem_marshal_public_key(out,
990
+ public_key_768_from_external(public_key));
991
+ }
992
+
993
+ int MLKEM1024_marshal_public_key(
994
+ CBB *out, const struct MLKEM1024_public_key *public_key) {
995
+ return mlkem_marshal_public_key(out,
996
+ public_key_1024_from_external(public_key));
997
+ }
998
+
999
+ // mlkem_parse_public_key_no_hash parses |in| into |pub| but doesn't calculate
1000
+ // the value of |pub->public_key_hash|.
1001
+ template <int RANK>
1002
+ static int mlkem_parse_public_key_no_hash(struct public_key<RANK> *pub,
1003
+ CBS *in) {
1004
+ CBS t_bytes;
1005
+ if (!CBS_get_bytes(in, &t_bytes, encoded_vector_size(RANK)) ||
1006
+ !vector_decode(&pub->t, CBS_data(&t_bytes), kLog2Prime) ||
1007
+ !CBS_copy_bytes(in, pub->rho, sizeof(pub->rho))) {
1008
+ return 0;
1009
+ }
1010
+ matrix_expand(&pub->m, pub->rho);
1011
+ return 1;
1012
+ }
1013
+
1014
+ template <int RANK>
1015
+ static int mlkem_parse_public_key(struct public_key<RANK> *pub, CBS *in) {
1016
+ CBS orig_in = *in;
1017
+ if (!mlkem_parse_public_key_no_hash(pub, in) || //
1018
+ CBS_len(in) != 0) {
1019
+ return 0;
1020
+ }
1021
+ hash_h(pub->public_key_hash, CBS_data(&orig_in), CBS_len(&orig_in));
1022
+ return 1;
1023
+ }
1024
+
1025
+ int MLKEM768_parse_public_key(struct MLKEM768_public_key *public_key, CBS *in) {
1026
+ struct ::public_key<RANK768> *pub = public_key_768_from_external(public_key);
1027
+ return mlkem_parse_public_key(pub, in);
1028
+ }
1029
+
1030
+ int MLKEM1024_parse_public_key(struct MLKEM1024_public_key *public_key,
1031
+ CBS *in) {
1032
+ struct ::public_key<RANK1024> *pub =
1033
+ public_key_1024_from_external(public_key);
1034
+ return mlkem_parse_public_key(pub, in);
1035
+ }
1036
+
1037
+ template <int RANK>
1038
+ static int mlkem_marshal_private_key(CBB *out,
1039
+ const struct private_key<RANK> *priv) {
1040
+ uint8_t *s_output;
1041
+ if (!CBB_add_space(out, &s_output, encoded_vector_size(RANK))) {
1042
+ return 0;
1043
+ }
1044
+ vector_encode(s_output, &priv->s, kLog2Prime);
1045
+ if (!mlkem_marshal_public_key(out, &priv->pub) ||
1046
+ !CBB_add_bytes(out, priv->pub.public_key_hash,
1047
+ sizeof(priv->pub.public_key_hash)) ||
1048
+ !CBB_add_bytes(out, priv->fo_failure_secret,
1049
+ sizeof(priv->fo_failure_secret))) {
1050
+ return 0;
1051
+ }
1052
+ return 1;
1053
+ }
1054
+
1055
+ int MLKEM768_marshal_private_key(
1056
+ CBB *out, const struct MLKEM768_private_key *private_key) {
1057
+ const struct ::private_key<RANK768> *const priv =
1058
+ private_key_768_from_external(private_key);
1059
+ return mlkem_marshal_private_key(out, priv);
1060
+ }
1061
+
1062
+ int MLKEM1024_marshal_private_key(
1063
+ CBB *out, const struct MLKEM1024_private_key *private_key) {
1064
+ const struct ::private_key<RANK1024> *const priv =
1065
+ private_key_1024_from_external(private_key);
1066
+ return mlkem_marshal_private_key(out, priv);
1067
+ }
1068
+
1069
+ template <int RANK>
1070
+ static int mlkem_parse_private_key(struct private_key<RANK> *priv, CBS *in) {
1071
+ CBS s_bytes;
1072
+ if (!CBS_get_bytes(in, &s_bytes, encoded_vector_size(RANK)) ||
1073
+ !vector_decode(&priv->s, CBS_data(&s_bytes), kLog2Prime) ||
1074
+ !mlkem_parse_public_key_no_hash(&priv->pub, in) ||
1075
+ !CBS_copy_bytes(in, priv->pub.public_key_hash,
1076
+ sizeof(priv->pub.public_key_hash)) ||
1077
+ !CBS_copy_bytes(in, priv->fo_failure_secret,
1078
+ sizeof(priv->fo_failure_secret)) ||
1079
+ CBS_len(in) != 0) {
1080
+ return 0;
1081
+ }
1082
+ return 1;
1083
+ }
1084
+
1085
+ int MLKEM768_parse_private_key(struct MLKEM768_private_key *out_private_key,
1086
+ CBS *in) {
1087
+ struct private_key<RANK768> *const priv =
1088
+ private_key_768_from_external(out_private_key);
1089
+ return mlkem_parse_private_key(priv, in);
1090
+ }
1091
+
1092
+ int MLKEM1024_parse_private_key(struct MLKEM1024_private_key *out_private_key,
1093
+ CBS *in) {
1094
+ struct private_key<RANK1024> *const priv =
1095
+ private_key_1024_from_external(out_private_key);
1096
+ return mlkem_parse_private_key(priv, in);
1097
+ }