grpc 1.24.0 → 1.25.0.pre1

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

Potentially problematic release.


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

Files changed (504) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +306 -243
  3. data/etc/roots.pem +0 -100
  4. data/include/grpc/grpc_security.h +44 -18
  5. data/include/grpc/impl/codegen/grpc_types.h +15 -0
  6. data/include/grpc/impl/codegen/port_platform.h +27 -11
  7. data/include/grpc/impl/codegen/sync_generic.h +1 -1
  8. data/src/boringssl/err_data.c +695 -650
  9. data/src/core/ext/filters/client_channel/client_channel.cc +257 -179
  10. data/src/core/ext/filters/client_channel/client_channel.h +24 -0
  11. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +2 -3
  12. data/src/core/ext/filters/client_channel/client_channel_factory.h +1 -5
  13. data/src/core/ext/filters/client_channel/health/health_check_client.cc +18 -45
  14. data/src/core/ext/filters/client_channel/health/health_check_client.h +5 -13
  15. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +1 -1
  16. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -3
  17. data/src/core/ext/filters/client_channel/lb_policy.h +65 -55
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +14 -14
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +113 -36
  20. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +14 -19
  21. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +36 -13
  22. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +3 -10
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +814 -1589
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.h +2 -5
  25. data/src/core/ext/filters/client_channel/lb_policy_factory.h +3 -6
  26. data/src/core/ext/filters/client_channel/resolver.cc +1 -2
  27. data/src/core/ext/filters/client_channel/resolver.h +8 -16
  28. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +25 -8
  29. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +46 -12
  30. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +10 -17
  31. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +7 -8
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +4 -4
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +111 -44
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +22 -14
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +1 -1
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +2 -2
  37. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +29 -10
  38. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +27 -36
  39. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +7 -10
  40. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +60 -16
  41. data/src/core/ext/filters/client_channel/resolver_factory.h +4 -8
  42. data/src/core/ext/filters/client_channel/resolver_registry.cc +1 -1
  43. data/src/core/ext/filters/client_channel/resolver_registry.h +1 -1
  44. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +7 -10
  45. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +7 -8
  46. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +1 -1
  47. data/src/core/ext/filters/client_channel/retry_throttle.cc +5 -5
  48. data/src/core/ext/filters/client_channel/retry_throttle.h +1 -4
  49. data/src/core/ext/filters/client_channel/service_config.h +8 -8
  50. data/src/core/ext/filters/client_channel/subchannel.cc +53 -86
  51. data/src/core/ext/filters/client_channel/subchannel.h +7 -9
  52. data/src/core/ext/filters/client_channel/subchannel_interface.h +9 -13
  53. data/src/core/ext/filters/client_channel/subchannel_pool_interface.h +3 -6
  54. data/src/core/ext/filters/client_channel/{lb_policy/xds/xds_load_balancer_api.cc → xds/xds_api.cc} +169 -52
  55. data/src/core/ext/filters/client_channel/xds/xds_api.h +171 -0
  56. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +450 -0
  57. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +99 -0
  58. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_channel.h +8 -6
  59. data/src/core/ext/filters/client_channel/xds/xds_channel_args.h +26 -0
  60. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_channel_secure.cc +28 -11
  61. data/src/core/ext/filters/client_channel/xds/xds_client.cc +1413 -0
  62. data/src/core/ext/filters/client_channel/xds/xds_client.h +221 -0
  63. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_client_stats.cc +1 -5
  64. data/src/core/ext/filters/client_channel/{lb_policy/xds → xds}/xds_client_stats.h +3 -4
  65. data/src/core/ext/filters/deadline/deadline_filter.cc +20 -20
  66. data/src/core/ext/filters/http/client/http_client_filter.cc +15 -15
  67. data/src/core/ext/filters/http/client_authority_filter.cc +14 -14
  68. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +12 -12
  69. data/src/core/ext/filters/max_age/max_age_filter.cc +59 -50
  70. data/src/core/ext/filters/message_size/message_size_filter.cc +18 -18
  71. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.cc +15 -14
  72. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +233 -175
  73. data/src/core/ext/transport/chttp2/transport/flow_control.h +21 -24
  74. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +253 -163
  75. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +24 -12
  76. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +2 -3
  77. data/src/core/ext/transport/chttp2/transport/internal.h +13 -15
  78. data/src/core/ext/transport/chttp2/transport/writing.cc +3 -0
  79. data/src/core/ext/transport/inproc/inproc_transport.cc +20 -13
  80. data/src/core/lib/channel/channel_args.cc +16 -0
  81. data/src/core/lib/channel/channel_args.h +22 -0
  82. data/src/core/lib/channel/channelz.cc +5 -6
  83. data/src/core/lib/channel/channelz.h +1 -1
  84. data/src/core/lib/channel/connected_channel.cc +20 -20
  85. data/src/core/lib/channel/handshaker.h +3 -4
  86. data/src/core/lib/channel/handshaker_factory.h +1 -3
  87. data/src/core/lib/debug/trace.h +3 -2
  88. data/src/core/lib/gprpp/arena.cc +3 -3
  89. data/src/core/lib/gprpp/arena.h +2 -3
  90. data/src/core/lib/gprpp/inlined_vector.h +9 -0
  91. data/src/core/lib/gprpp/map.h +3 -501
  92. data/src/core/lib/gprpp/memory.h +45 -41
  93. data/src/core/lib/gprpp/mpscq.cc +108 -0
  94. data/src/core/lib/gprpp/mpscq.h +98 -0
  95. data/src/core/lib/gprpp/orphanable.h +6 -11
  96. data/src/core/lib/gprpp/ref_counted.h +25 -19
  97. data/src/core/lib/gprpp/set.h +33 -0
  98. data/src/core/lib/gprpp/thd.h +2 -4
  99. data/src/core/lib/http/httpcli.cc +1 -1
  100. data/src/core/lib/http/httpcli_security_connector.cc +15 -11
  101. data/src/core/lib/http/parser.cc +1 -1
  102. data/src/core/lib/iomgr/buffer_list.cc +4 -5
  103. data/src/core/lib/iomgr/buffer_list.h +5 -6
  104. data/src/core/lib/iomgr/call_combiner.cc +4 -5
  105. data/src/core/lib/iomgr/call_combiner.h +2 -2
  106. data/src/core/lib/iomgr/cfstream_handle.h +3 -5
  107. data/src/core/lib/iomgr/closure.h +8 -3
  108. data/src/core/lib/iomgr/combiner.cc +45 -82
  109. data/src/core/lib/iomgr/combiner.h +32 -8
  110. data/src/core/lib/iomgr/endpoint_cfstream.cc +5 -3
  111. data/src/core/lib/iomgr/ev_epoll1_linux.cc +19 -15
  112. data/src/core/lib/iomgr/exec_ctx.h +4 -3
  113. data/src/core/lib/iomgr/executor.cc +4 -2
  114. data/src/core/lib/iomgr/executor.h +3 -0
  115. data/src/core/lib/iomgr/executor/mpmcqueue.h +3 -6
  116. data/src/core/lib/iomgr/executor/threadpool.cc +1 -2
  117. data/src/core/lib/iomgr/executor/threadpool.h +7 -11
  118. data/src/core/lib/iomgr/resource_quota.cc +55 -51
  119. data/src/core/lib/iomgr/resource_quota.h +13 -9
  120. data/src/core/lib/iomgr/socket_utils_common_posix.cc +13 -0
  121. data/src/core/lib/iomgr/socket_utils_posix.h +4 -0
  122. data/src/core/lib/iomgr/tcp_client_posix.cc +4 -11
  123. data/src/core/lib/iomgr/tcp_custom.cc +9 -7
  124. data/src/core/lib/iomgr/tcp_posix.cc +20 -16
  125. data/src/core/lib/iomgr/tcp_server.h +1 -4
  126. data/src/core/lib/iomgr/tcp_server_custom.cc +5 -5
  127. data/src/core/lib/iomgr/tcp_server_posix.cc +1 -1
  128. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +2 -11
  129. data/src/core/lib/iomgr/timer_custom.cc +2 -2
  130. data/src/core/lib/iomgr/udp_server.cc +3 -2
  131. data/src/core/lib/iomgr/udp_server.h +6 -12
  132. data/src/core/lib/json/json.h +1 -1
  133. data/src/core/lib/json/json_string.cc +2 -2
  134. data/src/core/lib/profiling/basic_timers.cc +2 -2
  135. data/src/core/lib/security/credentials/alts/alts_credentials.cc +2 -2
  136. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_server_options.cc +1 -1
  137. data/src/core/lib/security/credentials/credentials.h +4 -20
  138. data/src/core/lib/security/credentials/fake/fake_credentials.cc +4 -4
  139. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -3
  140. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +64 -0
  141. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +4 -4
  142. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +9 -7
  143. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -0
  144. data/src/core/lib/security/security_connector/local/local_security_connector.cc +4 -4
  145. data/src/core/lib/security/security_connector/security_connector.cc +1 -0
  146. data/src/core/lib/security/security_connector/security_connector.h +19 -17
  147. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +8 -5
  148. data/src/core/lib/security/security_connector/ssl_utils.cc +2 -2
  149. data/src/core/lib/security/security_connector/ssl_utils.h +1 -1
  150. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +14 -6
  151. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +4 -2
  152. data/src/core/lib/security/transport/client_auth_filter.cc +17 -17
  153. data/src/core/lib/security/transport/security_handshaker.cc +29 -13
  154. data/src/core/lib/security/transport/security_handshaker.h +4 -2
  155. data/src/core/lib/security/transport/server_auth_filter.cc +14 -14
  156. data/src/core/lib/slice/slice.cc +2 -10
  157. data/src/core/lib/slice/slice_hash_table.h +4 -6
  158. data/src/core/lib/slice/slice_intern.cc +42 -39
  159. data/src/core/lib/slice/slice_internal.h +3 -3
  160. data/src/core/lib/slice/slice_utils.h +21 -4
  161. data/src/core/lib/slice/slice_weak_hash_table.h +4 -6
  162. data/src/core/lib/surface/call.cc +3 -3
  163. data/src/core/lib/surface/channel.cc +7 -0
  164. data/src/core/lib/surface/completion_queue.cc +12 -11
  165. data/src/core/lib/surface/completion_queue.h +4 -2
  166. data/src/core/lib/surface/init.cc +1 -0
  167. data/src/core/lib/surface/lame_client.cc +33 -18
  168. data/src/core/lib/surface/server.cc +77 -76
  169. data/src/core/lib/surface/version.cc +1 -1
  170. data/src/core/lib/transport/byte_stream.h +3 -7
  171. data/src/core/lib/transport/connectivity_state.cc +112 -98
  172. data/src/core/lib/transport/connectivity_state.h +100 -50
  173. data/src/core/lib/transport/static_metadata.cc +276 -288
  174. data/src/core/lib/transport/static_metadata.h +73 -76
  175. data/src/core/lib/transport/status_conversion.cc +1 -1
  176. data/src/core/lib/transport/status_metadata.cc +1 -1
  177. data/src/core/lib/transport/transport.cc +2 -2
  178. data/src/core/lib/transport/transport.h +12 -4
  179. data/src/core/lib/transport/transport_op_string.cc +14 -11
  180. data/src/core/tsi/alts/frame_protector/alts_unseal_privacy_integrity_crypter.cc +1 -1
  181. data/src/core/tsi/alts/handshaker/alts_shared_resource.cc +1 -1
  182. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +5 -5
  183. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -2
  184. data/src/core/tsi/fake_transport_security.cc +7 -5
  185. data/src/core/tsi/grpc_shadow_boringssl.h +2918 -2627
  186. data/src/core/tsi/local_transport_security.cc +8 -6
  187. data/src/core/tsi/ssl/session_cache/ssl_session.h +1 -3
  188. data/src/core/tsi/ssl/session_cache/ssl_session_boringssl.cc +1 -2
  189. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +7 -5
  190. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +4 -6
  191. data/src/core/tsi/ssl/session_cache/ssl_session_openssl.cc +1 -2
  192. data/src/core/tsi/ssl_transport_security.cc +12 -12
  193. data/src/core/tsi/ssl_transport_security.h +2 -2
  194. data/src/core/tsi/transport_security_grpc.cc +7 -0
  195. data/src/core/tsi/transport_security_grpc.h +6 -0
  196. data/src/ruby/ext/grpc/extconf.rb +1 -0
  197. data/src/ruby/ext/grpc/rb_call.c +1 -1
  198. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  199. data/src/ruby/lib/grpc/generic/bidi_call.rb +1 -1
  200. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  201. data/src/ruby/lib/grpc/version.rb +1 -1
  202. data/src/ruby/spec/google_rpc_status_utils_spec.rb +2 -2
  203. data/third_party/boringssl/crypto/asn1/a_bool.c +18 -5
  204. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +17 -221
  205. data/third_party/boringssl/crypto/asn1/a_dup.c +0 -24
  206. data/third_party/boringssl/crypto/asn1/a_enum.c +2 -2
  207. data/third_party/boringssl/crypto/asn1/a_i2d_fp.c +10 -72
  208. data/third_party/boringssl/crypto/asn1/a_int.c +12 -71
  209. data/third_party/boringssl/crypto/asn1/a_mbstr.c +110 -216
  210. data/third_party/boringssl/crypto/asn1/a_object.c +16 -5
  211. data/third_party/boringssl/crypto/asn1/a_strnid.c +1 -0
  212. data/third_party/boringssl/crypto/asn1/asn1_lib.c +5 -1
  213. data/third_party/boringssl/crypto/asn1/tasn_enc.c +3 -1
  214. data/third_party/boringssl/crypto/base64/base64.c +2 -2
  215. data/third_party/boringssl/crypto/bio/bio.c +73 -9
  216. data/third_party/boringssl/crypto/bio/connect.c +4 -0
  217. data/third_party/boringssl/crypto/bio/fd.c +4 -0
  218. data/third_party/boringssl/crypto/bio/file.c +5 -2
  219. data/third_party/boringssl/crypto/bio/socket.c +4 -0
  220. data/third_party/boringssl/crypto/bio/socket_helper.c +4 -0
  221. data/third_party/boringssl/crypto/bn_extra/convert.c +11 -7
  222. data/third_party/boringssl/crypto/bytestring/ber.c +8 -4
  223. data/third_party/boringssl/crypto/bytestring/cbb.c +19 -7
  224. data/third_party/boringssl/crypto/bytestring/cbs.c +28 -15
  225. data/third_party/boringssl/crypto/bytestring/internal.h +28 -7
  226. data/third_party/boringssl/crypto/bytestring/unicode.c +155 -0
  227. data/third_party/boringssl/crypto/chacha/chacha.c +36 -19
  228. data/third_party/boringssl/crypto/chacha/internal.h +45 -0
  229. data/third_party/boringssl/crypto/cipher_extra/cipher_extra.c +29 -0
  230. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +269 -25
  231. data/third_party/boringssl/crypto/cipher_extra/e_aesctrhmac.c +16 -14
  232. data/third_party/boringssl/crypto/cipher_extra/e_aesgcmsiv.c +54 -38
  233. data/third_party/boringssl/crypto/cipher_extra/e_chacha20poly1305.c +133 -41
  234. data/third_party/boringssl/crypto/cipher_extra/e_tls.c +23 -15
  235. data/third_party/boringssl/crypto/cipher_extra/tls_cbc.c +24 -15
  236. data/third_party/boringssl/crypto/cmac/cmac.c +62 -25
  237. data/third_party/boringssl/crypto/conf/conf.c +7 -0
  238. data/third_party/boringssl/crypto/cpu-arm-linux.c +4 -148
  239. data/third_party/boringssl/crypto/cpu-arm-linux.h +201 -0
  240. data/third_party/boringssl/crypto/cpu-intel.c +45 -51
  241. data/third_party/boringssl/crypto/crypto.c +39 -22
  242. data/third_party/boringssl/crypto/curve25519/spake25519.c +1 -1
  243. data/third_party/boringssl/crypto/dsa/dsa.c +77 -53
  244. data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +20 -8
  245. data/third_party/boringssl/crypto/ec_extra/ec_derive.c +96 -0
  246. data/third_party/boringssl/crypto/{ecdh/ecdh.c → ecdh_extra/ecdh_extra.c} +20 -58
  247. data/third_party/boringssl/crypto/ecdsa_extra/ecdsa_asn1.c +1 -9
  248. data/third_party/boringssl/crypto/engine/engine.c +2 -1
  249. data/third_party/boringssl/crypto/err/err.c +2 -0
  250. data/third_party/boringssl/crypto/err/internal.h +2 -2
  251. data/third_party/boringssl/crypto/evp/evp.c +89 -8
  252. data/third_party/boringssl/crypto/evp/evp_asn1.c +56 -5
  253. data/third_party/boringssl/crypto/evp/evp_ctx.c +52 -14
  254. data/third_party/boringssl/crypto/evp/internal.h +18 -1
  255. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +5 -0
  256. data/third_party/boringssl/crypto/evp/p_ec.c +51 -3
  257. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +6 -7
  258. data/third_party/boringssl/crypto/evp/p_ed25519.c +36 -3
  259. data/third_party/boringssl/crypto/evp/p_ed25519_asn1.c +76 -45
  260. data/third_party/boringssl/crypto/evp/p_rsa.c +3 -1
  261. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +5 -0
  262. data/third_party/boringssl/crypto/evp/p_x25519.c +110 -0
  263. data/third_party/boringssl/crypto/evp/p_x25519_asn1.c +249 -0
  264. data/third_party/boringssl/crypto/evp/scrypt.c +6 -2
  265. data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +34 -274
  266. data/third_party/boringssl/crypto/fipsmodule/aes/internal.h +161 -21
  267. data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +111 -13
  268. data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +17 -21
  269. data/third_party/boringssl/crypto/fipsmodule/bcm.c +119 -7
  270. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +19 -2
  271. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +2 -2
  272. data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +93 -160
  273. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +48 -57
  274. data/third_party/boringssl/crypto/fipsmodule/bn/div_extra.c +87 -0
  275. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +143 -211
  276. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +0 -305
  277. data/third_party/boringssl/crypto/fipsmodule/bn/gcd_extra.c +325 -0
  278. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +168 -50
  279. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +68 -92
  280. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +7 -6
  281. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +11 -14
  282. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +358 -443
  283. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +25 -35
  284. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +20 -25
  285. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.h +76 -5
  286. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +14 -14
  287. data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +7 -2
  288. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +383 -516
  289. data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +4 -0
  290. data/third_party/boringssl/crypto/fipsmodule/cipher/internal.h +3 -4
  291. data/third_party/boringssl/crypto/fipsmodule/delocate.h +3 -2
  292. data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +32 -17
  293. data/third_party/boringssl/crypto/fipsmodule/digest/md32_common.h +3 -3
  294. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +228 -122
  295. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +34 -8
  296. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +311 -98
  297. data/third_party/boringssl/crypto/fipsmodule/ec/felem.c +82 -0
  298. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +263 -97
  299. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +22 -59
  300. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +317 -234
  301. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +9473 -9475
  302. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +313 -109
  303. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.h +36 -0
  304. data/third_party/boringssl/crypto/fipsmodule/ec/scalar.c +96 -0
  305. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +126 -792
  306. data/third_party/boringssl/crypto/fipsmodule/ec/simple_mul.c +84 -0
  307. data/third_party/boringssl/crypto/fipsmodule/ec/util.c +163 -12
  308. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +84 -211
  309. data/third_party/boringssl/crypto/fipsmodule/ecdh/ecdh.c +122 -0
  310. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +60 -205
  311. data/third_party/boringssl/crypto/fipsmodule/fips_shared_support.c +32 -0
  312. data/third_party/boringssl/crypto/fipsmodule/is_fips.c +2 -0
  313. data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +3 -1
  314. data/third_party/boringssl/crypto/fipsmodule/md5/internal.h +37 -0
  315. data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +11 -8
  316. data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +35 -79
  317. data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +7 -39
  318. data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +7 -27
  319. data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +123 -309
  320. data/third_party/boringssl/crypto/fipsmodule/modes/internal.h +189 -126
  321. data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +3 -2
  322. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +2 -2
  323. data/third_party/boringssl/crypto/fipsmodule/rand/internal.h +35 -0
  324. data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +24 -19
  325. data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +256 -77
  326. data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +10 -7
  327. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +5 -1
  328. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +131 -14
  329. data/third_party/boringssl/crypto/fipsmodule/self_check/self_check.c +83 -10
  330. data/third_party/boringssl/crypto/fipsmodule/sha/internal.h +53 -0
  331. data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +9 -13
  332. data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +18 -12
  333. data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +95 -168
  334. data/third_party/boringssl/crypto/hrss/hrss.c +2201 -0
  335. data/third_party/boringssl/crypto/hrss/internal.h +62 -0
  336. data/third_party/boringssl/crypto/internal.h +95 -20
  337. data/third_party/boringssl/crypto/lhash/lhash.c +45 -33
  338. data/third_party/boringssl/crypto/mem.c +39 -2
  339. data/third_party/boringssl/crypto/obj/obj.c +4 -4
  340. data/third_party/boringssl/crypto/obj/obj_dat.h +6181 -875
  341. data/third_party/boringssl/crypto/pem/pem_all.c +2 -3
  342. data/third_party/boringssl/crypto/pem/pem_info.c +144 -162
  343. data/third_party/boringssl/crypto/pem/pem_lib.c +53 -52
  344. data/third_party/boringssl/crypto/pem/pem_pkey.c +13 -21
  345. data/third_party/boringssl/crypto/pkcs7/pkcs7.c +15 -22
  346. data/third_party/boringssl/crypto/pkcs7/pkcs7_x509.c +168 -16
  347. data/third_party/boringssl/crypto/pkcs8/internal.h +11 -0
  348. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +24 -15
  349. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +42 -25
  350. data/third_party/boringssl/crypto/pkcs8/pkcs8_x509.c +559 -43
  351. data/third_party/boringssl/crypto/pool/internal.h +1 -1
  352. data/third_party/boringssl/crypto/pool/pool.c +21 -0
  353. data/third_party/boringssl/crypto/rand_extra/deterministic.c +8 -0
  354. data/third_party/boringssl/crypto/rand_extra/fuchsia.c +1 -14
  355. data/third_party/boringssl/crypto/refcount_lock.c +2 -2
  356. data/third_party/boringssl/crypto/rsa_extra/rsa_print.c +22 -0
  357. data/third_party/boringssl/crypto/siphash/siphash.c +80 -0
  358. data/third_party/boringssl/crypto/stack/stack.c +83 -32
  359. data/third_party/boringssl/crypto/thread_none.c +2 -2
  360. data/third_party/boringssl/crypto/thread_pthread.c +2 -2
  361. data/third_party/boringssl/crypto/thread_win.c +38 -19
  362. data/third_party/boringssl/crypto/x509/a_strex.c +22 -2
  363. data/third_party/boringssl/crypto/x509/asn1_gen.c +2 -1
  364. data/third_party/boringssl/crypto/x509/by_dir.c +7 -0
  365. data/third_party/boringssl/crypto/x509/by_file.c +12 -10
  366. data/third_party/boringssl/crypto/x509/t_crl.c +5 -8
  367. data/third_party/boringssl/crypto/x509/t_req.c +1 -3
  368. data/third_party/boringssl/crypto/x509/t_x509.c +5 -8
  369. data/third_party/boringssl/crypto/x509/x509_cmp.c +1 -1
  370. data/third_party/boringssl/crypto/x509/x509_def.c +1 -1
  371. data/third_party/boringssl/crypto/x509/x509_lu.c +114 -5
  372. data/third_party/boringssl/crypto/x509/x509_req.c +20 -0
  373. data/third_party/boringssl/crypto/x509/x509_set.c +5 -0
  374. data/third_party/boringssl/crypto/x509/x509_trs.c +1 -0
  375. data/third_party/boringssl/crypto/x509/x509_txt.c +4 -5
  376. data/third_party/boringssl/crypto/x509/x509_vfy.c +145 -138
  377. data/third_party/boringssl/crypto/x509/x509_vpm.c +2 -0
  378. data/third_party/boringssl/crypto/x509/x509cset.c +40 -0
  379. data/third_party/boringssl/crypto/x509/x509name.c +2 -3
  380. data/third_party/boringssl/crypto/x509/x_all.c +109 -210
  381. data/third_party/boringssl/crypto/x509/x_x509.c +6 -0
  382. data/third_party/boringssl/crypto/x509v3/ext_dat.h +1 -3
  383. data/third_party/boringssl/crypto/x509v3/internal.h +56 -0
  384. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +2 -0
  385. data/third_party/boringssl/crypto/x509v3/pcy_node.c +1 -0
  386. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +4 -2
  387. data/third_party/boringssl/crypto/x509v3/v3_akey.c +5 -2
  388. data/third_party/boringssl/crypto/x509v3/v3_alt.c +19 -13
  389. data/third_party/boringssl/crypto/x509v3/v3_conf.c +2 -1
  390. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +3 -2
  391. data/third_party/boringssl/crypto/x509v3/v3_genn.c +1 -6
  392. data/third_party/boringssl/crypto/x509v3/v3_lib.c +1 -0
  393. data/third_party/boringssl/crypto/x509v3/v3_ocsp.c +68 -0
  394. data/third_party/boringssl/crypto/x509v3/v3_pci.c +2 -1
  395. data/third_party/boringssl/crypto/x509v3/v3_purp.c +47 -69
  396. data/third_party/boringssl/crypto/x509v3/v3_skey.c +5 -2
  397. data/third_party/boringssl/crypto/x509v3/v3_utl.c +69 -25
  398. data/third_party/boringssl/include/openssl/aead.h +45 -19
  399. data/third_party/boringssl/include/openssl/aes.h +32 -7
  400. data/third_party/boringssl/include/openssl/asn1.h +7 -77
  401. data/third_party/boringssl/include/openssl/base.h +120 -6
  402. data/third_party/boringssl/include/openssl/base64.h +4 -1
  403. data/third_party/boringssl/include/openssl/bio.h +112 -81
  404. data/third_party/boringssl/include/openssl/blowfish.h +3 -3
  405. data/third_party/boringssl/include/openssl/bn.h +55 -29
  406. data/third_party/boringssl/include/openssl/buf.h +2 -2
  407. data/third_party/boringssl/include/openssl/bytestring.h +54 -32
  408. data/third_party/boringssl/include/openssl/cast.h +2 -2
  409. data/third_party/boringssl/include/openssl/cipher.h +46 -16
  410. data/third_party/boringssl/include/openssl/cmac.h +6 -2
  411. data/third_party/boringssl/include/openssl/conf.h +3 -6
  412. data/third_party/boringssl/include/openssl/cpu.h +25 -9
  413. data/third_party/boringssl/include/openssl/crypto.h +32 -10
  414. data/third_party/boringssl/include/openssl/curve25519.h +4 -4
  415. data/third_party/boringssl/include/openssl/dh.h +3 -2
  416. data/third_party/boringssl/include/openssl/digest.h +21 -7
  417. data/third_party/boringssl/include/openssl/dsa.h +8 -2
  418. data/third_party/boringssl/include/openssl/e_os2.h +18 -0
  419. data/third_party/boringssl/include/openssl/ec.h +25 -21
  420. data/third_party/boringssl/include/openssl/ec_key.h +36 -8
  421. data/third_party/boringssl/include/openssl/ecdh.h +17 -0
  422. data/third_party/boringssl/include/openssl/ecdsa.h +3 -3
  423. data/third_party/boringssl/include/openssl/engine.h +4 -4
  424. data/third_party/boringssl/include/openssl/err.h +3 -0
  425. data/third_party/boringssl/include/openssl/evp.h +199 -42
  426. data/third_party/boringssl/include/openssl/hmac.h +4 -4
  427. data/third_party/boringssl/include/openssl/hrss.h +100 -0
  428. data/third_party/boringssl/include/openssl/lhash.h +131 -23
  429. data/third_party/boringssl/include/openssl/md4.h +6 -4
  430. data/third_party/boringssl/include/openssl/md5.h +6 -4
  431. data/third_party/boringssl/include/openssl/mem.h +6 -2
  432. data/third_party/boringssl/include/openssl/nid.h +3 -0
  433. data/third_party/boringssl/include/openssl/obj.h +3 -0
  434. data/third_party/boringssl/include/openssl/pem.h +102 -64
  435. data/third_party/boringssl/include/openssl/pkcs7.h +136 -3
  436. data/third_party/boringssl/include/openssl/pkcs8.h +42 -3
  437. data/third_party/boringssl/include/openssl/pool.h +13 -2
  438. data/third_party/boringssl/include/openssl/ripemd.h +5 -4
  439. data/third_party/boringssl/include/openssl/rsa.h +46 -15
  440. data/third_party/boringssl/include/openssl/sha.h +40 -28
  441. data/third_party/boringssl/include/openssl/siphash.h +37 -0
  442. data/third_party/boringssl/include/openssl/span.h +17 -9
  443. data/third_party/boringssl/include/openssl/ssl.h +766 -393
  444. data/third_party/boringssl/include/openssl/ssl3.h +4 -3
  445. data/third_party/boringssl/include/openssl/stack.h +134 -77
  446. data/third_party/boringssl/include/openssl/thread.h +1 -1
  447. data/third_party/boringssl/include/openssl/tls1.h +25 -9
  448. data/third_party/boringssl/include/openssl/type_check.h +14 -15
  449. data/third_party/boringssl/include/openssl/x509.h +28 -3
  450. data/third_party/boringssl/include/openssl/x509_vfy.h +98 -32
  451. data/third_party/boringssl/include/openssl/x509v3.h +17 -13
  452. data/third_party/boringssl/ssl/d1_both.cc +9 -18
  453. data/third_party/boringssl/ssl/d1_lib.cc +4 -3
  454. data/third_party/boringssl/ssl/d1_pkt.cc +4 -4
  455. data/third_party/boringssl/ssl/d1_srtp.cc +15 -15
  456. data/third_party/boringssl/ssl/dtls_method.cc +0 -1
  457. data/third_party/boringssl/ssl/dtls_record.cc +28 -28
  458. data/third_party/boringssl/ssl/handoff.cc +295 -91
  459. data/third_party/boringssl/ssl/handshake.cc +133 -72
  460. data/third_party/boringssl/ssl/handshake_client.cc +218 -189
  461. data/third_party/boringssl/ssl/handshake_server.cc +399 -272
  462. data/third_party/boringssl/ssl/internal.h +1413 -928
  463. data/third_party/boringssl/ssl/s3_both.cc +175 -36
  464. data/third_party/boringssl/ssl/s3_lib.cc +9 -13
  465. data/third_party/boringssl/ssl/s3_pkt.cc +63 -29
  466. data/third_party/boringssl/ssl/ssl_aead_ctx.cc +55 -35
  467. data/third_party/boringssl/ssl/ssl_asn1.cc +57 -73
  468. data/third_party/boringssl/ssl/ssl_buffer.cc +13 -12
  469. data/third_party/boringssl/ssl/ssl_cert.cc +313 -210
  470. data/third_party/boringssl/ssl/ssl_cipher.cc +159 -221
  471. data/third_party/boringssl/ssl/ssl_file.cc +2 -0
  472. data/third_party/boringssl/ssl/ssl_key_share.cc +164 -19
  473. data/third_party/boringssl/ssl/ssl_lib.cc +847 -555
  474. data/third_party/boringssl/ssl/ssl_privkey.cc +441 -111
  475. data/third_party/boringssl/ssl/ssl_session.cc +230 -178
  476. data/third_party/boringssl/ssl/ssl_transcript.cc +21 -142
  477. data/third_party/boringssl/ssl/ssl_versions.cc +88 -93
  478. data/third_party/boringssl/ssl/ssl_x509.cc +279 -218
  479. data/third_party/boringssl/ssl/t1_enc.cc +5 -96
  480. data/third_party/boringssl/ssl/t1_lib.cc +931 -678
  481. data/third_party/boringssl/ssl/tls13_both.cc +251 -121
  482. data/third_party/boringssl/ssl/tls13_client.cc +129 -73
  483. data/third_party/boringssl/ssl/tls13_enc.cc +350 -282
  484. data/third_party/boringssl/ssl/tls13_server.cc +259 -192
  485. data/third_party/boringssl/ssl/tls_method.cc +26 -21
  486. data/third_party/boringssl/ssl/tls_record.cc +42 -47
  487. data/third_party/boringssl/third_party/fiat/curve25519.c +261 -1324
  488. data/third_party/boringssl/third_party/fiat/curve25519_32.h +911 -0
  489. data/third_party/boringssl/third_party/fiat/curve25519_64.h +559 -0
  490. data/third_party/boringssl/third_party/fiat/p256.c +238 -999
  491. data/third_party/boringssl/third_party/fiat/p256_32.h +3226 -0
  492. data/third_party/boringssl/third_party/fiat/p256_64.h +1217 -0
  493. data/third_party/upb/upb/port_def.inc +1 -1
  494. data/third_party/upb/upb/table.c +2 -1
  495. metadata +71 -43
  496. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +0 -127
  497. data/src/core/lib/gpr/mpscq.cc +0 -117
  498. data/src/core/lib/gpr/mpscq.h +0 -88
  499. data/src/core/lib/gprpp/abstract.h +0 -47
  500. data/src/core/lib/gprpp/pair.h +0 -38
  501. data/third_party/boringssl/crypto/cipher_extra/e_ssl3.c +0 -460
  502. data/third_party/boringssl/crypto/fipsmodule/modes/ccm.c +0 -256
  503. data/third_party/boringssl/include/openssl/lhash_macros.h +0 -174
  504. data/third_party/boringssl/ssl/custom_extensions.cc +0 -265
