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,1687 @@
1
+ /* Copyright (c) 2024, Google LLC
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/mldsa.h>
16
+
17
+ #include <assert.h>
18
+ #include <stdlib.h>
19
+
20
+ #include <openssl/bytestring.h>
21
+ #include <openssl/mem.h>
22
+ #include <openssl/rand.h>
23
+
24
+ #include "../internal.h"
25
+ #include "../keccak/internal.h"
26
+ #include "./internal.h"
27
+
28
+ #define DEGREE 256
29
+ #define K 6
30
+ #define L 5
31
+ #define ETA 4
32
+ #define TAU 49
33
+ #define BETA 196
34
+ #define OMEGA 55
35
+
36
+ #define RHO_BYTES 32
37
+ #define SIGMA_BYTES 64
38
+ #define K_BYTES 32
39
+ #define TR_BYTES 64
40
+ #define MU_BYTES 64
41
+ #define RHO_PRIME_BYTES 64
42
+ #define LAMBDA_BITS 192
43
+ #define LAMBDA_BYTES (LAMBDA_BITS / 8)
44
+
45
+ // 2^23 - 2^13 + 1
46
+ static const uint32_t kPrime = 8380417;
47
+ // Inverse of -kPrime modulo 2^32
48
+ static const uint32_t kPrimeNegInverse = 4236238847;
49
+ static const int kDroppedBits = 13;
50
+ static const uint32_t kHalfPrime = (8380417 - 1) / 2;
51
+ static const uint32_t kGamma1 = 1 << 19;
52
+ static const uint32_t kGamma2 = (8380417 - 1) / 32;
53
+ // 256^-1 mod kPrime, in Montgomery form.
54
+ static const uint32_t kInverseDegreeMontgomery = 41978;
55
+
56
+ typedef struct scalar {
57
+ uint32_t c[DEGREE];
58
+ } scalar;
59
+
60
+ typedef struct vectork {
61
+ scalar v[K];
62
+ } vectork;
63
+
64
+ typedef struct vectorl {
65
+ scalar v[L];
66
+ } vectorl;
67
+
68
+ typedef struct matrix {
69
+ scalar v[K][L];
70
+ } matrix;
71
+
72
+ /* Arithmetic */
73
+
74
+ // This bit of Python will be referenced in some of the following comments:
75
+ //
76
+ // q = 8380417
77
+ // # Inverse of -q modulo 2^32
78
+ // q_neg_inverse = 4236238847
79
+ // # 2^64 modulo q
80
+ // montgomery_square = 2365951
81
+ //
82
+ // def bitreverse(i):
83
+ // ret = 0
84
+ // for n in range(8):
85
+ // bit = i & 1
86
+ // ret <<= 1
87
+ // ret |= bit
88
+ // i >>= 1
89
+ // return ret
90
+ //
91
+ // def montgomery_reduce(x):
92
+ // a = (x * q_neg_inverse) % 2**32
93
+ // b = x + a * q
94
+ // assert b & 0xFFFF_FFFF == 0
95
+ // c = b >> 32
96
+ // assert c < q
97
+ // return c
98
+ //
99
+ // def montgomery_transform(x):
100
+ // return montgomery_reduce(x * montgomery_square)
101
+
102
+ // kNTTRootsMontgomery = [
103
+ // montgomery_transform(pow(1753, bitreverse(i), q)) for i in range(256)
104
+ // ]
105
+ static const uint32_t kNTTRootsMontgomery[256] = {
106
+ 4193792, 25847, 5771523, 7861508, 237124, 7602457, 7504169, 466468,
107
+ 1826347, 2353451, 8021166, 6288512, 3119733, 5495562, 3111497, 2680103,
108
+ 2725464, 1024112, 7300517, 3585928, 7830929, 7260833, 2619752, 6271868,
109
+ 6262231, 4520680, 6980856, 5102745, 1757237, 8360995, 4010497, 280005,
110
+ 2706023, 95776, 3077325, 3530437, 6718724, 4788269, 5842901, 3915439,
111
+ 4519302, 5336701, 3574422, 5512770, 3539968, 8079950, 2348700, 7841118,
112
+ 6681150, 6736599, 3505694, 4558682, 3507263, 6239768, 6779997, 3699596,
113
+ 811944, 531354, 954230, 3881043, 3900724, 5823537, 2071892, 5582638,
114
+ 4450022, 6851714, 4702672, 5339162, 6927966, 3475950, 2176455, 6795196,
115
+ 7122806, 1939314, 4296819, 7380215, 5190273, 5223087, 4747489, 126922,
116
+ 3412210, 7396998, 2147896, 2715295, 5412772, 4686924, 7969390, 5903370,
117
+ 7709315, 7151892, 8357436, 7072248, 7998430, 1349076, 1852771, 6949987,
118
+ 5037034, 264944, 508951, 3097992, 44288, 7280319, 904516, 3958618,
119
+ 4656075, 8371839, 1653064, 5130689, 2389356, 8169440, 759969, 7063561,
120
+ 189548, 4827145, 3159746, 6529015, 5971092, 8202977, 1315589, 1341330,
121
+ 1285669, 6795489, 7567685, 6940675, 5361315, 4499357, 4751448, 3839961,
122
+ 2091667, 3407706, 2316500, 3817976, 5037939, 2244091, 5933984, 4817955,
123
+ 266997, 2434439, 7144689, 3513181, 4860065, 4621053, 7183191, 5187039,
124
+ 900702, 1859098, 909542, 819034, 495491, 6767243, 8337157, 7857917,
125
+ 7725090, 5257975, 2031748, 3207046, 4823422, 7855319, 7611795, 4784579,
126
+ 342297, 286988, 5942594, 4108315, 3437287, 5038140, 1735879, 203044,
127
+ 2842341, 2691481, 5790267, 1265009, 4055324, 1247620, 2486353, 1595974,
128
+ 4613401, 1250494, 2635921, 4832145, 5386378, 1869119, 1903435, 7329447,
129
+ 7047359, 1237275, 5062207, 6950192, 7929317, 1312455, 3306115, 6417775,
130
+ 7100756, 1917081, 5834105, 7005614, 1500165, 777191, 2235880, 3406031,
131
+ 7838005, 5548557, 6709241, 6533464, 5796124, 4656147, 594136, 4603424,
132
+ 6366809, 2432395, 2454455, 8215696, 1957272, 3369112, 185531, 7173032,
133
+ 5196991, 162844, 1616392, 3014001, 810149, 1652634, 4686184, 6581310,
134
+ 5341501, 3523897, 3866901, 269760, 2213111, 7404533, 1717735, 472078,
135
+ 7953734, 1723600, 6577327, 1910376, 6712985, 7276084, 8119771, 4546524,
136
+ 5441381, 6144432, 7959518, 6094090, 183443, 7403526, 1612842, 4834730,
137
+ 7826001, 3919660, 8332111, 7018208, 3937738, 1400424, 7534263, 1976782};
138
+
139
+ // Reduces x mod kPrime in constant time, where 0 <= x < 2*kPrime.
140
+ static uint32_t reduce_once(uint32_t x) {
141
+ declassify_assert(x < 2 * kPrime);
142
+ // return x < kPrime ? x : x - kPrime;
143
+ return constant_time_select_int(constant_time_lt_w(x, kPrime), x, x - kPrime);
144
+ }
145
+
146
+ // Returns the absolute value in constant time.
147
+ static uint32_t abs_signed(uint32_t x) {
148
+ // return is_positive(x) ? x : -x;
149
+ // Note: MSVC doesn't like applying the unary minus operator to unsigned types
150
+ // (warning C4146), so we write the negation as a bitwise not plus one
151
+ // (assuming two's complement representation).
152
+ return constant_time_select_int(constant_time_lt_w(x, 0x80000000), x, 0u - x);
153
+ }
154
+
155
+ // Returns the absolute value modulo kPrime.
156
+ static uint32_t abs_mod_prime(uint32_t x) {
157
+ declassify_assert(x < kPrime);
158
+ // return x > kHalfPrime ? kPrime - x : x;
159
+ return constant_time_select_int(constant_time_lt_w(kHalfPrime, x), kPrime - x,
160
+ x);
161
+ }
162
+
163
+ // Returns the maximum of two values in constant time.
164
+ static uint32_t maximum(uint32_t x, uint32_t y) {
165
+ // return x < y ? y : x;
166
+ return constant_time_select_int(constant_time_lt_w(x, y), y, x);
167
+ }
168
+
169
+ static uint32_t mod_sub(uint32_t a, uint32_t b) {
170
+ declassify_assert(a < kPrime);
171
+ declassify_assert(b < kPrime);
172
+ return reduce_once(kPrime + a - b);
173
+ }
174
+
175
+ static void scalar_add(scalar *out, const scalar *lhs, const scalar *rhs) {
176
+ for (int i = 0; i < DEGREE; i++) {
177
+ out->c[i] = reduce_once(lhs->c[i] + rhs->c[i]);
178
+ }
179
+ }
180
+
181
+ static void scalar_sub(scalar *out, const scalar *lhs, const scalar *rhs) {
182
+ for (int i = 0; i < DEGREE; i++) {
183
+ out->c[i] = mod_sub(lhs->c[i], rhs->c[i]);
184
+ }
185
+ }
186
+
187
+ static uint32_t reduce_montgomery(uint64_t x) {
188
+ declassify_assert(x <= ((uint64_t)kPrime << 32));
189
+ uint64_t a = (uint32_t)x * kPrimeNegInverse;
190
+ uint64_t b = x + a * kPrime;
191
+ declassify_assert((b & 0xffffffff) == 0);
192
+ uint32_t c = b >> 32;
193
+ return reduce_once(c);
194
+ }
195
+
196
+ // Multiply two scalars in the number theoretically transformed state.
197
+ static void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) {
198
+ for (int i = 0; i < DEGREE; i++) {
199
+ out->c[i] = reduce_montgomery((uint64_t)lhs->c[i] * (uint64_t)rhs->c[i]);
200
+ }
201
+ }
202
+
203
+ // In place number theoretic transform of a given scalar.
204
+ //
205
+ // FIPS 204, Algorithm 41 (`NTT`).
206
+ static void scalar_ntt(scalar *s) {
207
+ // Step: 1, 2, 4, 8, ..., 128
208
+ // Offset: 128, 64, 32, 16, ..., 1
209
+ int offset = DEGREE;
210
+ for (int step = 1; step < DEGREE; step <<= 1) {
211
+ offset >>= 1;
212
+ int k = 0;
213
+ for (int i = 0; i < step; i++) {
214
+ assert(k == 2 * offset * i);
215
+ const uint32_t step_root = kNTTRootsMontgomery[step + i];
216
+ for (int j = k; j < k + offset; j++) {
217
+ uint32_t even = s->c[j];
218
+ // |reduce_montgomery| works on values up to kPrime*R and R > 2*kPrime.
219
+ // |step_root| < kPrime because it's static data. |s->c[...]| is <
220
+ // kPrime by the invariants of that struct.
221
+ uint32_t odd =
222
+ reduce_montgomery((uint64_t)step_root * (uint64_t)s->c[j + offset]);
223
+ s->c[j] = reduce_once(odd + even);
224
+ s->c[j + offset] = mod_sub(even, odd);
225
+ }
226
+ k += 2 * offset;
227
+ }
228
+ }
229
+ }
230
+
231
+ // In place inverse number theoretic transform of a given scalar.
232
+ //
233
+ // FIPS 204, Algorithm 42 (`NTT^-1`).
234
+ static void scalar_inverse_ntt(scalar *s) {
235
+ // Step: 128, 64, 32, 16, ..., 1
236
+ // Offset: 1, 2, 4, 8, ..., 128
237
+ int step = DEGREE;
238
+ for (int offset = 1; offset < DEGREE; offset <<= 1) {
239
+ step >>= 1;
240
+ int k = 0;
241
+ for (int i = 0; i < step; i++) {
242
+ assert(k == 2 * offset * i);
243
+ const uint32_t step_root =
244
+ kPrime - kNTTRootsMontgomery[step + (step - 1 - i)];
245
+ for (int j = k; j < k + offset; j++) {
246
+ uint32_t even = s->c[j];
247
+ uint32_t odd = s->c[j + offset];
248
+ s->c[j] = reduce_once(odd + even);
249
+
250
+ // |reduce_montgomery| works on values up to kPrime*R and R > 2*kPrime.
251
+ // kPrime + even < 2*kPrime because |even| < kPrime, by the invariants
252
+ // of that structure. Thus kPrime + even - odd < 2*kPrime because odd >=
253
+ // 0, because it's unsigned and less than kPrime. Lastly step_root <
254
+ // kPrime, because |kNTTRootsMontgomery| is static data.
255
+ s->c[j + offset] = reduce_montgomery((uint64_t)step_root *
256
+ (uint64_t)(kPrime + even - odd));
257
+ }
258
+ k += 2 * offset;
259
+ }
260
+ }
261
+ for (int i = 0; i < DEGREE; i++) {
262
+ s->c[i] = reduce_montgomery((uint64_t)s->c[i] *
263
+ (uint64_t)kInverseDegreeMontgomery);
264
+ }
265
+ }
266
+
267
+ static void vectork_zero(vectork *out) { OPENSSL_memset(out, 0, sizeof(*out)); }
268
+
269
+ static void vectork_add(vectork *out, const vectork *lhs, const vectork *rhs) {
270
+ for (int i = 0; i < K; i++) {
271
+ scalar_add(&out->v[i], &lhs->v[i], &rhs->v[i]);
272
+ }
273
+ }
274
+
275
+ static void vectork_sub(vectork *out, const vectork *lhs, const vectork *rhs) {
276
+ for (int i = 0; i < K; i++) {
277
+ scalar_sub(&out->v[i], &lhs->v[i], &rhs->v[i]);
278
+ }
279
+ }
280
+
281
+ static void vectork_mult_scalar(vectork *out, const vectork *lhs,
282
+ const scalar *rhs) {
283
+ for (int i = 0; i < K; i++) {
284
+ scalar_mult(&out->v[i], &lhs->v[i], rhs);
285
+ }
286
+ }
287
+
288
+ static void vectork_ntt(vectork *a) {
289
+ for (int i = 0; i < K; i++) {
290
+ scalar_ntt(&a->v[i]);
291
+ }
292
+ }
293
+
294
+ static void vectork_inverse_ntt(vectork *a) {
295
+ for (int i = 0; i < K; i++) {
296
+ scalar_inverse_ntt(&a->v[i]);
297
+ }
298
+ }
299
+
300
+ static void vectorl_add(vectorl *out, const vectorl *lhs, const vectorl *rhs) {
301
+ for (int i = 0; i < L; i++) {
302
+ scalar_add(&out->v[i], &lhs->v[i], &rhs->v[i]);
303
+ }
304
+ }
305
+
306
+ static void vectorl_mult_scalar(vectorl *out, const vectorl *lhs,
307
+ const scalar *rhs) {
308
+ for (int i = 0; i < L; i++) {
309
+ scalar_mult(&out->v[i], &lhs->v[i], rhs);
310
+ }
311
+ }
312
+
313
+ static void vectorl_ntt(vectorl *a) {
314
+ for (int i = 0; i < L; i++) {
315
+ scalar_ntt(&a->v[i]);
316
+ }
317
+ }
318
+
319
+ static void vectorl_inverse_ntt(vectorl *a) {
320
+ for (int i = 0; i < L; i++) {
321
+ scalar_inverse_ntt(&a->v[i]);
322
+ }
323
+ }
324
+
325
+ static void matrix_mult(vectork *out, const matrix *m, const vectorl *a) {
326
+ vectork_zero(out);
327
+ for (int i = 0; i < K; i++) {
328
+ for (int j = 0; j < L; j++) {
329
+ scalar product;
330
+ scalar_mult(&product, &m->v[i][j], &a->v[j]);
331
+ scalar_add(&out->v[i], &out->v[i], &product);
332
+ }
333
+ }
334
+ }
335
+
336
+ /* Rounding & hints */
337
+
338
+ // FIPS 204, Algorithm 35 (`Power2Round`).
339
+ static void power2_round(uint32_t *r1, uint32_t *r0, uint32_t r) {
340
+ *r1 = r >> kDroppedBits;
341
+ *r0 = r - (*r1 << kDroppedBits);
342
+
343
+ uint32_t r0_adjusted = mod_sub(*r0, 1 << kDroppedBits);
344
+ uint32_t r1_adjusted = *r1 + 1;
345
+
346
+ // Mask is set iff r0 > 2^(dropped_bits - 1).
347
+ crypto_word_t mask =
348
+ constant_time_lt_w((uint32_t)(1 << (kDroppedBits - 1)), *r0);
349
+ // r0 = mask ? r0_adjusted : r0
350
+ *r0 = constant_time_select_int(mask, r0_adjusted, *r0);
351
+ // r1 = mask ? r1_adjusted : r1
352
+ *r1 = constant_time_select_int(mask, r1_adjusted, *r1);
353
+ }
354
+
355
+ // Scale back previously rounded value.
356
+ static void scale_power2_round(uint32_t *out, uint32_t r1) {
357
+ // Pre-condition: 0 <= r1 <= 2^10 - 1
358
+ assert(r1 < (1u << 10));
359
+
360
+ *out = r1 << kDroppedBits;
361
+
362
+ // Post-condition: 0 <= out <= 2^23 - 2^13 = kPrime - 1
363
+ assert(*out < kPrime);
364
+ }
365
+
366
+ // FIPS 204, Algorithm 37 (`HighBits`).
367
+ static uint32_t high_bits(uint32_t x) {
368
+ // Reference description (given 0 <= x < q):
369
+ //
370
+ // ```
371
+ // int32_t r0 = x mod+- (2 * kGamma2);
372
+ // if (x - r0 == q - 1) {
373
+ // return 0;
374
+ // } else {
375
+ // return (x - r0) / (2 * kGamma2);
376
+ // }
377
+ // ```
378
+ //
379
+ // Below is the formula taken from the reference implementation.
380
+ //
381
+ // Here, kGamma2 == 2^18 - 2^8
382
+ // This returns ((ceil(x / 2^7) * (2^10 + 1) + 2^21) / 2^22) mod 2^4
383
+ uint32_t r1 = (x + 127) >> 7;
384
+ r1 = (r1 * 1025 + (1 << 21)) >> 22;
385
+ r1 &= 15;
386
+ return r1;
387
+ }
388
+
389
+ // FIPS 204, Algorithm 36 (`Decompose`).
390
+ static void decompose(uint32_t *r1, int32_t *r0, uint32_t r) {
391
+ *r1 = high_bits(r);
392
+
393
+ *r0 = r;
394
+ *r0 -= *r1 * 2 * (int32_t)kGamma2;
395
+ *r0 -= (((int32_t)kHalfPrime - *r0) >> 31) & (int32_t)kPrime;
396
+ }
397
+
398
+ // FIPS 204, Algorithm 38 (`LowBits`).
399
+ static int32_t low_bits(uint32_t x) {
400
+ uint32_t r1;
401
+ int32_t r0;
402
+ decompose(&r1, &r0, x);
403
+ return r0;
404
+ }
405
+
406
+ // FIPS 204, Algorithm 39 (`MakeHint`).
407
+ //
408
+ // In the spec this takes two arguments, z and r, and is called with
409
+ // z = -ct0
410
+ // r = w - cs2 + ct0
411
+ //
412
+ // It then computes HighBits (algorithm 37) of z and z+r. But z+r is just w -
413
+ // cs2, so this takes three arguments and saves an addition.
414
+ static int32_t make_hint(uint32_t ct0, uint32_t cs2, uint32_t w) {
415
+ uint32_t r_plus_z = mod_sub(w, cs2);
416
+ uint32_t r = reduce_once(r_plus_z + ct0);
417
+ return high_bits(r) != high_bits(r_plus_z);
418
+ }
419
+
420
+ // FIPS 204, Algorithm 40 (`UseHint`).
421
+ static uint32_t use_hint_vartime(uint32_t h, uint32_t r) {
422
+ uint32_t r1;
423
+ int32_t r0;
424
+ decompose(&r1, &r0, r);
425
+
426
+ if (h) {
427
+ if (r0 > 0) {
428
+ // m = 16, thus |mod m| in the spec turns into |& 15|.
429
+ return (r1 + 1) & 15;
430
+ } else {
431
+ return (r1 - 1) & 15;
432
+ }
433
+ }
434
+ return r1;
435
+ }
436
+
437
+ static void scalar_power2_round(scalar *s1, scalar *s0, const scalar *s) {
438
+ for (int i = 0; i < DEGREE; i++) {
439
+ power2_round(&s1->c[i], &s0->c[i], s->c[i]);
440
+ }
441
+ }
442
+
443
+ static void scalar_scale_power2_round(scalar *out, const scalar *in) {
444
+ for (int i = 0; i < DEGREE; i++) {
445
+ scale_power2_round(&out->c[i], in->c[i]);
446
+ }
447
+ }
448
+
449
+ static void scalar_high_bits(scalar *out, const scalar *in) {
450
+ for (int i = 0; i < DEGREE; i++) {
451
+ out->c[i] = high_bits(in->c[i]);
452
+ }
453
+ }
454
+
455
+ static void scalar_low_bits(scalar *out, const scalar *in) {
456
+ for (int i = 0; i < DEGREE; i++) {
457
+ out->c[i] = low_bits(in->c[i]);
458
+ }
459
+ }
460
+
461
+ static void scalar_max(uint32_t *max, const scalar *s) {
462
+ for (int i = 0; i < DEGREE; i++) {
463
+ uint32_t abs = abs_mod_prime(s->c[i]);
464
+ *max = maximum(*max, abs);
465
+ }
466
+ }
467
+
468
+ static void scalar_max_signed(uint32_t *max, const scalar *s) {
469
+ for (int i = 0; i < DEGREE; i++) {
470
+ uint32_t abs = abs_signed(s->c[i]);
471
+ *max = maximum(*max, abs);
472
+ }
473
+ }
474
+
475
+ static void scalar_make_hint(scalar *out, const scalar *ct0, const scalar *cs2,
476
+ const scalar *w) {
477
+ for (int i = 0; i < DEGREE; i++) {
478
+ out->c[i] = make_hint(ct0->c[i], cs2->c[i], w->c[i]);
479
+ }
480
+ }
481
+
482
+ static void scalar_use_hint_vartime(scalar *out, const scalar *h,
483
+ const scalar *r) {
484
+ for (int i = 0; i < DEGREE; i++) {
485
+ out->c[i] = use_hint_vartime(h->c[i], r->c[i]);
486
+ }
487
+ }
488
+
489
+ static void vectork_power2_round(vectork *t1, vectork *t0, const vectork *t) {
490
+ for (int i = 0; i < K; i++) {
491
+ scalar_power2_round(&t1->v[i], &t0->v[i], &t->v[i]);
492
+ }
493
+ }
494
+
495
+ static void vectork_scale_power2_round(vectork *out, const vectork *in) {
496
+ for (int i = 0; i < K; i++) {
497
+ scalar_scale_power2_round(&out->v[i], &in->v[i]);
498
+ }
499
+ }
500
+
501
+ static void vectork_high_bits(vectork *out, const vectork *in) {
502
+ for (int i = 0; i < K; i++) {
503
+ scalar_high_bits(&out->v[i], &in->v[i]);
504
+ }
505
+ }
506
+
507
+ static void vectork_low_bits(vectork *out, const vectork *in) {
508
+ for (int i = 0; i < K; i++) {
509
+ scalar_low_bits(&out->v[i], &in->v[i]);
510
+ }
511
+ }
512
+
513
+ static uint32_t vectork_max(const vectork *a) {
514
+ uint32_t max = 0;
515
+ for (int i = 0; i < K; i++) {
516
+ scalar_max(&max, &a->v[i]);
517
+ }
518
+ return max;
519
+ }
520
+
521
+ static uint32_t vectork_max_signed(const vectork *a) {
522
+ uint32_t max = 0;
523
+ for (int i = 0; i < K; i++) {
524
+ scalar_max_signed(&max, &a->v[i]);
525
+ }
526
+ return max;
527
+ }
528
+
529
+ // The input vector contains only zeroes and ones.
530
+ static size_t vectork_count_ones(const vectork *a) {
531
+ size_t count = 0;
532
+ for (int i = 0; i < K; i++) {
533
+ for (int j = 0; j < DEGREE; j++) {
534
+ count += a->v[i].c[j];
535
+ }
536
+ }
537
+ return count;
538
+ }
539
+
540
+ static void vectork_make_hint(vectork *out, const vectork *ct0,
541
+ const vectork *cs2, const vectork *w) {
542
+ for (int i = 0; i < K; i++) {
543
+ scalar_make_hint(&out->v[i], &ct0->v[i], &cs2->v[i], &w->v[i]);
544
+ }
545
+ }
546
+
547
+ static void vectork_use_hint_vartime(vectork *out, const vectork *h,
548
+ const vectork *r) {
549
+ for (int i = 0; i < K; i++) {
550
+ scalar_use_hint_vartime(&out->v[i], &h->v[i], &r->v[i]);
551
+ }
552
+ }
553
+
554
+ static uint32_t vectorl_max(const vectorl *a) {
555
+ uint32_t max = 0;
556
+ for (int i = 0; i < L; i++) {
557
+ scalar_max(&max, &a->v[i]);
558
+ }
559
+ return max;
560
+ }
561
+
562
+ /* Bit packing */
563
+
564
+ // FIPS 204, Algorithm 16 (`SimpleBitPack`). Specialized to bitlen(b) = 4.
565
+ static void scalar_encode_4(uint8_t out[128], const scalar *s) {
566
+ // Every two elements lands on a byte boundary.
567
+ static_assert(DEGREE % 2 == 0, "DEGREE must be a multiple of 2");
568
+ for (int i = 0; i < DEGREE / 2; i++) {
569
+ uint32_t a = s->c[2 * i];
570
+ uint32_t b = s->c[2 * i + 1];
571
+ declassify_assert(a < 16);
572
+ declassify_assert(b < 16);
573
+ out[i] = a | (b << 4);
574
+ }
575
+ }
576
+
577
+ // FIPS 204, Algorithm 16 (`SimpleBitPack`). Specialized to bitlen(b) = 10.
578
+ static void scalar_encode_10(uint8_t out[320], const scalar *s) {
579
+ // Every four elements lands on a byte boundary.
580
+ static_assert(DEGREE % 4 == 0, "DEGREE must be a multiple of 4");
581
+ for (int i = 0; i < DEGREE / 4; i++) {
582
+ uint32_t a = s->c[4 * i];
583
+ uint32_t b = s->c[4 * i + 1];
584
+ uint32_t c = s->c[4 * i + 2];
585
+ uint32_t d = s->c[4 * i + 3];
586
+ declassify_assert(a < 1024);
587
+ declassify_assert(b < 1024);
588
+ declassify_assert(c < 1024);
589
+ declassify_assert(d < 1024);
590
+ out[5 * i] = (uint8_t)a;
591
+ out[5 * i + 1] = (uint8_t)((a >> 8) | (b << 2));
592
+ out[5 * i + 2] = (uint8_t)((b >> 6) | (c << 4));
593
+ out[5 * i + 3] = (uint8_t)((c >> 4) | (d << 6));
594
+ out[5 * i + 4] = (uint8_t)(d >> 2);
595
+ }
596
+ }
597
+
598
+ // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(b) = 4 and b =
599
+ // 2^19.
600
+ static void scalar_encode_signed_4_eta(uint8_t out[128], const scalar *s) {
601
+ // Every two elements lands on a byte boundary.
602
+ static_assert(DEGREE % 2 == 0, "DEGREE must be a multiple of 2");
603
+ for (int i = 0; i < DEGREE / 2; i++) {
604
+ uint32_t a = mod_sub(ETA, s->c[2 * i]);
605
+ uint32_t b = mod_sub(ETA, s->c[2 * i + 1]);
606
+ declassify_assert(a < 16);
607
+ declassify_assert(b < 16);
608
+ out[i] = a | (b << 4);
609
+ }
610
+ }
611
+
612
+ // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(b) = 13 and b =
613
+ // 2^12.
614
+ static void scalar_encode_signed_13_12(uint8_t out[416], const scalar *s) {
615
+ static const uint32_t kMax = 1u << 12;
616
+ // Every two elements lands on a byte boundary.
617
+ static_assert(DEGREE % 8 == 0, "DEGREE must be a multiple of 8");
618
+ for (int i = 0; i < DEGREE / 8; i++) {
619
+ uint32_t a = mod_sub(kMax, s->c[8 * i]);
620
+ uint32_t b = mod_sub(kMax, s->c[8 * i + 1]);
621
+ uint32_t c = mod_sub(kMax, s->c[8 * i + 2]);
622
+ uint32_t d = mod_sub(kMax, s->c[8 * i + 3]);
623
+ uint32_t e = mod_sub(kMax, s->c[8 * i + 4]);
624
+ uint32_t f = mod_sub(kMax, s->c[8 * i + 5]);
625
+ uint32_t g = mod_sub(kMax, s->c[8 * i + 6]);
626
+ uint32_t h = mod_sub(kMax, s->c[8 * i + 7]);
627
+ declassify_assert(a < (1u << 13));
628
+ declassify_assert(b < (1u << 13));
629
+ declassify_assert(c < (1u << 13));
630
+ declassify_assert(d < (1u << 13));
631
+ declassify_assert(e < (1u << 13));
632
+ declassify_assert(f < (1u << 13));
633
+ declassify_assert(g < (1u << 13));
634
+ declassify_assert(h < (1u << 13));
635
+ a |= b << 13;
636
+ a |= c << 26;
637
+ c >>= 6;
638
+ c |= d << 7;
639
+ c |= e << 20;
640
+ e >>= 12;
641
+ e |= f << 1;
642
+ e |= g << 14;
643
+ e |= h << 27;
644
+ h >>= 5;
645
+ OPENSSL_memcpy(&out[13 * i], &a, sizeof(a));
646
+ OPENSSL_memcpy(&out[13 * i + 4], &c, sizeof(c));
647
+ OPENSSL_memcpy(&out[13 * i + 8], &e, sizeof(e));
648
+ OPENSSL_memcpy(&out[13 * i + 12], &h, 1);
649
+ }
650
+ }
651
+
652
+ // FIPS 204, Algorithm 17 (`BitPack`). Specialized to bitlen(b) = 20 and b =
653
+ // 2^19.
654
+ static void scalar_encode_signed_20_19(uint8_t out[640], const scalar *s) {
655
+ static const uint32_t kMax = 1u << 19;
656
+ // Every two elements lands on a byte boundary.
657
+ static_assert(DEGREE % 4 == 0, "DEGREE must be a multiple of 4");
658
+ for (int i = 0; i < DEGREE / 4; i++) {
659
+ uint32_t a = mod_sub(kMax, s->c[4 * i]);
660
+ uint32_t b = mod_sub(kMax, s->c[4 * i + 1]);
661
+ uint32_t c = mod_sub(kMax, s->c[4 * i + 2]);
662
+ uint32_t d = mod_sub(kMax, s->c[4 * i + 3]);
663
+ declassify_assert(a < (1u << 20));
664
+ declassify_assert(b < (1u << 20));
665
+ declassify_assert(c < (1u << 20));
666
+ declassify_assert(d < (1u << 20));
667
+ a |= b << 20;
668
+ b >>= 12;
669
+ b |= c << 8;
670
+ b |= d << 28;
671
+ d >>= 4;
672
+ OPENSSL_memcpy(&out[10 * i], &a, sizeof(a));
673
+ OPENSSL_memcpy(&out[10 * i + 4], &b, sizeof(b));
674
+ OPENSSL_memcpy(&out[10 * i + 8], &d, 2);
675
+ }
676
+ }
677
+
678
+ // FIPS 204, Algorithm 17 (`BitPack`).
679
+ static void scalar_encode_signed(uint8_t *out, const scalar *s, int bits,
680
+ uint32_t max) {
681
+ if (bits == 4) {
682
+ assert(max == ETA);
683
+ scalar_encode_signed_4_eta(out, s);
684
+ } else if (bits == 20) {
685
+ assert(max == 1u << 19);
686
+ scalar_encode_signed_20_19(out, s);
687
+ } else {
688
+ assert(bits == 13);
689
+ assert(max == 1u << 12);
690
+ scalar_encode_signed_13_12(out, s);
691
+ }
692
+ }
693
+
694
+ // FIPS 204, Algorithm 18 (`SimpleBitUnpack`). Specialized for bitlen(b) == 10.
695
+ static void scalar_decode_10(scalar *out, const uint8_t in[320]) {
696
+ uint32_t v;
697
+ static_assert(DEGREE % 4 == 0, "DEGREE must be a multiple of 4");
698
+ for (int i = 0; i < DEGREE / 4; i++) {
699
+ OPENSSL_memcpy(&v, &in[5 * i], sizeof(v));
700
+ out->c[4 * i] = v & 0x3ff;
701
+ out->c[4 * i + 1] = (v >> 10) & 0x3ff;
702
+ out->c[4 * i + 2] = (v >> 20) & 0x3ff;
703
+ out->c[4 * i + 3] = (v >> 30) | (((uint32_t)in[5 * i + 4]) << 2);
704
+ }
705
+ }
706
+
707
+ // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 4 and b =
708
+ // eta.
709
+ static int scalar_decode_signed_4_eta(scalar *out, const uint8_t in[128]) {
710
+ uint32_t v;
711
+ static_assert(DEGREE % 8 == 0, "DEGREE must be a multiple of 8");
712
+ for (int i = 0; i < DEGREE / 8; i++) {
713
+ OPENSSL_memcpy(&v, &in[4 * i], sizeof(v));
714
+ static_assert(ETA == 4, "ETA must be 4");
715
+ // None of the nibbles may be >= 9. So if the MSB of any nibble is set, none
716
+ // of the other bits may be set. First, select all the MSBs.
717
+ const uint32_t msbs = v & 0x88888888u;
718
+ // For each nibble where the MSB is set, form a mask of all the other bits.
719
+ const uint32_t mask = (msbs >> 1) | (msbs >> 2) | (msbs >> 3);
720
+ // A nibble is only out of range in the case of invalid input, in which case
721
+ // it is okay to leak the value.
722
+ if (constant_time_declassify_int((mask & v) != 0)) {
723
+ return 0;
724
+ }
725
+
726
+ out->c[i * 8] = mod_sub(ETA, v & 15);
727
+ out->c[i * 8 + 1] = mod_sub(ETA, (v >> 4) & 15);
728
+ out->c[i * 8 + 2] = mod_sub(ETA, (v >> 8) & 15);
729
+ out->c[i * 8 + 3] = mod_sub(ETA, (v >> 12) & 15);
730
+ out->c[i * 8 + 4] = mod_sub(ETA, (v >> 16) & 15);
731
+ out->c[i * 8 + 5] = mod_sub(ETA, (v >> 20) & 15);
732
+ out->c[i * 8 + 6] = mod_sub(ETA, (v >> 24) & 15);
733
+ out->c[i * 8 + 7] = mod_sub(ETA, v >> 28);
734
+ }
735
+ return 1;
736
+ }
737
+
738
+ // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 13 and b =
739
+ // 2^12.
740
+ static void scalar_decode_signed_13_12(scalar *out, const uint8_t in[416]) {
741
+ static const uint32_t kMax = 1u << 12;
742
+ static const uint32_t k13Bits = (1u << 13) - 1;
743
+ static const uint32_t k7Bits = (1u << 7) - 1;
744
+
745
+ uint32_t a, b, c;
746
+ uint8_t d;
747
+ static_assert(DEGREE % 8 == 0, "DEGREE must be a multiple of 8");
748
+ for (int i = 0; i < DEGREE / 8; i++) {
749
+ OPENSSL_memcpy(&a, &in[13 * i], sizeof(a));
750
+ OPENSSL_memcpy(&b, &in[13 * i + 4], sizeof(b));
751
+ OPENSSL_memcpy(&c, &in[13 * i + 8], sizeof(c));
752
+ d = in[13 * i + 12];
753
+
754
+ // It's not possible for a 13-bit number to be out of range when the max is
755
+ // 2^12.
756
+ out->c[i * 8] = mod_sub(kMax, a & k13Bits);
757
+ out->c[i * 8 + 1] = mod_sub(kMax, (a >> 13) & k13Bits);
758
+ out->c[i * 8 + 2] = mod_sub(kMax, (a >> 26) | ((b & k7Bits) << 6));
759
+ out->c[i * 8 + 3] = mod_sub(kMax, (b >> 7) & k13Bits);
760
+ out->c[i * 8 + 4] = mod_sub(kMax, (b >> 20) | ((c & 1) << 12));
761
+ out->c[i * 8 + 5] = mod_sub(kMax, (c >> 1) & k13Bits);
762
+ out->c[i * 8 + 6] = mod_sub(kMax, (c >> 14) & k13Bits);
763
+ out->c[i * 8 + 7] = mod_sub(kMax, (c >> 27) | ((uint32_t)d) << 5);
764
+ }
765
+ }
766
+
767
+ // FIPS 204, Algorithm 19 (`BitUnpack`). Specialized to bitlen(a+b) = 20 and b =
768
+ // 2^19.
769
+ static void scalar_decode_signed_20_19(scalar *out, const uint8_t in[640]) {
770
+ static const uint32_t kMax = 1u << 19;
771
+ static const uint32_t k20Bits = (1u << 20) - 1;
772
+
773
+ uint32_t a, b;
774
+ uint16_t c;
775
+ static_assert(DEGREE % 4 == 0, "DEGREE must be a multiple of 4");
776
+ for (int i = 0; i < DEGREE / 4; i++) {
777
+ OPENSSL_memcpy(&a, &in[10 * i], sizeof(a));
778
+ OPENSSL_memcpy(&b, &in[10 * i + 4], sizeof(b));
779
+ OPENSSL_memcpy(&c, &in[10 * i + 8], sizeof(c));
780
+
781
+ // It's not possible for a 20-bit number to be out of range when the max is
782
+ // 2^19.
783
+ out->c[i * 4] = mod_sub(kMax, a & k20Bits);
784
+ out->c[i * 4 + 1] = mod_sub(kMax, (a >> 20) | ((b & 0xff) << 12));
785
+ out->c[i * 4 + 2] = mod_sub(kMax, (b >> 8) & k20Bits);
786
+ out->c[i * 4 + 3] = mod_sub(kMax, (b >> 28) | ((uint32_t)c) << 4);
787
+ }
788
+ }
789
+
790
+ // FIPS 204, Algorithm 19 (`BitUnpack`).
791
+ static int scalar_decode_signed(scalar *out, const uint8_t *in, int bits,
792
+ uint32_t max) {
793
+ if (bits == 4) {
794
+ assert(max == ETA);
795
+ return scalar_decode_signed_4_eta(out, in);
796
+ } else if (bits == 13) {
797
+ assert(max == (1u << 12));
798
+ scalar_decode_signed_13_12(out, in);
799
+ return 1;
800
+ } else if (bits == 20) {
801
+ assert(max == (1u << 19));
802
+ scalar_decode_signed_20_19(out, in);
803
+ return 1;
804
+ } else {
805
+ abort();
806
+ }
807
+ }
808
+
809
+ /* Expansion functions */
810
+
811
+ // FIPS 204, Algorithm 30 (`RejNTTPoly`).
812
+ //
813
+ // Rejection samples a Keccak stream to get uniformly distributed elements. This
814
+ // is used for matrix expansion and only operates on public inputs.
815
+ static void scalar_from_keccak_vartime(
816
+ scalar *out, const uint8_t derived_seed[RHO_BYTES + 2]) {
817
+ struct BORINGSSL_keccak_st keccak_ctx;
818
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake128);
819
+ BORINGSSL_keccak_absorb(&keccak_ctx, derived_seed, RHO_BYTES + 2);
820
+ assert(keccak_ctx.squeeze_offset == 0);
821
+ assert(keccak_ctx.rate_bytes == 168);
822
+ static_assert(168 % 3 == 0, "block and coefficient boundaries do not align");
823
+
824
+ int done = 0;
825
+ while (done < DEGREE) {
826
+ uint8_t block[168];
827
+ BORINGSSL_keccak_squeeze(&keccak_ctx, block, sizeof(block));
828
+ for (size_t i = 0; i < sizeof(block) && done < DEGREE; i += 3) {
829
+ // FIPS 204, Algorithm 14 (`CoeffFromThreeBytes`).
830
+ uint32_t value = (uint32_t)block[i] | ((uint32_t)block[i + 1] << 8) |
831
+ (((uint32_t)block[i + 2] & 0x7f) << 16);
832
+ if (value < kPrime) {
833
+ out->c[done++] = value;
834
+ }
835
+ }
836
+ }
837
+ }
838
+
839
+ // FIPS 204, Algorithm 31 (`RejBoundedPoly`).
840
+ static void scalar_uniform_eta_4(scalar *out,
841
+ const uint8_t derived_seed[SIGMA_BYTES + 2]) {
842
+ static_assert(ETA == 4, "This implementation is specialized for ETA == 4");
843
+
844
+ struct BORINGSSL_keccak_st keccak_ctx;
845
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
846
+ BORINGSSL_keccak_absorb(&keccak_ctx, derived_seed, SIGMA_BYTES + 2);
847
+ assert(keccak_ctx.squeeze_offset == 0);
848
+ assert(keccak_ctx.rate_bytes == 136);
849
+
850
+ int done = 0;
851
+ while (done < DEGREE) {
852
+ uint8_t block[136];
853
+ BORINGSSL_keccak_squeeze(&keccak_ctx, block, sizeof(block));
854
+ for (size_t i = 0; i < sizeof(block) && done < DEGREE; ++i) {
855
+ uint32_t t0 = block[i] & 0x0F;
856
+ uint32_t t1 = block[i] >> 4;
857
+ // FIPS 204, Algorithm 15 (`CoefFromHalfByte`). Although both the input
858
+ // and output here are secret, it is OK to leak when we rejected a byte.
859
+ // Individual bytes of the SHAKE-256 stream are (indistiguishable from)
860
+ // independent of each other and the original seed, so leaking information
861
+ // about the rejected bytes does not reveal the input or output.
862
+ if (constant_time_declassify_int(t0 < 9)) {
863
+ out->c[done++] = mod_sub(ETA, t0);
864
+ }
865
+ if (done < DEGREE && constant_time_declassify_int(t1 < 9)) {
866
+ out->c[done++] = mod_sub(ETA, t1);
867
+ }
868
+ }
869
+ }
870
+ }
871
+
872
+ // FIPS 204, Algorithm 34 (`ExpandMask`), but just a single step.
873
+ static void scalar_sample_mask(
874
+ scalar *out, const uint8_t derived_seed[RHO_PRIME_BYTES + 2]) {
875
+ uint8_t buf[640];
876
+ BORINGSSL_keccak(buf, sizeof(buf), derived_seed, RHO_PRIME_BYTES + 2,
877
+ boringssl_shake256);
878
+
879
+ scalar_decode_signed_20_19(out, buf);
880
+ }
881
+
882
+ // FIPS 204, Algorithm 29 (`SampleInBall`).
883
+ static void scalar_sample_in_ball_vartime(scalar *out, const uint8_t *seed,
884
+ int len) {
885
+ assert(len == 2 * LAMBDA_BYTES);
886
+
887
+ struct BORINGSSL_keccak_st keccak_ctx;
888
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
889
+ BORINGSSL_keccak_absorb(&keccak_ctx, seed, len);
890
+ assert(keccak_ctx.squeeze_offset == 0);
891
+ assert(keccak_ctx.rate_bytes == 136);
892
+
893
+ uint8_t block[136];
894
+ BORINGSSL_keccak_squeeze(&keccak_ctx, block, sizeof(block));
895
+
896
+ uint64_t signs = CRYPTO_load_u64_le(block);
897
+ int offset = 8;
898
+ // SampleInBall implements a Fisher–Yates shuffle, which unavoidably leaks
899
+ // where the zeros are by memory access pattern. Although this leak happens
900
+ // before bad signatures are rejected, this is safe. See
901
+ // https://boringssl-review.googlesource.com/c/boringssl/+/67747/comment/8d8f01ac_70af3f21/
902
+ CONSTTIME_DECLASSIFY(block + offset, sizeof(block) - offset);
903
+
904
+ OPENSSL_memset(out, 0, sizeof(*out));
905
+ for (size_t i = DEGREE - TAU; i < DEGREE; i++) {
906
+ size_t byte;
907
+ for (;;) {
908
+ if (offset == 136) {
909
+ BORINGSSL_keccak_squeeze(&keccak_ctx, block, sizeof(block));
910
+ // See above.
911
+ CONSTTIME_DECLASSIFY(block, sizeof(block));
912
+ offset = 0;
913
+ }
914
+
915
+ byte = block[offset++];
916
+ if (byte <= i) {
917
+ break;
918
+ }
919
+ }
920
+
921
+ out->c[i] = out->c[byte];
922
+ out->c[byte] = mod_sub(1, 2 * (signs & 1));
923
+ signs >>= 1;
924
+ }
925
+ }
926
+
927
+ // FIPS 204, Algorithm 32 (`ExpandA`).
928
+ static void matrix_expand(matrix *out, const uint8_t rho[RHO_BYTES]) {
929
+ static_assert(K <= 0x100, "K must fit in 8 bits");
930
+ static_assert(L <= 0x100, "L must fit in 8 bits");
931
+
932
+ uint8_t derived_seed[RHO_BYTES + 2];
933
+ OPENSSL_memcpy(derived_seed, rho, RHO_BYTES);
934
+ for (int i = 0; i < K; i++) {
935
+ for (int j = 0; j < L; j++) {
936
+ derived_seed[RHO_BYTES + 1] = (uint8_t)i;
937
+ derived_seed[RHO_BYTES] = (uint8_t)j;
938
+ scalar_from_keccak_vartime(&out->v[i][j], derived_seed);
939
+ }
940
+ }
941
+ }
942
+
943
+ // FIPS 204, Algorithm 33 (`ExpandS`).
944
+ static void vector_expand_short(vectorl *s1, vectork *s2,
945
+ const uint8_t sigma[SIGMA_BYTES]) {
946
+ static_assert(K <= 0x100, "K must fit in 8 bits");
947
+ static_assert(L <= 0x100, "L must fit in 8 bits");
948
+ static_assert(K + L <= 0x100, "K+L must fit in 8 bits");
949
+
950
+ uint8_t derived_seed[SIGMA_BYTES + 2];
951
+ OPENSSL_memcpy(derived_seed, sigma, SIGMA_BYTES);
952
+ derived_seed[SIGMA_BYTES] = 0;
953
+ derived_seed[SIGMA_BYTES + 1] = 0;
954
+ for (int i = 0; i < L; i++) {
955
+ scalar_uniform_eta_4(&s1->v[i], derived_seed);
956
+ ++derived_seed[SIGMA_BYTES];
957
+ }
958
+ for (int i = 0; i < K; i++) {
959
+ scalar_uniform_eta_4(&s2->v[i], derived_seed);
960
+ ++derived_seed[SIGMA_BYTES];
961
+ }
962
+ }
963
+
964
+ // FIPS 204, Algorithm 34 (`ExpandMask`).
965
+ static void vectorl_expand_mask(vectorl *out,
966
+ const uint8_t seed[RHO_PRIME_BYTES],
967
+ size_t kappa) {
968
+ assert(kappa + L <= 0x10000);
969
+
970
+ uint8_t derived_seed[RHO_PRIME_BYTES + 2];
971
+ OPENSSL_memcpy(derived_seed, seed, RHO_PRIME_BYTES);
972
+ for (int i = 0; i < L; i++) {
973
+ size_t index = kappa + i;
974
+ derived_seed[RHO_PRIME_BYTES] = index & 0xFF;
975
+ derived_seed[RHO_PRIME_BYTES + 1] = (index >> 8) & 0xFF;
976
+ scalar_sample_mask(&out->v[i], derived_seed);
977
+ }
978
+ }
979
+
980
+ /* Encoding */
981
+
982
+ // FIPS 204, Algorithm 16 (`SimpleBitPack`).
983
+ //
984
+ // Encodes an entire vector into 32*K*|bits| bytes. Note that since 256 (DEGREE)
985
+ // is divisible by 8, the individual vector entries will always fill a whole
986
+ // number of bytes, so we do not need to worry about bit packing here.
987
+ static void vectork_encode(uint8_t *out, const vectork *a, int bits) {
988
+ if (bits == 4) {
989
+ for (int i = 0; i < K; i++) {
990
+ scalar_encode_4(out + i * bits * DEGREE / 8, &a->v[i]);
991
+ }
992
+ } else {
993
+ assert(bits == 10);
994
+ for (int i = 0; i < K; i++) {
995
+ scalar_encode_10(out + i * bits * DEGREE / 8, &a->v[i]);
996
+ }
997
+ }
998
+ }
999
+
1000
+ // FIPS 204, Algorithm 18 (`SimpleBitUnpack`).
1001
+ static void vectork_decode_10(vectork *out, const uint8_t *in) {
1002
+ for (int i = 0; i < K; i++) {
1003
+ scalar_decode_10(&out->v[i], in + i * 10 * DEGREE / 8);
1004
+ }
1005
+ }
1006
+
1007
+ static void vectork_encode_signed(uint8_t *out, const vectork *a, int bits,
1008
+ uint32_t max) {
1009
+ for (int i = 0; i < K; i++) {
1010
+ scalar_encode_signed(out + i * bits * DEGREE / 8, &a->v[i], bits, max);
1011
+ }
1012
+ }
1013
+
1014
+ static int vectork_decode_signed(vectork *out, const uint8_t *in, int bits,
1015
+ uint32_t max) {
1016
+ for (int i = 0; i < K; i++) {
1017
+ if (!scalar_decode_signed(&out->v[i], in + i * bits * DEGREE / 8, bits,
1018
+ max)) {
1019
+ return 0;
1020
+ }
1021
+ }
1022
+ return 1;
1023
+ }
1024
+
1025
+ // FIPS 204, Algorithm 17 (`BitPack`).
1026
+ //
1027
+ // Encodes an entire vector into 32*L*|bits| bytes. Note that since 256 (DEGREE)
1028
+ // is divisible by 8, the individual vector entries will always fill a whole
1029
+ // number of bytes, so we do not need to worry about bit packing here.
1030
+ static void vectorl_encode_signed(uint8_t *out, const vectorl *a, int bits,
1031
+ uint32_t max) {
1032
+ for (int i = 0; i < L; i++) {
1033
+ scalar_encode_signed(out + i * bits * DEGREE / 8, &a->v[i], bits, max);
1034
+ }
1035
+ }
1036
+
1037
+ static int vectorl_decode_signed(vectorl *out, const uint8_t *in, int bits,
1038
+ uint32_t max) {
1039
+ for (int i = 0; i < L; i++) {
1040
+ if (!scalar_decode_signed(&out->v[i], in + i * bits * DEGREE / 8, bits,
1041
+ max)) {
1042
+ return 0;
1043
+ }
1044
+ }
1045
+ return 1;
1046
+ }
1047
+
1048
+ // FIPS 204, Algorithm 28 (`w1Encode`).
1049
+ static void w1_encode(uint8_t out[128 * K], const vectork *w1) {
1050
+ vectork_encode(out, w1, 4);
1051
+ }
1052
+
1053
+ // FIPS 204, Algorithm 20 (`HintBitPack`).
1054
+ static void hint_bit_pack(uint8_t out[OMEGA + K], const vectork *h) {
1055
+ OPENSSL_memset(out, 0, OMEGA + K);
1056
+ int index = 0;
1057
+ for (int i = 0; i < K; i++) {
1058
+ for (int j = 0; j < DEGREE; j++) {
1059
+ if (h->v[i].c[j]) {
1060
+ // h must have at most OMEGA non-zero coefficients.
1061
+ BSSL_CHECK(index < OMEGA);
1062
+ out[index++] = j;
1063
+ }
1064
+ }
1065
+ out[OMEGA + i] = index;
1066
+ }
1067
+ }
1068
+
1069
+ // FIPS 204, Algorithm 21 (`HintBitUnpack`).
1070
+ static int hint_bit_unpack(vectork *h, const uint8_t in[OMEGA + K]) {
1071
+ vectork_zero(h);
1072
+ int index = 0;
1073
+ for (int i = 0; i < K; i++) {
1074
+ const int limit = in[OMEGA + i];
1075
+ if (limit < index || limit > OMEGA) {
1076
+ return 0;
1077
+ }
1078
+
1079
+ int last = -1;
1080
+ while (index < limit) {
1081
+ int byte = in[index++];
1082
+ if (last >= 0 && byte <= last) {
1083
+ return 0;
1084
+ }
1085
+ last = byte;
1086
+ static_assert(DEGREE == 256,
1087
+ "DEGREE must be 256 for this write to be in bounds");
1088
+ h->v[i].c[byte] = 1;
1089
+ }
1090
+ }
1091
+ for (; index < OMEGA; index++) {
1092
+ if (in[index] != 0) {
1093
+ return 0;
1094
+ }
1095
+ }
1096
+ return 1;
1097
+ }
1098
+
1099
+ struct public_key {
1100
+ uint8_t rho[RHO_BYTES];
1101
+ vectork t1;
1102
+ // Pre-cached value(s).
1103
+ uint8_t public_key_hash[TR_BYTES];
1104
+ };
1105
+
1106
+ struct private_key {
1107
+ uint8_t rho[RHO_BYTES];
1108
+ uint8_t k[K_BYTES];
1109
+ uint8_t public_key_hash[TR_BYTES];
1110
+ vectorl s1;
1111
+ vectork s2;
1112
+ vectork t0;
1113
+ };
1114
+
1115
+ struct signature {
1116
+ uint8_t c_tilde[2 * LAMBDA_BYTES];
1117
+ vectorl z;
1118
+ vectork h;
1119
+ };
1120
+
1121
+ // FIPS 204, Algorithm 22 (`pkEncode`).
1122
+ static int mldsa_marshal_public_key(CBB *out, const struct public_key *pub) {
1123
+ if (!CBB_add_bytes(out, pub->rho, sizeof(pub->rho))) {
1124
+ return 0;
1125
+ }
1126
+
1127
+ uint8_t *vectork_output;
1128
+ if (!CBB_add_space(out, &vectork_output, 320 * K)) {
1129
+ return 0;
1130
+ }
1131
+ vectork_encode(vectork_output, &pub->t1, 10);
1132
+
1133
+ return 1;
1134
+ }
1135
+
1136
+ // FIPS 204, Algorithm 23 (`pkDecode`).
1137
+ static int mldsa_parse_public_key(struct public_key *pub, CBS *in) {
1138
+ if (!CBS_copy_bytes(in, pub->rho, sizeof(pub->rho))) {
1139
+ return 0;
1140
+ }
1141
+
1142
+ CBS t1_bytes;
1143
+ if (!CBS_get_bytes(in, &t1_bytes, 320 * K)) {
1144
+ return 0;
1145
+ }
1146
+ vectork_decode_10(&pub->t1, CBS_data(&t1_bytes));
1147
+
1148
+ return 1;
1149
+ }
1150
+
1151
+ // FIPS 204, Algorithm 24 (`skEncode`).
1152
+ static int mldsa_marshal_private_key(CBB *out, const struct private_key *priv) {
1153
+ if (!CBB_add_bytes(out, priv->rho, sizeof(priv->rho)) ||
1154
+ !CBB_add_bytes(out, priv->k, sizeof(priv->k)) ||
1155
+ !CBB_add_bytes(out, priv->public_key_hash,
1156
+ sizeof(priv->public_key_hash))) {
1157
+ return 0;
1158
+ }
1159
+
1160
+ uint8_t *vectorl_output;
1161
+ if (!CBB_add_space(out, &vectorl_output, 128 * L)) {
1162
+ return 0;
1163
+ }
1164
+ vectorl_encode_signed(vectorl_output, &priv->s1, 4, ETA);
1165
+
1166
+ uint8_t *vectork_output;
1167
+ if (!CBB_add_space(out, &vectork_output, 128 * K)) {
1168
+ return 0;
1169
+ }
1170
+ vectork_encode_signed(vectork_output, &priv->s2, 4, ETA);
1171
+
1172
+ if (!CBB_add_space(out, &vectork_output, 416 * K)) {
1173
+ return 0;
1174
+ }
1175
+ vectork_encode_signed(vectork_output, &priv->t0, 13, 1 << 12);
1176
+
1177
+ return 1;
1178
+ }
1179
+
1180
+ // FIPS 204, Algorithm 25 (`skDecode`).
1181
+ static int mldsa_parse_private_key(struct private_key *priv, CBS *in) {
1182
+ CBS s1_bytes;
1183
+ CBS s2_bytes;
1184
+ CBS t0_bytes;
1185
+ if (!CBS_copy_bytes(in, priv->rho, sizeof(priv->rho)) ||
1186
+ !CBS_copy_bytes(in, priv->k, sizeof(priv->k)) ||
1187
+ !CBS_copy_bytes(in, priv->public_key_hash,
1188
+ sizeof(priv->public_key_hash)) ||
1189
+ !CBS_get_bytes(in, &s1_bytes, 128 * L) ||
1190
+ !vectorl_decode_signed(&priv->s1, CBS_data(&s1_bytes), 4, ETA) ||
1191
+ !CBS_get_bytes(in, &s2_bytes, 128 * K) ||
1192
+ !vectork_decode_signed(&priv->s2, CBS_data(&s2_bytes), 4, ETA) ||
1193
+ !CBS_get_bytes(in, &t0_bytes, 416 * K) ||
1194
+ // Note: Decoding 13 bits into (-2^12, 2^12] cannot fail.
1195
+ !vectork_decode_signed(&priv->t0, CBS_data(&t0_bytes), 13, 1 << 12)) {
1196
+ return 0;
1197
+ }
1198
+
1199
+ return 1;
1200
+ }
1201
+
1202
+ // FIPS 204, Algorithm 26 (`sigEncode`).
1203
+ static int mldsa_marshal_signature(CBB *out, const struct signature *sign) {
1204
+ if (!CBB_add_bytes(out, sign->c_tilde, sizeof(sign->c_tilde))) {
1205
+ return 0;
1206
+ }
1207
+
1208
+ uint8_t *vectorl_output;
1209
+ if (!CBB_add_space(out, &vectorl_output, 640 * L)) {
1210
+ return 0;
1211
+ }
1212
+ vectorl_encode_signed(vectorl_output, &sign->z, 20, 1 << 19);
1213
+
1214
+ uint8_t *hint_output;
1215
+ if (!CBB_add_space(out, &hint_output, OMEGA + K)) {
1216
+ return 0;
1217
+ }
1218
+ hint_bit_pack(hint_output, &sign->h);
1219
+
1220
+ return 1;
1221
+ }
1222
+
1223
+ // FIPS 204, Algorithm 27 (`sigDecode`).
1224
+ static int mldsa_parse_signature(struct signature *sign, CBS *in) {
1225
+ CBS z_bytes;
1226
+ CBS hint_bytes;
1227
+ if (!CBS_copy_bytes(in, sign->c_tilde, sizeof(sign->c_tilde)) ||
1228
+ !CBS_get_bytes(in, &z_bytes, 640 * L) ||
1229
+ // Note: Decoding 20 bits into (-2^19, 2^19] cannot fail.
1230
+ !vectorl_decode_signed(&sign->z, CBS_data(&z_bytes), 20, 1 << 19) ||
1231
+ !CBS_get_bytes(in, &hint_bytes, OMEGA + K) ||
1232
+ !hint_bit_unpack(&sign->h, CBS_data(&hint_bytes))) {
1233
+ return 0;
1234
+ };
1235
+
1236
+ return 1;
1237
+ }
1238
+
1239
+ static struct private_key *private_key_from_external(
1240
+ const struct MLDSA65_private_key *external) {
1241
+ static_assert(
1242
+ sizeof(struct MLDSA65_private_key) == sizeof(struct private_key),
1243
+ "Kyber private key size incorrect");
1244
+ static_assert(
1245
+ alignof(struct MLDSA65_private_key) == alignof(struct private_key),
1246
+ "Kyber private key align incorrect");
1247
+ return (struct private_key *)external;
1248
+ }
1249
+
1250
+ static struct public_key *public_key_from_external(
1251
+ const struct MLDSA65_public_key *external) {
1252
+ static_assert(sizeof(struct MLDSA65_public_key) == sizeof(struct public_key),
1253
+ "mldsa public key size incorrect");
1254
+ static_assert(
1255
+ alignof(struct MLDSA65_public_key) == alignof(struct public_key),
1256
+ "mldsa public key align incorrect");
1257
+ return (struct public_key *)external;
1258
+ }
1259
+
1260
+ /* API */
1261
+
1262
+ // Calls |MLDSA_generate_key_external_entropy| with random bytes from
1263
+ // |RAND_bytes|. Returns 1 on success and 0 on failure.
1264
+ int MLDSA65_generate_key(
1265
+ uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES],
1266
+ uint8_t out_seed[MLDSA_SEED_BYTES],
1267
+ struct MLDSA65_private_key *out_private_key) {
1268
+ RAND_bytes(out_seed, MLDSA_SEED_BYTES);
1269
+ return MLDSA65_generate_key_external_entropy(out_encoded_public_key,
1270
+ out_private_key, out_seed);
1271
+ }
1272
+
1273
+ int MLDSA65_private_key_from_seed(struct MLDSA65_private_key *out_private_key,
1274
+ const uint8_t *seed, size_t seed_len) {
1275
+ if (seed_len != MLDSA_SEED_BYTES) {
1276
+ return 0;
1277
+ }
1278
+ uint8_t public_key[MLDSA65_PUBLIC_KEY_BYTES];
1279
+ return MLDSA65_generate_key_external_entropy(public_key, out_private_key,
1280
+ seed);
1281
+ }
1282
+
1283
+ // FIPS 204, Algorithm 6 (`ML-DSA.KeyGen_internal`). Returns 1 on success and 0
1284
+ // on failure.
1285
+ int MLDSA65_generate_key_external_entropy(
1286
+ uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES],
1287
+ struct MLDSA65_private_key *out_private_key,
1288
+ const uint8_t entropy[MLDSA_SEED_BYTES]) {
1289
+ int ret = 0;
1290
+
1291
+ // Intermediate values, allocated on the heap to allow use when there is a
1292
+ // limited amount of stack.
1293
+ struct values_st {
1294
+ struct public_key pub;
1295
+ matrix a_ntt;
1296
+ vectorl s1_ntt;
1297
+ vectork t;
1298
+ };
1299
+ struct values_st *values = OPENSSL_malloc(sizeof(*values));
1300
+ if (values == NULL) {
1301
+ goto err;
1302
+ }
1303
+
1304
+ struct private_key *priv = private_key_from_external(out_private_key);
1305
+
1306
+ uint8_t augmented_entropy[MLDSA_SEED_BYTES + 2];
1307
+ OPENSSL_memcpy(augmented_entropy, entropy, MLDSA_SEED_BYTES);
1308
+ // The k and l parameters are appended to the seed.
1309
+ augmented_entropy[MLDSA_SEED_BYTES] = K;
1310
+ augmented_entropy[MLDSA_SEED_BYTES + 1] = L;
1311
+ uint8_t expanded_seed[RHO_BYTES + SIGMA_BYTES + K_BYTES];
1312
+ BORINGSSL_keccak(expanded_seed, sizeof(expanded_seed), augmented_entropy,
1313
+ sizeof(augmented_entropy), boringssl_shake256);
1314
+ const uint8_t *const rho = expanded_seed;
1315
+ const uint8_t *const sigma = expanded_seed + RHO_BYTES;
1316
+ const uint8_t *const k = expanded_seed + RHO_BYTES + SIGMA_BYTES;
1317
+ // rho is public.
1318
+ CONSTTIME_DECLASSIFY(rho, RHO_BYTES);
1319
+ OPENSSL_memcpy(values->pub.rho, rho, sizeof(values->pub.rho));
1320
+ OPENSSL_memcpy(priv->rho, rho, sizeof(priv->rho));
1321
+ OPENSSL_memcpy(priv->k, k, sizeof(priv->k));
1322
+
1323
+ matrix_expand(&values->a_ntt, rho);
1324
+ vector_expand_short(&priv->s1, &priv->s2, sigma);
1325
+
1326
+ OPENSSL_memcpy(&values->s1_ntt, &priv->s1, sizeof(values->s1_ntt));
1327
+ vectorl_ntt(&values->s1_ntt);
1328
+
1329
+ matrix_mult(&values->t, &values->a_ntt, &values->s1_ntt);
1330
+ vectork_inverse_ntt(&values->t);
1331
+ vectork_add(&values->t, &values->t, &priv->s2);
1332
+
1333
+ vectork_power2_round(&values->pub.t1, &priv->t0, &values->t);
1334
+ // t1 is public.
1335
+ CONSTTIME_DECLASSIFY(&values->pub.t1, sizeof(values->pub.t1));
1336
+
1337
+ CBB cbb;
1338
+ CBB_init_fixed(&cbb, out_encoded_public_key, MLDSA65_PUBLIC_KEY_BYTES);
1339
+ if (!mldsa_marshal_public_key(&cbb, &values->pub)) {
1340
+ goto err;
1341
+ }
1342
+ assert(CBB_len(&cbb) == MLDSA65_PUBLIC_KEY_BYTES);
1343
+
1344
+ BORINGSSL_keccak(priv->public_key_hash, sizeof(priv->public_key_hash),
1345
+ out_encoded_public_key, MLDSA65_PUBLIC_KEY_BYTES,
1346
+ boringssl_shake256);
1347
+
1348
+ ret = 1;
1349
+ err:
1350
+ OPENSSL_free(values);
1351
+ return ret;
1352
+ }
1353
+
1354
+ int MLDSA65_public_from_private(struct MLDSA65_public_key *out_public_key,
1355
+ const struct MLDSA65_private_key *private_key) {
1356
+ int ret = 0;
1357
+
1358
+ // Intermediate values, allocated on the heap to allow use when there is a
1359
+ // limited amount of stack.
1360
+ struct values_st {
1361
+ matrix a_ntt;
1362
+ vectorl s1_ntt;
1363
+ vectork t;
1364
+ vectork t0;
1365
+ };
1366
+ struct values_st *values = OPENSSL_malloc(sizeof(*values));
1367
+ if (values == NULL) {
1368
+ goto err;
1369
+ }
1370
+
1371
+ const struct private_key *priv = private_key_from_external(private_key);
1372
+ struct public_key *pub = public_key_from_external(out_public_key);
1373
+
1374
+ OPENSSL_memcpy(pub->rho, priv->rho, sizeof(pub->rho));
1375
+ OPENSSL_memcpy(pub->public_key_hash, priv->public_key_hash,
1376
+ sizeof(pub->public_key_hash));
1377
+
1378
+ matrix_expand(&values->a_ntt, priv->rho);
1379
+
1380
+ OPENSSL_memcpy(&values->s1_ntt, &priv->s1, sizeof(values->s1_ntt));
1381
+ vectorl_ntt(&values->s1_ntt);
1382
+
1383
+ matrix_mult(&values->t, &values->a_ntt, &values->s1_ntt);
1384
+ vectork_inverse_ntt(&values->t);
1385
+ vectork_add(&values->t, &values->t, &priv->s2);
1386
+
1387
+ vectork_power2_round(&pub->t1, &values->t0, &values->t);
1388
+
1389
+ ret = 1;
1390
+ err:
1391
+ OPENSSL_free(values);
1392
+ return ret;
1393
+ }
1394
+
1395
+ // FIPS 204, Algorithm 7 (`ML-DSA.Sign_internal`). Returns 1 on success and 0 on
1396
+ // failure.
1397
+ int MLDSA65_sign_internal(
1398
+ uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES],
1399
+ const struct MLDSA65_private_key *private_key, const uint8_t *msg,
1400
+ size_t msg_len, const uint8_t *context_prefix, size_t context_prefix_len,
1401
+ const uint8_t *context, size_t context_len,
1402
+ const uint8_t randomizer[MLDSA_SIGNATURE_RANDOMIZER_BYTES]) {
1403
+ int ret = 0;
1404
+ const struct private_key *priv = private_key_from_external(private_key);
1405
+
1406
+ uint8_t mu[MU_BYTES];
1407
+ struct BORINGSSL_keccak_st keccak_ctx;
1408
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
1409
+ BORINGSSL_keccak_absorb(&keccak_ctx, priv->public_key_hash,
1410
+ sizeof(priv->public_key_hash));
1411
+ BORINGSSL_keccak_absorb(&keccak_ctx, context_prefix, context_prefix_len);
1412
+ BORINGSSL_keccak_absorb(&keccak_ctx, context, context_len);
1413
+ BORINGSSL_keccak_absorb(&keccak_ctx, msg, msg_len);
1414
+ BORINGSSL_keccak_squeeze(&keccak_ctx, mu, MU_BYTES);
1415
+
1416
+ uint8_t rho_prime[RHO_PRIME_BYTES];
1417
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
1418
+ BORINGSSL_keccak_absorb(&keccak_ctx, priv->k, sizeof(priv->k));
1419
+ BORINGSSL_keccak_absorb(&keccak_ctx, randomizer,
1420
+ MLDSA_SIGNATURE_RANDOMIZER_BYTES);
1421
+ BORINGSSL_keccak_absorb(&keccak_ctx, mu, MU_BYTES);
1422
+ BORINGSSL_keccak_squeeze(&keccak_ctx, rho_prime, RHO_PRIME_BYTES);
1423
+
1424
+ // Intermediate values, allocated on the heap to allow use when there is a
1425
+ // limited amount of stack.
1426
+ struct values_st {
1427
+ struct signature sign;
1428
+ vectorl s1_ntt;
1429
+ vectork s2_ntt;
1430
+ vectork t0_ntt;
1431
+ matrix a_ntt;
1432
+ vectorl y;
1433
+ vectork w;
1434
+ vectork w1;
1435
+ vectorl cs1;
1436
+ vectork cs2;
1437
+ };
1438
+ struct values_st *values = OPENSSL_malloc(sizeof(*values));
1439
+ if (values == NULL) {
1440
+ goto err;
1441
+ }
1442
+ OPENSSL_memcpy(&values->s1_ntt, &priv->s1, sizeof(values->s1_ntt));
1443
+ vectorl_ntt(&values->s1_ntt);
1444
+
1445
+ OPENSSL_memcpy(&values->s2_ntt, &priv->s2, sizeof(values->s2_ntt));
1446
+ vectork_ntt(&values->s2_ntt);
1447
+
1448
+ OPENSSL_memcpy(&values->t0_ntt, &priv->t0, sizeof(values->t0_ntt));
1449
+ vectork_ntt(&values->t0_ntt);
1450
+
1451
+ matrix_expand(&values->a_ntt, priv->rho);
1452
+
1453
+ // kappa must not exceed 2**16/L = 13107. But the probability of it exceeding
1454
+ // even 1000 iterations is vanishingly small.
1455
+ for (size_t kappa = 0;; kappa += L) {
1456
+ vectorl_expand_mask(&values->y, rho_prime, kappa);
1457
+
1458
+ vectorl *y_ntt = &values->cs1;
1459
+ OPENSSL_memcpy(y_ntt, &values->y, sizeof(*y_ntt));
1460
+ vectorl_ntt(y_ntt);
1461
+
1462
+ matrix_mult(&values->w, &values->a_ntt, y_ntt);
1463
+ vectork_inverse_ntt(&values->w);
1464
+
1465
+ vectork_high_bits(&values->w1, &values->w);
1466
+ uint8_t w1_encoded[128 * K];
1467
+ w1_encode(w1_encoded, &values->w1);
1468
+
1469
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
1470
+ BORINGSSL_keccak_absorb(&keccak_ctx, mu, MU_BYTES);
1471
+ BORINGSSL_keccak_absorb(&keccak_ctx, w1_encoded, 128 * K);
1472
+ BORINGSSL_keccak_squeeze(&keccak_ctx, values->sign.c_tilde,
1473
+ 2 * LAMBDA_BYTES);
1474
+
1475
+ scalar c_ntt;
1476
+ scalar_sample_in_ball_vartime(&c_ntt, values->sign.c_tilde,
1477
+ sizeof(values->sign.c_tilde));
1478
+ scalar_ntt(&c_ntt);
1479
+
1480
+ vectorl_mult_scalar(&values->cs1, &values->s1_ntt, &c_ntt);
1481
+ vectorl_inverse_ntt(&values->cs1);
1482
+ vectork_mult_scalar(&values->cs2, &values->s2_ntt, &c_ntt);
1483
+ vectork_inverse_ntt(&values->cs2);
1484
+
1485
+ vectorl_add(&values->sign.z, &values->y, &values->cs1);
1486
+
1487
+ vectork *r0 = &values->w1;
1488
+ vectork_sub(r0, &values->w, &values->cs2);
1489
+ vectork_low_bits(r0, r0);
1490
+
1491
+ // Leaking the fact that a signature was rejected is fine as the next
1492
+ // attempt at a signature will be (indistinguishable from) independent of
1493
+ // this one. Note, however, that we additionally leak which of the two
1494
+ // branches rejected the signature. Section 5.5 of
1495
+ // https://pq-crystals.org/dilithium/data/dilithium-specification-round3.pdf
1496
+ // describes this leak as OK. Note we leak less than what is described by
1497
+ // the paper; we do not reveal which coefficient violated the bound, and we
1498
+ // hide which of the |z_max| or |r0_max| bound failed. See also
1499
+ // https://boringssl-review.googlesource.com/c/boringssl/+/67747/comment/2bbab0fa_d241d35a/
1500
+ uint32_t z_max = vectorl_max(&values->sign.z);
1501
+ uint32_t r0_max = vectork_max_signed(r0);
1502
+ if (constant_time_declassify_w(
1503
+ constant_time_ge_w(z_max, kGamma1 - BETA) |
1504
+ constant_time_ge_w(r0_max, kGamma2 - BETA))) {
1505
+ continue;
1506
+ }
1507
+
1508
+ vectork *ct0 = &values->w1;
1509
+ vectork_mult_scalar(ct0, &values->t0_ntt, &c_ntt);
1510
+ vectork_inverse_ntt(ct0);
1511
+ vectork_make_hint(&values->sign.h, ct0, &values->cs2, &values->w);
1512
+
1513
+ // See above.
1514
+ uint32_t ct0_max = vectork_max(ct0);
1515
+ size_t h_ones = vectork_count_ones(&values->sign.h);
1516
+ if (constant_time_declassify_w(constant_time_ge_w(ct0_max, kGamma2) |
1517
+ constant_time_lt_w(OMEGA, h_ones))) {
1518
+ continue;
1519
+ }
1520
+
1521
+ // Although computed with the private key, the signature is public.
1522
+ CONSTTIME_DECLASSIFY(values->sign.c_tilde, sizeof(values->sign.c_tilde));
1523
+ CONSTTIME_DECLASSIFY(&values->sign.z, sizeof(values->sign.z));
1524
+ CONSTTIME_DECLASSIFY(&values->sign.h, sizeof(values->sign.h));
1525
+
1526
+ CBB cbb;
1527
+ CBB_init_fixed(&cbb, out_encoded_signature, MLDSA65_SIGNATURE_BYTES);
1528
+ if (!mldsa_marshal_signature(&cbb, &values->sign)) {
1529
+ goto err;
1530
+ }
1531
+
1532
+ BSSL_CHECK(CBB_len(&cbb) == MLDSA65_SIGNATURE_BYTES);
1533
+ ret = 1;
1534
+ break;
1535
+ }
1536
+
1537
+ err:
1538
+ OPENSSL_free(values);
1539
+ return ret;
1540
+ }
1541
+
1542
+ // mldsa signature in randomized mode, filling the random bytes with
1543
+ // |RAND_bytes|. Returns 1 on success and 0 on failure.
1544
+ int MLDSA65_sign(uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES],
1545
+ const struct MLDSA65_private_key *private_key,
1546
+ const uint8_t *msg, size_t msg_len, const uint8_t *context,
1547
+ size_t context_len) {
1548
+ if (context_len > 255) {
1549
+ return 0;
1550
+ }
1551
+
1552
+ uint8_t randomizer[MLDSA_SIGNATURE_RANDOMIZER_BYTES];
1553
+ RAND_bytes(randomizer, sizeof(randomizer));
1554
+
1555
+ const uint8_t context_prefix[2] = {0, context_len};
1556
+ return MLDSA65_sign_internal(out_encoded_signature, private_key, msg, msg_len,
1557
+ context_prefix, sizeof(context_prefix), context,
1558
+ context_len, randomizer);
1559
+ }
1560
+
1561
+ // FIPS 204, Algorithm 3 (`ML-DSA.Verify`).
1562
+ int MLDSA65_verify(const struct MLDSA65_public_key *public_key,
1563
+ const uint8_t *signature, size_t signature_len,
1564
+ const uint8_t *msg, size_t msg_len, const uint8_t *context,
1565
+ size_t context_len) {
1566
+ if (context_len > 255 || signature_len != MLDSA65_SIGNATURE_BYTES) {
1567
+ return 0;
1568
+ }
1569
+
1570
+ const uint8_t context_prefix[2] = {0, context_len};
1571
+ return MLDSA65_verify_internal(public_key, signature, msg, msg_len,
1572
+ context_prefix, sizeof(context_prefix),
1573
+ context, context_len);
1574
+ }
1575
+
1576
+ // FIPS 204, Algorithm 8 (`ML-DSA.Verify_internal`).
1577
+ int MLDSA65_verify_internal(
1578
+ const struct MLDSA65_public_key *public_key,
1579
+ const uint8_t encoded_signature[MLDSA65_SIGNATURE_BYTES],
1580
+ const uint8_t *msg, size_t msg_len, const uint8_t *context_prefix,
1581
+ size_t context_prefix_len, const uint8_t *context, size_t context_len) {
1582
+ int ret = 0;
1583
+
1584
+ // Intermediate values, allocated on the heap to allow use when there is a
1585
+ // limited amount of stack.
1586
+ struct values_st {
1587
+ struct signature sign;
1588
+ matrix a_ntt;
1589
+ vectorl z_ntt;
1590
+ vectork az_ntt;
1591
+ vectork ct1_ntt;
1592
+ };
1593
+ struct values_st *values = OPENSSL_malloc(sizeof(*values));
1594
+ if (values == NULL) {
1595
+ goto err;
1596
+ }
1597
+
1598
+ const struct public_key *pub = public_key_from_external(public_key);
1599
+
1600
+ CBS cbs;
1601
+ CBS_init(&cbs, encoded_signature, MLDSA65_SIGNATURE_BYTES);
1602
+ if (!mldsa_parse_signature(&values->sign, &cbs)) {
1603
+ goto err;
1604
+ }
1605
+
1606
+ matrix_expand(&values->a_ntt, pub->rho);
1607
+
1608
+ uint8_t mu[MU_BYTES];
1609
+ struct BORINGSSL_keccak_st keccak_ctx;
1610
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
1611
+ BORINGSSL_keccak_absorb(&keccak_ctx, pub->public_key_hash,
1612
+ sizeof(pub->public_key_hash));
1613
+ BORINGSSL_keccak_absorb(&keccak_ctx, context_prefix, context_prefix_len);
1614
+ BORINGSSL_keccak_absorb(&keccak_ctx, context, context_len);
1615
+ BORINGSSL_keccak_absorb(&keccak_ctx, msg, msg_len);
1616
+ BORINGSSL_keccak_squeeze(&keccak_ctx, mu, MU_BYTES);
1617
+
1618
+ scalar c_ntt;
1619
+ scalar_sample_in_ball_vartime(&c_ntt, values->sign.c_tilde,
1620
+ sizeof(values->sign.c_tilde));
1621
+ scalar_ntt(&c_ntt);
1622
+
1623
+ OPENSSL_memcpy(&values->z_ntt, &values->sign.z, sizeof(values->z_ntt));
1624
+ vectorl_ntt(&values->z_ntt);
1625
+
1626
+ matrix_mult(&values->az_ntt, &values->a_ntt, &values->z_ntt);
1627
+
1628
+ vectork_scale_power2_round(&values->ct1_ntt, &pub->t1);
1629
+ vectork_ntt(&values->ct1_ntt);
1630
+
1631
+ vectork_mult_scalar(&values->ct1_ntt, &values->ct1_ntt, &c_ntt);
1632
+
1633
+ vectork *const w1 = &values->az_ntt;
1634
+ vectork_sub(w1, &values->az_ntt, &values->ct1_ntt);
1635
+ vectork_inverse_ntt(w1);
1636
+
1637
+ vectork_use_hint_vartime(w1, &values->sign.h, w1);
1638
+ uint8_t w1_encoded[128 * K];
1639
+ w1_encode(w1_encoded, w1);
1640
+
1641
+ uint8_t c_tilde[2 * LAMBDA_BYTES];
1642
+ BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
1643
+ BORINGSSL_keccak_absorb(&keccak_ctx, mu, MU_BYTES);
1644
+ BORINGSSL_keccak_absorb(&keccak_ctx, w1_encoded, 128 * K);
1645
+ BORINGSSL_keccak_squeeze(&keccak_ctx, c_tilde, 2 * LAMBDA_BYTES);
1646
+
1647
+ uint32_t z_max = vectorl_max(&values->sign.z);
1648
+ if (z_max < kGamma1 - BETA &&
1649
+ OPENSSL_memcmp(c_tilde, values->sign.c_tilde, 2 * LAMBDA_BYTES) == 0) {
1650
+ ret = 1;
1651
+ }
1652
+
1653
+ err:
1654
+ OPENSSL_free(values);
1655
+ return ret;
1656
+ }
1657
+
1658
+ /* Serialization of keys. */
1659
+
1660
+ int MLDSA65_marshal_public_key(CBB *out,
1661
+ const struct MLDSA65_public_key *public_key) {
1662
+ return mldsa_marshal_public_key(out, public_key_from_external(public_key));
1663
+ }
1664
+
1665
+ int MLDSA65_parse_public_key(struct MLDSA65_public_key *public_key, CBS *in) {
1666
+ struct public_key *pub = public_key_from_external(public_key);
1667
+ CBS orig_in = *in;
1668
+ if (!mldsa_parse_public_key(pub, in) || CBS_len(in) != 0) {
1669
+ return 0;
1670
+ }
1671
+
1672
+ // Compute pre-cached values.
1673
+ BORINGSSL_keccak(pub->public_key_hash, sizeof(pub->public_key_hash),
1674
+ CBS_data(&orig_in), CBS_len(&orig_in), boringssl_shake256);
1675
+ return 1;
1676
+ }
1677
+
1678
+ int MLDSA65_marshal_private_key(CBB *out,
1679
+ const struct MLDSA65_private_key *private_key) {
1680
+ return mldsa_marshal_private_key(out, private_key_from_external(private_key));
1681
+ }
1682
+
1683
+ int MLDSA65_parse_private_key(struct MLDSA65_private_key *private_key,
1684
+ CBS *in) {
1685
+ struct private_key *priv = private_key_from_external(private_key);
1686
+ return mldsa_parse_private_key(priv, in) && CBS_len(in) == 0;
1687
+ }