@fails-components/webtransport 0.0.1-rc.0 → 0.0.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (384) hide show
  1. package/.github/workflows/npm-publish-packages.yml +0 -0
  2. package/CMakeLists.txt +2 -0
  3. package/package.json +1 -1
  4. package/readme.md +0 -0
  5. package/src/http3serversession.h +1 -1
  6. package/src/webtransport.js +4 -2
  7. package/third_party/boringssl/src/CMakeLists.txt +9 -12
  8. package/third_party/boringssl/src/crypto/CMakeLists.txt +2 -3
  9. package/third_party/boringssl/src/crypto/asn1/time_support.c +206 -206
  10. package/third_party/boringssl/src/crypto/bio/socket_helper.c +124 -122
  11. package/third_party/boringssl/src/crypto/evp/digestsign.c +268 -231
  12. package/third_party/boringssl/src/crypto/evp/p_ec.c +286 -286
  13. package/third_party/boringssl/src/crypto/fipsmodule/aes/key_wrap.c +246 -236
  14. package/third_party/boringssl/src/crypto/fipsmodule/aes/mode_wrappers.c +124 -122
  15. package/third_party/boringssl/src/crypto/fipsmodule/bcm.c +3 -0
  16. package/third_party/boringssl/src/crypto/fipsmodule/bn/bytes.c +246 -230
  17. package/third_party/boringssl/src/crypto/fipsmodule/bn/internal.h +734 -715
  18. package/third_party/boringssl/src/crypto/fipsmodule/bn/random.c +349 -341
  19. package/third_party/boringssl/src/crypto/fipsmodule/cipher/cipher.c +670 -648
  20. package/third_party/boringssl/src/crypto/fipsmodule/cipher/e_aes.c +1504 -1472
  21. package/third_party/boringssl/src/crypto/{cipher_extra → fipsmodule/cipher}/e_aesccm.c +435 -446
  22. package/third_party/boringssl/src/crypto/{cmac → fipsmodule/cmac}/cavp_3des_cmac_tests.txt +0 -0
  23. package/third_party/boringssl/src/crypto/{cmac → fipsmodule/cmac}/cavp_aes128_cmac_tests.txt +0 -0
  24. package/third_party/boringssl/src/crypto/{cmac → fipsmodule/cmac}/cavp_aes192_cmac_tests.txt +0 -0
  25. package/third_party/boringssl/src/crypto/{cmac → fipsmodule/cmac}/cavp_aes256_cmac_tests.txt +0 -0
  26. package/third_party/boringssl/src/crypto/{cmac → fipsmodule/cmac}/cmac.c +318 -278
  27. package/third_party/boringssl/src/crypto/{cmac → fipsmodule/cmac}/cmac_test.cc +268 -267
  28. package/third_party/boringssl/src/crypto/fipsmodule/dh/dh.c +5 -1
  29. package/third_party/boringssl/src/crypto/fipsmodule/ec/ec.c +11 -17
  30. package/third_party/boringssl/src/crypto/fipsmodule/ec/ec_key.c +15 -4
  31. package/third_party/boringssl/src/crypto/fipsmodule/ec/internal.h +2 -6
  32. package/third_party/boringssl/src/crypto/fipsmodule/ec/p224-64.c +1164 -1180
  33. package/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz.c +3 -3
  34. package/third_party/boringssl/src/crypto/fipsmodule/ec/p256-nistz_test.cc +2 -2
  35. package/third_party/boringssl/src/crypto/fipsmodule/ec/p256.c +44 -33
  36. package/third_party/boringssl/src/crypto/fipsmodule/ec/scalar.c +169 -175
  37. package/third_party/boringssl/src/crypto/fipsmodule/ec/simple.c +352 -357
  38. package/third_party/boringssl/src/crypto/fipsmodule/ecdh/ecdh.c +4 -0
  39. package/third_party/boringssl/src/crypto/fipsmodule/ecdsa/ecdsa.c +14 -9
  40. package/third_party/boringssl/src/crypto/fipsmodule/hmac/hmac.c +256 -235
  41. package/third_party/boringssl/src/crypto/fipsmodule/rand/ctrdrbg.c +204 -202
  42. package/third_party/boringssl/src/crypto/fipsmodule/rsa/padding.c +702 -695
  43. package/third_party/boringssl/src/crypto/fipsmodule/rsa/rsa_impl.c +7 -2
  44. package/third_party/boringssl/src/crypto/fipsmodule/self_check/self_check.c +7 -0
  45. package/third_party/boringssl/src/crypto/fipsmodule/service_indicator/internal.h +89 -0
  46. package/third_party/boringssl/src/crypto/fipsmodule/service_indicator/service_indicator.c +333 -0
  47. package/third_party/boringssl/src/crypto/fipsmodule/service_indicator/service_indicator_test.cc +2410 -0
  48. package/third_party/boringssl/src/crypto/fipsmodule/sha/sha1.c +359 -357
  49. package/third_party/boringssl/src/crypto/fipsmodule/sha/sha256.c +325 -321
  50. package/third_party/boringssl/src/crypto/fipsmodule/sha/sha512.c +510 -508
  51. package/third_party/boringssl/src/crypto/fipsmodule/tls/kdf.c +177 -165
  52. package/third_party/boringssl/src/crypto/internal.h +13 -0
  53. package/third_party/boringssl/src/crypto/trust_token/trust_token_test.cc +884 -884
  54. package/third_party/boringssl/src/crypto/x509/internal.h +0 -2
  55. package/third_party/boringssl/src/crypto/x509/t_crl.c +148 -148
  56. package/third_party/boringssl/src/crypto/x509/t_req.c +249 -250
  57. package/third_party/boringssl/src/crypto/x509/t_x509.c +2 -3
  58. package/third_party/boringssl/src/crypto/x509/x509_req.c +262 -304
  59. package/third_party/boringssl/src/crypto/x509/x509_set.c +250 -240
  60. package/third_party/boringssl/src/crypto/x509/x509_test.cc +466 -4
  61. package/third_party/boringssl/src/crypto/x509/x509cset.c +292 -279
  62. package/third_party/boringssl/src/crypto/x509/x509rset.c +89 -86
  63. package/third_party/boringssl/src/crypto/x509/x_crl.c +37 -125
  64. package/third_party/boringssl/src/crypto/x509/x_req.c +114 -109
  65. package/third_party/boringssl/src/crypto/x509/x_x509.c +394 -394
  66. package/third_party/boringssl/src/crypto/x509v3/v3_akey.c +11 -16
  67. package/third_party/boringssl/src/crypto/x509v3/v3_alt.c +644 -642
  68. package/third_party/boringssl/src/crypto/x509v3/v3_bcons.c +130 -133
  69. package/third_party/boringssl/src/crypto/x509v3/v3_bitst.c +149 -144
  70. package/third_party/boringssl/src/crypto/x509v3/v3_cpols.c +496 -500
  71. package/third_party/boringssl/src/crypto/x509v3/v3_enum.c +105 -106
  72. package/third_party/boringssl/src/crypto/x509v3/v3_ia5.c +126 -121
  73. package/third_party/boringssl/src/crypto/x509v3/v3_info.c +206 -218
  74. package/third_party/boringssl/src/crypto/x509v3/v3_int.c +97 -91
  75. package/third_party/boringssl/src/crypto/x509v3/v3_pci.c +290 -289
  76. package/third_party/boringssl/src/crypto/x509v3/v3_skey.c +161 -156
  77. package/third_party/boringssl/src/crypto/x509v3/v3_utl.c +5 -3
  78. package/third_party/boringssl/src/include/openssl/aead.h +480 -480
  79. package/third_party/boringssl/src/include/openssl/asn1.h +2 -0
  80. package/third_party/boringssl/src/include/openssl/base.h +1 -1
  81. package/third_party/boringssl/src/include/openssl/service_indicator.h +96 -0
  82. package/third_party/boringssl/src/include/openssl/ssl.h +7 -0
  83. package/third_party/boringssl/src/include/openssl/x509.h +11 -33
  84. package/third_party/boringssl/src/include/openssl/x509v3.h +990 -1018
  85. package/third_party/boringssl/src/sources.cmake +4 -4
  86. package/third_party/boringssl/src/ssl/internal.h +10 -0
  87. package/third_party/boringssl/src/ssl/ssl_lib.cc +3081 -3072
  88. package/third_party/boringssl/src/ssl/ssl_test.cc +46 -0
  89. package/third_party/boringssl/src/ssl/tls13_server.cc +4 -6
  90. package/third_party/boringssl/src/util/bot/DEPS +14 -27
  91. package/third_party/boringssl/src/util/bot/UPDATING +3 -9
  92. package/third_party/boringssl/src/util/bot/update_clang.py +172 -172
  93. package/third_party/boringssl/src/util/bot/vs_toolchain.py +3 -3
  94. package/third_party/quiche/.bazelrc +8 -0
  95. package/third_party/quiche/BUILD.bazel +7 -0
  96. package/third_party/quiche/WHITESPACE +3 -1
  97. package/third_party/quiche/WORKSPACE.bazel +87 -0
  98. package/third_party/quiche/build/BUILD.bazel +7 -0
  99. package/third_party/quiche/build/source_list.bzl +1511 -0
  100. package/third_party/quiche/build/source_list.gni +1511 -0
  101. package/third_party/quiche/build/source_list.json +1511 -0
  102. package/third_party/quiche/build/test.bzl +26 -0
  103. package/third_party/quiche/build/zlib.BUILD +25 -0
  104. package/third_party/quiche/quiche/BUILD.bazel +270 -0
  105. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_enums.cc +3 -3
  106. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_enums.h +7 -7
  107. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_frame.cc +39 -18
  108. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_frame.h +11 -10
  109. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_frame_test.cc +67 -63
  110. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_headers.cc +13 -12
  111. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_headers.h +16 -8
  112. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_headers_test.cc +9 -9
  113. package/third_party/quiche/quiche/{common/balsa → balsa}/balsa_visitor_interface.h +4 -4
  114. package/third_party/quiche/quiche/{common/balsa → balsa}/framer_interface.h +3 -3
  115. package/third_party/quiche/quiche/{common/balsa → balsa}/header_api.h +3 -3
  116. package/third_party/quiche/quiche/{common/balsa → balsa}/header_properties.cc +1 -1
  117. package/third_party/quiche/quiche/{common/balsa → balsa}/header_properties.h +3 -3
  118. package/third_party/quiche/quiche/{common/balsa → balsa}/header_properties_test.cc +1 -1
  119. package/third_party/quiche/quiche/{common/balsa → balsa}/http_validation_policy.cc +1 -1
  120. package/third_party/quiche/quiche/{common/balsa → balsa}/http_validation_policy.h +3 -3
  121. package/third_party/quiche/quiche/{common/balsa → balsa}/noop_balsa_visitor.h +4 -4
  122. package/third_party/quiche/quiche/{common/balsa → balsa}/simple_buffer.cc +22 -29
  123. package/third_party/quiche/quiche/{common/balsa → balsa}/simple_buffer.h +18 -16
  124. package/third_party/quiche/quiche/{common/balsa → balsa}/simple_buffer_test.cc +79 -32
  125. package/third_party/quiche/quiche/{common/balsa → balsa}/standard_header_map.cc +1 -1
  126. package/third_party/quiche/quiche/{common/balsa → balsa}/standard_header_map.h +3 -3
  127. package/third_party/quiche/quiche/common/platform/api/quiche_command_line_flags.h +6 -1
  128. package/third_party/quiche/quiche/common/platform/api/quiche_flags.h +8 -0
  129. package/third_party/quiche/quiche/common/platform/api/quiche_logging.h +0 -3
  130. package/third_party/quiche/quiche/common/platform/api/quiche_lower_case_string_test.cc +29 -0
  131. package/third_party/quiche/quiche/common/platform/api/quiche_test.h +5 -7
  132. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quic_flags_impl.h +1 -1
  133. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_bug_tracker_impl.h +3 -2
  134. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_command_line_flags_impl.cc +41 -0
  135. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_command_line_flags_impl.h +33 -0
  136. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_expect_bug_impl.h +15 -0
  137. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_logging_impl.h +84 -50
  138. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_lower_case_string_impl.h +25 -0
  139. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_stack_trace_impl.cc +1 -1
  140. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_test_impl.cc +25 -0
  141. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_test_impl.h +56 -0
  142. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_test_output_impl.h +27 -0
  143. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_thread_impl.h +26 -0
  144. package/third_party/quiche/quiche/common/platform/default/quiche_platform_impl/quiche_url_utils_impl.cc +1 -0
  145. package/third_party/quiche/quiche/common/quiche_buffer_allocator_test.cc +1 -1
  146. package/third_party/quiche/quiche/common/quiche_mem_slice_storage.h +2 -3
  147. package/third_party/quiche/quiche/common/structured_headers.cc +14 -12
  148. package/third_party/quiche/quiche/http2/adapter/event_forwarder.cc +7 -5
  149. package/third_party/quiche/quiche/http2/adapter/event_forwarder.h +4 -2
  150. package/third_party/quiche/quiche/http2/adapter/event_forwarder_test.cc +14 -9
  151. package/third_party/quiche/quiche/http2/adapter/header_validator.cc +43 -19
  152. package/third_party/quiche/quiche/http2/adapter/header_validator.h +11 -40
  153. package/third_party/quiche/quiche/http2/adapter/header_validator_base.h +69 -0
  154. package/third_party/quiche/quiche/http2/adapter/header_validator_test.cc +73 -1
  155. package/third_party/quiche/quiche/http2/adapter/nghttp2_adapter_test.cc +66 -0
  156. package/third_party/quiche/quiche/http2/adapter/nghttp2_session_test.cc +8 -5
  157. package/third_party/quiche/quiche/http2/adapter/nghttp2_test.cc +1 -1
  158. package/third_party/quiche/quiche/http2/adapter/noop_header_validator.cc +22 -0
  159. package/third_party/quiche/quiche/http2/adapter/noop_header_validator.h +25 -0
  160. package/third_party/quiche/quiche/http2/adapter/noop_header_validator_test.cc +523 -0
  161. package/third_party/quiche/quiche/http2/adapter/oghttp2_adapter_test.cc +186 -5
  162. package/third_party/quiche/quiche/http2/adapter/oghttp2_session.cc +37 -43
  163. package/third_party/quiche/quiche/http2/adapter/oghttp2_session.h +22 -23
  164. package/third_party/quiche/quiche/http2/adapter/test_utils.cc +1 -1
  165. package/third_party/quiche/quiche/http2/adapter/window_manager_test.cc +11 -5
  166. package/third_party/quiche/quiche/http2/core/http2_trace_logging.cc +13 -11
  167. package/third_party/quiche/quiche/http2/core/http2_trace_logging.h +5 -4
  168. package/third_party/quiche/quiche/http2/core/priority_write_scheduler.h +42 -36
  169. package/third_party/quiche/quiche/http2/core/priority_write_scheduler_test.cc +2 -2
  170. package/third_party/quiche/quiche/http2/decoder/decode_buffer_test.cc +9 -9
  171. package/third_party/quiche/quiche/http2/decoder/decode_http2_structures_test.cc +1 -1
  172. package/third_party/quiche/quiche/http2/decoder/http2_frame_decoder_test.cc +14 -14
  173. package/third_party/quiche/quiche/http2/decoder/http2_structure_decoder_test.cc +17 -17
  174. package/third_party/quiche/quiche/http2/decoder/payload_decoders/altsvc_payload_decoder_test.cc +1 -1
  175. package/third_party/quiche/quiche/http2/decoder/payload_decoders/continuation_payload_decoder_test.cc +1 -1
  176. package/third_party/quiche/quiche/http2/decoder/payload_decoders/data_payload_decoder_test.cc +2 -2
  177. package/third_party/quiche/quiche/http2/decoder/payload_decoders/goaway_payload_decoder_test.cc +1 -1
  178. package/third_party/quiche/quiche/http2/decoder/payload_decoders/headers_payload_decoder_test.cc +1 -1
  179. package/third_party/quiche/quiche/http2/decoder/payload_decoders/ping_payload_decoder_test.cc +1 -1
  180. package/third_party/quiche/quiche/http2/decoder/payload_decoders/priority_payload_decoder_test.cc +1 -1
  181. package/third_party/quiche/quiche/http2/decoder/payload_decoders/priority_update_payload_decoder_test.cc +1 -1
  182. package/third_party/quiche/quiche/http2/decoder/payload_decoders/push_promise_payload_decoder_test.cc +1 -1
  183. package/third_party/quiche/quiche/http2/decoder/payload_decoders/rst_stream_payload_decoder_test.cc +1 -1
  184. package/third_party/quiche/quiche/http2/decoder/payload_decoders/settings_payload_decoder_test.cc +1 -1
  185. package/third_party/quiche/quiche/http2/decoder/payload_decoders/unknown_payload_decoder_test.cc +1 -1
  186. package/third_party/quiche/quiche/http2/decoder/payload_decoders/window_update_payload_decoder_test.cc +1 -1
  187. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_block_decoder_test.cc +2 -2
  188. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_decoder_state_test.cc +8 -8
  189. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_decoder_string_buffer_test.cc +4 -3
  190. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_decoder_tables_test.cc +17 -17
  191. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_decoder_test.cc +25 -25
  192. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_entry_decoder_test.cc +2 -2
  193. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_entry_type_decoder_test.cc +7 -6
  194. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_string_decoder_test.cc +6 -6
  195. package/third_party/quiche/quiche/http2/hpack/decoder/hpack_whole_entry_buffer_test.cc +1 -1
  196. package/third_party/quiche/quiche/http2/hpack/huffman/hpack_huffman_decoder_test.cc +2 -2
  197. package/third_party/quiche/quiche/http2/hpack/huffman/hpack_huffman_encoder_test.cc +1 -1
  198. package/third_party/quiche/quiche/http2/hpack/huffman/hpack_huffman_transcoder_test.cc +5 -5
  199. package/third_party/quiche/quiche/http2/hpack/varint/hpack_varint_decoder_test.cc +4 -4
  200. package/third_party/quiche/quiche/http2/hpack/varint/hpack_varint_round_trip_test.cc +1 -1
  201. package/third_party/quiche/quiche/http2/http2_constants_test.cc +1 -1
  202. package/third_party/quiche/quiche/http2/http2_structures_test.cc +23 -22
  203. package/third_party/quiche/quiche/http2/test_tools/frame_decoder_state_test_util.cc +1 -1
  204. package/third_party/quiche/quiche/http2/test_tools/frame_decoder_state_test_util.h +1 -1
  205. package/third_party/quiche/quiche/http2/test_tools/frame_parts.cc +38 -38
  206. package/third_party/quiche/quiche/http2/test_tools/hpack_block_collector.cc +15 -15
  207. package/third_party/quiche/quiche/http2/test_tools/hpack_entry_collector.cc +24 -22
  208. package/third_party/quiche/quiche/http2/test_tools/hpack_string_collector.cc +5 -5
  209. package/third_party/quiche/quiche/http2/test_tools/http2_frame_builder_test.cc +70 -89
  210. package/third_party/quiche/quiche/http2/test_tools/payload_decoder_base_test_util.cc +1 -1
  211. package/third_party/quiche/quiche/http2/test_tools/payload_decoder_base_test_util.h +29 -26
  212. package/third_party/quiche/quiche/http2/test_tools/{random_decoder_test.cc → random_decoder_test_base.cc} +13 -10
  213. package/third_party/quiche/quiche/http2/test_tools/{random_decoder_test.h → random_decoder_test_base.h} +15 -17
  214. package/third_party/quiche/quiche/http2/test_tools/{random_decoder_test_test.cc → random_decoder_test_base_test.cc} +1 -1
  215. package/third_party/quiche/quiche/http2/test_tools/verify_macros.h +32 -0
  216. package/third_party/quiche/quiche/quic/core/congestion_control/bbr2_sender.cc +2 -1
  217. package/third_party/quiche/quiche/quic/core/congestion_control/bbr2_simulator_test.cc +3 -2
  218. package/third_party/quiche/quiche/quic/core/congestion_control/bbr_sender_test.cc +3 -2
  219. package/third_party/quiche/quiche/quic/core/congestion_control/rtt_stats.cc +3 -2
  220. package/third_party/quiche/quiche/quic/core/congestion_control/rtt_stats.h +2 -1
  221. package/third_party/quiche/quiche/quic/core/congestion_control/rtt_stats_test.cc +2 -13
  222. package/third_party/quiche/quiche/quic/core/crypto/certificate_view.cc +15 -0
  223. package/third_party/quiche/quiche/quic/core/crypto/certificate_view.h +7 -0
  224. package/third_party/quiche/quiche/quic/core/crypto/certificate_view_test.cc +16 -0
  225. package/third_party/quiche/quiche/quic/core/crypto/crypto_protocol.h +1 -0
  226. package/third_party/quiche/quiche/quic/core/crypto/proof_source.h +2 -2
  227. package/third_party/quiche/quiche/quic/core/crypto/proof_source_x509.cc +3 -5
  228. package/third_party/quiche/quiche/quic/core/crypto/proof_source_x509.h +1 -2
  229. package/third_party/quiche/quiche/quic/core/crypto/quic_crypto_server_config.cc +1 -1
  230. package/third_party/quiche/quiche/quic/core/crypto/quic_crypto_server_config.h +3 -3
  231. package/third_party/quiche/quiche/quic/core/frames/quic_blocked_frame.cc +2 -8
  232. package/third_party/quiche/quiche/quic/core/frames/quic_blocked_frame.h +0 -1
  233. package/third_party/quiche/quiche/quic/core/frames/quic_crypto_frame.h +2 -0
  234. package/third_party/quiche/quiche/quic/core/frames/quic_frames_test.cc +2 -1
  235. package/third_party/quiche/quiche/quic/core/http/capsule.cc +2 -22
  236. package/third_party/quiche/quiche/quic/core/http/capsule.h +0 -2
  237. package/third_party/quiche/quiche/quic/core/http/capsule_test.cc +1 -2
  238. package/third_party/quiche/quiche/quic/core/http/end_to_end_test.cc +148 -57
  239. package/third_party/quiche/quiche/quic/core/http/http_constants.cc +1 -1
  240. package/third_party/quiche/quiche/quic/core/http/http_constants.h +2 -2
  241. package/third_party/quiche/quiche/quic/core/http/quic_header_list_test.cc +2 -2
  242. package/third_party/quiche/quiche/quic/core/http/quic_headers_stream_test.cc +18 -14
  243. package/third_party/quiche/quiche/quic/core/http/quic_send_control_stream.h +2 -1
  244. package/third_party/quiche/quiche/quic/core/http/quic_send_control_stream_test.cc +3 -7
  245. package/third_party/quiche/quiche/quic/core/http/quic_server_session_base.h +4 -0
  246. package/third_party/quiche/quiche/quic/core/http/quic_spdy_session.cc +47 -65
  247. package/third_party/quiche/quiche/quic/core/http/quic_spdy_session.h +5 -17
  248. package/third_party/quiche/quiche/quic/core/http/quic_spdy_session_test.cc +63 -54
  249. package/third_party/quiche/quiche/quic/core/http/quic_spdy_stream.cc +34 -91
  250. package/third_party/quiche/quiche/quic/core/http/quic_spdy_stream.h +1 -9
  251. package/third_party/quiche/quiche/quic/core/http/quic_spdy_stream_test.cc +27 -134
  252. package/third_party/quiche/quiche/quic/core/http/spdy_utils.cc +0 -41
  253. package/third_party/quiche/quiche/quic/core/http/spdy_utils.h +0 -9
  254. package/third_party/quiche/quiche/quic/core/http/spdy_utils_test.cc +0 -32
  255. package/third_party/quiche/quiche/quic/core/http/web_transport_http3.cc +0 -1
  256. package/third_party/quiche/quiche/quic/core/packet_number_indexed_queue_test.cc +2 -2
  257. package/third_party/quiche/quiche/quic/core/qpack/qpack_header_table.cc +2 -1
  258. package/third_party/quiche/quiche/quic/core/qpack/qpack_send_stream.h +2 -1
  259. package/third_party/quiche/quiche/quic/core/quic_arena_scoped_ptr_test.cc +2 -2
  260. package/third_party/quiche/quiche/quic/core/quic_connection.cc +68 -46
  261. package/third_party/quiche/quiche/quic/core/quic_connection.h +7 -6
  262. package/third_party/quiche/quiche/quic/core/quic_connection_context_test.cc +2 -2
  263. package/third_party/quiche/quiche/quic/core/quic_connection_id_manager_test.cc +2 -2
  264. package/third_party/quiche/quiche/quic/core/quic_connection_id_test.cc +2 -2
  265. package/third_party/quiche/quiche/quic/core/quic_connection_test.cc +45 -132
  266. package/third_party/quiche/quiche/quic/core/quic_constants.h +0 -7
  267. package/third_party/quiche/quiche/quic/core/quic_control_frame_manager.cc +11 -3
  268. package/third_party/quiche/quiche/quic/core/quic_control_frame_manager.h +1 -1
  269. package/third_party/quiche/quiche/quic/core/quic_control_frame_manager_test.cc +2 -2
  270. package/third_party/quiche/quiche/quic/core/quic_crypto_client_handshaker.cc +13 -1
  271. package/third_party/quiche/quiche/quic/core/quic_crypto_client_handshaker.h +3 -0
  272. package/third_party/quiche/quiche/quic/core/quic_crypto_client_handshaker_test.cc +2 -2
  273. package/third_party/quiche/quiche/quic/core/quic_crypto_client_stream.cc +6 -0
  274. package/third_party/quiche/quiche/quic/core/quic_crypto_client_stream.h +8 -0
  275. package/third_party/quiche/quiche/quic/core/quic_crypto_server_stream.cc +26 -13
  276. package/third_party/quiche/quiche/quic/core/quic_crypto_server_stream.h +5 -2
  277. package/third_party/quiche/quiche/quic/core/quic_crypto_stream.cc +55 -47
  278. package/third_party/quiche/quiche/quic/core/quic_crypto_stream.h +15 -10
  279. package/third_party/quiche/quiche/quic/core/quic_crypto_stream_test.cc +34 -5
  280. package/third_party/quiche/quiche/quic/core/quic_dispatcher.cc +20 -11
  281. package/third_party/quiche/quiche/quic/core/quic_dispatcher_test.cc +37 -13
  282. package/third_party/quiche/quiche/quic/core/quic_flags_list.h +6 -10
  283. package/third_party/quiche/quiche/quic/core/quic_flow_controller.cc +3 -3
  284. package/third_party/quiche/quiche/quic/core/quic_flow_controller.h +2 -2
  285. package/third_party/quiche/quiche/quic/core/quic_flow_controller_test.cc +15 -10
  286. package/third_party/quiche/quiche/quic/core/quic_framer.cc +1 -1
  287. package/third_party/quiche/quiche/quic/core/quic_one_block_arena.h +3 -2
  288. package/third_party/quiche/quiche/quic/core/quic_one_block_arena_test.cc +2 -2
  289. package/third_party/quiche/quiche/quic/core/quic_packet_creator.cc +3 -11
  290. package/third_party/quiche/quiche/quic/core/quic_packet_creator.h +0 -8
  291. package/third_party/quiche/quiche/quic/core/quic_ping_manager.cc +14 -14
  292. package/third_party/quiche/quiche/quic/core/quic_ping_manager_test.cc +48 -0
  293. package/third_party/quiche/quiche/quic/core/quic_received_packet_manager.cc +3 -18
  294. package/third_party/quiche/quiche/quic/core/quic_received_packet_manager_test.cc +4 -14
  295. package/third_party/quiche/quiche/quic/core/quic_server_id_test.cc +2 -2
  296. package/third_party/quiche/quiche/quic/core/quic_session.cc +4 -4
  297. package/third_party/quiche/quiche/quic/core/quic_session.h +1 -1
  298. package/third_party/quiche/quiche/quic/core/quic_session_test.cc +21 -1
  299. package/third_party/quiche/quiche/quic/core/quic_stream.cc +4 -7
  300. package/third_party/quiche/quiche/quic/core/quic_stream_test.cc +2 -4
  301. package/third_party/quiche/quiche/quic/core/quic_trace_visitor_test.cc +2 -2
  302. package/third_party/quiche/quiche/quic/core/quic_types.h +3 -8
  303. package/third_party/quiche/quiche/quic/core/quic_udp_socket_posix.cc +5 -4
  304. package/third_party/quiche/quiche/quic/core/quic_unacked_packet_map_test.cc +1 -1
  305. package/third_party/quiche/quiche/quic/core/quic_utils.cc +1 -1
  306. package/third_party/quiche/quiche/quic/core/quic_utils.h +2 -2
  307. package/third_party/quiche/quiche/quic/core/quic_versions_test.cc +0 -1
  308. package/third_party/quiche/quiche/quic/core/tls_client_handshaker.cc +13 -0
  309. package/third_party/quiche/quiche/quic/core/tls_client_handshaker.h +2 -0
  310. package/third_party/quiche/quiche/quic/core/tls_server_handshaker.cc +17 -3
  311. package/third_party/quiche/quiche/quic/core/tls_server_handshaker.h +3 -1
  312. package/third_party/quiche/quiche/quic/core/uber_received_packet_manager_test.cc +18 -10
  313. package/third_party/quiche/quiche/quic/masque/masque_client_bin.cc +6 -4
  314. package/third_party/quiche/quiche/quic/masque/masque_client_session.cc +23 -7
  315. package/third_party/quiche/quiche/quic/masque/masque_client_session.h +1 -1
  316. package/third_party/quiche/quiche/quic/masque/masque_server_backend.cc +13 -0
  317. package/third_party/quiche/quiche/quic/masque/masque_server_backend.h +2 -0
  318. package/third_party/quiche/quiche/quic/masque/masque_server_bin.cc +5 -4
  319. package/third_party/quiche/quiche/quic/masque/masque_server_session.cc +30 -40
  320. package/third_party/quiche/quiche/quic/masque/masque_server_session.h +1 -1
  321. package/third_party/quiche/quiche/quic/platform/api/quic_export.h +4 -4
  322. package/third_party/quiche/quiche/quic/platform/api/quic_flags.h +0 -1
  323. package/third_party/quiche/quiche/quic/platform/api/quic_logging.h +0 -6
  324. package/third_party/quiche/quiche/quic/platform/api/quic_test.h +6 -5
  325. package/third_party/quiche/quiche/quic/qbone/bonnet/icmp_reachable_test.cc +2 -2
  326. package/third_party/quiche/quiche/quic/qbone/bonnet/mock_qbone_tunnel.h +2 -0
  327. package/third_party/quiche/quiche/quic/qbone/bonnet/qbone_tunnel_interface.h +2 -0
  328. package/third_party/quiche/quiche/quic/qbone/bonnet/tun_device_controller_test.cc +2 -2
  329. package/third_party/quiche/quiche/quic/qbone/bonnet/tun_device_packet_exchanger_test.cc +2 -2
  330. package/third_party/quiche/quiche/quic/qbone/bonnet/tun_device_test.cc +2 -2
  331. package/third_party/quiche/quiche/quic/qbone/platform/netlink_test.cc +2 -2
  332. package/third_party/quiche/quiche/quic/qbone/platform/tcp_packet.cc +5 -4
  333. package/third_party/quiche/quiche/quic/qbone/qbone_client_session.cc +4 -2
  334. package/third_party/quiche/quiche/quic/qbone/qbone_packet_processor.cc +2 -1
  335. package/third_party/quiche/quiche/quic/qbone/qbone_packet_processor_test.cc +2 -2
  336. package/third_party/quiche/quiche/quic/qbone/qbone_server_session.cc +4 -2
  337. package/third_party/quiche/quiche/quic/qbone/qbone_session_base.cc +3 -2
  338. package/third_party/quiche/quiche/quic/qbone/qbone_stream.cc +4 -4
  339. package/third_party/quiche/quiche/quic/test_tools/quic_connection_peer.cc +1 -1
  340. package/third_party/quiche/quiche/quic/test_tools/quic_http_response_cache_data/test.example.com/index.html +63 -0
  341. package/third_party/quiche/quiche/quic/test_tools/quic_http_response_cache_data/test.example.com/map.html +65 -0
  342. package/third_party/quiche/quiche/quic/test_tools/quic_test_utils.cc +4 -3
  343. package/third_party/quiche/quiche/quic/test_tools/quic_test_utils.h +18 -1
  344. package/third_party/quiche/quiche/quic/test_tools/simulator/quic_endpoint_test.cc +1 -1
  345. package/third_party/quiche/quiche/quic/test_tools/simulator/simulator_test.cc +1 -1
  346. package/third_party/quiche/quiche/quic/test_tools/test_ticket_crypter.cc +1 -1
  347. package/third_party/quiche/quiche/quic/test_tools/test_ticket_crypter.h +2 -2
  348. package/third_party/quiche/quiche/quic/{core/crypto → tools}/crypto_message_printer_bin.cc +0 -0
  349. package/third_party/quiche/quiche/quic/{core/qpack → tools}/qpack_offline_decoder_bin.cc +0 -0
  350. package/third_party/quiche/quiche/quic/tools/quic_client_interop_test_bin.cc +4 -3
  351. package/third_party/quiche/quiche/quic/tools/quic_memory_cache_backend_test.cc +3 -31
  352. package/third_party/quiche/quiche/quic/tools/quic_packet_printer_bin.cc +2 -2
  353. package/third_party/quiche/quiche/quic/tools/quic_simple_server_session.h +1 -1
  354. package/third_party/quiche/quiche/quic/tools/quic_simple_server_session_test.cc +1 -1
  355. package/third_party/quiche/quiche/quic/tools/quic_toy_client.cc +52 -38
  356. package/third_party/quiche/quiche/quic/tools/quic_toy_server.cc +10 -7
  357. package/third_party/quiche/quiche/quic/tools/simple_ticket_crypter.cc +1 -1
  358. package/third_party/quiche/quiche/quic/tools/simple_ticket_crypter.h +1 -1
  359. package/third_party/quiche/quiche/quic/tools/web_transport_test_visitors.h +3 -2
  360. package/third_party/quiche/quiche/spdy/core/array_output_buffer.h +1 -1
  361. package/third_party/quiche/quiche/spdy/core/hpack/hpack_decoder_adapter_test.cc +1 -1
  362. package/third_party/quiche/quiche/spdy/core/hpack/hpack_encoder_test.cc +2 -1
  363. package/third_party/quiche/quiche/spdy/core/hpack/hpack_header_table_test.cc +1 -1
  364. package/third_party/quiche/quiche/spdy/core/hpack/hpack_round_trip_test.cc +2 -1
  365. package/third_party/quiche/quiche/spdy/core/hpack/hpack_static_table_test.cc +1 -1
  366. package/third_party/quiche/quiche/spdy/core/http2_frame_decoder_adapter.cc +8 -6
  367. package/third_party/quiche/quiche/spdy/core/http2_frame_decoder_adapter.h +17 -6
  368. package/third_party/quiche/quiche/spdy/core/metadata_extension.cc +1 -1
  369. package/third_party/quiche/quiche/spdy/core/metadata_extension_test.cc +1 -1
  370. package/third_party/quiche/quiche/spdy/core/spdy_framer_test.cc +21 -17
  371. package/third_party/quiche/quiche/spdy/core/spdy_intrusive_list_test.cc +3 -3
  372. package/third_party/quiche/quiche/spdy/core/spdy_no_op_visitor.h +6 -4
  373. package/third_party/quiche/quiche/spdy/core/spdy_pinnable_buffer_piece_test.cc +1 -1
  374. package/third_party/quiche/quiche/spdy/core/spdy_prefixed_buffer_reader_test.cc +1 -1
  375. package/third_party/quiche/quiche/spdy/core/spdy_protocol_test.cc +1 -1
  376. package/third_party/quiche/quiche/spdy/test_tools/mock_spdy_framer_visitor.h +5 -4
  377. package/third_party/boringssl/src/util/bot/cmake-win32.zip.sha1 +0 -1
  378. package/third_party/quiche/quiche/common/platform/api/quiche_mock_log.h +0 -18
  379. package/third_party/quiche/quiche/common/platform/api/quiche_test_helpers.h +0 -8
  380. package/third_party/quiche/quiche/quic/platform/api/quic_mock_log.h +0 -19
  381. package/third_party/quiche/quiche/quic/tools/quic_transport_simple_server_dispatcher.cc +0 -51
  382. package/third_party/quiche/quiche/quic/tools/quic_transport_simple_server_dispatcher.h +0 -41
  383. package/third_party/quiche/quiche/quic/tools/quic_transport_simple_server_session.cc +0 -168
  384. package/third_party/quiche/quiche/quic/tools/quic_transport_simple_server_session.h +0 -78