@@ -108,10 +108,10 @@
108
108
 
109
109
  #include <openssl/bn.h>
110
110
 
111
+ #include <limits.h>
111
112
  #include <string.h>
112
113
 
113
114
  #include <openssl/err.h>
114
- #include <openssl/mem.h>
115
115
  #include <openssl/rand.h>
116
116
  #include <openssl/type_check.h>
117
117
 
@@ -121,9 +121,6 @@
121
121
 
122
122
 
123
123
  int BN_rand(BIGNUM *rnd, int bits, int top, int bottom) {
124
- uint8_t *buf = NULL;
125
- int ret = 0, bit, bytes, mask;
126
-
127
124
  if (rnd == NULL) {
128
125
  return 0;
129
126
  }
@@ -144,48 +141,41 @@ int BN_rand(BIGNUM *rnd, int bits, int top, int bottom) {
144
141
  return 1;
145
142
  }
146
143
 
147
- bytes = (bits + 7) / 8;
148
- bit = (bits - 1) % 8;
149
- mask = 0xff << (bit + 1);
150
-
151
- buf = OPENSSL_malloc(bytes);
152
- if (buf == NULL) {
153
- OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
154
- goto err;
144
+ if (bits > INT_MAX - (BN_BITS2 - 1)) {
145
+ OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
146
+ return 0;
155
147
  }
156
148
 
157
- // Make a random number and set the top and bottom bits.
158
- RAND_bytes(buf, bytes);
149
+ int words = (bits + BN_BITS2 - 1) / BN_BITS2;
150
+ int bit = (bits - 1) % BN_BITS2;
151
+ const BN_ULONG kOne = 1;
152
+ const BN_ULONG kThree = 3;
153
+ BN_ULONG mask = bit < BN_BITS2 - 1 ? (kOne << (bit + 1)) - 1 : BN_MASK2;
154
+ if (!bn_wexpand(rnd, words)) {
155
+ return 0;
156
+ }
159
157
 
158
+ RAND_bytes((uint8_t *)rnd->d, words * sizeof(BN_ULONG));
159
+ rnd->d[words - 1] &= mask;
160
160
  if (top != BN_RAND_TOP_ANY) {
161
161
  if (top == BN_RAND_TOP_TWO && bits > 1) {
162
162
  if (bit == 0) {
163
- buf[0] = 1;
164
- buf[1] |= 0x80;
163
+ rnd->d[words - 1] |= 1;
164
+ rnd->d[words - 2] |= kOne << (BN_BITS2 - 1);
165
165
  } else {
166
- buf[0] |= (3 << (bit - 1));
166
+ rnd->d[words - 1] |= kThree << (bit - 1);
167
167
  }
168
168
  } else {
169
- buf[0] |= (1 << bit);
169
+ rnd->d[words - 1] |= kOne << bit;
170
170
  }
171
171
  }
172
-
173
- buf[0] &= ~mask;
174
-
175
- // Set the bottom bit if requested,
176
- if (bottom == BN_RAND_BOTTOM_ODD) {
177
- buf[bytes - 1] |= 1;
178
- }
179
-
180
- if (!BN_bin2bn(buf, bytes, rnd)) {
181
- goto err;
172
+ if (bottom == BN_RAND_BOTTOM_ODD) {
173
+ rnd->d[0] |= 1;
182
174
  }
183
175
 
184
- ret = 1;
185
-
186
- err:
187
- OPENSSL_free(buf);
188
- return ret;
176
+ rnd->neg = 0;
177
+ rnd->width = words;
178
+ return 1;
189
179
  }
190
180
 
191
181
  int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) {
@@ -205,8 +195,8 @@ static crypto_word_t bn_less_than_word_mask(const BN_ULONG *a, size_t len,
205
195
  }
206
196
 
207
197
  // |a| < |b| iff a[1..len-1] are all zero and a[0] < b.
208
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
209
- crypto_word_t_too_small);
198
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
199
+ "crypto_word_t is too small");
210
200
  crypto_word_t mask = 0;
211
201
  for (size_t i = 1; i < len; i++) {
212
202
  mask |= a[i];
@@ -12,27 +12,16 @@
12
12
  * (2) University of Haifa, Israel
13
13
  */
14
14
 
15
- #include <openssl/base.h>
16
-
17
- #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
18
-
19
15
  #include "rsaz_exp.h"
20
16
 
17
+ #if defined(RSAZ_ENABLED)
18
+
21
19
  #include <openssl/mem.h>
22
20
 
21
+ #include "internal.h"
23
22
  #include "../../internal.h"
24
23
 
25
24
 
26
- // See crypto/bn/asm/rsaz-avx2.pl for further details.
27
- void rsaz_1024_norm2red_avx2(void *red, const void *norm);
28
- void rsaz_1024_mul_avx2(void *ret, const void *a, const void *b, const void *n,
29
- BN_ULONG k);
30
- void rsaz_1024_sqr_avx2(void *ret, const void *a, const void *n, BN_ULONG k,
31
- int cnt);
32
- void rsaz_1024_scatter5_avx2(void *tbl, const void *val, int i);
33
- void rsaz_1024_gather5_avx2(void *val, const void *tbl, int i);
34
- void rsaz_1024_red2norm_avx2(void *norm, const void *red);
35
-
36
25
  // one is 1 in RSAZ's representation.
37
26
  alignas(64) static const BN_ULONG one[40] = {
38
27
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -44,19 +33,25 @@ alignas(64) static const BN_ULONG two80[40] = {
44
33
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
45
34
 
46
35
  void RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16],
47
- const BN_ULONG base_norm[16], const BN_ULONG exponent[16],
48
- const BN_ULONG m_norm[16], const BN_ULONG RR[16], BN_ULONG k0) {
49
- alignas(64) uint8_t storage[(320 * 3) + (32 * 9 * 16)]; // 5.5KB
50
- unsigned char *a_inv, *m, *result, *table_s = storage + (320 * 3),
51
- *R2 = table_s; // borrow
36
+ const BN_ULONG base_norm[16],
37
+ const BN_ULONG exponent[16],
38
+ const BN_ULONG m_norm[16], const BN_ULONG RR[16],
39
+ BN_ULONG k0,
40
+ BN_ULONG storage[MOD_EXP_CTIME_STORAGE_LEN]) {
41
+ OPENSSL_STATIC_ASSERT(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH % 64 == 0,
42
+ "MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH is too small");
43
+ assert((uintptr_t)storage % 64 == 0);
44
+
45
+ BN_ULONG *a_inv, *m, *result, *table_s = storage + 40 * 3, *R2 = table_s;
46
+ // Note |R2| aliases |table_s|.
52
47
  if (((((uintptr_t)storage & 4095) + 320) >> 12) != 0) {
53
48
  result = storage;
54
- a_inv = storage + 320;
55
- m = storage + (320 * 2); // should not cross page
49
+ a_inv = storage + 40;
50
+ m = storage + 40 * 2; // should not cross page
56
51
  } else {
57
52
  m = storage; // should not cross page
58
- result = storage + 320;
59
- a_inv = storage + (320 * 2);
53
+ result = storage + 40;
54
+ a_inv = storage + 40 * 2;
60
55
  }
61
56
 
62
57
  rsaz_1024_norm2red_avx2(m, m_norm);
@@ -225,7 +220,7 @@ void RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16],
225
220
 
226
221
  rsaz_1024_red2norm_avx2(result_norm, result);
227
222
 
228
- OPENSSL_cleanse(storage, sizeof(storage));
223
+ OPENSSL_cleanse(storage, MOD_EXP_CTIME_STORAGE_LEN * sizeof(BN_ULONG));
229
224
  }