@@ -1,1472 +1,1504 @@
1
- /* ====================================================================
2
- * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
3
- *
4
- * Redistribution and use in source and binary forms, with or without
5
- * modification, are permitted provided that the following conditions
6
- * are met:
7
- *
8
- * 1. Redistributions of source code must retain the above copyright
9
- * notice, this list of conditions and the following disclaimer.
10
- *
11
- * 2. Redistributions in binary form must reproduce the above copyright
12
- * notice, this list of conditions and the following disclaimer in
13
- * the documentation and/or other materials provided with the
14
- * distribution.
15
- *
16
- * 3. All advertising materials mentioning features or use of this
17
- * software must display the following acknowledgment:
18
- * "This product includes software developed by the OpenSSL Project
19
- * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20
- *
21
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22
- * endorse or promote products derived from this software without
23
- * prior written permission. For written permission, please contact
24
- * openssl-core@openssl.org.
25
- *
26
- * 5. Products derived from this software may not be called "OpenSSL"
27
- * nor may "OpenSSL" appear in their names without prior written
28
- * permission of the OpenSSL Project.
29
- *
30
- * 6. Redistributions of any form whatsoever must retain the following
31
- * acknowledgment:
32
- * "This product includes software developed by the OpenSSL Project
33
- * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34
- *
35
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
- * OF THE POSSIBILITY OF SUCH DAMAGE.
47
- * ==================================================================== */
48
-
49
- #include <assert.h>
50
- #include <string.h>
51
-
52
- #include <openssl/aead.h>
53
- #include <openssl/aes.h>
54
- #include <openssl/cipher.h>
55
- #include <openssl/err.h>
56
- #include <openssl/mem.h>
57
- #include <openssl/nid.h>
58
- #include <openssl/rand.h>
59
-
60
- #include "internal.h"
61
- #include "../../internal.h"
62
- #include "../aes/internal.h"
63
- #include "../modes/internal.h"
64
- #include "../delocate.h"
65
-
66
-
67
- OPENSSL_MSVC_PRAGMA(warning(push))
68
- OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) // Unreachable code.
69
-
70
- #define AES_GCM_NONCE_LENGTH 12
71
-
72
- #if defined(BSAES)
73
- static void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in,
74
- uint8_t *out, size_t blocks,
75
- const AES_KEY *key,
76
- const uint8_t ivec[16]) {
77
- // |bsaes_ctr32_encrypt_blocks| is faster than |vpaes_ctr32_encrypt_blocks|,
78
- // but it takes at least one full 8-block batch to amortize the conversion.
79
- if (blocks < 8) {
80
- vpaes_ctr32_encrypt_blocks(in, out, blocks, key, ivec);
81
- return;
82
- }
83
-
84
- size_t bsaes_blocks = blocks;
85
- if (bsaes_blocks % 8 < 6) {
86
- // |bsaes_ctr32_encrypt_blocks| internally works in 8-block batches. If the
87
- // final batch is too small (under six blocks), it is faster to loop over
88
- // |vpaes_encrypt|. Round |bsaes_blocks| down to a multiple of 8.
89
- bsaes_blocks -= bsaes_blocks % 8;
90
- }
91
-
92
- AES_KEY bsaes;
93
- vpaes_encrypt_key_to_bsaes(&bsaes, key);
94
- bsaes_ctr32_encrypt_blocks(in, out, bsaes_blocks, &bsaes, ivec);
95
- OPENSSL_cleanse(&bsaes, sizeof(bsaes));
96
-
97
- in += 16 * bsaes_blocks;
98
- out += 16 * bsaes_blocks;
99
- blocks -= bsaes_blocks;
100
-
101
- union {
102
- uint32_t u32[4];
103
- uint8_t u8[16];
104
- } new_ivec;
105
- memcpy(new_ivec.u8, ivec, 16);
106
- uint32_t ctr = CRYPTO_bswap4(new_ivec.u32[3]) + bsaes_blocks;
107
- new_ivec.u32[3] = CRYPTO_bswap4(ctr);
108
-
109
- // Finish any remaining blocks with |vpaes_ctr32_encrypt_blocks|.
110
- vpaes_ctr32_encrypt_blocks(in, out, blocks, key, new_ivec.u8);
111
- }
112
- #endif // BSAES
113
-
114
- typedef struct {
115
- union {
116
- double align;
117
- AES_KEY ks;
118
- } ks;
119
- block128_f block;
120
- union {
121
- cbc128_f cbc;
122
- ctr128_f ctr;
123
- } stream;
124
- } EVP_AES_KEY;
125
-
126
- typedef struct {
127
- GCM128_CONTEXT gcm;
128
- union {
129
- double align;
130
- AES_KEY ks;
131
- } ks; // AES key schedule to use
132
- int key_set; // Set if key initialised
133
- int iv_set; // Set if an iv is set
134
- uint8_t *iv; // Temporary IV store
135
- int ivlen; // IV length
136
- int taglen;
137
- int iv_gen; // It is OK to generate IVs
138
- ctr128_f ctr;
139
- } EVP_AES_GCM_CTX;
140
-
141
- static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
142
- const uint8_t *iv, int enc) {
143
- int ret;
144
- EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
145
- const int mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
146
-
147
- if (mode == EVP_CIPH_CTR_MODE) {
148
- switch (ctx->key_len) {
149
- case 16:
150
- boringssl_fips_inc_counter(fips_counter_evp_aes_128_ctr);
151
- break;
152
-
153
- case 32:
154
- boringssl_fips_inc_counter(fips_counter_evp_aes_256_ctr);
155
- break;
156
- }
157
- }
158
-
159
- if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
160
- if (hwaes_capable()) {
161
- ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
162
- dat->block = aes_hw_decrypt;
163
- dat->stream.cbc = NULL;
164
- if (mode == EVP_CIPH_CBC_MODE) {
165
- dat->stream.cbc = aes_hw_cbc_encrypt;
166
- }
167
- } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) {
168
- assert(vpaes_capable());
169
- ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
170
- if (ret == 0) {
171
- vpaes_decrypt_key_to_bsaes(&dat->ks.ks, &dat->ks.ks);
172
- }
173
- // If |dat->stream.cbc| is provided, |dat->block| is never used.
174
- dat->block = NULL;
175
- dat->stream.cbc = bsaes_cbc_encrypt;
176
- } else if (vpaes_capable()) {
177
- ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
178
- dat->block = vpaes_decrypt;
179
- dat->stream.cbc = NULL;
180
- #if defined(VPAES_CBC)
181
- if (mode == EVP_CIPH_CBC_MODE) {
182
- dat->stream.cbc = vpaes_cbc_encrypt;
183
- }
184
- #endif
185
- } else {
186
- ret = aes_nohw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
187
- dat->block = aes_nohw_decrypt;
188
- dat->stream.cbc = NULL;
189
- if (mode == EVP_CIPH_CBC_MODE) {
190
- dat->stream.cbc = aes_nohw_cbc_encrypt;
191
- }
192
- }
193
- } else if (hwaes_capable()) {
194
- ret = aes_hw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
195
- dat->block = aes_hw_encrypt;
196
- dat->stream.cbc = NULL;
197
- if (mode == EVP_CIPH_CBC_MODE) {
198
- dat->stream.cbc = aes_hw_cbc_encrypt;
199
- } else if (mode == EVP_CIPH_CTR_MODE) {
200
- dat->stream.ctr = aes_hw_ctr32_encrypt_blocks;
201
- }
202
- } else if (vpaes_capable()) {
203
- ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
204
- dat->block = vpaes_encrypt;
205
- dat->stream.cbc = NULL;
206
- #if defined(VPAES_CBC)
207
- if (mode == EVP_CIPH_CBC_MODE) {
208
- dat->stream.cbc = vpaes_cbc_encrypt;
209
- }
210
- #endif
211
- if (mode == EVP_CIPH_CTR_MODE) {
212
- #if defined(BSAES)
213
- assert(bsaes_capable());
214
- dat->stream.ctr = vpaes_ctr32_encrypt_blocks_with_bsaes;
215
- #elif defined(VPAES_CTR32)
216
- dat->stream.ctr = vpaes_ctr32_encrypt_blocks;
217
- #endif
218
- }
219
- } else {
220
- ret = aes_nohw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
221
- dat->block = aes_nohw_encrypt;
222
- dat->stream.cbc = NULL;
223
- if (mode == EVP_CIPH_CBC_MODE) {
224
- dat->stream.cbc = aes_nohw_cbc_encrypt;
225
- }
226
- }
227
-
228
- if (ret < 0) {
229
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
230
- return 0;
231
- }
232
-
233
- return 1;
234
- }
235
-
236
- static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
237
- size_t len) {
238
- EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
239
-
240
- if (dat->stream.cbc) {
241
- (*dat->stream.cbc)(in, out, len, &dat->ks.ks, ctx->iv, ctx->encrypt);
242
- } else if (ctx->encrypt) {
243
- CRYPTO_cbc128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
244
- } else {
245
- CRYPTO_cbc128_decrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
246
- }
247
-
248
- return 1;
249
- }
250
-
251
- static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
252
- size_t len) {
253
- size_t bl = ctx->cipher->block_size;
254
- EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
255
-
256
- if (len < bl) {
257
- return 1;
258
- }
259
-
260
- len -= bl;
261
- for (size_t i = 0; i <= len; i += bl) {
262
- (*dat->block)(in + i, out + i, &dat->ks.ks);
263
- }
264
-
265
- return 1;
266
- }
267
-
268
- static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
269
- size_t len) {
270
- EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
271
-
272
- if (dat->stream.ctr) {
273
- CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
274
- &ctx->num, dat->stream.ctr);
275
- } else {
276
- CRYPTO_ctr128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
277
- &ctx->num, dat->block);
278
- }
279
- return 1;
280
- }
281
-
282
- static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
283
- size_t len) {
284
- EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
285
-
286
- CRYPTO_ofb128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, &ctx->num,
287
- dat->block);
288
- return 1;
289
- }
290
-
291
- ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
292
- block128_f *out_block, const uint8_t *key,
293
- size_t key_bytes) {
294
- if (hwaes_capable()) {
295
- aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key);
296
- if (gcm_key != NULL) {
297
- CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt, 1);
298
- }
299
- if (out_block) {
300
- *out_block = aes_hw_encrypt;
301
- }
302
- return aes_hw_ctr32_encrypt_blocks;
303
- }
304
-
305
- if (vpaes_capable()) {
306
- vpaes_set_encrypt_key(key, key_bytes * 8, aes_key);
307
- if (out_block) {
308
- *out_block = vpaes_encrypt;
309
- }
310
- if (gcm_key != NULL) {
311
- CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt, 0);
312
- }
313
- #if defined(BSAES)
314
- assert(bsaes_capable());
315
- return vpaes_ctr32_encrypt_blocks_with_bsaes;
316
- #elif defined(VPAES_CTR32)
317
- return vpaes_ctr32_encrypt_blocks;
318
- #else
319
- return NULL;
320
- #endif
321
- }
322
-
323
- aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key);
324
- if (gcm_key != NULL) {
325
- CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0);
326
- }
327
- if (out_block) {
328
- *out_block = aes_nohw_encrypt;
329
- }
330
- return aes_nohw_ctr32_encrypt_blocks;
331
- }
332
-
333
- #if defined(OPENSSL_32_BIT)
334
- #define EVP_AES_GCM_CTX_PADDING (4+8)
335
- #else
336
- #define EVP_AES_GCM_CTX_PADDING 8
337
- #endif
338
-
339
- static EVP_AES_GCM_CTX *aes_gcm_from_cipher_ctx(EVP_CIPHER_CTX *ctx) {
340
- #if defined(__GNUC__) || defined(__clang__)
341
- OPENSSL_STATIC_ASSERT(
342
- alignof(EVP_AES_GCM_CTX) <= 16,
343
- "EVP_AES_GCM_CTX needs more alignment than this function provides");
344
- #endif
345
-
346
- // |malloc| guarantees up to 4-byte alignment on 32-bit and 8-byte alignment
347
- // on 64-bit systems, so we need to adjust to reach 16-byte alignment.
348
- assert(ctx->cipher->ctx_size ==
349
- sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING);
350
-
351
- char *ptr = ctx->cipher_data;
352
- #if defined(OPENSSL_32_BIT)
353
- assert((uintptr_t)ptr % 4 == 0);
354
- ptr += (uintptr_t)ptr & 4;
355
- #endif
356
- assert((uintptr_t)ptr % 8 == 0);
357
- ptr += (uintptr_t)ptr & 8;
358
- return (EVP_AES_GCM_CTX *)ptr;
359
- }
360
-
361
- static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
362
- const uint8_t *iv, int enc) {
363
- EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(ctx);
364
- if (!iv && !key) {
365
- return 1;
366
- }
367
-
368
- switch (ctx->key_len) {
369
- case 16:
370
- boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
371
- break;
372
-
373
- case 32:
374
- boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
375
- break;
376
- }
377
-
378
- if (key) {
379
- OPENSSL_memset(&gctx->gcm, 0, sizeof(gctx->gcm));
380
- gctx->ctr = aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm.gcm_key, NULL, key,
381
- ctx->key_len);
382
- // If we have an iv can set it directly, otherwise use saved IV.
383
- if (iv == NULL && gctx->iv_set) {
384
- iv = gctx->iv;
385
- }
386
- if (iv) {
387
- CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
388
- gctx->iv_set = 1;
389
- }
390
- gctx->key_set = 1;
391
- } else {
392
- // If key set use IV, otherwise copy
393
- if (gctx->key_set) {
394
- CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
395
- } else {
396
- OPENSSL_memcpy(gctx->iv, iv, gctx->ivlen);
397
- }
398
- gctx->iv_set = 1;
399
- gctx->iv_gen = 0;
400
- }
401
- return 1;
402
- }
403
-
404
- static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) {
405
- EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(c);
406
- OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
407
- if (gctx->iv != c->iv) {
408
- OPENSSL_free(gctx->iv);
409
- }
410
- }
411
-
412
- // increment counter (64-bit int) by 1
413
- static void ctr64_inc(uint8_t *counter) {
414
- int n = 8;
415
- uint8_t c;
416
-
417
- do {
418
- --n;
419
- c = counter[n];
420
- ++c;
421
- counter[n] = c;
422
- if (c) {
423
- return;
424
- }
425
- } while (n);
426
- }
427
-
428
- static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
429
- EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(c);
430
- switch (type) {
431
- case EVP_CTRL_INIT:
432
- gctx->key_set = 0;
433
- gctx->iv_set = 0;
434
- gctx->ivlen = c->cipher->iv_len;
435
- gctx->iv = c->iv;
436
- gctx->taglen = -1;
437
- gctx->iv_gen = 0;
438
- return 1;
439
-
440
- case EVP_CTRL_AEAD_SET_IVLEN:
441
- if (arg <= 0) {
442
- return 0;
443
- }
444
-
445
- // Allocate memory for IV if needed
446
- if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) {
447
- if (gctx->iv != c->iv) {
448
- OPENSSL_free(gctx->iv);
449
- }
450
- gctx->iv = OPENSSL_malloc(arg);
451
- if (!gctx->iv) {
452
- return 0;
453
- }
454
- }
455
- gctx->ivlen = arg;
456
- return 1;
457
-
458
- case EVP_CTRL_AEAD_SET_TAG:
459
- if (arg <= 0 || arg > 16 || c->encrypt) {
460
- return 0;
461
- }
462
- OPENSSL_memcpy(c->buf, ptr, arg);
463
- gctx->taglen = arg;
464
- return 1;
465
-
466
- case EVP_CTRL_AEAD_GET_TAG:
467
- if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) {
468
- return 0;
469
- }
470
- OPENSSL_memcpy(ptr, c->buf, arg);
471
- return 1;
472
-
473
- case EVP_CTRL_AEAD_SET_IV_FIXED:
474
- // Special case: -1 length restores whole IV
475
- if (arg == -1) {
476
- OPENSSL_memcpy(gctx->iv, ptr, gctx->ivlen);
477
- gctx->iv_gen = 1;
478
- return 1;
479
- }
480
- // Fixed field must be at least 4 bytes and invocation field
481
- // at least 8.
482
- if (arg < 4 || (gctx->ivlen - arg) < 8) {
483
- return 0;
484
- }
485
- if (arg) {
486
- OPENSSL_memcpy(gctx->iv, ptr, arg);
487
- }
488
- if (c->encrypt && !RAND_bytes(gctx->iv + arg, gctx->ivlen - arg)) {
489
- return 0;
490
- }
491
- gctx->iv_gen = 1;
492
- return 1;
493
-
494
- case EVP_CTRL_GCM_IV_GEN:
495
- if (gctx->iv_gen == 0 || gctx->key_set == 0) {
496
- return 0;
497
- }
498
- CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen);
499
- if (arg <= 0 || arg > gctx->ivlen) {
500
- arg = gctx->ivlen;
501
- }
502
- OPENSSL_memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
503
- // Invocation field will be at least 8 bytes in size and
504
- // so no need to check wrap around or increment more than
505
- // last 8 bytes.
506
- ctr64_inc(gctx->iv + gctx->ivlen - 8);
507
- gctx->iv_set = 1;
508
- return 1;
509
-
510
- case EVP_CTRL_GCM_SET_IV_INV:
511
- if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) {
512
- return 0;
513
- }
514
- OPENSSL_memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
515
- CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen);
516
- gctx->iv_set = 1;
517
- return 1;
518
-
519
- case EVP_CTRL_COPY: {
520
- EVP_CIPHER_CTX *out = ptr;
521
- EVP_AES_GCM_CTX *gctx_out = aes_gcm_from_cipher_ctx(out);
522
- // |EVP_CIPHER_CTX_copy| copies this generically, but we must redo it in
523
- // case |out->cipher_data| and |in->cipher_data| are differently aligned.
524
- OPENSSL_memcpy(gctx_out, gctx, sizeof(EVP_AES_GCM_CTX));
525
- if (gctx->iv == c->iv) {
526
- gctx_out->iv = out->iv;
527
- } else {
528
- gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
529
- if (!gctx_out->iv) {
530
- return 0;
531
- }
532
- OPENSSL_memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
533
- }
534
- return 1;
535
- }
536
-
537
- default:
538
- return -1;
539
- }
540
- }
541
-
542
- static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
543
- size_t len) {
544
- EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(ctx);
545
-
546
- // If not set up, return error
547
- if (!gctx->key_set) {
548
- return -1;
549
- }
550
- if (!gctx->iv_set) {
551
- return -1;
552
- }
553
-
554
- if (in) {
555
- if (out == NULL) {
556
- if (!CRYPTO_gcm128_aad(&gctx->gcm, in, len)) {
557
- return -1;
558
- }
559
- } else if (ctx->encrypt) {
560
- if (gctx->ctr) {
561
- if (!CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len,
562
- gctx->ctr)) {
563
- return -1;
564
- }
565
- } else {
566
- if (!CRYPTO_gcm128_encrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) {
567
- return -1;
568
- }
569
- }
570
- } else {
571
- if (gctx->ctr) {
572
- if (!CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len,
573
- gctx->ctr)) {
574
- return -1;
575
- }
576
- } else {
577
- if (!CRYPTO_gcm128_decrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) {
578
- return -1;
579
- }
580
- }
581
- }
582
- return len;
583
- } else {
584
- if (!ctx->encrypt) {
585
- if (gctx->taglen < 0 ||
586
- !CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen)) {
587
- return -1;
588
- }
589
- gctx->iv_set = 0;
590
- return 0;
591
- }
592
- CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
593
- gctx->taglen = 16;
594
- // Don't reuse the IV
595
- gctx->iv_set = 0;
596
- return 0;
597
- }
598
- }
599
-
600
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_cbc_generic) {
601
- memset(out, 0, sizeof(EVP_CIPHER));
602
-
603
- out->nid = NID_aes_128_cbc;
604
- out->block_size = 16;
605
- out->key_len = 16;
606
- out->iv_len = 16;
607
- out->ctx_size = sizeof(EVP_AES_KEY);
608
- out->flags = EVP_CIPH_CBC_MODE;
609
- out->init = aes_init_key;
610
- out->cipher = aes_cbc_cipher;
611
- }
612
-
613
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ctr_generic) {
614
- memset(out, 0, sizeof(EVP_CIPHER));
615
-
616
- out->nid = NID_aes_128_ctr;
617
- out->block_size = 1;
618
- out->key_len = 16;
619
- out->iv_len = 16;
620
- out->ctx_size = sizeof(EVP_AES_KEY);
621
- out->flags = EVP_CIPH_CTR_MODE;
622
- out->init = aes_init_key;
623
- out->cipher = aes_ctr_cipher;
624
- }
625
-
626
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ecb_generic) {
627
- memset(out, 0, sizeof(EVP_CIPHER));
628
-
629
- out->nid = NID_aes_128_ecb;
630
- out->block_size = 16;
631
- out->key_len = 16;
632
- out->ctx_size = sizeof(EVP_AES_KEY);
633
- out->flags = EVP_CIPH_ECB_MODE;
634
- out->init = aes_init_key;
635
- out->cipher = aes_ecb_cipher;
636
- }
637
-
638
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ofb_generic) {
639
- memset(out, 0, sizeof(EVP_CIPHER));
640
-
641
- out->nid = NID_aes_128_ofb128;
642
- out->block_size = 1;
643
- out->key_len = 16;
644
- out->iv_len = 16;
645
- out->ctx_size = sizeof(EVP_AES_KEY);
646
- out->flags = EVP_CIPH_OFB_MODE;
647
- out->init = aes_init_key;
648
- out->cipher = aes_ofb_cipher;
649
- }
650
-
651
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_gcm_generic) {
652
- memset(out, 0, sizeof(EVP_CIPHER));
653
-
654
- out->nid = NID_aes_128_gcm;
655
- out->block_size = 1;
656
- out->key_len = 16;
657
- out->iv_len = AES_GCM_NONCE_LENGTH;
658
- out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
659
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
660
- EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
661
- EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
662
- out->init = aes_gcm_init_key;
663
- out->cipher = aes_gcm_cipher;
664
- out->cleanup = aes_gcm_cleanup;
665
- out->ctrl = aes_gcm_ctrl;
666
- }
667
-
668
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_cbc_generic) {
669
- memset(out, 0, sizeof(EVP_CIPHER));
670
-
671
- out->nid = NID_aes_192_cbc;
672
- out->block_size = 16;
673
- out->key_len = 24;
674
- out->iv_len = 16;
675
- out->ctx_size = sizeof(EVP_AES_KEY);
676
- out->flags = EVP_CIPH_CBC_MODE;
677
- out->init = aes_init_key;
678
- out->cipher = aes_cbc_cipher;
679
- }
680
-
681
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ctr_generic) {
682
- memset(out, 0, sizeof(EVP_CIPHER));
683
-
684
- out->nid = NID_aes_192_ctr;
685
- out->block_size = 1;
686
- out->key_len = 24;
687
- out->iv_len = 16;
688
- out->ctx_size = sizeof(EVP_AES_KEY);
689
- out->flags = EVP_CIPH_CTR_MODE;
690
- out->init = aes_init_key;
691
- out->cipher = aes_ctr_cipher;
692
- }
693
-
694
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ecb_generic) {
695
- memset(out, 0, sizeof(EVP_CIPHER));
696
-
697
- out->nid = NID_aes_192_ecb;
698
- out->block_size = 16;
699
- out->key_len = 24;
700
- out->ctx_size = sizeof(EVP_AES_KEY);
701
- out->flags = EVP_CIPH_ECB_MODE;
702
- out->init = aes_init_key;
703
- out->cipher = aes_ecb_cipher;
704
- }
705
-
706
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ofb_generic) {
707
- memset(out, 0, sizeof(EVP_CIPHER));
708
-
709
- out->nid = NID_aes_192_ofb128;
710
- out->block_size = 1;
711
- out->key_len = 24;
712
- out->iv_len = 16;
713
- out->ctx_size = sizeof(EVP_AES_KEY);
714
- out->flags = EVP_CIPH_OFB_MODE;
715
- out->init = aes_init_key;
716
- out->cipher = aes_ofb_cipher;
717
- }
718
-
719
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_gcm_generic) {
720
- memset(out, 0, sizeof(EVP_CIPHER));
721
-
722
- out->nid = NID_aes_192_gcm;
723
- out->block_size = 1;
724
- out->key_len = 24;
725
- out->iv_len = AES_GCM_NONCE_LENGTH;
726
- out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
727
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
728
- EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
729
- EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
730
- out->init = aes_gcm_init_key;
731
- out->cipher = aes_gcm_cipher;
732
- out->cleanup = aes_gcm_cleanup;
733
- out->ctrl = aes_gcm_ctrl;
734
- }
735
-
736
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_cbc_generic) {
737
- memset(out, 0, sizeof(EVP_CIPHER));
738
-
739
- out->nid = NID_aes_256_cbc;
740
- out->block_size = 16;
741
- out->key_len = 32;
742
- out->iv_len = 16;
743
- out->ctx_size = sizeof(EVP_AES_KEY);
744
- out->flags = EVP_CIPH_CBC_MODE;
745
- out->init = aes_init_key;
746
- out->cipher = aes_cbc_cipher;
747
- }
748
-
749
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ctr_generic) {
750
- memset(out, 0, sizeof(EVP_CIPHER));
751
-
752
- out->nid = NID_aes_256_ctr;
753
- out->block_size = 1;
754
- out->key_len = 32;
755
- out->iv_len = 16;
756
- out->ctx_size = sizeof(EVP_AES_KEY);
757
- out->flags = EVP_CIPH_CTR_MODE;
758
- out->init = aes_init_key;
759
- out->cipher = aes_ctr_cipher;
760
- }
761
-
762
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ecb_generic) {
763
- memset(out, 0, sizeof(EVP_CIPHER));
764
-
765
- out->nid = NID_aes_256_ecb;
766
- out->block_size = 16;
767
- out->key_len = 32;
768
- out->ctx_size = sizeof(EVP_AES_KEY);
769
- out->flags = EVP_CIPH_ECB_MODE;
770
- out->init = aes_init_key;
771
- out->cipher = aes_ecb_cipher;
772
- }
773
-
774
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ofb_generic) {
775
- memset(out, 0, sizeof(EVP_CIPHER));
776
-
777
- out->nid = NID_aes_256_ofb128;
778
- out->block_size = 1;
779
- out->key_len = 32;
780
- out->iv_len = 16;
781
- out->ctx_size = sizeof(EVP_AES_KEY);
782
- out->flags = EVP_CIPH_OFB_MODE;
783
- out->init = aes_init_key;
784
- out->cipher = aes_ofb_cipher;
785
- }
786
-
787
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_gcm_generic) {
788
- memset(out, 0, sizeof(EVP_CIPHER));
789
-
790
- out->nid = NID_aes_256_gcm;
791
- out->block_size = 1;
792
- out->key_len = 32;
793
- out->iv_len = AES_GCM_NONCE_LENGTH;
794
- out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
795
- out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
796
- EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
797
- EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
798
- out->init = aes_gcm_init_key;
799
- out->cipher = aes_gcm_cipher;
800
- out->cleanup = aes_gcm_cleanup;
801
- out->ctrl = aes_gcm_ctrl;
802
- }
803
-
804
- #if defined(HWAES_ECB)
805
-
806
- static int aes_hw_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
807
- const uint8_t *in, size_t len) {
808
- size_t bl = ctx->cipher->block_size;
809
-
810
- if (len < bl) {
811
- return 1;
812
- }
813
-
814
- aes_hw_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt);
815
-
816
- return 1;
817
- }
818
-
819
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_128_ecb) {
820
- memset(out, 0, sizeof(EVP_CIPHER));
821
-
822
- out->nid = NID_aes_128_ecb;
823
- out->block_size = 16;
824
- out->key_len = 16;
825
- out->ctx_size = sizeof(EVP_AES_KEY);
826
- out->flags = EVP_CIPH_ECB_MODE;
827
- out->init = aes_init_key;
828
- out->cipher = aes_hw_ecb_cipher;
829
- }
830
-
831
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_192_ecb) {
832
- memset(out, 0, sizeof(EVP_CIPHER));
833
-
834
- out->nid = NID_aes_192_ecb;
835
- out->block_size = 16;
836
- out->key_len = 24;
837
- out->ctx_size = sizeof(EVP_AES_KEY);
838
- out->flags = EVP_CIPH_ECB_MODE;
839
- out->init = aes_init_key;
840
- out->cipher = aes_hw_ecb_cipher;
841
- }
842
-
843
- DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_256_ecb) {
844
- memset(out, 0, sizeof(EVP_CIPHER));
845
-
846
- out->nid = NID_aes_256_ecb;
847
- out->block_size = 16;
848
- out->key_len = 32;
849
- out->ctx_size = sizeof(EVP_AES_KEY);
850
- out->flags = EVP_CIPH_ECB_MODE;
851
- out->init = aes_init_key;
852
- out->cipher = aes_hw_ecb_cipher;
853
- }
854
-
855
- #define EVP_ECB_CIPHER_FUNCTION(keybits) \
856
- const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
857
- if (hwaes_capable()) { \
858
- return aes_hw_##keybits##_ecb(); \
859
- } \
860
- return aes_##keybits##_ecb_generic(); \
861
- }
862
-
863
- #else
864
-
865
- #define EVP_ECB_CIPHER_FUNCTION(keybits) \
866
- const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
867
- return aes_##keybits##_ecb_generic(); \
868
- }
869
-
870
- #endif // HWAES_ECB
871
-
872
- #define EVP_CIPHER_FUNCTION(keybits, mode) \
873
- const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \
874
- return aes_##keybits##_##mode##_generic(); \
875
- }
876
-
877
- EVP_CIPHER_FUNCTION(128, cbc)
878
- EVP_CIPHER_FUNCTION(128, ctr)
879
- EVP_CIPHER_FUNCTION(128, ofb)
880
- EVP_CIPHER_FUNCTION(128, gcm)
881
-
882
- EVP_CIPHER_FUNCTION(192, cbc)
883
- EVP_CIPHER_FUNCTION(192, ctr)
884
- EVP_CIPHER_FUNCTION(192, ofb)
885
- EVP_CIPHER_FUNCTION(192, gcm)
886
-
887
- EVP_CIPHER_FUNCTION(256, cbc)
888
- EVP_CIPHER_FUNCTION(256, ctr)
889
- EVP_CIPHER_FUNCTION(256, ofb)
890
- EVP_CIPHER_FUNCTION(256, gcm)
891
-
892
- EVP_ECB_CIPHER_FUNCTION(128)
893
- EVP_ECB_CIPHER_FUNCTION(192)
894
- EVP_ECB_CIPHER_FUNCTION(256)
895
-
896
-
897
- #define EVP_AEAD_AES_GCM_TAG_LEN 16
898
-
899
- struct aead_aes_gcm_ctx {
900
- union {
901
- double align;
902
- AES_KEY ks;
903
- } ks;
904
- GCM128_KEY gcm_key;
905
- ctr128_f ctr;
906
- };
907
-
908
- static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
909
- size_t *out_tag_len, const uint8_t *key,
910
- size_t key_len, size_t tag_len) {
911
- const size_t key_bits = key_len * 8;
912
-
913
- switch (key_bits) {
914
- case 128:
915
- boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
916
- break;
917
-
918
- case 256:
919
- boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
920
- break;
921
- }
922
-
923
- if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
924
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
925
- return 0; // EVP_AEAD_CTX_init should catch this.
926
- }
927
-
928
- if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
929
- tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
930
- }
931
-
932
- if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) {
933
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
934
- return 0;
935
- }
936
-
937
- gcm_ctx->ctr =
938
- aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm_key, NULL, key, key_len);
939
- *out_tag_len = tag_len;
940
- return 1;
941
- }
942
-
943
- OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
944
- sizeof(struct aead_aes_gcm_ctx),
945
- "AEAD state is too small");
946
- #if defined(__GNUC__) || defined(__clang__)
947
- OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
948
- alignof(struct aead_aes_gcm_ctx),
949
- "AEAD state has insufficient alignment");
950
- #endif
951
-
952
- static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
953
- size_t key_len, size_t requested_tag_len) {
954
- struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
955
-
956
- size_t actual_tag_len;
957
- if (!aead_aes_gcm_init_impl(gcm_ctx, &actual_tag_len, key, key_len,
958
- requested_tag_len)) {
959
- return 0;
960
- }
961
-
962
- ctx->tag_len = actual_tag_len;
963
- return 1;
964
- }
965
-
966
- static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {}
967
-
968
- static int aead_aes_gcm_seal_scatter_impl(
969
- const struct aead_aes_gcm_ctx *gcm_ctx,
970
- uint8_t *out, uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
971
- const uint8_t *nonce, size_t nonce_len,
972
- const uint8_t *in, size_t in_len,
973
- const uint8_t *extra_in, size_t extra_in_len,
974
- const uint8_t *ad, size_t ad_len,
975
- size_t tag_len) {
976
- if (extra_in_len + tag_len < tag_len) {
977
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
978
- return 0;
979
- }
980
- if (max_out_tag_len < extra_in_len + tag_len) {
981
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
982
- return 0;
983
- }
984
- if (nonce_len == 0) {
985
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
986
- return 0;
987
- }
988
-
989
- const AES_KEY *key = &gcm_ctx->ks.ks;
990
-
991
- GCM128_CONTEXT gcm;
992
- OPENSSL_memset(&gcm, 0, sizeof(gcm));
993
- OPENSSL_memcpy(&gcm.gcm_key, &gcm_ctx->gcm_key, sizeof(gcm.gcm_key));
994
- CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
995
-
996
- if (ad_len > 0 && !CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
997
- return 0;
998
- }
999
-
1000
- if (gcm_ctx->ctr) {
1001
- if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, in, out, in_len,
1002
- gcm_ctx->ctr)) {
1003
- return 0;
1004
- }
1005
- } else {
1006
- if (!CRYPTO_gcm128_encrypt(&gcm, key, in, out, in_len)) {
1007
- return 0;
1008
- }
1009
- }
1010
-
1011
- if (extra_in_len) {
1012
- if (gcm_ctx->ctr) {
1013
- if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, extra_in, out_tag,
1014
- extra_in_len, gcm_ctx->ctr)) {
1015
- return 0;
1016
- }
1017
- } else {
1018
- if (!CRYPTO_gcm128_encrypt(&gcm, key, extra_in, out_tag, extra_in_len)) {
1019
- return 0;
1020
- }
1021
- }
1022
- }
1023
-
1024
- CRYPTO_gcm128_tag(&gcm, out_tag + extra_in_len, tag_len);
1025
- *out_tag_len = tag_len + extra_in_len;
1026
-
1027
- return 1;
1028
- }
1029
-
1030
- static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
1031
- uint8_t *out_tag, size_t *out_tag_len,
1032
- size_t max_out_tag_len,
1033
- const uint8_t *nonce, size_t nonce_len,
1034
- const uint8_t *in, size_t in_len,
1035
- const uint8_t *extra_in,
1036
- size_t extra_in_len,
1037
- const uint8_t *ad, size_t ad_len) {
1038
- const struct aead_aes_gcm_ctx *gcm_ctx =
1039
- (const struct aead_aes_gcm_ctx *)&ctx->state;
1040
- return aead_aes_gcm_seal_scatter_impl(
1041
- gcm_ctx, out, out_tag, out_tag_len, max_out_tag_len, nonce, nonce_len, in,
1042
- in_len, extra_in, extra_in_len, ad, ad_len, ctx->tag_len);
1043
- }
1044
-
1045
- static int aead_aes_gcm_open_gather_impl(const struct aead_aes_gcm_ctx *gcm_ctx,
1046
- uint8_t *out,
1047
- const uint8_t *nonce, size_t nonce_len,
1048
- const uint8_t *in, size_t in_len,
1049
- const uint8_t *in_tag,
1050
- size_t in_tag_len,
1051
- const uint8_t *ad, size_t ad_len,
1052
- size_t tag_len) {
1053
- uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
1054
-
1055
- if (nonce_len == 0) {
1056
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
1057
- return 0;
1058
- }
1059
-
1060
- if (in_tag_len != tag_len) {
1061
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
1062
- return 0;
1063
- }
1064
-
1065
- const AES_KEY *key = &gcm_ctx->ks.ks;
1066
-
1067
- GCM128_CONTEXT gcm;
1068
- OPENSSL_memset(&gcm, 0, sizeof(gcm));
1069
- OPENSSL_memcpy(&gcm.gcm_key, &gcm_ctx->gcm_key, sizeof(gcm.gcm_key));
1070
- CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
1071
-
1072
- if (!CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
1073
- return 0;
1074
- }
1075
-
1076
- if (gcm_ctx->ctr) {
1077
- if (!CRYPTO_gcm128_decrypt_ctr32(&gcm, key, in, out, in_len,
1078
- gcm_ctx->ctr)) {
1079
- return 0;
1080
- }
1081
- } else {
1082
- if (!CRYPTO_gcm128_decrypt(&gcm, key, in, out, in_len)) {
1083
- return 0;
1084
- }
1085
- }
1086
-
1087
- CRYPTO_gcm128_tag(&gcm, tag, tag_len);
1088
- if (CRYPTO_memcmp(tag, in_tag, tag_len) != 0) {
1089
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
1090
- return 0;
1091
- }
1092
-
1093
- return 1;
1094
- }
1095
-
1096
- static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
1097
- const uint8_t *nonce, size_t nonce_len,
1098
- const uint8_t *in, size_t in_len,
1099
- const uint8_t *in_tag, size_t in_tag_len,
1100
- const uint8_t *ad, size_t ad_len) {
1101
- struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *)&ctx->state;
1102
- return aead_aes_gcm_open_gather_impl(gcm_ctx, out, nonce, nonce_len, in,
1103
- in_len, in_tag, in_tag_len, ad, ad_len,
1104
- ctx->tag_len);
1105
- }
1106
-
1107
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm) {
1108
- memset(out, 0, sizeof(EVP_AEAD));
1109
-
1110
- out->key_len = 16;
1111
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1112
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1113
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1114
- out->seal_scatter_supports_extra_in = 1;
1115
-
1116
- out->init = aead_aes_gcm_init;
1117
- out->cleanup = aead_aes_gcm_cleanup;
1118
- out->seal_scatter = aead_aes_gcm_seal_scatter;
1119
- out->open_gather = aead_aes_gcm_open_gather;
1120
- }
1121
-
1122
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_192_gcm) {
1123
- memset(out, 0, sizeof(EVP_AEAD));
1124
-
1125
- out->key_len = 24;
1126
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1127
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1128
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1129
- out->seal_scatter_supports_extra_in = 1;
1130
-
1131
- out->init = aead_aes_gcm_init;
1132
- out->cleanup = aead_aes_gcm_cleanup;
1133
- out->seal_scatter = aead_aes_gcm_seal_scatter;
1134
- out->open_gather = aead_aes_gcm_open_gather;
1135
- }
1136
-
1137
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
1138
- memset(out, 0, sizeof(EVP_AEAD));
1139
-
1140
- out->key_len = 32;
1141
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1142
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1143
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1144
- out->seal_scatter_supports_extra_in = 1;
1145
-
1146
- out->init = aead_aes_gcm_init;
1147
- out->cleanup = aead_aes_gcm_cleanup;
1148
- out->seal_scatter = aead_aes_gcm_seal_scatter;
1149
- out->open_gather = aead_aes_gcm_open_gather;
1150
- }
1151
-
1152
- static int aead_aes_gcm_init_randnonce(EVP_AEAD_CTX *ctx, const uint8_t *key,
1153
- size_t key_len,
1154
- size_t requested_tag_len) {
1155
- if (requested_tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH) {
1156
- if (requested_tag_len < AES_GCM_NONCE_LENGTH) {
1157
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
1158
- return 0;
1159
- }
1160
- requested_tag_len -= AES_GCM_NONCE_LENGTH;
1161
- }
1162
-
1163
- if (!aead_aes_gcm_init(ctx, key, key_len, requested_tag_len)) {
1164
- return 0;
1165
- }
1166
-
1167
- ctx->tag_len += AES_GCM_NONCE_LENGTH;
1168
- return 1;
1169
- }
1170
-
1171
- static int aead_aes_gcm_seal_scatter_randnonce(
1172
- const EVP_AEAD_CTX *ctx,
1173
- uint8_t *out, uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
1174
- const uint8_t *external_nonce, size_t external_nonce_len,
1175
- const uint8_t *in, size_t in_len,
1176
- const uint8_t *extra_in, size_t extra_in_len,
1177
- const uint8_t *ad, size_t ad_len) {
1178
- if (external_nonce_len != 0) {
1179
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
1180
- return 0;
1181
- }
1182
-
1183
- uint8_t nonce[AES_GCM_NONCE_LENGTH];
1184
- if (max_out_tag_len < sizeof(nonce)) {
1185
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
1186
- return 0;
1187
- }
1188
-
1189
- RAND_bytes(nonce, sizeof(nonce));
1190
- const struct aead_aes_gcm_ctx *gcm_ctx =
1191
- (const struct aead_aes_gcm_ctx *)&ctx->state;
1192
- if (!aead_aes_gcm_seal_scatter_impl(gcm_ctx, out, out_tag, out_tag_len,
1193
- max_out_tag_len - AES_GCM_NONCE_LENGTH,
1194
- nonce, sizeof(nonce), in, in_len,
1195
- extra_in, extra_in_len, ad, ad_len,
1196
- ctx->tag_len - AES_GCM_NONCE_LENGTH)) {
1197
- return 0;
1198
- }
1199
-
1200
- assert(*out_tag_len + sizeof(nonce) <= max_out_tag_len);
1201
- memcpy(out_tag + *out_tag_len, nonce, sizeof(nonce));
1202
- *out_tag_len += sizeof(nonce);
1203
-
1204
- return 1;
1205
- }
1206
-
1207
- static int aead_aes_gcm_open_gather_randnonce(
1208
- const EVP_AEAD_CTX *ctx, uint8_t *out,
1209
- const uint8_t *external_nonce, size_t external_nonce_len,
1210
- const uint8_t *in, size_t in_len,
1211
- const uint8_t *in_tag, size_t in_tag_len,
1212
- const uint8_t *ad, size_t ad_len) {
1213
- if (external_nonce_len != 0) {
1214
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
1215
- return 0;
1216
- }
1217
-
1218
- if (in_tag_len < AES_GCM_NONCE_LENGTH) {
1219
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
1220
- return 0;
1221
- }
1222
- const uint8_t *nonce = in_tag + in_tag_len - AES_GCM_NONCE_LENGTH;
1223
-
1224
- const struct aead_aes_gcm_ctx *gcm_ctx =
1225
- (const struct aead_aes_gcm_ctx *)&ctx->state;
1226
- return aead_aes_gcm_open_gather_impl(
1227
- gcm_ctx, out, nonce, AES_GCM_NONCE_LENGTH, in, in_len, in_tag,
1228
- in_tag_len - AES_GCM_NONCE_LENGTH, ad, ad_len,
1229
- ctx->tag_len - AES_GCM_NONCE_LENGTH);
1230
- }
1231
-
1232
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_randnonce) {
1233
- memset(out, 0, sizeof(EVP_AEAD));
1234
-
1235
- out->key_len = 16;
1236
- out->nonce_len = 0;
1237
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1238
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1239
- out->seal_scatter_supports_extra_in = 1;
1240
-
1241
- out->init = aead_aes_gcm_init_randnonce;
1242
- out->cleanup = aead_aes_gcm_cleanup;
1243
- out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
1244
- out->open_gather = aead_aes_gcm_open_gather_randnonce;
1245
- }
1246
-
1247
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_randnonce) {
1248
- memset(out, 0, sizeof(EVP_AEAD));
1249
-
1250
- out->key_len = 32;
1251
- out->nonce_len = 0;
1252
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1253
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1254
- out->seal_scatter_supports_extra_in = 1;
1255
-
1256
- out->init = aead_aes_gcm_init_randnonce;
1257
- out->cleanup = aead_aes_gcm_cleanup;
1258
- out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
1259
- out->open_gather = aead_aes_gcm_open_gather_randnonce;
1260
- }
1261
-
1262
- struct aead_aes_gcm_tls12_ctx {
1263
- struct aead_aes_gcm_ctx gcm_ctx;
1264
- uint64_t min_next_nonce;
1265
- };
1266
-
1267
- OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1268
- sizeof(struct aead_aes_gcm_tls12_ctx),
1269
- "AEAD state is too small");
1270
- #if defined(__GNUC__) || defined(__clang__)
1271
- OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
1272
- alignof(struct aead_aes_gcm_tls12_ctx),
1273
- "AEAD state has insufficient alignment");
1274
- #endif
1275
-
1276
- static int aead_aes_gcm_tls12_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1277
- size_t key_len, size_t requested_tag_len) {
1278
- struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1279
- (struct aead_aes_gcm_tls12_ctx *) &ctx->state;
1280
-
1281
- gcm_ctx->min_next_nonce = 0;
1282
-
1283
- size_t actual_tag_len;
1284
- if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1285
- requested_tag_len)) {
1286
- return 0;
1287
- }
1288
-
1289
- ctx->tag_len = actual_tag_len;
1290
- return 1;
1291
- }
1292
-
1293
- static int aead_aes_gcm_tls12_seal_scatter(
1294
- const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1295
- size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1296
- size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1297
- size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1298
- struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1299
- (struct aead_aes_gcm_tls12_ctx *) &ctx->state;
1300
-
1301
- if (nonce_len != AES_GCM_NONCE_LENGTH) {
1302
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1303
- return 0;
1304
- }
1305
-
1306
- // The given nonces must be strictly monotonically increasing.
1307
- uint64_t given_counter;
1308
- OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
1309
- sizeof(given_counter));
1310
- given_counter = CRYPTO_bswap8(given_counter);
1311
- if (given_counter == UINT64_MAX ||
1312
- given_counter < gcm_ctx->min_next_nonce) {
1313
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1314
- return 0;
1315
- }
1316
-
1317
- gcm_ctx->min_next_nonce = given_counter + 1;
1318
-
1319
- return aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1320
- max_out_tag_len, nonce, nonce_len, in,
1321
- in_len, extra_in, extra_in_len, ad, ad_len);
1322
- }
1323
-
1324
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls12) {
1325
- memset(out, 0, sizeof(EVP_AEAD));
1326
-
1327
- out->key_len = 16;
1328
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1329
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1330
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1331
- out->seal_scatter_supports_extra_in = 1;
1332
-
1333
- out->init = aead_aes_gcm_tls12_init;
1334
- out->cleanup = aead_aes_gcm_cleanup;
1335
- out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1336
- out->open_gather = aead_aes_gcm_open_gather;
1337
- }
1338
-
1339
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls12) {
1340
- memset(out, 0, sizeof(EVP_AEAD));
1341
-
1342
- out->key_len = 32;
1343
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1344
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1345
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1346
- out->seal_scatter_supports_extra_in = 1;
1347
-
1348
- out->init = aead_aes_gcm_tls12_init;
1349
- out->cleanup = aead_aes_gcm_cleanup;
1350
- out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1351
- out->open_gather = aead_aes_gcm_open_gather;
1352
- }
1353
-
1354
- struct aead_aes_gcm_tls13_ctx {
1355
- struct aead_aes_gcm_ctx gcm_ctx;
1356
- uint64_t min_next_nonce;
1357
- uint64_t mask;
1358
- uint8_t first;
1359
- };
1360
-
1361
- OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1362
- sizeof(struct aead_aes_gcm_tls13_ctx),
1363
- "AEAD state is too small");
1364
- #if defined(__GNUC__) || defined(__clang__)
1365
- OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
1366
- alignof(struct aead_aes_gcm_tls13_ctx),
1367
- "AEAD state has insufficient alignment");
1368
- #endif
1369
-
1370
- static int aead_aes_gcm_tls13_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1371
- size_t key_len, size_t requested_tag_len) {
1372
- struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1373
- (struct aead_aes_gcm_tls13_ctx *) &ctx->state;
1374
-
1375
- gcm_ctx->min_next_nonce = 0;
1376
- gcm_ctx->first = 1;
1377
-
1378
- size_t actual_tag_len;
1379
- if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1380
- requested_tag_len)) {
1381
- return 0;
1382
- }
1383
-
1384
- ctx->tag_len = actual_tag_len;
1385
- return 1;
1386
- }
1387
-
1388
- static int aead_aes_gcm_tls13_seal_scatter(
1389
- const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1390
- size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1391
- size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1392
- size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1393
- struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1394
- (struct aead_aes_gcm_tls13_ctx *) &ctx->state;
1395
-
1396
- if (nonce_len != AES_GCM_NONCE_LENGTH) {
1397
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1398
- return 0;
1399
- }
1400
-
1401
- // The given nonces must be strictly monotonically increasing. See
1402
- // https://tools.ietf.org/html/rfc8446#section-5.3 for details of the TLS 1.3
1403
- // nonce construction.
1404
- uint64_t given_counter;
1405
- OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
1406
- sizeof(given_counter));
1407
- given_counter = CRYPTO_bswap8(given_counter);
1408
-
1409
- if (gcm_ctx->first) {
1410
- // In the first call the sequence number will be zero and therefore the
1411
- // given nonce will be 0 ^ mask = mask.
1412
- gcm_ctx->mask = given_counter;
1413
- gcm_ctx->first = 0;
1414
- }
1415
- given_counter ^= gcm_ctx->mask;
1416
-
1417
- if (given_counter == UINT64_MAX ||
1418
- given_counter < gcm_ctx->min_next_nonce) {
1419
- OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1420
- return 0;
1421
- }
1422
-
1423
- gcm_ctx->min_next_nonce = given_counter + 1;
1424
-
1425
- return aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1426
- max_out_tag_len, nonce, nonce_len, in,
1427
- in_len, extra_in, extra_in_len, ad, ad_len);
1428
- }
1429
-
1430
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls13) {
1431
- memset(out, 0, sizeof(EVP_AEAD));
1432
-
1433
- out->key_len = 16;
1434
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1435
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1436
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1437
- out->seal_scatter_supports_extra_in = 1;
1438
-
1439
- out->init = aead_aes_gcm_tls13_init;
1440
- out->cleanup = aead_aes_gcm_cleanup;
1441
- out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1442
- out->open_gather = aead_aes_gcm_open_gather;
1443
- }
1444
-
1445
- DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls13) {
1446
- memset(out, 0, sizeof(EVP_AEAD));
1447
-
1448
- out->key_len = 32;
1449
- out->nonce_len = AES_GCM_NONCE_LENGTH;
1450
- out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1451
- out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1452
- out->seal_scatter_supports_extra_in = 1;
1453
-
1454
- out->init = aead_aes_gcm_tls13_init;
1455
- out->cleanup = aead_aes_gcm_cleanup;
1456
- out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1457
- out->open_gather = aead_aes_gcm_open_gather;
1458
- }
1459
-
1460
- int EVP_has_aes_hardware(void) {
1461
- #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
1462
- return hwaes_capable() && crypto_gcm_clmul_enabled();
1463
- #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
1464
- return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable();
1465
- #elif defined(OPENSSL_PPC64LE)
1466
- return CRYPTO_is_PPC64LE_vcrypto_capable();
1467
- #else
1468
- return 0;
1469
- #endif
1470
- }
1471
-
1472
- OPENSSL_MSVC_PRAGMA(warning(pop))
1
+ /* ====================================================================
2
+ * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. All advertising materials mentioning features or use of this
17
+ * software must display the following acknowledgment:
18
+ * "This product includes software developed by the OpenSSL Project
19
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20
+ *
21
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22
+ * endorse or promote products derived from this software without
23
+ * prior written permission. For written permission, please contact
24
+ * openssl-core@openssl.org.
25
+ *
26
+ * 5. Products derived from this software may not be called "OpenSSL"
27
+ * nor may "OpenSSL" appear in their names without prior written
28
+ * permission of the OpenSSL Project.
29
+ *
30
+ * 6. Redistributions of any form whatsoever must retain the following
31
+ * acknowledgment:
32
+ * "This product includes software developed by the OpenSSL Project
33
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34
+ *
35
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
47
+ * ==================================================================== */
48
+
49
+ #include <assert.h>
50
+ #include <string.h>
51
+
52
+ #include <openssl/aead.h>
53
+ #include <openssl/aes.h>
54
+ #include <openssl/cipher.h>
55
+ #include <openssl/err.h>
56
+ #include <openssl/mem.h>
57
+ #include <openssl/nid.h>
58
+ #include <openssl/rand.h>
59
+
60
+ #include "internal.h"
61
+ #include "../../internal.h"
62
+ #include "../aes/internal.h"
63
+ #include "../modes/internal.h"
64
+ #include "../service_indicator/internal.h"
65
+ #include "../delocate.h"
66
+
67
+
68
+ OPENSSL_MSVC_PRAGMA(warning(push))
69
+ OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) // Unreachable code.
70
+
71
+ #define AES_GCM_NONCE_LENGTH 12
72
+
73
+ #if defined(BSAES)
74
+ static void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in,
75
+ uint8_t *out, size_t blocks,
76
+ const AES_KEY *key,
77
+ const uint8_t ivec[16]) {
78
+ // |bsaes_ctr32_encrypt_blocks| is faster than |vpaes_ctr32_encrypt_blocks|,
79
+ // but it takes at least one full 8-block batch to amortize the conversion.
80
+ if (blocks < 8) {
81
+ vpaes_ctr32_encrypt_blocks(in, out, blocks, key, ivec);
82
+ return;
83
+ }
84
+
85
+ size_t bsaes_blocks = blocks;
86
+ if (bsaes_blocks % 8 < 6) {
87
+ // |bsaes_ctr32_encrypt_blocks| internally works in 8-block batches. If the
88
+ // final batch is too small (under six blocks), it is faster to loop over
89
+ // |vpaes_encrypt|. Round |bsaes_blocks| down to a multiple of 8.
90
+ bsaes_blocks -= bsaes_blocks % 8;
91
+ }
92
+
93
+ AES_KEY bsaes;
94
+ vpaes_encrypt_key_to_bsaes(&bsaes, key);
95
+ bsaes_ctr32_encrypt_blocks(in, out, bsaes_blocks, &bsaes, ivec);
96
+ OPENSSL_cleanse(&bsaes, sizeof(bsaes));
97
+
98
+ in += 16 * bsaes_blocks;
99
+ out += 16 * bsaes_blocks;
100
+ blocks -= bsaes_blocks;
101
+
102
+ union {
103
+ uint32_t u32[4];
104
+ uint8_t u8[16];
105
+ } new_ivec;
106
+ memcpy(new_ivec.u8, ivec, 16);
107
+ uint32_t ctr = CRYPTO_bswap4(new_ivec.u32[3]) + bsaes_blocks;
108
+ new_ivec.u32[3] = CRYPTO_bswap4(ctr);
109
+
110
+ // Finish any remaining blocks with |vpaes_ctr32_encrypt_blocks|.
111
+ vpaes_ctr32_encrypt_blocks(in, out, blocks, key, new_ivec.u8);
112
+ }
113
+ #endif // BSAES
114
+
115
+ typedef struct {
116
+ union {
117
+ double align;
118
+ AES_KEY ks;
119
+ } ks;
120
+ block128_f block;
121
+ union {
122
+ cbc128_f cbc;
123
+ ctr128_f ctr;
124
+ } stream;
125
+ } EVP_AES_KEY;
126
+
127
+ typedef struct {
128
+ GCM128_CONTEXT gcm;
129
+ union {
130
+ double align;
131
+ AES_KEY ks;
132
+ } ks; // AES key schedule to use
133
+ int key_set; // Set if key initialised
134
+ int iv_set; // Set if an iv is set
135
+ uint8_t *iv; // Temporary IV store
136
+ int ivlen; // IV length
137
+ int taglen;
138
+ int iv_gen; // It is OK to generate IVs
139
+ ctr128_f ctr;
140
+ } EVP_AES_GCM_CTX;
141
+
142
+ static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
143
+ const uint8_t *iv, int enc) {
144
+ int ret;
145
+ EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
146
+ const int mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
147
+
148
+ if (mode == EVP_CIPH_CTR_MODE) {
149
+ switch (ctx->key_len) {
150
+ case 16:
151
+ boringssl_fips_inc_counter(fips_counter_evp_aes_128_ctr);
152
+ break;
153
+
154
+ case 32:
155
+ boringssl_fips_inc_counter(fips_counter_evp_aes_256_ctr);
156
+ break;
157
+ }
158
+ }
159
+
160
+ if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
161
+ if (hwaes_capable()) {
162
+ ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
163
+ dat->block = aes_hw_decrypt;
164
+ dat->stream.cbc = NULL;
165
+ if (mode == EVP_CIPH_CBC_MODE) {
166
+ dat->stream.cbc = aes_hw_cbc_encrypt;
167
+ }
168
+ } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) {
169
+ assert(vpaes_capable());
170
+ ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
171
+ if (ret == 0) {
172
+ vpaes_decrypt_key_to_bsaes(&dat->ks.ks, &dat->ks.ks);
173
+ }
174
+ // If |dat->stream.cbc| is provided, |dat->block| is never used.
175
+ dat->block = NULL;
176
+ dat->stream.cbc = bsaes_cbc_encrypt;
177
+ } else if (vpaes_capable()) {
178
+ ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
179
+ dat->block = vpaes_decrypt;
180
+ dat->stream.cbc = NULL;
181
+ #if defined(VPAES_CBC)
182
+ if (mode == EVP_CIPH_CBC_MODE) {
183
+ dat->stream.cbc = vpaes_cbc_encrypt;
184
+ }
185
+ #endif
186
+ } else {
187
+ ret = aes_nohw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
188
+ dat->block = aes_nohw_decrypt;
189
+ dat->stream.cbc = NULL;
190
+ if (mode == EVP_CIPH_CBC_MODE) {
191
+ dat->stream.cbc = aes_nohw_cbc_encrypt;
192
+ }
193
+ }
194
+ } else if (hwaes_capable()) {
195
+ ret = aes_hw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
196
+ dat->block = aes_hw_encrypt;
197
+ dat->stream.cbc = NULL;
198
+ if (mode == EVP_CIPH_CBC_MODE) {
199
+ dat->stream.cbc = aes_hw_cbc_encrypt;
200
+ } else if (mode == EVP_CIPH_CTR_MODE) {
201
+ dat->stream.ctr = aes_hw_ctr32_encrypt_blocks;
202
+ }
203
+ } else if (vpaes_capable()) {
204
+ ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
205
+ dat->block = vpaes_encrypt;
206
+ dat->stream.cbc = NULL;
207
+ #if defined(VPAES_CBC)
208
+ if (mode == EVP_CIPH_CBC_MODE) {
209
+ dat->stream.cbc = vpaes_cbc_encrypt;
210
+ }
211
+ #endif
212
+ if (mode == EVP_CIPH_CTR_MODE) {
213
+ #if defined(BSAES)
214
+ assert(bsaes_capable());
215
+ dat->stream.ctr = vpaes_ctr32_encrypt_blocks_with_bsaes;
216
+ #elif defined(VPAES_CTR32)
217
+ dat->stream.ctr = vpaes_ctr32_encrypt_blocks;
218
+ #endif
219
+ }
220
+ } else {
221
+ ret = aes_nohw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
222
+ dat->block = aes_nohw_encrypt;
223
+ dat->stream.cbc = NULL;
224
+ if (mode == EVP_CIPH_CBC_MODE) {
225
+ dat->stream.cbc = aes_nohw_cbc_encrypt;
226
+ }
227
+ }
228
+
229
+ if (ret < 0) {
230
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
231
+ return 0;
232
+ }
233
+
234
+ return 1;
235
+ }
236
+
237
+ static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
238
+ size_t len) {
239
+ EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
240
+
241
+ if (dat->stream.cbc) {
242
+ (*dat->stream.cbc)(in, out, len, &dat->ks.ks, ctx->iv, ctx->encrypt);
243
+ } else if (ctx->encrypt) {
244
+ CRYPTO_cbc128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
245
+ } else {
246
+ CRYPTO_cbc128_decrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
247
+ }
248
+
249
+ return 1;
250
+ }
251
+
252
+ static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
253
+ size_t len) {
254
+ size_t bl = ctx->cipher->block_size;
255
+ EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
256
+
257
+ if (len < bl) {
258
+ return 1;
259
+ }
260
+
261
+ len -= bl;
262
+ for (size_t i = 0; i <= len; i += bl) {
263
+ (*dat->block)(in + i, out + i, &dat->ks.ks);
264
+ }
265
+
266
+ return 1;
267
+ }
268
+
269
+ static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
270
+ size_t len) {
271
+ EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
272
+
273
+ if (dat->stream.ctr) {
274
+ CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
275
+ &ctx->num, dat->stream.ctr);
276
+ } else {
277
+ CRYPTO_ctr128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
278
+ &ctx->num, dat->block);
279
+ }
280
+ return 1;
281
+ }
282
+
283
+ static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
284
+ size_t len) {
285
+ EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
286
+
287
+ CRYPTO_ofb128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, &ctx->num,
288
+ dat->block);
289
+ return 1;
290
+ }
291
+
292
+ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
293
+ block128_f *out_block, const uint8_t *key,
294
+ size_t key_bytes) {
295
+ if (hwaes_capable()) {
296
+ aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key);
297
+ if (gcm_key != NULL) {
298
+ CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt, 1);
299
+ }
300
+ if (out_block) {
301
+ *out_block = aes_hw_encrypt;
302
+ }
303
+ return aes_hw_ctr32_encrypt_blocks;
304
+ }
305
+
306
+ if (vpaes_capable()) {
307
+ vpaes_set_encrypt_key(key, key_bytes * 8, aes_key);
308
+ if (out_block) {
309
+ *out_block = vpaes_encrypt;
310
+ }
311
+ if (gcm_key != NULL) {
312
+ CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt, 0);
313
+ }
314
+ #if defined(BSAES)
315
+ assert(bsaes_capable());
316
+ return vpaes_ctr32_encrypt_blocks_with_bsaes;
317
+ #elif defined(VPAES_CTR32)
318
+ return vpaes_ctr32_encrypt_blocks;
319
+ #else
320
+ return NULL;
321
+ #endif
322
+ }
323
+
324
+ aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key);
325
+ if (gcm_key != NULL) {
326
+ CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0);
327
+ }
328
+ if (out_block) {
329
+ *out_block = aes_nohw_encrypt;
330
+ }
331
+ return aes_nohw_ctr32_encrypt_blocks;
332
+ }
333
+
334
+ #if defined(OPENSSL_32_BIT)
335
+ #define EVP_AES_GCM_CTX_PADDING (4+8)
336
+ #else
337
+ #define EVP_AES_GCM_CTX_PADDING 8
338
+ #endif
339
+
340
+ static EVP_AES_GCM_CTX *aes_gcm_from_cipher_ctx(EVP_CIPHER_CTX *ctx) {
341
+ #if defined(__GNUC__) || defined(__clang__)
342
+ OPENSSL_STATIC_ASSERT(
343
+ alignof(EVP_AES_GCM_CTX) <= 16,
344
+ "EVP_AES_GCM_CTX needs more alignment than this function provides");
345
+ #endif
346
+
347
+ // |malloc| guarantees up to 4-byte alignment on 32-bit and 8-byte alignment
348
+ // on 64-bit systems, so we need to adjust to reach 16-byte alignment.
349
+ assert(ctx->cipher->ctx_size ==
350
+ sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING);
351
+
352
+ char *ptr = ctx->cipher_data;
353
+ #if defined(OPENSSL_32_BIT)
354
+ assert((uintptr_t)ptr % 4 == 0);
355
+ ptr += (uintptr_t)ptr & 4;
356
+ #endif
357
+ assert((uintptr_t)ptr % 8 == 0);
358
+ ptr += (uintptr_t)ptr & 8;
359
+ return (EVP_AES_GCM_CTX *)ptr;
360
+ }
361
+
362
+ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
363
+ const uint8_t *iv, int enc) {
364
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(ctx);
365
+ if (!iv && !key) {
366
+ return 1;
367
+ }
368
+
369
+ switch (ctx->key_len) {
370
+ case 16:
371
+ boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
372
+ break;
373
+
374
+ case 32:
375
+ boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
376
+ break;
377
+ }
378
+
379
+ if (key) {
380
+ OPENSSL_memset(&gctx->gcm, 0, sizeof(gctx->gcm));
381
+ gctx->ctr = aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm.gcm_key, NULL, key,
382
+ ctx->key_len);
383
+ // If we have an iv can set it directly, otherwise use saved IV.
384
+ if (iv == NULL && gctx->iv_set) {
385
+ iv = gctx->iv;
386
+ }
387
+ if (iv) {
388
+ CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
389
+ gctx->iv_set = 1;
390
+ }
391
+ gctx->key_set = 1;
392
+ } else {
393
+ // If key set use IV, otherwise copy
394
+ if (gctx->key_set) {
395
+ CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
396
+ } else {
397
+ OPENSSL_memcpy(gctx->iv, iv, gctx->ivlen);
398
+ }
399
+ gctx->iv_set = 1;
400
+ gctx->iv_gen = 0;
401
+ }
402
+ return 1;
403
+ }
404
+
405
+ static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) {
406
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(c);
407
+ OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
408
+ if (gctx->iv != c->iv) {
409
+ OPENSSL_free(gctx->iv);
410
+ }
411
+ }
412
+
413
+ // increment counter (64-bit int) by 1
414
+ static void ctr64_inc(uint8_t *counter) {
415
+ int n = 8;
416
+ uint8_t c;
417
+
418
+ do {
419
+ --n;
420
+ c = counter[n];
421
+ ++c;
422
+ counter[n] = c;
423
+ if (c) {
424
+ return;
425
+ }
426
+ } while (n);
427
+ }
428
+
429
+ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
430
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(c);
431
+ switch (type) {
432
+ case EVP_CTRL_INIT:
433
+ gctx->key_set = 0;
434
+ gctx->iv_set = 0;
435
+ gctx->ivlen = c->cipher->iv_len;
436
+ gctx->iv = c->iv;
437
+ gctx->taglen = -1;
438
+ gctx->iv_gen = 0;
439
+ return 1;
440
+
441
+ case EVP_CTRL_AEAD_SET_IVLEN:
442
+ if (arg <= 0) {
443
+ return 0;
444
+ }
445
+
446
+ // Allocate memory for IV if needed
447
+ if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) {
448
+ if (gctx->iv != c->iv) {
449
+ OPENSSL_free(gctx->iv);
450
+ }
451
+ gctx->iv = OPENSSL_malloc(arg);
452
+ if (!gctx->iv) {
453
+ return 0;
454
+ }
455
+ }
456
+ gctx->ivlen = arg;
457
+ return 1;
458
+
459
+ case EVP_CTRL_AEAD_SET_TAG:
460
+ if (arg <= 0 || arg > 16 || c->encrypt) {
461
+ return 0;
462
+ }
463
+ OPENSSL_memcpy(c->buf, ptr, arg);
464
+ gctx->taglen = arg;
465
+ return 1;
466
+
467
+ case EVP_CTRL_AEAD_GET_TAG:
468
+ if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) {
469
+ return 0;
470
+ }
471
+ OPENSSL_memcpy(ptr, c->buf, arg);
472
+ return 1;
473
+
474
+ case EVP_CTRL_AEAD_SET_IV_FIXED:
475
+ // Special case: -1 length restores whole IV
476
+ if (arg == -1) {
477
+ OPENSSL_memcpy(gctx->iv, ptr, gctx->ivlen);
478
+ gctx->iv_gen = 1;
479
+ return 1;
480
+ }
481
+ // Fixed field must be at least 4 bytes and invocation field
482
+ // at least 8.
483
+ if (arg < 4 || (gctx->ivlen - arg) < 8) {
484
+ return 0;
485
+ }
486
+ if (arg) {
487
+ OPENSSL_memcpy(gctx->iv, ptr, arg);
488
+ }
489
+ if (c->encrypt) {
490
+ // |RAND_bytes| calls within the fipsmodule should be wrapped with state
491
+ // lock functions to avoid updating the service indicator with the DRBG
492
+ // functions.
493
+ FIPS_service_indicator_lock_state();
494
+ RAND_bytes(gctx->iv + arg, gctx->ivlen - arg);
495
+ FIPS_service_indicator_unlock_state();
496
+ }
497
+ gctx->iv_gen = 1;
498
+ return 1;
499
+
500
+ case EVP_CTRL_GCM_IV_GEN:
501
+ if (gctx->iv_gen == 0 || gctx->key_set == 0) {
502
+ return 0;
503
+ }
504
+ CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen);
505
+ if (arg <= 0 || arg > gctx->ivlen) {
506
+ arg = gctx->ivlen;
507
+ }
508
+ OPENSSL_memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
509
+ // Invocation field will be at least 8 bytes in size and
510
+ // so no need to check wrap around or increment more than
511
+ // last 8 bytes.
512
+ ctr64_inc(gctx->iv + gctx->ivlen - 8);
513
+ gctx->iv_set = 1;
514
+ return 1;
515
+
516
+ case EVP_CTRL_GCM_SET_IV_INV:
517
+ if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) {
518
+ return 0;
519
+ }
520
+ OPENSSL_memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
521
+ CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen);
522
+ gctx->iv_set = 1;
523
+ return 1;
524
+
525
+ case EVP_CTRL_COPY: {
526
+ EVP_CIPHER_CTX *out = ptr;
527
+ EVP_AES_GCM_CTX *gctx_out = aes_gcm_from_cipher_ctx(out);
528
+ // |EVP_CIPHER_CTX_copy| copies this generically, but we must redo it in
529
+ // case |out->cipher_data| and |in->cipher_data| are differently aligned.
530
+ OPENSSL_memcpy(gctx_out, gctx, sizeof(EVP_AES_GCM_CTX));
531
+ if (gctx->iv == c->iv) {
532
+ gctx_out->iv = out->iv;
533
+ } else {
534
+ gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
535
+ if (!gctx_out->iv) {
536
+ return 0;
537
+ }
538
+ OPENSSL_memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
539
+ }
540
+ return 1;
541
+ }
542
+
543
+ default:
544
+ return -1;
545
+ }
546
+ }
547
+
548
+ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
549
+ size_t len) {
550
+ EVP_AES_GCM_CTX *gctx = aes_gcm_from_cipher_ctx(ctx);
551
+
552
+ // If not set up, return error
553
+ if (!gctx->key_set) {
554
+ return -1;
555
+ }
556
+ if (!gctx->iv_set) {
557
+ return -1;
558
+ }
559
+
560
+ if (in) {
561
+ if (out == NULL) {
562
+ if (!CRYPTO_gcm128_aad(&gctx->gcm, in, len)) {
563
+ return -1;
564
+ }
565
+ } else if (ctx->encrypt) {
566
+ if (gctx->ctr) {
567
+ if (!CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len,
568
+ gctx->ctr)) {
569
+ return -1;
570
+ }
571
+ } else {
572
+ if (!CRYPTO_gcm128_encrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) {
573
+ return -1;
574
+ }
575
+ }
576
+ } else {
577
+ if (gctx->ctr) {
578
+ if (!CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len,
579
+ gctx->ctr)) {
580
+ return -1;
581
+ }
582
+ } else {
583
+ if (!CRYPTO_gcm128_decrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) {
584
+ return -1;
585
+ }
586
+ }
587
+ }
588
+ return len;
589
+ } else {
590
+ if (!ctx->encrypt) {
591
+ if (gctx->taglen < 0 ||
592
+ !CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen)) {
593
+ return -1;
594
+ }
595
+ gctx->iv_set = 0;
596
+ return 0;
597
+ }
598
+ CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
599
+ gctx->taglen = 16;
600
+ // Don't reuse the IV
601
+ gctx->iv_set = 0;
602
+ return 0;
603
+ }
604
+ }
605
+
606
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_cbc_generic) {
607
+ memset(out, 0, sizeof(EVP_CIPHER));
608
+
609
+ out->nid = NID_aes_128_cbc;
610
+ out->block_size = 16;
611
+ out->key_len = 16;
612
+ out->iv_len = 16;
613
+ out->ctx_size = sizeof(EVP_AES_KEY);
614
+ out->flags = EVP_CIPH_CBC_MODE;
615
+ out->init = aes_init_key;
616
+ out->cipher = aes_cbc_cipher;
617
+ }
618
+
619
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ctr_generic) {
620
+ memset(out, 0, sizeof(EVP_CIPHER));
621
+
622
+ out->nid = NID_aes_128_ctr;
623
+ out->block_size = 1;
624
+ out->key_len = 16;
625
+ out->iv_len = 16;
626
+ out->ctx_size = sizeof(EVP_AES_KEY);
627
+ out->flags = EVP_CIPH_CTR_MODE;
628
+ out->init = aes_init_key;
629
+ out->cipher = aes_ctr_cipher;
630
+ }
631
+
632
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ecb_generic) {
633
+ memset(out, 0, sizeof(EVP_CIPHER));
634
+
635
+ out->nid = NID_aes_128_ecb;
636
+ out->block_size = 16;
637
+ out->key_len = 16;
638
+ out->ctx_size = sizeof(EVP_AES_KEY);
639
+ out->flags = EVP_CIPH_ECB_MODE;
640
+ out->init = aes_init_key;
641
+ out->cipher = aes_ecb_cipher;
642
+ }
643
+
644
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ofb_generic) {
645
+ memset(out, 0, sizeof(EVP_CIPHER));
646
+
647
+ out->nid = NID_aes_128_ofb128;
648
+ out->block_size = 1;
649
+ out->key_len = 16;
650
+ out->iv_len = 16;
651
+ out->ctx_size = sizeof(EVP_AES_KEY);
652
+ out->flags = EVP_CIPH_OFB_MODE;
653
+ out->init = aes_init_key;
654
+ out->cipher = aes_ofb_cipher;
655
+ }
656
+
657
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_gcm_generic) {
658
+ memset(out, 0, sizeof(EVP_CIPHER));
659
+
660
+ out->nid = NID_aes_128_gcm;
661
+ out->block_size = 1;
662
+ out->key_len = 16;
663
+ out->iv_len = AES_GCM_NONCE_LENGTH;
664
+ out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
665
+ out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
666
+ EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
667
+ EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
668
+ out->init = aes_gcm_init_key;
669
+ out->cipher = aes_gcm_cipher;
670
+ out->cleanup = aes_gcm_cleanup;
671
+ out->ctrl = aes_gcm_ctrl;
672
+ }
673
+
674
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_cbc_generic) {
675
+ memset(out, 0, sizeof(EVP_CIPHER));
676
+
677
+ out->nid = NID_aes_192_cbc;
678
+ out->block_size = 16;
679
+ out->key_len = 24;
680
+ out->iv_len = 16;
681
+ out->ctx_size = sizeof(EVP_AES_KEY);
682
+ out->flags = EVP_CIPH_CBC_MODE;
683
+ out->init = aes_init_key;
684
+ out->cipher = aes_cbc_cipher;
685
+ }
686
+
687
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ctr_generic) {
688
+ memset(out, 0, sizeof(EVP_CIPHER));
689
+
690
+ out->nid = NID_aes_192_ctr;
691
+ out->block_size = 1;
692
+ out->key_len = 24;
693
+ out->iv_len = 16;
694
+ out->ctx_size = sizeof(EVP_AES_KEY);
695
+ out->flags = EVP_CIPH_CTR_MODE;
696
+ out->init = aes_init_key;
697
+ out->cipher = aes_ctr_cipher;
698
+ }
699
+
700
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ecb_generic) {
701
+ memset(out, 0, sizeof(EVP_CIPHER));
702
+
703
+ out->nid = NID_aes_192_ecb;
704
+ out->block_size = 16;
705
+ out->key_len = 24;
706
+ out->ctx_size = sizeof(EVP_AES_KEY);
707
+ out->flags = EVP_CIPH_ECB_MODE;
708
+ out->init = aes_init_key;
709
+ out->cipher = aes_ecb_cipher;
710
+ }
711
+
712
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ofb_generic) {
713
+ memset(out, 0, sizeof(EVP_CIPHER));
714
+
715
+ out->nid = NID_aes_192_ofb128;
716
+ out->block_size = 1;
717
+ out->key_len = 24;
718
+ out->iv_len = 16;
719
+ out->ctx_size = sizeof(EVP_AES_KEY);
720
+ out->flags = EVP_CIPH_OFB_MODE;
721
+ out->init = aes_init_key;
722
+ out->cipher = aes_ofb_cipher;
723
+ }
724
+
725
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_gcm_generic) {
726
+ memset(out, 0, sizeof(EVP_CIPHER));
727
+
728
+ out->nid = NID_aes_192_gcm;
729
+ out->block_size = 1;
730
+ out->key_len = 24;
731
+ out->iv_len = AES_GCM_NONCE_LENGTH;
732
+ out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
733
+ out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
734
+ EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
735
+ EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
736
+ out->init = aes_gcm_init_key;
737
+ out->cipher = aes_gcm_cipher;
738
+ out->cleanup = aes_gcm_cleanup;
739
+ out->ctrl = aes_gcm_ctrl;
740
+ }
741
+
742
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_cbc_generic) {
743
+ memset(out, 0, sizeof(EVP_CIPHER));
744
+
745
+ out->nid = NID_aes_256_cbc;
746
+ out->block_size = 16;
747
+ out->key_len = 32;
748
+ out->iv_len = 16;
749
+ out->ctx_size = sizeof(EVP_AES_KEY);
750
+ out->flags = EVP_CIPH_CBC_MODE;
751
+ out->init = aes_init_key;
752
+ out->cipher = aes_cbc_cipher;
753
+ }
754
+
755
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ctr_generic) {
756
+ memset(out, 0, sizeof(EVP_CIPHER));
757
+
758
+ out->nid = NID_aes_256_ctr;
759
+ out->block_size = 1;
760
+ out->key_len = 32;
761
+ out->iv_len = 16;
762
+ out->ctx_size = sizeof(EVP_AES_KEY);
763
+ out->flags = EVP_CIPH_CTR_MODE;
764
+ out->init = aes_init_key;
765
+ out->cipher = aes_ctr_cipher;
766
+ }
767
+
768
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ecb_generic) {
769
+ memset(out, 0, sizeof(EVP_CIPHER));
770
+
771
+ out->nid = NID_aes_256_ecb;
772
+ out->block_size = 16;
773
+ out->key_len = 32;
774
+ out->ctx_size = sizeof(EVP_AES_KEY);
775
+ out->flags = EVP_CIPH_ECB_MODE;
776
+ out->init = aes_init_key;
777
+ out->cipher = aes_ecb_cipher;
778
+ }
779
+
780
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ofb_generic) {
781
+ memset(out, 0, sizeof(EVP_CIPHER));
782
+
783
+ out->nid = NID_aes_256_ofb128;
784
+ out->block_size = 1;
785
+ out->key_len = 32;
786
+ out->iv_len = 16;
787
+ out->ctx_size = sizeof(EVP_AES_KEY);
788
+ out->flags = EVP_CIPH_OFB_MODE;
789
+ out->init = aes_init_key;
790
+ out->cipher = aes_ofb_cipher;
791
+ }
792
+
793
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_gcm_generic) {
794
+ memset(out, 0, sizeof(EVP_CIPHER));
795
+
796
+ out->nid = NID_aes_256_gcm;
797
+ out->block_size = 1;
798
+ out->key_len = 32;
799
+ out->iv_len = AES_GCM_NONCE_LENGTH;
800
+ out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
801
+ out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
802
+ EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
803
+ EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
804
+ out->init = aes_gcm_init_key;
805
+ out->cipher = aes_gcm_cipher;
806
+ out->cleanup = aes_gcm_cleanup;
807
+ out->ctrl = aes_gcm_ctrl;
808
+ }
809
+
810
+ #if defined(HWAES_ECB)
811
+
812
+ static int aes_hw_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
813
+ const uint8_t *in, size_t len) {
814
+ size_t bl = ctx->cipher->block_size;
815
+
816
+ if (len < bl) {
817
+ return 1;
818
+ }
819
+
820
+ aes_hw_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt);
821
+
822
+ return 1;
823
+ }
824
+
825
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_128_ecb) {
826
+ memset(out, 0, sizeof(EVP_CIPHER));
827
+
828
+ out->nid = NID_aes_128_ecb;
829
+ out->block_size = 16;
830
+ out->key_len = 16;
831
+ out->ctx_size = sizeof(EVP_AES_KEY);
832
+ out->flags = EVP_CIPH_ECB_MODE;
833
+ out->init = aes_init_key;
834
+ out->cipher = aes_hw_ecb_cipher;
835
+ }
836
+
837
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_192_ecb) {
838
+ memset(out, 0, sizeof(EVP_CIPHER));
839
+
840
+ out->nid = NID_aes_192_ecb;
841
+ out->block_size = 16;
842
+ out->key_len = 24;
843
+ out->ctx_size = sizeof(EVP_AES_KEY);
844
+ out->flags = EVP_CIPH_ECB_MODE;
845
+ out->init = aes_init_key;
846
+ out->cipher = aes_hw_ecb_cipher;
847
+ }
848
+
849
+ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_256_ecb) {
850
+ memset(out, 0, sizeof(EVP_CIPHER));
851
+
852
+ out->nid = NID_aes_256_ecb;
853
+ out->block_size = 16;
854
+ out->key_len = 32;
855
+ out->ctx_size = sizeof(EVP_AES_KEY);
856
+ out->flags = EVP_CIPH_ECB_MODE;
857
+ out->init = aes_init_key;
858
+ out->cipher = aes_hw_ecb_cipher;
859
+ }
860
+
861
+ #define EVP_ECB_CIPHER_FUNCTION(keybits) \
862
+ const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
863
+ if (hwaes_capable()) { \
864
+ return aes_hw_##keybits##_ecb(); \
865
+ } \
866
+ return aes_##keybits##_ecb_generic(); \
867
+ }
868
+
869
+ #else
870
+
871
+ #define EVP_ECB_CIPHER_FUNCTION(keybits) \
872
+ const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
873
+ return aes_##keybits##_ecb_generic(); \
874
+ }
875
+
876
+ #endif // HWAES_ECB
877
+
878
+ #define EVP_CIPHER_FUNCTION(keybits, mode) \
879
+ const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \
880
+ return aes_##keybits##_##mode##_generic(); \
881
+ }
882
+
883
+ EVP_CIPHER_FUNCTION(128, cbc)
884
+ EVP_CIPHER_FUNCTION(128, ctr)
885
+ EVP_CIPHER_FUNCTION(128, ofb)
886
+ EVP_CIPHER_FUNCTION(128, gcm)
887
+
888
+ EVP_CIPHER_FUNCTION(192, cbc)
889
+ EVP_CIPHER_FUNCTION(192, ctr)
890
+ EVP_CIPHER_FUNCTION(192, ofb)
891
+ EVP_CIPHER_FUNCTION(192, gcm)
892
+
893
+ EVP_CIPHER_FUNCTION(256, cbc)
894
+ EVP_CIPHER_FUNCTION(256, ctr)
895
+ EVP_CIPHER_FUNCTION(256, ofb)
896
+ EVP_CIPHER_FUNCTION(256, gcm)
897
+
898
+ EVP_ECB_CIPHER_FUNCTION(128)
899
+ EVP_ECB_CIPHER_FUNCTION(192)
900
+ EVP_ECB_CIPHER_FUNCTION(256)
901
+
902
+
903
+ #define EVP_AEAD_AES_GCM_TAG_LEN 16
904
+
905
+ struct aead_aes_gcm_ctx {
906
+ union {
907
+ double align;
908
+ AES_KEY ks;
909
+ } ks;
910
+ GCM128_KEY gcm_key;
911
+ ctr128_f ctr;
912
+ };
913
+
914
+ static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
915
+ size_t *out_tag_len, const uint8_t *key,
916
+ size_t key_len, size_t tag_len) {
917
+ const size_t key_bits = key_len * 8;
918
+
919
+ switch (key_bits) {
920
+ case 128:
921
+ boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
922
+ break;
923
+
924
+ case 256:
925
+ boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
926
+ break;
927
+ }
928
+
929
+ if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
930
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
931
+ return 0; // EVP_AEAD_CTX_init should catch this.
932
+ }
933
+
934
+ if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
935
+ tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
936
+ }
937
+
938
+ if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) {
939
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
940
+ return 0;
941
+ }
942
+
943
+ gcm_ctx->ctr =
944
+ aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm_key, NULL, key, key_len);
945
+ *out_tag_len = tag_len;
946
+ return 1;
947
+ }
948
+
949
+ OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
950
+ sizeof(struct aead_aes_gcm_ctx),
951
+ "AEAD state is too small");
952
+ #if defined(__GNUC__) || defined(__clang__)
953
+ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
954
+ alignof(struct aead_aes_gcm_ctx),
955
+ "AEAD state has insufficient alignment");
956
+ #endif
957
+
958
+ static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
959
+ size_t key_len, size_t requested_tag_len) {
960
+ struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
961
+
962
+ size_t actual_tag_len;
963
+ if (!aead_aes_gcm_init_impl(gcm_ctx, &actual_tag_len, key, key_len,
964
+ requested_tag_len)) {
965
+ return 0;
966
+ }
967
+
968
+ ctx->tag_len = actual_tag_len;
969
+ return 1;
970
+ }
971
+
972
+ static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {}
973
+
974
+ static int aead_aes_gcm_seal_scatter_impl(
975
+ const struct aead_aes_gcm_ctx *gcm_ctx,
976
+ uint8_t *out, uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
977
+ const uint8_t *nonce, size_t nonce_len,
978
+ const uint8_t *in, size_t in_len,
979
+ const uint8_t *extra_in, size_t extra_in_len,
980
+ const uint8_t *ad, size_t ad_len,
981
+ size_t tag_len) {
982
+ if (extra_in_len + tag_len < tag_len) {
983
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
984
+ return 0;
985
+ }
986
+ if (max_out_tag_len < extra_in_len + tag_len) {
987
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
988
+ return 0;
989
+ }
990
+ if (nonce_len == 0) {
991
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
992
+ return 0;
993
+ }
994
+
995
+ const AES_KEY *key = &gcm_ctx->ks.ks;
996
+
997
+ GCM128_CONTEXT gcm;
998
+ OPENSSL_memset(&gcm, 0, sizeof(gcm));
999
+ OPENSSL_memcpy(&gcm.gcm_key, &gcm_ctx->gcm_key, sizeof(gcm.gcm_key));
1000
+ CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
1001
+
1002
+ if (ad_len > 0 && !CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
1003
+ return 0;
1004
+ }
1005
+
1006
+ if (gcm_ctx->ctr) {
1007
+ if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, in, out, in_len,
1008
+ gcm_ctx->ctr)) {
1009
+ return 0;
1010
+ }
1011
+ } else {
1012
+ if (!CRYPTO_gcm128_encrypt(&gcm, key, in, out, in_len)) {
1013
+ return 0;
1014
+ }
1015
+ }
1016
+
1017
+ if (extra_in_len) {
1018
+ if (gcm_ctx->ctr) {
1019
+ if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, extra_in, out_tag,
1020
+ extra_in_len, gcm_ctx->ctr)) {
1021
+ return 0;
1022
+ }
1023
+ } else {
1024
+ if (!CRYPTO_gcm128_encrypt(&gcm, key, extra_in, out_tag, extra_in_len)) {
1025
+ return 0;
1026
+ }
1027
+ }
1028
+ }
1029
+
1030
+ CRYPTO_gcm128_tag(&gcm, out_tag + extra_in_len, tag_len);
1031
+ *out_tag_len = tag_len + extra_in_len;
1032
+
1033
+ return 1;
1034
+ }
1035
+
1036
+ static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
1037
+ uint8_t *out_tag, size_t *out_tag_len,
1038
+ size_t max_out_tag_len,
1039
+ const uint8_t *nonce, size_t nonce_len,
1040
+ const uint8_t *in, size_t in_len,
1041
+ const uint8_t *extra_in,
1042
+ size_t extra_in_len,
1043
+ const uint8_t *ad, size_t ad_len) {
1044
+ const struct aead_aes_gcm_ctx *gcm_ctx =
1045
+ (const struct aead_aes_gcm_ctx *)&ctx->state;
1046
+ return aead_aes_gcm_seal_scatter_impl(
1047
+ gcm_ctx, out, out_tag, out_tag_len, max_out_tag_len, nonce, nonce_len, in,
1048
+ in_len, extra_in, extra_in_len, ad, ad_len, ctx->tag_len);
1049
+ }
1050
+
1051
+ static int aead_aes_gcm_open_gather_impl(const struct aead_aes_gcm_ctx *gcm_ctx,
1052
+ uint8_t *out,
1053
+ const uint8_t *nonce, size_t nonce_len,
1054
+ const uint8_t *in, size_t in_len,
1055
+ const uint8_t *in_tag,
1056
+ size_t in_tag_len,
1057
+ const uint8_t *ad, size_t ad_len,
1058
+ size_t tag_len) {
1059
+ uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
1060
+
1061
+ if (nonce_len == 0) {
1062
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
1063
+ return 0;
1064
+ }
1065
+
1066
+ if (in_tag_len != tag_len) {
1067
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
1068
+ return 0;
1069
+ }
1070
+
1071
+ const AES_KEY *key = &gcm_ctx->ks.ks;
1072
+
1073
+ GCM128_CONTEXT gcm;
1074
+ OPENSSL_memset(&gcm, 0, sizeof(gcm));
1075
+ OPENSSL_memcpy(&gcm.gcm_key, &gcm_ctx->gcm_key, sizeof(gcm.gcm_key));
1076
+ CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
1077
+
1078
+ if (!CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
1079
+ return 0;
1080
+ }
1081
+
1082
+ if (gcm_ctx->ctr) {
1083
+ if (!CRYPTO_gcm128_decrypt_ctr32(&gcm, key, in, out, in_len,
1084
+ gcm_ctx->ctr)) {
1085
+ return 0;
1086
+ }
1087
+ } else {
1088
+ if (!CRYPTO_gcm128_decrypt(&gcm, key, in, out, in_len)) {
1089
+ return 0;
1090
+ }
1091
+ }
1092
+
1093
+ CRYPTO_gcm128_tag(&gcm, tag, tag_len);
1094
+ if (CRYPTO_memcmp(tag, in_tag, tag_len) != 0) {
1095
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
1096
+ return 0;
1097
+ }
1098
+
1099
+ return 1;
1100
+ }
1101
+
1102
+ static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
1103
+ const uint8_t *nonce, size_t nonce_len,
1104
+ const uint8_t *in, size_t in_len,
1105
+ const uint8_t *in_tag, size_t in_tag_len,
1106
+ const uint8_t *ad, size_t ad_len) {
1107
+ struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *)&ctx->state;
1108
+ if (!aead_aes_gcm_open_gather_impl(gcm_ctx, out, nonce, nonce_len, in, in_len,
1109
+ in_tag, in_tag_len, ad, ad_len,
1110
+ ctx->tag_len)) {
1111
+ return 0;
1112
+ }
1113
+
1114
+ AEAD_GCM_verify_service_indicator(ctx);
1115
+ return 1;
1116
+ }
1117
+
1118
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm) {
1119
+ memset(out, 0, sizeof(EVP_AEAD));
1120
+
1121
+ out->key_len = 16;
1122
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1123
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1124
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1125
+ out->seal_scatter_supports_extra_in = 1;
1126
+
1127
+ out->init = aead_aes_gcm_init;
1128
+ out->cleanup = aead_aes_gcm_cleanup;
1129
+ out->seal_scatter = aead_aes_gcm_seal_scatter;
1130
+ out->open_gather = aead_aes_gcm_open_gather;
1131
+ }
1132
+
1133
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_192_gcm) {
1134
+ memset(out, 0, sizeof(EVP_AEAD));
1135
+
1136
+ out->key_len = 24;
1137
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1138
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1139
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1140
+ out->seal_scatter_supports_extra_in = 1;
1141
+
1142
+ out->init = aead_aes_gcm_init;
1143
+ out->cleanup = aead_aes_gcm_cleanup;
1144
+ out->seal_scatter = aead_aes_gcm_seal_scatter;
1145
+ out->open_gather = aead_aes_gcm_open_gather;
1146
+ }
1147
+
1148
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
1149
+ memset(out, 0, sizeof(EVP_AEAD));
1150
+
1151
+ out->key_len = 32;
1152
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1153
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1154
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1155
+ out->seal_scatter_supports_extra_in = 1;
1156
+
1157
+ out->init = aead_aes_gcm_init;
1158
+ out->cleanup = aead_aes_gcm_cleanup;
1159
+ out->seal_scatter = aead_aes_gcm_seal_scatter;
1160
+ out->open_gather = aead_aes_gcm_open_gather;
1161
+ }
1162
+
1163
+ static int aead_aes_gcm_init_randnonce(EVP_AEAD_CTX *ctx, const uint8_t *key,
1164
+ size_t key_len,
1165
+ size_t requested_tag_len) {
1166
+ if (requested_tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH) {
1167
+ if (requested_tag_len < AES_GCM_NONCE_LENGTH) {
1168
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
1169
+ return 0;
1170
+ }
1171
+ requested_tag_len -= AES_GCM_NONCE_LENGTH;
1172
+ }
1173
+
1174
+ if (!aead_aes_gcm_init(ctx, key, key_len, requested_tag_len)) {
1175
+ return 0;
1176
+ }
1177
+
1178
+ ctx->tag_len += AES_GCM_NONCE_LENGTH;
1179
+ return 1;
1180
+ }
1181
+
1182
+ static int aead_aes_gcm_seal_scatter_randnonce(
1183
+ const EVP_AEAD_CTX *ctx,
1184
+ uint8_t *out, uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
1185
+ const uint8_t *external_nonce, size_t external_nonce_len,
1186
+ const uint8_t *in, size_t in_len,
1187
+ const uint8_t *extra_in, size_t extra_in_len,
1188
+ const uint8_t *ad, size_t ad_len) {
1189
+ if (external_nonce_len != 0) {
1190
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
1191
+ return 0;
1192
+ }
1193
+
1194
+ uint8_t nonce[AES_GCM_NONCE_LENGTH];
1195
+ if (max_out_tag_len < sizeof(nonce)) {
1196
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
1197
+ return 0;
1198
+ }
1199
+
1200
+ // |RAND_bytes| calls within the fipsmodule should be wrapped with state lock
1201
+ // functions to avoid updating the service indicator with the DRBG functions.
1202
+ FIPS_service_indicator_lock_state();
1203
+ RAND_bytes(nonce, sizeof(nonce));
1204
+ FIPS_service_indicator_unlock_state();
1205
+
1206
+ const struct aead_aes_gcm_ctx *gcm_ctx =
1207
+ (const struct aead_aes_gcm_ctx *)&ctx->state;
1208
+ if (!aead_aes_gcm_seal_scatter_impl(gcm_ctx, out, out_tag, out_tag_len,
1209
+ max_out_tag_len - AES_GCM_NONCE_LENGTH,
1210
+ nonce, sizeof(nonce), in, in_len,
1211
+ extra_in, extra_in_len, ad, ad_len,
1212
+ ctx->tag_len - AES_GCM_NONCE_LENGTH)) {
1213
+ return 0;
1214
+ }
1215
+
1216
+ assert(*out_tag_len + sizeof(nonce) <= max_out_tag_len);
1217
+ memcpy(out_tag + *out_tag_len, nonce, sizeof(nonce));
1218
+ *out_tag_len += sizeof(nonce);
1219
+
1220
+ AEAD_GCM_verify_service_indicator(ctx);
1221
+ return 1;
1222
+ }
1223
+
1224
+ static int aead_aes_gcm_open_gather_randnonce(
1225
+ const EVP_AEAD_CTX *ctx, uint8_t *out,
1226
+ const uint8_t *external_nonce, size_t external_nonce_len,
1227
+ const uint8_t *in, size_t in_len,
1228
+ const uint8_t *in_tag, size_t in_tag_len,
1229
+ const uint8_t *ad, size_t ad_len) {
1230
+ if (external_nonce_len != 0) {
1231
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
1232
+ return 0;
1233
+ }
1234
+
1235
+ if (in_tag_len < AES_GCM_NONCE_LENGTH) {
1236
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
1237
+ return 0;
1238
+ }
1239
+ const uint8_t *nonce = in_tag + in_tag_len - AES_GCM_NONCE_LENGTH;
1240
+
1241
+ const struct aead_aes_gcm_ctx *gcm_ctx =
1242
+ (const struct aead_aes_gcm_ctx *)&ctx->state;
1243
+ if (!aead_aes_gcm_open_gather_impl(
1244
+ gcm_ctx, out, nonce, AES_GCM_NONCE_LENGTH, in, in_len, in_tag,
1245
+ in_tag_len - AES_GCM_NONCE_LENGTH, ad, ad_len,
1246
+ ctx->tag_len - AES_GCM_NONCE_LENGTH)) {
1247
+ return 0;
1248
+ }
1249
+
1250
+ AEAD_GCM_verify_service_indicator(ctx);
1251
+ return 1;
1252
+ }
1253
+
1254
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_randnonce) {
1255
+ memset(out, 0, sizeof(EVP_AEAD));
1256
+
1257
+ out->key_len = 16;
1258
+ out->nonce_len = 0;
1259
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1260
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1261
+ out->seal_scatter_supports_extra_in = 1;
1262
+
1263
+ out->init = aead_aes_gcm_init_randnonce;
1264
+ out->cleanup = aead_aes_gcm_cleanup;
1265
+ out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
1266
+ out->open_gather = aead_aes_gcm_open_gather_randnonce;
1267
+ }
1268
+
1269
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_randnonce) {
1270
+ memset(out, 0, sizeof(EVP_AEAD));
1271
+
1272
+ out->key_len = 32;
1273
+ out->nonce_len = 0;
1274
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1275
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1276
+ out->seal_scatter_supports_extra_in = 1;
1277
+
1278
+ out->init = aead_aes_gcm_init_randnonce;
1279
+ out->cleanup = aead_aes_gcm_cleanup;
1280
+ out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
1281
+ out->open_gather = aead_aes_gcm_open_gather_randnonce;
1282
+ }
1283
+
1284
+ struct aead_aes_gcm_tls12_ctx {
1285
+ struct aead_aes_gcm_ctx gcm_ctx;
1286
+ uint64_t min_next_nonce;
1287
+ };
1288
+
1289
+ OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1290
+ sizeof(struct aead_aes_gcm_tls12_ctx),
1291
+ "AEAD state is too small");
1292
+ #if defined(__GNUC__) || defined(__clang__)
1293
+ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
1294
+ alignof(struct aead_aes_gcm_tls12_ctx),
1295
+ "AEAD state has insufficient alignment");
1296
+ #endif
1297
+
1298
+ static int aead_aes_gcm_tls12_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1299
+ size_t key_len, size_t requested_tag_len) {
1300
+ struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1301
+ (struct aead_aes_gcm_tls12_ctx *) &ctx->state;
1302
+
1303
+ gcm_ctx->min_next_nonce = 0;
1304
+
1305
+ size_t actual_tag_len;
1306
+ if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1307
+ requested_tag_len)) {
1308
+ return 0;
1309
+ }
1310
+
1311
+ ctx->tag_len = actual_tag_len;
1312
+ return 1;
1313
+ }
1314
+
1315
+ static int aead_aes_gcm_tls12_seal_scatter(
1316
+ const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1317
+ size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1318
+ size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1319
+ size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1320
+ struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1321
+ (struct aead_aes_gcm_tls12_ctx *) &ctx->state;
1322
+
1323
+ if (nonce_len != AES_GCM_NONCE_LENGTH) {
1324
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1325
+ return 0;
1326
+ }
1327
+
1328
+ // The given nonces must be strictly monotonically increasing.
1329
+ uint64_t given_counter;
1330
+ OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
1331
+ sizeof(given_counter));
1332
+ given_counter = CRYPTO_bswap8(given_counter);
1333
+ if (given_counter == UINT64_MAX ||
1334
+ given_counter < gcm_ctx->min_next_nonce) {
1335
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1336
+ return 0;
1337
+ }
1338
+
1339
+ gcm_ctx->min_next_nonce = given_counter + 1;
1340
+
1341
+ if (!aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1342
+ max_out_tag_len, nonce, nonce_len, in, in_len,
1343
+ extra_in, extra_in_len, ad, ad_len)) {
1344
+ return 0;
1345
+ }
1346
+
1347
+ AEAD_GCM_verify_service_indicator(ctx);
1348
+ return 1;
1349
+ }
1350
+
1351
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls12) {
1352
+ memset(out, 0, sizeof(EVP_AEAD));
1353
+
1354
+ out->key_len = 16;
1355
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1356
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1357
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1358
+ out->seal_scatter_supports_extra_in = 1;
1359
+
1360
+ out->init = aead_aes_gcm_tls12_init;
1361
+ out->cleanup = aead_aes_gcm_cleanup;
1362
+ out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1363
+ out->open_gather = aead_aes_gcm_open_gather;
1364
+ }
1365
+
1366
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls12) {
1367
+ memset(out, 0, sizeof(EVP_AEAD));
1368
+
1369
+ out->key_len = 32;
1370
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1371
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1372
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1373
+ out->seal_scatter_supports_extra_in = 1;
1374
+
1375
+ out->init = aead_aes_gcm_tls12_init;
1376
+ out->cleanup = aead_aes_gcm_cleanup;
1377
+ out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1378
+ out->open_gather = aead_aes_gcm_open_gather;
1379
+ }
1380
+
1381
+ struct aead_aes_gcm_tls13_ctx {
1382
+ struct aead_aes_gcm_ctx gcm_ctx;
1383
+ uint64_t min_next_nonce;
1384
+ uint64_t mask;
1385
+ uint8_t first;
1386
+ };
1387
+
1388
+ OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1389
+ sizeof(struct aead_aes_gcm_tls13_ctx),
1390
+ "AEAD state is too small");
1391
+ #if defined(__GNUC__) || defined(__clang__)
1392
+ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
1393
+ alignof(struct aead_aes_gcm_tls13_ctx),
1394
+ "AEAD state has insufficient alignment");
1395
+ #endif
1396
+
1397
+ static int aead_aes_gcm_tls13_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1398
+ size_t key_len, size_t requested_tag_len) {
1399
+ struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1400
+ (struct aead_aes_gcm_tls13_ctx *) &ctx->state;
1401
+
1402
+ gcm_ctx->min_next_nonce = 0;
1403
+ gcm_ctx->first = 1;
1404
+
1405
+ size_t actual_tag_len;
1406
+ if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1407
+ requested_tag_len)) {
1408
+ return 0;
1409
+ }
1410
+
1411
+ ctx->tag_len = actual_tag_len;
1412
+ return 1;
1413
+ }
1414
+
1415
+ static int aead_aes_gcm_tls13_seal_scatter(
1416
+ const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1417
+ size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1418
+ size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1419
+ size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1420
+ struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1421
+ (struct aead_aes_gcm_tls13_ctx *) &ctx->state;
1422
+
1423
+ if (nonce_len != AES_GCM_NONCE_LENGTH) {
1424
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1425
+ return 0;
1426
+ }
1427
+
1428
+ // The given nonces must be strictly monotonically increasing. See
1429
+ // https://tools.ietf.org/html/rfc8446#section-5.3 for details of the TLS 1.3
1430
+ // nonce construction.
1431
+ uint64_t given_counter;
1432
+ OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
1433
+ sizeof(given_counter));
1434
+ given_counter = CRYPTO_bswap8(given_counter);
1435
+
1436
+ if (gcm_ctx->first) {
1437
+ // In the first call the sequence number will be zero and therefore the
1438
+ // given nonce will be 0 ^ mask = mask.
1439
+ gcm_ctx->mask = given_counter;
1440
+ gcm_ctx->first = 0;
1441
+ }
1442
+ given_counter ^= gcm_ctx->mask;
1443
+
1444
+ if (given_counter == UINT64_MAX ||
1445
+ given_counter < gcm_ctx->min_next_nonce) {
1446
+ OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1447
+ return 0;
1448
+ }
1449
+
1450
+ gcm_ctx->min_next_nonce = given_counter + 1;
1451
+
1452
+ if (!aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1453
+ max_out_tag_len, nonce, nonce_len, in, in_len,
1454
+ extra_in, extra_in_len, ad, ad_len)) {
1455
+ return 0;
1456
+ }
1457
+
1458
+ AEAD_GCM_verify_service_indicator(ctx);
1459
+ return 1;
1460
+ }
1461
+
1462
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls13) {
1463
+ memset(out, 0, sizeof(EVP_AEAD));
1464
+
1465
+ out->key_len = 16;
1466
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1467
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1468
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1469
+ out->seal_scatter_supports_extra_in = 1;
1470
+
1471
+ out->init = aead_aes_gcm_tls13_init;
1472
+ out->cleanup = aead_aes_gcm_cleanup;
1473
+ out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1474
+ out->open_gather = aead_aes_gcm_open_gather;
1475
+ }
1476
+
1477
+ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls13) {
1478
+ memset(out, 0, sizeof(EVP_AEAD));
1479
+
1480
+ out->key_len = 32;
1481
+ out->nonce_len = AES_GCM_NONCE_LENGTH;
1482
+ out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1483
+ out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1484
+ out->seal_scatter_supports_extra_in = 1;
1485
+
1486
+ out->init = aead_aes_gcm_tls13_init;
1487
+ out->cleanup = aead_aes_gcm_cleanup;
1488
+ out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1489
+ out->open_gather = aead_aes_gcm_open_gather;
1490
+ }
1491
+
1492
+ int EVP_has_aes_hardware(void) {
1493
+ #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
1494
+ return hwaes_capable() && crypto_gcm_clmul_enabled();
1495
+ #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
1496
+ return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable();
1497
+ #elif defined(OPENSSL_PPC64LE)
1498
+ return CRYPTO_is_PPC64LE_vcrypto_capable();
1499
+ #else
1500
+ return 0;
1501
+ #endif
1502
+ }
1503
+
1504
+ OPENSSL_MSVC_PRAGMA(warning(pop))