230
225
 
231
- #endif // OPENSSL_X86_64
226
+ #endif // RSAZ_ENABLED
@@ -16,18 +16,89 @@
16
16
  #define OPENSSL_HEADER_BN_RSAZ_EXP_H
17
17
 
18
18
  #include <openssl/bn.h>
19
+ #include <openssl/cpu.h>
20
+
21
+ #include "internal.h"
22
+
23
+ #if defined(__cplusplus)
24
+ extern "C" {
25
+ #endif
26
+
27
+ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
28
+ #define RSAZ_ENABLED
29
+
19
30
 
20
31
  // RSAZ_1024_mod_exp_avx2 sets |result| to |base_norm| raised to |exponent|
21
32
  // modulo |m_norm|. |base_norm| must be fully-reduced and |exponent| must have
22
33
  // the high bit set (it is 1024 bits wide). |RR| and |k0| must be |RR| and |n0|,
23
- // respectively, extracted from |m_norm|'s |BN_MONT_CTX|.
34
+ // respectively, extracted from |m_norm|'s |BN_MONT_CTX|. |storage_words| is a
35
+ // temporary buffer that must be aligned to |MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH|
36
+ // bytes.
24
37
  void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16], const BN_ULONG base_norm[16],
25
38
  const BN_ULONG exponent[16],
26
39
  const BN_ULONG m_norm[16], const BN_ULONG RR[16],
27
- BN_ULONG k0);
40
+ BN_ULONG k0,
41
+ BN_ULONG storage_words[MOD_EXP_CTIME_STORAGE_LEN]);
42
+
43
+ OPENSSL_INLINE int rsaz_avx2_capable(void) {
44
+ const uint32_t *cap = OPENSSL_ia32cap_get();
45
+ return (cap[2] & (1 << 5)) != 0; // AVX2
46
+ }
47
+
48
+ OPENSSL_INLINE int rsaz_avx2_preferred(void) {
49
+ const uint32_t *cap = OPENSSL_ia32cap_get();
50
+ static const uint32_t kBMI2AndADX = (1 << 8) | (1 << 19);
51
+ if ((cap[2] & kBMI2AndADX) == kBMI2AndADX) {
52
+ // If BMI2 and ADX are available, x86_64-mont5.pl is faster.
53
+ return 0;
54
+ }
55
+ return (cap[2] & (1 << 5)) != 0; // AVX2
56
+ }
57
+
58
+
59
+ // Assembly functions.
60
+
61
+ // RSAZ represents 1024-bit integers using unsaturated 29-bit limbs stored in
62
+ // 64-bit integers. This requires 36 limbs but padded up to 40.
63
+ //
64
+ // See crypto/bn/asm/rsaz-avx2.pl for further details.
65
+
66
+ // rsaz_1024_norm2red_avx2 converts |norm| from |BIGNUM| to RSAZ representation
67
+ // and writes the result to |red|.
68
+ void rsaz_1024_norm2red_avx2(BN_ULONG red[40], const BN_ULONG norm[16]);
69
+
70
+ // rsaz_1024_mul_avx2 computes |a| * |b| mod |n| and writes the result to |ret|.
71
+ // Inputs and outputs are in Montgomery form, using RSAZ's representation. |k|
72
+ // is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|.
73
+ void rsaz_1024_mul_avx2(BN_ULONG ret[40], const BN_ULONG a[40],
74
+ const BN_ULONG b[40], const BN_ULONG n[40], BN_ULONG k);
75
+
76
+ // rsaz_1024_mul_avx2 computes |a|^(2*|count|) mod |n| and writes the result to
77
+ // |ret|. Inputs and outputs are in Montgomery form, using RSAZ's
78
+ // representation. |k| is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|.
79
+ void rsaz_1024_sqr_avx2(BN_ULONG ret[40], const BN_ULONG a[40],
80
+ const BN_ULONG n[40], BN_ULONG k, int count);
81
+
82
+ // rsaz_1024_scatter5_avx2 stores |val| at index |i| of |tbl|. |i| must be
83
+ // positive and at most 31. Note the table only uses 18 |BN_ULONG|s per entry
84
+ // instead of 40. It packs two 29-bit limbs into each |BN_ULONG| and only stores
85
+ // 36 limbs rather than the padded 40.
86
+ void rsaz_1024_scatter5_avx2(BN_ULONG tbl[32 * 18], const BN_ULONG val[40],
87
+ int i);
88
+
89
+ // rsaz_1024_gather5_avx2 loads index |i| of |tbl| and writes it to |val|.
90
+ void rsaz_1024_gather5_avx2(BN_ULONG val[40], const BN_ULONG tbl[32 * 18],
91
+ int i);
92
+
93
+ // rsaz_1024_red2norm_avx2 converts |red| from RSAZ to |BIGNUM| representation
94
+ // and writes the result to |norm|.
95
+ void rsaz_1024_red2norm_avx2(BN_ULONG norm[16], const BN_ULONG red[40]);
96
+
97
+
98
+ #endif // !OPENSSL_NO_ASM && OPENSSL_X86_64
28
99
 
29
- // rsaz_avx2_eligible returns one if |RSAZ_1024_mod_exp_avx2| should be used and
30
- // zero otherwise.
31
- int rsaz_avx2_eligible(void);
100
+ #if defined(__cplusplus)
101
+ } // extern "C"
102
+ #endif
32
103
 
33
104
  #endif // OPENSSL_HEADER_BN_RSAZ_EXP_H
@@ -133,8 +133,8 @@ int BN_lshift1(BIGNUM *r, const BIGNUM *a) {
133
133
  return 1;
134
134
  }
135
135
 
136
- static void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift,
137
- size_t num) {
136
+ void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift,
137
+ size_t num) {
138
138
  unsigned shift_bits = shift % BN_BITS2;
139
139
  size_t shift_words = shift / BN_BITS2;
140
140
  if (shift_words >= num) {
@@ -296,15 +296,15 @@ int BN_mask_bits(BIGNUM *a, int n) {
296
296
  }
297
297
 
298
298
  static int bn_count_low_zero_bits_word(BN_ULONG l) {
299
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
300
- crypto_word_t_too_small);
301
- OPENSSL_COMPILE_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
302
- crypto_word_t_too_small_2);
303
- OPENSSL_COMPILE_ASSERT(BN_BITS2 == sizeof(BN_ULONG) * 8,
304
- bn_ulong_has_padding_bits);
299
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
300
+ "crypto_word_t is too small");
301
+ OPENSSL_STATIC_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
302
+ "crypto_word_t is too small");
303
+ OPENSSL_STATIC_ASSERT(BN_BITS2 == sizeof(BN_ULONG) * 8,
304
+ "BN_ULONG has padding bits");
305
305
  // C has very bizarre rules for types smaller than an int.
306
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) >= sizeof(int),
307
- bn_ulong_is_promoted_to_int);
306
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) >= sizeof(int),
307
+ "BN_ULONG gets promoted to int");
308
308
 
309
309
  crypto_word_t mask;
310
310
  int bits = 0;
@@ -342,10 +342,10 @@ static int bn_count_low_zero_bits_word(BN_ULONG l) {
342
342
  }
343
343
 
344
344
  int BN_count_low_zero_bits(const BIGNUM *bn) {
345
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
346
- crypto_word_t_too_small);
347
- OPENSSL_COMPILE_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
348
- crypto_word_t_too_small_2);
345
+ OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
346
+ "crypto_word_t is too small");
347
+ OPENSSL_STATIC_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
348
+ "crypto_word_t is too small");
349
349
 
350
350
  int ret = 0;
351
351
  crypto_word_t saw_nonzero = 0;
@@ -125,9 +125,10 @@ int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
125
125
  return 1;
126
126
  }
127
127
 
128
- void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) {
128
+ int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) {
129
129
  EVP_CIPHER_CTX_cleanup(ctx);
130
130
  EVP_CIPHER_CTX_init(ctx);
131
+ return 1;
131
132
  }
132
133
 
133
134
  int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
@@ -191,7 +192,7 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
191
192
 
192
193
  case EVP_CIPH_CFB_MODE:
193
194
  ctx->num = 0;
194
- // fall-through
195
+ OPENSSL_FALLTHROUGH;
195
196
 
196
197
  case EVP_CIPH_CBC_MODE:
197
198
  assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
@@ -496,6 +497,10 @@ int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
496
497
  return ctx->cipher->nid;
497
498
  }
498
499
 
500
+ int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx) {
501
+ return ctx->encrypt;
502
+ }
503
+
499
504
  unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
500
505
  return ctx->cipher->block_size;
501
506
  }
@@ -46,6 +46,7 @@
46
46
  * OF THE POSSIBILITY OF SUCH DAMAGE.
47
47
  * ==================================================================== */
48
48
 
49
+ #include <assert.h>
49
50
  #include <string.h>
50
51
 
51
52
  #include <openssl/aead.h>
@@ -63,13 +64,52 @@
63
64
  #include "../modes/internal.h"
64
65
  #include "../delocate.h"
65
66
 
66
- #if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
67
- #include <openssl/arm_arch.h>
68
- #endif
69
-
70
67
 
68
+ OPENSSL_MSVC_PRAGMA(warning(push))
71
69
  OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) // Unreachable code.
72
70
 
71
+ #if defined(BSAES)
72
+ static void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in,
73
+ uint8_t *out, size_t blocks,
74
+ const AES_KEY *key,
75
+ const uint8_t ivec[16]) {
76
+ // |bsaes_ctr32_encrypt_blocks| is faster than |vpaes_ctr32_encrypt_blocks|,
77
+ // but it takes at least one full 8-block batch to amortize the conversion.
78
+ if (blocks < 8) {
79
+ vpaes_ctr32_encrypt_blocks(in, out, blocks, key, ivec);
80
+ return;
81
+ }
82
+
83
+ size_t bsaes_blocks = blocks;
84
+ if (bsaes_blocks % 8 < 6) {
85
+ // |bsaes_ctr32_encrypt_blocks| internally works in 8-block batches. If the
86
+ // final batch is too small (under six blocks), it is faster to loop over
87
+ // |vpaes_encrypt|. Round |bsaes_blocks| down to a multiple of 8.
88
+ bsaes_blocks -= bsaes_blocks % 8;
89
+ }
90
+
91
+ AES_KEY bsaes;
92
+ vpaes_encrypt_key_to_bsaes(&bsaes, key);
93
+ bsaes_ctr32_encrypt_blocks(in, out, bsaes_blocks, &bsaes, ivec);
94
+ OPENSSL_cleanse(&bsaes, sizeof(bsaes));
95
+
96
+ in += 16 * bsaes_blocks;
97
+ out += 16 * bsaes_blocks;
98
+ blocks -= bsaes_blocks;
99
+
100
+ union {
101
+ uint32_t u32[4];
102
+ uint8_t u8[16];
103
+ } new_ivec;
104
+ memcpy(new_ivec.u8, ivec, 16);
105
+ uint32_t ctr = CRYPTO_bswap4(new_ivec.u32[3]) + bsaes_blocks;
106
+ new_ivec.u32[3] = CRYPTO_bswap4(ctr);
107
+
108
+ // Finish any remaining blocks with |vpaes_ctr32_encrypt_blocks|.
109
+ vpaes_ctr32_encrypt_blocks(in, out, blocks, key, new_ivec.u8);
110
+ }
111
+ #endif // BSAES
112
+
73
113
  typedef struct {
74
114
  union {
75
115
  double align;
@@ -83,13 +123,13 @@ typedef struct {
83
123
  } EVP_AES_KEY;
84
124
 
85
125
  typedef struct {
126
+ GCM128_CONTEXT gcm;
86
127
  union {
87
128
  double align;
88
129
  AES_KEY ks;
89
130
  } ks; // AES key schedule to use
90
131
  int key_set; // Set if key initialised
91
132
  int iv_set; // Set if an iv is set
92
- GCM128_CONTEXT gcm;
93
133
  uint8_t *iv; // Temporary IV store
94
134
  int ivlen; // IV length
95
135
  int taglen;
@@ -97,129 +137,6 @@ typedef struct {
97
137
  ctr128_f ctr;
98
138
  } EVP_AES_GCM_CTX;
99
139
 
100
- #if !defined(OPENSSL_NO_ASM) && \
101
- (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
102
- #define VPAES
103
- static char vpaes_capable(void) {
104
- return (OPENSSL_ia32cap_P[1] & (1 << (41 - 32))) != 0;
105
- }
106
-
107
- #if defined(OPENSSL_X86_64)
108
- #define BSAES
109
- static char bsaes_capable(void) {
110
- return vpaes_capable();
111
- }
112
- #endif
113
-
114
- #elif !defined(OPENSSL_NO_ASM) && \
115
- (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
116
-
117
- #if defined(OPENSSL_ARM) && __ARM_MAX_ARCH__ >= 7
118
- #define BSAES
119
- static char bsaes_capable(void) {
120
- return CRYPTO_is_NEON_capable();
121
- }
122
- #endif
123
-
124
- #endif
125
-
126
-
127
- #if defined(BSAES)
128
- // On platforms where BSAES gets defined (just above), then these functions are
129
- // provided by asm.
130
- void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
131
- const AES_KEY *key, uint8_t ivec[16], int enc);
132
- void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
133
- const AES_KEY *key, const uint8_t ivec[16]);
134
- #else
135
- static char bsaes_capable(void) {
136
- return 0;
137
- }
138
-
139
- // On other platforms, bsaes_capable() will always return false and so the
140
- // following will never be called.
141
- static void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
142
- const AES_KEY *key, uint8_t ivec[16], int enc) {
143
- abort();
144
- }
145
-
146
- static void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
147
- size_t len, const AES_KEY *key,
148
- const uint8_t ivec[16]) {
149
- abort();
150
- }
151
- #endif
152
-
153
- #if defined(VPAES)
154
- // On platforms where VPAES gets defined (just above), then these functions are
155
- // provided by asm.
156
- int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
157
- int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
158
-
159
- void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
160
- void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
161
-
162
- void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
163
- const AES_KEY *key, uint8_t *ivec, int enc);
164
- #else
165
- static char vpaes_capable(void) {
166
- return 0;
167
- }
168
-
169
- // On other platforms, vpaes_capable() will always return false and so the
170
- // following will never be called.
171
- static int vpaes_set_encrypt_key(const uint8_t *userKey, int bits,
172
- AES_KEY *key) {
173
- abort();
174
- }
175
- static int vpaes_set_decrypt_key(const uint8_t *userKey, int bits,
176
- AES_KEY *key) {
177
- abort();
178
- }
179
- static void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
180
- abort();
181
- }
182
- static void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
183
- abort();
184
- }
185
- static void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
186
- const AES_KEY *key, uint8_t *ivec, int enc) {
187
- abort();
188
- }
189
- #endif
190
-
191
- #if !defined(OPENSSL_NO_ASM) && \
192
- (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
193
- int aesni_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
194
- int aesni_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
195
-
196
- void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
197
- void aesni_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
198
-
199
- void aesni_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
200
- const AES_KEY *key, int enc);
201
- void aesni_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
202
- const AES_KEY *key, uint8_t *ivec, int enc);
203
-
204
- #else
205
-
206
- // On other platforms, aesni_capable() will always return false and so the
207
- // following will never be called.
208
- static void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
209
- abort();
210
- }
211
- static int aesni_set_encrypt_key(const uint8_t *userKey, int bits,
212
- AES_KEY *key) {
213
- abort();
214
- }
215
- static void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
216
- size_t blocks, const void *key,
217
- const uint8_t *ivec) {
218
- abort();
219
- }
220
-
221
- #endif
222
-
223
140
  static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
224
141
  const uint8_t *iv, int enc) {
225
142
  int ret, mode;
@@ -229,49 +146,74 @@ static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
229
146
  if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
230
147
  if (hwaes_capable()) {
231
148
  ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
232
- dat->block = (block128_f)aes_hw_decrypt;
149
+ dat->block = aes_hw_decrypt;
233
150
  dat->stream.cbc = NULL;
234
151
  if (mode == EVP_CIPH_CBC_MODE) {
235
- dat->stream.cbc = (cbc128_f)aes_hw_cbc_encrypt;
152
+ dat->stream.cbc = aes_hw_cbc_encrypt;
236
153
  }
237
154
  } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) {
238
- ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
239
- dat->block = (block128_f)AES_decrypt;
240
- dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
155
+ assert(vpaes_capable());
156
+ ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
157
+ if (ret == 0) {
158
+ vpaes_decrypt_key_to_bsaes(&dat->ks.ks, &dat->ks.ks);
159
+ }
160
+ // If |dat->stream.cbc| is provided, |dat->block| is never used.
161
+ dat->block = NULL;
162
+ dat->stream.cbc = bsaes_cbc_encrypt;
241
163
  } else if (vpaes_capable()) {
242
164
  ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
243
- dat->block = (block128_f)vpaes_decrypt;
244
- dat->stream.cbc =
245
- mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL;
165
+ dat->block = vpaes_decrypt;
166
+ dat->stream.cbc = NULL;
167
+ #if defined(VPAES_CBC)
168
+ if (mode == EVP_CIPH_CBC_MODE) {
169
+ dat->stream.cbc = vpaes_cbc_encrypt;
170
+ }
171
+ #endif
246
172
  } else {
247
- ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
248
- dat->block = (block128_f)AES_decrypt;
249
- dat->stream.cbc =
250
- mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL;
173
+ ret = aes_nohw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
174
+ dat->block = aes_nohw_decrypt;
175
+ dat->stream.cbc = NULL;
176
+ #if defined(AES_NOHW_CBC)
177
+ if (mode == EVP_CIPH_CBC_MODE) {
178
+ dat->stream.cbc = aes_nohw_cbc_encrypt;
179
+ }
180
+ #endif
251
181
  }
252
182
  } else if (hwaes_capable()) {
253
183
  ret = aes_hw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
254
- dat->block = (block128_f)aes_hw_encrypt;
184
+ dat->block = aes_hw_encrypt;
255
185
  dat->stream.cbc = NULL;
256
186
  if (mode == EVP_CIPH_CBC_MODE) {
257
- dat->stream.cbc = (cbc128_f)aes_hw_cbc_encrypt;
187
+ dat->stream.cbc = aes_hw_cbc_encrypt;
258
188
  } else if (mode == EVP_CIPH_CTR_MODE) {
259
- dat->stream.ctr = (ctr128_f)aes_hw_ctr32_encrypt_blocks;
189
+ dat->stream.ctr = aes_hw_ctr32_encrypt_blocks;
260
190
  }
261
- } else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) {
262
- ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
263
- dat->block = (block128_f)AES_encrypt;
264
- dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
265
191
  } else if (vpaes_capable()) {
266
192
  ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
267
- dat->block = (block128_f)vpaes_encrypt;
268
- dat->stream.cbc =
269
- mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL;
193
+ dat->block = vpaes_encrypt;
194
+ dat->stream.cbc = NULL;
195
+ #if defined(VPAES_CBC)
196
+ if (mode == EVP_CIPH_CBC_MODE) {
197
+ dat->stream.cbc = vpaes_cbc_encrypt;
198
+ }
199
+ #endif
200
+ if (mode == EVP_CIPH_CTR_MODE) {
201
+ #if defined(BSAES)
202
+ assert(bsaes_capable());
203
+ dat->stream.ctr = vpaes_ctr32_encrypt_blocks_with_bsaes;
204
+ #elif defined(VPAES_CTR32)
205
+ dat->stream.ctr = vpaes_ctr32_encrypt_blocks;
206
+ #endif
207
+ }
270
208
  } else {
271
- ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
272
- dat->block = (block128_f)AES_encrypt;
273
- dat->stream.cbc =
274
- mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL;
209
+ ret = aes_nohw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
210
+ dat->block = aes_nohw_encrypt;
211
+ dat->stream.cbc = NULL;
212
+ #if defined(AES_NOHW_CBC)
213
+ if (mode == EVP_CIPH_CBC_MODE) {
214
+ dat->stream.cbc = aes_nohw_cbc_encrypt;
215
+ }
216
+ #endif
275
217
  }
276
218
 
277
219
  if (ret < 0) {
@@ -287,11 +229,11 @@ static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
287
229
  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
288
230
 
289
231
  if (dat->stream.cbc) {
290
- (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, ctx->encrypt);
232
+ (*dat->stream.cbc)(in, out, len, &dat->ks.ks, ctx->iv, ctx->encrypt);
291
233
  } else if (ctx->encrypt) {
292
- CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
234
+ CRYPTO_cbc128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
293
235
  } else {
294
- CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
236
+ CRYPTO_cbc128_decrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
295
237
  }
296
238
 
297
239
  return 1;
@@ -308,7 +250,7 @@ static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
308
250
 
309
251
  len -= bl;
310
252
  for (size_t i = 0; i <= len; i += bl) {
311
- (*dat->block)(in + i, out + i, &dat->ks);
253
+ (*dat->block)(in + i, out + i, &dat->ks.ks);
312
254
  }
313
255
 
314
256
  return 1;
@@ -319,11 +261,11 @@ static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
319
261
  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
320
262
 
321
263
  if (dat->stream.ctr) {
322
- CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv, ctx->buf,
264
+ CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
323
265
  &ctx->num, dat->stream.ctr);
324
266
  } else {
325
- CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, ctx->buf, &ctx->num,
326
- dat->block);
267
+ CRYPTO_ctr128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
268
+ &ctx->num, dat->block);
327
269
  }
328
270
  return 1;
329
271
  }
@@ -332,78 +274,91 @@ static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
332
274
  size_t len) {
333
275
  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
334
276
 
335
- CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, dat->block);
277
+ CRYPTO_ofb128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, &ctx->num,
278
+ dat->block);
336
279
  return 1;
337
280
  }
338
281
 
339
- static char aesni_capable(void);
340
-
341
- ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx,
282
+ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
342
283
  block128_f *out_block, const uint8_t *key,
343
284
  size_t key_bytes) {
344
- if (aesni_capable()) {
345
- aesni_set_encrypt_key(key, key_bytes * 8, aes_key);
346
- if (gcm_ctx != NULL) {
347
- CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt, 1);
348
- }
349
- if (out_block) {
350
- *out_block = (block128_f) aesni_encrypt;
351
- }
352
- return (ctr128_f)aesni_ctr32_encrypt_blocks;
353
- }
354
-
355
285
  if (hwaes_capable()) {
356
286
  aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key);
357
- if (gcm_ctx != NULL) {
358
- CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_hw_encrypt, 0);
359
- }
360
- if (out_block) {
361
- *out_block = (block128_f) aes_hw_encrypt;
362
- }
363
- return (ctr128_f)aes_hw_ctr32_encrypt_blocks;
364
- }
365
-
366
- if (bsaes_capable()) {
367
- AES_set_encrypt_key(key, key_bytes * 8, aes_key);
368
- if (gcm_ctx != NULL) {
369
- CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt, 0);
287
+ if (gcm_key != NULL) {
288
+ CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt, 1);
370
289
  }
371
290
  if (out_block) {
372
- *out_block = (block128_f) AES_encrypt;
291
+ *out_block = aes_hw_encrypt;
373
292
  }
374
- return (ctr128_f)bsaes_ctr32_encrypt_blocks;
293
+ return aes_hw_ctr32_encrypt_blocks;
375
294
  }
376
295
 
377
296
  if (vpaes_capable()) {
378
297
  vpaes_set_encrypt_key(key, key_bytes * 8, aes_key);
379
298
  if (out_block) {
380
- *out_block = (block128_f) vpaes_encrypt;
299
+ *out_block = vpaes_encrypt;
381
300
  }
382
- if (gcm_ctx != NULL) {
383
- CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt, 0);
301
+ if (gcm_key != NULL) {
302
+ CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt, 0);
384
303
  }
304
+ #if defined(BSAES)
305
+ assert(bsaes_capable());
306
+ return vpaes_ctr32_encrypt_blocks_with_bsaes;
307
+ #elif defined(VPAES_CTR32)
308
+ return vpaes_ctr32_encrypt_blocks;
309
+ #else
385
310
  return NULL;
311
+ #endif
386
312
  }
387
313
 
388
- AES_set_encrypt_key(key, key_bytes * 8, aes_key);
389
- if (gcm_ctx != NULL) {
390
- CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt, 0);
314
+ aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key);
315
+ if (gcm_key != NULL) {
316
+ CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0);
391
317
  }
392
318
  if (out_block) {
393
- *out_block = (block128_f) AES_encrypt;
319
+ *out_block = aes_nohw_encrypt;
394
320
  }
395
321
  return NULL;
396
322
  }
397
323
 
324
+ #if defined(OPENSSL_32_BIT)
325
+ #define EVP_AES_GCM_CTX_PADDING (4+8)
326
+ #else
327
+ #define EVP_AES_GCM_CTX_PADDING 8
328
+ #endif
329
+
330
+ static EVP_AES_GCM_CTX *aes_gcm_from_cipher_ctx(EVP_CIPHER_CTX *ctx) {
331
+ #if defined(__GNUC__) || defined(__clang__)
332
+ OPENSSL_STATIC_ASSERT(
333
+ alignof(EVP_AES_GCM_CTX) <= 16,
334
+ "EVP_AES_GCM_CTX needs more alignment than this function provides");
335
+ #endif
336
+
337
+ // |malloc| guarantees up to 4-byte alignment on 32-bit and 8-byte alignment
338
+ // on 64-bit systems, so we need to adjust to reach 16-byte alignment.
339
+ assert(ctx->cipher->ctx_size ==
340
+ sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING);
341
+
342
+ char *ptr = ctx->cipher_data;
343
+ #if defined(OPENSSL_32_BIT)
344
+ assert((uintptr_t)ptr % 4 == 0);
345
+ ptr += (uintptr_t)ptr & 4;
346
+ #endif
347
+ assert((uintptr_t)ptr % 8 == 0);
348
+ ptr += (uintptr_t)ptr & 8;
349
+ return (EVP_AES_GCM_CTX *)ptr;
350
+ }
351
+
398
352
  static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
399
353
  const uint8_t *iv, int enc) {
400
- EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
354
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(ctx);
401
355
  if (!iv && !key) {
402
356
  return 1;
403
357
  }
404
358
  if (key) {
405
- gctx->ctr =
406
- aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm, NULL, key, ctx->key_len);
359
+ OPENSSL_memset(&gctx->gcm, 0, sizeof(gctx->gcm));
360
+ gctx->ctr = aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm.gcm_key, NULL, key,
361
+ ctx->key_len);
407
362
  // If we have an iv can set it directly, otherwise use saved IV.
408
363
  if (iv == NULL && gctx->iv_set) {
409
364
  iv = gctx->iv;
@@ -427,7 +382,7 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
427
382
  }
428
383
 
429
384
  static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) {
430
- EVP_AES_GCM_CTX *gctx = c->cipher_data;
385
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(c);
431
386
  OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
432
387
  if (gctx->iv != c->iv) {
433
388
  OPENSSL_free(gctx->iv);
@@ -451,7 +406,7 @@ static void ctr64_inc(uint8_t *counter) {
451
406
  }
452
407
 
453
408
  static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
454
- EVP_AES_GCM_CTX *gctx = c->cipher_data;
409
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(c);
455
410
  switch (type) {
456
411
  case EVP_CTRL_INIT:
457
412
  gctx->key_set = 0;
@@ -462,7 +417,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
462
417
  gctx->iv_gen = 0;
463
418
  return 1;
464
419
 
465
- case EVP_CTRL_GCM_SET_IVLEN:
420
+ case EVP_CTRL_AEAD_SET_IVLEN:
466
421
  if (arg <= 0) {
467
422
  return 0;
468
423
  }
@@ -480,7 +435,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
480
435
  gctx->ivlen = arg;
481
436
  return 1;
482
437
 
483
- case EVP_CTRL_GCM_SET_TAG:
438
+ case EVP_CTRL_AEAD_SET_TAG:
484
439
  if (arg <= 0 || arg > 16 || c->encrypt) {
485
440
  return 0;
486
441
  }
@@ -488,14 +443,14 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
488
443
  gctx->taglen = arg;
489
444
  return 1;
490
445
 
491
- case EVP_CTRL_GCM_GET_TAG:
446
+ case EVP_CTRL_AEAD_GET_TAG:
492
447
  if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) {
493
448
  return 0;
494
449
  }
495
450
  OPENSSL_memcpy(ptr, c->buf, arg);
496
451
  return 1;
497
452
 
498
- case EVP_CTRL_GCM_SET_IV_FIXED:
453
+ case EVP_CTRL_AEAD_SET_IV_FIXED:
499
454
  // Special case: -1 length restores whole IV
500
455
  if (arg == -1) {
501
456
  OPENSSL_memcpy(gctx->iv, ptr, gctx->ivlen);
@@ -543,7 +498,10 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
543
498
 
544
499
  case EVP_CTRL_COPY: {
545
500
  EVP_CIPHER_CTX *out = ptr;
546
- EVP_AES_GCM_CTX *gctx_out = out->cipher_data;
501
+ EVP_AES_GCM_CTX *gctx_out = aes_gcm_from_cipher_ctx(out);
502
+ // |EVP_CIPHER_CTX_copy| copies this generically, but we must redo it in
503
+ // case |out->cipher_data| and |in->cipher_data| are differently aligned.
504
+ OPENSSL_memcpy(gctx_out, gctx, sizeof(EVP_AES_GCM_CTX));
547
505
  if (gctx->iv == c->iv) {
548
506
  gctx_out->iv = out->iv;
549
507
  } else {
@@ -563,7 +521,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
563
521
 
564
522
  static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
565
523
  size_t len) {
566
- EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
524
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(ctx);
567
525
 
568
526
  // If not set up, return error
569
527
  if (!gctx->key_set) {
@@ -677,8 +635,8 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_gcm_generic) {
677
635
  out->block_size = 1;
678
636
  out->key_len = 16;
679
637
  out->iv_len = 12;
680
- out->ctx_size = sizeof(EVP_AES_GCM_CTX);
681
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV |
638
+ out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
639
+ out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
682
640
  EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
683
641
  EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
684
642
  out->init = aes_gcm_init_key;
@@ -725,6 +683,19 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ecb_generic) {
725
683
  out->cipher = aes_ecb_cipher;
726
684
  }
727
685
 
686
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ofb_generic) {
687
+ memset(out, 0, sizeof(EVP_CIPHER));
688
+
689
+ out->nid = NID_aes_192_ofb128;
690
+ out->block_size = 1;
691
+ out->key_len = 24;
692
+ out->iv_len = 16;
693
+ out->ctx_size = sizeof(EVP_AES_KEY);
694
+ out->flags = EVP_CIPH_OFB_MODE;
695
+ out->init = aes_init_key;
696
+ out->cipher = aes_ofb_cipher;
697
+ }
698
+
728
699
  DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_gcm_generic) {
729
700
  memset(out, 0, sizeof(EVP_CIPHER));
730
701
 
@@ -732,8 +703,8 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_gcm_generic) {
732
703
  out->block_size = 1;
733
704
  out->key_len = 24;
734
705
  out->iv_len = 12;
735
- out->ctx_size = sizeof(EVP_AES_GCM_CTX);
736
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV |
706
+ out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
707
+ out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
737
708
  EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
738
709
  EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
739
710
  out->init = aes_gcm_init_key;
@@ -800,8 +771,8 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_gcm_generic) {
800
771
  out->block_size = 1;
801
772
  out->key_len = 32;
802
773
  out->iv_len = 12;
803
- out->ctx_size = sizeof(EVP_AES_GCM_CTX);
804
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV |
774
+ out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
775
+ out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
805
776
  EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
806
777
  EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
807
778
  out->init = aes_gcm_init_key;
@@ -810,126 +781,22 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_gcm_generic) {
810
781
  out->ctrl = aes_gcm_ctrl;
811
782
  }
812
783
 
813
- #if !defined(OPENSSL_NO_ASM) && \
814
- (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
815
-
816
- // AES-NI section.
784
+ #if defined(HWAES_ECB)
817
785
 
818
- static char aesni_capable(void) {
819
- return (OPENSSL_ia32cap_P[1] & (1 << (57 - 32))) != 0;
820
- }
821
-
822
- static int aesni_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
823
- const uint8_t *iv, int enc) {
824
- int ret, mode;
825
- EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
826
-
827
- mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
828
- if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
829
- ret = aesni_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
830
- dat->block = (block128_f)aesni_decrypt;
831
- dat->stream.cbc =
832
- mode == EVP_CIPH_CBC_MODE ? (cbc128_f)aesni_cbc_encrypt : NULL;
833
- } else {
834
- ret = aesni_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
835
- dat->block = (block128_f)aesni_encrypt;
836
- if (mode == EVP_CIPH_CBC_MODE) {
837
- dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
838
- } else if (mode == EVP_CIPH_CTR_MODE) {
839
- dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
840
- } else {
841
- dat->stream.cbc = NULL;
842
- }
843
- }
844
-
845
- if (ret < 0) {
846
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
847
- return 0;
848
- }
849
-
850
- return 1;
851
- }
852
-
853
- static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
854
- const uint8_t *in, size_t len) {
855
- aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, ctx->encrypt);
856
-
857
- return 1;
858
- }
859
-
860
- static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
861
- const uint8_t *in, size_t len) {
786
+ static int aes_hw_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
787
+ const uint8_t *in, size_t len) {
862
788
  size_t bl = ctx->cipher->block_size;
863
789
 
864
790
  if (len < bl) {
865
791
  return 1;
866
792
  }
867
793
 
868
- aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt);
794
+ aes_hw_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt);
869
795
 
870
796
  return 1;
871
797
  }
872
798
 
873
- static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
874
- const uint8_t *iv, int enc) {
875
- EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
876
- if (!iv && !key) {
877
- return 1;
878
- }
879
- if (key) {
880
- aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
881
- CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)aesni_encrypt, 1);
882
- gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
883
- // If we have an iv can set it directly, otherwise use
884
- // saved IV.
885
- if (iv == NULL && gctx->iv_set) {
886
- iv = gctx->iv;
887
- }
888
- if (iv) {
889
- CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
890
- gctx->iv_set = 1;
891
- }
892
- gctx->key_set = 1;
893
- } else {
894
- // If key set use IV, otherwise copy
895
- if (gctx->key_set) {
896
- CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
897
- } else {
898
- OPENSSL_memcpy(gctx->iv, iv, gctx->ivlen);
899
- }
900
- gctx->iv_set = 1;
901
- gctx->iv_gen = 0;
902
- }
903
- return 1;
904
- }
905
-
906
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_128_cbc) {
907
- memset(out, 0, sizeof(EVP_CIPHER));
908
-
909
- out->nid = NID_aes_128_cbc;
910
- out->block_size = 16;
911
- out->key_len = 16;
912
- out->iv_len = 16;
913
- out->ctx_size = sizeof(EVP_AES_KEY);
914
- out->flags = EVP_CIPH_CBC_MODE;
915
- out->init = aesni_init_key;
916
- out->cipher = aesni_cbc_cipher;
917
- }
918
-
919
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_128_ctr) {
920
- memset(out, 0, sizeof(EVP_CIPHER));
921
-
922
- out->nid = NID_aes_128_ctr;
923
- out->block_size = 1;
924
- out->key_len = 16;
925
- out->iv_len = 16;
926
- out->ctx_size = sizeof(EVP_AES_KEY);
927
- out->flags = EVP_CIPH_CTR_MODE;
928
- out->init = aesni_init_key;
929
- out->cipher = aes_ctr_cipher;
930
- }
931
-
932
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_128_ecb) {
799
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_128_ecb) {
933
800
  memset(out, 0, sizeof(EVP_CIPHER));
934
801
 
935
802
  out->nid = NID_aes_128_ecb;
@@ -937,67 +804,11 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_128_ecb) {
937
804
  out->key_len = 16;
938
805
  out->ctx_size = sizeof(EVP_AES_KEY);
939
806
  out->flags = EVP_CIPH_ECB_MODE;
940
- out->init = aesni_init_key;
941
- out->cipher = aesni_ecb_cipher;
942
- }
943
-
944
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_128_ofb) {
945
- memset(out, 0, sizeof(EVP_CIPHER));
946
-
947
- out->nid = NID_aes_128_ofb128;
948
- out->block_size = 1;
949
- out->key_len = 16;
950
- out->iv_len = 16;
951
- out->ctx_size = sizeof(EVP_AES_KEY);
952
- out->flags = EVP_CIPH_OFB_MODE;
953
- out->init = aesni_init_key;
954
- out->cipher = aes_ofb_cipher;
955
- }
956
-
957
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_128_gcm) {
958
- memset(out, 0, sizeof(EVP_CIPHER));
959
-
960
- out->nid = NID_aes_128_gcm;
961
- out->block_size = 1;
962
- out->key_len = 16;
963
- out->iv_len = 12;
964
- out->ctx_size = sizeof(EVP_AES_GCM_CTX);
965
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV |
966
- EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
967
- EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
968
- out->init = aesni_gcm_init_key;
969
- out->cipher = aes_gcm_cipher;
970
- out->cleanup = aes_gcm_cleanup;
971
- out->ctrl = aes_gcm_ctrl;
972
- }
973
-
974
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_192_cbc) {
975
- memset(out, 0, sizeof(EVP_CIPHER));
976
-
977
- out->nid = NID_aes_192_cbc;
978
- out->block_size = 16;
979
- out->key_len = 24;
980
- out->iv_len = 16;
981
- out->ctx_size = sizeof(EVP_AES_KEY);
982
- out->flags = EVP_CIPH_CBC_MODE;
983
- out->init = aesni_init_key;
984
- out->cipher = aesni_cbc_cipher;
985
- }
986
-
987
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_192_ctr) {
988
- memset(out, 0, sizeof(EVP_CIPHER));
989
-
990
- out->nid = NID_aes_192_ctr;
991
- out->block_size = 1;
992
- out->key_len = 24;
993
- out->iv_len = 16;
994
- out->ctx_size = sizeof(EVP_AES_KEY);
995
- out->flags = EVP_CIPH_CTR_MODE;
996
- out->init = aesni_init_key;
997
- out->cipher = aes_ctr_cipher;
807
+ out->init = aes_init_key;
808
+ out->cipher = aes_hw_ecb_cipher;
998
809
  }
999
810
 
1000
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_192_ecb) {
811
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_192_ecb) {
1001
812
  memset(out, 0, sizeof(EVP_CIPHER));
1002
813
 
1003
814
  out->nid = NID_aes_192_ecb;
@@ -1005,54 +816,11 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_192_ecb) {
1005
816
  out->key_len = 24;
1006
817
  out->ctx_size = sizeof(EVP_AES_KEY);
1007
818
  out->flags = EVP_CIPH_ECB_MODE;
1008
- out->init = aesni_init_key;
1009
- out->cipher = aesni_ecb_cipher;
1010
- }
1011
-
1012
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_192_gcm) {
1013
- memset(out, 0, sizeof(EVP_CIPHER));
1014
-
1015
- out->nid = NID_aes_192_gcm;
1016
- out->block_size = 1;
1017
- out->key_len = 24;
1018
- out->iv_len = 12;
1019
- out->ctx_size = sizeof(EVP_AES_GCM_CTX);
1020
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV |
1021
- EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
1022
- EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
1023
- out->init = aesni_gcm_init_key;
1024
- out->cipher = aes_gcm_cipher;
1025
- out->cleanup = aes_gcm_cleanup;
1026
- out->ctrl = aes_gcm_ctrl;
1027
- }
1028
-
1029
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_256_cbc) {
1030
- memset(out, 0, sizeof(EVP_CIPHER));
1031
-
1032
- out->nid = NID_aes_256_cbc;
1033
- out->block_size = 16;
1034
- out->key_len = 32;
1035
- out->iv_len = 16;
1036
- out->ctx_size = sizeof(EVP_AES_KEY);
1037
- out->flags = EVP_CIPH_CBC_MODE;
1038
- out->init = aesni_init_key;
1039
- out->cipher = aesni_cbc_cipher;
1040
- }
1041
-
1042
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_256_ctr) {
1043
- memset(out, 0, sizeof(EVP_CIPHER));
1044
-
1045
- out->nid = NID_aes_256_ctr;
1046
- out->block_size = 1;
1047
- out->key_len = 32;
1048
- out->iv_len = 16;
1049
- out->ctx_size = sizeof(EVP_AES_KEY);
1050
- out->flags = EVP_CIPH_CTR_MODE;
1051
- out->init = aesni_init_key;
1052
- out->cipher = aes_ctr_cipher;
819
+ out->init = aes_init_key;
820
+ out->cipher = aes_hw_ecb_cipher;
1053
821
  }
1054
822
 
1055
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_256_ecb) {
823
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_256_ecb) {
1056
824
  memset(out, 0, sizeof(EVP_CIPHER));
1057
825
 
1058
826
  out->nid = NID_aes_256_ecb;
@@ -1060,80 +828,51 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_256_ecb) {
1060
828
  out->key_len = 32;
1061
829
  out->ctx_size = sizeof(EVP_AES_KEY);
1062
830
  out->flags = EVP_CIPH_ECB_MODE;
1063
- out->init = aesni_init_key;
1064
- out->cipher = aesni_ecb_cipher;
1065
- }
1066
-
1067
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_256_ofb) {
1068
- memset(out, 0, sizeof(EVP_CIPHER));
1069
-
1070
- out->nid = NID_aes_256_ofb128;
1071
- out->block_size = 1;
1072
- out->key_len = 32;
1073
- out->iv_len = 16;
1074
- out->ctx_size = sizeof(EVP_AES_KEY);
1075
- out->flags = EVP_CIPH_OFB_MODE;
1076
- out->init = aesni_init_key;
1077
- out->cipher = aes_ofb_cipher;
831
+ out->init = aes_init_key;
832
+ out->cipher = aes_hw_ecb_cipher;
1078
833
  }
1079
834
 
1080
- DEFINE_LOCAL_DATA(EVP_CIPHER, aesni_256_gcm) {
1081
- memset(out, 0, sizeof(EVP_CIPHER));
835
+ #define EVP_ECB_CIPHER_FUNCTION(keybits) \
836
+ const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
837
+ if (hwaes_capable()) { \
838
+ return aes_hw_##keybits##_ecb(); \
839
+ } \
840
+ return aes_##keybits##_ecb_generic(); \
841
+ }
1082
842
 
1083
- out->nid = NID_aes_256_gcm;
1084
- out->block_size = 1;
1085
- out->key_len = 32;
1086
- out->iv_len = 12;
1087
- out->ctx_size = sizeof(EVP_AES_GCM_CTX);
1088
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV |
1089
- EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
1090
- EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY |
1091
- EVP_CIPH_FLAG_AEAD_CIPHER;
1092
- out->init = aesni_gcm_init_key;
1093
- out->cipher = aes_gcm_cipher;
1094
- out->cleanup = aes_gcm_cleanup;
1095
- out->ctrl = aes_gcm_ctrl;
1096
- }
843
+ #else
1097
844
 
1098
- #define EVP_CIPHER_FUNCTION(keybits, mode) \
1099
- const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \
1100
- if (aesni_capable()) { \
1101
- return aesni_##keybits##_##mode(); \
1102
- } else { \
1103
- return aes_##keybits##_##mode##_generic(); \
1104
- } \
845
+ #define EVP_ECB_CIPHER_FUNCTION(keybits) \
846
+ const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
847
+ return aes_##keybits##_ecb_generic(); \
1105
848
  }
1106
849
 
1107
- #else // ^^^ OPENSSL_X86_64 || OPENSSL_X86
1108
-
1109
- static char aesni_capable(void) {
1110
- return 0;
1111
- }
850
+ #endif // HWAES_ECB
1112
851
 
1113
852
  #define EVP_CIPHER_FUNCTION(keybits, mode) \
1114
853
  const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \
1115
854
  return aes_##keybits##_##mode##_generic(); \
1116
855
  }
1117
856
 
1118
- #endif
1119
-
1120
857
  EVP_CIPHER_FUNCTION(128, cbc)
1121
858
  EVP_CIPHER_FUNCTION(128, ctr)
1122
- EVP_CIPHER_FUNCTION(128, ecb)
1123
859
  EVP_CIPHER_FUNCTION(128, ofb)
1124
860
  EVP_CIPHER_FUNCTION(128, gcm)
1125
861
 
1126
862
  EVP_CIPHER_FUNCTION(192, cbc)
1127
863
  EVP_CIPHER_FUNCTION(192, ctr)
1128
- EVP_CIPHER_FUNCTION(192, ecb)
864
+ EVP_CIPHER_FUNCTION(192, ofb)
1129
865
  EVP_CIPHER_FUNCTION(192, gcm)
1130
866
 
1131
867
  EVP_CIPHER_FUNCTION(256, cbc)
1132
868
  EVP_CIPHER_FUNCTION(256, ctr)
1133
- EVP_CIPHER_FUNCTION(256, ecb)
1134
869
  EVP_CIPHER_FUNCTION(256, ofb)
1135
870
  EVP_CIPHER_FUNCTION(256, gcm)
1136
871
 
872
+ EVP_ECB_CIPHER_FUNCTION(128)
873
+ EVP_ECB_CIPHER_FUNCTION(192)
874
+ EVP_ECB_CIPHER_FUNCTION(256)
875
+
1137
876
 
1138
877
  #define EVP_AEAD_AES_GCM_TAG_LEN 16
1139
878
 
@@ -1142,21 +881,16 @@ struct aead_aes_gcm_ctx {
1142
881
  double align;
1143
882
  AES_KEY ks;
1144
883
  } ks;
1145
- GCM128_CONTEXT gcm;
884
+ GCM128_KEY gcm_key;
1146
885
  ctr128_f ctr;
1147
886
  };
1148
887
 
1149
- struct aead_aes_gcm_tls12_ctx {
1150
- struct aead_aes_gcm_ctx gcm_ctx;
1151
- uint64_t min_next_nonce;
1152
- };
1153
-
1154
888
  static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
1155
889
  size_t *out_tag_len, const uint8_t *key,
1156
890
  size_t key_len, size_t tag_len) {
1157
891
  const size_t key_bits = key_len * 8;
1158
892
 
1159
- if (key_bits != 128 && key_bits != 256) {
893
+ if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
1160
894
  OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
1161
895
  return 0; // EVP_AEAD_CTX_init should catch this.
1162
896
  }
@@ -1171,34 +905,35 @@ static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
1171
905
  }
1172
906
 
1173
907
  gcm_ctx->ctr =
1174
- aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, NULL, key, key_len);
908
+ aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm_key, NULL, key, key_len);
1175
909
  *out_tag_len = tag_len;
1176
910
  return 1;
1177
911
  }
1178
912
 
913
+ OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
914
+ sizeof(struct aead_aes_gcm_ctx),
915
+ "AEAD state is too small");
916
+ #if defined(__GNUC__) || defined(__clang__)
917
+ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
918
+ alignof(struct aead_aes_gcm_ctx),
919
+ "AEAD state has insufficient alignment");
920
+ #endif
921
+
1179
922
  static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1180
923
  size_t key_len, size_t requested_tag_len) {
1181
- struct aead_aes_gcm_ctx *gcm_ctx;
1182
- gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx));
1183
- if (gcm_ctx == NULL) {
1184
- return 0;
1185
- }
924
+ struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
1186
925
 
1187
926
  size_t actual_tag_len;
1188
927
  if (!aead_aes_gcm_init_impl(gcm_ctx, &actual_tag_len, key, key_len,
1189
928
  requested_tag_len)) {
1190
- OPENSSL_free(gcm_ctx);
1191
929
  return 0;
1192
930
  }
1193
931
 
1194
- ctx->aead_state = gcm_ctx;
1195
932
  ctx->tag_len = actual_tag_len;
1196
933
  return 1;
1197
934
  }
1198
935
 
1199
- static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {
1200
- OPENSSL_free(ctx->aead_state);
1201
- }
936
+ static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {}
1202
937
 
1203
938
  static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
1204
939
  uint8_t *out_tag, size_t *out_tag_len,
@@ -1208,8 +943,7 @@ static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
1208
943
  const uint8_t *extra_in,
1209
944
  size_t extra_in_len,
1210
945
  const uint8_t *ad, size_t ad_len) {
1211
- const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
1212
- GCM128_CONTEXT gcm;
946
+ struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
1213
947
 
1214
948
  if (extra_in_len + ctx->tag_len < ctx->tag_len) {
1215
949
  OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
@@ -1226,7 +960,9 @@ static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
1226
960
 
1227
961
  const AES_KEY *key = &gcm_ctx->ks.ks;
1228
962
 
1229
- OPENSSL_memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
963
+ GCM128_CONTEXT gcm;
964
+ OPENSSL_memset(&gcm, 0, sizeof(gcm));
965
+ OPENSSL_memcpy(&gcm.gcm_key, &gcm_ctx->gcm_key, sizeof(gcm.gcm_key));
1230
966
  CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
1231
967
 
1232
968
  if (ad_len > 0 && !CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
@@ -1268,9 +1004,8 @@ static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
1268
1004
  const uint8_t *in, size_t in_len,
1269
1005
  const uint8_t *in_tag, size_t in_tag_len,
1270
1006
  const uint8_t *ad, size_t ad_len) {
1271
- const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
1007
+ struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
1272
1008
  uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
1273
- GCM128_CONTEXT gcm;
1274
1009
 
1275
1010
  if (nonce_len == 0) {
1276
1011
  OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
@@ -1284,7 +1019,9 @@ static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
1284
1019
 
1285
1020
  const AES_KEY *key = &gcm_ctx->ks.ks;
1286
1021
 
1287
- OPENSSL_memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
1022
+ GCM128_CONTEXT gcm;
1023
+ OPENSSL_memset(&gcm, 0, sizeof(gcm));
1024
+ OPENSSL_memcpy(&gcm.gcm_key, &gcm_ctx->gcm_key, sizeof(gcm.gcm_key));
1288
1025
  CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
1289
1026
 
1290
1027
  if (!CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
@@ -1326,6 +1063,21 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm) {
1326
1063
  out->open_gather = aead_aes_gcm_open_gather;
1327
1064
  }
1328
1065
 
1066
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_192_gcm) {
1067
+ memset(out, 0, sizeof(EVP_AEAD));
1068
+
1069
+ out->key_len = 24;
1070
+ out->nonce_len = 12;
1071
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1072
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1073
+ out->seal_scatter_supports_extra_in = 1;
1074
+
1075
+ out->init = aead_aes_gcm_init;
1076
+ out->cleanup = aead_aes_gcm_cleanup;
1077
+ out->seal_scatter = aead_aes_gcm_seal_scatter;
1078
+ out->open_gather = aead_aes_gcm_open_gather;
1079
+ }
1080
+
1329
1081
  DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
1330
1082
  memset(out, 0, sizeof(EVP_AEAD));
1331
1083
 
@@ -1341,38 +1093,45 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
1341
1093
  out->open_gather = aead_aes_gcm_open_gather;
1342
1094
  }
1343
1095
 
1096
+ struct aead_aes_gcm_tls12_ctx {
1097
+ struct aead_aes_gcm_ctx gcm_ctx;
1098
+ uint64_t min_next_nonce;
1099
+ };
1100
+
1101
+ OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1102
+ sizeof(struct aead_aes_gcm_tls12_ctx),
1103
+ "AEAD state is too small");
1104
+ #if defined(__GNUC__) || defined(__clang__)
1105
+ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
1106
+ alignof(struct aead_aes_gcm_tls12_ctx),
1107
+ "AEAD state has insufficient alignment");
1108
+ #endif
1109
+
1344
1110
  static int aead_aes_gcm_tls12_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1345
1111
  size_t key_len, size_t requested_tag_len) {
1346
- struct aead_aes_gcm_tls12_ctx *gcm_ctx;
1347
- gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_tls12_ctx));
1348
- if (gcm_ctx == NULL) {
1349
- return 0;
1350
- }
1112
+ struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1113
+ (struct aead_aes_gcm_tls12_ctx *) &ctx->state;
1351
1114
 
1352
1115
  gcm_ctx->min_next_nonce = 0;
1353
1116
 
1354
1117
  size_t actual_tag_len;
1355
1118
  if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1356
1119
  requested_tag_len)) {
1357
- OPENSSL_free(gcm_ctx);
1358
1120
  return 0;
1359
1121
  }
1360
1122
 
1361
- ctx->aead_state = gcm_ctx;
1362
1123
  ctx->tag_len = actual_tag_len;
1363
1124
  return 1;
1364
1125
  }
1365
1126
 
1366
- static void aead_aes_gcm_tls12_cleanup(EVP_AEAD_CTX *ctx) {
1367
- OPENSSL_free(ctx->aead_state);
1368
- }
1369
-
1370
1127
  static int aead_aes_gcm_tls12_seal_scatter(
1371
1128
  const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1372
1129
  size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1373
1130
  size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1374
1131
  size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1375
- struct aead_aes_gcm_tls12_ctx *gcm_ctx = ctx->aead_state;
1132
+ struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1133
+ (struct aead_aes_gcm_tls12_ctx *) &ctx->state;
1134
+
1376
1135
  if (nonce_len != 12) {
1377
1136
  OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1378
1137
  return 0;
@@ -1406,7 +1165,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls12) {
1406
1165
  out->seal_scatter_supports_extra_in = 1;
1407
1166
 
1408
1167
  out->init = aead_aes_gcm_tls12_init;
1409
- out->cleanup = aead_aes_gcm_tls12_cleanup;
1168
+ out->cleanup = aead_aes_gcm_cleanup;
1410
1169
  out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1411
1170
  out->open_gather = aead_aes_gcm_open_gather;
1412
1171
  }
@@ -1421,17 +1180,125 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls12) {
1421
1180
  out->seal_scatter_supports_extra_in = 1;
1422
1181
 
1423
1182
  out->init = aead_aes_gcm_tls12_init;
1424
- out->cleanup = aead_aes_gcm_tls12_cleanup;
1183
+ out->cleanup = aead_aes_gcm_cleanup;
1425
1184
  out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1426
1185
  out->open_gather = aead_aes_gcm_open_gather;
1427
1186
  }
1428
1187
 
1188
+ struct aead_aes_gcm_tls13_ctx {
1189
+ struct aead_aes_gcm_ctx gcm_ctx;
1190
+ uint64_t min_next_nonce;
1191
+ uint64_t mask;
1192
+ uint8_t first;
1193
+ };
1194
+
1195
+ OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1196
+ sizeof(struct aead_aes_gcm_tls13_ctx),
1197
+ "AEAD state is too small");
1198
+ #if defined(__GNUC__) || defined(__clang__)
1199
+ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
1200
+ alignof(struct aead_aes_gcm_tls13_ctx),
1201
+ "AEAD state has insufficient alignment");
1202
+ #endif
1203
+
1204
+ static int aead_aes_gcm_tls13_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1205
+ size_t key_len, size_t requested_tag_len) {
1206
+ struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1207
+ (struct aead_aes_gcm_tls13_ctx *) &ctx->state;
1208
+
1209
+ gcm_ctx->min_next_nonce = 0;
1210
+ gcm_ctx->first = 1;
1211
+
1212
+ size_t actual_tag_len;
1213
+ if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1214
+ requested_tag_len)) {
1215
+ return 0;
1216
+ }
1217
+
1218
+ ctx->tag_len = actual_tag_len;
1219
+ return 1;
1220
+ }
1221
+
1222
+ static int aead_aes_gcm_tls13_seal_scatter(
1223
+ const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1224
+ size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1225
+ size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1226
+ size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1227
+ struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1228
+ (struct aead_aes_gcm_tls13_ctx *) &ctx->state;
1229
+
1230
+ if (nonce_len != 12) {
1231
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1232
+ return 0;
1233
+ }
1234
+
1235
+ // The given nonces must be strictly monotonically increasing. See
1236
+ // https://tools.ietf.org/html/rfc8446#section-5.3 for details of the TLS 1.3
1237
+ // nonce construction.
1238
+ uint64_t given_counter;
1239
+ OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
1240
+ sizeof(given_counter));
1241
+ given_counter = CRYPTO_bswap8(given_counter);
1242
+
1243
+ if (gcm_ctx->first) {
1244
+ // In the first call the sequence number will be zero and therefore the
1245
+ // given nonce will be 0 ^ mask = mask.
1246
+ gcm_ctx->mask = given_counter;
1247
+ gcm_ctx->first = 0;
1248
+ }
1249
+ given_counter ^= gcm_ctx->mask;
1250
+
1251
+ if (given_counter == UINT64_MAX ||
1252
+ given_counter < gcm_ctx->min_next_nonce) {
1253
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1254
+ return 0;
1255
+ }
1256
+
1257
+ gcm_ctx->min_next_nonce = given_counter + 1;
1258
+
1259
+ return aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1260
+ max_out_tag_len, nonce, nonce_len, in,
1261
+ in_len, extra_in, extra_in_len, ad, ad_len);
1262
+ }
1263
+
1264
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls13) {
1265
+ memset(out, 0, sizeof(EVP_AEAD));
1266
+
1267
+ out->key_len = 16;
1268
+ out->nonce_len = 12;
1269
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1270
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1271
+ out->seal_scatter_supports_extra_in = 1;
1272
+
1273
+ out->init = aead_aes_gcm_tls13_init;
1274
+ out->cleanup = aead_aes_gcm_cleanup;
1275
+ out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1276
+ out->open_gather = aead_aes_gcm_open_gather;
1277
+ }
1278
+
1279
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls13) {
1280
+ memset(out, 0, sizeof(EVP_AEAD));
1281
+
1282
+ out->key_len = 32;
1283
+ out->nonce_len = 12;
1284
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1285
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1286
+ out->seal_scatter_supports_extra_in = 1;
1287
+
1288
+ out->init = aead_aes_gcm_tls13_init;
1289
+ out->cleanup = aead_aes_gcm_cleanup;
1290
+ out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1291
+ out->open_gather = aead_aes_gcm_open_gather;
1292
+ }
1293
+
1429
1294
  int EVP_has_aes_hardware(void) {
1430
1295
  #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
1431
- return aesni_capable() && crypto_gcm_clmul_enabled();
1296
+ return hwaes_capable() && crypto_gcm_clmul_enabled();
1432
1297
  #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
1433
1298
  return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable();
1434
1299
  #else
1435
1300
  return 0;
1436
1301
  #endif
1437
1302
  }
1303
+
1304
+ OPENSSL_MSVC_PRAGMA(warning(pop))